1 /* radiotrack (radioreveal) driver for Linux radio support
3 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
4 * Converted to new API by Alan Cox <Alan.Cox@linux.org>
5 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
8 * 1999-02-24 Russell Kroll <rkroll@exploits.org>
9 * Fine tuning/VIDEO_TUNER_LOW
10 * Frequency range expanded to start at 87 MHz
12 * TODO: Allow for more than one of these foolish entities :-)
14 * Notes on the hardware (reverse engineered from other peoples'
15 * reverse engineering of AIMS' code :-)
17 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
19 * The signal strength query is unsurprisingly inaccurate. And it seems
20 * to indicate that (on my card, at least) the frequency setting isn't
21 * too great. (I have to tune up .025MHz from what the freq should be
22 * to get a report that the thing is tuned.)
24 * Volume control is (ugh) analogue:
25 * out(port, start_increasing_volume);
27 * out(port, stop_changing_the_volume);
31 #include <linux/module.h> /* Modules */
32 #include <linux/init.h> /* Initdata */
33 #include <linux/ioport.h> /* request_region */
34 #include <linux/delay.h> /* udelay */
35 #include <asm/io.h> /* outb, outb_p */
36 #include <asm/uaccess.h> /* copy to/from user */
37 #include <linux/videodev2.h> /* kernel radio structs */
38 #include <media/v4l2-common.h>
40 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
41 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
43 #ifndef CONFIG_RADIO_RTRACK_PORT
44 #define CONFIG_RADIO_RTRACK_PORT -1
47 static int io
= CONFIG_RADIO_RTRACK_PORT
;
48 static int radio_nr
= -1;
49 static struct mutex lock
;
55 unsigned long curfreq
;
62 static void sleep_delay(long n
)
64 /* Sleep nicely for 'n' uS */
65 int d
=n
/msecs_to_jiffies(1000);
69 msleep(jiffies_to_msecs(d
));
72 static void rt_decvol(void)
74 outb(0x58, io
); /* volume down + sigstr + on */
76 outb(0xd8, io
); /* volume steady + sigstr + on */
79 static void rt_incvol(void)
81 outb(0x98, io
); /* volume up + sigstr + on */
83 outb(0xd8, io
); /* volume steady + sigstr + on */
86 static void rt_mute(struct rt_device
*dev
)
90 outb(0xd0, io
); /* volume steady, off */
94 static int rt_setvol(struct rt_device
*dev
, int vol
)
100 if(vol
== dev
->curvol
) { /* requested volume = current */
101 if (dev
->muted
) { /* user is unmuting the card */
103 outb (0xd8, io
); /* enable card */
109 if(vol
== 0) { /* volume = 0 means mute the card */
110 outb(0x48, io
); /* volume down but still "on" */
111 sleep_delay(2000000); /* make sure it's totally down */
112 outb(0xd0, io
); /* volume steady, off */
113 dev
->curvol
= 0; /* track the volume state! */
119 if(vol
> dev
->curvol
)
120 for(i
= dev
->curvol
; i
< vol
; i
++)
123 for(i
= dev
->curvol
; i
> vol
; i
--)
131 /* the 128+64 on these outb's is to keep the volume stable while tuning
132 * without them, the volume _will_ creep up with each frequency change
133 * and bit 4 (+16) is to keep the signal strength meter enabled
136 static void send_0_byte(int port
, struct rt_device
*dev
)
138 if ((dev
->curvol
== 0) || (dev
->muted
)) {
139 outb_p(128+64+16+ 1, port
); /* wr-enable + data low */
140 outb_p(128+64+16+2+1, port
); /* clock */
143 outb_p(128+64+16+8+ 1, port
); /* on + wr-enable + data low */
144 outb_p(128+64+16+8+2+1, port
); /* clock */
149 static void send_1_byte(int port
, struct rt_device
*dev
)
151 if ((dev
->curvol
== 0) || (dev
->muted
)) {
152 outb_p(128+64+16+4 +1, port
); /* wr-enable+data high */
153 outb_p(128+64+16+4+2+1, port
); /* clock */
156 outb_p(128+64+16+8+4 +1, port
); /* on+wr-enable+data high */
157 outb_p(128+64+16+8+4+2+1, port
); /* clock */
163 static int rt_setfreq(struct rt_device
*dev
, unsigned long freq
)
167 /* adapted from radio-aztech.c */
169 /* now uses VIDEO_TUNER_LOW for fine tuning */
171 freq
+= 171200; /* Add 10.7 MHz IF */
172 freq
/= 800; /* Convert to 50 kHz units */
174 mutex_lock(&lock
); /* Stop other ops interfering */
176 send_0_byte (io
, dev
); /* 0: LSB of frequency */
178 for (i
= 0; i
< 13; i
++) /* : frequency bits (1-13) */
180 send_1_byte (io
, dev
);
182 send_0_byte (io
, dev
);
184 send_0_byte (io
, dev
); /* 14: test bit - always 0 */
185 send_0_byte (io
, dev
); /* 15: test bit - always 0 */
187 send_0_byte (io
, dev
); /* 16: band data 0 - always 0 */
188 send_0_byte (io
, dev
); /* 17: band data 1 - always 0 */
189 send_0_byte (io
, dev
); /* 18: band data 2 - always 0 */
190 send_0_byte (io
, dev
); /* 19: time base - always 0 */
192 send_0_byte (io
, dev
); /* 20: spacing (0 = 25 kHz) */
193 send_1_byte (io
, dev
); /* 21: spacing (1 = 25 kHz) */
194 send_0_byte (io
, dev
); /* 22: spacing (0 = 25 kHz) */
195 send_1_byte (io
, dev
); /* 23: AM/FM (FM = 1, always) */
197 if ((dev
->curvol
== 0) || (dev
->muted
))
198 outb (0xd0, io
); /* volume steady + sigstr */
200 outb (0xd8, io
); /* volume steady + sigstr + on */
207 static int rt_getsigstr(struct rt_device
*dev
)
209 if (inb(io
) & 2) /* bit set = no signal present */
211 return 1; /* signal present */
214 static struct v4l2_queryctrl radio_qctrl
[] = {
216 .id
= V4L2_CID_AUDIO_MUTE
,
221 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
223 .id
= V4L2_CID_AUDIO_VOLUME
,
228 .default_value
= 0xff,
229 .type
= V4L2_CTRL_TYPE_INTEGER
,
233 static int vidioc_querycap(struct file
*file
, void *priv
,
234 struct v4l2_capability
*v
)
236 strlcpy(v
->driver
, "radio-aimslab", sizeof(v
->driver
));
237 strlcpy(v
->card
, "RadioTrack", sizeof(v
->card
));
238 sprintf(v
->bus_info
, "ISA");
239 v
->version
= RADIO_VERSION
;
240 v
->capabilities
= V4L2_CAP_TUNER
;
244 static int vidioc_g_tuner(struct file
*file
, void *priv
,
245 struct v4l2_tuner
*v
)
247 struct video_device
*dev
= video_devdata(file
);
248 struct rt_device
*rt
= dev
->priv
;
253 strcpy(v
->name
, "FM");
254 v
->type
= V4L2_TUNER_RADIO
;
255 v
->rangelow
= (87*16000);
256 v
->rangehigh
= (108*16000);
257 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
258 v
->capability
= V4L2_TUNER_CAP_LOW
;
259 v
->audmode
= V4L2_TUNER_MODE_MONO
;
260 v
->signal
= 0xffff*rt_getsigstr(rt
);
264 static int vidioc_s_tuner(struct file
*file
, void *priv
,
265 struct v4l2_tuner
*v
)
272 static int vidioc_s_frequency(struct file
*file
, void *priv
,
273 struct v4l2_frequency
*f
)
275 struct video_device
*dev
= video_devdata(file
);
276 struct rt_device
*rt
= dev
->priv
;
278 rt
->curfreq
= f
->frequency
;
279 rt_setfreq(rt
, rt
->curfreq
);
283 static int vidioc_g_frequency(struct file
*file
, void *priv
,
284 struct v4l2_frequency
*f
)
286 struct video_device
*dev
= video_devdata(file
);
287 struct rt_device
*rt
= dev
->priv
;
289 f
->type
= V4L2_TUNER_RADIO
;
290 f
->frequency
= rt
->curfreq
;
294 static int vidioc_queryctrl(struct file
*file
, void *priv
,
295 struct v4l2_queryctrl
*qc
)
299 for (i
= 0; i
< ARRAY_SIZE(radio_qctrl
); i
++) {
300 if (qc
->id
&& qc
->id
== radio_qctrl
[i
].id
) {
301 memcpy(qc
, &(radio_qctrl
[i
]),
309 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
310 struct v4l2_control
*ctrl
)
312 struct video_device
*dev
= video_devdata(file
);
313 struct rt_device
*rt
= dev
->priv
;
316 case V4L2_CID_AUDIO_MUTE
:
317 ctrl
->value
= rt
->muted
;
319 case V4L2_CID_AUDIO_VOLUME
:
320 ctrl
->value
= rt
->curvol
* 6554;
326 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
327 struct v4l2_control
*ctrl
)
329 struct video_device
*dev
= video_devdata(file
);
330 struct rt_device
*rt
= dev
->priv
;
333 case V4L2_CID_AUDIO_MUTE
:
337 rt_setvol(rt
,rt
->curvol
);
339 case V4L2_CID_AUDIO_VOLUME
:
340 rt_setvol(rt
,ctrl
->value
);
346 static int vidioc_g_audio (struct file
*file
, void *priv
,
347 struct v4l2_audio
*a
)
352 strcpy(a
->name
, "Radio");
353 a
->capability
= V4L2_AUDCAP_STEREO
;
357 static int vidioc_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
363 static int vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
370 static int vidioc_s_audio(struct file
*file
, void *priv
,
371 struct v4l2_audio
*a
)
378 static struct rt_device rtrack_unit
;
380 static const struct file_operations rtrack_fops
= {
381 .owner
= THIS_MODULE
,
382 .open
= video_exclusive_open
,
383 .release
= video_exclusive_release
,
384 .ioctl
= video_ioctl2
,
386 .compat_ioctl
= v4l_compat_ioctl32
,
391 static struct video_device rtrack_radio
=
393 .owner
= THIS_MODULE
,
394 .name
= "RadioTrack radio",
395 .type
= VID_TYPE_TUNER
,
396 .fops
= &rtrack_fops
,
397 .vidioc_querycap
= vidioc_querycap
,
398 .vidioc_g_tuner
= vidioc_g_tuner
,
399 .vidioc_s_tuner
= vidioc_s_tuner
,
400 .vidioc_g_audio
= vidioc_g_audio
,
401 .vidioc_s_audio
= vidioc_s_audio
,
402 .vidioc_g_input
= vidioc_g_input
,
403 .vidioc_s_input
= vidioc_s_input
,
404 .vidioc_g_frequency
= vidioc_g_frequency
,
405 .vidioc_s_frequency
= vidioc_s_frequency
,
406 .vidioc_queryctrl
= vidioc_queryctrl
,
407 .vidioc_g_ctrl
= vidioc_g_ctrl
,
408 .vidioc_s_ctrl
= vidioc_s_ctrl
,
411 static int __init
rtrack_init(void)
415 printk(KERN_ERR
"You must set an I/O address with io=0x???\n");
419 if (!request_region(io
, 2, "rtrack"))
421 printk(KERN_ERR
"rtrack: port 0x%x already in use\n", io
);
425 rtrack_radio
.priv
=&rtrack_unit
;
427 if(video_register_device(&rtrack_radio
, VFL_TYPE_RADIO
, radio_nr
)==-1)
429 release_region(io
, 2);
432 printk(KERN_INFO
"AIMSlab RadioTrack/RadioReveal card driver.\n");
434 /* Set up the I/O locking */
438 /* mute card - prevents noisy bootups */
440 /* this ensures that the volume is all the way down */
441 outb(0x48, io
); /* volume down but still "on" */
442 sleep_delay(2000000); /* make sure it's totally down */
443 outb(0xc0, io
); /* steady volume, mute card */
444 rtrack_unit
.curvol
= 0;
449 MODULE_AUTHOR("M.Kirkwood");
450 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
451 MODULE_LICENSE("GPL");
453 module_param(io
, int, 0);
454 MODULE_PARM_DESC(io
, "I/O address of the RadioTrack card (0x20f or 0x30f)");
455 module_param(radio_nr
, int, 0);
457 static void __exit
cleanup_rtrack_module(void)
459 video_unregister_device(&rtrack_radio
);
460 release_region(io
,2);
463 module_init(rtrack_init
);
464 module_exit(cleanup_rtrack_module
);