1 /* radio-aztech.c - Aztech radio card driver for Linux 2.2
3 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
4 * Adapted to support the Video for Linux API by
5 * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by:
9 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
10 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
11 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
13 * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
14 * along with more information on the card itself.
17 * 1999-02-24 Russell Kroll <rkroll@exploits.org>
18 * Fine tuning/VIDEO_TUNER_LOW
19 * Range expanded to 87-108 MHz (from 87.9-107.8)
21 * Notable changes from the original source:
22 * - includes stripped down to the essentials
23 * - for loops used as delays replaced with udelay()
24 * - #defines removed, changed to static values
25 * - tuning structure changed - no more character arrays, other changes
28 #include <linux/module.h> /* Modules */
29 #include <linux/init.h> /* Initdata */
30 #include <linux/ioport.h> /* request_region */
31 #include <linux/delay.h> /* udelay */
32 #include <linux/videodev2.h> /* kernel radio structs */
33 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
34 #include <linux/io.h> /* outb, outb_p */
35 #include <linux/uaccess.h> /* copy to/from user */
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-ioctl.h>
39 MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
40 MODULE_DESCRIPTION("A driver for the Aztech radio card.");
41 MODULE_LICENSE("GPL");
43 /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
45 #ifndef CONFIG_RADIO_AZTECH_PORT
46 #define CONFIG_RADIO_AZTECH_PORT -1
49 static int io = CONFIG_RADIO_AZTECH_PORT;
50 static int radio_nr = -1;
51 static int radio_wait_time = 1000;
53 module_param(io, int, 0);
54 module_param(radio_nr, int, 0);
55 MODULE_PARM_DESC(io, "I/O address of the Aztech card (0x350 or 0x358)");
57 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
61 struct v4l2_device v4l2_dev;
62 struct video_device vdev;
65 unsigned long curfreq;
70 static struct aztech aztech_card;
72 static int volconvert(int level)
74 level >>= 14; /* Map 16bits down to 2 bit */
77 /* convert to card-friendly values */
88 return 0; /* Quieten gcc */
91 static void send_0_byte(struct aztech *az)
93 udelay(radio_wait_time);
94 outb_p(2 + volconvert(az->curvol), az->io);
95 outb_p(64 + 2 + volconvert(az->curvol), az->io);
98 static void send_1_byte(struct aztech *az)
100 udelay (radio_wait_time);
101 outb_p(128 + 2 + volconvert(az->curvol), az->io);
102 outb_p(128 + 64 + 2 + volconvert(az->curvol), az->io);
105 static int az_setvol(struct aztech *az, int vol)
107 mutex_lock(&az->lock);
108 outb(volconvert(vol), az->io);
109 mutex_unlock(&az->lock);
113 /* thanks to Michael Dwyer for giving me a dose of clues in
114 * the signal strength department..
116 * This card has a stereo bit - bit 0 set = mono, not set = stereo
117 * It also has a "signal" bit - bit 1 set = bad signal, not set = good
121 static int az_getsigstr(struct aztech *az)
125 mutex_lock(&az->lock);
126 if (inb(az->io) & 2) /* bit set = no signal present */
128 mutex_unlock(&az->lock);
132 static int az_getstereo(struct aztech *az)
136 mutex_lock(&az->lock);
137 if (inb(az->io) & 1) /* bit set = mono */
139 mutex_unlock(&az->lock);
143 static int az_setfreq(struct aztech *az, unsigned long frequency)
147 mutex_lock(&az->lock);
149 az->curfreq = frequency;
150 frequency += 171200; /* Add 10.7 MHz IF */
151 frequency /= 800; /* Convert to 50 kHz units */
153 send_0_byte(az); /* 0: LSB of frequency */
155 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
156 if (frequency & (1 << i))
161 send_0_byte(az); /* 14: test bit - always 0 */
162 send_0_byte(az); /* 15: test bit - always 0 */
163 send_0_byte(az); /* 16: band data 0 - always 0 */
164 if (az->stereo) /* 17: stereo (1 to enable) */
169 send_1_byte(az); /* 18: band data 1 - unknown */
170 send_0_byte(az); /* 19: time base - always 0 */
171 send_0_byte(az); /* 20: spacing (0 = 25 kHz) */
172 send_1_byte(az); /* 21: spacing (1 = 25 kHz) */
173 send_0_byte(az); /* 22: spacing (0 = 25 kHz) */
174 send_1_byte(az); /* 23: AM/FM (FM = 1, always) */
176 /* latch frequency */
178 udelay(radio_wait_time);
179 outb_p(128 + 64 + volconvert(az->curvol), az->io);
181 mutex_unlock(&az->lock);
186 static int vidioc_querycap(struct file *file, void *priv,
187 struct v4l2_capability *v)
189 strlcpy(v->driver, "radio-aztech", sizeof(v->driver));
190 strlcpy(v->card, "Aztech Radio", sizeof(v->card));
191 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
192 v->version = RADIO_VERSION;
193 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
197 static int vidioc_g_tuner(struct file *file, void *priv,
198 struct v4l2_tuner *v)
200 struct aztech *az = video_drvdata(file);
205 strlcpy(v->name, "FM", sizeof(v->name));
206 v->type = V4L2_TUNER_RADIO;
208 v->rangelow = 87 * 16000;
209 v->rangehigh = 108 * 16000;
210 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
211 v->capability = V4L2_TUNER_CAP_LOW;
212 if (az_getstereo(az))
213 v->audmode = V4L2_TUNER_MODE_STEREO;
215 v->audmode = V4L2_TUNER_MODE_MONO;
216 v->signal = 0xFFFF * az_getsigstr(az);
221 static int vidioc_s_tuner(struct file *file, void *priv,
222 struct v4l2_tuner *v)
224 return v->index ? -EINVAL : 0;
227 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
233 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
235 return i ? -EINVAL : 0;
238 static int vidioc_g_audio(struct file *file, void *priv,
239 struct v4l2_audio *a)
242 strlcpy(a->name, "Radio", sizeof(a->name));
243 a->capability = V4L2_AUDCAP_STEREO;
247 static int vidioc_s_audio(struct file *file, void *priv,
248 struct v4l2_audio *a)
250 return a->index ? -EINVAL : 0;
253 static int vidioc_s_frequency(struct file *file, void *priv,
254 struct v4l2_frequency *f)
256 struct aztech *az = video_drvdata(file);
258 az_setfreq(az, f->frequency);
262 static int vidioc_g_frequency(struct file *file, void *priv,
263 struct v4l2_frequency *f)
265 struct aztech *az = video_drvdata(file);
267 f->type = V4L2_TUNER_RADIO;
268 f->frequency = az->curfreq;
272 static int vidioc_queryctrl(struct file *file, void *priv,
273 struct v4l2_queryctrl *qc)
276 case V4L2_CID_AUDIO_MUTE:
277 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
278 case V4L2_CID_AUDIO_VOLUME:
279 return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
284 static int vidioc_g_ctrl(struct file *file, void *priv,
285 struct v4l2_control *ctrl)
287 struct aztech *az = video_drvdata(file);
290 case V4L2_CID_AUDIO_MUTE:
296 case V4L2_CID_AUDIO_VOLUME:
297 ctrl->value = az->curvol * 6554;
303 static int vidioc_s_ctrl(struct file *file, void *priv,
304 struct v4l2_control *ctrl)
306 struct aztech *az = video_drvdata(file);
309 case V4L2_CID_AUDIO_MUTE:
313 az_setvol(az, az->curvol);
315 case V4L2_CID_AUDIO_VOLUME:
316 az_setvol(az, ctrl->value);
322 static int aztech_open(struct file *file)
327 static int aztech_release(struct file *file)
332 static const struct v4l2_file_operations aztech_fops = {
333 .owner = THIS_MODULE,
335 .release = aztech_release,
336 .ioctl = video_ioctl2,
339 static const struct v4l2_ioctl_ops aztech_ioctl_ops = {
340 .vidioc_querycap = vidioc_querycap,
341 .vidioc_g_tuner = vidioc_g_tuner,
342 .vidioc_s_tuner = vidioc_s_tuner,
343 .vidioc_g_audio = vidioc_g_audio,
344 .vidioc_s_audio = vidioc_s_audio,
345 .vidioc_g_input = vidioc_g_input,
346 .vidioc_s_input = vidioc_s_input,
347 .vidioc_g_frequency = vidioc_g_frequency,
348 .vidioc_s_frequency = vidioc_s_frequency,
349 .vidioc_queryctrl = vidioc_queryctrl,
350 .vidioc_g_ctrl = vidioc_g_ctrl,
351 .vidioc_s_ctrl = vidioc_s_ctrl,
354 static int __init aztech_init(void)
356 struct aztech *az = &aztech_card;
357 struct v4l2_device *v4l2_dev = &az->v4l2_dev;
360 strlcpy(v4l2_dev->name, "aztech", sizeof(v4l2_dev->name));
364 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x350 or 0x358\n");
368 if (!request_region(az->io, 2, "aztech")) {
369 v4l2_err(v4l2_dev, "port 0x%x already in use\n", az->io);
373 res = v4l2_device_register(NULL, v4l2_dev);
375 release_region(az->io, 2);
376 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
380 mutex_init(&az->lock);
381 strlcpy(az->vdev.name, v4l2_dev->name, sizeof(az->vdev.name));
382 az->vdev.v4l2_dev = v4l2_dev;
383 az->vdev.fops = &aztech_fops;
384 az->vdev.ioctl_ops = &aztech_ioctl_ops;
385 az->vdev.release = video_device_release_empty;
386 video_set_drvdata(&az->vdev, az);
388 if (video_register_device(&az->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
389 v4l2_device_unregister(v4l2_dev);
390 release_region(az->io, 2);
394 v4l2_info(v4l2_dev, "Aztech radio card driver v1.00/19990224 rkroll@exploits.org\n");
395 /* mute card - prevents noisy bootups */
400 static void __exit aztech_exit(void)
402 struct aztech *az = &aztech_card;
404 video_unregister_device(&az->vdev);
405 v4l2_device_unregister(&az->v4l2_dev);
406 release_region(az->io, 2);
409 module_init(aztech_init);
410 module_exit(aztech_exit);