Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* 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 Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 12 | * Jason Lewis (jlewis@twilight.vtc.vsc.edu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * 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 Cox | d9b0144 | 2008-10-27 15:13:47 -0300 | [diff] [blame] | 27 | * 2003-01-31 Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * Cleaned up locking, delay code, general odds and ends |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 29 | * |
| 30 | * 2006-07-30 Hans J. Koch <koch@hjk-az.de> |
| 31 | * Changed API to V4L2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | */ |
| 33 | |
Mauro Carvalho Chehab | 6e6a8b5 | 2018-01-04 13:08:56 -0500 | [diff] [blame] | 34 | #include <linux/module.h> /* Modules */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/init.h> /* Initdata */ |
Peter Osterlund | fb911ee | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 36 | #include <linux/ioport.h> /* request_region */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/delay.h> /* udelay */ |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 38 | #include <linux/videodev2.h> /* V4L2 API defs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/param.h> |
| 40 | #include <linux/pnp.h> |
Alexey Dobriyan | a99bbaf | 2009-10-04 16:11:37 +0400 | [diff] [blame] | 41 | #include <linux/sched.h> |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 42 | #include <linux/io.h> /* outb, outb_p */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 43 | #include <media/v4l2-device.h> |
| 44 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 45 | #include <media/v4l2-ctrls.h> |
| 46 | #include <media/v4l2-fh.h> |
| 47 | #include <media/v4l2-event.h> |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 48 | |
| 49 | MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath"); |
| 50 | MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card."); |
| 51 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 29834c1 | 2011-06-25 10:15:42 -0300 | [diff] [blame] | 52 | MODULE_VERSION("0.3.4"); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 53 | |
| 54 | static int io = -1; /* default to isapnp activation */ |
| 55 | static int radio_nr = -1; |
| 56 | |
| 57 | module_param(io, int, 0); |
| 58 | MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)"); |
| 59 | module_param(radio_nr, int, 0); |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #define RDS_BUFFER 256 |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 62 | #define RDS_RX_FLAG 1 |
| 63 | #define MBS_RX_FLAG 2 |
| 64 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 65 | struct cadet { |
| 66 | struct v4l2_device v4l2_dev; |
| 67 | struct video_device vdev; |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 68 | struct v4l2_ctrl_handler ctrl_handler; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 69 | int io; |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 70 | bool is_fm_band; |
| 71 | u32 curfreq; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 72 | int tunestat; |
| 73 | int sigstrength; |
| 74 | wait_queue_head_t read_queue; |
| 75 | struct timer_list readtimer; |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 76 | u8 rdsin, rdsout, rdsstat; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 77 | unsigned char rdsbuf[RDS_BUFFER]; |
| 78 | struct mutex lock; |
| 79 | int reading; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 80 | }; |
| 81 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 82 | static struct cadet cadet_card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Signal Strength Threshold Values |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 86 | * The V4L API spec does not define any particular unit for the signal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * strength value. These values are in microvolts of RF at the tuner's input. |
| 88 | */ |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 89 | static u16 sigtable[2][4] = { |
| 90 | { 1835, 2621, 4128, 65535 }, |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 91 | { 2185, 4369, 13107, 65535 }, |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 92 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 94 | static 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. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 114 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 115 | static int cadet_getstereo(struct cadet *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 117 | int ret = V4L2_TUNER_SUB_MONO; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 118 | |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 119 | if (!dev->is_fm_band) /* Only FM has stereo capability! */ |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 120 | return V4L2_TUNER_SUB_MONO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 122 | outb(7, dev->io); /* Select tuner control */ |
| 123 | if ((inb(dev->io + 1) & 0x40) == 0) |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 124 | ret = V4L2_TUNER_SUB_STEREO; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 125 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 128 | static unsigned cadet_gettune(struct cadet *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 130 | int curvol, i; |
| 131 | unsigned fifo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 133 | /* |
| 134 | * Prepare for read |
| 135 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 137 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 142 | /* |
| 143 | * Read the shift register |
| 144 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 145 | 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 Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 151 | } |
| 152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 154 | /* |
| 155 | * Restore volume/mute setting |
| 156 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 157 | outb(curvol, dev->io + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return fifo; |
| 159 | } |
| 160 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 161 | static unsigned cadet_getfreq(struct cadet *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 163 | int i; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 164 | unsigned freq = 0, test, fifo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * Read current tuning |
| 168 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 169 | fifo = cadet_gettune(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 171 | /* |
| 172 | * Convert to actual frequency |
| 173 | */ |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 174 | if (!dev->is_fm_band) /* AM */ |
| 175 | return ((fifo & 0x7fff) - 450) * 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 177 | 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 Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 186 | return freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 189 | static void cadet_settune(struct cadet *dev, unsigned fifo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 191 | int i; |
| 192 | unsigned test; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 194 | outb(7, dev->io); /* Select tuner control */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* |
| 196 | * Write the shift register |
| 197 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 198 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 214 | static void cadet_setfreq(struct cadet *dev, unsigned freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 216 | unsigned fifo; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 217 | int i, j, test; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 218 | int curvol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 220 | freq = clamp(freq, bands[dev->is_fm_band].rangelow, |
| 221 | bands[dev->is_fm_band].rangehigh); |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 222 | dev->curfreq = freq; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 223 | /* |
| 224 | * Formulate a fifo command |
| 225 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 226 | fifo = 0; |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 227 | if (dev->is_fm_band) { /* FM */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 228 | test = 102400; |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 229 | freq = freq / 16; /* Make it kHz */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 230 | 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 Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 236 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 237 | test = test >> 1; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 238 | } |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 239 | } else { /* AM */ |
| 240 | fifo = (freq / 16) + 450; /* Make it kHz */ |
| 241 | fifo |= 0x100000; /* Select AM Band */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 244 | /* |
| 245 | * Save current volume/mute setting |
| 246 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 248 | outb(7, dev->io); /* Select tuner control */ |
| 249 | curvol = inb(dev->io + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * Tune the card |
| 253 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 254 | for (j = 3; j > -1; j--) { |
| 255 | cadet_settune(dev, fifo | (j << 16)); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 256 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 257 | outb(7, dev->io); /* Select tuner control */ |
| 258 | outb(curvol, dev->io + 1); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | msleep(100); |
| 261 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 262 | cadet_gettune(dev); |
| 263 | if ((dev->tunestat & 0x40) == 0) { /* Tuned */ |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 264 | dev->sigstrength = sigtable[dev->is_fm_band][j]; |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 265 | goto reset_rds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 268 | dev->sigstrength = 0; |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 269 | reset_rds: |
| 270 | outb(3, dev->io); |
| 271 | outb(inb(dev->io + 1) & 0x7f, dev->io + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Hans Verkuil | ce0ede2 | 2014-02-10 07:21:36 -0300 | [diff] [blame] | 274 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Kees Cook | 297fced | 2017-10-24 11:23:03 -0400 | [diff] [blame] | 285 | static void cadet_handler(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Kees Cook | 297fced | 2017-10-24 11:23:03 -0400 | [diff] [blame] | 287 | struct cadet *dev = from_timer(dev, t, readtimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 289 | /* 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 Verkuil | ce0ede2 | 2014-02-10 07:21:36 -0300 | [diff] [blame] | 293 | pr_err("cadet: RDS fifo overflow\n"); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 294 | outb(0x80, dev->io); /* Select RDS fifo */ |
Hans Verkuil | ce0ede2 | 2014-02-10 07:21:36 -0300 | [diff] [blame] | 295 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 296 | while ((inb(dev->io) & 0x80) != 0) { |
| 297 | dev->rdsbuf[dev->rdsin] = inb(dev->io + 1); |
Hans Verkuil | ce0ede2 | 2014-02-10 07:21:36 -0300 | [diff] [blame] | 298 | if (dev->rdsin + 1 != dev->rdsout) |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 299 | dev->rdsin++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 301 | mutex_unlock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Service pending read |
| 306 | */ |
Hans Verkuil | ce0ede2 | 2014-02-10 07:21:36 -0300 | [diff] [blame] | 307 | if (cadet_has_rds_data(dev)) |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 308 | wake_up_interruptible(&dev->read_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 310 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | * Clean up and exit |
| 312 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 313 | dev->readtimer.expires = jiffies + msecs_to_jiffies(50); |
| 314 | add_timer(&dev->readtimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 317 | static void cadet_start_rds(struct cadet *dev) |
| 318 | { |
| 319 | dev->rdsstat = 1; |
| 320 | outb(0x80, dev->io); /* Select RDS fifo */ |
Kees Cook | 297fced | 2017-10-24 11:23:03 -0400 | [diff] [blame] | 321 | timer_setup(&dev->readtimer, cadet_handler, 0); |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 322 | dev->readtimer.expires = jiffies + msecs_to_jiffies(50); |
| 323 | add_timer(&dev->readtimer); |
| 324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 326 | static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 328 | struct cadet *dev = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | unsigned char readbuf[RDS_BUFFER]; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 330 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 332 | mutex_lock(&dev->lock); |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 333 | if (dev->rdsstat == 0) |
| 334 | cadet_start_rds(dev); |
Hans Verkuil | ce0ede2 | 2014-02-10 07:21:36 -0300 | [diff] [blame] | 335 | 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 Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 344 | while (i < count && dev->rdsin != dev->rdsout) |
| 345 | readbuf[i++] = dev->rdsbuf[dev->rdsout++]; |
Hans Verkuil | ce0ede2 | 2014-02-10 07:21:36 -0300 | [diff] [blame] | 346 | mutex_unlock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 348 | if (i && copy_to_user(data, readbuf, i)) |
Hans Verkuil | ce0ede2 | 2014-02-10 07:21:36 -0300 | [diff] [blame] | 349 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return i; |
| 351 | } |
| 352 | |
| 353 | |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 354 | static int vidioc_querycap(struct file *file, void *priv, |
| 355 | struct v4l2_capability *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 357 | 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 Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 360 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 363 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 364 | struct v4l2_tuner *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 366 | struct cadet *dev = video_drvdata(file); |
| 367 | |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 368 | if (v->index) |
| 369 | return -EINVAL; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 370 | v->type = V4L2_TUNER_RADIO; |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 371 | strscpy(v->name, "Radio", sizeof(v->name)); |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 372 | 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 Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 376 | v->rxsubchans = cadet_getstereo(dev); |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 377 | 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 Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 383 | } else { |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 384 | v->rangelow = 8320; /* 520 kHz */ |
| 385 | v->rangehigh = 26400; /* 1650 kHz */ |
| 386 | v->rxsubchans = V4L2_TUNER_SUB_MONO; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 387 | } |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 388 | v->audmode = V4L2_TUNER_MODE_STEREO; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 389 | v->signal = dev->sigstrength; /* We might need to modify scaling of this */ |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int vidioc_s_tuner(struct file *file, void *priv, |
Hans Verkuil | 2f73c7c | 2013-03-15 06:10:06 -0300 | [diff] [blame] | 394 | const struct v4l2_tuner *v) |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 395 | { |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 396 | return v->index ? -EINVAL : 0; |
| 397 | } |
| 398 | |
| 399 | static int vidioc_enum_freq_bands(struct file *file, void *priv, |
| 400 | struct v4l2_frequency_band *band) |
| 401 | { |
| 402 | if (band->tuner) |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 403 | return -EINVAL; |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 404 | if (band->index >= ARRAY_SIZE(bands)) |
| 405 | return -EINVAL; |
| 406 | *band = bands[band->index]; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 411 | struct v4l2_frequency *f) |
| 412 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 413 | struct cadet *dev = video_drvdata(file); |
| 414 | |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 415 | if (f->tuner) |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 416 | return -EINVAL; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 417 | f->type = V4L2_TUNER_RADIO; |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 418 | f->frequency = dev->curfreq; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | static int vidioc_s_frequency(struct file *file, void *priv, |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 424 | const struct v4l2_frequency *f) |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 425 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 426 | struct cadet *dev = video_drvdata(file); |
| 427 | |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 428 | if (f->tuner) |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 429 | return -EINVAL; |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 430 | dev->is_fm_band = |
| 431 | f->frequency >= (bands[0].rangehigh + bands[1].rangelow) / 2; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 432 | cadet_setfreq(dev, f->frequency); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 436 | static int cadet_s_ctrl(struct v4l2_ctrl *ctrl) |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 437 | { |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 438 | struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 439 | |
| 440 | switch (ctrl->id) { |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 441 | case V4L2_CID_AUDIO_MUTE: |
| 442 | outb(7, dev->io); /* Select tuner control */ |
| 443 | if (ctrl->val) |
| 444 | outb(0x00, dev->io + 1); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 445 | else |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 446 | outb(0x20, dev->io + 1); |
| 447 | return 0; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 448 | } |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 449 | return -EINVAL; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static int cadet_open(struct file *file) |
| 453 | { |
| 454 | struct cadet *dev = video_drvdata(file); |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 455 | int err; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 456 | |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 457 | mutex_lock(&dev->lock); |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 458 | err = v4l2_fh_open(file); |
| 459 | if (err) |
| 460 | goto fail; |
| 461 | if (v4l2_fh_is_singular_file(file)) |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 462 | init_waitqueue_head(&dev->read_queue); |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 463 | fail: |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 464 | mutex_unlock(&dev->lock); |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 465 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 468 | static int cadet_release(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 470 | struct cadet *dev = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 472 | mutex_lock(&dev->lock); |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 473 | if (v4l2_fh_is_singular_file(file) && dev->rdsstat) { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 474 | del_timer_sync(&dev->readtimer); |
| 475 | dev->rdsstat = 0; |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 476 | } |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 477 | v4l2_fh_release(file); |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 478 | mutex_unlock(&dev->lock); |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | |
Al Viro | c23e0cb | 2017-07-03 03:02:56 -0400 | [diff] [blame] | 482 | static __poll_t cadet_poll(struct file *file, struct poll_table_struct *wait) |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 483 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 484 | struct cadet *dev = video_drvdata(file); |
Al Viro | 0169943 | 2017-07-03 03:14:15 -0400 | [diff] [blame] | 485 | __poll_t req_events = poll_requested_events(wait); |
Al Viro | c23e0cb | 2017-07-03 03:02:56 -0400 | [diff] [blame] | 486 | __poll_t res = v4l2_ctrl_poll(file, wait); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 487 | |
| 488 | poll_wait(file, &dev->read_queue, wait); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 489 | if (dev->rdsstat == 0 && (req_events & (EPOLLIN | EPOLLRDNORM))) { |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 490 | mutex_lock(&dev->lock); |
| 491 | if (dev->rdsstat == 0) |
| 492 | cadet_start_rds(dev); |
| 493 | mutex_unlock(&dev->lock); |
| 494 | } |
Hans Verkuil | ce0ede2 | 2014-02-10 07:21:36 -0300 | [diff] [blame] | 495 | if (cadet_has_rds_data(dev)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 496 | res |= EPOLLIN | EPOLLRDNORM; |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 497 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 501 | static const struct v4l2_file_operations cadet_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | .owner = THIS_MODULE, |
| 503 | .open = cadet_open, |
Mauro Carvalho Chehab | 6e6a8b5 | 2018-01-04 13:08:56 -0500 | [diff] [blame] | 504 | .release = cadet_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | .read = cadet_read, |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 506 | .unlocked_ioctl = video_ioctl2, |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 507 | .poll = cadet_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | }; |
| 509 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 510 | static const struct v4l2_ioctl_ops cadet_ioctl_ops = { |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 511 | .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 Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 516 | .vidioc_enum_freq_bands = vidioc_enum_freq_bands, |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 517 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 518 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 519 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
| 520 | }; |
| 521 | |
| 522 | static const struct v4l2_ctrl_ops cadet_ctrl_ops = { |
| 523 | .s_ctrl = cadet_s_ctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | }; |
| 525 | |
Bjorn Helgaas | 044dfc9 | 2008-03-31 21:21:48 -0300 | [diff] [blame] | 526 | #ifdef CONFIG_PNP |
| 527 | |
Arvind Yadav | db4a850 | 2017-08-16 01:30:10 -0400 | [diff] [blame] | 528 | static const struct pnp_device_id cadet_pnp_devices[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | /* ADS Cadet AM/FM Radio Card */ |
| 530 | {.id = "MSM0c24", .driver_data = 0}, |
| 531 | {.id = ""} |
| 532 | }; |
| 533 | |
| 534 | MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices); |
| 535 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 536 | static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | { |
| 538 | if (!dev) |
| 539 | return -ENODEV; |
| 540 | /* only support one device */ |
| 541 | if (io > 0) |
| 542 | return -EBUSY; |
| 543 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 544 | if (!pnp_port_valid(dev, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | io = pnp_port_start(dev, 0); |
| 548 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 549 | printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | return io; |
| 552 | } |
| 553 | |
| 554 | static 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 Helgaas | 044dfc9 | 2008-03-31 21:21:48 -0300 | [diff] [blame] | 561 | #else |
| 562 | static struct pnp_driver cadet_pnp_driver; |
| 563 | #endif |
| 564 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 565 | static void cadet_probe(struct cadet *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 567 | static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | int i; |
| 569 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 570 | for (i = 0; i < 8; i++) { |
| 571 | dev->io = iovals[i]; |
| 572 | if (request_region(dev->io, 2, "cadet-probe")) { |
Hans Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 573 | cadet_setfreq(dev, bands[1].rangelow); |
| 574 | if (cadet_getfreq(dev) == bands[1].rangelow) { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 575 | release_region(dev->io, 2); |
| 576 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 578 | release_region(dev->io, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 581 | dev->io = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 584 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | * 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 | |
| 589 | static int __init cadet_init(void) |
| 590 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 591 | struct cadet *dev = &cadet_card; |
| 592 | struct v4l2_device *v4l2_dev = &dev->v4l2_dev; |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 593 | struct v4l2_ctrl_handler *hdl; |
| 594 | int res = -ENODEV; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 595 | |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 596 | strscpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name)); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 597 | mutex_init(&dev->lock); |
| 598 | |
| 599 | /* If a probe was requested then probe ISAPnP first (safest) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | if (io < 0) |
| 601 | pnp_register_driver(&cadet_pnp_driver); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 602 | dev->io = io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 604 | /* If that fails then probe unsafely if probe is requested */ |
| 605 | if (dev->io < 0) |
| 606 | cadet_probe(dev); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 607 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 608 | /* Else we bail out */ |
| 609 | if (dev->io < 0) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 610 | #ifdef MODULE |
Hans Verkuil | b24c20cc | 2009-03-09 08:11:21 -0300 | [diff] [blame] | 611 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | #endif |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 614 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 616 | if (!request_region(dev->io, 2, "cadet")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | goto fail; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 618 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | goto fail; |
| 624 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 625 | |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 626 | 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 Verkuil | 6652c71 | 2012-07-10 08:26:04 -0300 | [diff] [blame] | 637 | dev->is_fm_band = true; |
| 638 | dev->curfreq = bands[dev->is_fm_band].rangelow; |
| 639 | cadet_setfreq(dev, dev->curfreq); |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 640 | strscpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 641 | 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 Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 645 | dev->vdev.lock = &dev->lock; |
Hans Verkuil | e83ce30 | 2019-06-04 07:19:52 -0400 | [diff] [blame] | 646 | dev->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO | |
| 647 | V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 648 | video_set_drvdata(&dev->vdev, dev); |
| 649 | |
Peter Senna Tschudin | 8ca7080 | 2012-09-06 11:23:52 -0300 | [diff] [blame] | 650 | res = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr); |
| 651 | if (res < 0) |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 652 | goto err_hdl; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 653 | v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return 0; |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 655 | err_hdl: |
| 656 | v4l2_ctrl_handler_free(hdl); |
| 657 | v4l2_device_unregister(v4l2_dev); |
| 658 | release_region(dev->io, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | fail: |
| 660 | pnp_unregister_driver(&cadet_pnp_driver); |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 661 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 664 | static void __exit cadet_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 666 | struct cadet *dev = &cadet_card; |
| 667 | |
| 668 | video_unregister_device(&dev->vdev); |
Hans Verkuil | b54c97db | 2012-07-02 09:36:39 -0300 | [diff] [blame] | 669 | v4l2_ctrl_handler_free(&dev->ctrl_handler); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 670 | v4l2_device_unregister(&dev->v4l2_dev); |
Hans Verkuil | cc0d326 | 2012-07-02 09:46:46 -0300 | [diff] [blame] | 671 | outb(7, dev->io); /* Mute */ |
| 672 | outb(0x00, dev->io + 1); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 673 | release_region(dev->io, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | pnp_unregister_driver(&cadet_pnp_driver); |
| 675 | } |
| 676 | |
| 677 | module_init(cadet_init); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 678 | module_exit(cadet_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |