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