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