blob: 93536b7e75c7770e975bf008090b6747117ddaf6 [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 Verkuilbec2aec2009-03-06 13:48:47 -030069 int curtuner;
70 int tunestat;
71 int sigstrength;
72 wait_queue_head_t read_queue;
73 struct timer_list readtimer;
74 __u8 rdsin, rdsout, rdsstat;
75 unsigned char rdsbuf[RDS_BUFFER];
76 struct mutex lock;
77 int reading;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -030078};
79
Hans Verkuilbec2aec2009-03-06 13:48:47 -030080static struct cadet cadet_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * Signal Strength Threshold Values
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030084 * The V4L API spec does not define any particular unit for the signal
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * strength value. These values are in microvolts of RF at the tuner's input.
86 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -030087static __u16 sigtable[2][4] = {
88 { 5, 10, 30, 150 },
89 { 28, 40, 63, 1000 }
90};
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030092
Hans Verkuilbec2aec2009-03-06 13:48:47 -030093static int cadet_getstereo(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030095 int ret = V4L2_TUNER_SUB_MONO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030096
97 if (dev->curtuner != 0) /* Only FM has stereo capability! */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030098 return V4L2_TUNER_SUB_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300100 outb(7, dev->io); /* Select tuner control */
101 if ((inb(dev->io + 1) & 0x40) == 0)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300102 ret = V4L2_TUNER_SUB_STEREO;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300103 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300106static unsigned cadet_gettune(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300108 int curvol, i;
109 unsigned fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300111 /*
112 * Prepare for read
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300115 outb(7, dev->io); /* Select tuner control */
116 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
117 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
118 dev->tunestat = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300120 /*
121 * Read the shift register
122 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300123 for (i = 0; i < 25; i++) {
124 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
125 if (i < 24) {
126 outb(0x01, dev->io + 1);
127 dev->tunestat &= inb(dev->io + 1);
128 outb(0x00, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300129 }
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300132 /*
133 * Restore volume/mute setting
134 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300135 outb(curvol, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return fifo;
137}
138
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300139static unsigned cadet_getfreq(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300141 int i;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300142 unsigned freq = 0, test, fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /*
145 * Read current tuning
146 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300147 fifo = cadet_gettune(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300149 /*
150 * Convert to actual frequency
151 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300152 if (dev->curtuner == 0) { /* FM */
153 test = 12500;
154 for (i = 0; i < 14; i++) {
155 if ((fifo & 0x01) != 0)
156 freq += test;
157 test = test << 1;
158 fifo = fifo >> 1;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300159 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300160 freq -= 10700000; /* IF frequency is 10.7 MHz */
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300161 freq = (freq * 16) / 1000; /* Make it 1/16 kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300163 if (dev->curtuner == 1) /* AM */
164 freq = ((fifo & 0x7fff) - 2010) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300166 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300169static void cadet_settune(struct cadet *dev, unsigned fifo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300171 int i;
172 unsigned test;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300174 outb(7, dev->io); /* Select tuner control */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /*
176 * Write the shift register
177 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300178 test = 0;
179 test = (fifo >> 23) & 0x02; /* Align data for SDO */
180 test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
181 outb(7, dev->io); /* Select tuner control */
182 outb(test, dev->io + 1); /* Initialize for write */
183 for (i = 0; i < 25; i++) {
184 test |= 0x01; /* Toggle SCK High */
185 outb(test, dev->io + 1);
186 test &= 0xfe; /* Toggle SCK Low */
187 outb(test, dev->io + 1);
188 fifo = fifo << 1; /* Prepare the next bit */
189 test = 0x1c | ((fifo >> 23) & 0x02);
190 outb(test, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300194static void cadet_setfreq(struct cadet *dev, unsigned freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300196 unsigned fifo;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300197 int i, j, test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300198 int curvol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
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;
204 if (dev->curtuner == 0) { /* FM */
205 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300217 if (dev->curtuner == 1) { /* AM */
218 fifo = (freq / 16) + 2010; /* Make it kHz */
219 fifo |= 0x100000; /* Select AM Band */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300222 /*
223 * Save current volume/mute setting
224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300226 outb(7, dev->io); /* Select tuner control */
227 curvol = inb(dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /*
230 * Tune the card
231 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300232 for (j = 3; j > -1; j--) {
233 cadet_settune(dev, fifo | (j << 16));
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300234
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300235 outb(7, dev->io); /* Select tuner control */
236 outb(curvol, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 msleep(100);
239
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300240 cadet_gettune(dev);
241 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
242 dev->sigstrength = sigtable[dev->curtuner][j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return;
244 }
245 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300246 dev->sigstrength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300250static void cadet_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300252 struct cadet *dev = (void *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300254 /* Service the RDS fifo */
255 if (mutex_trylock(&dev->lock)) {
256 outb(0x3, dev->io); /* Select RDS Decoder Control */
257 if ((inb(dev->io + 1) & 0x20) != 0)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300258 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300259 outb(0x80, dev->io); /* Select RDS fifo */
260 while ((inb(dev->io) & 0x80) != 0) {
261 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
262 if (dev->rdsin == dev->rdsout)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300263 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 else
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300265 dev->rdsin++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300267 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
270 /*
271 * Service pending read
272 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300273 if (dev->rdsin != dev->rdsout)
274 wake_up_interruptible(&dev->read_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300276 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * Clean up and exit
278 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300279 init_timer(&dev->readtimer);
280 dev->readtimer.function = cadet_handler;
281 dev->readtimer.data = (unsigned long)0;
282 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
283 add_timer(&dev->readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300287static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300289 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned char readbuf[RDS_BUFFER];
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300291 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Hans Verkuil1cccee02010-11-14 09:43:52 -0300293 mutex_lock(&dev->lock);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300294 if (dev->rdsstat == 0) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300295 dev->rdsstat = 1;
296 outb(0x80, dev->io); /* Select RDS fifo */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300297 init_timer(&dev->readtimer);
298 dev->readtimer.function = cadet_handler;
299 dev->readtimer.data = (unsigned long)dev;
300 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
301 add_timer(&dev->readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300303 if (dev->rdsin == dev->rdsout) {
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300304 if (file->f_flags & O_NONBLOCK) {
305 i = -EWOULDBLOCK;
306 goto unlock;
307 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300308 interruptible_sleep_on(&dev->read_queue);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300309 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300310 while (i < count && dev->rdsin != dev->rdsout)
311 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300313 if (copy_to_user(data, readbuf, i))
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300314 i = -EFAULT;
315unlock:
316 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return i;
318}
319
320
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300321static int vidioc_querycap(struct file *file, void *priv,
322 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300324 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
325 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
326 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300327 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
Hans Verkuil8e280f22009-06-20 06:19:09 -0300328 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300329 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300333static int vidioc_g_tuner(struct file *file, void *priv,
334 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300336 struct cadet *dev = video_drvdata(file);
337
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300338 v->type = V4L2_TUNER_RADIO;
339 switch (v->index) {
340 case 0:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300341 strlcpy(v->name, "FM", sizeof(v->name));
Matti Aaltonencb0ed222010-10-18 06:54:14 -0300342 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300343 V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW;
344 v->rangelow = 1400000; /* 87.5 MHz */
345 v->rangehigh = 1728000; /* 108.0 MHz */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300346 v->rxsubchans = cadet_getstereo(dev);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300347 v->audmode = V4L2_TUNER_MODE_STEREO;
Hans Verkuil8e280f22009-06-20 06:19:09 -0300348 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300349 break;
350 case 1:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300351 strlcpy(v->name, "AM", sizeof(v->name));
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300352 v->capability = V4L2_TUNER_CAP_LOW;
353 v->rangelow = 8320; /* 520 kHz */
354 v->rangehigh = 26400; /* 1650 kHz */
355 v->rxsubchans = V4L2_TUNER_SUB_MONO;
356 v->audmode = V4L2_TUNER_MODE_MONO;
357 break;
358 default:
359 return -EINVAL;
360 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300361 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300362 return 0;
363}
364
365static int vidioc_s_tuner(struct file *file, void *priv,
366 struct v4l2_tuner *v)
367{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300368 if (v->index != 0 && v->index != 1)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300369 return -EINVAL;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300370 return 0;
371}
372
373static int vidioc_g_frequency(struct file *file, void *priv,
374 struct v4l2_frequency *f)
375{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300376 struct cadet *dev = video_drvdata(file);
377
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300378 if (f->tuner > 1)
379 return -EINVAL;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300380 f->type = V4L2_TUNER_RADIO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300381 f->frequency = cadet_getfreq(dev);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300382 return 0;
383}
384
385
386static int vidioc_s_frequency(struct file *file, void *priv,
387 struct v4l2_frequency *f)
388{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300389 struct cadet *dev = video_drvdata(file);
390
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300391 if (f->type != V4L2_TUNER_RADIO)
392 return -EINVAL;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300393 if (f->tuner == 0) {
394 if (f->frequency < 1400000)
395 f->frequency = 1400000;
396 else if (f->frequency > 1728000)
397 f->frequency = 1728000;
398 } else if (f->tuner == 1) {
399 if (f->frequency < 8320)
400 f->frequency = 8320;
401 else if (f->frequency > 26400)
402 f->frequency = 26400;
403 } else
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300404 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300405 cadet_setfreq(dev, f->frequency);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300406 return 0;
407}
408
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300409static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300410{
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300411 struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300412
413 switch (ctrl->id) {
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300414 case V4L2_CID_AUDIO_MUTE:
415 outb(7, dev->io); /* Select tuner control */
416 if (ctrl->val)
417 outb(0x00, dev->io + 1);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300418 else
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300419 outb(0x20, dev->io + 1);
420 return 0;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300421 }
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300422 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300423}
424
425static int cadet_open(struct file *file)
426{
427 struct cadet *dev = video_drvdata(file);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300428 int err;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300429
Hans Verkuil1cccee02010-11-14 09:43:52 -0300430 mutex_lock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300431 err = v4l2_fh_open(file);
432 if (err)
433 goto fail;
434 if (v4l2_fh_is_singular_file(file))
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300435 init_waitqueue_head(&dev->read_queue);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300436fail:
Hans Verkuil1cccee02010-11-14 09:43:52 -0300437 mutex_unlock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300441static int cadet_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300443 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Hans Verkuil1cccee02010-11-14 09:43:52 -0300445 mutex_lock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300446 if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300447 del_timer_sync(&dev->readtimer);
448 dev->rdsstat = 0;
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300449 }
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300450 v4l2_fh_release(file);
Hans Verkuil1cccee02010-11-14 09:43:52 -0300451 mutex_unlock(&dev->lock);
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300452 return 0;
453}
454
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300455static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300456{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300457 struct cadet *dev = video_drvdata(file);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300458 unsigned int res = v4l2_ctrl_poll(file, wait);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300459
460 poll_wait(file, &dev->read_queue, wait);
461 if (dev->rdsin != dev->rdsout)
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300462 res |= POLLIN | POLLRDNORM;
463 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466
Hans Verkuilbec43662008-12-30 06:58:20 -0300467static const struct v4l2_file_operations cadet_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 .owner = THIS_MODULE,
469 .open = cadet_open,
470 .release = cadet_release,
471 .read = cadet_read,
Hans Verkuil1cccee02010-11-14 09:43:52 -0300472 .unlocked_ioctl = video_ioctl2,
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300473 .poll = cadet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474};
475
Hans Verkuila3998102008-07-21 02:57:38 -0300476static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300477 .vidioc_querycap = vidioc_querycap,
478 .vidioc_g_tuner = vidioc_g_tuner,
479 .vidioc_s_tuner = vidioc_s_tuner,
480 .vidioc_g_frequency = vidioc_g_frequency,
481 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300482 .vidioc_log_status = v4l2_ctrl_log_status,
483 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
484 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
485};
486
487static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
488 .s_ctrl = cadet_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489};
490
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300491#ifdef CONFIG_PNP
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static struct pnp_device_id cadet_pnp_devices[] = {
494 /* ADS Cadet AM/FM Radio Card */
495 {.id = "MSM0c24", .driver_data = 0},
496 {.id = ""}
497};
498
499MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
500
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300501static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 if (!dev)
504 return -ENODEV;
505 /* only support one device */
506 if (io > 0)
507 return -EBUSY;
508
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300509 if (!pnp_port_valid(dev, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 io = pnp_port_start(dev, 0);
513
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300514 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 return io;
517}
518
519static struct pnp_driver cadet_pnp_driver = {
520 .name = "radio-cadet",
521 .id_table = cadet_pnp_devices,
522 .probe = cadet_pnp_probe,
523 .remove = NULL,
524};
525
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300526#else
527static struct pnp_driver cadet_pnp_driver;
528#endif
529
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300530static void cadet_probe(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300532 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int i;
534
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300535 for (i = 0; i < 8; i++) {
536 dev->io = iovals[i];
537 if (request_region(dev->io, 2, "cadet-probe")) {
538 cadet_setfreq(dev, 1410);
539 if (cadet_getfreq(dev) == 1410) {
540 release_region(dev->io, 2);
541 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300543 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300546 dev->io = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300549/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * io should only be set if the user has used something like
551 * isapnp (the userspace program) to initialize this card for us
552 */
553
554static int __init cadet_init(void)
555{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300556 struct cadet *dev = &cadet_card;
557 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300558 struct v4l2_ctrl_handler *hdl;
559 int res = -ENODEV;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300560
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300561 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
562 mutex_init(&dev->lock);
563
564 /* If a probe was requested then probe ISAPnP first (safest) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (io < 0)
566 pnp_register_driver(&cadet_pnp_driver);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300567 dev->io = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300569 /* If that fails then probe unsafely if probe is requested */
570 if (dev->io < 0)
571 cadet_probe(dev);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300572
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300573 /* Else we bail out */
574 if (dev->io < 0) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300575#ifdef MODULE
Hans Verkuilb24c20cc2009-03-09 08:11:21 -0300576 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
577 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300579 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300581 if (!request_region(dev->io, 2, "cadet"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto fail;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300583
584 res = v4l2_device_register(NULL, v4l2_dev);
585 if (res < 0) {
586 release_region(dev->io, 2);
587 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto fail;
589 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300590
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300591 hdl = &dev->ctrl_handler;
592 v4l2_ctrl_handler_init(hdl, 2);
593 v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
594 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
595 v4l2_dev->ctrl_handler = hdl;
596 if (hdl->error) {
597 res = hdl->error;
598 v4l2_err(v4l2_dev, "Could not register controls\n");
599 goto err_hdl;
600 }
601
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300602 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
603 dev->vdev.v4l2_dev = v4l2_dev;
604 dev->vdev.fops = &cadet_fops;
605 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
606 dev->vdev.release = video_device_release_empty;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300607 dev->vdev.lock = &dev->lock;
608 set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300609 video_set_drvdata(&dev->vdev, dev);
610
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300611 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
612 goto err_hdl;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300613 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 0;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300615err_hdl:
616 v4l2_ctrl_handler_free(hdl);
617 v4l2_device_unregister(v4l2_dev);
618 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619fail:
620 pnp_unregister_driver(&cadet_pnp_driver);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300621 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300624static void __exit cadet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300626 struct cadet *dev = &cadet_card;
627
628 video_unregister_device(&dev->vdev);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300629 v4l2_ctrl_handler_free(&dev->ctrl_handler);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300630 v4l2_device_unregister(&dev->v4l2_dev);
631 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 pnp_unregister_driver(&cadet_pnp_driver);
633}
634
635module_init(cadet_init);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300636module_exit(cadet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637