1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/ioport.h>
36#include <linux/delay.h>
37#include <linux/videodev2.h>
38#include <linux/param.h>
39#include <linux/pnp.h>
40#include <linux/sched.h>
41#include <linux/io.h>
42#include <media/v4l2-device.h>
43#include <media/v4l2-ioctl.h>
44#include <media/v4l2-ctrls.h>
45#include <media/v4l2-fh.h>
46#include <media/v4l2-event.h>
47
48MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
49MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
50MODULE_LICENSE("GPL");
51MODULE_VERSION("0.3.4");
52
53static int io = -1;
54static int radio_nr = -1;
55
56module_param(io, int, 0);
57MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
58module_param(radio_nr, int, 0);
59
60#define RDS_BUFFER 256
61#define RDS_RX_FLAG 1
62#define MBS_RX_FLAG 2
63
64struct cadet {
65 struct v4l2_device v4l2_dev;
66 struct video_device vdev;
67 struct v4l2_ctrl_handler ctrl_handler;
68 int io;
69 bool is_fm_band;
70 u32 curfreq;
71 int tunestat;
72 int sigstrength;
73 wait_queue_head_t read_queue;
74 struct timer_list readtimer;
75 u8 rdsin, rdsout, rdsstat;
76 unsigned char rdsbuf[RDS_BUFFER];
77 struct mutex lock;
78 int reading;
79};
80
81static struct cadet cadet_card;
82
83
84
85
86
87
88static u16 sigtable[2][4] = {
89 { 1835, 2621, 4128, 65535 },
90 { 2185, 4369, 13107, 65535 },
91};
92
93static const struct v4l2_frequency_band bands[] = {
94 {
95 .index = 0,
96 .type = V4L2_TUNER_RADIO,
97 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
98 .rangelow = 8320,
99 .rangehigh = 26400,
100 .modulation = V4L2_BAND_MODULATION_AM,
101 }, {
102 .index = 1,
103 .type = V4L2_TUNER_RADIO,
104 .capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
105 V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW |
106 V4L2_TUNER_CAP_FREQ_BANDS,
107 .rangelow = 1400000,
108 .rangehigh = 1728000,
109 .modulation = V4L2_BAND_MODULATION_FM,
110 },
111};
112
113
114static int cadet_getstereo(struct cadet *dev)
115{
116 int ret = V4L2_TUNER_SUB_MONO;
117
118 if (!dev->is_fm_band)
119 return V4L2_TUNER_SUB_MONO;
120
121 outb(7, dev->io);
122 if ((inb(dev->io + 1) & 0x40) == 0)
123 ret = V4L2_TUNER_SUB_STEREO;
124 return ret;
125}
126
127static unsigned cadet_gettune(struct cadet *dev)
128{
129 int curvol, i;
130 unsigned fifo = 0;
131
132
133
134
135
136 outb(7, dev->io);
137 curvol = inb(dev->io + 1);
138 outb(0x00, dev->io + 1);
139 dev->tunestat = 0xffff;
140
141
142
143
144 for (i = 0; i < 25; i++) {
145 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
146 if (i < 24) {
147 outb(0x01, dev->io + 1);
148 dev->tunestat &= inb(dev->io + 1);
149 outb(0x00, dev->io + 1);
150 }
151 }
152
153
154
155
156 outb(curvol, dev->io + 1);
157 return fifo;
158}
159
160static unsigned cadet_getfreq(struct cadet *dev)
161{
162 int i;
163 unsigned freq = 0, test, fifo = 0;
164
165
166
167
168 fifo = cadet_gettune(dev);
169
170
171
172
173 if (!dev->is_fm_band)
174 return ((fifo & 0x7fff) - 450) * 16;
175
176 test = 12500;
177 for (i = 0; i < 14; i++) {
178 if ((fifo & 0x01) != 0)
179 freq += test;
180 test = test << 1;
181 fifo = fifo >> 1;
182 }
183 freq -= 10700000;
184 freq = (freq * 16) / 1000;
185 return freq;
186}
187
188static void cadet_settune(struct cadet *dev, unsigned fifo)
189{
190 int i;
191 unsigned test;
192
193 outb(7, dev->io);
194
195
196
197 test = 0;
198 test = (fifo >> 23) & 0x02;
199 test |= 0x1c;
200 outb(7, dev->io);
201 outb(test, dev->io + 1);
202 for (i = 0; i < 25; i++) {
203 test |= 0x01;
204 outb(test, dev->io + 1);
205 test &= 0xfe;
206 outb(test, dev->io + 1);
207 fifo = fifo << 1;
208 test = 0x1c | ((fifo >> 23) & 0x02);
209 outb(test, dev->io + 1);
210 }
211}
212
213static void cadet_setfreq(struct cadet *dev, unsigned freq)
214{
215 unsigned fifo;
216 int i, j, test;
217 int curvol;
218
219 freq = clamp(freq, bands[dev->is_fm_band].rangelow,
220 bands[dev->is_fm_band].rangehigh);
221 dev->curfreq = freq;
222
223
224
225 fifo = 0;
226 if (dev->is_fm_band) {
227 test = 102400;
228 freq = freq / 16;
229 freq += 10700;
230 for (i = 0; i < 14; i++) {
231 fifo = fifo << 1;
232 if (freq >= test) {
233 fifo |= 0x01;
234 freq -= test;
235 }
236 test = test >> 1;
237 }
238 } else {
239 fifo = (freq / 16) + 450;
240 fifo |= 0x100000;
241 }
242
243
244
245
246
247 outb(7, dev->io);
248 curvol = inb(dev->io + 1);
249
250
251
252
253 for (j = 3; j > -1; j--) {
254 cadet_settune(dev, fifo | (j << 16));
255
256 outb(7, dev->io);
257 outb(curvol, dev->io + 1);
258
259 msleep(100);
260
261 cadet_gettune(dev);
262 if ((dev->tunestat & 0x40) == 0) {
263 dev->sigstrength = sigtable[dev->is_fm_band][j];
264 goto reset_rds;
265 }
266 }
267 dev->sigstrength = 0;
268reset_rds:
269 outb(3, dev->io);
270 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
271}
272
273
274static void cadet_handler(unsigned long data)
275{
276 struct cadet *dev = (void *)data;
277
278
279 if (mutex_trylock(&dev->lock)) {
280 outb(0x3, dev->io);
281 if ((inb(dev->io + 1) & 0x20) != 0)
282 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
283 outb(0x80, dev->io);
284 while ((inb(dev->io) & 0x80) != 0) {
285 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
286 if (dev->rdsin + 1 == dev->rdsout)
287 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
288 else
289 dev->rdsin++;
290 }
291 mutex_unlock(&dev->lock);
292 }
293
294
295
296
297 if (dev->rdsin != dev->rdsout)
298 wake_up_interruptible(&dev->read_queue);
299
300
301
302
303 init_timer(&dev->readtimer);
304 dev->readtimer.function = cadet_handler;
305 dev->readtimer.data = data;
306 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
307 add_timer(&dev->readtimer);
308}
309
310static void cadet_start_rds(struct cadet *dev)
311{
312 dev->rdsstat = 1;
313 outb(0x80, dev->io);
314 init_timer(&dev->readtimer);
315 dev->readtimer.function = cadet_handler;
316 dev->readtimer.data = (unsigned long)dev;
317 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
318 add_timer(&dev->readtimer);
319}
320
321static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
322{
323 struct cadet *dev = video_drvdata(file);
324 unsigned char readbuf[RDS_BUFFER];
325 int i = 0;
326
327 mutex_lock(&dev->lock);
328 if (dev->rdsstat == 0)
329 cadet_start_rds(dev);
330 if (dev->rdsin == dev->rdsout) {
331 if (file->f_flags & O_NONBLOCK) {
332 i = -EWOULDBLOCK;
333 goto unlock;
334 }
335 mutex_unlock(&dev->lock);
336 interruptible_sleep_on(&dev->read_queue);
337 mutex_lock(&dev->lock);
338 }
339 while (i < count && dev->rdsin != dev->rdsout)
340 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
341
342 if (i && copy_to_user(data, readbuf, i))
343 i = -EFAULT;
344unlock:
345 mutex_unlock(&dev->lock);
346 return i;
347}
348
349
350static int vidioc_querycap(struct file *file, void *priv,
351 struct v4l2_capability *v)
352{
353 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
354 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
355 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
356 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
357 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
358 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
359 return 0;
360}
361
362static int vidioc_g_tuner(struct file *file, void *priv,
363 struct v4l2_tuner *v)
364{
365 struct cadet *dev = video_drvdata(file);
366
367 if (v->index)
368 return -EINVAL;
369 v->type = V4L2_TUNER_RADIO;
370 strlcpy(v->name, "Radio", sizeof(v->name));
371 v->capability = bands[0].capability | bands[1].capability;
372 v->rangelow = bands[0].rangelow;
373 v->rangehigh = bands[1].rangehigh;
374 if (dev->is_fm_band) {
375 v->rxsubchans = cadet_getstereo(dev);
376 outb(3, dev->io);
377 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
378 mdelay(100);
379 outb(3, dev->io);
380 if (inb(dev->io + 1) & 0x80)
381 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
382 } else {
383 v->rangelow = 8320;
384 v->rangehigh = 26400;
385 v->rxsubchans = V4L2_TUNER_SUB_MONO;
386 }
387 v->audmode = V4L2_TUNER_MODE_STEREO;
388 v->signal = dev->sigstrength;
389 return 0;
390}
391
392static int vidioc_s_tuner(struct file *file, void *priv,
393 const struct v4l2_tuner *v)
394{
395 return v->index ? -EINVAL : 0;
396}
397
398static int vidioc_enum_freq_bands(struct file *file, void *priv,
399 struct v4l2_frequency_band *band)
400{
401 if (band->tuner)
402 return -EINVAL;
403 if (band->index >= ARRAY_SIZE(bands))
404 return -EINVAL;
405 *band = bands[band->index];
406 return 0;
407}
408
409static int vidioc_g_frequency(struct file *file, void *priv,
410 struct v4l2_frequency *f)
411{
412 struct cadet *dev = video_drvdata(file);
413
414 if (f->tuner)
415 return -EINVAL;
416 f->type = V4L2_TUNER_RADIO;
417 f->frequency = dev->curfreq;
418 return 0;
419}
420
421
422static int vidioc_s_frequency(struct file *file, void *priv,
423 const struct v4l2_frequency *f)
424{
425 struct cadet *dev = video_drvdata(file);
426
427 if (f->tuner)
428 return -EINVAL;
429 dev->is_fm_band =
430 f->frequency >= (bands[0].rangehigh + bands[1].rangelow) / 2;
431 cadet_setfreq(dev, f->frequency);
432 return 0;
433}
434
435static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
436{
437 struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
438
439 switch (ctrl->id) {
440 case V4L2_CID_AUDIO_MUTE:
441 outb(7, dev->io);
442 if (ctrl->val)
443 outb(0x00, dev->io + 1);
444 else
445 outb(0x20, dev->io + 1);
446 return 0;
447 }
448 return -EINVAL;
449}
450
451static int cadet_open(struct file *file)
452{
453 struct cadet *dev = video_drvdata(file);
454 int err;
455
456 mutex_lock(&dev->lock);
457 err = v4l2_fh_open(file);
458 if (err)
459 goto fail;
460 if (v4l2_fh_is_singular_file(file))
461 init_waitqueue_head(&dev->read_queue);
462fail:
463 mutex_unlock(&dev->lock);
464 return err;
465}
466
467static int cadet_release(struct file *file)
468{
469 struct cadet *dev = video_drvdata(file);
470
471 mutex_lock(&dev->lock);
472 if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
473 del_timer_sync(&dev->readtimer);
474 dev->rdsstat = 0;
475 }
476 v4l2_fh_release(file);
477 mutex_unlock(&dev->lock);
478 return 0;
479}
480
481static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
482{
483 struct cadet *dev = video_drvdata(file);
484 unsigned long req_events = poll_requested_events(wait);
485 unsigned int res = v4l2_ctrl_poll(file, wait);
486
487 poll_wait(file, &dev->read_queue, wait);
488 if (dev->rdsstat == 0 && (req_events & (POLLIN | POLLRDNORM))) {
489 mutex_lock(&dev->lock);
490 if (dev->rdsstat == 0)
491 cadet_start_rds(dev);
492 mutex_unlock(&dev->lock);
493 }
494 if (dev->rdsin != dev->rdsout)
495 res |= POLLIN | POLLRDNORM;
496 return res;
497}
498
499
500static const struct v4l2_file_operations cadet_fops = {
501 .owner = THIS_MODULE,
502 .open = cadet_open,
503 .release = cadet_release,
504 .read = cadet_read,
505 .unlocked_ioctl = video_ioctl2,
506 .poll = cadet_poll,
507};
508
509static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
510 .vidioc_querycap = vidioc_querycap,
511 .vidioc_g_tuner = vidioc_g_tuner,
512 .vidioc_s_tuner = vidioc_s_tuner,
513 .vidioc_g_frequency = vidioc_g_frequency,
514 .vidioc_s_frequency = vidioc_s_frequency,
515 .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
516 .vidioc_log_status = v4l2_ctrl_log_status,
517 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
518 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
519};
520
521static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
522 .s_ctrl = cadet_s_ctrl,
523};
524
525#ifdef CONFIG_PNP
526
527static struct pnp_device_id cadet_pnp_devices[] = {
528
529 {.id = "MSM0c24", .driver_data = 0},
530 {.id = ""}
531};
532
533MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
534
535static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
536{
537 if (!dev)
538 return -ENODEV;
539
540 if (io > 0)
541 return -EBUSY;
542
543 if (!pnp_port_valid(dev, 0))
544 return -ENODEV;
545
546 io = pnp_port_start(dev, 0);
547
548 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
549
550 return io;
551}
552
553static struct pnp_driver cadet_pnp_driver = {
554 .name = "radio-cadet",
555 .id_table = cadet_pnp_devices,
556 .probe = cadet_pnp_probe,
557 .remove = NULL,
558};
559
560#else
561static struct pnp_driver cadet_pnp_driver;
562#endif
563
564static void cadet_probe(struct cadet *dev)
565{
566 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
567 int i;
568
569 for (i = 0; i < 8; i++) {
570 dev->io = iovals[i];
571 if (request_region(dev->io, 2, "cadet-probe")) {
572 cadet_setfreq(dev, bands[1].rangelow);
573 if (cadet_getfreq(dev) == bands[1].rangelow) {
574 release_region(dev->io, 2);
575 return;
576 }
577 release_region(dev->io, 2);
578 }
579 }
580 dev->io = -1;
581}
582
583
584
585
586
587
588static int __init cadet_init(void)
589{
590 struct cadet *dev = &cadet_card;
591 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
592 struct v4l2_ctrl_handler *hdl;
593 int res = -ENODEV;
594
595 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
596 mutex_init(&dev->lock);
597
598
599 if (io < 0)
600 pnp_register_driver(&cadet_pnp_driver);
601 dev->io = io;
602
603
604 if (dev->io < 0)
605 cadet_probe(dev);
606
607
608 if (dev->io < 0) {
609#ifdef MODULE
610 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
611 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
612#endif
613 goto fail;
614 }
615 if (!request_region(dev->io, 2, "cadet"))
616 goto fail;
617
618 res = v4l2_device_register(NULL, v4l2_dev);
619 if (res < 0) {
620 release_region(dev->io, 2);
621 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
622 goto fail;
623 }
624
625 hdl = &dev->ctrl_handler;
626 v4l2_ctrl_handler_init(hdl, 2);
627 v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
628 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
629 v4l2_dev->ctrl_handler = hdl;
630 if (hdl->error) {
631 res = hdl->error;
632 v4l2_err(v4l2_dev, "Could not register controls\n");
633 goto err_hdl;
634 }
635
636 dev->is_fm_band = true;
637 dev->curfreq = bands[dev->is_fm_band].rangelow;
638 cadet_setfreq(dev, dev->curfreq);
639 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
640 dev->vdev.v4l2_dev = v4l2_dev;
641 dev->vdev.fops = &cadet_fops;
642 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
643 dev->vdev.release = video_device_release_empty;
644 dev->vdev.lock = &dev->lock;
645 set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
646 video_set_drvdata(&dev->vdev, dev);
647
648 res = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
649 if (res < 0)
650 goto err_hdl;
651 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
652 return 0;
653err_hdl:
654 v4l2_ctrl_handler_free(hdl);
655 v4l2_device_unregister(v4l2_dev);
656 release_region(dev->io, 2);
657fail:
658 pnp_unregister_driver(&cadet_pnp_driver);
659 return res;
660}
661
662static void __exit cadet_exit(void)
663{
664 struct cadet *dev = &cadet_card;
665
666 video_unregister_device(&dev->vdev);
667 v4l2_ctrl_handler_free(&dev->ctrl_handler);
668 v4l2_device_unregister(&dev->v4l2_dev);
669 outb(7, dev->io);
670 outb(0x00, dev->io + 1);
671 release_region(dev->io, 2);
672 pnp_unregister_driver(&cadet_pnp_driver);
673}
674
675module_init(cadet_init);
676module_exit(cadet_exit);
677
678