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