Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 11 | * Jason Lewis (jlewis@twilight.vtc.vsc.edu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 28 | * |
| 29 | * 2006-07-30 Hans J. Koch <koch@hjk-az.de> |
| 30 | * Changed API to V4L2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #include <linux/module.h> /* Modules */ |
| 34 | #include <linux/init.h> /* Initdata */ |
Peter Osterlund | fb911ee | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 35 | #include <linux/ioport.h> /* request_region */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/delay.h> /* udelay */ |
| 37 | #include <asm/io.h> /* outb, outb_p */ |
| 38 | #include <asm/uaccess.h> /* copy to/from user */ |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 39 | #include <linux/videodev2.h> /* V4L2 API defs */ |
Mauro Carvalho Chehab | 5e87efa | 2006-06-05 10:26:32 -0300 | [diff] [blame] | 40 | #include <media/v4l2-common.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/param.h> |
| 42 | #include <linux/pnp.h> |
| 43 | |
| 44 | #define RDS_BUFFER 256 |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 45 | #define RDS_RX_FLAG 1 |
| 46 | #define MBS_RX_FLAG 2 |
| 47 | |
| 48 | #define CADET_VERSION KERNEL_VERSION(0,3,3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | static int io=-1; /* default to isapnp activation */ |
| 51 | static int radio_nr = -1; |
| 52 | static int users=0; |
| 53 | static int curtuner=0; |
| 54 | static int tunestat=0; |
| 55 | static int sigstrength=0; |
| 56 | static wait_queue_head_t read_queue; |
| 57 | static struct timer_list readtimer; |
| 58 | static __u8 rdsin=0,rdsout=0,rdsstat=0; |
| 59 | static unsigned char rdsbuf[RDS_BUFFER]; |
| 60 | static spinlock_t cadet_io_lock; |
| 61 | |
| 62 | static int cadet_probe(void); |
| 63 | |
| 64 | /* |
| 65 | * Signal Strength Threshold Values |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 66 | * The V4L API spec does not define any particular unit for the signal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * strength value. These values are in microvolts of RF at the tuner's input. |
| 68 | */ |
| 69 | static __u16 sigtable[2][4]={{5,10,30,150},{28,40,63,1000}}; |
| 70 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 71 | |
| 72 | static int |
| 73 | cadet_getstereo(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 75 | int ret = V4L2_TUNER_SUB_MONO; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 76 | if(curtuner != 0) /* Only FM has stereo capability! */ |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 77 | return V4L2_TUNER_SUB_MONO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | spin_lock(&cadet_io_lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 80 | outb(7,io); /* Select tuner control */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | if( (inb(io+1) & 0x40) == 0) |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 82 | ret = V4L2_TUNER_SUB_STEREO; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 83 | spin_unlock(&cadet_io_lock); |
| 84 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 87 | static unsigned |
| 88 | cadet_gettune(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 90 | int curvol,i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | unsigned fifo=0; |
| 92 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 93 | /* |
| 94 | * Prepare for read |
| 95 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | spin_lock(&cadet_io_lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | tunestat=0xffff; |
| 103 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | tunestat&=inb(io+1); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 112 | outb(0x00,io+1); |
| 113 | } |
| 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 116 | /* |
| 117 | * Restore volume/mute setting |
| 118 | */ |
| 119 | outb(curvol,io+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | spin_unlock(&cadet_io_lock); |
| 121 | |
| 122 | return fifo; |
| 123 | } |
| 124 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 125 | static unsigned |
| 126 | cadet_getfreq(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 128 | int i; |
| 129 | unsigned freq=0,test,fifo=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Read current tuning |
| 133 | */ |
| 134 | fifo=cadet_gettune(); |
| 135 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 136 | /* |
| 137 | * Convert to actual frequency |
| 138 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if(curtuner==0) { /* FM */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | if(curtuner==1) { /* AM */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 152 | freq=((fifo&0x7fff)-2010)*16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 155 | return freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 158 | static void |
| 159 | cadet_settune(unsigned fifo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 161 | int i; |
| 162 | unsigned test; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | spin_lock(&cadet_io_lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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++) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 176 | test|=0x01; /* Toggle SCK High */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 187 | static void |
| 188 | cadet_setfreq(unsigned freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 190 | unsigned fifo; |
| 191 | int i,j,test; |
| 192 | int curvol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 194 | /* |
| 195 | * Formulate a fifo command |
| 196 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | fifo=0; |
| 198 | if(curtuner==0) { /* FM */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | if(curtuner==1) { /* AM */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 212 | fifo=(freq/16)+2010; /* Make it kHz */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | fifo|=0x100000; /* Select AM Band */ |
| 214 | } |
| 215 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 216 | /* |
| 217 | * Save current volume/mute setting |
| 218 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | spin_lock(&cadet_io_lock); |
| 221 | outb(7,io); /* Select tuner control */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 222 | curvol=inb(io+1); |
| 223 | spin_unlock(&cadet_io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * Tune the card |
| 227 | */ |
| 228 | for(j=3;j>-1;j--) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 229 | cadet_settune(fifo|(j<<16)); |
| 230 | |
| 231 | spin_lock(&cadet_io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | outb(7,io); /* Select tuner control */ |
| 233 | outb(curvol,io+1); |
| 234 | spin_unlock(&cadet_io_lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | msleep(100); |
| 237 | |
| 238 | cadet_gettune(); |
| 239 | if((tunestat & 0x40) == 0) { /* Tuned */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 240 | sigstrength=sigtable[curtuner][j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | } |
| 244 | sigstrength=0; |
| 245 | } |
| 246 | |
| 247 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 248 | static int |
| 249 | cadet_getvol(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | int ret = 0; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | spin_lock(&cadet_io_lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 264 | static void |
| 265 | cadet_setvol(int vol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
| 267 | spin_lock(&cadet_io_lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 268 | outb(7,io); /* Select tuner control */ |
| 269 | if(vol>0) |
| 270 | outb(0x20,io+1); |
| 271 | else |
| 272 | outb(0x00,io+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | spin_unlock(&cadet_io_lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 276 | static void |
| 277 | cadet_handler(unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
| 279 | /* |
| 280 | * Service the RDS fifo |
| 281 | */ |
| 282 | |
| 283 | if(spin_trylock(&cadet_io_lock)) |
| 284 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 285 | outb(0x3,io); /* Select RDS Decoder Control */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if((inb(io+1)&0x20)!=0) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 287 | printk(KERN_CRIT "cadet: RDS fifo overflow\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | outb(0x80,io); /* Select RDS fifo */ |
| 290 | while((inb(io)&0x80)!=0) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 291 | rdsbuf[rdsin]=inb(io+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if(rdsin==rdsout) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 293 | printk(KERN_WARNING "cadet: RDS buffer overflow\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | else |
| 295 | rdsin++; |
| 296 | } |
| 297 | spin_unlock(&cadet_io_lock); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Service pending read |
| 302 | */ |
| 303 | if( rdsin!=rdsout) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 304 | wake_up_interruptible(&read_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 306 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 318 | static ssize_t |
| 319 | cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 321 | int i=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | unsigned char readbuf[RDS_BUFFER]; |
| 323 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 324 | if(rdsstat==0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | spin_lock(&cadet_io_lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 326 | rdsstat=1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 336 | if (file->f_flags & O_NONBLOCK) |
| 337 | return -EWOULDBLOCK; |
| 338 | interruptible_sleep_on(&read_queue); |
| 339 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | while( i<count && rdsin!=rdsout) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 341 | readbuf[i++]=rdsbuf[rdsout++]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | if (copy_to_user(data,readbuf,i)) |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 344 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return i; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | |
| 350 | static int cadet_do_ioctl(struct inode *inode, struct file *file, |
| 351 | unsigned int cmd, void *arg) |
| 352 | { |
| 353 | switch(cmd) |
| 354 | { |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 355 | case VIDIOC_QUERYCAP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | return 0; |
| 366 | } |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 367 | case VIDIOC_G_TUNER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 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; |
| 398 | } |
| 399 | |
| 400 | t->signal = sigstrength; /* We might need to modify scaling of this */ |
| 401 | return 0; |
| 402 | } |
| 403 | case VIDIOC_S_TUNER: |
| 404 | { |
| 405 | struct v4l2_tuner *t = arg; |
| 406 | if((t->index != 0)&&(t->index != 1)) |
| 407 | return -EINVAL; |
| 408 | |
| 409 | curtuner = t->index; |
| 410 | return 0; |
| 411 | } |
| 412 | case VIDIOC_G_FREQUENCY: |
| 413 | { |
| 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(); |
| 419 | return 0; |
| 420 | } |
| 421 | case VIDIOC_S_FREQUENCY: |
| 422 | { |
| 423 | struct v4l2_frequency *f = arg; |
| 424 | if (f->type != V4L2_TUNER_RADIO){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return -EINVAL; |
| 426 | } |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 427 | if((curtuner==0)&&((f->frequency<1400)||(f->frequency>1728))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | return -EINVAL; |
| 429 | } |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 430 | if((curtuner==1)&&((f->frequency<8320)||(f->frequency>26400))) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 431 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 433 | cadet_setfreq(f->frequency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return 0; |
| 435 | } |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 436 | case VIDIOC_G_CTRL: |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 437 | { |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 449 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 451 | case VIDIOC_S_CTRL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | return 0; |
| 466 | } |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | default: |
| 469 | return -ENOIOCTLCMD; |
| 470 | } |
| 471 | } |
| 472 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 473 | static int |
| 474 | cadet_ioctl(struct inode *inode, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | unsigned int cmd, unsigned long arg) |
| 476 | { |
| 477 | return video_usercopy(inode, file, cmd, arg, cadet_do_ioctl); |
| 478 | } |
| 479 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 480 | static int |
| 481 | cadet_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | users++; |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 484 | if (1 == users) init_waitqueue_head(&read_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 488 | static int |
| 489 | cadet_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | users--; |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 492 | if (0 == users){ |
| 493 | del_timer_sync(&readtimer); |
| 494 | rdsstat=0; |
| 495 | } |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static unsigned int |
| 500 | cadet_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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | static 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, |
Hans J. Koch | c0c7fa0 | 2006-08-08 09:10:12 -0300 | [diff] [blame^] | 515 | .poll = cadet_poll, |
Arnd Bergmann | 0d0fbf8 | 2006-01-09 15:24:57 -0200 | [diff] [blame] | 516 | .compat_ioctl = v4l_compat_ioctl32, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | .llseek = no_llseek, |
| 518 | }; |
| 519 | |
| 520 | static struct video_device cadet_radio= |
| 521 | { |
| 522 | .owner = THIS_MODULE, |
| 523 | .name = "Cadet radio", |
| 524 | .type = VID_TYPE_TUNER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | .fops = &cadet_fops, |
| 526 | }; |
| 527 | |
| 528 | static struct pnp_device_id cadet_pnp_devices[] = { |
| 529 | /* ADS Cadet AM/FM Radio Card */ |
| 530 | {.id = "MSM0c24", .driver_data = 0}, |
| 531 | {.id = ""} |
| 532 | }; |
| 533 | |
| 534 | MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices); |
| 535 | |
| 536 | static 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 | |
| 555 | static 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 | |
| 562 | static int cadet_probe(void) |
| 563 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 564 | static int iovals[8]={0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | int i; |
| 566 | |
| 567 | for(i=0;i<8;i++) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 568 | io=iovals[i]; |
Alexey Dobriyan | f1ac046 | 2005-10-14 15:59:04 -0700 | [diff] [blame] | 569 | if (request_region(io, 2, "cadet-probe")) { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 570 | cadet_setfreq(1410); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | if(cadet_getfreq()==1410) { |
| 572 | release_region(io, 2); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 573 | return io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | release_region(io, 2); |
| 576 | } |
| 577 | } |
| 578 | return -1; |
| 579 | } |
| 580 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 581 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 586 | static int __init cadet_init(void) |
| 587 | { |
| 588 | spin_lock_init(&cadet_io_lock); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 604 | |
| 605 | if(io < 0) { |
| 606 | #ifdef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | printk(KERN_ERR "You must set an I/O address with io=0x???\n"); |
| 608 | #endif |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 609 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 619 | fail: |
| 620 | pnp_unregister_driver(&cadet_pnp_driver); |
| 621 | return -1; |
| 622 | } |
| 623 | |
| 624 | |
| 625 | |
| 626 | MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath"); |
| 627 | MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card."); |
| 628 | MODULE_LICENSE("GPL"); |
| 629 | |
| 630 | module_param(io, int, 0); |
| 631 | MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)"); |
| 632 | module_param(radio_nr, int, 0); |
| 633 | |
| 634 | static 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 | |
| 641 | module_init(cadet_init); |
| 642 | module_exit(cadet_cleanup_module); |
| 643 | |