ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / radio / radio-sf16fmr2.c
1 /* SF16FMR2 radio driver for Linux radio support
2  * heavily based on fmi driver...
3  * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
4  *
5  * Notes on the hardware
6  *
7  *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
8  *  No volume control - only mute/unmute - you have to use line volume
9  *
10  *  For read stereo/mono you must wait 0.1 sec after set frequency and
11  *  card unmuted so I set frequency on unmute
12  *  Signal handling seem to work only on autoscanning (not implemented)
13  */
14
15 #include <linux/module.h>       /* Modules                      */
16 #include <linux/init.h>         /* Initdata                     */
17 #include <linux/ioport.h>       /* check_region, request_region */
18 #include <linux/delay.h>        /* udelay                       */
19 #include <asm/io.h>             /* outb, outb_p                 */
20 #include <asm/uaccess.h>        /* copy to/from user            */
21 #include <linux/videodev.h>     /* kernel radio structs         */
22 #include <asm/semaphore.h>
23
24 static struct semaphore lock;
25
26 #undef DEBUG
27 //#define DEBUG 1
28
29 #ifdef DEBUG
30 # define  debug_print(s) printk s
31 #else
32 # define  debug_print(s)
33 #endif
34
35 /* this should be static vars for module size */
36 struct fmr2_device
37 {
38         int port;
39         int curvol; /* 0-65535, if not volume 0 or 65535 */
40         int mute;
41         int stereo; /* card is producing stereo audio */
42         unsigned long curfreq; /* freq in kHz */
43         int card_type;
44         __u32 flags;
45 };
46
47 static int io = 0x384;
48 static int radio_nr = -1;
49
50 /* hw precision is 12.5 kHz
51  * It is only usefull to give freq in intervall of 200 (=0.0125Mhz),
52  * other bits will be truncated
53  */
54 #define RSF16_ENCODE(x) ((x)/200+856)
55 #define RSF16_MINFREQ 87*16000
56 #define RSF16_MAXFREQ 108*16000
57
58 /* from radio-aimslab */
59 static void sleep_delay(unsigned long n)
60 {
61         unsigned d=n/(1000000U/HZ);
62         if (!d)
63                 udelay(n);
64         else
65         {
66                 set_current_state(TASK_UNINTERRUPTIBLE);
67                 schedule_timeout(d);
68         }
69 }
70
71 static inline void wait(int n,int port)
72 {
73         for (;n;--n) inb(port);
74 }
75
76 static void outbits(int bits, unsigned int data, int nWait, int port)
77 {
78         int bit;
79         for(;--bits>=0;) {
80                 bit = (data>>bits) & 1;
81                 outb(bit,port);
82                 wait(nWait,port);
83                 outb(bit|2,port);
84                 wait(nWait,port);
85                 outb(bit,port);
86                 wait(nWait,port);
87         }
88 }
89
90 static inline void fmr2_mute(int port)
91 {
92         outb(0x00, port);
93         wait(4,port);
94 }
95
96 static inline void fmr2_unmute(int port)
97 {
98         outb(0x04, port);
99         wait(4,port);
100 }
101
102 static inline int fmr2_stereo_mode(int port)
103 {
104         int n = inb(port);
105         outb(6,port);
106         inb(port);
107         n = ((n>>3)&1)^1;
108         debug_print((KERN_DEBUG "stereo: %d\n", n));
109         return n;
110 }
111
112 static int fmr2_product_info(struct fmr2_device *dev)
113 {
114         int n = inb(dev->port);
115         n &= 0xC1;
116         if (n == 0)
117         {
118                 /* this should support volume set */
119                 dev->card_type = 12;
120                 return 0;
121         }
122         /* not volume (mine is 11) */
123         dev->card_type = (n==128)?11:0;
124         return n;
125 }
126
127 static inline int fmr2_getsigstr(struct fmr2_device *dev)
128 {
129         /* !!! work only if scanning freq */
130         int port = dev->port, res = 0xffff;
131         outb(5,port);
132         wait(4,port);
133         if (!(inb(port)&1)) res = 0;
134         debug_print((KERN_DEBUG "signal: %d\n", res));
135         return res;
136 }
137
138 /* set frequency and unmute card */
139 static int fmr2_setfreq(struct fmr2_device *dev)
140 {
141         int port = dev->port;
142         unsigned long freq = dev->curfreq;
143
144         fmr2_mute(port);
145
146         /* 0x42 for mono output
147          * 0x102 forward scanning
148          * 0x182 scansione avanti
149          */
150         outbits(9,0x2,3,port);
151         outbits(16,RSF16_ENCODE(freq),2,port);
152
153         fmr2_unmute(port);
154
155         /* wait 0.11 sec */
156         sleep_delay(110000LU);
157
158         /* NOTE if mute this stop radio
159            you must set freq on unmute */
160         dev->stereo = fmr2_stereo_mode(port);
161         return 0;
162 }
163
164 /* !!! not tested, in my card this does't work !!! */
165 static int fmr2_setvolume(struct fmr2_device *dev)
166 {
167         int i,a,n, port = dev->port;
168
169         if (dev->card_type != 11) return 1;
170
171         switch( (dev->curvol+(1<<11)) >> 12 )
172         {
173         case 0: case 1: n = 0x21; break;
174         case 2: n = 0x84; break;
175         case 3: n = 0x90; break;
176         case 4: n = 0x104; break;
177         case 5: n = 0x110; break;
178         case 6: n = 0x204; break;
179         case 7: n = 0x210; break;
180         case 8: n = 0x402; break;
181         case 9: n = 0x404; break;
182         default:
183         case 10: n = 0x408; break;
184         case 11: n = 0x410; break;
185         case 12: n = 0x801; break;
186         case 13: n = 0x802; break;
187         case 14: n = 0x804; break;
188         case 15: n = 0x808; break;
189         case 16: n = 0x810; break;
190         }
191         for(i=12;--i>=0;)
192         {
193                 a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
194                 outb(a|4, port);
195                 wait(4,port);
196                 outb(a|0x24, port);
197                 wait(4,port);
198                 outb(a|4, port);
199                 wait(4,port);
200         }
201         for(i=6;--i>=0;)
202         {
203                 a = ((0x18 >> i) & 1) << 6;
204                 outb(a|4, port);
205                 wait(4,port);
206                 outb(a|0x24, port);
207                 wait(4,port);
208                 outb(a|4, port);
209                 wait(4,port);
210         }
211         wait(4,port);
212         outb(0x14, port);
213
214         return 0;
215 }
216
217 static int fmr2_do_ioctl(struct inode *inode, struct file *file,
218                       unsigned int cmd, void *arg)
219 {
220         struct video_device *dev = video_devdata(file);
221         struct fmr2_device *fmr2 = dev->priv;
222         debug_print((KERN_DEBUG "freq %ld flags %d vol %d mute %d "
223                 "stereo %d type %d\n",
224                 fmr2->curfreq, fmr2->flags, fmr2->curvol, fmr2->mute,
225                 fmr2->stereo, fmr2->card_type));
226
227         switch(cmd)
228         {
229                 case VIDIOCGCAP:
230                 {
231                         struct video_capability *v = arg;
232                         memset(v,0,sizeof(*v));
233                         strcpy(v->name, "SF16-FMR2 radio");
234                         v->type=VID_TYPE_TUNER;
235                         v->channels=1;
236                         v->audios=1;
237                         return 0;
238                 }
239                 case VIDIOCGTUNER:
240                 {
241                         struct video_tuner *v = arg;
242                         int mult;
243
244                         if(v->tuner)     /* Only 1 tuner */
245                                 return -EINVAL;
246                         strcpy(v->name, "FM");
247                         mult = (fmr2->flags & VIDEO_TUNER_LOW) ? 1 : 1000;
248                         v->rangelow = RSF16_MINFREQ/mult;
249                         v->rangehigh = RSF16_MAXFREQ/mult;
250                         v->flags = fmr2->flags | VIDEO_AUDIO_MUTABLE;
251                         if (fmr2->mute)
252                                 v->flags |= VIDEO_AUDIO_MUTE;
253                         v->mode=VIDEO_MODE_AUTO;
254                         down(&lock);
255                         v->signal = fmr2_getsigstr(fmr2);
256                         up(&lock);
257                         return 0;
258                 }
259                 case VIDIOCSTUNER:
260                 {
261                         struct video_tuner *v = arg;
262                         if (v->tuner!=0)
263                                 return -EINVAL;
264                         fmr2->flags = v->flags & VIDEO_TUNER_LOW;
265                         return 0;
266                 }
267                 case VIDIOCGFREQ:
268                 {
269                         unsigned long *freq = arg;
270                         *freq = fmr2->curfreq;
271                         if (!(fmr2->flags & VIDEO_TUNER_LOW))
272                                 *freq /= 1000;
273                         return 0;
274                 }
275                 case VIDIOCSFREQ:
276                 {
277                         unsigned long *freq = arg;
278                         if (!(fmr2->flags & VIDEO_TUNER_LOW))
279                                 *freq *= 1000;
280                         if ( *freq < RSF16_MINFREQ || *freq > RSF16_MAXFREQ )
281                                 return -EINVAL;
282                         /* rounding in steps of 200 to match th freq
283                          * that will be used
284                          */
285                         fmr2->curfreq = (*freq/200)*200;
286
287                         /* set card freq (if not muted) */
288                         if (fmr2->curvol && !fmr2->mute)
289                         {
290                                 down(&lock);
291                                 fmr2_setfreq(fmr2);
292                                 up(&lock);
293                         }
294                         return 0;
295                 }
296                 case VIDIOCGAUDIO:
297                 {
298                         struct video_audio *v = arg;
299                         memset(v,0,sizeof(*v));
300                         /* !!! do not return VIDEO_AUDIO_MUTE */
301                         v->flags = VIDEO_AUDIO_MUTABLE;
302                         strcpy(v->name, "Radio");
303                         /* get current stereo mode */
304                         v->mode = fmr2->stereo ? VIDEO_SOUND_STEREO: VIDEO_SOUND_MONO;
305                         /* volume supported ? */
306                         if (fmr2->card_type == 11)
307                         {
308                                 v->flags |= VIDEO_AUDIO_VOLUME;
309                                 v->step = 1 << 12;
310                                 v->volume = fmr2->curvol;
311                         }
312                         debug_print((KERN_DEBUG "Get flags %d vol %d\n", v->flags, v->volume));
313                         return 0;
314                 }
315                 case VIDIOCSAUDIO:
316                 {
317                         struct video_audio *v = arg;
318                         if(v->audio)
319                                 return -EINVAL;
320                         debug_print((KERN_DEBUG "Set flags %d vol %d\n", v->flags, v->volume));
321                         /* set volume */
322                         if (v->flags & VIDEO_AUDIO_VOLUME)
323                                 fmr2->curvol = v->volume; /* !!! set with precision */
324                         if (fmr2->card_type != 11) fmr2->curvol = 65535;
325                         fmr2->mute = 0;
326                         if (v->flags & VIDEO_AUDIO_MUTE)
327                                 fmr2->mute = 1;
328 #ifdef DEBUG
329                         if (fmr2->curvol && !fmr2->mute)
330                                 printk(KERN_DEBUG "unmute\n");
331                         else
332                                 printk(KERN_DEBUG "mute\n");
333 #endif
334                         down(&lock);
335                         if (fmr2->curvol && !fmr2->mute)
336                         {
337                                 fmr2_setvolume(fmr2);
338                                 fmr2_setfreq(fmr2);
339                         }
340                         else fmr2_mute(fmr2->port);
341                         up(&lock);
342                         return 0;
343                 }
344                 case VIDIOCGUNIT:
345                 {
346                         struct video_unit *v = arg;
347                         v->video=VIDEO_NO_UNIT;
348                         v->vbi=VIDEO_NO_UNIT;
349                         v->radio=dev->minor;
350                         v->audio=0; /* How do we find out this??? */
351                         v->teletext=VIDEO_NO_UNIT;
352                         return 0;
353                 }
354                 default:
355                         return -ENOIOCTLCMD;
356         }
357 }
358
359 static int fmr2_ioctl(struct inode *inode, struct file *file,
360                       unsigned int cmd, unsigned long arg)
361  {
362         return video_usercopy(inode, file, cmd, arg, fmr2_do_ioctl);
363 }
364
365 static struct fmr2_device fmr2_unit;
366
367 static struct file_operations fmr2_fops = {
368         .owner          = THIS_MODULE,
369         .open           = video_exclusive_open,
370         .release        = video_exclusive_release,
371         .ioctl          = fmr2_ioctl,
372         .llseek         = no_llseek,
373 };
374
375 static struct video_device fmr2_radio=
376 {
377         .owner          = THIS_MODULE,
378         .name           = "SF16FMR2 radio",
379         . type          = VID_TYPE_TUNER,
380         .hardware       = VID_HARDWARE_SF16FMR2,
381         .fops           = &fmr2_fops,
382 };
383
384 static int __init fmr2_init(void)
385 {
386         fmr2_unit.port = io;
387         fmr2_unit.curvol = 0;
388         fmr2_unit.mute = 0;
389         fmr2_unit.curfreq = 0;
390         fmr2_unit.stereo = 1;
391         fmr2_unit.flags = VIDEO_TUNER_LOW;
392         fmr2_unit.card_type = 0;
393         fmr2_radio.priv = &fmr2_unit;
394
395         init_MUTEX(&lock);
396
397         if (request_region(io, 2, "sf16fmr2"))
398         {
399                 printk(KERN_ERR "fmr2: port 0x%x already in use\n", io);
400                 return -EBUSY;
401         }
402
403         if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
404         {
405                 release_region(io, 2);
406                 return -EINVAL;
407         }
408
409         printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
410         debug_print((KERN_DEBUG "Mute %d Low %d\n",VIDEO_AUDIO_MUTE,VIDEO_TUNER_LOW));
411         /* mute card - prevents noisy bootups */
412         down(&lock);
413         fmr2_mute(io);
414         fmr2_product_info(&fmr2_unit);
415         up(&lock);
416         debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
417         return 0;
418 }
419
420 MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
421 MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
422 MODULE_LICENSE("GPL");
423
424 MODULE_PARM(io, "i");
425 MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
426 MODULE_PARM(radio_nr, "i");
427
428 static void __exit fmr2_cleanup_module(void)
429 {
430         video_unregister_device(&fmr2_radio);
431         release_region(io,2);
432 }
433
434 module_init(fmr2_init);
435 module_exit(fmr2_cleanup_module);
436
437 #ifndef MODULE
438
439 static int __init fmr2_setup_io(char *str)
440 {
441         get_option(&str, &io);
442         return 1;
443 }
444
445 __setup("sf16fmr2=", fmr2_setup_io);
446
447 #endif