]>
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 | ||
d591b9cc | 33 | #include <linux/version.h> |
1da177e4 LT |
34 | #include <linux/module.h> /* Modules */ |
35 | #include <linux/init.h> /* Initdata */ | |
fb911ee8 | 36 | #include <linux/ioport.h> /* request_region */ |
1da177e4 | 37 | #include <linux/delay.h> /* udelay */ |
c0c7fa09 | 38 | #include <linux/videodev2.h> /* V4L2 API defs */ |
1da177e4 LT |
39 | #include <linux/param.h> |
40 | #include <linux/pnp.h> | |
a99bbaf5 | 41 | #include <linux/sched.h> |
bec2aec5 | 42 | #include <linux/io.h> /* outb, outb_p */ |
bec2aec5 HV |
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) | |
1da177e4 LT |
58 | |
59 | #define RDS_BUFFER 256 | |
c0c7fa09 HK |
60 | #define RDS_RX_FLAG 1 |
61 | #define MBS_RX_FLAG 2 | |
62 | ||
bec2aec5 HV |
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; | |
c1c4fd3e DL |
77 | }; |
78 | ||
bec2aec5 | 79 | static struct cadet cadet_card; |
1da177e4 LT |
80 | |
81 | /* | |
82 | * Signal Strength Threshold Values | |
4286c6f6 | 83 | * The V4L API spec does not define any particular unit for the signal |
1da177e4 LT |
84 | * strength value. These values are in microvolts of RF at the tuner's input. |
85 | */ | |
bec2aec5 HV |
86 | static __u16 sigtable[2][4] = { |
87 | { 5, 10, 30, 150 }, | |
88 | { 28, 40, 63, 1000 } | |
89 | }; | |
1da177e4 | 90 | |
1da177e4 | 91 | |
bec2aec5 | 92 | static int cadet_getstereo(struct cadet *dev) |
1da177e4 | 93 | { |
c0c7fa09 | 94 | int ret = V4L2_TUNER_SUB_MONO; |
bec2aec5 HV |
95 | |
96 | if (dev->curtuner != 0) /* Only FM has stereo capability! */ | |
c0c7fa09 | 97 | return V4L2_TUNER_SUB_MONO; |
1da177e4 | 98 | |
bec2aec5 HV |
99 | mutex_lock(&dev->lock); |
100 | outb(7, dev->io); /* Select tuner control */ | |
101 | if ((inb(dev->io + 1) & 0x40) == 0) | |
c0c7fa09 | 102 | ret = V4L2_TUNER_SUB_STEREO; |
bec2aec5 | 103 | mutex_unlock(&dev->lock); |
4286c6f6 | 104 | return ret; |
1da177e4 LT |
105 | } |
106 | ||
bec2aec5 | 107 | static unsigned cadet_gettune(struct cadet *dev) |
1da177e4 | 108 | { |
bec2aec5 HV |
109 | int curvol, i; |
110 | unsigned fifo = 0; | |
1da177e4 | 111 | |
4286c6f6 MCC |
112 | /* |
113 | * Prepare for read | |
114 | */ | |
1da177e4 | 115 | |
bec2aec5 | 116 | mutex_lock(&dev->lock); |
4286c6f6 | 117 | |
bec2aec5 HV |
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; | |
1da177e4 | 122 | |
4286c6f6 MCC |
123 | /* |
124 | * Read the shift register | |
125 | */ | |
bec2aec5 HV |
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); | |
4286c6f6 MCC |
132 | } |
133 | } | |
134 | ||
135 | /* | |
136 | * Restore volume/mute setting | |
137 | */ | |
bec2aec5 HV |
138 | outb(curvol, dev->io + 1); |
139 | mutex_unlock(&dev->lock); | |
1da177e4 LT |
140 | |
141 | return fifo; | |
142 | } | |
143 | ||
bec2aec5 | 144 | static unsigned cadet_getfreq(struct cadet *dev) |
1da177e4 | 145 | { |
4286c6f6 | 146 | int i; |
bec2aec5 | 147 | unsigned freq = 0, test, fifo = 0; |
1da177e4 LT |
148 | |
149 | /* | |
150 | * Read current tuning | |
151 | */ | |
bec2aec5 | 152 | fifo = cadet_gettune(dev); |
1da177e4 | 153 | |
4286c6f6 MCC |
154 | /* |
155 | * Convert to actual frequency | |
156 | */ | |
bec2aec5 HV |
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; | |
4286c6f6 | 164 | } |
bec2aec5 HV |
165 | freq -= 10700000; /* IF frequency is 10.7 MHz */ |
166 | freq = (freq * 16) / 1000000; /* Make it 1/16 MHz */ | |
1da177e4 | 167 | } |
bec2aec5 HV |
168 | if (dev->curtuner == 1) /* AM */ |
169 | freq = ((fifo & 0x7fff) - 2010) * 16; | |
1da177e4 | 170 | |
4286c6f6 | 171 | return freq; |
1da177e4 LT |
172 | } |
173 | ||
bec2aec5 | 174 | static void cadet_settune(struct cadet *dev, unsigned fifo) |
1da177e4 | 175 | { |
4286c6f6 MCC |
176 | int i; |
177 | unsigned test; | |
1da177e4 | 178 | |
bec2aec5 | 179 | mutex_lock(&dev->lock); |
4286c6f6 | 180 | |
bec2aec5 | 181 | outb(7, dev->io); /* Select tuner control */ |
1da177e4 LT |
182 | /* |
183 | * Write the shift register | |
184 | */ | |
bec2aec5 HV |
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); | |
1da177e4 | 198 | } |
bec2aec5 | 199 | mutex_unlock(&dev->lock); |
1da177e4 LT |
200 | } |
201 | ||
bec2aec5 | 202 | static void cadet_setfreq(struct cadet *dev, unsigned freq) |
1da177e4 | 203 | { |
4286c6f6 | 204 | unsigned fifo; |
bec2aec5 | 205 | int i, j, test; |
4286c6f6 | 206 | int curvol; |
1da177e4 | 207 | |
4286c6f6 MCC |
208 | /* |
209 | * Formulate a fifo command | |
210 | */ | |
bec2aec5 HV |
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; | |
4286c6f6 | 221 | } |
bec2aec5 | 222 | test = test >> 1; |
4286c6f6 | 223 | } |
1da177e4 | 224 | } |
bec2aec5 HV |
225 | if (dev->curtuner == 1) { /* AM */ |
226 | fifo = (freq / 16) + 2010; /* Make it kHz */ | |
227 | fifo |= 0x100000; /* Select AM Band */ | |
1da177e4 LT |
228 | } |
229 | ||
4286c6f6 MCC |
230 | /* |
231 | * Save current volume/mute setting | |
232 | */ | |
1da177e4 | 233 | |
bec2aec5 HV |
234 | mutex_lock(&dev->lock); |
235 | outb(7, dev->io); /* Select tuner control */ | |
236 | curvol = inb(dev->io + 1); | |
237 | mutex_unlock(&dev->lock); | |
1da177e4 LT |
238 | |
239 | /* | |
240 | * Tune the card | |
241 | */ | |
bec2aec5 HV |
242 | for (j = 3; j > -1; j--) { |
243 | cadet_settune(dev, fifo | (j << 16)); | |
4286c6f6 | 244 | |
bec2aec5 HV |
245 | mutex_lock(&dev->lock); |
246 | outb(7, dev->io); /* Select tuner control */ | |
247 | outb(curvol, dev->io + 1); | |
248 | mutex_unlock(&dev->lock); | |
4286c6f6 | 249 | |
1da177e4 LT |
250 | msleep(100); |
251 | ||
bec2aec5 HV |
252 | cadet_gettune(dev); |
253 | if ((dev->tunestat & 0x40) == 0) { /* Tuned */ | |
254 | dev->sigstrength = sigtable[dev->curtuner][j]; | |
1da177e4 LT |
255 | return; |
256 | } | |
257 | } | |
bec2aec5 | 258 | dev->sigstrength = 0; |
1da177e4 LT |
259 | } |
260 | ||
261 | ||
bec2aec5 | 262 | static int cadet_getvol(struct cadet *dev) |
1da177e4 LT |
263 | { |
264 | int ret = 0; | |
4286c6f6 | 265 | |
bec2aec5 | 266 | mutex_lock(&dev->lock); |
4286c6f6 | 267 | |
bec2aec5 HV |
268 | outb(7, dev->io); /* Select tuner control */ |
269 | if ((inb(dev->io + 1) & 0x20) != 0) | |
4286c6f6 MCC |
270 | ret = 0xffff; |
271 | ||
bec2aec5 | 272 | mutex_unlock(&dev->lock); |
4286c6f6 | 273 | return ret; |
1da177e4 LT |
274 | } |
275 | ||
276 | ||
bec2aec5 | 277 | static void cadet_setvol(struct cadet *dev, int vol) |
1da177e4 | 278 | { |
bec2aec5 HV |
279 | mutex_lock(&dev->lock); |
280 | outb(7, dev->io); /* Select tuner control */ | |
281 | if (vol > 0) | |
282 | outb(0x20, dev->io + 1); | |
4286c6f6 | 283 | else |
bec2aec5 HV |
284 | outb(0x00, dev->io + 1); |
285 | mutex_unlock(&dev->lock); | |
4286c6f6 | 286 | } |
1da177e4 | 287 | |
bec2aec5 | 288 | static void cadet_handler(unsigned long data) |
1da177e4 | 289 | { |
bec2aec5 | 290 | struct cadet *dev = (void *)data; |
1da177e4 | 291 | |
bec2aec5 HV |
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) | |
4286c6f6 | 296 | printk(KERN_CRIT "cadet: RDS fifo overflow\n"); |
bec2aec5 HV |
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) | |
4286c6f6 | 301 | printk(KERN_WARNING "cadet: RDS buffer overflow\n"); |
1da177e4 | 302 | else |
bec2aec5 | 303 | dev->rdsin++; |
1da177e4 | 304 | } |
bec2aec5 | 305 | mutex_unlock(&dev->lock); |
1da177e4 LT |
306 | } |
307 | ||
308 | /* | |
309 | * Service pending read | |
310 | */ | |
bec2aec5 HV |
311 | if (dev->rdsin != dev->rdsout) |
312 | wake_up_interruptible(&dev->read_queue); | |
1da177e4 | 313 | |
4286c6f6 | 314 | /* |
1da177e4 LT |
315 | * Clean up and exit |
316 | */ | |
bec2aec5 HV |
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); | |
1da177e4 LT |
322 | } |
323 | ||
324 | ||
bec2aec5 | 325 | static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos) |
1da177e4 | 326 | { |
bec2aec5 | 327 | struct cadet *dev = video_drvdata(file); |
1da177e4 | 328 | unsigned char readbuf[RDS_BUFFER]; |
bec2aec5 HV |
329 | int i = 0; |
330 | ||
331 | if (dev->rdsstat == 0) { | |
332 | mutex_lock(&dev->lock); | |
333 | dev->rdsstat = 1; | |
334 | outb(0x80, dev->io); /* Select RDS fifo */ | |
335 | mutex_unlock(&dev->lock); | |
336 | init_timer(&dev->readtimer); | |
337 | dev->readtimer.function = cadet_handler; | |
338 | dev->readtimer.data = (unsigned long)dev; | |
339 | dev->readtimer.expires = jiffies + msecs_to_jiffies(50); | |
340 | add_timer(&dev->readtimer); | |
1da177e4 | 341 | } |
bec2aec5 | 342 | if (dev->rdsin == dev->rdsout) { |
4286c6f6 MCC |
343 | if (file->f_flags & O_NONBLOCK) |
344 | return -EWOULDBLOCK; | |
bec2aec5 | 345 | interruptible_sleep_on(&dev->read_queue); |
4286c6f6 | 346 | } |
bec2aec5 HV |
347 | while (i < count && dev->rdsin != dev->rdsout) |
348 | readbuf[i++] = dev->rdsbuf[dev->rdsout++]; | |
1da177e4 | 349 | |
bec2aec5 | 350 | if (copy_to_user(data, readbuf, i)) |
4286c6f6 | 351 | return -EFAULT; |
1da177e4 LT |
352 | return i; |
353 | } | |
354 | ||
355 | ||
c1c4fd3e DL |
356 | static int vidioc_querycap(struct file *file, void *priv, |
357 | struct v4l2_capability *v) | |
358 | { | |
bec2aec5 HV |
359 | strlcpy(v->driver, "ADS Cadet", sizeof(v->driver)); |
360 | strlcpy(v->card, "ADS Cadet", sizeof(v->card)); | |
361 | strlcpy(v->bus_info, "ISA", sizeof(v->bus_info)); | |
c1c4fd3e | 362 | v->version = CADET_VERSION; |
8e280f24 HV |
363 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO | |
364 | V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE; | |
c1c4fd3e DL |
365 | return 0; |
366 | } | |
1da177e4 | 367 | |
c1c4fd3e DL |
368 | static int vidioc_g_tuner(struct file *file, void *priv, |
369 | struct v4l2_tuner *v) | |
1da177e4 | 370 | { |
bec2aec5 HV |
371 | struct cadet *dev = video_drvdata(file); |
372 | ||
c1c4fd3e DL |
373 | v->type = V4L2_TUNER_RADIO; |
374 | switch (v->index) { | |
375 | case 0: | |
bec2aec5 | 376 | strlcpy(v->name, "FM", sizeof(v->name)); |
cb0ed222 MA |
377 | v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS | |
378 | V4L2_TUNER_CAP_RDS_BLOCK_IO; | |
c1c4fd3e DL |
379 | v->rangelow = 1400; /* 87.5 MHz */ |
380 | v->rangehigh = 1728; /* 108.0 MHz */ | |
bec2aec5 HV |
381 | v->rxsubchans = cadet_getstereo(dev); |
382 | switch (v->rxsubchans) { | |
c1c4fd3e DL |
383 | case V4L2_TUNER_SUB_MONO: |
384 | v->audmode = V4L2_TUNER_MODE_MONO; | |
385 | break; | |
386 | case V4L2_TUNER_SUB_STEREO: | |
387 | v->audmode = V4L2_TUNER_MODE_STEREO; | |
388 | break; | |
bec2aec5 HV |
389 | default: |
390 | break; | |
1da177e4 | 391 | } |
8e280f24 | 392 | v->rxsubchans |= V4L2_TUNER_SUB_RDS; |
c1c4fd3e DL |
393 | break; |
394 | case 1: | |
bec2aec5 | 395 | strlcpy(v->name, "AM", sizeof(v->name)); |
c1c4fd3e DL |
396 | v->capability = V4L2_TUNER_CAP_LOW; |
397 | v->rangelow = 8320; /* 520 kHz */ | |
398 | v->rangehigh = 26400; /* 1650 kHz */ | |
399 | v->rxsubchans = V4L2_TUNER_SUB_MONO; | |
400 | v->audmode = V4L2_TUNER_MODE_MONO; | |
401 | break; | |
402 | default: | |
403 | return -EINVAL; | |
404 | } | |
bec2aec5 | 405 | v->signal = dev->sigstrength; /* We might need to modify scaling of this */ |
c1c4fd3e DL |
406 | return 0; |
407 | } | |
c0c7fa09 | 408 | |
c1c4fd3e DL |
409 | static int vidioc_s_tuner(struct file *file, void *priv, |
410 | struct v4l2_tuner *v) | |
411 | { | |
bec2aec5 HV |
412 | struct cadet *dev = video_drvdata(file); |
413 | ||
414 | if (v->index != 0 && v->index != 1) | |
c1c4fd3e | 415 | return -EINVAL; |
bec2aec5 | 416 | dev->curtuner = v->index; |
c1c4fd3e DL |
417 | return 0; |
418 | } | |
c0c7fa09 | 419 | |
c1c4fd3e DL |
420 | static int vidioc_g_frequency(struct file *file, void *priv, |
421 | struct v4l2_frequency *f) | |
422 | { | |
bec2aec5 HV |
423 | struct cadet *dev = video_drvdata(file); |
424 | ||
425 | f->tuner = dev->curtuner; | |
c1c4fd3e | 426 | f->type = V4L2_TUNER_RADIO; |
bec2aec5 | 427 | f->frequency = cadet_getfreq(dev); |
c1c4fd3e DL |
428 | return 0; |
429 | } | |
430 | ||
431 | ||
432 | static int vidioc_s_frequency(struct file *file, void *priv, | |
433 | struct v4l2_frequency *f) | |
434 | { | |
bec2aec5 HV |
435 | struct cadet *dev = video_drvdata(file); |
436 | ||
c1c4fd3e DL |
437 | if (f->type != V4L2_TUNER_RADIO) |
438 | return -EINVAL; | |
bec2aec5 | 439 | if (dev->curtuner == 0 && (f->frequency < 1400 || f->frequency > 1728)) |
c1c4fd3e | 440 | return -EINVAL; |
bec2aec5 | 441 | if (dev->curtuner == 1 && (f->frequency < 8320 || f->frequency > 26400)) |
c1c4fd3e | 442 | return -EINVAL; |
bec2aec5 | 443 | cadet_setfreq(dev, f->frequency); |
c1c4fd3e DL |
444 | return 0; |
445 | } | |
446 | ||
447 | static int vidioc_queryctrl(struct file *file, void *priv, | |
448 | struct v4l2_queryctrl *qc) | |
449 | { | |
bec2aec5 HV |
450 | switch (qc->id) { |
451 | case V4L2_CID_AUDIO_MUTE: | |
452 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); | |
453 | case V4L2_CID_AUDIO_VOLUME: | |
454 | return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff); | |
c1c4fd3e DL |
455 | } |
456 | return -EINVAL; | |
457 | } | |
c0c7fa09 | 458 | |
c1c4fd3e DL |
459 | static int vidioc_g_ctrl(struct file *file, void *priv, |
460 | struct v4l2_control *ctrl) | |
461 | { | |
bec2aec5 HV |
462 | struct cadet *dev = video_drvdata(file); |
463 | ||
464 | switch (ctrl->id) { | |
c1c4fd3e | 465 | case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */ |
bec2aec5 | 466 | ctrl->value = (cadet_getvol(dev) == 0); |
c1c4fd3e DL |
467 | break; |
468 | case V4L2_CID_AUDIO_VOLUME: | |
bec2aec5 | 469 | ctrl->value = cadet_getvol(dev); |
c1c4fd3e DL |
470 | break; |
471 | default: | |
472 | return -EINVAL; | |
1da177e4 | 473 | } |
c1c4fd3e | 474 | return 0; |
1da177e4 LT |
475 | } |
476 | ||
c1c4fd3e DL |
477 | static int vidioc_s_ctrl(struct file *file, void *priv, |
478 | struct v4l2_control *ctrl) | |
1da177e4 | 479 | { |
bec2aec5 HV |
480 | struct cadet *dev = video_drvdata(file); |
481 | ||
c1c4fd3e DL |
482 | switch (ctrl->id){ |
483 | case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */ | |
484 | if (ctrl->value) | |
bec2aec5 | 485 | cadet_setvol(dev, 0); |
c1c4fd3e | 486 | else |
bec2aec5 | 487 | cadet_setvol(dev, 0xffff); |
c1c4fd3e DL |
488 | break; |
489 | case V4L2_CID_AUDIO_VOLUME: | |
bec2aec5 | 490 | cadet_setvol(dev, ctrl->value); |
c1c4fd3e DL |
491 | break; |
492 | default: | |
493 | return -EINVAL; | |
494 | } | |
495 | return 0; | |
496 | } | |
497 | ||
c1c4fd3e DL |
498 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
499 | { | |
500 | *i = 0; | |
501 | return 0; | |
502 | } | |
503 | ||
504 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) | |
505 | { | |
bec2aec5 HV |
506 | return i ? -EINVAL : 0; |
507 | } | |
508 | ||
509 | static int vidioc_g_audio(struct file *file, void *priv, | |
510 | struct v4l2_audio *a) | |
511 | { | |
512 | a->index = 0; | |
513 | strlcpy(a->name, "Radio", sizeof(a->name)); | |
514 | a->capability = V4L2_AUDCAP_STEREO; | |
c1c4fd3e DL |
515 | return 0; |
516 | } | |
517 | ||
518 | static int vidioc_s_audio(struct file *file, void *priv, | |
519 | struct v4l2_audio *a) | |
520 | { | |
bec2aec5 | 521 | return a->index ? -EINVAL : 0; |
1da177e4 LT |
522 | } |
523 | ||
bec2aec5 | 524 | static int cadet_open(struct file *file) |
1da177e4 | 525 | { |
bec2aec5 HV |
526 | struct cadet *dev = video_drvdata(file); |
527 | ||
528 | dev->users++; | |
529 | if (1 == dev->users) | |
530 | init_waitqueue_head(&dev->read_queue); | |
1da177e4 LT |
531 | return 0; |
532 | } | |
533 | ||
bec2aec5 | 534 | static int cadet_release(struct file *file) |
1da177e4 | 535 | { |
bec2aec5 HV |
536 | struct cadet *dev = video_drvdata(file); |
537 | ||
538 | dev->users--; | |
539 | if (0 == dev->users) { | |
540 | del_timer_sync(&dev->readtimer); | |
541 | dev->rdsstat = 0; | |
c0c7fa09 HK |
542 | } |
543 | return 0; | |
544 | } | |
545 | ||
bec2aec5 | 546 | static unsigned int cadet_poll(struct file *file, struct poll_table_struct *wait) |
c0c7fa09 | 547 | { |
bec2aec5 HV |
548 | struct cadet *dev = video_drvdata(file); |
549 | ||
550 | poll_wait(file, &dev->read_queue, wait); | |
551 | if (dev->rdsin != dev->rdsout) | |
c0c7fa09 | 552 | return POLLIN | POLLRDNORM; |
1da177e4 LT |
553 | return 0; |
554 | } | |
555 | ||
556 | ||
bec43661 | 557 | static const struct v4l2_file_operations cadet_fops = { |
1da177e4 LT |
558 | .owner = THIS_MODULE, |
559 | .open = cadet_open, | |
560 | .release = cadet_release, | |
561 | .read = cadet_read, | |
c1c4fd3e | 562 | .ioctl = video_ioctl2, |
c0c7fa09 | 563 | .poll = cadet_poll, |
1da177e4 LT |
564 | }; |
565 | ||
a399810c | 566 | static const struct v4l2_ioctl_ops cadet_ioctl_ops = { |
c1c4fd3e DL |
567 | .vidioc_querycap = vidioc_querycap, |
568 | .vidioc_g_tuner = vidioc_g_tuner, | |
569 | .vidioc_s_tuner = vidioc_s_tuner, | |
570 | .vidioc_g_frequency = vidioc_g_frequency, | |
571 | .vidioc_s_frequency = vidioc_s_frequency, | |
572 | .vidioc_queryctrl = vidioc_queryctrl, | |
573 | .vidioc_g_ctrl = vidioc_g_ctrl, | |
574 | .vidioc_s_ctrl = vidioc_s_ctrl, | |
575 | .vidioc_g_audio = vidioc_g_audio, | |
576 | .vidioc_s_audio = vidioc_s_audio, | |
577 | .vidioc_g_input = vidioc_g_input, | |
578 | .vidioc_s_input = vidioc_s_input, | |
1da177e4 LT |
579 | }; |
580 | ||
044dfc99 BH |
581 | #ifdef CONFIG_PNP |
582 | ||
1da177e4 LT |
583 | static struct pnp_device_id cadet_pnp_devices[] = { |
584 | /* ADS Cadet AM/FM Radio Card */ | |
585 | {.id = "MSM0c24", .driver_data = 0}, | |
586 | {.id = ""} | |
587 | }; | |
588 | ||
589 | MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices); | |
590 | ||
bec2aec5 | 591 | static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id) |
1da177e4 LT |
592 | { |
593 | if (!dev) | |
594 | return -ENODEV; | |
595 | /* only support one device */ | |
596 | if (io > 0) | |
597 | return -EBUSY; | |
598 | ||
bec2aec5 | 599 | if (!pnp_port_valid(dev, 0)) |
1da177e4 | 600 | return -ENODEV; |
1da177e4 LT |
601 | |
602 | io = pnp_port_start(dev, 0); | |
603 | ||
bec2aec5 | 604 | printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io); |
1da177e4 LT |
605 | |
606 | return io; | |
607 | } | |
608 | ||
609 | static struct pnp_driver cadet_pnp_driver = { | |
610 | .name = "radio-cadet", | |
611 | .id_table = cadet_pnp_devices, | |
612 | .probe = cadet_pnp_probe, | |
613 | .remove = NULL, | |
614 | }; | |
615 | ||
044dfc99 BH |
616 | #else |
617 | static struct pnp_driver cadet_pnp_driver; | |
618 | #endif | |
619 | ||
bec2aec5 | 620 | static void cadet_probe(struct cadet *dev) |
1da177e4 | 621 | { |
bec2aec5 | 622 | static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e }; |
1da177e4 LT |
623 | int i; |
624 | ||
bec2aec5 HV |
625 | for (i = 0; i < 8; i++) { |
626 | dev->io = iovals[i]; | |
627 | if (request_region(dev->io, 2, "cadet-probe")) { | |
628 | cadet_setfreq(dev, 1410); | |
629 | if (cadet_getfreq(dev) == 1410) { | |
630 | release_region(dev->io, 2); | |
631 | return; | |
1da177e4 | 632 | } |
bec2aec5 | 633 | release_region(dev->io, 2); |
1da177e4 LT |
634 | } |
635 | } | |
bec2aec5 | 636 | dev->io = -1; |
1da177e4 LT |
637 | } |
638 | ||
4286c6f6 | 639 | /* |
1da177e4 LT |
640 | * io should only be set if the user has used something like |
641 | * isapnp (the userspace program) to initialize this card for us | |
642 | */ | |
643 | ||
644 | static int __init cadet_init(void) | |
645 | { | |
bec2aec5 HV |
646 | struct cadet *dev = &cadet_card; |
647 | struct v4l2_device *v4l2_dev = &dev->v4l2_dev; | |
648 | int res; | |
4286c6f6 | 649 | |
bec2aec5 HV |
650 | strlcpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name)); |
651 | mutex_init(&dev->lock); | |
652 | ||
653 | /* If a probe was requested then probe ISAPnP first (safest) */ | |
1da177e4 LT |
654 | if (io < 0) |
655 | pnp_register_driver(&cadet_pnp_driver); | |
bec2aec5 | 656 | dev->io = io; |
1da177e4 | 657 | |
bec2aec5 HV |
658 | /* If that fails then probe unsafely if probe is requested */ |
659 | if (dev->io < 0) | |
660 | cadet_probe(dev); | |
4286c6f6 | 661 | |
bec2aec5 HV |
662 | /* Else we bail out */ |
663 | if (dev->io < 0) { | |
4286c6f6 | 664 | #ifdef MODULE |
b24c20cc HV |
665 | v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n"); |
666 | v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n"); | |
1da177e4 | 667 | #endif |
4286c6f6 | 668 | goto fail; |
1da177e4 | 669 | } |
bec2aec5 HV |
670 | if (!request_region(dev->io, 2, "cadet")) |
671 | goto fail; | |
672 | ||
673 | res = v4l2_device_register(NULL, v4l2_dev); | |
674 | if (res < 0) { | |
675 | release_region(dev->io, 2); | |
676 | v4l2_err(v4l2_dev, "could not register v4l2_device\n"); | |
1da177e4 | 677 | goto fail; |
bec2aec5 HV |
678 | } |
679 | ||
680 | strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); | |
681 | dev->vdev.v4l2_dev = v4l2_dev; | |
682 | dev->vdev.fops = &cadet_fops; | |
683 | dev->vdev.ioctl_ops = &cadet_ioctl_ops; | |
684 | dev->vdev.release = video_device_release_empty; | |
685 | video_set_drvdata(&dev->vdev, dev); | |
686 | ||
687 | if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) { | |
688 | v4l2_device_unregister(v4l2_dev); | |
689 | release_region(dev->io, 2); | |
1da177e4 LT |
690 | goto fail; |
691 | } | |
bec2aec5 | 692 | v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io); |
1da177e4 LT |
693 | return 0; |
694 | fail: | |
695 | pnp_unregister_driver(&cadet_pnp_driver); | |
bec2aec5 | 696 | return -ENODEV; |
1da177e4 LT |
697 | } |
698 | ||
bec2aec5 | 699 | static void __exit cadet_exit(void) |
1da177e4 | 700 | { |
bec2aec5 HV |
701 | struct cadet *dev = &cadet_card; |
702 | ||
703 | video_unregister_device(&dev->vdev); | |
704 | v4l2_device_unregister(&dev->v4l2_dev); | |
705 | release_region(dev->io, 2); | |
1da177e4 LT |
706 | pnp_unregister_driver(&cadet_pnp_driver); |
707 | } | |
708 | ||
709 | module_init(cadet_init); | |
bec2aec5 | 710 | module_exit(cadet_exit); |
1da177e4 | 711 |