]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/radio/radio-cadet.c
V4L/DVB (4406): Convert radio-cadet to V4L2 API
[net-next-2.6.git] / drivers / media / radio / radio-cadet.c
CommitLineData
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 *
26 * 2003-01-31 Alan Cox <alan@redhat.com>
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
LT
36#include <linux/delay.h> /* udelay */
37#include <asm/io.h> /* outb, outb_p */
38#include <asm/uaccess.h> /* copy to/from user */
c0c7fa09 39#include <linux/videodev2.h> /* V4L2 API defs */
5e87efa3 40#include <media/v4l2-common.h>
1da177e4
LT
41#include <linux/param.h>
42#include <linux/pnp.h>
43
44#define RDS_BUFFER 256
c0c7fa09
HK
45#define RDS_RX_FLAG 1
46#define MBS_RX_FLAG 2
47
48#define CADET_VERSION KERNEL_VERSION(0,3,3)
1da177e4
LT
49
50static int io=-1; /* default to isapnp activation */
51static int radio_nr = -1;
52static int users=0;
53static int curtuner=0;
54static int tunestat=0;
55static int sigstrength=0;
56static wait_queue_head_t read_queue;
57static struct timer_list readtimer;
58static __u8 rdsin=0,rdsout=0,rdsstat=0;
59static unsigned char rdsbuf[RDS_BUFFER];
60static spinlock_t cadet_io_lock;
61
62static int cadet_probe(void);
63
64/*
65 * Signal Strength Threshold Values
4286c6f6 66 * The V4L API spec does not define any particular unit for the signal
1da177e4
LT
67 * strength value. These values are in microvolts of RF at the tuner's input.
68 */
69static __u16 sigtable[2][4]={{5,10,30,150},{28,40,63,1000}};
70
1da177e4 71
c0c7fa09
HK
72static int
73cadet_getstereo(void)
1da177e4 74{
c0c7fa09 75 int ret = V4L2_TUNER_SUB_MONO;
4286c6f6 76 if(curtuner != 0) /* Only FM has stereo capability! */
c0c7fa09 77 return V4L2_TUNER_SUB_MONO;
1da177e4
LT
78
79 spin_lock(&cadet_io_lock);
4286c6f6 80 outb(7,io); /* Select tuner control */
1da177e4 81 if( (inb(io+1) & 0x40) == 0)
c0c7fa09 82 ret = V4L2_TUNER_SUB_STEREO;
4286c6f6
MCC
83 spin_unlock(&cadet_io_lock);
84 return ret;
1da177e4
LT
85}
86
c0c7fa09
HK
87static unsigned
88cadet_gettune(void)
1da177e4 89{
4286c6f6 90 int curvol,i;
1da177e4
LT
91 unsigned fifo=0;
92
4286c6f6
MCC
93 /*
94 * Prepare for read
95 */
1da177e4
LT
96
97 spin_lock(&cadet_io_lock);
4286c6f6
MCC
98
99 outb(7,io); /* Select tuner control */
100 curvol=inb(io+1); /* Save current volume/mute setting */
101 outb(0x00,io+1); /* Ensure WRITE-ENABLE is LOW */
1da177e4
LT
102 tunestat=0xffff;
103
4286c6f6
MCC
104 /*
105 * Read the shift register
106 */
107 for(i=0;i<25;i++) {
108 fifo=(fifo<<1)|((inb(io+1)>>7)&0x01);
109 if(i<24) {
110 outb(0x01,io+1);
1da177e4 111 tunestat&=inb(io+1);
4286c6f6
MCC
112 outb(0x00,io+1);
113 }
114 }
115
116 /*
117 * Restore volume/mute setting
118 */
119 outb(curvol,io+1);
1da177e4
LT
120 spin_unlock(&cadet_io_lock);
121
122 return fifo;
123}
124
c0c7fa09
HK
125static unsigned
126cadet_getfreq(void)
1da177e4 127{
4286c6f6
MCC
128 int i;
129 unsigned freq=0,test,fifo=0;
1da177e4
LT
130
131 /*
132 * Read current tuning
133 */
134 fifo=cadet_gettune();
135
4286c6f6
MCC
136 /*
137 * Convert to actual frequency
138 */
1da177e4 139 if(curtuner==0) { /* FM */
4286c6f6
MCC
140 test=12500;
141 for(i=0;i<14;i++) {
142 if((fifo&0x01)!=0) {
143 freq+=test;
144 }
145 test=test<<1;
146 fifo=fifo>>1;
147 }
148 freq-=10700000; /* IF frequency is 10.7 MHz */
149 freq=(freq*16)/1000000; /* Make it 1/16 MHz */
1da177e4
LT
150 }
151 if(curtuner==1) { /* AM */
4286c6f6 152 freq=((fifo&0x7fff)-2010)*16;
1da177e4
LT
153 }
154
4286c6f6 155 return freq;
1da177e4
LT
156}
157
c0c7fa09
HK
158static void
159cadet_settune(unsigned fifo)
1da177e4 160{
4286c6f6
MCC
161 int i;
162 unsigned test;
1da177e4
LT
163
164 spin_lock(&cadet_io_lock);
4286c6f6 165
1da177e4
LT
166 outb(7,io); /* Select tuner control */
167 /*
168 * Write the shift register
169 */
170 test=0;
171 test=(fifo>>23)&0x02; /* Align data for SDO */
172 test|=0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
173 outb(7,io); /* Select tuner control */
174 outb(test,io+1); /* Initialize for write */
175 for(i=0;i<25;i++) {
4286c6f6 176 test|=0x01; /* Toggle SCK High */
1da177e4
LT
177 outb(test,io+1);
178 test&=0xfe; /* Toggle SCK Low */
179 outb(test,io+1);
180 fifo=fifo<<1; /* Prepare the next bit */
181 test=0x1c|((fifo>>23)&0x02);
182 outb(test,io+1);
183 }
184 spin_unlock(&cadet_io_lock);
185}
186
c0c7fa09
HK
187static void
188cadet_setfreq(unsigned freq)
1da177e4 189{
4286c6f6
MCC
190 unsigned fifo;
191 int i,j,test;
192 int curvol;
1da177e4 193
4286c6f6
MCC
194 /*
195 * Formulate a fifo command
196 */
1da177e4
LT
197 fifo=0;
198 if(curtuner==0) { /* FM */
4286c6f6
MCC
199 test=102400;
200 freq=(freq*1000)/16; /* Make it kHz */
201 freq+=10700; /* IF is 10700 kHz */
202 for(i=0;i<14;i++) {
203 fifo=fifo<<1;
204 if(freq>=test) {
205 fifo|=0x01;
206 freq-=test;
207 }
208 test=test>>1;
209 }
1da177e4
LT
210 }
211 if(curtuner==1) { /* AM */
4286c6f6 212 fifo=(freq/16)+2010; /* Make it kHz */
1da177e4
LT
213 fifo|=0x100000; /* Select AM Band */
214 }
215
4286c6f6
MCC
216 /*
217 * Save current volume/mute setting
218 */
1da177e4
LT
219
220 spin_lock(&cadet_io_lock);
221 outb(7,io); /* Select tuner control */
4286c6f6
MCC
222 curvol=inb(io+1);
223 spin_unlock(&cadet_io_lock);
1da177e4
LT
224
225 /*
226 * Tune the card
227 */
228 for(j=3;j>-1;j--) {
4286c6f6
MCC
229 cadet_settune(fifo|(j<<16));
230
231 spin_lock(&cadet_io_lock);
1da177e4
LT
232 outb(7,io); /* Select tuner control */
233 outb(curvol,io+1);
234 spin_unlock(&cadet_io_lock);
4286c6f6 235
1da177e4
LT
236 msleep(100);
237
238 cadet_gettune();
239 if((tunestat & 0x40) == 0) { /* Tuned */
4286c6f6 240 sigstrength=sigtable[curtuner][j];
1da177e4
LT
241 return;
242 }
243 }
244 sigstrength=0;
245}
246
247
c0c7fa09
HK
248static int
249cadet_getvol(void)
1da177e4
LT
250{
251 int ret = 0;
4286c6f6 252
1da177e4 253 spin_lock(&cadet_io_lock);
4286c6f6
MCC
254
255 outb(7,io); /* Select tuner control */
256 if((inb(io + 1) & 0x20) != 0)
257 ret = 0xffff;
258
259 spin_unlock(&cadet_io_lock);
260 return ret;
1da177e4
LT
261}
262
263
c0c7fa09
HK
264static void
265cadet_setvol(int vol)
1da177e4
LT
266{
267 spin_lock(&cadet_io_lock);
4286c6f6
MCC
268 outb(7,io); /* Select tuner control */
269 if(vol>0)
270 outb(0x20,io+1);
271 else
272 outb(0x00,io+1);
1da177e4 273 spin_unlock(&cadet_io_lock);
4286c6f6 274}
1da177e4 275
c0c7fa09
HK
276static void
277cadet_handler(unsigned long data)
1da177e4
LT
278{
279 /*
280 * Service the RDS fifo
281 */
282
283 if(spin_trylock(&cadet_io_lock))
284 {
4286c6f6 285 outb(0x3,io); /* Select RDS Decoder Control */
1da177e4 286 if((inb(io+1)&0x20)!=0) {
4286c6f6 287 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
1da177e4
LT
288 }
289 outb(0x80,io); /* Select RDS fifo */
290 while((inb(io)&0x80)!=0) {
4286c6f6 291 rdsbuf[rdsin]=inb(io+1);
1da177e4 292 if(rdsin==rdsout)
4286c6f6 293 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
1da177e4
LT
294 else
295 rdsin++;
296 }
297 spin_unlock(&cadet_io_lock);
298 }
299
300 /*
301 * Service pending read
302 */
303 if( rdsin!=rdsout)
4286c6f6 304 wake_up_interruptible(&read_queue);
1da177e4 305
4286c6f6 306 /*
1da177e4
LT
307 * Clean up and exit
308 */
309 init_timer(&readtimer);
310 readtimer.function=cadet_handler;
311 readtimer.data=(unsigned long)0;
312 readtimer.expires=jiffies+(HZ/20);
313 add_timer(&readtimer);
314}
315
316
317
c0c7fa09
HK
318static ssize_t
319cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1da177e4 320{
4286c6f6 321 int i=0;
1da177e4
LT
322 unsigned char readbuf[RDS_BUFFER];
323
4286c6f6 324 if(rdsstat==0) {
1da177e4 325 spin_lock(&cadet_io_lock);
4286c6f6 326 rdsstat=1;
1da177e4
LT
327 outb(0x80,io); /* Select RDS fifo */
328 spin_unlock(&cadet_io_lock);
329 init_timer(&readtimer);
330 readtimer.function=cadet_handler;
331 readtimer.data=(unsigned long)0;
332 readtimer.expires=jiffies+(HZ/20);
333 add_timer(&readtimer);
334 }
335 if(rdsin==rdsout) {
4286c6f6
MCC
336 if (file->f_flags & O_NONBLOCK)
337 return -EWOULDBLOCK;
338 interruptible_sleep_on(&read_queue);
339 }
1da177e4 340 while( i<count && rdsin!=rdsout)
4286c6f6 341 readbuf[i++]=rdsbuf[rdsout++];
1da177e4
LT
342
343 if (copy_to_user(data,readbuf,i))
4286c6f6 344 return -EFAULT;
1da177e4
LT
345 return i;
346}
347
348
349
350static int cadet_do_ioctl(struct inode *inode, struct file *file,
351 unsigned int cmd, void *arg)
352{
353 switch(cmd)
354 {
c0c7fa09 355 case VIDIOC_QUERYCAP:
1da177e4 356 {
c0c7fa09
HK
357 struct v4l2_capability *cap = arg;
358 memset(cap,0,sizeof(*cap));
359 cap->capabilities =
360 V4L2_CAP_TUNER |
361 V4L2_CAP_READWRITE;
362 cap->version = CADET_VERSION;
363 strcpy(cap->driver, "ADS Cadet");
364 strcpy(cap->card, "ADS Cadet");
1da177e4
LT
365 return 0;
366 }
c0c7fa09 367 case VIDIOC_G_TUNER:
1da177e4 368 {
c0c7fa09
HK
369 struct v4l2_tuner *t = arg;
370 memset(t,0,sizeof(*t));
371 t->type = V4L2_TUNER_RADIO;
372 switch (t->index)
373 {
374 case 0: strcpy(t->name, "FM");
375 t->capability = V4L2_TUNER_CAP_STEREO;
376 t->rangelow = 1400; /* 87.5 MHz */
377 t->rangehigh = 1728; /* 108.0 MHz */
378 t->rxsubchans=cadet_getstereo();
379 switch (t->rxsubchans){
380 case V4L2_TUNER_SUB_MONO:
381 t->audmode = V4L2_TUNER_MODE_MONO;
382 break;
383 case V4L2_TUNER_SUB_STEREO:
384 t->audmode = V4L2_TUNER_MODE_STEREO;
385 break;
386 default: ;
387 }
388 break;
389 case 1: strcpy(t->name, "AM");
390 t->capability = V4L2_TUNER_CAP_LOW;
391 t->rangelow = 8320; /* 520 kHz */
392 t->rangehigh = 26400; /* 1650 kHz */
393 t->rxsubchans = V4L2_TUNER_SUB_MONO;
394 t->audmode = V4L2_TUNER_MODE_MONO;
395 break;
396 default:
397 return -EINVAL;
1da177e4 398 }
c0c7fa09
HK
399
400 t->signal = sigstrength; /* We might need to modify scaling of this */
1da177e4
LT
401 return 0;
402 }
c0c7fa09 403 case VIDIOC_S_TUNER:
1da177e4 404 {
c0c7fa09
HK
405 struct v4l2_tuner *t = arg;
406 if((t->index != 0)&&(t->index != 1))
1da177e4 407 return -EINVAL;
c0c7fa09
HK
408
409 curtuner = t->index;
1da177e4
LT
410 return 0;
411 }
c0c7fa09 412 case VIDIOC_G_FREQUENCY:
1da177e4 413 {
c0c7fa09
HK
414 struct v4l2_frequency *f = arg;
415 memset(f,0,sizeof(*f));
416 f->tuner = curtuner;
417 f->type = V4L2_TUNER_RADIO;
418 f->frequency = cadet_getfreq();
1da177e4
LT
419 return 0;
420 }
c0c7fa09 421 case VIDIOC_S_FREQUENCY:
1da177e4 422 {
c0c7fa09
HK
423 struct v4l2_frequency *f = arg;
424 if (f->type != V4L2_TUNER_RADIO){
425 return -EINVAL;
426 }
427 if((curtuner==0)&&((f->frequency<1400)||(f->frequency>1728))) {
4286c6f6 428 return -EINVAL;
1da177e4 429 }
c0c7fa09 430 if((curtuner==1)&&((f->frequency<8320)||(f->frequency>26400))) {
4286c6f6 431 return -EINVAL;
1da177e4 432 }
c0c7fa09 433 cadet_setfreq(f->frequency);
1da177e4
LT
434 return 0;
435 }
c0c7fa09 436 case VIDIOC_G_CTRL:
4286c6f6 437 {
c0c7fa09
HK
438 struct v4l2_control *c = arg;
439 switch (c->id){
440 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
441 c->value = (cadet_getvol() == 0);
442 break;
443 case V4L2_CID_AUDIO_VOLUME:
444 c->value = cadet_getvol();
445 break;
446 default:
447 return -EINVAL;
1da177e4 448 }
4286c6f6 449 return 0;
1da177e4 450 }
c0c7fa09 451 case VIDIOC_S_CTRL:
1da177e4 452 {
c0c7fa09
HK
453 struct v4l2_control *c = arg;
454 switch (c->id){
455 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
456 if (c->value) cadet_setvol(0);
457 else cadet_setvol(0xffff);
458 break;
459 case V4L2_CID_AUDIO_VOLUME:
460 cadet_setvol(c->value);
461 break;
462 default:
463 return -EINVAL;
464 }
1da177e4
LT
465 return 0;
466 }
c0c7fa09 467
1da177e4
LT
468 default:
469 return -ENOIOCTLCMD;
470 }
471}
472
c0c7fa09
HK
473static int
474cadet_ioctl(struct inode *inode, struct file *file,
1da177e4
LT
475 unsigned int cmd, unsigned long arg)
476{
477 return video_usercopy(inode, file, cmd, arg, cadet_do_ioctl);
478}
479
c0c7fa09
HK
480static int
481cadet_open(struct inode *inode, struct file *file)
1da177e4 482{
1da177e4 483 users++;
c0c7fa09 484 if (1 == users) init_waitqueue_head(&read_queue);
1da177e4
LT
485 return 0;
486}
487
c0c7fa09
HK
488static int
489cadet_release(struct inode *inode, struct file *file)
1da177e4 490{
1da177e4 491 users--;
c0c7fa09
HK
492 if (0 == users){
493 del_timer_sync(&readtimer);
494 rdsstat=0;
495 }
496 return 0;
497}
498
499static unsigned int
500cadet_poll(struct file *file, struct poll_table_struct *wait)
501{
502 poll_wait(file,&read_queue,wait);
503 if(rdsin != rdsout)
504 return POLLIN | POLLRDNORM;
1da177e4
LT
505 return 0;
506}
507
508
509static struct file_operations cadet_fops = {
510 .owner = THIS_MODULE,
511 .open = cadet_open,
512 .release = cadet_release,
513 .read = cadet_read,
514 .ioctl = cadet_ioctl,
c0c7fa09 515 .poll = cadet_poll,
0d0fbf81 516 .compat_ioctl = v4l_compat_ioctl32,
1da177e4
LT
517 .llseek = no_llseek,
518};
519
520static struct video_device cadet_radio=
521{
522 .owner = THIS_MODULE,
523 .name = "Cadet radio",
524 .type = VID_TYPE_TUNER,
1da177e4
LT
525 .fops = &cadet_fops,
526};
527
528static struct pnp_device_id cadet_pnp_devices[] = {
529 /* ADS Cadet AM/FM Radio Card */
530 {.id = "MSM0c24", .driver_data = 0},
531 {.id = ""}
532};
533
534MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
535
536static int cadet_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id *dev_id)
537{
538 if (!dev)
539 return -ENODEV;
540 /* only support one device */
541 if (io > 0)
542 return -EBUSY;
543
544 if (!pnp_port_valid(dev, 0)) {
545 return -ENODEV;
546 }
547
548 io = pnp_port_start(dev, 0);
549
550 printk ("radio-cadet: PnP reports device at %#x\n", io);
551
552 return io;
553}
554
555static struct pnp_driver cadet_pnp_driver = {
556 .name = "radio-cadet",
557 .id_table = cadet_pnp_devices,
558 .probe = cadet_pnp_probe,
559 .remove = NULL,
560};
561
562static int cadet_probe(void)
563{
4286c6f6 564 static int iovals[8]={0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e};
1da177e4
LT
565 int i;
566
567 for(i=0;i<8;i++) {
4286c6f6 568 io=iovals[i];
f1ac046d 569 if (request_region(io, 2, "cadet-probe")) {
4286c6f6 570 cadet_setfreq(1410);
1da177e4
LT
571 if(cadet_getfreq()==1410) {
572 release_region(io, 2);
4286c6f6 573 return io;
1da177e4
LT
574 }
575 release_region(io, 2);
576 }
577 }
578 return -1;
579}
580
4286c6f6 581/*
1da177e4
LT
582 * io should only be set if the user has used something like
583 * isapnp (the userspace program) to initialize this card for us
584 */
585
586static int __init cadet_init(void)
587{
588 spin_lock_init(&cadet_io_lock);
4286c6f6 589
1da177e4
LT
590 /*
591 * If a probe was requested then probe ISAPnP first (safest)
592 */
593 if (io < 0)
594 pnp_register_driver(&cadet_pnp_driver);
595 /*
596 * If that fails then probe unsafely if probe is requested
597 */
598 if(io < 0)
599 io = cadet_probe ();
600
601 /*
602 * Else we bail out
603 */
4286c6f6
MCC
604
605 if(io < 0) {
606#ifdef MODULE
1da177e4
LT
607 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
608#endif
4286c6f6 609 goto fail;
1da177e4
LT
610 }
611 if (!request_region(io,2,"cadet"))
612 goto fail;
613 if(video_register_device(&cadet_radio,VFL_TYPE_RADIO,radio_nr)==-1) {
614 release_region(io,2);
615 goto fail;
616 }
617 printk(KERN_INFO "ADS Cadet Radio Card at 0x%x\n",io);
618 return 0;
619fail:
620 pnp_unregister_driver(&cadet_pnp_driver);
621 return -1;
622}
623
624
625
626MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
627MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
628MODULE_LICENSE("GPL");
629
630module_param(io, int, 0);
631MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
632module_param(radio_nr, int, 0);
633
634static void __exit cadet_cleanup_module(void)
635{
636 video_unregister_device(&cadet_radio);
637 release_region(io,2);
638 pnp_unregister_driver(&cadet_pnp_driver);
639}
640
641module_init(cadet_init);
642module_exit(cadet_cleanup_module);
643