blob: b701ea6e7c7379fc0593fae6c22c4678152cd4e7 [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
Andrew Mortond591b9c2006-08-08 10:52:22 -030033#include <linux/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h> /* Modules */
35#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070036#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/delay.h> /* udelay */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030038#include <linux/videodev2.h> /* V4L2 API defs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/param.h>
40#include <linux/pnp.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040041#include <linux/sched.h>
Hans Verkuilbec2aec2009-03-06 13:48:47 -030042#include <linux/io.h> /* outb, outb_p */
Hans Verkuilbec2aec2009-03-06 13:48:47 -030043#include <media/v4l2-device.h>
44#include <media/v4l2-ioctl.h>
45
46MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
47MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
48MODULE_LICENSE("GPL");
49
50static int io = -1; /* default to isapnp activation */
51static int radio_nr = -1;
52
53module_param(io, int, 0);
54MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
55module_param(radio_nr, int, 0);
56
57#define CADET_VERSION KERNEL_VERSION(0, 3, 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define RDS_BUFFER 256
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030060#define RDS_RX_FLAG 1
61#define MBS_RX_FLAG 2
62
Hans Verkuilbec2aec2009-03-06 13:48:47 -030063struct cadet {
64 struct v4l2_device v4l2_dev;
65 struct video_device vdev;
66 int io;
67 int users;
68 int curtuner;
69 int tunestat;
70 int sigstrength;
71 wait_queue_head_t read_queue;
72 struct timer_list readtimer;
73 __u8 rdsin, rdsout, rdsstat;
74 unsigned char rdsbuf[RDS_BUFFER];
75 struct mutex lock;
76 int reading;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -030077};
78
Hans Verkuilbec2aec2009-03-06 13:48:47 -030079static struct cadet cadet_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * Signal Strength Threshold Values
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030083 * The V4L API spec does not define any particular unit for the signal
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * strength value. These values are in microvolts of RF at the tuner's input.
85 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -030086static __u16 sigtable[2][4] = {
87 { 5, 10, 30, 150 },
88 { 28, 40, 63, 1000 }
89};
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030091
Hans Verkuilbec2aec2009-03-06 13:48:47 -030092static int cadet_getstereo(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030094 int ret = V4L2_TUNER_SUB_MONO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030095
96 if (dev->curtuner != 0) /* Only FM has stereo capability! */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030097 return V4L2_TUNER_SUB_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Hans Verkuilbec2aec2009-03-06 13:48:47 -030099 mutex_lock(&dev->lock);
100 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;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300103 mutex_unlock(&dev->lock);
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 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300117
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300118 outb(7, dev->io); /* Select tuner control */
119 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
120 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
121 dev->tunestat = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300123 /*
124 * Read the shift register
125 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300126 for (i = 0; i < 25; i++) {
127 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
128 if (i < 24) {
129 outb(0x01, dev->io + 1);
130 dev->tunestat &= inb(dev->io + 1);
131 outb(0x00, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300132 }
133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300135 /*
136 * Restore volume/mute setting
137 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300138 outb(curvol, dev->io + 1);
139 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return fifo;
142}
143
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300144static unsigned cadet_getfreq(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300146 int i;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300147 unsigned freq = 0, test, fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /*
150 * Read current tuning
151 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300152 fifo = cadet_gettune(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300154 /*
155 * Convert to actual frequency
156 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300157 if (dev->curtuner == 0) { /* FM */
158 test = 12500;
159 for (i = 0; i < 14; i++) {
160 if ((fifo & 0x01) != 0)
161 freq += test;
162 test = test << 1;
163 fifo = fifo >> 1;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300164 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300165 freq -= 10700000; /* IF frequency is 10.7 MHz */
166 freq = (freq * 16) / 1000000; /* Make it 1/16 MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300168 if (dev->curtuner == 1) /* AM */
169 freq = ((fifo & 0x7fff) - 2010) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300171 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300174static void cadet_settune(struct cadet *dev, unsigned fifo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300176 int i;
177 unsigned test;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300179 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300180
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300181 outb(7, dev->io); /* Select tuner control */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /*
183 * Write the shift register
184 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300185 test = 0;
186 test = (fifo >> 23) & 0x02; /* Align data for SDO */
187 test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
188 outb(7, dev->io); /* Select tuner control */
189 outb(test, dev->io + 1); /* Initialize for write */
190 for (i = 0; i < 25; i++) {
191 test |= 0x01; /* Toggle SCK High */
192 outb(test, dev->io + 1);
193 test &= 0xfe; /* Toggle SCK Low */
194 outb(test, dev->io + 1);
195 fifo = fifo << 1; /* Prepare the next bit */
196 test = 0x1c | ((fifo >> 23) & 0x02);
197 outb(test, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300199 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300202static void cadet_setfreq(struct cadet *dev, unsigned freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300204 unsigned fifo;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300205 int i, j, test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300206 int curvol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300208 /*
209 * Formulate a fifo command
210 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300211 fifo = 0;
212 if (dev->curtuner == 0) { /* FM */
213 test = 102400;
214 freq = (freq * 1000) / 16; /* Make it kHz */
215 freq += 10700; /* IF is 10700 kHz */
216 for (i = 0; i < 14; i++) {
217 fifo = fifo << 1;
218 if (freq >= test) {
219 fifo |= 0x01;
220 freq -= test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300221 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300222 test = test >> 1;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300225 if (dev->curtuner == 1) { /* AM */
226 fifo = (freq / 16) + 2010; /* Make it kHz */
227 fifo |= 0x100000; /* Select AM Band */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300230 /*
231 * Save current volume/mute setting
232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300234 mutex_lock(&dev->lock);
235 outb(7, dev->io); /* Select tuner control */
236 curvol = inb(dev->io + 1);
237 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /*
240 * Tune the card
241 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300242 for (j = 3; j > -1; j--) {
243 cadet_settune(dev, fifo | (j << 16));
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300244
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300245 mutex_lock(&dev->lock);
246 outb(7, dev->io); /* Select tuner control */
247 outb(curvol, dev->io + 1);
248 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 msleep(100);
251
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300252 cadet_gettune(dev);
253 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
254 dev->sigstrength = sigtable[dev->curtuner][j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return;
256 }
257 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300258 dev->sigstrength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300262static int cadet_getvol(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 int ret = 0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300265
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300266 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300267
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300268 outb(7, dev->io); /* Select tuner control */
269 if ((inb(dev->io + 1) & 0x20) != 0)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300270 ret = 0xffff;
271
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300272 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300273 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300277static void cadet_setvol(struct cadet *dev, int vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300279 mutex_lock(&dev->lock);
280 outb(7, dev->io); /* Select tuner control */
281 if (vol > 0)
282 outb(0x20, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300283 else
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300284 outb(0x00, dev->io + 1);
285 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300288static void cadet_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300290 struct cadet *dev = (void *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300292 /* Service the RDS fifo */
293 if (mutex_trylock(&dev->lock)) {
294 outb(0x3, dev->io); /* Select RDS Decoder Control */
295 if ((inb(dev->io + 1) & 0x20) != 0)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300296 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300297 outb(0x80, dev->io); /* Select RDS fifo */
298 while ((inb(dev->io) & 0x80) != 0) {
299 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
300 if (dev->rdsin == dev->rdsout)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300301 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 else
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300303 dev->rdsin++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300305 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
308 /*
309 * Service pending read
310 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300311 if (dev->rdsin != dev->rdsout)
312 wake_up_interruptible(&dev->read_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300314 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * Clean up and exit
316 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300317 init_timer(&dev->readtimer);
318 dev->readtimer.function = cadet_handler;
319 dev->readtimer.data = (unsigned long)0;
320 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
321 add_timer(&dev->readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300325static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300327 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 unsigned char readbuf[RDS_BUFFER];
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300329 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300331 if (dev->rdsstat == 0) {
332 mutex_lock(&dev->lock);
333 dev->rdsstat = 1;
334 outb(0x80, dev->io); /* Select RDS fifo */
335 mutex_unlock(&dev->lock);
336 init_timer(&dev->readtimer);
337 dev->readtimer.function = cadet_handler;
338 dev->readtimer.data = (unsigned long)dev;
339 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
340 add_timer(&dev->readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300342 if (dev->rdsin == dev->rdsout) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300343 if (file->f_flags & O_NONBLOCK)
344 return -EWOULDBLOCK;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300345 interruptible_sleep_on(&dev->read_queue);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300346 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300347 while (i < count && dev->rdsin != dev->rdsout)
348 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300350 if (copy_to_user(data, readbuf, i))
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300351 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return i;
353}
354
355
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300356static int vidioc_querycap(struct file *file, void *priv,
357 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300359 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
360 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
361 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300362 v->version = CADET_VERSION;
Hans Verkuil8e280f22009-06-20 06:19:09 -0300363 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
364 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300368static int vidioc_g_tuner(struct file *file, void *priv,
369 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300371 struct cadet *dev = video_drvdata(file);
372
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300373 v->type = V4L2_TUNER_RADIO;
374 switch (v->index) {
375 case 0:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300376 strlcpy(v->name, "FM", sizeof(v->name));
Matti Aaltonencb0ed222010-10-18 06:54:14 -0300377 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
378 V4L2_TUNER_CAP_RDS_BLOCK_IO;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300379 v->rangelow = 1400; /* 87.5 MHz */
380 v->rangehigh = 1728; /* 108.0 MHz */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300381 v->rxsubchans = cadet_getstereo(dev);
382 switch (v->rxsubchans) {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300383 case V4L2_TUNER_SUB_MONO:
384 v->audmode = V4L2_TUNER_MODE_MONO;
385 break;
386 case V4L2_TUNER_SUB_STEREO:
387 v->audmode = V4L2_TUNER_MODE_STEREO;
388 break;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300389 default:
390 break;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300391 }
Hans Verkuil8e280f22009-06-20 06:19:09 -0300392 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300393 break;
394 case 1:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300395 strlcpy(v->name, "AM", sizeof(v->name));
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300396 v->capability = V4L2_TUNER_CAP_LOW;
397 v->rangelow = 8320; /* 520 kHz */
398 v->rangehigh = 26400; /* 1650 kHz */
399 v->rxsubchans = V4L2_TUNER_SUB_MONO;
400 v->audmode = V4L2_TUNER_MODE_MONO;
401 break;
402 default:
403 return -EINVAL;
404 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300405 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300406 return 0;
407}
408
409static int vidioc_s_tuner(struct file *file, void *priv,
410 struct v4l2_tuner *v)
411{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300412 struct cadet *dev = video_drvdata(file);
413
414 if (v->index != 0 && v->index != 1)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300415 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300416 dev->curtuner = v->index;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300417 return 0;
418}
419
420static int vidioc_g_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
425 f->tuner = dev->curtuner;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300426 f->type = V4L2_TUNER_RADIO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300427 f->frequency = cadet_getfreq(dev);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300428 return 0;
429}
430
431
432static int vidioc_s_frequency(struct file *file, void *priv,
433 struct v4l2_frequency *f)
434{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300435 struct cadet *dev = video_drvdata(file);
436
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300437 if (f->type != V4L2_TUNER_RADIO)
438 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300439 if (dev->curtuner == 0 && (f->frequency < 1400 || f->frequency > 1728))
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300440 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300441 if (dev->curtuner == 1 && (f->frequency < 8320 || f->frequency > 26400))
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300442 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300443 cadet_setfreq(dev, f->frequency);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300444 return 0;
445}
446
447static int vidioc_queryctrl(struct file *file, void *priv,
448 struct v4l2_queryctrl *qc)
449{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300450 switch (qc->id) {
451 case V4L2_CID_AUDIO_MUTE:
452 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
453 case V4L2_CID_AUDIO_VOLUME:
454 return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300455 }
456 return -EINVAL;
457}
458
459static int vidioc_g_ctrl(struct file *file, void *priv,
460 struct v4l2_control *ctrl)
461{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300462 struct cadet *dev = video_drvdata(file);
463
464 switch (ctrl->id) {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300465 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300466 ctrl->value = (cadet_getvol(dev) == 0);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300467 break;
468 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300469 ctrl->value = cadet_getvol(dev);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300470 break;
471 default:
472 return -EINVAL;
473 }
474 return 0;
475}
476
477static int vidioc_s_ctrl(struct file *file, void *priv,
478 struct v4l2_control *ctrl)
479{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300480 struct cadet *dev = video_drvdata(file);
481
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300482 switch (ctrl->id){
483 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
484 if (ctrl->value)
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300485 cadet_setvol(dev, 0);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300486 else
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300487 cadet_setvol(dev, 0xffff);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300488 break;
489 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300490 cadet_setvol(dev, ctrl->value);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300491 break;
492 default:
493 return -EINVAL;
494 }
495 return 0;
496}
497
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300498static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
499{
500 *i = 0;
501 return 0;
502}
503
504static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
505{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300506 return i ? -EINVAL : 0;
507}
508
509static int vidioc_g_audio(struct file *file, void *priv,
510 struct v4l2_audio *a)
511{
512 a->index = 0;
513 strlcpy(a->name, "Radio", sizeof(a->name));
514 a->capability = V4L2_AUDCAP_STEREO;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300515 return 0;
516}
517
518static int vidioc_s_audio(struct file *file, void *priv,
519 struct v4l2_audio *a)
520{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300521 return a->index ? -EINVAL : 0;
522}
523
524static int cadet_open(struct file *file)
525{
526 struct cadet *dev = video_drvdata(file);
527
528 dev->users++;
529 if (1 == dev->users)
530 init_waitqueue_head(&dev->read_queue);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300534static int cadet_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300536 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300538 dev->users--;
539 if (0 == dev->users) {
540 del_timer_sync(&dev->readtimer);
541 dev->rdsstat = 0;
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300542 }
543 return 0;
544}
545
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300546static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300547{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300548 struct cadet *dev = video_drvdata(file);
549
550 poll_wait(file, &dev->read_queue, wait);
551 if (dev->rdsin != dev->rdsout)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300552 return POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
554}
555
556
Hans Verkuilbec43662008-12-30 06:58:20 -0300557static const struct v4l2_file_operations cadet_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 .owner = THIS_MODULE,
559 .open = cadet_open,
560 .release = cadet_release,
561 .read = cadet_read,
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300562 .ioctl = video_ioctl2,
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300563 .poll = cadet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564};
565
Hans Verkuila3998102008-07-21 02:57:38 -0300566static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300567 .vidioc_querycap = vidioc_querycap,
568 .vidioc_g_tuner = vidioc_g_tuner,
569 .vidioc_s_tuner = vidioc_s_tuner,
570 .vidioc_g_frequency = vidioc_g_frequency,
571 .vidioc_s_frequency = vidioc_s_frequency,
572 .vidioc_queryctrl = vidioc_queryctrl,
573 .vidioc_g_ctrl = vidioc_g_ctrl,
574 .vidioc_s_ctrl = vidioc_s_ctrl,
575 .vidioc_g_audio = vidioc_g_audio,
576 .vidioc_s_audio = vidioc_s_audio,
577 .vidioc_g_input = vidioc_g_input,
578 .vidioc_s_input = vidioc_s_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579};
580
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300581#ifdef CONFIG_PNP
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static struct pnp_device_id cadet_pnp_devices[] = {
584 /* ADS Cadet AM/FM Radio Card */
585 {.id = "MSM0c24", .driver_data = 0},
586 {.id = ""}
587};
588
589MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
590
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300591static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 if (!dev)
594 return -ENODEV;
595 /* only support one device */
596 if (io > 0)
597 return -EBUSY;
598
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300599 if (!pnp_port_valid(dev, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 io = pnp_port_start(dev, 0);
603
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300604 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 return io;
607}
608
609static struct pnp_driver cadet_pnp_driver = {
610 .name = "radio-cadet",
611 .id_table = cadet_pnp_devices,
612 .probe = cadet_pnp_probe,
613 .remove = NULL,
614};
615
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300616#else
617static struct pnp_driver cadet_pnp_driver;
618#endif
619
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300620static void cadet_probe(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300622 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int i;
624
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300625 for (i = 0; i < 8; i++) {
626 dev->io = iovals[i];
627 if (request_region(dev->io, 2, "cadet-probe")) {
628 cadet_setfreq(dev, 1410);
629 if (cadet_getfreq(dev) == 1410) {
630 release_region(dev->io, 2);
631 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300633 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300636 dev->io = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300639/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * io should only be set if the user has used something like
641 * isapnp (the userspace program) to initialize this card for us
642 */
643
644static int __init cadet_init(void)
645{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300646 struct cadet *dev = &cadet_card;
647 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
648 int res;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300649
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300650 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
651 mutex_init(&dev->lock);
652
653 /* If a probe was requested then probe ISAPnP first (safest) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (io < 0)
655 pnp_register_driver(&cadet_pnp_driver);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300656 dev->io = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300658 /* If that fails then probe unsafely if probe is requested */
659 if (dev->io < 0)
660 cadet_probe(dev);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300661
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300662 /* Else we bail out */
663 if (dev->io < 0) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300664#ifdef MODULE
Hans Verkuilb24c20cc2009-03-09 08:11:21 -0300665 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
666 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300668 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300670 if (!request_region(dev->io, 2, "cadet"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto fail;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300672
673 res = v4l2_device_register(NULL, v4l2_dev);
674 if (res < 0) {
675 release_region(dev->io, 2);
676 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 goto fail;
678 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300679
680 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
681 dev->vdev.v4l2_dev = v4l2_dev;
682 dev->vdev.fops = &cadet_fops;
683 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
684 dev->vdev.release = video_device_release_empty;
685 video_set_drvdata(&dev->vdev, dev);
686
687 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
688 v4l2_device_unregister(v4l2_dev);
689 release_region(dev->io, 2);
690 goto fail;
691 }
692 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
694fail:
695 pnp_unregister_driver(&cadet_pnp_driver);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300696 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300699static void __exit cadet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300701 struct cadet *dev = &cadet_card;
702
703 video_unregister_device(&dev->vdev);
704 v4l2_device_unregister(&dev->v4l2_dev);
705 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 pnp_unregister_driver(&cadet_pnp_driver);
707}
708
709module_init(cadet_init);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300710module_exit(cadet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711