blob: 82affaedf06778ad73d0d40fa1a87b0e621630cd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
2 *
3 * by Fred Gleason <fredg@wava.com>
4 * Version 0.3.3
5 *
6 * (Loosely) based on code for the Aztech radio card by
7 *
8 * Russell Kroll (rkroll@exploits.org)
9 * Quay Ly
10 * Donald Song
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030011 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
13 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
14 *
15 * History:
16 * 2000-04-29 Russell Kroll <rkroll@exploits.org>
17 * Added ISAPnP detection for Linux 2.3/2.4
18 *
19 * 2001-01-10 Russell Kroll <rkroll@exploits.org>
20 * Removed dead CONFIG_RADIO_CADET_PORT code
21 * PnP detection on load is now default (no args necessary)
22 *
23 * 2002-01-17 Adam Belay <ambx1@neo.rr.com>
24 * Updated to latest pnp code
25 *
Alan Coxd9b01442008-10-27 15:13:47 -030026 * 2003-01-31 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * Cleaned up locking, delay code, general odds and ends
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030028 *
29 * 2006-07-30 Hans J. Koch <koch@hjk-az.de>
30 * Changed API to V4L2
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
33#include <linux/module.h> /* Modules */
34#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070035#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h> /* udelay */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030037#include <linux/videodev2.h> /* V4L2 API defs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/param.h>
39#include <linux/pnp.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040040#include <linux/sched.h>
Hans Verkuilbec2aec2009-03-06 13:48:47 -030041#include <linux/io.h> /* outb, outb_p */
Hans Verkuilbec2aec2009-03-06 13:48:47 -030042#include <media/v4l2-device.h>
43#include <media/v4l2-ioctl.h>
Hans Verkuilb54c97db2012-07-02 09:36:39 -030044#include <media/v4l2-ctrls.h>
45#include <media/v4l2-fh.h>
46#include <media/v4l2-event.h>
Hans Verkuilbec2aec2009-03-06 13:48:47 -030047
48MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
49MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
50MODULE_LICENSE("GPL");
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030051MODULE_VERSION("0.3.4");
Hans Verkuilbec2aec2009-03-06 13:48:47 -030052
53static int io = -1; /* default to isapnp activation */
54static int radio_nr = -1;
55
56module_param(io, int, 0);
57MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
58module_param(radio_nr, int, 0);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define RDS_BUFFER 256
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030061#define RDS_RX_FLAG 1
62#define MBS_RX_FLAG 2
63
Hans Verkuilbec2aec2009-03-06 13:48:47 -030064struct cadet {
65 struct v4l2_device v4l2_dev;
66 struct video_device vdev;
Hans Verkuilb54c97db2012-07-02 09:36:39 -030067 struct v4l2_ctrl_handler ctrl_handler;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030068 int io;
Hans Verkuil6652c712012-07-10 08:26:04 -030069 bool is_fm_band;
70 u32 curfreq;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030071 int tunestat;
72 int sigstrength;
73 wait_queue_head_t read_queue;
74 struct timer_list readtimer;
Hans Verkuilcc0d3262012-07-02 09:46:46 -030075 u8 rdsin, rdsout, rdsstat;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030076 unsigned char rdsbuf[RDS_BUFFER];
77 struct mutex lock;
78 int reading;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -030079};
80
Hans Verkuilbec2aec2009-03-06 13:48:47 -030081static struct cadet cadet_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * Signal Strength Threshold Values
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030085 * The V4L API spec does not define any particular unit for the signal
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * strength value. These values are in microvolts of RF at the tuner's input.
87 */
Hans Verkuil6652c712012-07-10 08:26:04 -030088static u16 sigtable[2][4] = {
89 { 1835, 2621, 4128, 65535 },
Hans Verkuilcc0d3262012-07-02 09:46:46 -030090 { 2185, 4369, 13107, 65535 },
Hans Verkuilbec2aec2009-03-06 13:48:47 -030091};
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Hans Verkuilb530a442013-03-19 04:09:26 -030093static const struct v4l2_frequency_band bands[] = {
94 {
95 .index = 0,
96 .type = V4L2_TUNER_RADIO,
97 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
98 .rangelow = 8320, /* 520 kHz */
99 .rangehigh = 26400, /* 1650 kHz */
100 .modulation = V4L2_BAND_MODULATION_AM,
101 }, {
102 .index = 1,
103 .type = V4L2_TUNER_RADIO,
104 .capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
105 V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW |
106 V4L2_TUNER_CAP_FREQ_BANDS,
107 .rangelow = 1400000, /* 87.5 MHz */
108 .rangehigh = 1728000, /* 108.0 MHz */
109 .modulation = V4L2_BAND_MODULATION_FM,
110 },
111};
112
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300113
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300114static int cadet_getstereo(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300116 int ret = V4L2_TUNER_SUB_MONO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300117
Hans Verkuil6652c712012-07-10 08:26:04 -0300118 if (!dev->is_fm_band) /* Only FM has stereo capability! */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300119 return V4L2_TUNER_SUB_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300121 outb(7, dev->io); /* Select tuner control */
122 if ((inb(dev->io + 1) & 0x40) == 0)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300123 ret = V4L2_TUNER_SUB_STEREO;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300124 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300127static unsigned cadet_gettune(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300129 int curvol, i;
130 unsigned fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300132 /*
133 * Prepare for read
134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300136 outb(7, dev->io); /* Select tuner control */
137 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
138 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
139 dev->tunestat = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300141 /*
142 * Read the shift register
143 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300144 for (i = 0; i < 25; i++) {
145 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
146 if (i < 24) {
147 outb(0x01, dev->io + 1);
148 dev->tunestat &= inb(dev->io + 1);
149 outb(0x00, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300150 }
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300153 /*
154 * Restore volume/mute setting
155 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300156 outb(curvol, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return fifo;
158}
159
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300160static unsigned cadet_getfreq(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300162 int i;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300163 unsigned freq = 0, test, fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 /*
166 * Read current tuning
167 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300168 fifo = cadet_gettune(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300170 /*
171 * Convert to actual frequency
172 */
Hans Verkuil6652c712012-07-10 08:26:04 -0300173 if (!dev->is_fm_band) /* AM */
174 return ((fifo & 0x7fff) - 450) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Hans Verkuil6652c712012-07-10 08:26:04 -0300176 test = 12500;
177 for (i = 0; i < 14; i++) {
178 if ((fifo & 0x01) != 0)
179 freq += test;
180 test = test << 1;
181 fifo = fifo >> 1;
182 }
183 freq -= 10700000; /* IF frequency is 10.7 MHz */
184 freq = (freq * 16) / 1000; /* Make it 1/16 kHz */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300185 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300188static void cadet_settune(struct cadet *dev, unsigned fifo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300190 int i;
191 unsigned test;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300193 outb(7, dev->io); /* Select tuner control */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /*
195 * Write the shift register
196 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300197 test = 0;
198 test = (fifo >> 23) & 0x02; /* Align data for SDO */
199 test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
200 outb(7, dev->io); /* Select tuner control */
201 outb(test, dev->io + 1); /* Initialize for write */
202 for (i = 0; i < 25; i++) {
203 test |= 0x01; /* Toggle SCK High */
204 outb(test, dev->io + 1);
205 test &= 0xfe; /* Toggle SCK Low */
206 outb(test, dev->io + 1);
207 fifo = fifo << 1; /* Prepare the next bit */
208 test = 0x1c | ((fifo >> 23) & 0x02);
209 outb(test, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300213static void cadet_setfreq(struct cadet *dev, unsigned freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300215 unsigned fifo;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300216 int i, j, test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300217 int curvol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Hans Verkuilb530a442013-03-19 04:09:26 -0300219 freq = clamp(freq, bands[dev->is_fm_band].rangelow,
220 bands[dev->is_fm_band].rangehigh);
Hans Verkuil6652c712012-07-10 08:26:04 -0300221 dev->curfreq = freq;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300222 /*
223 * Formulate a fifo command
224 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300225 fifo = 0;
Hans Verkuil6652c712012-07-10 08:26:04 -0300226 if (dev->is_fm_band) { /* FM */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300227 test = 102400;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300228 freq = freq / 16; /* Make it kHz */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300229 freq += 10700; /* IF is 10700 kHz */
230 for (i = 0; i < 14; i++) {
231 fifo = fifo << 1;
232 if (freq >= test) {
233 fifo |= 0x01;
234 freq -= test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300235 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300236 test = test >> 1;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300237 }
Hans Verkuil6652c712012-07-10 08:26:04 -0300238 } else { /* AM */
239 fifo = (freq / 16) + 450; /* Make it kHz */
240 fifo |= 0x100000; /* Select AM Band */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300243 /*
244 * Save current volume/mute setting
245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300247 outb(7, dev->io); /* Select tuner control */
248 curvol = inb(dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /*
251 * Tune the card
252 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300253 for (j = 3; j > -1; j--) {
254 cadet_settune(dev, fifo | (j << 16));
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300255
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300256 outb(7, dev->io); /* Select tuner control */
257 outb(curvol, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 msleep(100);
260
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300261 cadet_gettune(dev);
262 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
Hans Verkuil6652c712012-07-10 08:26:04 -0300263 dev->sigstrength = sigtable[dev->is_fm_band][j];
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300264 goto reset_rds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300267 dev->sigstrength = 0;
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300268reset_rds:
269 outb(3, dev->io);
270 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Hans Verkuilce0ede22014-02-10 07:21:36 -0300273static bool cadet_has_rds_data(struct cadet *dev)
274{
275 bool result;
276
277 mutex_lock(&dev->lock);
278 result = dev->rdsin != dev->rdsout;
279 mutex_unlock(&dev->lock);
280 return result;
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300284static void cadet_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300286 struct cadet *dev = (void *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300288 /* Service the RDS fifo */
289 if (mutex_trylock(&dev->lock)) {
290 outb(0x3, dev->io); /* Select RDS Decoder Control */
291 if ((inb(dev->io + 1) & 0x20) != 0)
Hans Verkuilce0ede22014-02-10 07:21:36 -0300292 pr_err("cadet: RDS fifo overflow\n");
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300293 outb(0x80, dev->io); /* Select RDS fifo */
Hans Verkuilce0ede22014-02-10 07:21:36 -0300294
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300295 while ((inb(dev->io) & 0x80) != 0) {
296 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
Hans Verkuilce0ede22014-02-10 07:21:36 -0300297 if (dev->rdsin + 1 != dev->rdsout)
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300298 dev->rdsin++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300300 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 /*
304 * Service pending read
305 */
Hans Verkuilce0ede22014-02-10 07:21:36 -0300306 if (cadet_has_rds_data(dev))
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300307 wake_up_interruptible(&dev->read_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300309 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * Clean up and exit
311 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300312 init_timer(&dev->readtimer);
313 dev->readtimer.function = cadet_handler;
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300314 dev->readtimer.data = data;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300315 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
316 add_timer(&dev->readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300319static void cadet_start_rds(struct cadet *dev)
320{
321 dev->rdsstat = 1;
322 outb(0x80, dev->io); /* Select RDS fifo */
323 init_timer(&dev->readtimer);
324 dev->readtimer.function = cadet_handler;
325 dev->readtimer.data = (unsigned long)dev;
326 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
327 add_timer(&dev->readtimer);
328}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300330static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300332 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unsigned char readbuf[RDS_BUFFER];
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300334 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Hans Verkuil1cccee02010-11-14 09:43:52 -0300336 mutex_lock(&dev->lock);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300337 if (dev->rdsstat == 0)
338 cadet_start_rds(dev);
Hans Verkuilce0ede22014-02-10 07:21:36 -0300339 mutex_unlock(&dev->lock);
340
341 if (!cadet_has_rds_data(dev) && (file->f_flags & O_NONBLOCK))
342 return -EWOULDBLOCK;
343 i = wait_event_interruptible(dev->read_queue, cadet_has_rds_data(dev));
344 if (i)
345 return i;
346
347 mutex_lock(&dev->lock);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300348 while (i < count && dev->rdsin != dev->rdsout)
349 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
Hans Verkuilce0ede22014-02-10 07:21:36 -0300350 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300352 if (i && copy_to_user(data, readbuf, i))
Hans Verkuilce0ede22014-02-10 07:21:36 -0300353 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return i;
355}
356
357
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300358static int vidioc_querycap(struct file *file, void *priv,
359 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300361 strlcpy(v->driver, "ADS Cadet", sizeof(v->driver));
362 strlcpy(v->card, "ADS Cadet", sizeof(v->card));
Hans Verkuilce0ede22014-02-10 07:21:36 -0300363 strlcpy(v->bus_info, "ISA:radio-cadet", sizeof(v->bus_info));
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300364 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
Hans Verkuil8e280f22009-06-20 06:19:09 -0300365 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300366 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300370static int vidioc_g_tuner(struct file *file, void *priv,
371 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300373 struct cadet *dev = video_drvdata(file);
374
Hans Verkuil6652c712012-07-10 08:26:04 -0300375 if (v->index)
376 return -EINVAL;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300377 v->type = V4L2_TUNER_RADIO;
Hans Verkuil6652c712012-07-10 08:26:04 -0300378 strlcpy(v->name, "Radio", sizeof(v->name));
379 v->capability = bands[0].capability | bands[1].capability;
380 v->rangelow = bands[0].rangelow; /* 520 kHz (start of AM band) */
381 v->rangehigh = bands[1].rangehigh; /* 108.0 MHz (end of FM band) */
382 if (dev->is_fm_band) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300383 v->rxsubchans = cadet_getstereo(dev);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300384 outb(3, dev->io);
385 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
386 mdelay(100);
387 outb(3, dev->io);
388 if (inb(dev->io + 1) & 0x80)
389 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
Hans Verkuil6652c712012-07-10 08:26:04 -0300390 } else {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300391 v->rangelow = 8320; /* 520 kHz */
392 v->rangehigh = 26400; /* 1650 kHz */
393 v->rxsubchans = V4L2_TUNER_SUB_MONO;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300394 }
Hans Verkuil6652c712012-07-10 08:26:04 -0300395 v->audmode = V4L2_TUNER_MODE_STEREO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300396 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300397 return 0;
398}
399
400static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300401 const struct v4l2_tuner *v)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300402{
Hans Verkuil6652c712012-07-10 08:26:04 -0300403 return v->index ? -EINVAL : 0;
404}
405
406static int vidioc_enum_freq_bands(struct file *file, void *priv,
407 struct v4l2_frequency_band *band)
408{
409 if (band->tuner)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300410 return -EINVAL;
Hans Verkuil6652c712012-07-10 08:26:04 -0300411 if (band->index >= ARRAY_SIZE(bands))
412 return -EINVAL;
413 *band = bands[band->index];
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300414 return 0;
415}
416
417static int vidioc_g_frequency(struct file *file, void *priv,
418 struct v4l2_frequency *f)
419{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300420 struct cadet *dev = video_drvdata(file);
421
Hans Verkuil6652c712012-07-10 08:26:04 -0300422 if (f->tuner)
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300423 return -EINVAL;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300424 f->type = V4L2_TUNER_RADIO;
Hans Verkuil6652c712012-07-10 08:26:04 -0300425 f->frequency = dev->curfreq;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300426 return 0;
427}
428
429
430static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -0300431 const struct v4l2_frequency *f)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300432{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300433 struct cadet *dev = video_drvdata(file);
434
Hans Verkuil6652c712012-07-10 08:26:04 -0300435 if (f->tuner)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300436 return -EINVAL;
Hans Verkuil6652c712012-07-10 08:26:04 -0300437 dev->is_fm_band =
438 f->frequency >= (bands[0].rangehigh + bands[1].rangelow) / 2;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300439 cadet_setfreq(dev, f->frequency);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300440 return 0;
441}
442
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300443static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300444{
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300445 struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300446
447 switch (ctrl->id) {
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300448 case V4L2_CID_AUDIO_MUTE:
449 outb(7, dev->io); /* Select tuner control */
450 if (ctrl->val)
451 outb(0x00, dev->io + 1);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300452 else
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300453 outb(0x20, dev->io + 1);
454 return 0;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300455 }
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300456 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300457}
458
459static int cadet_open(struct file *file)
460{
461 struct cadet *dev = video_drvdata(file);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300462 int err;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300463
Hans Verkuil1cccee02010-11-14 09:43:52 -0300464 mutex_lock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300465 err = v4l2_fh_open(file);
466 if (err)
467 goto fail;
468 if (v4l2_fh_is_singular_file(file))
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300469 init_waitqueue_head(&dev->read_queue);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300470fail:
Hans Verkuil1cccee02010-11-14 09:43:52 -0300471 mutex_unlock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300475static int cadet_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300477 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Hans Verkuil1cccee02010-11-14 09:43:52 -0300479 mutex_lock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300480 if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300481 del_timer_sync(&dev->readtimer);
482 dev->rdsstat = 0;
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300483 }
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300484 v4l2_fh_release(file);
Hans Verkuil1cccee02010-11-14 09:43:52 -0300485 mutex_unlock(&dev->lock);
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300486 return 0;
487}
488
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300489static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300490{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300491 struct cadet *dev = video_drvdata(file);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300492 unsigned long req_events = poll_requested_events(wait);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300493 unsigned int res = v4l2_ctrl_poll(file, wait);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300494
495 poll_wait(file, &dev->read_queue, wait);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300496 if (dev->rdsstat == 0 && (req_events & (POLLIN | POLLRDNORM))) {
497 mutex_lock(&dev->lock);
498 if (dev->rdsstat == 0)
499 cadet_start_rds(dev);
500 mutex_unlock(&dev->lock);
501 }
Hans Verkuilce0ede22014-02-10 07:21:36 -0300502 if (cadet_has_rds_data(dev))
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300503 res |= POLLIN | POLLRDNORM;
504 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507
Hans Verkuilbec43662008-12-30 06:58:20 -0300508static const struct v4l2_file_operations cadet_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 .owner = THIS_MODULE,
510 .open = cadet_open,
511 .release = cadet_release,
512 .read = cadet_read,
Hans Verkuil1cccee02010-11-14 09:43:52 -0300513 .unlocked_ioctl = video_ioctl2,
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300514 .poll = cadet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515};
516
Hans Verkuila3998102008-07-21 02:57:38 -0300517static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300518 .vidioc_querycap = vidioc_querycap,
519 .vidioc_g_tuner = vidioc_g_tuner,
520 .vidioc_s_tuner = vidioc_s_tuner,
521 .vidioc_g_frequency = vidioc_g_frequency,
522 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil6652c712012-07-10 08:26:04 -0300523 .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300524 .vidioc_log_status = v4l2_ctrl_log_status,
525 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
526 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
527};
528
529static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
530 .s_ctrl = cadet_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531};
532
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300533#ifdef CONFIG_PNP
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535static struct pnp_device_id cadet_pnp_devices[] = {
536 /* ADS Cadet AM/FM Radio Card */
537 {.id = "MSM0c24", .driver_data = 0},
538 {.id = ""}
539};
540
541MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
542
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300543static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 if (!dev)
546 return -ENODEV;
547 /* only support one device */
548 if (io > 0)
549 return -EBUSY;
550
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300551 if (!pnp_port_valid(dev, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 io = pnp_port_start(dev, 0);
555
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300556 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 return io;
559}
560
561static struct pnp_driver cadet_pnp_driver = {
562 .name = "radio-cadet",
563 .id_table = cadet_pnp_devices,
564 .probe = cadet_pnp_probe,
565 .remove = NULL,
566};
567
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300568#else
569static struct pnp_driver cadet_pnp_driver;
570#endif
571
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300572static void cadet_probe(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300574 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int i;
576
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300577 for (i = 0; i < 8; i++) {
578 dev->io = iovals[i];
579 if (request_region(dev->io, 2, "cadet-probe")) {
Hans Verkuil6652c712012-07-10 08:26:04 -0300580 cadet_setfreq(dev, bands[1].rangelow);
581 if (cadet_getfreq(dev) == bands[1].rangelow) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300582 release_region(dev->io, 2);
583 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300585 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300588 dev->io = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300591/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * io should only be set if the user has used something like
593 * isapnp (the userspace program) to initialize this card for us
594 */
595
596static int __init cadet_init(void)
597{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300598 struct cadet *dev = &cadet_card;
599 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300600 struct v4l2_ctrl_handler *hdl;
601 int res = -ENODEV;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300602
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300603 strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
604 mutex_init(&dev->lock);
605
606 /* If a probe was requested then probe ISAPnP first (safest) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (io < 0)
608 pnp_register_driver(&cadet_pnp_driver);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300609 dev->io = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300611 /* If that fails then probe unsafely if probe is requested */
612 if (dev->io < 0)
613 cadet_probe(dev);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300614
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300615 /* Else we bail out */
616 if (dev->io < 0) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300617#ifdef MODULE
Hans Verkuilb24c20cc2009-03-09 08:11:21 -0300618 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
619 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#endif
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300621 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300623 if (!request_region(dev->io, 2, "cadet"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto fail;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300625
626 res = v4l2_device_register(NULL, v4l2_dev);
627 if (res < 0) {
628 release_region(dev->io, 2);
629 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto fail;
631 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300632
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300633 hdl = &dev->ctrl_handler;
634 v4l2_ctrl_handler_init(hdl, 2);
635 v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
636 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
637 v4l2_dev->ctrl_handler = hdl;
638 if (hdl->error) {
639 res = hdl->error;
640 v4l2_err(v4l2_dev, "Could not register controls\n");
641 goto err_hdl;
642 }
643
Hans Verkuil6652c712012-07-10 08:26:04 -0300644 dev->is_fm_band = true;
645 dev->curfreq = bands[dev->is_fm_band].rangelow;
646 cadet_setfreq(dev, dev->curfreq);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300647 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
648 dev->vdev.v4l2_dev = v4l2_dev;
649 dev->vdev.fops = &cadet_fops;
650 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
651 dev->vdev.release = video_device_release_empty;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300652 dev->vdev.lock = &dev->lock;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300653 video_set_drvdata(&dev->vdev, dev);
654
Peter Senna Tschudin8ca70802012-09-06 11:23:52 -0300655 res = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
656 if (res < 0)
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300657 goto err_hdl;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300658 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 0;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300660err_hdl:
661 v4l2_ctrl_handler_free(hdl);
662 v4l2_device_unregister(v4l2_dev);
663 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664fail:
665 pnp_unregister_driver(&cadet_pnp_driver);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300666 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300669static void __exit cadet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300671 struct cadet *dev = &cadet_card;
672
673 video_unregister_device(&dev->vdev);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300674 v4l2_ctrl_handler_free(&dev->ctrl_handler);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300675 v4l2_device_unregister(&dev->v4l2_dev);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300676 outb(7, dev->io); /* Mute */
677 outb(0x00, dev->io + 1);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300678 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 pnp_unregister_driver(&cadet_pnp_driver);
680}
681
682module_init(cadet_init);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300683module_exit(cadet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684