1 /* GemTek radio card driver for Linux (C) 1998 Jonas Munsin <jmunsin@iki.fi>
3 * GemTek hasn't released any specs on the card, so the protocol had to
4 * be reverse engineered with dosemu.
6 * Besides the protocol changes, this is mostly a copy of:
8 * RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
10 * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
11 * Converted to new API by Alan Cox <Alan.Cox@linux.org>
12 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
14 * TODO: Allow for more than one of these foolish entities :-)
16 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
19 #include <linux/module.h> /* Modules */
20 #include <linux/init.h> /* Initdata */
21 #include <linux/ioport.h> /* request_region */
22 #include <linux/delay.h> /* udelay */
23 #include <asm/io.h> /* outb, outb_p */
24 #include <asm/uaccess.h> /* copy to/from user */
25 #include <linux/videodev2.h> /* kernel radio structs */
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-common.h>
28 #include <linux/spinlock.h>
30 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
31 #define RADIO_VERSION KERNEL_VERSION(0,0,3)
32 #define RADIO_BANNER "GemTek Radio card driver: v0.0.3"
38 MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <pexu@kapsi.fi>");
39 MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
40 MODULE_LICENSE("GPL");
46 #ifndef CONFIG_RADIO_GEMTEK_PORT
47 #define CONFIG_RADIO_GEMTEK_PORT -1
49 #ifndef CONFIG_RADIO_GEMTEK_PROBE
50 #define CONFIG_RADIO_GEMTEK_PROBE 1
53 static int io = CONFIG_RADIO_GEMTEK_PORT;
54 static int probe = CONFIG_RADIO_GEMTEK_PROBE;
56 static int shutdown = 1;
57 static int keepmuted = 1;
58 static int initmute = 1;
59 static int radio_nr = -1;
61 module_param(io, int, 0444);
62 MODULE_PARM_DESC(io, "Force I/O port for the GemTek Radio card if automatic "
63 "probing is disabled or fails. The most common I/O ports are: 0x20c "
64 "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
65 "work for the combined sound/radiocard).");
67 module_param(probe, bool, 0444);
68 MODULE_PARM_DESC(probe, "Enable automatic device probing. Note: only the most "
69 "common I/O ports used by the card are probed.");
71 module_param(hardmute, bool, 0644);
72 MODULE_PARM_DESC(hardmute, "Enable `hard muting' by shutting down PLL, may "
73 "reduce static noise.");
75 module_param(shutdown, bool, 0644);
76 MODULE_PARM_DESC(shutdown, "Enable shutting down PLL and muting line when "
77 "module is unloaded.");
79 module_param(keepmuted, bool, 0644);
80 MODULE_PARM_DESC(keepmuted, "Keep card muted even when frequency is changed.");
82 module_param(initmute, bool, 0444);
83 MODULE_PARM_DESC(initmute, "Mute card when module is loaded.");
85 module_param(radio_nr, int, 0444);
88 * Functions for controlling the card.
90 #define GEMTEK_LOWFREQ (87*16000)
91 #define GEMTEK_HIGHFREQ (108*16000)
94 * Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal
95 * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
98 #define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
99 #define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
101 #define GEMTEK_CK 0x01 /* Clock signal */
102 #define GEMTEK_DA 0x02 /* Serial data */
103 #define GEMTEK_CE 0x04 /* Chip enable */
104 #define GEMTEK_NS 0x08 /* No signal */
105 #define GEMTEK_MT 0x10 /* Line mute */
106 #define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */
107 #define GEMTEK_PLL_OFF 0x07 /* PLL off */
109 #define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */
111 #define SHORT_DELAY 5 /* usec */
112 #define LONG_DELAY 75 /* usec */
114 struct gemtek_device {
115 unsigned long lastfreq;
120 #define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */
121 #define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */
122 #define BU2614_VOID_BITS 4 /* unused */
123 #define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */
124 #define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */
125 #define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */
126 #define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/
127 #define BU2614_VOID2_BITS 1 /* unused */
128 #define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */
129 #define BU2614_TEST_BITS 1 /* TS, Test data is input */
131 #define BU2614_FREQ_SHIFT 0
132 #define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
133 #define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
134 #define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
135 #define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
136 #define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
137 #define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
138 #define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
139 #define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
140 #define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
142 #define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \
143 BU2614_##field##_SHIFT)
144 #define BU2614_PORT_MASK MKMASK(PORT)
145 #define BU2614_FREQ_MASK MKMASK(FREQ)
146 #define BU2614_VOID_MASK MKMASK(VOID)
147 #define BU2614_FMES_MASK MKMASK(FMES)
148 #define BU2614_STDF_MASK MKMASK(STDF)
149 #define BU2614_SWIN_MASK MKMASK(SWIN)
150 #define BU2614_SWAL_MASK MKMASK(SWAL)
151 #define BU2614_VOID2_MASK MKMASK(VOID2)
152 #define BU2614_FMUN_MASK MKMASK(FMUN)
153 #define BU2614_TEST_MASK MKMASK(TEST)
155 static struct gemtek_device gemtek_unit;
157 static spinlock_t lock;
160 * Set data which will be sent to BU2614FS.
162 #define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
163 ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
166 * Transmit settings to BU2614FS over GemTek IC.
168 static void gemtek_bu2614_transmit(struct gemtek_device *dev)
174 mute = dev->muted ? GEMTEK_MT : 0x00;
176 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
178 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
181 for (i = 0, q = dev->bu2614data; i < 32; i++, q >>= 1) {
182 bit = (q & 1) ? GEMTEK_DA : 0;
183 outb_p(mute | GEMTEK_CE | bit, io);
185 outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, io);
189 outb_p(mute | GEMTEK_DA | GEMTEK_CK, io);
191 outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, io);
198 * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
200 static unsigned long gemtek_convfreq(unsigned long freq)
202 return ((freq<<FSCALE) + IF_OFFSET + REF_FREQ/2) / REF_FREQ;
208 static void gemtek_setfreq(struct gemtek_device *dev, unsigned long freq)
211 if (keepmuted && hardmute && dev->muted)
214 if (freq < GEMTEK_LOWFREQ)
215 freq = GEMTEK_LOWFREQ;
216 else if (freq > GEMTEK_HIGHFREQ)
217 freq = GEMTEK_HIGHFREQ;
219 dev->lastfreq = freq;
222 gemtek_bu2614_set(dev, BU2614_PORT, 0);
223 gemtek_bu2614_set(dev, BU2614_FMES, 0);
224 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
225 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
226 gemtek_bu2614_set(dev, BU2614_FMUN, 1); /* GT bit set */
227 gemtek_bu2614_set(dev, BU2614_TEST, 0);
229 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
230 gemtek_bu2614_set(dev, BU2614_FREQ, gemtek_convfreq(freq));
232 gemtek_bu2614_transmit(dev);
238 static void gemtek_mute(struct gemtek_device *dev)
244 /* Turn off PLL, disable data output */
245 gemtek_bu2614_set(dev, BU2614_PORT, 0);
246 gemtek_bu2614_set(dev, BU2614_FMES, 0); /* CT bit off */
247 gemtek_bu2614_set(dev, BU2614_SWIN, 0); /* FM-mode */
248 gemtek_bu2614_set(dev, BU2614_SWAL, 0);
249 gemtek_bu2614_set(dev, BU2614_FMUN, 0); /* GT bit off */
250 gemtek_bu2614_set(dev, BU2614_TEST, 0);
251 gemtek_bu2614_set(dev, BU2614_STDF, GEMTEK_PLL_OFF);
252 gemtek_bu2614_set(dev, BU2614_FREQ, 0);
253 gemtek_bu2614_transmit(dev);
257 /* Read bus contents (CE, CK and DA). */
259 /* Write it back with mute flag set. */
260 outb_p((i >> 5) | GEMTEK_MT, io);
270 static void gemtek_unmute(struct gemtek_device *dev)
276 /* Turn PLL back on. */
277 gemtek_setfreq(dev, dev->lastfreq);
290 * Get signal strength (= stereo status).
292 static inline int gemtek_getsigstr(void)
294 return inb_p(io) & GEMTEK_NS ? 0 : 1;
298 * Check if requested card acts like GemTek Radio card.
300 static int gemtek_verify(int port)
302 static int verified = -1;
305 if (verified == port)
310 q = inb_p(port); /* Read bus contents before probing. */
311 /* Try to turn on CE, CK and DA respectively and check if card responds
313 for (i = 0; i < 3; ++i) {
314 outb_p(1 << i, port);
317 if ((inb_p(port) & (~GEMTEK_NS)) != (0x17 | (1 << (i + 5)))) {
322 outb_p(q >> 5, port); /* Write bus contents back. */
332 * Automatic probing for card.
334 static int gemtek_probe(void)
336 int ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
340 printk(KERN_INFO "Automatic device probing disabled.\n");
344 printk(KERN_INFO "Automatic device probing enabled.\n");
346 for (i = 0; i < ARRAY_SIZE(ioports); ++i) {
347 printk(KERN_INFO "Trying I/O port 0x%x...\n", ioports[i]);
349 if (!request_region(ioports[i], 1, "gemtek-probe")) {
350 printk(KERN_WARNING "I/O port 0x%x busy!\n",
355 if (gemtek_verify(ioports[i])) {
356 printk(KERN_INFO "Card found from I/O port "
357 "0x%x!\n", ioports[i]);
359 release_region(ioports[i], 1);
365 release_region(ioports[i], 1);
368 printk(KERN_ERR "Automatic probing failed!\n");
374 * Video 4 Linux stuff.
377 static struct v4l2_queryctrl radio_qctrl[] = {
379 .id = V4L2_CID_AUDIO_MUTE,
384 .type = V4L2_CTRL_TYPE_BOOLEAN,
386 .id = V4L2_CID_AUDIO_VOLUME,
391 .default_value = 0xff,
392 .type = V4L2_CTRL_TYPE_INTEGER,
396 static const struct file_operations gemtek_fops = {
397 .owner = THIS_MODULE,
398 .open = video_exclusive_open,
399 .release = video_exclusive_release,
400 .ioctl = video_ioctl2,
402 .compat_ioctl = v4l_compat_ioctl32,
407 static int vidioc_querycap(struct file *file, void *priv,
408 struct v4l2_capability *v)
410 strlcpy(v->driver, "radio-gemtek", sizeof(v->driver));
411 strlcpy(v->card, "GemTek", sizeof(v->card));
412 sprintf(v->bus_info, "ISA");
413 v->version = RADIO_VERSION;
414 v->capabilities = V4L2_CAP_TUNER;
418 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
423 strcpy(v->name, "FM");
424 v->type = V4L2_TUNER_RADIO;
425 v->rangelow = GEMTEK_LOWFREQ;
426 v->rangehigh = GEMTEK_HIGHFREQ;
427 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
428 v->signal = 0xffff * gemtek_getsigstr();
430 v->audmode = V4L2_TUNER_MODE_STEREO;
431 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
433 v->audmode = V4L2_TUNER_MODE_MONO;
434 v->rxsubchans = V4L2_TUNER_SUB_MONO;
440 static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
447 static int vidioc_s_frequency(struct file *file, void *priv,
448 struct v4l2_frequency *f)
450 struct video_device *dev = video_devdata(file);
451 struct gemtek_device *rt = dev->priv;
453 gemtek_setfreq(rt, f->frequency);
458 static int vidioc_g_frequency(struct file *file, void *priv,
459 struct v4l2_frequency *f)
461 struct video_device *dev = video_devdata(file);
462 struct gemtek_device *rt = dev->priv;
464 f->type = V4L2_TUNER_RADIO;
465 f->frequency = rt->lastfreq;
469 static int vidioc_queryctrl(struct file *file, void *priv,
470 struct v4l2_queryctrl *qc)
474 for (i = 0; i < ARRAY_SIZE(radio_qctrl); ++i) {
475 if (qc->id && qc->id == radio_qctrl[i].id) {
476 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
483 static int vidioc_g_ctrl(struct file *file, void *priv,
484 struct v4l2_control *ctrl)
486 struct video_device *dev = video_devdata(file);
487 struct gemtek_device *rt = dev->priv;
490 case V4L2_CID_AUDIO_MUTE:
491 ctrl->value = rt->muted;
493 case V4L2_CID_AUDIO_VOLUME:
503 static int vidioc_s_ctrl(struct file *file, void *priv,
504 struct v4l2_control *ctrl)
506 struct video_device *dev = video_devdata(file);
507 struct gemtek_device *rt = dev->priv;
510 case V4L2_CID_AUDIO_MUTE:
516 case V4L2_CID_AUDIO_VOLUME:
526 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
531 strcpy(a->name, "Radio");
532 a->capability = V4L2_AUDCAP_STEREO;
536 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
542 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
549 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
556 static const struct v4l2_ioctl_ops gemtek_ioctl_ops = {
557 .vidioc_querycap = vidioc_querycap,
558 .vidioc_g_tuner = vidioc_g_tuner,
559 .vidioc_s_tuner = vidioc_s_tuner,
560 .vidioc_g_audio = vidioc_g_audio,
561 .vidioc_s_audio = vidioc_s_audio,
562 .vidioc_g_input = vidioc_g_input,
563 .vidioc_s_input = vidioc_s_input,
564 .vidioc_g_frequency = vidioc_g_frequency,
565 .vidioc_s_frequency = vidioc_s_frequency,
566 .vidioc_queryctrl = vidioc_queryctrl,
567 .vidioc_g_ctrl = vidioc_g_ctrl,
568 .vidioc_s_ctrl = vidioc_s_ctrl
571 static struct video_device gemtek_radio = {
572 .name = "GemTek Radio card",
573 .fops = &gemtek_fops,
574 .ioctl_ops = &gemtek_ioctl_ops,
578 * Initialization / cleanup related stuff.
584 static int __init gemtek_init(void)
586 printk(KERN_INFO RADIO_BANNER "\n");
588 spin_lock_init(&lock);
592 if (!request_region(io, 1, "gemtek")) {
593 printk(KERN_ERR "I/O port 0x%x already in use.\n", io);
597 if (!gemtek_verify(io))
598 printk(KERN_WARNING "Card at I/O port 0x%x does not "
599 "respond properly, check your "
600 "configuration.\n", io);
602 printk(KERN_INFO "Using I/O port 0x%x.\n", io);
604 printk(KERN_ERR "Automatic probing failed and no "
605 "fixed I/O port defined.\n");
608 printk(KERN_ERR "Automatic probing disabled but no fixed "
609 "I/O port defined.");
613 gemtek_radio.priv = &gemtek_unit;
615 if (video_register_device(&gemtek_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
616 release_region(io, 1);
621 gemtek_unit.lastfreq = GEMTEK_LOWFREQ;
622 gemtek_unit.bu2614data = 0;
625 gemtek_mute(&gemtek_unit);
633 static void __exit gemtek_exit(void)
636 hardmute = 1; /* Turn off PLL */
637 gemtek_mute(&gemtek_unit);
639 printk(KERN_INFO "Module unloaded but card not muted!\n");
642 video_unregister_device(&gemtek_radio);
643 release_region(io, 1);
646 module_init(gemtek_init);
647 module_exit(gemtek_exit);