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 | |
| 33 | #include <linux/module.h> /* Modules */ |
| 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> |
| 44 | |
| 45 | MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath"); |
| 46 | MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card."); |
| 47 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 29834c1 | 2011-06-25 10:15:42 -0300 | [diff] [blame] | 48 | MODULE_VERSION("0.3.4"); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 49 | |
| 50 | static int io = -1; /* default to isapnp activation */ |
| 51 | static int radio_nr = -1; |
| 52 | |
| 53 | module_param(io, int, 0); |
| 54 | MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)"); |
| 55 | module_param(radio_nr, int, 0); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #define RDS_BUFFER 256 |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 58 | #define RDS_RX_FLAG 1 |
| 59 | #define MBS_RX_FLAG 2 |
| 60 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 61 | struct cadet { |
| 62 | struct v4l2_device v4l2_dev; |
| 63 | struct video_device vdev; |
| 64 | int io; |
| 65 | int users; |
| 66 | int curtuner; |
| 67 | int tunestat; |
| 68 | int sigstrength; |
| 69 | wait_queue_head_t read_queue; |
| 70 | struct timer_list readtimer; |
| 71 | __u8 rdsin, rdsout, rdsstat; |
| 72 | unsigned char rdsbuf[RDS_BUFFER]; |
| 73 | struct mutex lock; |
| 74 | int reading; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 75 | }; |
| 76 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 77 | static struct cadet cadet_card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * Signal Strength Threshold Values |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 81 | * 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] | 82 | * strength value. These values are in microvolts of RF at the tuner's input. |
| 83 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 84 | static __u16 sigtable[2][4] = { |
| 85 | { 5, 10, 30, 150 }, |
| 86 | { 28, 40, 63, 1000 } |
| 87 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 89 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 90 | static int cadet_getstereo(struct cadet *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 92 | int ret = V4L2_TUNER_SUB_MONO; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 93 | |
| 94 | if (dev->curtuner != 0) /* Only FM has stereo capability! */ |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 95 | return V4L2_TUNER_SUB_MONO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 97 | mutex_lock(&dev->lock); |
| 98 | outb(7, dev->io); /* Select tuner control */ |
| 99 | if ((inb(dev->io + 1) & 0x40) == 0) |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 100 | ret = V4L2_TUNER_SUB_STEREO; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 101 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 102 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 105 | static unsigned cadet_gettune(struct cadet *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 107 | int curvol, i; |
| 108 | unsigned fifo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 110 | /* |
| 111 | * Prepare for read |
| 112 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 114 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 115 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 116 | outb(7, dev->io); /* Select tuner control */ |
| 117 | curvol = inb(dev->io + 1); /* Save current volume/mute setting */ |
| 118 | outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */ |
| 119 | dev->tunestat = 0xffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 121 | /* |
| 122 | * Read the shift register |
| 123 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 124 | for (i = 0; i < 25; i++) { |
| 125 | fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01); |
| 126 | if (i < 24) { |
| 127 | outb(0x01, dev->io + 1); |
| 128 | dev->tunestat &= inb(dev->io + 1); |
| 129 | outb(0x00, dev->io + 1); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 130 | } |
| 131 | } |
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 | * Restore volume/mute setting |
| 135 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 136 | outb(curvol, dev->io + 1); |
| 137 | mutex_unlock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | return fifo; |
| 140 | } |
| 141 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 142 | static unsigned cadet_getfreq(struct cadet *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 144 | int i; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 145 | unsigned freq = 0, test, fifo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * Read current tuning |
| 149 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 150 | fifo = cadet_gettune(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 152 | /* |
| 153 | * Convert to actual frequency |
| 154 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 155 | if (dev->curtuner == 0) { /* FM */ |
| 156 | test = 12500; |
| 157 | for (i = 0; i < 14; i++) { |
| 158 | if ((fifo & 0x01) != 0) |
| 159 | freq += test; |
| 160 | test = test << 1; |
| 161 | fifo = fifo >> 1; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 162 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 163 | freq -= 10700000; /* IF frequency is 10.7 MHz */ |
| 164 | freq = (freq * 16) / 1000000; /* Make it 1/16 MHz */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 166 | if (dev->curtuner == 1) /* AM */ |
| 167 | freq = ((fifo & 0x7fff) - 2010) * 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 169 | return freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 172 | static void cadet_settune(struct cadet *dev, unsigned fifo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 174 | int i; |
| 175 | unsigned test; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 177 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 178 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 179 | outb(7, dev->io); /* Select tuner control */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* |
| 181 | * Write the shift register |
| 182 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 183 | test = 0; |
| 184 | test = (fifo >> 23) & 0x02; /* Align data for SDO */ |
| 185 | test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */ |
| 186 | outb(7, dev->io); /* Select tuner control */ |
| 187 | outb(test, dev->io + 1); /* Initialize for write */ |
| 188 | for (i = 0; i < 25; i++) { |
| 189 | test |= 0x01; /* Toggle SCK High */ |
| 190 | outb(test, dev->io + 1); |
| 191 | test &= 0xfe; /* Toggle SCK Low */ |
| 192 | outb(test, dev->io + 1); |
| 193 | fifo = fifo << 1; /* Prepare the next bit */ |
| 194 | test = 0x1c | ((fifo >> 23) & 0x02); |
| 195 | outb(test, dev->io + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 197 | mutex_unlock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 200 | static void cadet_setfreq(struct cadet *dev, unsigned freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 202 | unsigned fifo; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 203 | int i, j, test; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 204 | int curvol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 206 | /* |
| 207 | * Formulate a fifo command |
| 208 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 209 | fifo = 0; |
| 210 | if (dev->curtuner == 0) { /* FM */ |
| 211 | test = 102400; |
| 212 | freq = (freq * 1000) / 16; /* Make it kHz */ |
| 213 | freq += 10700; /* IF is 10700 kHz */ |
| 214 | for (i = 0; i < 14; i++) { |
| 215 | fifo = fifo << 1; |
| 216 | if (freq >= test) { |
| 217 | fifo |= 0x01; |
| 218 | freq -= test; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 219 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 220 | test = test >> 1; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 223 | if (dev->curtuner == 1) { /* AM */ |
| 224 | fifo = (freq / 16) + 2010; /* Make it kHz */ |
| 225 | fifo |= 0x100000; /* Select AM Band */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 228 | /* |
| 229 | * Save current volume/mute setting |
| 230 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 232 | mutex_lock(&dev->lock); |
| 233 | outb(7, dev->io); /* Select tuner control */ |
| 234 | curvol = inb(dev->io + 1); |
| 235 | mutex_unlock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * Tune the card |
| 239 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 240 | for (j = 3; j > -1; j--) { |
| 241 | cadet_settune(dev, fifo | (j << 16)); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 242 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 243 | mutex_lock(&dev->lock); |
| 244 | outb(7, dev->io); /* Select tuner control */ |
| 245 | outb(curvol, dev->io + 1); |
| 246 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | msleep(100); |
| 249 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 250 | cadet_gettune(dev); |
| 251 | if ((dev->tunestat & 0x40) == 0) { /* Tuned */ |
| 252 | dev->sigstrength = sigtable[dev->curtuner][j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 256 | dev->sigstrength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 260 | static int cadet_getvol(struct cadet *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
| 262 | int ret = 0; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 263 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 264 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 265 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 266 | outb(7, dev->io); /* Select tuner control */ |
| 267 | if ((inb(dev->io + 1) & 0x20) != 0) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 268 | ret = 0xffff; |
| 269 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 270 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 271 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 275 | static void cadet_setvol(struct cadet *dev, int vol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 277 | mutex_lock(&dev->lock); |
| 278 | outb(7, dev->io); /* Select tuner control */ |
| 279 | if (vol > 0) |
| 280 | outb(0x20, dev->io + 1); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 281 | else |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 282 | outb(0x00, dev->io + 1); |
| 283 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 286 | static void cadet_handler(unsigned long data) |
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 | struct cadet *dev = (void *)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 290 | /* Service the RDS fifo */ |
| 291 | if (mutex_trylock(&dev->lock)) { |
| 292 | outb(0x3, dev->io); /* Select RDS Decoder Control */ |
| 293 | if ((inb(dev->io + 1) & 0x20) != 0) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 294 | printk(KERN_CRIT "cadet: RDS fifo overflow\n"); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 295 | outb(0x80, dev->io); /* Select RDS fifo */ |
| 296 | while ((inb(dev->io) & 0x80) != 0) { |
| 297 | dev->rdsbuf[dev->rdsin] = inb(dev->io + 1); |
| 298 | if (dev->rdsin == dev->rdsout) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 299 | printk(KERN_WARNING "cadet: RDS buffer overflow\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | else |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 301 | dev->rdsin++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 303 | mutex_unlock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Service pending read |
| 308 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 309 | if (dev->rdsin != dev->rdsout) |
| 310 | wake_up_interruptible(&dev->read_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 312 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | * Clean up and exit |
| 314 | */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 315 | init_timer(&dev->readtimer); |
| 316 | dev->readtimer.function = cadet_handler; |
| 317 | dev->readtimer.data = (unsigned long)0; |
| 318 | dev->readtimer.expires = jiffies + msecs_to_jiffies(50); |
| 319 | add_timer(&dev->readtimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 323 | 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] | 324 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 325 | struct cadet *dev = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | unsigned char readbuf[RDS_BUFFER]; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 327 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 329 | mutex_lock(&dev->lock); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 330 | if (dev->rdsstat == 0) { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 331 | dev->rdsstat = 1; |
| 332 | outb(0x80, dev->io); /* Select RDS fifo */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 333 | init_timer(&dev->readtimer); |
| 334 | dev->readtimer.function = cadet_handler; |
| 335 | dev->readtimer.data = (unsigned long)dev; |
| 336 | dev->readtimer.expires = jiffies + msecs_to_jiffies(50); |
| 337 | add_timer(&dev->readtimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 339 | if (dev->rdsin == dev->rdsout) { |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 340 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 341 | if (file->f_flags & O_NONBLOCK) |
| 342 | return -EWOULDBLOCK; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 343 | interruptible_sleep_on(&dev->read_queue); |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 344 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 345 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 346 | while (i < count && dev->rdsin != dev->rdsout) |
| 347 | readbuf[i++] = dev->rdsbuf[dev->rdsout++]; |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 348 | mutex_unlock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 350 | if (copy_to_user(data, readbuf, i)) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 351 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return i; |
| 353 | } |
| 354 | |
| 355 | |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 356 | static int vidioc_querycap(struct file *file, void *priv, |
| 357 | struct v4l2_capability *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 359 | strlcpy(v->driver, "ADS Cadet", sizeof(v->driver)); |
| 360 | strlcpy(v->card, "ADS Cadet", sizeof(v->card)); |
| 361 | strlcpy(v->bus_info, "ISA", sizeof(v->bus_info)); |
Hans Verkuil | 8e280f2 | 2009-06-20 06:19:09 -0300 | [diff] [blame] | 362 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO | |
| 363 | V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 364 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 367 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 368 | struct v4l2_tuner *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 370 | struct cadet *dev = video_drvdata(file); |
| 371 | |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 372 | v->type = V4L2_TUNER_RADIO; |
| 373 | switch (v->index) { |
| 374 | case 0: |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 375 | strlcpy(v->name, "FM", sizeof(v->name)); |
Matti Aaltonen | cb0ed22 | 2010-10-18 06:54:14 -0300 | [diff] [blame] | 376 | v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS | |
| 377 | V4L2_TUNER_CAP_RDS_BLOCK_IO; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 378 | v->rangelow = 1400; /* 87.5 MHz */ |
| 379 | v->rangehigh = 1728; /* 108.0 MHz */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 380 | v->rxsubchans = cadet_getstereo(dev); |
| 381 | switch (v->rxsubchans) { |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 382 | case V4L2_TUNER_SUB_MONO: |
| 383 | v->audmode = V4L2_TUNER_MODE_MONO; |
| 384 | break; |
| 385 | case V4L2_TUNER_SUB_STEREO: |
| 386 | v->audmode = V4L2_TUNER_MODE_STEREO; |
| 387 | break; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 388 | default: |
| 389 | break; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 390 | } |
Hans Verkuil | 8e280f2 | 2009-06-20 06:19:09 -0300 | [diff] [blame] | 391 | v->rxsubchans |= V4L2_TUNER_SUB_RDS; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 392 | break; |
| 393 | case 1: |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 394 | strlcpy(v->name, "AM", sizeof(v->name)); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 395 | v->capability = V4L2_TUNER_CAP_LOW; |
| 396 | v->rangelow = 8320; /* 520 kHz */ |
| 397 | v->rangehigh = 26400; /* 1650 kHz */ |
| 398 | v->rxsubchans = V4L2_TUNER_SUB_MONO; |
| 399 | v->audmode = V4L2_TUNER_MODE_MONO; |
| 400 | break; |
| 401 | default: |
| 402 | return -EINVAL; |
| 403 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 404 | v->signal = dev->sigstrength; /* We might need to modify scaling of this */ |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 409 | struct v4l2_tuner *v) |
| 410 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 411 | struct cadet *dev = video_drvdata(file); |
| 412 | |
| 413 | if (v->index != 0 && v->index != 1) |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 414 | return -EINVAL; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 415 | dev->curtuner = v->index; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 420 | struct v4l2_frequency *f) |
| 421 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 422 | struct cadet *dev = video_drvdata(file); |
| 423 | |
| 424 | f->tuner = dev->curtuner; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 425 | f->type = V4L2_TUNER_RADIO; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 426 | f->frequency = cadet_getfreq(dev); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | |
| 431 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 432 | struct v4l2_frequency *f) |
| 433 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 434 | struct cadet *dev = video_drvdata(file); |
| 435 | |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 436 | if (f->type != V4L2_TUNER_RADIO) |
| 437 | return -EINVAL; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 438 | if (dev->curtuner == 0 && (f->frequency < 1400 || f->frequency > 1728)) |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 439 | return -EINVAL; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 440 | if (dev->curtuner == 1 && (f->frequency < 8320 || f->frequency > 26400)) |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 441 | return -EINVAL; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 442 | cadet_setfreq(dev, f->frequency); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 447 | struct v4l2_queryctrl *qc) |
| 448 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 449 | switch (qc->id) { |
| 450 | case V4L2_CID_AUDIO_MUTE: |
| 451 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); |
| 452 | case V4L2_CID_AUDIO_VOLUME: |
| 453 | return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 454 | } |
| 455 | return -EINVAL; |
| 456 | } |
| 457 | |
| 458 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 459 | struct v4l2_control *ctrl) |
| 460 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 461 | struct cadet *dev = video_drvdata(file); |
| 462 | |
| 463 | switch (ctrl->id) { |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 464 | case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */ |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 465 | ctrl->value = (cadet_getvol(dev) == 0); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 466 | break; |
| 467 | case V4L2_CID_AUDIO_VOLUME: |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 468 | ctrl->value = cadet_getvol(dev); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 469 | break; |
| 470 | default: |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 477 | struct v4l2_control *ctrl) |
| 478 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 479 | struct cadet *dev = video_drvdata(file); |
| 480 | |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 481 | switch (ctrl->id){ |
| 482 | case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */ |
| 483 | if (ctrl->value) |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 484 | cadet_setvol(dev, 0); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 485 | else |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 486 | cadet_setvol(dev, 0xffff); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 487 | break; |
| 488 | case V4L2_CID_AUDIO_VOLUME: |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 489 | cadet_setvol(dev, ctrl->value); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 490 | break; |
| 491 | default: |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | return 0; |
| 495 | } |
| 496 | |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 497 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
| 498 | { |
| 499 | *i = 0; |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
| 504 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 505 | return i ? -EINVAL : 0; |
| 506 | } |
| 507 | |
| 508 | static int vidioc_g_audio(struct file *file, void *priv, |
| 509 | struct v4l2_audio *a) |
| 510 | { |
| 511 | a->index = 0; |
| 512 | strlcpy(a->name, "Radio", sizeof(a->name)); |
| 513 | a->capability = V4L2_AUDCAP_STEREO; |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int vidioc_s_audio(struct file *file, void *priv, |
| 518 | struct v4l2_audio *a) |
| 519 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 520 | return a->index ? -EINVAL : 0; |
| 521 | } |
| 522 | |
| 523 | static int cadet_open(struct file *file) |
| 524 | { |
| 525 | struct cadet *dev = video_drvdata(file); |
| 526 | |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 527 | mutex_lock(&dev->lock); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 528 | dev->users++; |
| 529 | if (1 == dev->users) |
| 530 | init_waitqueue_head(&dev->read_queue); |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 531 | mutex_unlock(&dev->lock); |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 532 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 535 | static int cadet_release(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 537 | struct cadet *dev = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 539 | mutex_lock(&dev->lock); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 540 | dev->users--; |
| 541 | if (0 == dev->users) { |
| 542 | del_timer_sync(&dev->readtimer); |
| 543 | dev->rdsstat = 0; |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 544 | } |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 545 | mutex_unlock(&dev->lock); |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 549 | 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] | 550 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 551 | struct cadet *dev = video_drvdata(file); |
| 552 | |
| 553 | poll_wait(file, &dev->read_queue, wait); |
| 554 | if (dev->rdsin != dev->rdsout) |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 555 | return POLLIN | POLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 560 | static const struct v4l2_file_operations cadet_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | .owner = THIS_MODULE, |
| 562 | .open = cadet_open, |
| 563 | .release = cadet_release, |
| 564 | .read = cadet_read, |
Hans Verkuil | 1cccee0 | 2010-11-14 09:43:52 -0300 | [diff] [blame] | 565 | .unlocked_ioctl = video_ioctl2, |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame] | 566 | .poll = cadet_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | }; |
| 568 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 569 | static const struct v4l2_ioctl_ops cadet_ioctl_ops = { |
Douglas Landgraf | c1c4fd3 | 2007-05-07 16:17:16 -0300 | [diff] [blame] | 570 | .vidioc_querycap = vidioc_querycap, |
| 571 | .vidioc_g_tuner = vidioc_g_tuner, |
| 572 | .vidioc_s_tuner = vidioc_s_tuner, |
| 573 | .vidioc_g_frequency = vidioc_g_frequency, |
| 574 | .vidioc_s_frequency = vidioc_s_frequency, |
| 575 | .vidioc_queryctrl = vidioc_queryctrl, |
| 576 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 577 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 578 | .vidioc_g_audio = vidioc_g_audio, |
| 579 | .vidioc_s_audio = vidioc_s_audio, |
| 580 | .vidioc_g_input = vidioc_g_input, |
| 581 | .vidioc_s_input = vidioc_s_input, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | }; |
| 583 | |
Bjorn Helgaas | 044dfc9 | 2008-03-31 21:21:48 -0300 | [diff] [blame] | 584 | #ifdef CONFIG_PNP |
| 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | static struct pnp_device_id cadet_pnp_devices[] = { |
| 587 | /* ADS Cadet AM/FM Radio Card */ |
| 588 | {.id = "MSM0c24", .driver_data = 0}, |
| 589 | {.id = ""} |
| 590 | }; |
| 591 | |
| 592 | MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices); |
| 593 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 594 | 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] | 595 | { |
| 596 | if (!dev) |
| 597 | return -ENODEV; |
| 598 | /* only support one device */ |
| 599 | if (io > 0) |
| 600 | return -EBUSY; |
| 601 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 602 | if (!pnp_port_valid(dev, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | io = pnp_port_start(dev, 0); |
| 606 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 607 | printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
| 609 | return io; |
| 610 | } |
| 611 | |
| 612 | static struct pnp_driver cadet_pnp_driver = { |
| 613 | .name = "radio-cadet", |
| 614 | .id_table = cadet_pnp_devices, |
| 615 | .probe = cadet_pnp_probe, |
| 616 | .remove = NULL, |
| 617 | }; |
| 618 | |
Bjorn Helgaas | 044dfc9 | 2008-03-31 21:21:48 -0300 | [diff] [blame] | 619 | #else |
| 620 | static struct pnp_driver cadet_pnp_driver; |
| 621 | #endif |
| 622 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 623 | static void cadet_probe(struct cadet *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 625 | static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | int i; |
| 627 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 628 | for (i = 0; i < 8; i++) { |
| 629 | dev->io = iovals[i]; |
| 630 | if (request_region(dev->io, 2, "cadet-probe")) { |
| 631 | cadet_setfreq(dev, 1410); |
| 632 | if (cadet_getfreq(dev) == 1410) { |
| 633 | release_region(dev->io, 2); |
| 634 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 636 | release_region(dev->io, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 639 | dev->io = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 642 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | * io should only be set if the user has used something like |
| 644 | * isapnp (the userspace program) to initialize this card for us |
| 645 | */ |
| 646 | |
| 647 | static int __init cadet_init(void) |
| 648 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 649 | struct cadet *dev = &cadet_card; |
| 650 | struct v4l2_device *v4l2_dev = &dev->v4l2_dev; |
| 651 | int res; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 652 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 653 | strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name)); |
| 654 | mutex_init(&dev->lock); |
| 655 | |
| 656 | /* If a probe was requested then probe ISAPnP first (safest) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | if (io < 0) |
| 658 | pnp_register_driver(&cadet_pnp_driver); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 659 | dev->io = io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 661 | /* If that fails then probe unsafely if probe is requested */ |
| 662 | if (dev->io < 0) |
| 663 | cadet_probe(dev); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 664 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 665 | /* Else we bail out */ |
| 666 | if (dev->io < 0) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 667 | #ifdef MODULE |
Hans Verkuil | b24c20cc | 2009-03-09 08:11:21 -0300 | [diff] [blame] | 668 | v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n"); |
| 669 | v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | #endif |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 671 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 673 | if (!request_region(dev->io, 2, "cadet")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | goto fail; |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 675 | |
| 676 | res = v4l2_device_register(NULL, v4l2_dev); |
| 677 | if (res < 0) { |
| 678 | release_region(dev->io, 2); |
| 679 | v4l2_err(v4l2_dev, "could not register v4l2_device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | goto fail; |
| 681 | } |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 682 | |
| 683 | strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); |
| 684 | dev->vdev.v4l2_dev = v4l2_dev; |
| 685 | dev->vdev.fops = &cadet_fops; |
| 686 | dev->vdev.ioctl_ops = &cadet_ioctl_ops; |
| 687 | dev->vdev.release = video_device_release_empty; |
| 688 | video_set_drvdata(&dev->vdev, dev); |
| 689 | |
| 690 | if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) { |
| 691 | v4l2_device_unregister(v4l2_dev); |
| 692 | release_region(dev->io, 2); |
| 693 | goto fail; |
| 694 | } |
| 695 | 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] | 696 | return 0; |
| 697 | fail: |
| 698 | pnp_unregister_driver(&cadet_pnp_driver); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 699 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 702 | static void __exit cadet_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 704 | struct cadet *dev = &cadet_card; |
| 705 | |
| 706 | video_unregister_device(&dev->vdev); |
| 707 | v4l2_device_unregister(&dev->v4l2_dev); |
| 708 | release_region(dev->io, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | pnp_unregister_driver(&cadet_pnp_driver); |
| 710 | } |
| 711 | |
| 712 | module_init(cadet_init); |
Hans Verkuil | bec2aec | 2009-03-06 13:48:47 -0300 | [diff] [blame] | 713 | module_exit(cadet_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |