Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
4286c6f6 | 11 | * Jason Lewis (jlewis@twilight.vtc.vsc.edu) |
1da177e4 LT |
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 | * | |
d9b01449 | 26 | * 2003-01-31 Alan Cox <alan@lxorguk.ukuu.org.uk> |
1da177e4 | 27 | * Cleaned up locking, delay code, general odds and ends |
c0c7fa09 HK |
28 | * |
29 | * 2006-07-30 Hans J. Koch <koch@hjk-az.de> | |
30 | * Changed API to V4L2 | |
1da177e4 LT |
31 | */ |
32 | ||
33 | #include <linux/module.h> /* Modules */ | |
34 | #include <linux/init.h> /* Initdata */ | |
fb911ee8 | 35 | #include <linux/ioport.h> /* request_region */ |
1da177e4 | 36 | #include <linux/delay.h> /* udelay */ |
c0c7fa09 | 37 | #include <linux/videodev2.h> /* V4L2 API defs */ |
1da177e4 LT |
38 | #include <linux/param.h> |
39 | #include <linux/pnp.h> | |
a99bbaf5 | 40 | #include <linux/sched.h> |
bec2aec5 | 41 | #include <linux/io.h> /* outb, outb_p */ |
bec2aec5 HV |
42 | #include <media/v4l2-device.h> |
43 | #include <media/v4l2-ioctl.h> | |
b54c97db HV |
44 | #include <media/v4l2-ctrls.h> |
45 | #include <media/v4l2-fh.h> | |
46 | #include <media/v4l2-event.h> | |
bec2aec5 HV |
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"); | |
29834c1a | 51 | MODULE_VERSION("0.3.4"); |
bec2aec5 HV |
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 | ||
1da177e4 | 60 | #define RDS_BUFFER 256 |
c0c7fa09 HK |
61 | #define RDS_RX_FLAG 1 |
62 | #define MBS_RX_FLAG 2 | |
63 | ||
bec2aec5 HV |
64 | struct cadet { |
65 | struct v4l2_device v4l2_dev; | |
66 | struct video_device vdev; | |
b54c97db | 67 | struct v4l2_ctrl_handler ctrl_handler; |
bec2aec5 | 68 | int io; |
bec2aec5 HV |
69 | int curtuner; |
70 | int tunestat; | |
71 | int sigstrength; | |
72 | wait_queue_head_t read_queue; | |
73 | struct timer_list readtimer; | |
cc0d3266 | 74 | u8 rdsin, rdsout, rdsstat; |
bec2aec5 HV |
75 | unsigned char rdsbuf[RDS_BUFFER]; |
76 | struct mutex lock; | |
77 | int reading; | |
c1c4fd3e DL |
78 | }; |
79 | ||
bec2aec5 | 80 | static struct cadet cadet_card; |
1da177e4 LT |
81 | |
82 | /* | |
83 | * Signal Strength Threshold Values | |
4286c6f6 | 84 | * The V4L API spec does not define any particular unit for the signal |
1da177e4 LT |
85 | * strength value. These values are in microvolts of RF at the tuner's input. |
86 | */ | |
bec2aec5 | 87 | static __u16 sigtable[2][4] = { |
cc0d3266 HV |
88 | { 2185, 4369, 13107, 65535 }, |
89 | { 1835, 2621, 4128, 65535 } | |
bec2aec5 | 90 | }; |
1da177e4 | 91 | |
1da177e4 | 92 | |
bec2aec5 | 93 | static int cadet_getstereo(struct cadet *dev) |
1da177e4 | 94 | { |
c0c7fa09 | 95 | int ret = V4L2_TUNER_SUB_MONO; |
bec2aec5 HV |
96 | |
97 | if (dev->curtuner != 0) /* Only FM has stereo capability! */ | |
c0c7fa09 | 98 | return V4L2_TUNER_SUB_MONO; |
1da177e4 | 99 | |
bec2aec5 HV |
100 | outb(7, dev->io); /* Select tuner control */ |
101 | if ((inb(dev->io + 1) & 0x40) == 0) | |
c0c7fa09 | 102 | ret = V4L2_TUNER_SUB_STEREO; |
4286c6f6 | 103 | return ret; |
1da177e4 LT |
104 | } |
105 | ||
bec2aec5 | 106 | static unsigned cadet_gettune(struct cadet *dev) |
1da177e4 | 107 | { |
bec2aec5 HV |
108 | int curvol, i; |
109 | unsigned fifo = 0; | |
1da177e4 | 110 | |
4286c6f6 MCC |
111 | /* |
112 | * Prepare for read | |
113 | */ | |
1da177e4 | 114 | |
bec2aec5 HV |
115 | outb(7, dev->io); /* Select tuner control */ |
116 | curvol = inb(dev->io + 1); /* Save current volume/mute setting */ | |
117 | outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */ | |
118 | dev->tunestat = 0xffff; | |
1da177e4 | 119 | |
4286c6f6 MCC |
120 | /* |
121 | * Read the shift register | |
122 | */ | |
bec2aec5 HV |
123 | for (i = 0; i < 25; i++) { |
124 | fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01); | |
125 | if (i < 24) { | |
126 | outb(0x01, dev->io + 1); | |
127 | dev->tunestat &= inb(dev->io + 1); | |
128 | outb(0x00, dev->io + 1); | |
4286c6f6 MCC |
129 | } |
130 | } | |
131 | ||
132 | /* | |
133 | * Restore volume/mute setting | |
134 | */ | |
bec2aec5 | 135 | outb(curvol, dev->io + 1); |
1da177e4 LT |
136 | return fifo; |
137 | } | |
138 | ||
bec2aec5 | 139 | static unsigned cadet_getfreq(struct cadet *dev) |
1da177e4 | 140 | { |
4286c6f6 | 141 | int i; |
bec2aec5 | 142 | unsigned freq = 0, test, fifo = 0; |
1da177e4 LT |
143 | |
144 | /* | |
145 | * Read current tuning | |
146 | */ | |
bec2aec5 | 147 | fifo = cadet_gettune(dev); |
1da177e4 | 148 | |
4286c6f6 MCC |
149 | /* |
150 | * Convert to actual frequency | |
151 | */ | |
bec2aec5 HV |
152 | if (dev->curtuner == 0) { /* FM */ |
153 | test = 12500; | |
154 | for (i = 0; i < 14; i++) { | |
155 | if ((fifo & 0x01) != 0) | |
156 | freq += test; | |
157 | test = test << 1; | |
158 | fifo = fifo >> 1; | |
4286c6f6 | 159 | } |
bec2aec5 | 160 | freq -= 10700000; /* IF frequency is 10.7 MHz */ |
b54c97db | 161 | freq = (freq * 16) / 1000; /* Make it 1/16 kHz */ |
1da177e4 | 162 | } |
bec2aec5 HV |
163 | if (dev->curtuner == 1) /* AM */ |
164 | freq = ((fifo & 0x7fff) - 2010) * 16; | |
1da177e4 | 165 | |
4286c6f6 | 166 | return freq; |
1da177e4 LT |
167 | } |
168 | ||
bec2aec5 | 169 | static void cadet_settune(struct cadet *dev, unsigned fifo) |
1da177e4 | 170 | { |
4286c6f6 MCC |
171 | int i; |
172 | unsigned test; | |
1da177e4 | 173 | |
bec2aec5 | 174 | outb(7, dev->io); /* Select tuner control */ |
1da177e4 LT |
175 | /* |
176 | * Write the shift register | |
177 | */ | |
bec2aec5 HV |
178 | test = 0; |
179 | test = (fifo >> 23) & 0x02; /* Align data for SDO */ | |
180 | test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */ | |
181 | outb(7, dev->io); /* Select tuner control */ | |
182 | outb(test, dev->io + 1); /* Initialize for write */ | |
183 | for (i = 0; i < 25; i++) { | |
184 | test |= 0x01; /* Toggle SCK High */ | |
185 | outb(test, dev->io + 1); | |
186 | test &= 0xfe; /* Toggle SCK Low */ | |
187 | outb(test, dev->io + 1); | |
188 | fifo = fifo << 1; /* Prepare the next bit */ | |
189 | test = 0x1c | ((fifo >> 23) & 0x02); | |
190 | outb(test, dev->io + 1); | |
1da177e4 | 191 | } |
1da177e4 LT |
192 | } |
193 | ||
bec2aec5 | 194 | static void cadet_setfreq(struct cadet *dev, unsigned freq) |
1da177e4 | 195 | { |
4286c6f6 | 196 | unsigned fifo; |
bec2aec5 | 197 | int i, j, test; |
4286c6f6 | 198 | int curvol; |
1da177e4 | 199 | |
4286c6f6 MCC |
200 | /* |
201 | * Formulate a fifo command | |
202 | */ | |
bec2aec5 HV |
203 | fifo = 0; |
204 | if (dev->curtuner == 0) { /* FM */ | |
205 | test = 102400; | |
b54c97db | 206 | freq = freq / 16; /* Make it kHz */ |
bec2aec5 HV |
207 | freq += 10700; /* IF is 10700 kHz */ |
208 | for (i = 0; i < 14; i++) { | |
209 | fifo = fifo << 1; | |
210 | if (freq >= test) { | |
211 | fifo |= 0x01; | |
212 | freq -= test; | |
4286c6f6 | 213 | } |
bec2aec5 | 214 | test = test >> 1; |
4286c6f6 | 215 | } |
1da177e4 | 216 | } |
bec2aec5 HV |
217 | if (dev->curtuner == 1) { /* AM */ |
218 | fifo = (freq / 16) + 2010; /* Make it kHz */ | |
219 | fifo |= 0x100000; /* Select AM Band */ | |
1da177e4 LT |
220 | } |
221 | ||
4286c6f6 MCC |
222 | /* |
223 | * Save current volume/mute setting | |
224 | */ | |
1da177e4 | 225 | |
bec2aec5 HV |
226 | outb(7, dev->io); /* Select tuner control */ |
227 | curvol = inb(dev->io + 1); | |
1da177e4 LT |
228 | |
229 | /* | |
230 | * Tune the card | |
231 | */ | |
bec2aec5 HV |
232 | for (j = 3; j > -1; j--) { |
233 | cadet_settune(dev, fifo | (j << 16)); | |
4286c6f6 | 234 | |
bec2aec5 HV |
235 | outb(7, dev->io); /* Select tuner control */ |
236 | outb(curvol, dev->io + 1); | |
4286c6f6 | 237 | |
1da177e4 LT |
238 | msleep(100); |
239 | ||
bec2aec5 HV |
240 | cadet_gettune(dev); |
241 | if ((dev->tunestat & 0x40) == 0) { /* Tuned */ | |
242 | dev->sigstrength = sigtable[dev->curtuner][j]; | |
cc0d3266 | 243 | goto reset_rds; |
1da177e4 LT |
244 | } |
245 | } | |
bec2aec5 | 246 | dev->sigstrength = 0; |
cc0d3266 HV |
247 | reset_rds: |
248 | outb(3, dev->io); | |
249 | outb(inb(dev->io + 1) & 0x7f, dev->io + 1); | |
1da177e4 LT |
250 | } |
251 | ||
252 | ||
bec2aec5 | 253 | static void cadet_handler(unsigned long data) |
1da177e4 | 254 | { |
bec2aec5 | 255 | struct cadet *dev = (void *)data; |
1da177e4 | 256 | |
bec2aec5 HV |
257 | /* Service the RDS fifo */ |
258 | if (mutex_trylock(&dev->lock)) { | |
259 | outb(0x3, dev->io); /* Select RDS Decoder Control */ | |
260 | if ((inb(dev->io + 1) & 0x20) != 0) | |
4286c6f6 | 261 | printk(KERN_CRIT "cadet: RDS fifo overflow\n"); |
bec2aec5 HV |
262 | outb(0x80, dev->io); /* Select RDS fifo */ |
263 | while ((inb(dev->io) & 0x80) != 0) { | |
264 | dev->rdsbuf[dev->rdsin] = inb(dev->io + 1); | |
cc0d3266 | 265 | if (dev->rdsin + 1 == dev->rdsout) |
4286c6f6 | 266 | printk(KERN_WARNING "cadet: RDS buffer overflow\n"); |
1da177e4 | 267 | else |
bec2aec5 | 268 | dev->rdsin++; |
1da177e4 | 269 | } |
bec2aec5 | 270 | mutex_unlock(&dev->lock); |
1da177e4 LT |
271 | } |
272 | ||
273 | /* | |
274 | * Service pending read | |
275 | */ | |
bec2aec5 HV |
276 | if (dev->rdsin != dev->rdsout) |
277 | wake_up_interruptible(&dev->read_queue); | |
1da177e4 | 278 | |
4286c6f6 | 279 | /* |
1da177e4 LT |
280 | * Clean up and exit |
281 | */ | |
bec2aec5 HV |
282 | init_timer(&dev->readtimer); |
283 | dev->readtimer.function = cadet_handler; | |
cc0d3266 | 284 | dev->readtimer.data = data; |
bec2aec5 HV |
285 | dev->readtimer.expires = jiffies + msecs_to_jiffies(50); |
286 | add_timer(&dev->readtimer); | |
1da177e4 LT |
287 | } |
288 | ||
cc0d3266 HV |
289 | static void cadet_start_rds(struct cadet *dev) |
290 | { | |
291 | dev->rdsstat = 1; | |
292 | outb(0x80, dev->io); /* Select RDS fifo */ | |
293 | init_timer(&dev->readtimer); | |
294 | dev->readtimer.function = cadet_handler; | |
295 | dev->readtimer.data = (unsigned long)dev; | |
296 | dev->readtimer.expires = jiffies + msecs_to_jiffies(50); | |
297 | add_timer(&dev->readtimer); | |
298 | } | |
1da177e4 | 299 | |
bec2aec5 | 300 | static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos) |
1da177e4 | 301 | { |
bec2aec5 | 302 | struct cadet *dev = video_drvdata(file); |
1da177e4 | 303 | unsigned char readbuf[RDS_BUFFER]; |
bec2aec5 HV |
304 | int i = 0; |
305 | ||
1cccee0b | 306 | mutex_lock(&dev->lock); |
cc0d3266 HV |
307 | if (dev->rdsstat == 0) |
308 | cadet_start_rds(dev); | |
bec2aec5 | 309 | if (dev->rdsin == dev->rdsout) { |
b54c97db HV |
310 | if (file->f_flags & O_NONBLOCK) { |
311 | i = -EWOULDBLOCK; | |
312 | goto unlock; | |
313 | } | |
cc0d3266 | 314 | mutex_unlock(&dev->lock); |
bec2aec5 | 315 | interruptible_sleep_on(&dev->read_queue); |
cc0d3266 | 316 | mutex_lock(&dev->lock); |
4286c6f6 | 317 | } |
bec2aec5 HV |
318 | while (i < count && dev->rdsin != dev->rdsout) |
319 | readbuf[i++] = dev->rdsbuf[dev->rdsout++]; | |
1da177e4 | 320 | |
cc0d3266 | 321 | if (i && copy_to_user(data, readbuf, i)) |
b54c97db HV |
322 | i = -EFAULT; |
323 | unlock: | |
324 | mutex_unlock(&dev->lock); | |
1da177e4 LT |
325 | return i; |
326 | } | |
327 | ||
328 | ||
c1c4fd3e DL |
329 | static int vidioc_querycap(struct file *file, void *priv, |
330 | struct v4l2_capability *v) | |
331 | { | |
bec2aec5 HV |
332 | strlcpy(v->driver, "ADS Cadet", sizeof(v->driver)); |
333 | strlcpy(v->card, "ADS Cadet", sizeof(v->card)); | |
334 | strlcpy(v->bus_info, "ISA", sizeof(v->bus_info)); | |
b54c97db | 335 | v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO | |
8e280f24 | 336 | V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE; |
b54c97db | 337 | v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; |
c1c4fd3e DL |
338 | return 0; |
339 | } | |
1da177e4 | 340 | |
c1c4fd3e DL |
341 | static int vidioc_g_tuner(struct file *file, void *priv, |
342 | struct v4l2_tuner *v) | |
1da177e4 | 343 | { |
bec2aec5 HV |
344 | struct cadet *dev = video_drvdata(file); |
345 | ||
c1c4fd3e DL |
346 | v->type = V4L2_TUNER_RADIO; |
347 | switch (v->index) { | |
348 | case 0: | |
bec2aec5 | 349 | strlcpy(v->name, "FM", sizeof(v->name)); |
cb0ed222 | 350 | v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS | |
b54c97db HV |
351 | V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW; |
352 | v->rangelow = 1400000; /* 87.5 MHz */ | |
353 | v->rangehigh = 1728000; /* 108.0 MHz */ | |
bec2aec5 | 354 | v->rxsubchans = cadet_getstereo(dev); |
b54c97db | 355 | v->audmode = V4L2_TUNER_MODE_STEREO; |
cc0d3266 HV |
356 | outb(3, dev->io); |
357 | outb(inb(dev->io + 1) & 0x7f, dev->io + 1); | |
358 | mdelay(100); | |
359 | outb(3, dev->io); | |
360 | if (inb(dev->io + 1) & 0x80) | |
361 | v->rxsubchans |= V4L2_TUNER_SUB_RDS; | |
c1c4fd3e DL |
362 | break; |
363 | case 1: | |
bec2aec5 | 364 | strlcpy(v->name, "AM", sizeof(v->name)); |
c1c4fd3e DL |
365 | v->capability = V4L2_TUNER_CAP_LOW; |
366 | v->rangelow = 8320; /* 520 kHz */ | |
367 | v->rangehigh = 26400; /* 1650 kHz */ | |
368 | v->rxsubchans = V4L2_TUNER_SUB_MONO; | |
369 | v->audmode = V4L2_TUNER_MODE_MONO; | |
370 | break; | |
371 | default: | |
372 | return -EINVAL; | |
373 | } | |
bec2aec5 | 374 | v->signal = dev->sigstrength; /* We might need to modify scaling of this */ |
c1c4fd3e DL |
375 | return 0; |
376 | } | |
c0c7fa09 | 377 | |
c1c4fd3e DL |
378 | static int vidioc_s_tuner(struct file *file, void *priv, |
379 | struct v4l2_tuner *v) | |
380 | { | |
bec2aec5 | 381 | if (v->index != 0 && v->index != 1) |
c1c4fd3e | 382 | return -EINVAL; |
c1c4fd3e DL |
383 | return 0; |
384 | } | |
c0c7fa09 | 385 | |
c1c4fd3e DL |
386 | static int vidioc_g_frequency(struct file *file, void *priv, |
387 | struct v4l2_frequency *f) | |
388 | { | |
bec2aec5 HV |
389 | struct cadet *dev = video_drvdata(file); |
390 | ||
b54c97db HV |
391 | if (f->tuner > 1) |
392 | return -EINVAL; | |
c1c4fd3e | 393 | f->type = V4L2_TUNER_RADIO; |
bec2aec5 | 394 | f->frequency = cadet_getfreq(dev); |
c1c4fd3e DL |
395 | return 0; |
396 | } | |
397 | ||
398 | ||
399 | static int vidioc_s_frequency(struct file *file, void *priv, | |
400 | struct v4l2_frequency *f) | |
401 | { | |
bec2aec5 HV |
402 | struct cadet *dev = video_drvdata(file); |
403 | ||
c1c4fd3e DL |
404 | if (f->type != V4L2_TUNER_RADIO) |
405 | return -EINVAL; | |
b54c97db HV |
406 | if (f->tuner == 0) { |
407 | if (f->frequency < 1400000) | |
408 | f->frequency = 1400000; | |
409 | else if (f->frequency > 1728000) | |
410 | f->frequency = 1728000; | |
411 | } else if (f->tuner == 1) { | |
412 | if (f->frequency < 8320) | |
413 | f->frequency = 8320; | |
414 | else if (f->frequency > 26400) | |
415 | f->frequency = 26400; | |
416 | } else | |
c1c4fd3e | 417 | return -EINVAL; |
bec2aec5 | 418 | cadet_setfreq(dev, f->frequency); |
c1c4fd3e DL |
419 | return 0; |
420 | } | |
421 | ||
b54c97db | 422 | static int cadet_s_ctrl(struct v4l2_ctrl *ctrl) |
c1c4fd3e | 423 | { |
b54c97db | 424 | struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler); |
bec2aec5 HV |
425 | |
426 | switch (ctrl->id) { | |
b54c97db HV |
427 | case V4L2_CID_AUDIO_MUTE: |
428 | outb(7, dev->io); /* Select tuner control */ | |
429 | if (ctrl->val) | |
430 | outb(0x00, dev->io + 1); | |
c1c4fd3e | 431 | else |
b54c97db HV |
432 | outb(0x20, dev->io + 1); |
433 | return 0; | |
c1c4fd3e | 434 | } |
b54c97db | 435 | return -EINVAL; |
1da177e4 LT |
436 | } |
437 | ||
bec2aec5 | 438 | static int cadet_open(struct file *file) |
1da177e4 | 439 | { |
bec2aec5 | 440 | struct cadet *dev = video_drvdata(file); |
b54c97db | 441 | int err; |
bec2aec5 | 442 | |
1cccee0b | 443 | mutex_lock(&dev->lock); |
b54c97db HV |
444 | err = v4l2_fh_open(file); |
445 | if (err) | |
446 | goto fail; | |
447 | if (v4l2_fh_is_singular_file(file)) | |
bec2aec5 | 448 | init_waitqueue_head(&dev->read_queue); |
b54c97db | 449 | fail: |
1cccee0b | 450 | mutex_unlock(&dev->lock); |
b54c97db | 451 | return err; |
1da177e4 LT |
452 | } |
453 | ||
bec2aec5 | 454 | static int cadet_release(struct file *file) |
1da177e4 | 455 | { |
bec2aec5 HV |
456 | struct cadet *dev = video_drvdata(file); |
457 | ||
1cccee0b | 458 | mutex_lock(&dev->lock); |
b54c97db | 459 | if (v4l2_fh_is_singular_file(file) && dev->rdsstat) { |
bec2aec5 HV |
460 | del_timer_sync(&dev->readtimer); |
461 | dev->rdsstat = 0; | |
c0c7fa09 | 462 | } |
b54c97db | 463 | v4l2_fh_release(file); |
1cccee0b | 464 | mutex_unlock(&dev->lock); |
c0c7fa09 HK |
465 | return 0; |
466 | } | |
467 | ||
bec2aec5 | 468 | static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait) |
c0c7fa09 | 469 | { |
bec2aec5 | 470 | struct cadet *dev = video_drvdata(file); |
cc0d3266 | 471 | unsigned long req_events = poll_requested_events(wait); |
b54c97db | 472 | unsigned int res = v4l2_ctrl_poll(file, wait); |
bec2aec5 HV |
473 | |
474 | poll_wait(file, &dev->read_queue, wait); | |
cc0d3266 HV |
475 | if (dev->rdsstat == 0 && (req_events & (POLLIN | POLLRDNORM))) { |
476 | mutex_lock(&dev->lock); | |
477 | if (dev->rdsstat == 0) | |
478 | cadet_start_rds(dev); | |
479 | mutex_unlock(&dev->lock); | |
480 | } | |
bec2aec5 | 481 | if (dev->rdsin != dev->rdsout) |
b54c97db HV |
482 | res |= POLLIN | POLLRDNORM; |
483 | return res; | |
1da177e4 LT |
484 | } |
485 | ||
486 | ||
bec43661 | 487 | static const struct v4l2_file_operations cadet_fops = { |
1da177e4 LT |
488 | .owner = THIS_MODULE, |
489 | .open = cadet_open, | |
490 | .release = cadet_release, | |
491 | .read = cadet_read, | |
1cccee0b | 492 | .unlocked_ioctl = video_ioctl2, |
c0c7fa09 | 493 | .poll = cadet_poll, |
1da177e4 LT |
494 | }; |
495 | ||
a399810c | 496 | static const struct v4l2_ioctl_ops cadet_ioctl_ops = { |
c1c4fd3e DL |
497 | .vidioc_querycap = vidioc_querycap, |
498 | .vidioc_g_tuner = vidioc_g_tuner, | |
499 | .vidioc_s_tuner = vidioc_s_tuner, | |
500 | .vidioc_g_frequency = vidioc_g_frequency, | |
501 | .vidioc_s_frequency = vidioc_s_frequency, | |
b54c97db HV |
502 | .vidioc_log_status = v4l2_ctrl_log_status, |
503 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
504 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
505 | }; | |
506 | ||
507 | static const struct v4l2_ctrl_ops cadet_ctrl_ops = { | |
508 | .s_ctrl = cadet_s_ctrl, | |
1da177e4 LT |
509 | }; |
510 | ||
044dfc99 BH |
511 | #ifdef CONFIG_PNP |
512 | ||
1da177e4 LT |
513 | static struct pnp_device_id cadet_pnp_devices[] = { |
514 | /* ADS Cadet AM/FM Radio Card */ | |
515 | {.id = "MSM0c24", .driver_data = 0}, | |
516 | {.id = ""} | |
517 | }; | |
518 | ||
519 | MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices); | |
520 | ||
bec2aec5 | 521 | static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id) |
1da177e4 LT |
522 | { |
523 | if (!dev) | |
524 | return -ENODEV; | |
525 | /* only support one device */ | |
526 | if (io > 0) | |
527 | return -EBUSY; | |
528 | ||
bec2aec5 | 529 | if (!pnp_port_valid(dev, 0)) |
1da177e4 | 530 | return -ENODEV; |
1da177e4 LT |
531 | |
532 | io = pnp_port_start(dev, 0); | |
533 | ||
bec2aec5 | 534 | printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io); |
1da177e4 LT |
535 | |
536 | return io; | |
537 | } | |
538 | ||
539 | static struct pnp_driver cadet_pnp_driver = { | |
540 | .name = "radio-cadet", | |
541 | .id_table = cadet_pnp_devices, | |
542 | .probe = cadet_pnp_probe, | |
543 | .remove = NULL, | |
544 | }; | |
545 | ||
044dfc99 BH |
546 | #else |
547 | static struct pnp_driver cadet_pnp_driver; | |
548 | #endif | |
549 | ||
bec2aec5 | 550 | static void cadet_probe(struct cadet *dev) |
1da177e4 | 551 | { |
bec2aec5 | 552 | static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e }; |
1da177e4 LT |
553 | int i; |
554 | ||
bec2aec5 HV |
555 | for (i = 0; i < 8; i++) { |
556 | dev->io = iovals[i]; | |
557 | if (request_region(dev->io, 2, "cadet-probe")) { | |
558 | cadet_setfreq(dev, 1410); | |
559 | if (cadet_getfreq(dev) == 1410) { | |
560 | release_region(dev->io, 2); | |
561 | return; | |
1da177e4 | 562 | } |
bec2aec5 | 563 | release_region(dev->io, 2); |
1da177e4 LT |
564 | } |
565 | } | |
bec2aec5 | 566 | dev->io = -1; |
1da177e4 LT |
567 | } |
568 | ||
4286c6f6 | 569 | /* |
1da177e4 LT |
570 | * io should only be set if the user has used something like |
571 | * isapnp (the userspace program) to initialize this card for us | |
572 | */ | |
573 | ||
574 | static int __init cadet_init(void) | |
575 | { | |
bec2aec5 HV |
576 | struct cadet *dev = &cadet_card; |
577 | struct v4l2_device *v4l2_dev = &dev->v4l2_dev; | |
b54c97db HV |
578 | struct v4l2_ctrl_handler *hdl; |
579 | int res = -ENODEV; | |
4286c6f6 | 580 | |
bec2aec5 HV |
581 | strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name)); |
582 | mutex_init(&dev->lock); | |
583 | ||
584 | /* If a probe was requested then probe ISAPnP first (safest) */ | |
1da177e4 LT |
585 | if (io < 0) |
586 | pnp_register_driver(&cadet_pnp_driver); | |
bec2aec5 | 587 | dev->io = io; |
1da177e4 | 588 | |
bec2aec5 HV |
589 | /* If that fails then probe unsafely if probe is requested */ |
590 | if (dev->io < 0) | |
591 | cadet_probe(dev); | |
4286c6f6 | 592 | |
bec2aec5 HV |
593 | /* Else we bail out */ |
594 | if (dev->io < 0) { | |
4286c6f6 | 595 | #ifdef MODULE |
b24c20cc HV |
596 | v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n"); |
597 | v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n"); | |
1da177e4 | 598 | #endif |
4286c6f6 | 599 | goto fail; |
1da177e4 | 600 | } |
bec2aec5 HV |
601 | if (!request_region(dev->io, 2, "cadet")) |
602 | goto fail; | |
603 | ||
604 | res = v4l2_device_register(NULL, v4l2_dev); | |
605 | if (res < 0) { | |
606 | release_region(dev->io, 2); | |
607 | v4l2_err(v4l2_dev, "could not register v4l2_device\n"); | |
1da177e4 | 608 | goto fail; |
bec2aec5 HV |
609 | } |
610 | ||
b54c97db HV |
611 | hdl = &dev->ctrl_handler; |
612 | v4l2_ctrl_handler_init(hdl, 2); | |
613 | v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops, | |
614 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); | |
615 | v4l2_dev->ctrl_handler = hdl; | |
616 | if (hdl->error) { | |
617 | res = hdl->error; | |
618 | v4l2_err(v4l2_dev, "Could not register controls\n"); | |
619 | goto err_hdl; | |
620 | } | |
621 | ||
bec2aec5 HV |
622 | strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); |
623 | dev->vdev.v4l2_dev = v4l2_dev; | |
624 | dev->vdev.fops = &cadet_fops; | |
625 | dev->vdev.ioctl_ops = &cadet_ioctl_ops; | |
626 | dev->vdev.release = video_device_release_empty; | |
b54c97db HV |
627 | dev->vdev.lock = &dev->lock; |
628 | set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags); | |
bec2aec5 HV |
629 | video_set_drvdata(&dev->vdev, dev); |
630 | ||
b54c97db HV |
631 | if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) |
632 | goto err_hdl; | |
bec2aec5 | 633 | v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io); |
1da177e4 | 634 | return 0; |
b54c97db HV |
635 | err_hdl: |
636 | v4l2_ctrl_handler_free(hdl); | |
637 | v4l2_device_unregister(v4l2_dev); | |
638 | release_region(dev->io, 2); | |
1da177e4 LT |
639 | fail: |
640 | pnp_unregister_driver(&cadet_pnp_driver); | |
b54c97db | 641 | return res; |
1da177e4 LT |
642 | } |
643 | ||
bec2aec5 | 644 | static void __exit cadet_exit(void) |
1da177e4 | 645 | { |
bec2aec5 HV |
646 | struct cadet *dev = &cadet_card; |
647 | ||
648 | video_unregister_device(&dev->vdev); | |
b54c97db | 649 | v4l2_ctrl_handler_free(&dev->ctrl_handler); |
bec2aec5 | 650 | v4l2_device_unregister(&dev->v4l2_dev); |
cc0d3266 HV |
651 | outb(7, dev->io); /* Mute */ |
652 | outb(0x00, dev->io + 1); | |
bec2aec5 | 653 | release_region(dev->io, 2); |
1da177e4 LT |
654 | pnp_unregister_driver(&cadet_pnp_driver); |
655 | } | |
656 | ||
657 | module_init(cadet_init); | |
bec2aec5 | 658 | module_exit(cadet_exit); |
1da177e4 | 659 |