blob: 643d80ac28fb1269b70942d2a53ccb7303c266b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
2 *
3 * by Fred Gleason <fredg@wava.com>
4 * Version 0.3.3
5 *
6 * (Loosely) based on code for the Aztech radio card by
7 *
8 * Russell Kroll (rkroll@exploits.org)
9 * Quay Ly
10 * Donald Song
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030011 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
13 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
14 *
15 * History:
16 * 2000-04-29 Russell Kroll <rkroll@exploits.org>
17 * Added ISAPnP detection for Linux 2.3/2.4
18 *
19 * 2001-01-10 Russell Kroll <rkroll@exploits.org>
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 <ambx1@neo.rr.com>
24 * Updated to latest pnp code
25 *
Alan Coxd9b01442008-10-27 15:13:47 -030026 * 2003-01-31 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * Cleaned up locking, delay code, general odds and ends
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030028 *
29 * 2006-07-30 Hans J. Koch <koch@hjk-az.de>
30 * Changed API to V4L2
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
33#include <linux/module.h> /* Modules */
34#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070035#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h> /* udelay */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030037#include <linux/videodev2.h> /* V4L2 API defs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/param.h>
39#include <linux/pnp.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040040#include <linux/sched.h>
Hans Verkuilbec2aec2009-03-06 13:48:47 -030041#include <linux/io.h> /* outb, outb_p */
Hans Verkuilbec2aec2009-03-06 13:48:47 -030042#include <media/v4l2-device.h>
43#include <media/v4l2-ioctl.h>
Hans Verkuilb54c97db2012-07-02 09:36:39 -030044#include <media/v4l2-ctrls.h>
45#include <media/v4l2-fh.h>
46#include <media/v4l2-event.h>
Hans Verkuilbec2aec2009-03-06 13:48:47 -030047
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");
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030051MODULE_VERSION("0.3.4");
Hans Verkuilbec2aec2009-03-06 13:48:47 -030052
53static int io = -1; /* default to isapnp activation */
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define RDS_BUFFER 256
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030061#define RDS_RX_FLAG 1
62#define MBS_RX_FLAG 2
63
Hans Verkuilbec2aec2009-03-06 13:48:47 -030064struct cadet {
65 struct v4l2_device v4l2_dev;
66 struct video_device vdev;
Hans Verkuilb54c97db2012-07-02 09:36:39 -030067 struct v4l2_ctrl_handler ctrl_handler;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030068 int io;
Hans Verkuil6652c712012-07-10 08:26:04 -030069 bool is_fm_band;
70 u32 curfreq;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030071 int tunestat;
72 int sigstrength;
73 wait_queue_head_t read_queue;
74 struct timer_list readtimer;
Hans Verkuilcc0d3262012-07-02 09:46:46 -030075 u8 rdsin, rdsout, rdsstat;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030076 unsigned char rdsbuf[RDS_BUFFER];
77 struct mutex lock;
78 int reading;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -030079};
80
Hans Verkuilbec2aec2009-03-06 13:48:47 -030081static struct cadet cadet_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * Signal Strength Threshold Values
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030085 * The V4L API spec does not define any particular unit for the signal
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * strength value. These values are in microvolts of RF at the tuner's input.
87 */
Hans Verkuil6652c712012-07-10 08:26:04 -030088static u16 sigtable[2][4] = {
89 { 1835, 2621, 4128, 65535 },
Hans Verkuilcc0d3262012-07-02 09:46:46 -030090 { 2185, 4369, 13107, 65535 },
Hans Verkuilbec2aec2009-03-06 13:48:47 -030091};
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030093
Hans Verkuilbec2aec2009-03-06 13:48:47 -030094static int cadet_getstereo(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030096 int ret = V4L2_TUNER_SUB_MONO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030097
Hans Verkuil6652c712012-07-10 08:26:04 -030098 if (!dev->is_fm_band) /* Only FM has stereo capability! */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030099 return V4L2_TUNER_SUB_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300101 outb(7, dev->io); /* Select tuner control */
102 if ((inb(dev->io + 1) & 0x40) == 0)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300103 ret = V4L2_TUNER_SUB_STEREO;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300107static unsigned cadet_gettune(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300109 int curvol, i;
110 unsigned fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300112 /*
113 * Prepare for read
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300116 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300121 /*
122 * Read the shift register
123 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300124 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);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300130 }
131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300133 /*
134 * Restore volume/mute setting
135 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300136 outb(curvol, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return fifo;
138}
139
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300140static unsigned cadet_getfreq(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300142 int i;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300143 unsigned freq = 0, test, fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /*
146 * Read current tuning
147 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300148 fifo = cadet_gettune(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300150 /*
151 * Convert to actual frequency
152 */
Hans Verkuil6652c712012-07-10 08:26:04 -0300153 if (!dev->is_fm_band) /* AM */
154 return ((fifo & 0x7fff) - 450) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Hans Verkuil6652c712012-07-10 08:26:04 -0300156 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 */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300165 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300168static void cadet_settune(struct cadet *dev, unsigned fifo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300170 int i;
171 unsigned test;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300173 outb(7, dev->io); /* Select tuner control */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /*
175 * Write the shift register
176 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300177 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300193static void cadet_setfreq(struct cadet *dev, unsigned freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300195 unsigned fifo;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300196 int i, j, test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300197 int curvol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Hans Verkuil6652c712012-07-10 08:26:04 -0300199 dev->curfreq = freq;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300200 /*
201 * Formulate a fifo command
202 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300203 fifo = 0;
Hans Verkuil6652c712012-07-10 08:26:04 -0300204 if (dev->is_fm_band) { /* FM */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300205 test = 102400;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300206 freq = freq / 16; /* Make it kHz */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300207 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;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300213 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300214 test = test >> 1;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300215 }
Hans Verkuil6652c712012-07-10 08:26:04 -0300216 } else { /* AM */
217 fifo = (freq / 16) + 450; /* Make it kHz */
218 fifo |= 0x100000; /* Select AM Band */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300221 /*
222 * Save current volume/mute setting
223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300225 outb(7, dev->io); /* Select tuner control */
226 curvol = inb(dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /*
229 * Tune the card
230 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300231 for (j = 3; j > -1; j--) {
232 cadet_settune(dev, fifo | (j << 16));
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300233
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300234 outb(7, dev->io); /* Select tuner control */
235 outb(curvol, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 msleep(100);
238
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300239 cadet_gettune(dev);
240 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
Hans Verkuil6652c712012-07-10 08:26:04 -0300241 dev->sigstrength = sigtable[dev->is_fm_band][j];
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300242 goto reset_rds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300245 dev->sigstrength = 0;
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300246reset_rds:
247 outb(3, dev->io);
248 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300252static void cadet_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300254 struct cadet *dev = (void *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300256 /* 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)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300260 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300261 outb(0x80, dev->io); /* Select RDS fifo */
262 while ((inb(dev->io) & 0x80) != 0) {
263 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300264 if (dev->rdsin + 1 == dev->rdsout)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300265 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 else
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300267 dev->rdsin++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300269 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
272 /*
273 * Service pending read
274 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300275 if (dev->rdsin != dev->rdsout)
276 wake_up_interruptible(&dev->read_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300278 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * Clean up and exit
280 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300281 init_timer(&dev->readtimer);
282 dev->readtimer.function = cadet_handler;
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300283 dev->readtimer.data = data;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300284 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
285 add_timer(&dev->readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300288static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300299static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300301 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 unsigned char readbuf[RDS_BUFFER];
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300303 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Hans Verkuil1cccee02010-11-14 09:43:52 -0300305 mutex_lock(&dev->lock);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300306 if (dev->rdsstat == 0)
307 cadet_start_rds(dev);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300308 if (dev->rdsin == dev->rdsout) {
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300309 if (file->f_flags & O_NONBLOCK) {
310 i = -EWOULDBLOCK;
311 goto unlock;
312 }
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300313 mutex_unlock(&dev->lock);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300314 interruptible_sleep_on(&dev->read_queue);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300315 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300316 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300317 while (i < count && dev->rdsin != dev->rdsout)
318 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300320 if (i && copy_to_user(data, readbuf, i))
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300321 i = -EFAULT;
322unlock:
323 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return i;
325}
326
327
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300328static int vidioc_querycap(struct file *file, void *priv,
329 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300331 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));
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300334 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
Hans Verkuil8e280f22009-06-20 06:19:09 -0300335 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300336 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Hans Verkuil6652c712012-07-10 08:26:04 -0300340static const struct v4l2_frequency_band bands[] = {
341 {
342 .index = 0,
343 .type = V4L2_TUNER_RADIO,
344 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
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 |
352 V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW |
353 V4L2_TUNER_CAP_FREQ_BANDS,
354 .rangelow = 1400000, /* 87.5 MHz */
355 .rangehigh = 1728000, /* 108.0 MHz */
356 .modulation = V4L2_BAND_MODULATION_FM,
357 },
358};
359
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300360static int vidioc_g_tuner(struct file *file, void *priv,
361 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300363 struct cadet *dev = video_drvdata(file);
364
Hans Verkuil6652c712012-07-10 08:26:04 -0300365 if (v->index)
366 return -EINVAL;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300367 v->type = V4L2_TUNER_RADIO;
Hans Verkuil6652c712012-07-10 08:26:04 -0300368 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) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300373 v->rxsubchans = cadet_getstereo(dev);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300374 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)
379 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
Hans Verkuil6652c712012-07-10 08:26:04 -0300380 } else {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300381 v->rangelow = 8320; /* 520 kHz */
382 v->rangehigh = 26400; /* 1650 kHz */
383 v->rxsubchans = V4L2_TUNER_SUB_MONO;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300384 }
Hans Verkuil6652c712012-07-10 08:26:04 -0300385 v->audmode = V4L2_TUNER_MODE_STEREO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300386 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300387 return 0;
388}
389
390static int vidioc_s_tuner(struct file *file, void *priv,
391 struct v4l2_tuner *v)
392{
Hans Verkuil6652c712012-07-10 08:26:04 -0300393 return v->index ? -EINVAL : 0;
394}
395
396static int vidioc_enum_freq_bands(struct file *file, void *priv,
397 struct v4l2_frequency_band *band)
398{
399 if (band->tuner)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300400 return -EINVAL;
Hans Verkuil6652c712012-07-10 08:26:04 -0300401 if (band->index >= ARRAY_SIZE(bands))
402 return -EINVAL;
403 *band = bands[band->index];
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300404 return 0;
405}
406
407static int vidioc_g_frequency(struct file *file, void *priv,
408 struct v4l2_frequency *f)
409{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300410 struct cadet *dev = video_drvdata(file);
411
Hans Verkuil6652c712012-07-10 08:26:04 -0300412 if (f->tuner)
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300413 return -EINVAL;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300414 f->type = V4L2_TUNER_RADIO;
Hans Verkuil6652c712012-07-10 08:26:04 -0300415 f->frequency = dev->curfreq;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300416 return 0;
417}
418
419
420static int vidioc_s_frequency(struct file *file, void *priv,
421 struct v4l2_frequency *f)
422{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300423 struct cadet *dev = video_drvdata(file);
424
Hans Verkuil6652c712012-07-10 08:26:04 -0300425 if (f->tuner)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300426 return -EINVAL;
Hans Verkuil6652c712012-07-10 08:26:04 -0300427 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);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300431 cadet_setfreq(dev, f->frequency);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300432 return 0;
433}
434
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300435static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300436{
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300437 struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300438
439 switch (ctrl->id) {
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300440 case V4L2_CID_AUDIO_MUTE:
441 outb(7, dev->io); /* Select tuner control */
442 if (ctrl->val)
443 outb(0x00, dev->io + 1);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300444 else
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300445 outb(0x20, dev->io + 1);
446 return 0;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300447 }
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300448 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300449}
450
451static int cadet_open(struct file *file)
452{
453 struct cadet *dev = video_drvdata(file);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300454 int err;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300455
Hans Verkuil1cccee02010-11-14 09:43:52 -0300456 mutex_lock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300457 err = v4l2_fh_open(file);
458 if (err)
459 goto fail;
460 if (v4l2_fh_is_singular_file(file))
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300461 init_waitqueue_head(&dev->read_queue);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300462fail:
Hans Verkuil1cccee02010-11-14 09:43:52 -0300463 mutex_unlock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300464 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300467static int cadet_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300469 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Hans Verkuil1cccee02010-11-14 09:43:52 -0300471 mutex_lock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300472 if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300473 del_timer_sync(&dev->readtimer);
474 dev->rdsstat = 0;
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300475 }
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300476 v4l2_fh_release(file);
Hans Verkuil1cccee02010-11-14 09:43:52 -0300477 mutex_unlock(&dev->lock);
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300478 return 0;
479}
480
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300481static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300482{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300483 struct cadet *dev = video_drvdata(file);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300484 unsigned long req_events = poll_requested_events(wait);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300485 unsigned int res = v4l2_ctrl_poll(file, wait);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300486
487 poll_wait(file, &dev->read_queue, wait);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300488 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 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300494 if (dev->rdsin != dev->rdsout)
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300495 res |= POLLIN | POLLRDNORM;
496 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499
Hans Verkuilbec43662008-12-30 06:58:20 -0300500static const struct v4l2_file_operations cadet_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 .owner = THIS_MODULE,
502 .open = cadet_open,
503 .release = cadet_release,
504 .read = cadet_read,
Hans Verkuil1cccee02010-11-14 09:43:52 -0300505 .unlocked_ioctl = video_ioctl2,
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300506 .poll = cadet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507};
508
Hans Verkuila3998102008-07-21 02:57:38 -0300509static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300510 .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,
Hans Verkuil6652c712012-07-10 08:26:04 -0300515 .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300516 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523};
524
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300525#ifdef CONFIG_PNP
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static struct pnp_device_id cadet_pnp_devices[] = {
528 /* ADS Cadet AM/FM Radio Card */
529 {.id = "MSM0c24", .driver_data = 0},
530 {.id = ""}
531};
532
533MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
534
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300535static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 if (!dev)
538 return -ENODEV;
539 /* only support one device */
540 if (io > 0)
541 return -EBUSY;
542
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300543 if (!pnp_port_valid(dev, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 io = pnp_port_start(dev, 0);
547
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300548 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
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
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300560#else
561static struct pnp_driver cadet_pnp_driver;
562#endif
563
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300564static void cadet_probe(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300566 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int i;
568
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300569 for (i = 0; i < 8; i++) {
570 dev->io = iovals[i];
571 if (request_region(dev->io, 2, "cadet-probe")) {
Hans Verkuil6652c712012-07-10 08:26:04 -0300572 cadet_setfreq(dev, bands[1].rangelow);
573 if (cadet_getfreq(dev) == bands[1].rangelow) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300574 release_region(dev->io, 2);
575 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300577 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300580 dev->io = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300583/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * 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
588static int __init cadet_init(void)
589{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300590 struct cadet *dev = &cadet_card;
591 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300592 struct v4l2_ctrl_handler *hdl;
593 int res = -ENODEV;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300594
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300595 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) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (io < 0)
600 pnp_register_driver(&cadet_pnp_driver);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300601 dev->io = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300603 /* If that fails then probe unsafely if probe is requested */
604 if (dev->io < 0)
605 cadet_probe(dev);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300606
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300607 /* Else we bail out */
608 if (dev->io < 0) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300609#ifdef MODULE
Hans Verkuilb24c20cc2009-03-09 08:11:21 -0300610 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612#endif
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300613 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300615 if (!request_region(dev->io, 2, "cadet"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto fail;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300617
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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto fail;
623 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300624
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300625 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
Hans Verkuil6652c712012-07-10 08:26:04 -0300636 dev->is_fm_band = true;
637 dev->curfreq = bands[dev->is_fm_band].rangelow;
638 cadet_setfreq(dev, dev->curfreq);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300639 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;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300644 dev->vdev.lock = &dev->lock;
645 set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300646 video_set_drvdata(&dev->vdev, dev);
647
Peter Senna Tschudin8ca70802012-09-06 11:23:52 -0300648 res = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
649 if (res < 0)
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300650 goto err_hdl;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300651 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return 0;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300653err_hdl:
654 v4l2_ctrl_handler_free(hdl);
655 v4l2_device_unregister(v4l2_dev);
656 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657fail:
658 pnp_unregister_driver(&cadet_pnp_driver);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300659 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300662static void __exit cadet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300664 struct cadet *dev = &cadet_card;
665
666 video_unregister_device(&dev->vdev);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300667 v4l2_ctrl_handler_free(&dev->ctrl_handler);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300668 v4l2_device_unregister(&dev->v4l2_dev);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300669 outb(7, dev->io); /* Mute */
670 outb(0x00, dev->io + 1);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300671 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 pnp_unregister_driver(&cadet_pnp_driver);
673}
674
675module_init(cadet_init);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300676module_exit(cadet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677