1 /* SF16FMR2 radio driver for Linux radio support
2 * heavily based on fmi driver...
3 * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
5 * Notes on the hardware
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
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)
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>
24 static struct semaphore lock;
30 # define debug_print(s) printk s
32 # define debug_print(s)
35 /* this should be static vars for module size */
39 int curvol; /* 0-65535, if not volume 0 or 65535 */
41 int stereo; /* card is producing stereo audio */
42 unsigned long curfreq; /* freq in kHz */
47 static int io = 0x384;
48 static int radio_nr = -1;
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
54 #define RSF16_ENCODE(x) ((x)/200+856)
55 #define RSF16_MINFREQ 87*16000
56 #define RSF16_MAXFREQ 108*16000
58 /* from radio-aimslab */
59 static void sleep_delay(unsigned long n)
61 unsigned d=n/(1000000U/HZ);
66 set_current_state(TASK_UNINTERRUPTIBLE);
71 static inline void wait(int n,int port)
73 for (;n;--n) inb(port);
76 static void outbits(int bits, unsigned int data, int nWait, int port)
80 bit = (data>>bits) & 1;
90 static inline void fmr2_mute(int port)
96 static inline void fmr2_unmute(int port)
102 static inline int fmr2_stereo_mode(int port)
108 debug_print((KERN_DEBUG "stereo: %d\n", n));
112 static int fmr2_product_info(struct fmr2_device *dev)
114 int n = inb(dev->port);
118 /* this should support volume set */
122 /* not volume (mine is 11) */
123 dev->card_type = (n==128)?11:0;
127 static inline int fmr2_getsigstr(struct fmr2_device *dev)
129 /* !!! work only if scanning freq */
130 int port = dev->port, res = 0xffff;
133 if (!(inb(port)&1)) res = 0;
134 debug_print((KERN_DEBUG "signal: %d\n", res));
138 /* set frequency and unmute card */
139 static int fmr2_setfreq(struct fmr2_device *dev)
141 int port = dev->port;
142 unsigned long freq = dev->curfreq;
146 /* 0x42 for mono output
147 * 0x102 forward scanning
148 * 0x182 scansione avanti
150 outbits(9,0x2,3,port);
151 outbits(16,RSF16_ENCODE(freq),2,port);
156 sleep_delay(110000LU);
158 /* NOTE if mute this stop radio
159 you must set freq on unmute */
160 dev->stereo = fmr2_stereo_mode(port);
164 /* !!! not tested, in my card this does't work !!! */
165 static int fmr2_setvolume(struct fmr2_device *dev)
167 int i,a,n, port = dev->port;
169 if (dev->card_type != 11) return 1;
171 switch( (dev->curvol+(1<<11)) >> 12 )
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;
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;
193 a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
203 a = ((0x18 >> i) & 1) << 6;
217 static int fmr2_do_ioctl(struct inode *inode, struct file *file,
218 unsigned int cmd, void *arg)
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));
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;
241 struct video_tuner *v = arg;
244 if(v->tuner) /* Only 1 tuner */
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;
252 v->flags |= VIDEO_AUDIO_MUTE;
253 v->mode=VIDEO_MODE_AUTO;
255 v->signal = fmr2_getsigstr(fmr2);
261 struct video_tuner *v = arg;
264 fmr2->flags = v->flags & VIDEO_TUNER_LOW;
269 unsigned long *freq = arg;
270 *freq = fmr2->curfreq;
271 if (!(fmr2->flags & VIDEO_TUNER_LOW))
277 unsigned long *freq = arg;
278 if (!(fmr2->flags & VIDEO_TUNER_LOW))
280 if ( *freq < RSF16_MINFREQ || *freq > RSF16_MAXFREQ )
282 /* rounding in steps of 200 to match th freq
285 fmr2->curfreq = (*freq/200)*200;
287 /* set card freq (if not muted) */
288 if (fmr2->curvol && !fmr2->mute)
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)
308 v->flags |= VIDEO_AUDIO_VOLUME;
310 v->volume = fmr2->curvol;
312 debug_print((KERN_DEBUG "Get flags %d vol %d\n", v->flags, v->volume));
317 struct video_audio *v = arg;
320 debug_print((KERN_DEBUG "Set flags %d vol %d\n", v->flags, v->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;
326 if (v->flags & VIDEO_AUDIO_MUTE)
329 if (fmr2->curvol && !fmr2->mute)
330 printk(KERN_DEBUG "unmute\n");
332 printk(KERN_DEBUG "mute\n");
335 if (fmr2->curvol && !fmr2->mute)
337 fmr2_setvolume(fmr2);
340 else fmr2_mute(fmr2->port);
346 struct video_unit *v = arg;
347 v->video=VIDEO_NO_UNIT;
348 v->vbi=VIDEO_NO_UNIT;
350 v->audio=0; /* How do we find out this??? */
351 v->teletext=VIDEO_NO_UNIT;
359 static int fmr2_ioctl(struct inode *inode, struct file *file,
360 unsigned int cmd, unsigned long arg)
362 return video_usercopy(inode, file, cmd, arg, fmr2_do_ioctl);
365 static struct fmr2_device fmr2_unit;
367 static struct file_operations fmr2_fops = {
368 .owner = THIS_MODULE,
369 .open = video_exclusive_open,
370 .release = video_exclusive_release,
375 static struct video_device fmr2_radio=
377 .owner = THIS_MODULE,
378 .name = "SF16FMR2 radio",
379 . type = VID_TYPE_TUNER,
380 .hardware = VID_HARDWARE_SF16FMR2,
384 static int __init fmr2_init(void)
387 fmr2_unit.curvol = 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;
397 if (request_region(io, 2, "sf16fmr2"))
399 printk(KERN_ERR "fmr2: port 0x%x already in use\n", io);
403 if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
405 release_region(io, 2);
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 */
414 fmr2_product_info(&fmr2_unit);
416 debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
420 MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
421 MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
422 MODULE_LICENSE("GPL");
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");
428 static void __exit fmr2_cleanup_module(void)
430 video_unregister_device(&fmr2_radio);
431 release_region(io,2);
434 module_init(fmr2_init);
435 module_exit(fmr2_cleanup_module);
439 static int __init fmr2_setup_io(char *str)
441 get_option(&str, &io);
445 __setup("sf16fmr2=", fmr2_setup_io);