blob: bcebd8cb19ad43e0accfb4aa97088d27781837d1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070017#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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 */
Ingo Molnar3593cab2006-02-07 06:49:14 -020022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ingo Molnar3593cab2006-02-07 06:49:14 -020024static struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
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 */
36struct 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
47static int io = 0x384;
48static int radio_nr = -1;
49
50/* hw precision is 12.5 kHz
Adrian Bunka58a4142006-01-10 00:08:17 +010051 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * 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
58static inline void wait(int n,int port)
59{
60 for (;n;--n) inb(port);
61}
62
63static void outbits(int bits, unsigned int data, int nWait, int port)
64{
65 int bit;
66 for(;--bits>=0;) {
67 bit = (data>>bits) & 1;
68 outb(bit,port);
69 wait(nWait,port);
70 outb(bit|2,port);
71 wait(nWait,port);
72 outb(bit,port);
73 wait(nWait,port);
74 }
75}
76
77static inline void fmr2_mute(int port)
78{
79 outb(0x00, port);
80 wait(4,port);
81}
82
83static inline void fmr2_unmute(int port)
84{
85 outb(0x04, port);
86 wait(4,port);
87}
88
89static inline int fmr2_stereo_mode(int port)
90{
91 int n = inb(port);
92 outb(6,port);
93 inb(port);
94 n = ((n>>3)&1)^1;
95 debug_print((KERN_DEBUG "stereo: %d\n", n));
96 return n;
97}
98
99static int fmr2_product_info(struct fmr2_device *dev)
100{
101 int n = inb(dev->port);
102 n &= 0xC1;
103 if (n == 0)
104 {
105 /* this should support volume set */
106 dev->card_type = 12;
107 return 0;
108 }
109 /* not volume (mine is 11) */
110 dev->card_type = (n==128)?11:0;
111 return n;
112}
113
114static inline int fmr2_getsigstr(struct fmr2_device *dev)
115{
116 /* !!! work only if scanning freq */
117 int port = dev->port, res = 0xffff;
118 outb(5,port);
119 wait(4,port);
120 if (!(inb(port)&1)) res = 0;
121 debug_print((KERN_DEBUG "signal: %d\n", res));
122 return res;
123}
124
125/* set frequency and unmute card */
126static int fmr2_setfreq(struct fmr2_device *dev)
127{
128 int port = dev->port;
129 unsigned long freq = dev->curfreq;
130
131 fmr2_mute(port);
132
133 /* 0x42 for mono output
134 * 0x102 forward scanning
135 * 0x182 scansione avanti
136 */
137 outbits(9,0x2,3,port);
138 outbits(16,RSF16_ENCODE(freq),2,port);
139
140 fmr2_unmute(port);
141
142 /* wait 0.11 sec */
143 msleep(110);
144
145 /* NOTE if mute this stop radio
146 you must set freq on unmute */
147 dev->stereo = fmr2_stereo_mode(port);
148 return 0;
149}
150
151/* !!! not tested, in my card this does't work !!! */
152static int fmr2_setvolume(struct fmr2_device *dev)
153{
154 int i,a,n, port = dev->port;
155
156 if (dev->card_type != 11) return 1;
157
158 switch( (dev->curvol+(1<<11)) >> 12 )
159 {
160 case 0: case 1: n = 0x21; break;
161 case 2: n = 0x84; break;
162 case 3: n = 0x90; break;
163 case 4: n = 0x104; break;
164 case 5: n = 0x110; break;
165 case 6: n = 0x204; break;
166 case 7: n = 0x210; break;
167 case 8: n = 0x402; break;
168 case 9: n = 0x404; break;
169 default:
170 case 10: n = 0x408; break;
171 case 11: n = 0x410; break;
172 case 12: n = 0x801; break;
173 case 13: n = 0x802; break;
174 case 14: n = 0x804; break;
175 case 15: n = 0x808; break;
176 case 16: n = 0x810; break;
177 }
178 for(i=12;--i>=0;)
179 {
180 a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
181 outb(a|4, port);
182 wait(4,port);
183 outb(a|0x24, port);
184 wait(4,port);
185 outb(a|4, port);
186 wait(4,port);
187 }
188 for(i=6;--i>=0;)
189 {
190 a = ((0x18 >> i) & 1) << 6;
191 outb(a|4, port);
192 wait(4,port);
193 outb(a|0x24, port);
194 wait(4,port);
195 outb(a|4, port);
196 wait(4,port);
197 }
198 wait(4,port);
199 outb(0x14, port);
200
201 return 0;
202}
203
204static int fmr2_do_ioctl(struct inode *inode, struct file *file,
205 unsigned int cmd, void *arg)
206{
207 struct video_device *dev = video_devdata(file);
208 struct fmr2_device *fmr2 = dev->priv;
209 debug_print((KERN_DEBUG "freq %ld flags %d vol %d mute %d "
210 "stereo %d type %d\n",
211 fmr2->curfreq, fmr2->flags, fmr2->curvol, fmr2->mute,
212 fmr2->stereo, fmr2->card_type));
213
214 switch(cmd)
215 {
216 case VIDIOCGCAP:
217 {
218 struct video_capability *v = arg;
219 memset(v,0,sizeof(*v));
220 strcpy(v->name, "SF16-FMR2 radio");
221 v->type=VID_TYPE_TUNER;
222 v->channels=1;
223 v->audios=1;
224 return 0;
225 }
226 case VIDIOCGTUNER:
227 {
228 struct video_tuner *v = arg;
229 int mult;
230
231 if(v->tuner) /* Only 1 tuner */
232 return -EINVAL;
233 strcpy(v->name, "FM");
234 mult = (fmr2->flags & VIDEO_TUNER_LOW) ? 1 : 1000;
235 v->rangelow = RSF16_MINFREQ/mult;
236 v->rangehigh = RSF16_MAXFREQ/mult;
237 v->flags = fmr2->flags | VIDEO_AUDIO_MUTABLE;
238 if (fmr2->mute)
239 v->flags |= VIDEO_AUDIO_MUTE;
240 v->mode=VIDEO_MODE_AUTO;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200241 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 v->signal = fmr2_getsigstr(fmr2);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200243 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245 }
246 case VIDIOCSTUNER:
247 {
248 struct video_tuner *v = arg;
249 if (v->tuner!=0)
250 return -EINVAL;
251 fmr2->flags = v->flags & VIDEO_TUNER_LOW;
252 return 0;
253 }
254 case VIDIOCGFREQ:
255 {
256 unsigned long *freq = arg;
257 *freq = fmr2->curfreq;
258 if (!(fmr2->flags & VIDEO_TUNER_LOW))
259 *freq /= 1000;
260 return 0;
261 }
262 case VIDIOCSFREQ:
263 {
264 unsigned long *freq = arg;
265 if (!(fmr2->flags & VIDEO_TUNER_LOW))
266 *freq *= 1000;
267 if ( *freq < RSF16_MINFREQ || *freq > RSF16_MAXFREQ )
268 return -EINVAL;
269 /* rounding in steps of 200 to match th freq
270 * that will be used
271 */
272 fmr2->curfreq = (*freq/200)*200;
273
274 /* set card freq (if not muted) */
275 if (fmr2->curvol && !fmr2->mute)
276 {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200277 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 fmr2_setfreq(fmr2);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200279 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 return 0;
282 }
283 case VIDIOCGAUDIO:
284 {
285 struct video_audio *v = arg;
286 memset(v,0,sizeof(*v));
287 /* !!! do not return VIDEO_AUDIO_MUTE */
288 v->flags = VIDEO_AUDIO_MUTABLE;
289 strcpy(v->name, "Radio");
290 /* get current stereo mode */
291 v->mode = fmr2->stereo ? VIDEO_SOUND_STEREO: VIDEO_SOUND_MONO;
292 /* volume supported ? */
293 if (fmr2->card_type == 11)
294 {
295 v->flags |= VIDEO_AUDIO_VOLUME;
296 v->step = 1 << 12;
297 v->volume = fmr2->curvol;
298 }
299 debug_print((KERN_DEBUG "Get flags %d vol %d\n", v->flags, v->volume));
300 return 0;
301 }
302 case VIDIOCSAUDIO:
303 {
304 struct video_audio *v = arg;
305 if(v->audio)
306 return -EINVAL;
307 debug_print((KERN_DEBUG "Set flags %d vol %d\n", v->flags, v->volume));
308 /* set volume */
309 if (v->flags & VIDEO_AUDIO_VOLUME)
310 fmr2->curvol = v->volume; /* !!! set with precision */
311 if (fmr2->card_type != 11) fmr2->curvol = 65535;
312 fmr2->mute = 0;
313 if (v->flags & VIDEO_AUDIO_MUTE)
314 fmr2->mute = 1;
315#ifdef DEBUG
316 if (fmr2->curvol && !fmr2->mute)
317 printk(KERN_DEBUG "unmute\n");
318 else
319 printk(KERN_DEBUG "mute\n");
320#endif
Ingo Molnar3593cab2006-02-07 06:49:14 -0200321 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (fmr2->curvol && !fmr2->mute)
323 {
324 fmr2_setvolume(fmr2);
325 fmr2_setfreq(fmr2);
326 }
327 else fmr2_mute(fmr2->port);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200328 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330 }
331 case VIDIOCGUNIT:
332 {
333 struct video_unit *v = arg;
334 v->video=VIDEO_NO_UNIT;
335 v->vbi=VIDEO_NO_UNIT;
336 v->radio=dev->minor;
337 v->audio=0; /* How do we find out this??? */
338 v->teletext=VIDEO_NO_UNIT;
339 return 0;
340 }
341 default:
342 return -ENOIOCTLCMD;
343 }
344}
345
346static int fmr2_ioctl(struct inode *inode, struct file *file,
347 unsigned int cmd, unsigned long arg)
348 {
349 return video_usercopy(inode, file, cmd, arg, fmr2_do_ioctl);
350}
351
352static struct fmr2_device fmr2_unit;
353
354static struct file_operations fmr2_fops = {
355 .owner = THIS_MODULE,
356 .open = video_exclusive_open,
357 .release = video_exclusive_release,
358 .ioctl = fmr2_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200359 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 .llseek = no_llseek,
361};
362
363static struct video_device fmr2_radio=
364{
365 .owner = THIS_MODULE,
366 .name = "SF16FMR2 radio",
367 . type = VID_TYPE_TUNER,
368 .hardware = VID_HARDWARE_SF16FMR2,
369 .fops = &fmr2_fops,
370};
371
372static int __init fmr2_init(void)
373{
374 fmr2_unit.port = io;
375 fmr2_unit.curvol = 0;
376 fmr2_unit.mute = 0;
377 fmr2_unit.curfreq = 0;
378 fmr2_unit.stereo = 1;
379 fmr2_unit.flags = VIDEO_TUNER_LOW;
380 fmr2_unit.card_type = 0;
381 fmr2_radio.priv = &fmr2_unit;
382
Ingo Molnar3593cab2006-02-07 06:49:14 -0200383 mutex_init(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (request_region(io, 2, "sf16fmr2"))
386 {
387 printk(KERN_ERR "fmr2: port 0x%x already in use\n", io);
388 return -EBUSY;
389 }
390
391 if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
392 {
393 release_region(io, 2);
394 return -EINVAL;
395 }
396
397 printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
398 debug_print((KERN_DEBUG "Mute %d Low %d\n",VIDEO_AUDIO_MUTE,VIDEO_TUNER_LOW));
399 /* mute card - prevents noisy bootups */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200400 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 fmr2_mute(io);
402 fmr2_product_info(&fmr2_unit);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200403 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
405 return 0;
406}
407
408MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
409MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
410MODULE_LICENSE("GPL");
411
412module_param(io, int, 0);
413MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
414module_param(radio_nr, int, 0);
415
416static void __exit fmr2_cleanup_module(void)
417{
418 video_unregister_device(&fmr2_radio);
419 release_region(io,2);
420}
421
422module_init(fmr2_init);
423module_exit(fmr2_cleanup_module);
424
425#ifndef MODULE
426
427static int __init fmr2_setup_io(char *str)
428{
429 get_option(&str, &io);
430 return 1;
431}
432
433__setup("sf16fmr2=", fmr2_setup_io);
434
435#endif