Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
radio-cadet.c
Go to the documentation of this file.
1 /* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
2  *
3  * by Fred Gleason <[email protected]>
4  * Version 0.3.3
5  *
6  * (Loosely) based on code for the Aztech radio card by
7  *
8  * Russell Kroll ([email protected])
9  * Quay Ly
10  * Donald Song
11  * Jason Lewis ([email protected])
12  * Scott McGrath ([email protected])
13  * William McGrath ([email protected])
14  *
15  * History:
16  * 2000-04-29 Russell Kroll <[email protected]>
17  * Added ISAPnP detection for Linux 2.3/2.4
18  *
19  * 2001-01-10 Russell Kroll <[email protected]>
20  * Removed dead CONFIG_RADIO_CADET_PORT code
21  * PnP detection on load is now default (no args necessary)
22  *
23  * 2002-01-17 Adam Belay <[email protected]>
24  * Updated to latest pnp code
25  *
26  * 2003-01-31 Alan Cox <[email protected]>
27  * Cleaned up locking, delay code, general odds and ends
28  *
29  * 2006-07-30 Hans J. Koch <[email protected]>
30  * Changed API to V4L2
31  */
32 
33 #include <linux/module.h> /* Modules */
34 #include <linux/init.h> /* Initdata */
35 #include <linux/ioport.h> /* request_region */
36 #include <linux/delay.h> /* udelay */
37 #include <linux/videodev2.h> /* V4L2 API defs */
38 #include <linux/param.h>
39 #include <linux/pnp.h>
40 #include <linux/sched.h>
41 #include <linux/io.h> /* outb, outb_p */
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 
48 MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
49 MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
50 MODULE_LICENSE("GPL");
51 MODULE_VERSION("0.3.4");
52 
53 static int io = -1; /* default to isapnp activation */
54 static int radio_nr = -1;
55 
56 module_param(io, int, 0);
57 MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
58 module_param(radio_nr, int, 0);
59 
60 #define RDS_BUFFER 256
61 #define RDS_RX_FLAG 1
62 #define MBS_RX_FLAG 2
63 
64 struct cadet {
68  int io;
69  bool is_fm_band;
71  int tunestat;
76  unsigned char rdsbuf[RDS_BUFFER];
77  struct mutex lock;
78  int reading;
79 };
80 
81 static struct cadet cadet_card;
82 
83 /*
84  * Signal Strength Threshold Values
85  * The V4L API spec does not define any particular unit for the signal
86  * strength value. These values are in microvolts of RF at the tuner's input.
87  */
88 static u16 sigtable[2][4] = {
89  { 1835, 2621, 4128, 65535 },
90  { 2185, 4369, 13107, 65535 },
91 };
92 
93 
94 static int cadet_getstereo(struct cadet *dev)
95 {
97 
98  if (!dev->is_fm_band) /* Only FM has stereo capability! */
99  return V4L2_TUNER_SUB_MONO;
100 
101  outb(7, dev->io); /* Select tuner control */
102  if ((inb(dev->io + 1) & 0x40) == 0)
103  ret = V4L2_TUNER_SUB_STEREO;
104  return ret;
105 }
106 
107 static unsigned cadet_gettune(struct cadet *dev)
108 {
109  int curvol, i;
110  unsigned fifo = 0;
111 
112  /*
113  * Prepare for read
114  */
115 
116  outb(7, dev->io); /* Select tuner control */
117  curvol = inb(dev->io + 1); /* Save current volume/mute setting */
118  outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
119  dev->tunestat = 0xffff;
120 
121  /*
122  * Read the shift register
123  */
124  for (i = 0; i < 25; i++) {
125  fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
126  if (i < 24) {
127  outb(0x01, dev->io + 1);
128  dev->tunestat &= inb(dev->io + 1);
129  outb(0x00, dev->io + 1);
130  }
131  }
132 
133  /*
134  * Restore volume/mute setting
135  */
136  outb(curvol, dev->io + 1);
137  return fifo;
138 }
139 
140 static unsigned cadet_getfreq(struct cadet *dev)
141 {
142  int i;
143  unsigned freq = 0, test, fifo = 0;
144 
145  /*
146  * Read current tuning
147  */
148  fifo = cadet_gettune(dev);
149 
150  /*
151  * Convert to actual frequency
152  */
153  if (!dev->is_fm_band) /* AM */
154  return ((fifo & 0x7fff) - 450) * 16;
155 
156  test = 12500;
157  for (i = 0; i < 14; i++) {
158  if ((fifo & 0x01) != 0)
159  freq += test;
160  test = test << 1;
161  fifo = fifo >> 1;
162  }
163  freq -= 10700000; /* IF frequency is 10.7 MHz */
164  freq = (freq * 16) / 1000; /* Make it 1/16 kHz */
165  return freq;
166 }
167 
168 static void cadet_settune(struct cadet *dev, unsigned fifo)
169 {
170  int i;
171  unsigned test;
172 
173  outb(7, dev->io); /* Select tuner control */
174  /*
175  * Write the shift register
176  */
177  test = 0;
178  test = (fifo >> 23) & 0x02; /* Align data for SDO */
179  test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
180  outb(7, dev->io); /* Select tuner control */
181  outb(test, dev->io + 1); /* Initialize for write */
182  for (i = 0; i < 25; i++) {
183  test |= 0x01; /* Toggle SCK High */
184  outb(test, dev->io + 1);
185  test &= 0xfe; /* Toggle SCK Low */
186  outb(test, dev->io + 1);
187  fifo = fifo << 1; /* Prepare the next bit */
188  test = 0x1c | ((fifo >> 23) & 0x02);
189  outb(test, dev->io + 1);
190  }
191 }
192 
193 static void cadet_setfreq(struct cadet *dev, unsigned freq)
194 {
195  unsigned fifo;
196  int i, j, test;
197  int curvol;
198 
199  dev->curfreq = freq;
200  /*
201  * Formulate a fifo command
202  */
203  fifo = 0;
204  if (dev->is_fm_band) { /* FM */
205  test = 102400;
206  freq = freq / 16; /* Make it kHz */
207  freq += 10700; /* IF is 10700 kHz */
208  for (i = 0; i < 14; i++) {
209  fifo = fifo << 1;
210  if (freq >= test) {
211  fifo |= 0x01;
212  freq -= test;
213  }
214  test = test >> 1;
215  }
216  } else { /* AM */
217  fifo = (freq / 16) + 450; /* Make it kHz */
218  fifo |= 0x100000; /* Select AM Band */
219  }
220 
221  /*
222  * Save current volume/mute setting
223  */
224 
225  outb(7, dev->io); /* Select tuner control */
226  curvol = inb(dev->io + 1);
227 
228  /*
229  * Tune the card
230  */
231  for (j = 3; j > -1; j--) {
232  cadet_settune(dev, fifo | (j << 16));
233 
234  outb(7, dev->io); /* Select tuner control */
235  outb(curvol, dev->io + 1);
236 
237  msleep(100);
238 
239  cadet_gettune(dev);
240  if ((dev->tunestat & 0x40) == 0) { /* Tuned */
241  dev->sigstrength = sigtable[dev->is_fm_band][j];
242  goto reset_rds;
243  }
244  }
245  dev->sigstrength = 0;
246 reset_rds:
247  outb(3, dev->io);
248  outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
249 }
250 
251 
252 static void cadet_handler(unsigned long data)
253 {
254  struct cadet *dev = (void *)data;
255 
256  /* Service the RDS fifo */
257  if (mutex_trylock(&dev->lock)) {
258  outb(0x3, dev->io); /* Select RDS Decoder Control */
259  if ((inb(dev->io + 1) & 0x20) != 0)
260  printk(KERN_CRIT "cadet: RDS fifo overflow\n");
261  outb(0x80, dev->io); /* Select RDS fifo */
262  while ((inb(dev->io) & 0x80) != 0) {
263  dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
264  if (dev->rdsin + 1 == dev->rdsout)
265  printk(KERN_WARNING "cadet: RDS buffer overflow\n");
266  else
267  dev->rdsin++;
268  }
269  mutex_unlock(&dev->lock);
270  }
271 
272  /*
273  * Service pending read
274  */
275  if (dev->rdsin != dev->rdsout)
277 
278  /*
279  * Clean up and exit
280  */
281  init_timer(&dev->readtimer);
282  dev->readtimer.function = cadet_handler;
283  dev->readtimer.data = data;
284  dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
285  add_timer(&dev->readtimer);
286 }
287 
288 static void cadet_start_rds(struct cadet *dev)
289 {
290  dev->rdsstat = 1;
291  outb(0x80, dev->io); /* Select RDS fifo */
292  init_timer(&dev->readtimer);
293  dev->readtimer.function = cadet_handler;
294  dev->readtimer.data = (unsigned long)dev;
295  dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
296  add_timer(&dev->readtimer);
297 }
298 
299 static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
300 {
301  struct cadet *dev = video_drvdata(file);
302  unsigned char readbuf[RDS_BUFFER];
303  int i = 0;
304 
305  mutex_lock(&dev->lock);
306  if (dev->rdsstat == 0)
307  cadet_start_rds(dev);
308  if (dev->rdsin == dev->rdsout) {
309  if (file->f_flags & O_NONBLOCK) {
310  i = -EWOULDBLOCK;
311  goto unlock;
312  }
313  mutex_unlock(&dev->lock);
315  mutex_lock(&dev->lock);
316  }
317  while (i < count && dev->rdsin != dev->rdsout)
318  readbuf[i++] = dev->rdsbuf[dev->rdsout++];
319 
320  if (i && copy_to_user(data, readbuf, i))
321  i = -EFAULT;
322 unlock:
323  mutex_unlock(&dev->lock);
324  return i;
325 }
326 
327 
328 static int vidioc_querycap(struct file *file, void *priv,
329  struct v4l2_capability *v)
330 {
331  strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
332  strlcpy(v->card, "ADS Cadet", sizeof(v->card));
333  strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
337  return 0;
338 }
339 
340 static const struct v4l2_frequency_band bands[] = {
341  {
342  .index = 0,
343  .type = V4L2_TUNER_RADIO,
345  .rangelow = 8320, /* 520 kHz */
346  .rangehigh = 26400, /* 1650 kHz */
347  .modulation = V4L2_BAND_MODULATION_AM,
348  }, {
349  .index = 1,
350  .type = V4L2_TUNER_RADIO,
351  .capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
354  .rangelow = 1400000, /* 87.5 MHz */
355  .rangehigh = 1728000, /* 108.0 MHz */
356  .modulation = V4L2_BAND_MODULATION_FM,
357  },
358 };
359 
360 static int vidioc_g_tuner(struct file *file, void *priv,
361  struct v4l2_tuner *v)
362 {
363  struct cadet *dev = video_drvdata(file);
364 
365  if (v->index)
366  return -EINVAL;
367  v->type = V4L2_TUNER_RADIO;
368  strlcpy(v->name, "Radio", sizeof(v->name));
369  v->capability = bands[0].capability | bands[1].capability;
370  v->rangelow = bands[0].rangelow; /* 520 kHz (start of AM band) */
371  v->rangehigh = bands[1].rangehigh; /* 108.0 MHz (end of FM band) */
372  if (dev->is_fm_band) {
373  v->rxsubchans = cadet_getstereo(dev);
374  outb(3, dev->io);
375  outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
376  mdelay(100);
377  outb(3, dev->io);
378  if (inb(dev->io + 1) & 0x80)
380  } else {
381  v->rangelow = 8320; /* 520 kHz */
382  v->rangehigh = 26400; /* 1650 kHz */
384  }
386  v->signal = dev->sigstrength; /* We might need to modify scaling of this */
387  return 0;
388 }
389 
390 static int vidioc_s_tuner(struct file *file, void *priv,
391  struct v4l2_tuner *v)
392 {
393  return v->index ? -EINVAL : 0;
394 }
395 
396 static int vidioc_enum_freq_bands(struct file *file, void *priv,
397  struct v4l2_frequency_band *band)
398 {
399  if (band->tuner)
400  return -EINVAL;
401  if (band->index >= ARRAY_SIZE(bands))
402  return -EINVAL;
403  *band = bands[band->index];
404  return 0;
405 }
406 
407 static int vidioc_g_frequency(struct file *file, void *priv,
408  struct v4l2_frequency *f)
409 {
410  struct cadet *dev = video_drvdata(file);
411 
412  if (f->tuner)
413  return -EINVAL;
414  f->type = V4L2_TUNER_RADIO;
415  f->frequency = dev->curfreq;
416  return 0;
417 }
418 
419 
420 static int vidioc_s_frequency(struct file *file, void *priv,
421  struct v4l2_frequency *f)
422 {
423  struct cadet *dev = video_drvdata(file);
424 
425  if (f->tuner)
426  return -EINVAL;
427  dev->is_fm_band =
428  f->frequency >= (bands[0].rangehigh + bands[1].rangelow) / 2;
429  clamp(f->frequency, bands[dev->is_fm_band].rangelow,
430  bands[dev->is_fm_band].rangehigh);
431  cadet_setfreq(dev, f->frequency);
432  return 0;
433 }
434 
435 static 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); /* Select tuner control */
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 
451 static 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))
462 fail:
463  mutex_unlock(&dev->lock);
464  return err;
465 }
466 
467 static 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 
481 static 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 
500 static 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 
509 static 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 
521 static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
522  .s_ctrl = cadet_s_ctrl,
523 };
524 
525 #ifdef CONFIG_PNP
526 
527 static struct pnp_device_id cadet_pnp_devices[] = {
528  /* ADS Cadet AM/FM Radio Card */
529  {.id = "MSM0c24", .driver_data = 0},
530  {.id = ""}
531 };
532 
533 MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
534 
535 static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
536 {
537  if (!dev)
538  return -ENODEV;
539  /* only support one device */
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 
553 static 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
561 static struct pnp_driver cadet_pnp_driver;
562 #endif
563 
564 static 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  * io should only be set if the user has used something like
585  * isapnp (the userspace program) to initialize this card for us
586  */
587 
588 static 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  /* If a probe was requested then probe ISAPnP first (safest) */
599  if (io < 0)
600  pnp_register_driver(&cadet_pnp_driver);
601  dev->io = io;
602 
603  /* If that fails then probe unsafely if probe is requested */
604  if (dev->io < 0)
605  cadet_probe(dev);
606 
607  /* Else we bail out */
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  if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
649  goto err_hdl;
650  v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
651  return 0;
652 err_hdl:
654  v4l2_device_unregister(v4l2_dev);
655  release_region(dev->io, 2);
656 fail:
657  pnp_unregister_driver(&cadet_pnp_driver);
658  return res;
659 }
660 
661 static void __exit cadet_exit(void)
662 {
663  struct cadet *dev = &cadet_card;
664 
668  outb(7, dev->io); /* Mute */
669  outb(0x00, dev->io + 1);
670  release_region(dev->io, 2);
671  pnp_unregister_driver(&cadet_pnp_driver);
672 }
673 
674 module_init(cadet_init);
675 module_exit(cadet_exit);
676