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
273static bool cadet_has_rds_data(struct cadet *dev)
274{
275 bool result;
276
277 mutex_lock(&dev->lock);
278 result = dev->rdsin != dev->rdsout;
279 mutex_unlock(&dev->lock);
280 return result;
281}
282
283
284static void cadet_handler(struct timer_list *t)
285{
286 struct cadet *dev = from_timer(dev, t, readtimer);
287
288
289 if (mutex_trylock(&dev->lock)) {
290 outb(0x3, dev->io);
291 if ((inb(dev->io + 1) & 0x20) != 0)
292 pr_err("cadet: RDS fifo overflow\n");
293 outb(0x80, dev->io);
294
295 while ((inb(dev->io) & 0x80) != 0) {
296 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
297 if (dev->rdsin + 1 != dev->rdsout)
298 dev->rdsin++;
299 }
300 mutex_unlock(&dev->lock);
301 }
302
303
304
305
306 if (cadet_has_rds_data(dev))
307 wake_up_interruptible(&dev->read_queue);
308
309
310
311
312 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
313 add_timer(&dev->readtimer);
314}
315
316static void cadet_start_rds(struct cadet *dev)
317{
318 dev->rdsstat = 1;
319 outb(0x80, dev->io);
320 timer_setup(&dev->readtimer, cadet_handler, 0);
321 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
322 add_timer(&dev->readtimer);
323}
324
325static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
326{
327 struct cadet *dev = video_drvdata(file);
328 unsigned char readbuf[RDS_BUFFER];
329 int i = 0;
330
331 mutex_lock(&dev->lock);
332 if (dev->rdsstat == 0)
333 cadet_start_rds(dev);
334 mutex_unlock(&dev->lock);
335
336 if (!cadet_has_rds_data(dev) && (file->f_flags & O_NONBLOCK))
337 return -EWOULDBLOCK;
338 i = wait_event_interruptible(dev->read_queue, cadet_has_rds_data(dev));
339 if (i)
340 return i;
341
342 mutex_lock(&dev->lock);
343 while (i < count && dev->rdsin != dev->rdsout)
344 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
345 mutex_unlock(&dev->lock);
346
347 if (i && copy_to_user(data, readbuf, i))
348 return -EFAULT;
349 return i;
350}
351
352
353static int vidioc_querycap(struct file *file, void *priv,
354 struct v4l2_capability *v)
355{
356 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
357 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
358 strlcpy(v->bus_info, "ISA:radio-cadet", sizeof(v->bus_info));
359 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
360 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
361 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
362 return 0;
363}
364
365static int vidioc_g_tuner(struct file *file, void *priv,
366 struct v4l2_tuner *v)
367{
368 struct cadet *dev = video_drvdata(file);
369
370 if (v->index)
371 return -EINVAL;
372 v->type = V4L2_TUNER_RADIO;
373 strlcpy(v->name, "Radio", sizeof(v->name));
374 v->capability = bands[0].capability | bands[1].capability;
375 v->rangelow = bands[0].rangelow;
376 v->rangehigh = bands[1].rangehigh;
377 if (dev->is_fm_band) {
378 v->rxsubchans = cadet_getstereo(dev);
379 outb(3, dev->io);
380 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
381 mdelay(100);
382 outb(3, dev->io);
383 if (inb(dev->io + 1) & 0x80)
384 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
385 } else {
386 v->rangelow = 8320;
387 v->rangehigh = 26400;
388 v->rxsubchans = V4L2_TUNER_SUB_MONO;
389 }
390 v->audmode = V4L2_TUNER_MODE_STEREO;
391 v->signal = dev->sigstrength;
392 return 0;
393}
394
395static int vidioc_s_tuner(struct file *file, void *priv,
396 const struct v4l2_tuner *v)
397{
398 return v->index ? -EINVAL : 0;
399}
400
401static int vidioc_enum_freq_bands(struct file *file, void *priv,
402 struct v4l2_frequency_band *band)
403{
404 if (band->tuner)
405 return -EINVAL;
406 if (band->index >= ARRAY_SIZE(bands))
407 return -EINVAL;
408 *band = bands[band->index];
409 return 0;
410}
411
412static int vidioc_g_frequency(struct file *file, void *priv,
413 struct v4l2_frequency *f)
414{
415 struct cadet *dev = video_drvdata(file);
416
417 if (f->tuner)
418 return -EINVAL;
419 f->type = V4L2_TUNER_RADIO;
420 f->frequency = dev->curfreq;
421 return 0;
422}
423
424
425static int vidioc_s_frequency(struct file *file, void *priv,
426 const struct v4l2_frequency *f)
427{
428 struct cadet *dev = video_drvdata(file);
429
430 if (f->tuner)
431 return -EINVAL;
432 dev->is_fm_band =
433 f->frequency >= (bands[0].rangehigh + bands[1].rangelow) / 2;
434 cadet_setfreq(dev, f->frequency);
435 return 0;
436}
437
438static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
439{
440 struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
441
442 switch (ctrl->id) {
443 case V4L2_CID_AUDIO_MUTE:
444 outb(7, dev->io);
445 if (ctrl->val)
446 outb(0x00, dev->io + 1);
447 else
448 outb(0x20, dev->io + 1);
449 return 0;
450 }
451 return -EINVAL;
452}
453
454static int cadet_open(struct file *file)
455{
456 struct cadet *dev = video_drvdata(file);
457 int err;
458
459 mutex_lock(&dev->lock);
460 err = v4l2_fh_open(file);
461 if (err)
462 goto fail;
463 if (v4l2_fh_is_singular_file(file))
464 init_waitqueue_head(&dev->read_queue);
465fail:
466 mutex_unlock(&dev->lock);
467 return err;
468}
469
470static int cadet_release(struct file *file)
471{
472 struct cadet *dev = video_drvdata(file);
473
474 mutex_lock(&dev->lock);
475 if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
476 del_timer_sync(&dev->readtimer);
477 dev->rdsstat = 0;
478 }
479 v4l2_fh_release(file);
480 mutex_unlock(&dev->lock);
481 return 0;
482}
483
484static __poll_t cadet_poll(struct file *file, struct poll_table_struct *wait)
485{
486 struct cadet *dev = video_drvdata(file);
487 __poll_t req_events = poll_requested_events(wait);
488 __poll_t res = v4l2_ctrl_poll(file, wait);
489
490 poll_wait(file, &dev->read_queue, wait);
491 if (dev->rdsstat == 0 && (req_events & (EPOLLIN | EPOLLRDNORM))) {
492 mutex_lock(&dev->lock);
493 if (dev->rdsstat == 0)
494 cadet_start_rds(dev);
495 mutex_unlock(&dev->lock);
496 }
497 if (cadet_has_rds_data(dev))
498 res |= EPOLLIN | EPOLLRDNORM;
499 return res;
500}
501
502
503static const struct v4l2_file_operations cadet_fops = {
504 .owner = THIS_MODULE,
505 .open = cadet_open,
506 .release = cadet_release,
507 .read = cadet_read,
508 .unlocked_ioctl = video_ioctl2,
509 .poll = cadet_poll,
510};
511
512static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
513 .vidioc_querycap = vidioc_querycap,
514 .vidioc_g_tuner = vidioc_g_tuner,
515 .vidioc_s_tuner = vidioc_s_tuner,
516 .vidioc_g_frequency = vidioc_g_frequency,
517 .vidioc_s_frequency = vidioc_s_frequency,
518 .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
519 .vidioc_log_status = v4l2_ctrl_log_status,
520 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
521 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
522};
523
524static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
525 .s_ctrl = cadet_s_ctrl,
526};
527
528#ifdef CONFIG_PNP
529
530static const struct pnp_device_id cadet_pnp_devices[] = {
531
532 {.id = "MSM0c24", .driver_data = 0},
533 {.id = ""}
534};
535
536MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
537
538static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
539{
540 if (!dev)
541 return -ENODEV;
542
543 if (io > 0)
544 return -EBUSY;
545
546 if (!pnp_port_valid(dev, 0))
547 return -ENODEV;
548
549 io = pnp_port_start(dev, 0);
550
551 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
552
553 return io;
554}
555
556static struct pnp_driver cadet_pnp_driver = {
557 .name = "radio-cadet",
558 .id_table = cadet_pnp_devices,
559 .probe = cadet_pnp_probe,
560 .remove = NULL,
561};
562
563#else
564static struct pnp_driver cadet_pnp_driver;
565#endif
566
567static void cadet_probe(struct cadet *dev)
568{
569 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
570 int i;
571
572 for (i = 0; i < 8; i++) {
573 dev->io = iovals[i];
574 if (request_region(dev->io, 2, "cadet-probe")) {
575 cadet_setfreq(dev, bands[1].rangelow);
576 if (cadet_getfreq(dev) == bands[1].rangelow) {
577 release_region(dev->io, 2);
578 return;
579 }
580 release_region(dev->io, 2);
581 }
582 }
583 dev->io = -1;
584}
585
586
587
588
589
590
591static int __init cadet_init(void)
592{
593 struct cadet *dev = &cadet_card;
594 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
595 struct v4l2_ctrl_handler *hdl;
596 int res = -ENODEV;
597
598 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
599 mutex_init(&dev->lock);
600
601
602 if (io < 0)
603 pnp_register_driver(&cadet_pnp_driver);
604 dev->io = io;
605
606
607 if (dev->io < 0)
608 cadet_probe(dev);
609
610
611 if (dev->io < 0) {
612#ifdef MODULE
613 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
614 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
615#endif
616 goto fail;
617 }
618 if (!request_region(dev->io, 2, "cadet"))
619 goto fail;
620
621 res = v4l2_device_register(NULL, v4l2_dev);
622 if (res < 0) {
623 release_region(dev->io, 2);
624 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
625 goto fail;
626 }
627
628 hdl = &dev->ctrl_handler;
629 v4l2_ctrl_handler_init(hdl, 2);
630 v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
631 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
632 v4l2_dev->ctrl_handler = hdl;
633 if (hdl->error) {
634 res = hdl->error;
635 v4l2_err(v4l2_dev, "Could not register controls\n");
636 goto err_hdl;
637 }
638
639 dev->is_fm_band = true;
640 dev->curfreq = bands[dev->is_fm_band].rangelow;
641 cadet_setfreq(dev, dev->curfreq);
642 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
643 dev->vdev.v4l2_dev = v4l2_dev;
644 dev->vdev.fops = &cadet_fops;
645 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
646 dev->vdev.release = video_device_release_empty;
647 dev->vdev.lock = &dev->lock;
648 video_set_drvdata(&dev->vdev, dev);
649
650 res = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
651 if (res < 0)
652 goto err_hdl;
653 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
654 return 0;
655err_hdl:
656 v4l2_ctrl_handler_free(hdl);
657 v4l2_device_unregister(v4l2_dev);
658 release_region(dev->io, 2);
659fail:
660 pnp_unregister_driver(&cadet_pnp_driver);
661 return res;
662}
663
664static void __exit cadet_exit(void)
665{
666 struct cadet *dev = &cadet_card;
667
668 video_unregister_device(&dev->vdev);
669 v4l2_ctrl_handler_free(&dev->ctrl_handler);
670 v4l2_device_unregister(&dev->v4l2_dev);
671 outb(7, dev->io);
672 outb(0x00, dev->io + 1);
673 release_region(dev->io, 2);
674 pnp_unregister_driver(&cadet_pnp_driver);
675}
676
677module_init(cadet_init);
678module_exit(cadet_exit);
679
680