blob: 8b1440136c45b10710079bdbf2e80fba7f166978 [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>
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>
44
45MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
46MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
47MODULE_LICENSE("GPL");
48
49static int io = -1; /* default to isapnp activation */
50static int radio_nr = -1;
51
52module_param(io, int, 0);
53MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
54module_param(radio_nr, int, 0);
55
56#define CADET_VERSION KERNEL_VERSION(0, 3, 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define RDS_BUFFER 256
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030059#define RDS_RX_FLAG 1
60#define MBS_RX_FLAG 2
61
Hans Verkuilbec2aec2009-03-06 13:48:47 -030062struct cadet {
63 struct v4l2_device v4l2_dev;
64 struct video_device vdev;
65 int io;
66 int users;
67 int curtuner;
68 int tunestat;
69 int sigstrength;
70 wait_queue_head_t read_queue;
71 struct timer_list readtimer;
72 __u8 rdsin, rdsout, rdsstat;
73 unsigned char rdsbuf[RDS_BUFFER];
74 struct mutex lock;
75 int reading;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -030076};
77
Hans Verkuilbec2aec2009-03-06 13:48:47 -030078static struct cadet cadet_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 * Signal Strength Threshold Values
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030082 * The V4L API spec does not define any particular unit for the signal
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * strength value. These values are in microvolts of RF at the tuner's input.
84 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -030085static __u16 sigtable[2][4] = {
86 { 5, 10, 30, 150 },
87 { 28, 40, 63, 1000 }
88};
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030090
Hans Verkuilbec2aec2009-03-06 13:48:47 -030091static int cadet_getstereo(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030093 int ret = V4L2_TUNER_SUB_MONO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030094
95 if (dev->curtuner != 0) /* Only FM has stereo capability! */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030096 return V4L2_TUNER_SUB_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Hans Verkuilbec2aec2009-03-06 13:48:47 -030098 mutex_lock(&dev->lock);
99 outb(7, dev->io); /* Select tuner control */
100 if ((inb(dev->io + 1) & 0x40) == 0)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300101 ret = V4L2_TUNER_SUB_STEREO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300102 mutex_unlock(&dev->lock);
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 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300116
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300117 outb(7, dev->io); /* Select tuner control */
118 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
119 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
120 dev->tunestat = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300122 /*
123 * Read the shift register
124 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300125 for (i = 0; i < 25; i++) {
126 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
127 if (i < 24) {
128 outb(0x01, dev->io + 1);
129 dev->tunestat &= inb(dev->io + 1);
130 outb(0x00, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300131 }
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300134 /*
135 * Restore volume/mute setting
136 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300137 outb(curvol, dev->io + 1);
138 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 return fifo;
141}
142
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300143static unsigned cadet_getfreq(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300145 int i;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300146 unsigned freq = 0, test, fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /*
149 * Read current tuning
150 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300151 fifo = cadet_gettune(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300153 /*
154 * Convert to actual frequency
155 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300156 if (dev->curtuner == 0) { /* FM */
157 test = 12500;
158 for (i = 0; i < 14; i++) {
159 if ((fifo & 0x01) != 0)
160 freq += test;
161 test = test << 1;
162 fifo = fifo >> 1;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300163 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300164 freq -= 10700000; /* IF frequency is 10.7 MHz */
165 freq = (freq * 16) / 1000000; /* Make it 1/16 MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300167 if (dev->curtuner == 1) /* AM */
168 freq = ((fifo & 0x7fff) - 2010) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300170 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300173static void cadet_settune(struct cadet *dev, unsigned fifo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300175 int i;
176 unsigned test;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300178 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300179
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300180 outb(7, dev->io); /* Select tuner control */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
182 * Write the shift register
183 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300184 test = 0;
185 test = (fifo >> 23) & 0x02; /* Align data for SDO */
186 test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
187 outb(7, dev->io); /* Select tuner control */
188 outb(test, dev->io + 1); /* Initialize for write */
189 for (i = 0; i < 25; i++) {
190 test |= 0x01; /* Toggle SCK High */
191 outb(test, dev->io + 1);
192 test &= 0xfe; /* Toggle SCK Low */
193 outb(test, dev->io + 1);
194 fifo = fifo << 1; /* Prepare the next bit */
195 test = 0x1c | ((fifo >> 23) & 0x02);
196 outb(test, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300198 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300201static void cadet_setfreq(struct cadet *dev, unsigned freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300203 unsigned fifo;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300204 int i, j, test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300205 int curvol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300207 /*
208 * Formulate a fifo command
209 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300210 fifo = 0;
211 if (dev->curtuner == 0) { /* FM */
212 test = 102400;
213 freq = (freq * 1000) / 16; /* Make it kHz */
214 freq += 10700; /* IF is 10700 kHz */
215 for (i = 0; i < 14; i++) {
216 fifo = fifo << 1;
217 if (freq >= test) {
218 fifo |= 0x01;
219 freq -= test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300220 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300221 test = test >> 1;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300224 if (dev->curtuner == 1) { /* AM */
225 fifo = (freq / 16) + 2010; /* Make it kHz */
226 fifo |= 0x100000; /* Select AM Band */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300229 /*
230 * Save current volume/mute setting
231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300233 mutex_lock(&dev->lock);
234 outb(7, dev->io); /* Select tuner control */
235 curvol = inb(dev->io + 1);
236 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /*
239 * Tune the card
240 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300241 for (j = 3; j > -1; j--) {
242 cadet_settune(dev, fifo | (j << 16));
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300243
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300244 mutex_lock(&dev->lock);
245 outb(7, dev->io); /* Select tuner control */
246 outb(curvol, dev->io + 1);
247 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 msleep(100);
250
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300251 cadet_gettune(dev);
252 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
253 dev->sigstrength = sigtable[dev->curtuner][j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return;
255 }
256 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300257 dev->sigstrength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300261static int cadet_getvol(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int ret = 0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300264
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300265 mutex_lock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300266
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300267 outb(7, dev->io); /* Select tuner control */
268 if ((inb(dev->io + 1) & 0x20) != 0)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300269 ret = 0xffff;
270
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300271 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300272 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300276static void cadet_setvol(struct cadet *dev, int vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300278 mutex_lock(&dev->lock);
279 outb(7, dev->io); /* Select tuner control */
280 if (vol > 0)
281 outb(0x20, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300282 else
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300283 outb(0x00, dev->io + 1);
284 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300285}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300287static void cadet_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300289 struct cadet *dev = (void *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300291 /* Service the RDS fifo */
292 if (mutex_trylock(&dev->lock)) {
293 outb(0x3, dev->io); /* Select RDS Decoder Control */
294 if ((inb(dev->io + 1) & 0x20) != 0)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300295 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300296 outb(0x80, dev->io); /* Select RDS fifo */
297 while ((inb(dev->io) & 0x80) != 0) {
298 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
299 if (dev->rdsin == dev->rdsout)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300300 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 else
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300302 dev->rdsin++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300304 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
307 /*
308 * Service pending read
309 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300310 if (dev->rdsin != dev->rdsout)
311 wake_up_interruptible(&dev->read_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300313 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * Clean up and exit
315 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300316 init_timer(&dev->readtimer);
317 dev->readtimer.function = cadet_handler;
318 dev->readtimer.data = (unsigned long)0;
319 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
320 add_timer(&dev->readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300324static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300326 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned char readbuf[RDS_BUFFER];
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300328 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300330 if (dev->rdsstat == 0) {
331 mutex_lock(&dev->lock);
332 dev->rdsstat = 1;
333 outb(0x80, dev->io); /* Select RDS fifo */
334 mutex_unlock(&dev->lock);
335 init_timer(&dev->readtimer);
336 dev->readtimer.function = cadet_handler;
337 dev->readtimer.data = (unsigned long)dev;
338 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
339 add_timer(&dev->readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300341 if (dev->rdsin == dev->rdsout) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300342 if (file->f_flags & O_NONBLOCK)
343 return -EWOULDBLOCK;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300344 interruptible_sleep_on(&dev->read_queue);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300345 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300346 while (i < count && dev->rdsin != dev->rdsout)
347 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300349 if (copy_to_user(data, readbuf, i))
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300350 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return i;
352}
353
354
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300355static int vidioc_querycap(struct file *file, void *priv,
356 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300358 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
359 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
360 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300361 v->version = CADET_VERSION;
Hans Verkuil8e280f22009-06-20 06:19:09 -0300362 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
363 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300367static int vidioc_g_tuner(struct file *file, void *priv,
368 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300370 struct cadet *dev = video_drvdata(file);
371
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300372 v->type = V4L2_TUNER_RADIO;
373 switch (v->index) {
374 case 0:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300375 strlcpy(v->name, "FM", sizeof(v->name));
Hans Verkuil8e280f22009-06-20 06:19:09 -0300376 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300377 v->rangelow = 1400; /* 87.5 MHz */
378 v->rangehigh = 1728; /* 108.0 MHz */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300379 v->rxsubchans = cadet_getstereo(dev);
380 switch (v->rxsubchans) {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300381 case V4L2_TUNER_SUB_MONO:
382 v->audmode = V4L2_TUNER_MODE_MONO;
383 break;
384 case V4L2_TUNER_SUB_STEREO:
385 v->audmode = V4L2_TUNER_MODE_STEREO;
386 break;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300387 default:
388 break;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300389 }
Hans Verkuil8e280f22009-06-20 06:19:09 -0300390 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300391 break;
392 case 1:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300393 strlcpy(v->name, "AM", sizeof(v->name));
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300394 v->capability = V4L2_TUNER_CAP_LOW;
395 v->rangelow = 8320; /* 520 kHz */
396 v->rangehigh = 26400; /* 1650 kHz */
397 v->rxsubchans = V4L2_TUNER_SUB_MONO;
398 v->audmode = V4L2_TUNER_MODE_MONO;
399 break;
400 default:
401 return -EINVAL;
402 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300403 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300404 return 0;
405}
406
407static int vidioc_s_tuner(struct file *file, void *priv,
408 struct v4l2_tuner *v)
409{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300410 struct cadet *dev = video_drvdata(file);
411
412 if (v->index != 0 && v->index != 1)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300413 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300414 dev->curtuner = v->index;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300415 return 0;
416}
417
418static int vidioc_g_frequency(struct file *file, void *priv,
419 struct v4l2_frequency *f)
420{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300421 struct cadet *dev = video_drvdata(file);
422
423 f->tuner = dev->curtuner;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300424 f->type = V4L2_TUNER_RADIO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300425 f->frequency = cadet_getfreq(dev);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300426 return 0;
427}
428
429
430static int vidioc_s_frequency(struct file *file, void *priv,
431 struct v4l2_frequency *f)
432{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300433 struct cadet *dev = video_drvdata(file);
434
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300435 if (f->type != V4L2_TUNER_RADIO)
436 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300437 if (dev->curtuner == 0 && (f->frequency < 1400 || f->frequency > 1728))
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300438 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300439 if (dev->curtuner == 1 && (f->frequency < 8320 || f->frequency > 26400))
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300440 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300441 cadet_setfreq(dev, f->frequency);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300442 return 0;
443}
444
445static int vidioc_queryctrl(struct file *file, void *priv,
446 struct v4l2_queryctrl *qc)
447{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300448 switch (qc->id) {
449 case V4L2_CID_AUDIO_MUTE:
450 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
451 case V4L2_CID_AUDIO_VOLUME:
452 return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300453 }
454 return -EINVAL;
455}
456
457static int vidioc_g_ctrl(struct file *file, void *priv,
458 struct v4l2_control *ctrl)
459{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300460 struct cadet *dev = video_drvdata(file);
461
462 switch (ctrl->id) {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300463 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300464 ctrl->value = (cadet_getvol(dev) == 0);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300465 break;
466 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300467 ctrl->value = cadet_getvol(dev);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300468 break;
469 default:
470 return -EINVAL;
471 }
472 return 0;
473}
474
475static int vidioc_s_ctrl(struct file *file, void *priv,
476 struct v4l2_control *ctrl)
477{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300478 struct cadet *dev = video_drvdata(file);
479
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300480 switch (ctrl->id){
481 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
482 if (ctrl->value)
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300483 cadet_setvol(dev, 0);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300484 else
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300485 cadet_setvol(dev, 0xffff);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300486 break;
487 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300488 cadet_setvol(dev, ctrl->value);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300489 break;
490 default:
491 return -EINVAL;
492 }
493 return 0;
494}
495
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300496static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
497{
498 *i = 0;
499 return 0;
500}
501
502static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
503{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300504 return i ? -EINVAL : 0;
505}
506
507static int vidioc_g_audio(struct file *file, void *priv,
508 struct v4l2_audio *a)
509{
510 a->index = 0;
511 strlcpy(a->name, "Radio", sizeof(a->name));
512 a->capability = V4L2_AUDCAP_STEREO;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300513 return 0;
514}
515
516static int vidioc_s_audio(struct file *file, void *priv,
517 struct v4l2_audio *a)
518{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300519 return a->index ? -EINVAL : 0;
520}
521
522static int cadet_open(struct file *file)
523{
524 struct cadet *dev = video_drvdata(file);
525
526 dev->users++;
527 if (1 == dev->users)
528 init_waitqueue_head(&dev->read_queue);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300532static int cadet_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300534 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300536 dev->users--;
537 if (0 == dev->users) {
538 del_timer_sync(&dev->readtimer);
539 dev->rdsstat = 0;
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300540 }
541 return 0;
542}
543
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300544static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300545{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300546 struct cadet *dev = video_drvdata(file);
547
548 poll_wait(file, &dev->read_queue, wait);
549 if (dev->rdsin != dev->rdsout)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300550 return POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552}
553
554
Hans Verkuilbec43662008-12-30 06:58:20 -0300555static const struct v4l2_file_operations cadet_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 .owner = THIS_MODULE,
557 .open = cadet_open,
558 .release = cadet_release,
559 .read = cadet_read,
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300560 .ioctl = video_ioctl2,
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300561 .poll = cadet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562};
563
Hans Verkuila3998102008-07-21 02:57:38 -0300564static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300565 .vidioc_querycap = vidioc_querycap,
566 .vidioc_g_tuner = vidioc_g_tuner,
567 .vidioc_s_tuner = vidioc_s_tuner,
568 .vidioc_g_frequency = vidioc_g_frequency,
569 .vidioc_s_frequency = vidioc_s_frequency,
570 .vidioc_queryctrl = vidioc_queryctrl,
571 .vidioc_g_ctrl = vidioc_g_ctrl,
572 .vidioc_s_ctrl = vidioc_s_ctrl,
573 .vidioc_g_audio = vidioc_g_audio,
574 .vidioc_s_audio = vidioc_s_audio,
575 .vidioc_g_input = vidioc_g_input,
576 .vidioc_s_input = vidioc_s_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577};
578
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300579#ifdef CONFIG_PNP
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581static struct pnp_device_id cadet_pnp_devices[] = {
582 /* ADS Cadet AM/FM Radio Card */
583 {.id = "MSM0c24", .driver_data = 0},
584 {.id = ""}
585};
586
587MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
588
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300589static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 if (!dev)
592 return -ENODEV;
593 /* only support one device */
594 if (io > 0)
595 return -EBUSY;
596
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300597 if (!pnp_port_valid(dev, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 io = pnp_port_start(dev, 0);
601
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300602 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 return io;
605}
606
607static struct pnp_driver cadet_pnp_driver = {
608 .name = "radio-cadet",
609 .id_table = cadet_pnp_devices,
610 .probe = cadet_pnp_probe,
611 .remove = NULL,
612};
613
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300614#else
615static struct pnp_driver cadet_pnp_driver;
616#endif
617
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300618static void cadet_probe(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300620 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int i;
622
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300623 for (i = 0; i < 8; i++) {
624 dev->io = iovals[i];
625 if (request_region(dev->io, 2, "cadet-probe")) {
626 cadet_setfreq(dev, 1410);
627 if (cadet_getfreq(dev) == 1410) {
628 release_region(dev->io, 2);
629 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300631 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300634 dev->io = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300637/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * io should only be set if the user has used something like
639 * isapnp (the userspace program) to initialize this card for us
640 */
641
642static int __init cadet_init(void)
643{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300644 struct cadet *dev = &cadet_card;
645 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
646 int res;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300647
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300648 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
649 mutex_init(&dev->lock);
650
651 /* If a probe was requested then probe ISAPnP first (safest) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (io < 0)
653 pnp_register_driver(&cadet_pnp_driver);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300654 dev->io = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300656 /* If that fails then probe unsafely if probe is requested */
657 if (dev->io < 0)
658 cadet_probe(dev);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300659
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300660 /* Else we bail out */
661 if (dev->io < 0) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300662#ifdef MODULE
Hans Verkuilb24c20cc2009-03-09 08:11:21 -0300663 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
664 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#endif
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300666 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300668 if (!request_region(dev->io, 2, "cadet"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto fail;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300670
671 res = v4l2_device_register(NULL, v4l2_dev);
672 if (res < 0) {
673 release_region(dev->io, 2);
674 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 goto fail;
676 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300677
678 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
679 dev->vdev.v4l2_dev = v4l2_dev;
680 dev->vdev.fops = &cadet_fops;
681 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
682 dev->vdev.release = video_device_release_empty;
683 video_set_drvdata(&dev->vdev, dev);
684
685 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
686 v4l2_device_unregister(v4l2_dev);
687 release_region(dev->io, 2);
688 goto fail;
689 }
690 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692fail:
693 pnp_unregister_driver(&cadet_pnp_driver);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300694 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300697static void __exit cadet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300699 struct cadet *dev = &cadet_card;
700
701 video_unregister_device(&dev->vdev);
702 v4l2_device_unregister(&dev->v4l2_dev);
703 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 pnp_unregister_driver(&cadet_pnp_driver);
705}
706
707module_init(cadet_init);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300708module_exit(cadet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709