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)
14 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
17 #include <linux/module.h> /* Modules */
18 #include <linux/init.h> /* Initdata */
19 #include <linux/ioport.h> /* request_region */
20 #include <linux/delay.h> /* udelay */
21 #include <asm/io.h> /* outb, outb_p */
22 #include <asm/uaccess.h> /* copy to/from user */
23 #include <linux/videodev2.h> /* kernel radio structs */
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ioctl.h>
26 #include <linux/mutex.h>
28 static struct mutex lock;
30 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
31 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
33 #define AUD_VOL_INDEX 1
35 static struct v4l2_queryctrl radio_qctrl[] = {
37 .id = V4L2_CID_AUDIO_MUTE,
42 .type = V4L2_CTRL_TYPE_BOOLEAN,
45 .id = V4L2_CID_AUDIO_VOLUME,
51 .type = V4L2_CTRL_TYPE_INTEGER,
59 # define debug_print(s) printk s
61 # define debug_print(s)
64 /* this should be static vars for module size */
69 int curvol; /* 0-15 */
71 int stereo; /* card is producing stereo audio */
72 unsigned long curfreq; /* freq in kHz */
77 static int io = 0x384;
78 static int radio_nr = -1;
80 /* hw precision is 12.5 kHz
81 * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
82 * other bits will be truncated
84 #define RSF16_ENCODE(x) ((x)/200+856)
85 #define RSF16_MINFREQ 87*16000
86 #define RSF16_MAXFREQ 108*16000
88 static inline void wait(int n,int port)
90 for (;n;--n) inb(port);
93 static void outbits(int bits, unsigned int data, int nWait, int port)
97 bit = (data>>bits) & 1;
107 static inline void fmr2_mute(int port)
113 static inline void fmr2_unmute(int port)
119 static inline int fmr2_stereo_mode(int port)
125 debug_print((KERN_DEBUG "stereo: %d\n", n));
129 static int fmr2_product_info(struct fmr2_device *dev)
131 int n = inb(dev->port);
135 /* this should support volume set */
139 /* not volume (mine is 11) */
140 dev->card_type = (n==128)?11:0;
144 static inline int fmr2_getsigstr(struct fmr2_device *dev)
146 /* !!! work only if scanning freq */
147 int port = dev->port, res = 0xffff;
150 if (!(inb(port)&1)) res = 0;
151 debug_print((KERN_DEBUG "signal: %d\n", res));
155 /* set frequency and unmute card */
156 static int fmr2_setfreq(struct fmr2_device *dev)
158 int port = dev->port;
159 unsigned long freq = dev->curfreq;
163 /* 0x42 for mono output
164 * 0x102 forward scanning
165 * 0x182 scansione avanti
167 outbits(9,0x2,3,port);
168 outbits(16,RSF16_ENCODE(freq),2,port);
175 /* NOTE if mute this stop radio
176 you must set freq on unmute */
177 dev->stereo = fmr2_stereo_mode(port);
181 /* !!! not tested, in my card this does't work !!! */
182 static int fmr2_setvolume(struct fmr2_device *dev)
184 int vol[16] = { 0x021, 0x084, 0x090, 0x104,
185 0x110, 0x204, 0x210, 0x402,
186 0x404, 0x408, 0x410, 0x801,
187 0x802, 0x804, 0x808, 0x810 };
188 int i, a, port = dev->port;
189 int n = vol[dev->curvol & 0x0f];
191 if (dev->card_type != 11)
194 for (i = 12; --i >= 0; ) {
195 a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
198 outb(a | 0x24, port);
203 for (i = 6; --i >= 0; ) {
204 a = ((0x18 >> i) & 1) << 6;
207 outb(a | 0x24, port);
218 static int vidioc_querycap(struct file *file, void *priv,
219 struct v4l2_capability *v)
221 strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
222 strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
223 sprintf(v->bus_info, "ISA");
224 v->version = RADIO_VERSION;
225 v->capabilities = V4L2_CAP_TUNER;
229 static int vidioc_g_tuner(struct file *file, void *priv,
230 struct v4l2_tuner *v)
233 struct fmr2_device *fmr2 = video_drvdata(file);
238 strcpy(v->name, "FM");
239 v->type = V4L2_TUNER_RADIO;
241 mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
242 v->rangelow = RSF16_MINFREQ/mult;
243 v->rangehigh = RSF16_MAXFREQ/mult;
244 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
245 v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW;
246 v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
247 V4L2_TUNER_MODE_MONO;
249 v->signal = fmr2_getsigstr(fmr2);
254 static int vidioc_s_tuner(struct file *file, void *priv,
255 struct v4l2_tuner *v)
262 static int vidioc_s_frequency(struct file *file, void *priv,
263 struct v4l2_frequency *f)
265 struct fmr2_device *fmr2 = video_drvdata(file);
267 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
268 f->frequency *= 1000;
269 if (f->frequency < RSF16_MINFREQ ||
270 f->frequency > RSF16_MAXFREQ )
272 /*rounding in steps of 200 to match th freq
274 fmr2->curfreq = (f->frequency/200)*200;
276 /* set card freq (if not muted) */
277 if (fmr2->curvol && !fmr2->mute) {
285 static int vidioc_g_frequency(struct file *file, void *priv,
286 struct v4l2_frequency *f)
288 struct fmr2_device *fmr2 = video_drvdata(file);
290 f->type = V4L2_TUNER_RADIO;
291 f->frequency = fmr2->curfreq;
292 if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
293 f->frequency /= 1000;
297 static int vidioc_queryctrl(struct file *file, void *priv,
298 struct v4l2_queryctrl *qc)
302 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
303 if (qc->id && qc->id == radio_qctrl[i].id) {
304 memcpy(qc, &radio_qctrl[i], sizeof(*qc));
311 static int vidioc_g_ctrl(struct file *file, void *priv,
312 struct v4l2_control *ctrl)
314 struct fmr2_device *fmr2 = video_drvdata(file);
317 case V4L2_CID_AUDIO_MUTE:
318 ctrl->value = fmr2->mute;
320 case V4L2_CID_AUDIO_VOLUME:
321 ctrl->value = fmr2->curvol;
327 static int vidioc_s_ctrl(struct file *file, void *priv,
328 struct v4l2_control *ctrl)
330 struct fmr2_device *fmr2 = video_drvdata(file);
333 case V4L2_CID_AUDIO_MUTE:
334 fmr2->mute = ctrl->value;
336 case V4L2_CID_AUDIO_VOLUME:
337 if (ctrl->value > radio_qctrl[AUD_VOL_INDEX].maximum)
338 fmr2->curvol = radio_qctrl[AUD_VOL_INDEX].maximum;
340 fmr2->curvol = ctrl->value;
348 if (fmr2->curvol && !fmr2->mute)
349 printk(KERN_DEBUG "unmute\n");
351 printk(KERN_DEBUG "mute\n");
355 if (fmr2->curvol && !fmr2->mute) {
356 fmr2_setvolume(fmr2);
357 /* Set frequency and unmute card */
360 fmr2_mute(fmr2->port);
365 static int vidioc_g_audio(struct file *file, void *priv,
366 struct v4l2_audio *a)
371 strcpy(a->name, "Radio");
372 a->capability = V4L2_AUDCAP_STEREO;
376 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
382 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
389 static int vidioc_s_audio(struct file *file, void *priv,
390 struct v4l2_audio *a)
397 static struct fmr2_device fmr2_unit;
399 static int fmr2_exclusive_open(struct file *file)
401 return test_and_set_bit(0, &fmr2_unit.in_use) ? -EBUSY : 0;
404 static int fmr2_exclusive_release(struct file *file)
406 clear_bit(0, &fmr2_unit.in_use);
410 static const struct v4l2_file_operations fmr2_fops = {
411 .owner = THIS_MODULE,
412 .open = fmr2_exclusive_open,
413 .release = fmr2_exclusive_release,
414 .ioctl = video_ioctl2,
417 static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
418 .vidioc_querycap = vidioc_querycap,
419 .vidioc_g_tuner = vidioc_g_tuner,
420 .vidioc_s_tuner = vidioc_s_tuner,
421 .vidioc_g_audio = vidioc_g_audio,
422 .vidioc_s_audio = vidioc_s_audio,
423 .vidioc_g_input = vidioc_g_input,
424 .vidioc_s_input = vidioc_s_input,
425 .vidioc_g_frequency = vidioc_g_frequency,
426 .vidioc_s_frequency = vidioc_s_frequency,
427 .vidioc_queryctrl = vidioc_queryctrl,
428 .vidioc_g_ctrl = vidioc_g_ctrl,
429 .vidioc_s_ctrl = vidioc_s_ctrl,
432 static struct video_device fmr2_radio = {
433 .name = "SF16FMR2 radio",
435 .ioctl_ops = &fmr2_ioctl_ops,
436 .release = video_device_release_empty,
439 static int __init fmr2_init(void)
442 fmr2_unit.curvol = 0;
444 fmr2_unit.curfreq = 0;
445 fmr2_unit.stereo = 1;
446 fmr2_unit.flags = V4L2_TUNER_CAP_LOW;
447 fmr2_unit.card_type = 0;
448 video_set_drvdata(&fmr2_radio, &fmr2_unit);
452 if (!request_region(io, 2, "sf16fmr2")) {
453 printk(KERN_ERR "radio-sf16fmr2: request_region failed!\n");
457 if (video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
458 release_region(io, 2);
462 printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
463 /* mute card - prevents noisy bootups */
466 fmr2_product_info(&fmr2_unit);
468 debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
470 /* Only card_type == 11 implements volume */
471 if (fmr2_unit.card_type != 11)
472 radio_qctrl[AUD_VOL_INDEX].maximum = 1;
477 MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
478 MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
479 MODULE_LICENSE("GPL");
481 module_param(io, int, 0);
482 MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
483 module_param(radio_nr, int, 0);
485 static void __exit fmr2_cleanup_module(void)
487 video_unregister_device(&fmr2_radio);
488 release_region(io,2);
491 module_init(fmr2_init);
492 module_exit(fmr2_cleanup_module);
496 static int __init fmr2_setup_io(char *str)
498 get_option(&str, &io);
502 __setup("sf16fmr2=", fmr2_setup_io);