blob: c69bde39a2336c8ae623001bc7465139abc4f693 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* radiotrack (radioreveal) driver for Linux radio support
2 * (c) 1997 M. Kirkwood
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -03003 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Converted to new API by Alan Cox <Alan.Cox@linux.org>
5 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
6 *
7 * History:
8 * 1999-02-24 Russell Kroll <rkroll@exploits.org>
9 * Fine tuning/VIDEO_TUNER_LOW
10 * Frequency range expanded to start at 87 MHz
11 *
12 * TODO: Allow for more than one of these foolish entities :-)
13 *
14 * Notes on the hardware (reverse engineered from other peoples'
15 * reverse engineering of AIMS' code :-)
16 *
17 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
18 *
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.)
23 *
24 * Volume control is (ugh) analogue:
25 * out(port, start_increasing_volume);
26 * wait(a_wee_while);
27 * out(port, stop_changing_the_volume);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
31#include <linux/module.h> /* Modules */
32#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070033#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/delay.h> /* udelay */
35#include <asm/io.h> /* outb, outb_p */
36#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -030037#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030038#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -030040#include <linux/version.h> /* for KERNEL_VERSION MACRO */
41#define RADIO_VERSION KERNEL_VERSION(0,0,2)
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#ifndef CONFIG_RADIO_RTRACK_PORT
44#define CONFIG_RADIO_RTRACK_PORT -1
45#endif
46
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030047static int io = CONFIG_RADIO_RTRACK_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static int radio_nr = -1;
Ingo Molnar3593cab2006-02-07 06:49:14 -020049static struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51struct rt_device
52{
53 int port;
54 int curvol;
55 unsigned long curfreq;
56 int muted;
57};
58
59
60/* local things */
61
62static void sleep_delay(long n)
63{
64 /* Sleep nicely for 'n' uS */
Mauro Carvalho Chehaba2d66a32007-07-17 16:15:58 -030065 int d=n/msecs_to_jiffies(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if(!d)
67 udelay(n);
68 else
69 msleep(jiffies_to_msecs(d));
70}
71
72static void rt_decvol(void)
73{
74 outb(0x58, io); /* volume down + sigstr + on */
75 sleep_delay(100000);
76 outb(0xd8, io); /* volume steady + sigstr + on */
77}
78
79static void rt_incvol(void)
80{
81 outb(0x98, io); /* volume up + sigstr + on */
82 sleep_delay(100000);
83 outb(0xd8, io); /* volume steady + sigstr + on */
84}
85
86static void rt_mute(struct rt_device *dev)
87{
88 dev->muted = 1;
Ingo Molnar3593cab2006-02-07 06:49:14 -020089 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 outb(0xd0, io); /* volume steady, off */
Ingo Molnar3593cab2006-02-07 06:49:14 -020091 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94static int rt_setvol(struct rt_device *dev, int vol)
95{
96 int i;
97
Ingo Molnar3593cab2006-02-07 06:49:14 -020098 mutex_lock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if(vol == dev->curvol) { /* requested volume = current */
101 if (dev->muted) { /* user is unmuting the card */
102 dev->muted = 0;
103 outb (0xd8, io); /* enable card */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300104 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200105 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
107 }
108
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! */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200114 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116 }
117
118 dev->muted = 0;
119 if(vol > dev->curvol)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300120 for(i = dev->curvol; i < vol; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 rt_incvol();
122 else
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300123 for(i = dev->curvol; i > vol; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 rt_decvol();
125
126 dev->curvol = vol;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200127 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return 0;
129}
130
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300131/* the 128+64 on these outb's is to keep the volume stable while tuning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * without them, the volume _will_ creep up with each frequency change
133 * and bit 4 (+16) is to keep the signal strength meter enabled
134 */
135
136static void send_0_byte(int port, struct rt_device *dev)
137{
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 */
141 }
142 else {
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 */
145 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300146 sleep_delay(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static void send_1_byte(int port, struct rt_device *dev)
150{
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 */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 else {
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 */
158 }
159
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300160 sleep_delay(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163static int rt_setfreq(struct rt_device *dev, unsigned long freq)
164{
165 int i;
166
167 /* adapted from radio-aztech.c */
168
169 /* now uses VIDEO_TUNER_LOW for fine tuning */
170
171 freq += 171200; /* Add 10.7 MHz IF */
172 freq /= 800; /* Convert to 50 kHz units */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300173
Ingo Molnar3593cab2006-02-07 06:49:14 -0200174 mutex_lock(&lock); /* Stop other ops interfering */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 send_0_byte (io, dev); /* 0: LSB of frequency */
177
178 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
179 if (freq & (1 << i))
180 send_1_byte (io, dev);
181 else
182 send_0_byte (io, dev);
183
184 send_0_byte (io, dev); /* 14: test bit - always 0 */
185 send_0_byte (io, dev); /* 15: test bit - always 0 */
186
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 */
191
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) */
196
197 if ((dev->curvol == 0) || (dev->muted))
198 outb (0xd0, io); /* volume steady + sigstr */
199 else
200 outb (0xd8, io); /* volume steady + sigstr + on */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300201
Ingo Molnar3593cab2006-02-07 06:49:14 -0200202 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 return 0;
205}
206
207static int rt_getsigstr(struct rt_device *dev)
208{
209 if (inb(io) & 2) /* bit set = no signal present */
210 return 0;
211 return 1; /* signal present */
212}
213
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300214static struct v4l2_queryctrl radio_qctrl[] = {
215 {
216 .id = V4L2_CID_AUDIO_MUTE,
217 .name = "Mute",
218 .minimum = 0,
219 .maximum = 1,
220 .default_value = 1,
221 .type = V4L2_CTRL_TYPE_BOOLEAN,
222 },{
223 .id = V4L2_CID_AUDIO_VOLUME,
224 .name = "Volume",
225 .minimum = 0,
226 .maximum = 0xff,
227 .step = 1,
228 .default_value = 0xff,
229 .type = V4L2_CTRL_TYPE_INTEGER,
230 }
231};
232
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300233static int vidioc_querycap(struct file *file, void *priv,
234 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300236 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;
241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300244static int vidioc_g_tuner(struct file *file, void *priv,
245 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300247 struct video_device *dev = video_devdata(file);
248 struct rt_device *rt = dev->priv;
249
250 if (v->index > 0)
251 return -EINVAL;
252
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);
261 return 0;
262}
263
264static int vidioc_s_tuner(struct file *file, void *priv,
265 struct v4l2_tuner *v)
266{
267 if (v->index > 0)
268 return -EINVAL;
269 return 0;
270}
271
272static int vidioc_s_frequency(struct file *file, void *priv,
273 struct v4l2_frequency *f)
274{
275 struct video_device *dev = video_devdata(file);
276 struct rt_device *rt = dev->priv;
277
278 rt->curfreq = f->frequency;
279 rt_setfreq(rt, rt->curfreq);
280 return 0;
281}
282
283static int vidioc_g_frequency(struct file *file, void *priv,
284 struct v4l2_frequency *f)
285{
286 struct video_device *dev = video_devdata(file);
287 struct rt_device *rt = dev->priv;
288
289 f->type = V4L2_TUNER_RADIO;
290 f->frequency = rt->curfreq;
291 return 0;
292}
293
294static int vidioc_queryctrl(struct file *file, void *priv,
295 struct v4l2_queryctrl *qc)
296{
297 int i;
298
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]),
302 sizeof(*qc));
303 return 0;
304 }
305 }
306 return -EINVAL;
307}
308
309static int vidioc_g_ctrl(struct file *file, void *priv,
310 struct v4l2_control *ctrl)
311{
312 struct video_device *dev = video_devdata(file);
313 struct rt_device *rt = dev->priv;
314
315 switch (ctrl->id) {
316 case V4L2_CID_AUDIO_MUTE:
317 ctrl->value = rt->muted;
318 return 0;
319 case V4L2_CID_AUDIO_VOLUME:
320 ctrl->value = rt->curvol * 6554;
321 return 0;
322 }
323 return -EINVAL;
324}
325
326static int vidioc_s_ctrl(struct file *file, void *priv,
327 struct v4l2_control *ctrl)
328{
329 struct video_device *dev = video_devdata(file);
330 struct rt_device *rt = dev->priv;
331
332 switch (ctrl->id) {
333 case V4L2_CID_AUDIO_MUTE:
334 if (ctrl->value)
335 rt_mute(rt);
336 else
337 rt_setvol(rt,rt->curvol);
338 return 0;
339 case V4L2_CID_AUDIO_VOLUME:
340 rt_setvol(rt,ctrl->value);
341 return 0;
342 }
343 return -EINVAL;
344}
345
346static int vidioc_g_audio (struct file *file, void *priv,
347 struct v4l2_audio *a)
348{
349 if (a->index > 1)
350 return -EINVAL;
351
352 strcpy(a->name, "Radio");
353 a->capability = V4L2_AUDCAP_STEREO;
354 return 0;
355}
356
357static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
358{
359 *i = 0;
360 return 0;
361}
362
363static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
364{
365 if (i != 0)
366 return -EINVAL;
367 return 0;
368}
369
370static int vidioc_s_audio(struct file *file, void *priv,
371 struct v4l2_audio *a)
372{
373 if (a->index != 0)
374 return -EINVAL;
375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378static struct rt_device rtrack_unit;
379
Arjan van de Venfa027c22007-02-12 00:55:33 -0800380static const struct file_operations rtrack_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 .owner = THIS_MODULE,
382 .open = video_exclusive_open,
383 .release = video_exclusive_release,
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300384 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200385 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 .llseek = no_llseek,
387};
388
389static struct video_device rtrack_radio=
390{
391 .owner = THIS_MODULE,
392 .name = "RadioTrack radio",
393 .type = VID_TYPE_TUNER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 .fops = &rtrack_fops,
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300395 .vidioc_querycap = vidioc_querycap,
396 .vidioc_g_tuner = vidioc_g_tuner,
397 .vidioc_s_tuner = vidioc_s_tuner,
398 .vidioc_g_audio = vidioc_g_audio,
399 .vidioc_s_audio = vidioc_s_audio,
400 .vidioc_g_input = vidioc_g_input,
401 .vidioc_s_input = vidioc_s_input,
402 .vidioc_g_frequency = vidioc_g_frequency,
403 .vidioc_s_frequency = vidioc_s_frequency,
404 .vidioc_queryctrl = vidioc_queryctrl,
405 .vidioc_g_ctrl = vidioc_g_ctrl,
406 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407};
408
409static int __init rtrack_init(void)
410{
411 if(io==-1)
412 {
413 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
414 return -EINVAL;
415 }
416
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300417 if (!request_region(io, 2, "rtrack"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 {
419 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
420 return -EBUSY;
421 }
422
423 rtrack_radio.priv=&rtrack_unit;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
426 {
427 release_region(io, 2);
428 return -EINVAL;
429 }
430 printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
431
432 /* Set up the I/O locking */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300433
Ingo Molnar3593cab2006-02-07 06:49:14 -0200434 mutex_init(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300435
436 /* mute card - prevents noisy bootups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* this ensures that the volume is all the way down */
439 outb(0x48, io); /* volume down but still "on" */
440 sleep_delay(2000000); /* make sure it's totally down */
441 outb(0xc0, io); /* steady volume, mute card */
442 rtrack_unit.curvol = 0;
443
444 return 0;
445}
446
447MODULE_AUTHOR("M.Kirkwood");
448MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
449MODULE_LICENSE("GPL");
450
451module_param(io, int, 0);
452MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
453module_param(radio_nr, int, 0);
454
455static void __exit cleanup_rtrack_module(void)
456{
457 video_unregister_device(&rtrack_radio);
458 release_region(io,2);
459}
460
461module_init(rtrack_init);
462module_exit(cleanup_rtrack_module);
463