blob: bfd37f38b9ab3ad68df8430d16e6807dbf284654 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
2 *
3 * by Fred Gleason <fredg@wava.com>
4 * Version 0.3.3
5 *
6 * (Loosely) based on code for the Aztech radio card by
7 *
8 * Russell Kroll (rkroll@exploits.org)
9 * Quay Ly
10 * Donald Song
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030011 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
13 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
14 *
15 * History:
16 * 2000-04-29 Russell Kroll <rkroll@exploits.org>
17 * Added ISAPnP detection for Linux 2.3/2.4
18 *
19 * 2001-01-10 Russell Kroll <rkroll@exploits.org>
20 * Removed dead CONFIG_RADIO_CADET_PORT code
21 * PnP detection on load is now default (no args necessary)
22 *
23 * 2002-01-17 Adam Belay <ambx1@neo.rr.com>
24 * Updated to latest pnp code
25 *
Alan Coxd9b01442008-10-27 15:13:47 -030026 * 2003-01-31 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * Cleaned up locking, delay code, general odds and ends
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030028 *
29 * 2006-07-30 Hans J. Koch <koch@hjk-az.de>
30 * Changed API to V4L2
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
Andrew Mortond591b9c2006-08-08 10:52:22 -030033#include <linux/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h> /* Modules */
35#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070036#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/delay.h> /* udelay */
38#include <asm/io.h> /* outb, outb_p */
39#include <asm/uaccess.h> /* copy to/from user */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030040#include <linux/videodev2.h> /* V4L2 API defs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/param.h>
44#include <linux/pnp.h>
45
46#define RDS_BUFFER 256
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030047#define RDS_RX_FLAG 1
48#define MBS_RX_FLAG 2
49
50#define CADET_VERSION KERNEL_VERSION(0,3,3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -030052static struct v4l2_queryctrl radio_qctrl[] = {
53 {
54 .id = V4L2_CID_AUDIO_MUTE,
55 .name = "Mute",
56 .minimum = 0,
57 .maximum = 1,
58 .default_value = 1,
59 .type = V4L2_CTRL_TYPE_BOOLEAN,
60 },{
61 .id = V4L2_CID_AUDIO_VOLUME,
62 .name = "Volume",
63 .minimum = 0,
64 .maximum = 0xff,
65 .step = 1,
66 .default_value = 0xff,
67 .type = V4L2_CTRL_TYPE_INTEGER,
68 }
69};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int io=-1; /* default to isapnp activation */
72static int radio_nr = -1;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030073static int users;
74static int curtuner;
75static int tunestat;
76static int sigstrength;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static wait_queue_head_t read_queue;
78static struct timer_list readtimer;
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030079static __u8 rdsin, rdsout, rdsstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static unsigned char rdsbuf[RDS_BUFFER];
81static spinlock_t cadet_io_lock;
82
83static int cadet_probe(void);
84
85/*
86 * Signal Strength Threshold Values
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030087 * The V4L API spec does not define any particular unit for the signal
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * strength value. These values are in microvolts of RF at the tuner's input.
89 */
90static __u16 sigtable[2][4]={{5,10,30,150},{28,40,63,1000}};
91
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030092
93static int
94cadet_getstereo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030096 int ret = V4L2_TUNER_SUB_MONO;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030097 if(curtuner != 0) /* Only FM has stereo capability! */
Hans J. Kochc0c7fa02006-08-08 09:10:12 -030098 return V4L2_TUNER_SUB_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 spin_lock(&cadet_io_lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300101 outb(7,io); /* Select tuner control */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if( (inb(io+1) & 0x40) == 0)
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300103 ret = V4L2_TUNER_SUB_STEREO;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300104 spin_unlock(&cadet_io_lock);
105 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300108static unsigned
109cadet_gettune(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300111 int curvol,i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned fifo=0;
113
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300114 /*
115 * Prepare for read
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 spin_lock(&cadet_io_lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300119
120 outb(7,io); /* Select tuner control */
121 curvol=inb(io+1); /* Save current volume/mute setting */
122 outb(0x00,io+1); /* Ensure WRITE-ENABLE is LOW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 tunestat=0xffff;
124
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300125 /*
126 * Read the shift register
127 */
128 for(i=0;i<25;i++) {
129 fifo=(fifo<<1)|((inb(io+1)>>7)&0x01);
130 if(i<24) {
131 outb(0x01,io+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 tunestat&=inb(io+1);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300133 outb(0x00,io+1);
134 }
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300137 /*
138 * Restore volume/mute setting
139 */
140 outb(curvol,io+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 spin_unlock(&cadet_io_lock);
142
143 return fifo;
144}
145
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300146static unsigned
147cadet_getfreq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300149 int i;
150 unsigned freq=0,test,fifo=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /*
153 * Read current tuning
154 */
155 fifo=cadet_gettune();
156
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300157 /*
158 * Convert to actual frequency
159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if(curtuner==0) { /* FM */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300161 test=12500;
162 for(i=0;i<14;i++) {
163 if((fifo&0x01)!=0) {
164 freq+=test;
165 }
166 test=test<<1;
167 fifo=fifo>>1;
168 }
169 freq-=10700000; /* IF frequency is 10.7 MHz */
170 freq=(freq*16)/1000000; /* Make it 1/16 MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 if(curtuner==1) { /* AM */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300173 freq=((fifo&0x7fff)-2010)*16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300176 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300179static void
180cadet_settune(unsigned fifo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300182 int i;
183 unsigned test;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 spin_lock(&cadet_io_lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 outb(7,io); /* Select tuner control */
188 /*
189 * Write the shift register
190 */
191 test=0;
192 test=(fifo>>23)&0x02; /* Align data for SDO */
193 test|=0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
194 outb(7,io); /* Select tuner control */
195 outb(test,io+1); /* Initialize for write */
196 for(i=0;i<25;i++) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300197 test|=0x01; /* Toggle SCK High */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 outb(test,io+1);
199 test&=0xfe; /* Toggle SCK Low */
200 outb(test,io+1);
201 fifo=fifo<<1; /* Prepare the next bit */
202 test=0x1c|((fifo>>23)&0x02);
203 outb(test,io+1);
204 }
205 spin_unlock(&cadet_io_lock);
206}
207
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300208static void
209cadet_setfreq(unsigned freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300211 unsigned fifo;
212 int i,j,test;
213 int curvol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300215 /*
216 * Formulate a fifo command
217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 fifo=0;
219 if(curtuner==0) { /* FM */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300220 test=102400;
221 freq=(freq*1000)/16; /* Make it kHz */
222 freq+=10700; /* IF is 10700 kHz */
223 for(i=0;i<14;i++) {
224 fifo=fifo<<1;
225 if(freq>=test) {
226 fifo|=0x01;
227 freq-=test;
228 }
229 test=test>>1;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232 if(curtuner==1) { /* AM */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300233 fifo=(freq/16)+2010; /* Make it kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 fifo|=0x100000; /* Select AM Band */
235 }
236
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300237 /*
238 * Save current volume/mute setting
239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 spin_lock(&cadet_io_lock);
242 outb(7,io); /* Select tuner control */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300243 curvol=inb(io+1);
244 spin_unlock(&cadet_io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /*
247 * Tune the card
248 */
249 for(j=3;j>-1;j--) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300250 cadet_settune(fifo|(j<<16));
251
252 spin_lock(&cadet_io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 outb(7,io); /* Select tuner control */
254 outb(curvol,io+1);
255 spin_unlock(&cadet_io_lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 msleep(100);
258
259 cadet_gettune();
260 if((tunestat & 0x40) == 0) { /* Tuned */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300261 sigstrength=sigtable[curtuner][j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return;
263 }
264 }
265 sigstrength=0;
266}
267
268
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300269static int
270cadet_getvol(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 int ret = 0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 spin_lock(&cadet_io_lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300275
276 outb(7,io); /* Select tuner control */
277 if((inb(io + 1) & 0x20) != 0)
278 ret = 0xffff;
279
280 spin_unlock(&cadet_io_lock);
281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300285static void
286cadet_setvol(int vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 spin_lock(&cadet_io_lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300289 outb(7,io); /* Select tuner control */
290 if(vol>0)
291 outb(0x20,io+1);
292 else
293 outb(0x00,io+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 spin_unlock(&cadet_io_lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300297static void
298cadet_handler(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 /*
301 * Service the RDS fifo
302 */
303
304 if(spin_trylock(&cadet_io_lock))
305 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300306 outb(0x3,io); /* Select RDS Decoder Control */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if((inb(io+1)&0x20)!=0) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300308 printk(KERN_CRIT "cadet: RDS fifo overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 outb(0x80,io); /* Select RDS fifo */
311 while((inb(io)&0x80)!=0) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300312 rdsbuf[rdsin]=inb(io+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if(rdsin==rdsout)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300314 printk(KERN_WARNING "cadet: RDS buffer overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 else
316 rdsin++;
317 }
318 spin_unlock(&cadet_io_lock);
319 }
320
321 /*
322 * Service pending read
323 */
324 if( rdsin!=rdsout)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300325 wake_up_interruptible(&read_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300327 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * Clean up and exit
329 */
330 init_timer(&readtimer);
331 readtimer.function=cadet_handler;
332 readtimer.data=(unsigned long)0;
Mauro Carvalho Chehaba2d66a32007-07-17 16:15:58 -0300333 readtimer.expires=jiffies+msecs_to_jiffies(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 add_timer(&readtimer);
335}
336
337
338
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300339static ssize_t
340cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300342 int i=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unsigned char readbuf[RDS_BUFFER];
344
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300345 if(rdsstat==0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 spin_lock(&cadet_io_lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300347 rdsstat=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 outb(0x80,io); /* Select RDS fifo */
349 spin_unlock(&cadet_io_lock);
350 init_timer(&readtimer);
351 readtimer.function=cadet_handler;
352 readtimer.data=(unsigned long)0;
Mauro Carvalho Chehaba2d66a32007-07-17 16:15:58 -0300353 readtimer.expires=jiffies+msecs_to_jiffies(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 add_timer(&readtimer);
355 }
356 if(rdsin==rdsout) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300357 if (file->f_flags & O_NONBLOCK)
358 return -EWOULDBLOCK;
359 interruptible_sleep_on(&read_queue);
360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 while( i<count && rdsin!=rdsout)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300362 readbuf[i++]=rdsbuf[rdsout++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (copy_to_user(data,readbuf,i))
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300365 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return i;
367}
368
369
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300370static int vidioc_querycap(struct file *file, void *priv,
371 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300373 v->capabilities =
374 V4L2_CAP_TUNER |
375 V4L2_CAP_READWRITE;
376 v->version = CADET_VERSION;
377 strcpy(v->driver, "ADS Cadet");
378 strcpy(v->card, "ADS Cadet");
379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300382static int vidioc_g_tuner(struct file *file, void *priv,
383 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300385 v->type = V4L2_TUNER_RADIO;
386 switch (v->index) {
387 case 0:
388 strcpy(v->name, "FM");
389 v->capability = V4L2_TUNER_CAP_STEREO;
390 v->rangelow = 1400; /* 87.5 MHz */
391 v->rangehigh = 1728; /* 108.0 MHz */
392 v->rxsubchans=cadet_getstereo();
393 switch (v->rxsubchans){
394 case V4L2_TUNER_SUB_MONO:
395 v->audmode = V4L2_TUNER_MODE_MONO;
396 break;
397 case V4L2_TUNER_SUB_STEREO:
398 v->audmode = V4L2_TUNER_MODE_STEREO;
399 break;
400 default: ;
401 }
402 break;
403 case 1:
404 strcpy(v->name, "AM");
405 v->capability = V4L2_TUNER_CAP_LOW;
406 v->rangelow = 8320; /* 520 kHz */
407 v->rangehigh = 26400; /* 1650 kHz */
408 v->rxsubchans = V4L2_TUNER_SUB_MONO;
409 v->audmode = V4L2_TUNER_MODE_MONO;
410 break;
411 default:
412 return -EINVAL;
413 }
414 v->signal = sigstrength; /* We might need to modify scaling of this */
415 return 0;
416}
417
418static int vidioc_s_tuner(struct file *file, void *priv,
419 struct v4l2_tuner *v)
420{
421 if((v->index != 0)&&(v->index != 1))
422 return -EINVAL;
423 curtuner = v->index;
424 return 0;
425}
426
427static int vidioc_g_frequency(struct file *file, void *priv,
428 struct v4l2_frequency *f)
429{
430 f->tuner = curtuner;
431 f->type = V4L2_TUNER_RADIO;
432 f->frequency = cadet_getfreq();
433 return 0;
434}
435
436
437static int vidioc_s_frequency(struct file *file, void *priv,
438 struct v4l2_frequency *f)
439{
440 if (f->type != V4L2_TUNER_RADIO)
441 return -EINVAL;
442 if((curtuner==0)&&((f->frequency<1400)||(f->frequency>1728)))
443 return -EINVAL;
444 if((curtuner==1)&&((f->frequency<8320)||(f->frequency>26400)))
445 return -EINVAL;
446 cadet_setfreq(f->frequency);
447 return 0;
448}
449
450static int vidioc_queryctrl(struct file *file, void *priv,
451 struct v4l2_queryctrl *qc)
452{
453 int i;
454
455 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
456 if (qc->id && qc->id == radio_qctrl[i].id) {
457 memcpy(qc, &(radio_qctrl[i]),
458 sizeof(*qc));
459 return 0;
460 }
461 }
462 return -EINVAL;
463}
464
465static int vidioc_g_ctrl(struct file *file, void *priv,
466 struct v4l2_control *ctrl)
467{
468 switch (ctrl->id){
469 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
470 ctrl->value = (cadet_getvol() == 0);
471 break;
472 case V4L2_CID_AUDIO_VOLUME:
473 ctrl->value = cadet_getvol();
474 break;
475 default:
476 return -EINVAL;
477 }
478 return 0;
479}
480
481static int vidioc_s_ctrl(struct file *file, void *priv,
482 struct v4l2_control *ctrl)
483{
484 switch (ctrl->id){
485 case V4L2_CID_AUDIO_MUTE: /* TODO: Handle this correctly */
486 if (ctrl->value)
487 cadet_setvol(0);
488 else
489 cadet_setvol(0xffff);
490 break;
491 case V4L2_CID_AUDIO_VOLUME:
492 cadet_setvol(ctrl->value);
493 break;
494 default:
495 return -EINVAL;
496 }
497 return 0;
498}
499
500static int vidioc_g_audio(struct file *file, void *priv,
501 struct v4l2_audio *a)
502{
503 if (a->index > 1)
504 return -EINVAL;
505 strcpy(a->name, "Radio");
506 a->capability = V4L2_AUDCAP_STEREO;
507 return 0;
508}
509
510static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
511{
512 *i = 0;
513 return 0;
514}
515
516static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
517{
518 if (i != 0)
519 return -EINVAL;
520 return 0;
521}
522
523static int vidioc_s_audio(struct file *file, void *priv,
524 struct v4l2_audio *a)
525{
526 if (a->index != 0)
527 return -EINVAL;
528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300531static int
532cadet_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 users++;
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300535 if (1 == users) init_waitqueue_head(&read_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537}
538
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300539static int
540cadet_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 users--;
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300543 if (0 == users){
544 del_timer_sync(&readtimer);
545 rdsstat=0;
546 }
547 return 0;
548}
549
550static unsigned int
551cadet_poll(struct file *file, struct poll_table_struct *wait)
552{
553 poll_wait(file,&read_queue,wait);
554 if(rdsin != rdsout)
555 return POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 0;
557}
558
559
Arjan van de Venfa027c22007-02-12 00:55:33 -0800560static const struct file_operations cadet_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 .owner = THIS_MODULE,
562 .open = cadet_open,
563 .release = cadet_release,
564 .read = cadet_read,
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300565 .ioctl = video_ioctl2,
Hans J. Kochc0c7fa02006-08-08 09:10:12 -0300566 .poll = cadet_poll,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300567#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200568 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 .llseek = no_llseek,
571};
572
Hans Verkuila3998102008-07-21 02:57:38 -0300573static const struct v4l2_ioctl_ops cadet_ioctl_ops = {
Douglas Landgrafc1c4fd32007-05-07 16:17:16 -0300574 .vidioc_querycap = vidioc_querycap,
575 .vidioc_g_tuner = vidioc_g_tuner,
576 .vidioc_s_tuner = vidioc_s_tuner,
577 .vidioc_g_frequency = vidioc_g_frequency,
578 .vidioc_s_frequency = vidioc_s_frequency,
579 .vidioc_queryctrl = vidioc_queryctrl,
580 .vidioc_g_ctrl = vidioc_g_ctrl,
581 .vidioc_s_ctrl = vidioc_s_ctrl,
582 .vidioc_g_audio = vidioc_g_audio,
583 .vidioc_s_audio = vidioc_s_audio,
584 .vidioc_g_input = vidioc_g_input,
585 .vidioc_s_input = vidioc_s_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586};
587
Hans Verkuila3998102008-07-21 02:57:38 -0300588static struct video_device cadet_radio = {
Hans Verkuila3998102008-07-21 02:57:38 -0300589 .name = "Cadet radio",
Hans Verkuila3998102008-07-21 02:57:38 -0300590 .fops = &cadet_fops,
591 .ioctl_ops = &cadet_ioctl_ops,
Hans Verkuilaa5e90a2008-08-23 06:23:55 -0300592 .release = video_device_release_empty,
Hans Verkuila3998102008-07-21 02:57:38 -0300593};
594
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300595#ifdef CONFIG_PNP
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597static struct pnp_device_id cadet_pnp_devices[] = {
598 /* ADS Cadet AM/FM Radio Card */
599 {.id = "MSM0c24", .driver_data = 0},
600 {.id = ""}
601};
602
603MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
604
605static int cadet_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id *dev_id)
606{
607 if (!dev)
608 return -ENODEV;
609 /* only support one device */
610 if (io > 0)
611 return -EBUSY;
612
613 if (!pnp_port_valid(dev, 0)) {
614 return -ENODEV;
615 }
616
617 io = pnp_port_start(dev, 0);
618
619 printk ("radio-cadet: PnP reports device at %#x\n", io);
620
621 return io;
622}
623
624static struct pnp_driver cadet_pnp_driver = {
625 .name = "radio-cadet",
626 .id_table = cadet_pnp_devices,
627 .probe = cadet_pnp_probe,
628 .remove = NULL,
629};
630
Bjorn Helgaas044dfc92008-03-31 21:21:48 -0300631#else
632static struct pnp_driver cadet_pnp_driver;
633#endif
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635static int cadet_probe(void)
636{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300637 static int iovals[8]={0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int i;
639
640 for(i=0;i<8;i++) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300641 io=iovals[i];
Alexey Dobriyanf1ac0462005-10-14 15:59:04 -0700642 if (request_region(io, 2, "cadet-probe")) {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300643 cadet_setfreq(1410);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if(cadet_getfreq()==1410) {
645 release_region(io, 2);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300646 return io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 release_region(io, 2);
649 }
650 }
651 return -1;
652}
653
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300654/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * io should only be set if the user has used something like
656 * isapnp (the userspace program) to initialize this card for us
657 */
658
659static int __init cadet_init(void)
660{
661 spin_lock_init(&cadet_io_lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * If a probe was requested then probe ISAPnP first (safest)
665 */
666 if (io < 0)
667 pnp_register_driver(&cadet_pnp_driver);
668 /*
669 * If that fails then probe unsafely if probe is requested
670 */
671 if(io < 0)
672 io = cadet_probe ();
673
674 /*
675 * Else we bail out
676 */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300677
678 if(io < 0) {
679#ifdef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
681#endif
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300682 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 if (!request_region(io,2,"cadet"))
685 goto fail;
Hans Verkuilcba99ae2008-09-03 17:11:58 -0300686 if (video_register_device(&cadet_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 release_region(io,2);
688 goto fail;
689 }
690 printk(KERN_INFO "ADS Cadet Radio Card at 0x%x\n",io);
691 return 0;
692fail:
693 pnp_unregister_driver(&cadet_pnp_driver);
694 return -1;
695}
696
697
698
699MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
700MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
701MODULE_LICENSE("GPL");
702
703module_param(io, int, 0);
704MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
705module_param(radio_nr, int, 0);
706
707static void __exit cadet_cleanup_module(void)
708{
709 video_unregister_device(&cadet_radio);
710 release_region(io,2);
711 pnp_unregister_driver(&cadet_pnp_driver);
712}
713
714module_init(cadet_init);
715module_exit(cadet_cleanup_module);
716