blob: a5db9b4dc3de914a48636da942e8249f46ef1d8e [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
3 *
4 * by Fred Gleason <fredg@wava.com>
5 * Version 0.3.3
6 *
7 * (Loosely) based on code for the Aztech radio card by
8 *
9 * Russell Kroll (rkroll@exploits.org)
10 * Quay Ly
11 * Donald Song
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030012 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
14 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
15 *
16 * History:
17 * 2000-04-29 Russell Kroll <rkroll@exploits.org>
18 * Added ISAPnP detection for Linux 2.3/2.4
19 *
20 * 2001-01-10 Russell Kroll <rkroll@exploits.org>
21 * Removed dead CONFIG_RADIO_CADET_PORT code
22 * PnP detection on load is now default (no args necessary)
23 *
24 * 2002-01-17 Adam Belay <ambx1@neo.rr.com>
25 * Updated to latest pnp code
26 *
Alan Coxd9b01442008-10-27 15:13:47 -030027 * 2003-01-31 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Cleaned up locking, delay code, general odds and ends
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030029 *
30 * 2006-07-30 Hans J. Koch <koch@hjk-az.de>
31 * Changed API to V4L2
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -050034#include <linux/module.h> /* Modules */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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>
Hans Verkuilb54c97db2012-07-02 09:36:39 -030045#include <media/v4l2-ctrls.h>
46#include <media/v4l2-fh.h>
47#include <media/v4l2-event.h>
Hans Verkuilbec2aec2009-03-06 13:48:47 -030048
49MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
50MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
51MODULE_LICENSE("GPL");
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030052MODULE_VERSION("0.3.4");
Hans Verkuilbec2aec2009-03-06 13:48:47 -030053
54static int io = -1; /* default to isapnp activation */
55static int radio_nr = -1;
56
57module_param(io, int, 0);
58MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
59module_param(radio_nr, int, 0);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define RDS_BUFFER 256
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030062#define RDS_RX_FLAG 1
63#define MBS_RX_FLAG 2
64
Hans Verkuilbec2aec2009-03-06 13:48:47 -030065struct cadet {
66 struct v4l2_device v4l2_dev;
67 struct video_device vdev;
Hans Verkuilb54c97db2012-07-02 09:36:39 -030068 struct v4l2_ctrl_handler ctrl_handler;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030069 int io;
Hans Verkuil6652c712012-07-10 08:26:04 -030070 bool is_fm_band;
71 u32 curfreq;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030072 int tunestat;
73 int sigstrength;
74 wait_queue_head_t read_queue;
75 struct timer_list readtimer;
Hans Verkuilcc0d3262012-07-02 09:46:46 -030076 u8 rdsin, rdsout, rdsstat;
Hans Verkuilbec2aec2009-03-06 13:48:47 -030077 unsigned char rdsbuf[RDS_BUFFER];
78 struct mutex lock;
79 int reading;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -030080};
81
Hans Verkuilbec2aec2009-03-06 13:48:47 -030082static struct cadet cadet_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 * Signal Strength Threshold Values
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030086 * The V4L API spec does not define any particular unit for the signal
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * strength value. These values are in microvolts of RF at the tuner's input.
88 */
Hans Verkuil6652c712012-07-10 08:26:04 -030089static u16 sigtable[2][4] = {
90 { 1835, 2621, 4128, 65535 },
Hans Verkuilcc0d3262012-07-02 09:46:46 -030091 { 2185, 4369, 13107, 65535 },
Hans Verkuilbec2aec2009-03-06 13:48:47 -030092};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Hans Verkuilb530a442013-03-19 04:09:26 -030094static const struct v4l2_frequency_band bands[] = {
95 {
96 .index = 0,
97 .type = V4L2_TUNER_RADIO,
98 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
99 .rangelow = 8320, /* 520 kHz */
100 .rangehigh = 26400, /* 1650 kHz */
101 .modulation = V4L2_BAND_MODULATION_AM,
102 }, {
103 .index = 1,
104 .type = V4L2_TUNER_RADIO,
105 .capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
106 V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW |
107 V4L2_TUNER_CAP_FREQ_BANDS,
108 .rangelow = 1400000, /* 87.5 MHz */
109 .rangehigh = 1728000, /* 108.0 MHz */
110 .modulation = V4L2_BAND_MODULATION_FM,
111 },
112};
113
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300114
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300115static int cadet_getstereo(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300117 int ret = V4L2_TUNER_SUB_MONO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300118
Hans Verkuil6652c712012-07-10 08:26:04 -0300119 if (!dev->is_fm_band) /* Only FM has stereo capability! */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300120 return V4L2_TUNER_SUB_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300122 outb(7, dev->io); /* Select tuner control */
123 if ((inb(dev->io + 1) & 0x40) == 0)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300124 ret = V4L2_TUNER_SUB_STEREO;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300128static unsigned cadet_gettune(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300130 int curvol, i;
131 unsigned fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300133 /*
134 * Prepare for read
135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300137 outb(7, dev->io); /* Select tuner control */
138 curvol = inb(dev->io + 1); /* Save current volume/mute setting */
139 outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
140 dev->tunestat = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300142 /*
143 * Read the shift register
144 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300145 for (i = 0; i < 25; i++) {
146 fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
147 if (i < 24) {
148 outb(0x01, dev->io + 1);
149 dev->tunestat &= inb(dev->io + 1);
150 outb(0x00, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300151 }
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300154 /*
155 * Restore volume/mute setting
156 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300157 outb(curvol, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return fifo;
159}
160
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300161static unsigned cadet_getfreq(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300163 int i;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300164 unsigned freq = 0, test, fifo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /*
167 * Read current tuning
168 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300169 fifo = cadet_gettune(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300171 /*
172 * Convert to actual frequency
173 */
Hans Verkuil6652c712012-07-10 08:26:04 -0300174 if (!dev->is_fm_band) /* AM */
175 return ((fifo & 0x7fff) - 450) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Hans Verkuil6652c712012-07-10 08:26:04 -0300177 test = 12500;
178 for (i = 0; i < 14; i++) {
179 if ((fifo & 0x01) != 0)
180 freq += test;
181 test = test << 1;
182 fifo = fifo >> 1;
183 }
184 freq -= 10700000; /* IF frequency is 10.7 MHz */
185 freq = (freq * 16) / 1000; /* Make it 1/16 kHz */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300186 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300189static void cadet_settune(struct cadet *dev, unsigned fifo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300191 int i;
192 unsigned test;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300194 outb(7, dev->io); /* Select tuner control */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /*
196 * Write the shift register
197 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300198 test = 0;
199 test = (fifo >> 23) & 0x02; /* Align data for SDO */
200 test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
201 outb(7, dev->io); /* Select tuner control */
202 outb(test, dev->io + 1); /* Initialize for write */
203 for (i = 0; i < 25; i++) {
204 test |= 0x01; /* Toggle SCK High */
205 outb(test, dev->io + 1);
206 test &= 0xfe; /* Toggle SCK Low */
207 outb(test, dev->io + 1);
208 fifo = fifo << 1; /* Prepare the next bit */
209 test = 0x1c | ((fifo >> 23) & 0x02);
210 outb(test, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300214static void cadet_setfreq(struct cadet *dev, unsigned freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300216 unsigned fifo;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300217 int i, j, test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300218 int curvol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Hans Verkuilb530a442013-03-19 04:09:26 -0300220 freq = clamp(freq, bands[dev->is_fm_band].rangelow,
221 bands[dev->is_fm_band].rangehigh);
Hans Verkuil6652c712012-07-10 08:26:04 -0300222 dev->curfreq = freq;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300223 /*
224 * Formulate a fifo command
225 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300226 fifo = 0;
Hans Verkuil6652c712012-07-10 08:26:04 -0300227 if (dev->is_fm_band) { /* FM */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300228 test = 102400;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300229 freq = freq / 16; /* Make it kHz */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300230 freq += 10700; /* IF is 10700 kHz */
231 for (i = 0; i < 14; i++) {
232 fifo = fifo << 1;
233 if (freq >= test) {
234 fifo |= 0x01;
235 freq -= test;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300236 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300237 test = test >> 1;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300238 }
Hans Verkuil6652c712012-07-10 08:26:04 -0300239 } else { /* AM */
240 fifo = (freq / 16) + 450; /* Make it kHz */
241 fifo |= 0x100000; /* Select AM Band */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300244 /*
245 * Save current volume/mute setting
246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300248 outb(7, dev->io); /* Select tuner control */
249 curvol = inb(dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /*
252 * Tune the card
253 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300254 for (j = 3; j > -1; j--) {
255 cadet_settune(dev, fifo | (j << 16));
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300256
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300257 outb(7, dev->io); /* Select tuner control */
258 outb(curvol, dev->io + 1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 msleep(100);
261
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300262 cadet_gettune(dev);
263 if ((dev->tunestat & 0x40) == 0) { /* Tuned */
Hans Verkuil6652c712012-07-10 08:26:04 -0300264 dev->sigstrength = sigtable[dev->is_fm_band][j];
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300265 goto reset_rds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300268 dev->sigstrength = 0;
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300269reset_rds:
270 outb(3, dev->io);
271 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Hans Verkuilce0ede22014-02-10 07:21:36 -0300274static bool cadet_has_rds_data(struct cadet *dev)
275{
276 bool result;
277
278 mutex_lock(&dev->lock);
279 result = dev->rdsin != dev->rdsout;
280 mutex_unlock(&dev->lock);
281 return result;
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Kees Cook297fced2017-10-24 11:23:03 -0400285static void cadet_handler(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Kees Cook297fced2017-10-24 11:23:03 -0400287 struct cadet *dev = from_timer(dev, t, readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300289 /* Service the RDS fifo */
290 if (mutex_trylock(&dev->lock)) {
291 outb(0x3, dev->io); /* Select RDS Decoder Control */
292 if ((inb(dev->io + 1) & 0x20) != 0)
Hans Verkuilce0ede22014-02-10 07:21:36 -0300293 pr_err("cadet: RDS fifo overflow\n");
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300294 outb(0x80, dev->io); /* Select RDS fifo */
Hans Verkuilce0ede22014-02-10 07:21:36 -0300295
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300296 while ((inb(dev->io) & 0x80) != 0) {
297 dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
Hans Verkuilce0ede22014-02-10 07:21:36 -0300298 if (dev->rdsin + 1 != dev->rdsout)
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300299 dev->rdsin++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300301 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 /*
305 * Service pending read
306 */
Hans Verkuilce0ede22014-02-10 07:21:36 -0300307 if (cadet_has_rds_data(dev))
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300308 wake_up_interruptible(&dev->read_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300310 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * Clean up and exit
312 */
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300313 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
314 add_timer(&dev->readtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300317static void cadet_start_rds(struct cadet *dev)
318{
319 dev->rdsstat = 1;
320 outb(0x80, dev->io); /* Select RDS fifo */
Kees Cook297fced2017-10-24 11:23:03 -0400321 timer_setup(&dev->readtimer, cadet_handler, 0);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300322 dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
323 add_timer(&dev->readtimer);
324}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300326static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300328 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned char readbuf[RDS_BUFFER];
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300330 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Hans Verkuil1cccee02010-11-14 09:43:52 -0300332 mutex_lock(&dev->lock);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300333 if (dev->rdsstat == 0)
334 cadet_start_rds(dev);
Hans Verkuilce0ede22014-02-10 07:21:36 -0300335 mutex_unlock(&dev->lock);
336
337 if (!cadet_has_rds_data(dev) && (file->f_flags & O_NONBLOCK))
338 return -EWOULDBLOCK;
339 i = wait_event_interruptible(dev->read_queue, cadet_has_rds_data(dev));
340 if (i)
341 return i;
342
343 mutex_lock(&dev->lock);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300344 while (i < count && dev->rdsin != dev->rdsout)
345 readbuf[i++] = dev->rdsbuf[dev->rdsout++];
Hans Verkuilce0ede22014-02-10 07:21:36 -0300346 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300348 if (i && copy_to_user(data, readbuf, i))
Hans Verkuilce0ede22014-02-10 07:21:36 -0300349 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return i;
351}
352
353
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300354static int vidioc_querycap(struct file *file, void *priv,
355 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400357 strscpy(v->driver, "ADS Cadet", sizeof(v->driver));
358 strscpy(v->card, "ADS Cadet", sizeof(v->card));
359 strscpy(v->bus_info, "ISA:radio-cadet", sizeof(v->bus_info));
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300363static int vidioc_g_tuner(struct file *file, void *priv,
364 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300366 struct cadet *dev = video_drvdata(file);
367
Hans Verkuil6652c712012-07-10 08:26:04 -0300368 if (v->index)
369 return -EINVAL;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300370 v->type = V4L2_TUNER_RADIO;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400371 strscpy(v->name, "Radio", sizeof(v->name));
Hans Verkuil6652c712012-07-10 08:26:04 -0300372 v->capability = bands[0].capability | bands[1].capability;
373 v->rangelow = bands[0].rangelow; /* 520 kHz (start of AM band) */
374 v->rangehigh = bands[1].rangehigh; /* 108.0 MHz (end of FM band) */
375 if (dev->is_fm_band) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300376 v->rxsubchans = cadet_getstereo(dev);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300377 outb(3, dev->io);
378 outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
379 mdelay(100);
380 outb(3, dev->io);
381 if (inb(dev->io + 1) & 0x80)
382 v->rxsubchans |= V4L2_TUNER_SUB_RDS;
Hans Verkuil6652c712012-07-10 08:26:04 -0300383 } else {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300384 v->rangelow = 8320; /* 520 kHz */
385 v->rangehigh = 26400; /* 1650 kHz */
386 v->rxsubchans = V4L2_TUNER_SUB_MONO;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300387 }
Hans Verkuil6652c712012-07-10 08:26:04 -0300388 v->audmode = V4L2_TUNER_MODE_STEREO;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300389 v->signal = dev->sigstrength; /* We might need to modify scaling of this */
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300390 return 0;
391}
392
393static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300394 const struct v4l2_tuner *v)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300395{
Hans Verkuil6652c712012-07-10 08:26:04 -0300396 return v->index ? -EINVAL : 0;
397}
398
399static int vidioc_enum_freq_bands(struct file *file, void *priv,
400 struct v4l2_frequency_band *band)
401{
402 if (band->tuner)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300403 return -EINVAL;
Hans Verkuil6652c712012-07-10 08:26:04 -0300404 if (band->index >= ARRAY_SIZE(bands))
405 return -EINVAL;
406 *band = bands[band->index];
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300407 return 0;
408}
409
410static int vidioc_g_frequency(struct file *file, void *priv,
411 struct v4l2_frequency *f)
412{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300413 struct cadet *dev = video_drvdata(file);
414
Hans Verkuil6652c712012-07-10 08:26:04 -0300415 if (f->tuner)
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300416 return -EINVAL;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300417 f->type = V4L2_TUNER_RADIO;
Hans Verkuil6652c712012-07-10 08:26:04 -0300418 f->frequency = dev->curfreq;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300419 return 0;
420}
421
422
423static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -0300424 const struct v4l2_frequency *f)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300425{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300426 struct cadet *dev = video_drvdata(file);
427
Hans Verkuil6652c712012-07-10 08:26:04 -0300428 if (f->tuner)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300429 return -EINVAL;
Hans Verkuil6652c712012-07-10 08:26:04 -0300430 dev->is_fm_band =
431 f->frequency >= (bands[0].rangehigh + bands[1].rangelow) / 2;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300432 cadet_setfreq(dev, f->frequency);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300433 return 0;
434}
435
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300436static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300437{
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300438 struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300439
440 switch (ctrl->id) {
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300441 case V4L2_CID_AUDIO_MUTE:
442 outb(7, dev->io); /* Select tuner control */
443 if (ctrl->val)
444 outb(0x00, dev->io + 1);
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300445 else
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300446 outb(0x20, dev->io + 1);
447 return 0;
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300448 }
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300449 return -EINVAL;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300450}
451
452static int cadet_open(struct file *file)
453{
454 struct cadet *dev = video_drvdata(file);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300455 int err;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300456
Hans Verkuil1cccee02010-11-14 09:43:52 -0300457 mutex_lock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300458 err = v4l2_fh_open(file);
459 if (err)
460 goto fail;
461 if (v4l2_fh_is_singular_file(file))
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300462 init_waitqueue_head(&dev->read_queue);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300463fail:
Hans Verkuil1cccee02010-11-14 09:43:52 -0300464 mutex_unlock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300465 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300468static int cadet_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300470 struct cadet *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Hans Verkuil1cccee02010-11-14 09:43:52 -0300472 mutex_lock(&dev->lock);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300473 if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300474 del_timer_sync(&dev->readtimer);
475 dev->rdsstat = 0;
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300476 }
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300477 v4l2_fh_release(file);
Hans Verkuil1cccee02010-11-14 09:43:52 -0300478 mutex_unlock(&dev->lock);
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300479 return 0;
480}
481
Al Viroc23e0cb2017-07-03 03:02:56 -0400482static __poll_t cadet_poll(struct file *file, struct poll_table_struct *wait)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300483{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300484 struct cadet *dev = video_drvdata(file);
Al Viro01699432017-07-03 03:14:15 -0400485 __poll_t req_events = poll_requested_events(wait);
Al Viroc23e0cb2017-07-03 03:02:56 -0400486 __poll_t res = v4l2_ctrl_poll(file, wait);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300487
488 poll_wait(file, &dev->read_queue, wait);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800489 if (dev->rdsstat == 0 && (req_events & (EPOLLIN | EPOLLRDNORM))) {
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300490 mutex_lock(&dev->lock);
491 if (dev->rdsstat == 0)
492 cadet_start_rds(dev);
493 mutex_unlock(&dev->lock);
494 }
Hans Verkuilce0ede22014-02-10 07:21:36 -0300495 if (cadet_has_rds_data(dev))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800496 res |= EPOLLIN | EPOLLRDNORM;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300497 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500
Hans Verkuilbec43662008-12-30 06:58:20 -0300501static const struct v4l2_file_operations cadet_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 .owner = THIS_MODULE,
503 .open = cadet_open,
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500504 .release = cadet_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .read = cadet_read,
Hans Verkuil1cccee02010-11-14 09:43:52 -0300506 .unlocked_ioctl = video_ioctl2,
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300507 .poll = cadet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508};
509
Hans Verkuila3998102008-07-21 02:57:38 -0300510static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300511 .vidioc_querycap = vidioc_querycap,
512 .vidioc_g_tuner = vidioc_g_tuner,
513 .vidioc_s_tuner = vidioc_s_tuner,
514 .vidioc_g_frequency = vidioc_g_frequency,
515 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil6652c712012-07-10 08:26:04 -0300516 .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300517 .vidioc_log_status = v4l2_ctrl_log_status,
518 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
519 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
520};
521
522static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
523 .s_ctrl = cadet_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524};
525
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300526#ifdef CONFIG_PNP
527
Arvind Yadavdb4a8502017-08-16 01:30:10 -0400528static const struct pnp_device_id cadet_pnp_devices[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* ADS Cadet AM/FM Radio Card */
530 {.id = "MSM0c24", .driver_data = 0},
531 {.id = ""}
532};
533
534MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
535
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300536static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 if (!dev)
539 return -ENODEV;
540 /* only support one device */
541 if (io > 0)
542 return -EBUSY;
543
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300544 if (!pnp_port_valid(dev, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 io = pnp_port_start(dev, 0);
548
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300549 printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 return io;
552}
553
554static struct pnp_driver cadet_pnp_driver = {
555 .name = "radio-cadet",
556 .id_table = cadet_pnp_devices,
557 .probe = cadet_pnp_probe,
558 .remove = NULL,
559};
560
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300561#else
562static struct pnp_driver cadet_pnp_driver;
563#endif
564
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300565static void cadet_probe(struct cadet *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300567 static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int i;
569
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300570 for (i = 0; i < 8; i++) {
571 dev->io = iovals[i];
572 if (request_region(dev->io, 2, "cadet-probe")) {
Hans Verkuil6652c712012-07-10 08:26:04 -0300573 cadet_setfreq(dev, bands[1].rangelow);
574 if (cadet_getfreq(dev) == bands[1].rangelow) {
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300575 release_region(dev->io, 2);
576 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300578 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300581 dev->io = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300584/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * io should only be set if the user has used something like
586 * isapnp (the userspace program) to initialize this card for us
587 */
588
589static int __init cadet_init(void)
590{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300591 struct cadet *dev = &cadet_card;
592 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300593 struct v4l2_ctrl_handler *hdl;
594 int res = -ENODEV;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300595
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400596 strscpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300597 mutex_init(&dev->lock);
598
599 /* If a probe was requested then probe ISAPnP first (safest) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (io < 0)
601 pnp_register_driver(&cadet_pnp_driver);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300602 dev->io = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300604 /* If that fails then probe unsafely if probe is requested */
605 if (dev->io < 0)
606 cadet_probe(dev);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300607
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300608 /* Else we bail out */
609 if (dev->io < 0) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300610#ifdef MODULE
Hans Verkuilb24c20cc2009-03-09 08:11:21 -0300611 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
612 v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300614 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300616 if (!request_region(dev->io, 2, "cadet"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 goto fail;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300618
619 res = v4l2_device_register(NULL, v4l2_dev);
620 if (res < 0) {
621 release_region(dev->io, 2);
622 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto fail;
624 }
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300625
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300626 hdl = &dev->ctrl_handler;
627 v4l2_ctrl_handler_init(hdl, 2);
628 v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
629 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
630 v4l2_dev->ctrl_handler = hdl;
631 if (hdl->error) {
632 res = hdl->error;
633 v4l2_err(v4l2_dev, "Could not register controls\n");
634 goto err_hdl;
635 }
636
Hans Verkuil6652c712012-07-10 08:26:04 -0300637 dev->is_fm_band = true;
638 dev->curfreq = bands[dev->is_fm_band].rangelow;
639 cadet_setfreq(dev, dev->curfreq);
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400640 strscpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300641 dev->vdev.v4l2_dev = v4l2_dev;
642 dev->vdev.fops = &cadet_fops;
643 dev->vdev.ioctl_ops = &cadet_ioctl_ops;
644 dev->vdev.release = video_device_release_empty;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300645 dev->vdev.lock = &dev->lock;
Hans Verkuile83ce302019-06-04 07:19:52 -0400646 dev->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
647 V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300648 video_set_drvdata(&dev->vdev, dev);
649
Peter Senna Tschudin8ca70802012-09-06 11:23:52 -0300650 res = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
651 if (res < 0)
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300652 goto err_hdl;
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300653 v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300655err_hdl:
656 v4l2_ctrl_handler_free(hdl);
657 v4l2_device_unregister(v4l2_dev);
658 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659fail:
660 pnp_unregister_driver(&cadet_pnp_driver);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300661 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300664static void __exit cadet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300666 struct cadet *dev = &cadet_card;
667
668 video_unregister_device(&dev->vdev);
Hans Verkuilb54c97db2012-07-02 09:36:39 -0300669 v4l2_ctrl_handler_free(&dev->ctrl_handler);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300670 v4l2_device_unregister(&dev->v4l2_dev);
Hans Verkuilcc0d3262012-07-02 09:46:46 -0300671 outb(7, dev->io); /* Mute */
672 outb(0x00, dev->io + 1);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300673 release_region(dev->io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 pnp_unregister_driver(&cadet_pnp_driver);
675}
676
677module_init(cadet_init);
Hans Verkuilbec2aec2009-03-06 13:48:47 -0300678module_exit(cadet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679