blob: 62cc6874d63feecc68de7e2a30486db7bf549d1d [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>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030039#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -030041#include <linux/version.h> /* for KERNEL_VERSION MACRO */
42#define RADIO_VERSION KERNEL_VERSION(0,0,2)
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#ifndef CONFIG_RADIO_RTRACK_PORT
45#define CONFIG_RADIO_RTRACK_PORT -1
46#endif
47
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030048static int io = CONFIG_RADIO_RTRACK_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int radio_nr = -1;
Ingo Molnar3593cab2006-02-07 06:49:14 -020050static struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52struct rt_device
53{
Hans Verkuil3ca685a2008-08-23 04:49:13 -030054 unsigned long in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int port;
56 int curvol;
57 unsigned long curfreq;
58 int muted;
59};
60
61
62/* local things */
63
64static void sleep_delay(long n)
65{
66 /* Sleep nicely for 'n' uS */
Mauro Carvalho Chehaba2d66a32007-07-17 16:15:58 -030067 int d=n/msecs_to_jiffies(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if(!d)
69 udelay(n);
70 else
71 msleep(jiffies_to_msecs(d));
72}
73
74static void rt_decvol(void)
75{
76 outb(0x58, io); /* volume down + sigstr + on */
77 sleep_delay(100000);
78 outb(0xd8, io); /* volume steady + sigstr + on */
79}
80
81static void rt_incvol(void)
82{
83 outb(0x98, io); /* volume up + sigstr + on */
84 sleep_delay(100000);
85 outb(0xd8, io); /* volume steady + sigstr + on */
86}
87
88static void rt_mute(struct rt_device *dev)
89{
90 dev->muted = 1;
Ingo Molnar3593cab2006-02-07 06:49:14 -020091 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 outb(0xd0, io); /* volume steady, off */
Ingo Molnar3593cab2006-02-07 06:49:14 -020093 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static int rt_setvol(struct rt_device *dev, int vol)
97{
98 int i;
99
Ingo Molnar3593cab2006-02-07 06:49:14 -0200100 mutex_lock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if(vol == dev->curvol) { /* requested volume = current */
103 if (dev->muted) { /* user is unmuting the card */
104 dev->muted = 0;
105 outb (0xd8, io); /* enable card */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300106 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200107 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109 }
110
111 if(vol == 0) { /* volume = 0 means mute the card */
112 outb(0x48, io); /* volume down but still "on" */
113 sleep_delay(2000000); /* make sure it's totally down */
114 outb(0xd0, io); /* volume steady, off */
115 dev->curvol = 0; /* track the volume state! */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200116 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118 }
119
120 dev->muted = 0;
121 if(vol > dev->curvol)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300122 for(i = dev->curvol; i < vol; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 rt_incvol();
124 else
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300125 for(i = dev->curvol; i > vol; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 rt_decvol();
127
128 dev->curvol = vol;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200129 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 0;
131}
132
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300133/* the 128+64 on these outb's is to keep the volume stable while tuning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * without them, the volume _will_ creep up with each frequency change
135 * and bit 4 (+16) is to keep the signal strength meter enabled
136 */
137
138static void send_0_byte(int port, struct rt_device *dev)
139{
140 if ((dev->curvol == 0) || (dev->muted)) {
141 outb_p(128+64+16+ 1, port); /* wr-enable + data low */
142 outb_p(128+64+16+2+1, port); /* clock */
143 }
144 else {
145 outb_p(128+64+16+8+ 1, port); /* on + wr-enable + data low */
146 outb_p(128+64+16+8+2+1, port); /* clock */
147 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300148 sleep_delay(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static void send_1_byte(int port, struct rt_device *dev)
152{
153 if ((dev->curvol == 0) || (dev->muted)) {
154 outb_p(128+64+16+4 +1, port); /* wr-enable+data high */
155 outb_p(128+64+16+4+2+1, port); /* clock */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 else {
158 outb_p(128+64+16+8+4 +1, port); /* on+wr-enable+data high */
159 outb_p(128+64+16+8+4+2+1, port); /* clock */
160 }
161
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300162 sleep_delay(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static int rt_setfreq(struct rt_device *dev, unsigned long freq)
166{
167 int i;
168
169 /* adapted from radio-aztech.c */
170
171 /* now uses VIDEO_TUNER_LOW for fine tuning */
172
173 freq += 171200; /* Add 10.7 MHz IF */
174 freq /= 800; /* Convert to 50 kHz units */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300175
Ingo Molnar3593cab2006-02-07 06:49:14 -0200176 mutex_lock(&lock); /* Stop other ops interfering */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 send_0_byte (io, dev); /* 0: LSB of frequency */
179
180 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
181 if (freq & (1 << i))
182 send_1_byte (io, dev);
183 else
184 send_0_byte (io, dev);
185
186 send_0_byte (io, dev); /* 14: test bit - always 0 */
187 send_0_byte (io, dev); /* 15: test bit - always 0 */
188
189 send_0_byte (io, dev); /* 16: band data 0 - always 0 */
190 send_0_byte (io, dev); /* 17: band data 1 - always 0 */
191 send_0_byte (io, dev); /* 18: band data 2 - always 0 */
192 send_0_byte (io, dev); /* 19: time base - always 0 */
193
194 send_0_byte (io, dev); /* 20: spacing (0 = 25 kHz) */
195 send_1_byte (io, dev); /* 21: spacing (1 = 25 kHz) */
196 send_0_byte (io, dev); /* 22: spacing (0 = 25 kHz) */
197 send_1_byte (io, dev); /* 23: AM/FM (FM = 1, always) */
198
199 if ((dev->curvol == 0) || (dev->muted))
200 outb (0xd0, io); /* volume steady + sigstr */
201 else
202 outb (0xd8, io); /* volume steady + sigstr + on */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300203
Ingo Molnar3593cab2006-02-07 06:49:14 -0200204 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 return 0;
207}
208
209static int rt_getsigstr(struct rt_device *dev)
210{
211 if (inb(io) & 2) /* bit set = no signal present */
212 return 0;
213 return 1; /* signal present */
214}
215
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300216static struct v4l2_queryctrl radio_qctrl[] = {
217 {
218 .id = V4L2_CID_AUDIO_MUTE,
219 .name = "Mute",
220 .minimum = 0,
221 .maximum = 1,
222 .default_value = 1,
223 .type = V4L2_CTRL_TYPE_BOOLEAN,
224 },{
225 .id = V4L2_CID_AUDIO_VOLUME,
226 .name = "Volume",
227 .minimum = 0,
228 .maximum = 0xff,
229 .step = 1,
230 .default_value = 0xff,
231 .type = V4L2_CTRL_TYPE_INTEGER,
232 }
233};
234
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300235static int vidioc_querycap(struct file *file, void *priv,
236 struct v4l2_capability *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300238 strlcpy(v->driver, "radio-aimslab", sizeof(v->driver));
239 strlcpy(v->card, "RadioTrack", sizeof(v->card));
240 sprintf(v->bus_info, "ISA");
241 v->version = RADIO_VERSION;
242 v->capabilities = V4L2_CAP_TUNER;
243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300246static int vidioc_g_tuner(struct file *file, void *priv,
247 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300249 struct video_device *dev = video_devdata(file);
Hans Verkuil601e9442008-08-23 07:24:07 -0300250 struct rt_device *rt = video_get_drvdata(dev);
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300251
252 if (v->index > 0)
253 return -EINVAL;
254
255 strcpy(v->name, "FM");
256 v->type = V4L2_TUNER_RADIO;
257 v->rangelow = (87*16000);
258 v->rangehigh = (108*16000);
259 v->rxsubchans = V4L2_TUNER_SUB_MONO;
260 v->capability = V4L2_TUNER_CAP_LOW;
261 v->audmode = V4L2_TUNER_MODE_MONO;
262 v->signal = 0xffff*rt_getsigstr(rt);
263 return 0;
264}
265
266static int vidioc_s_tuner(struct file *file, void *priv,
267 struct v4l2_tuner *v)
268{
269 if (v->index > 0)
270 return -EINVAL;
271 return 0;
272}
273
274static int vidioc_s_frequency(struct file *file, void *priv,
275 struct v4l2_frequency *f)
276{
277 struct video_device *dev = video_devdata(file);
Hans Verkuil601e9442008-08-23 07:24:07 -0300278 struct rt_device *rt = video_get_drvdata(dev);
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300279
280 rt->curfreq = f->frequency;
281 rt_setfreq(rt, rt->curfreq);
282 return 0;
283}
284
285static int vidioc_g_frequency(struct file *file, void *priv,
286 struct v4l2_frequency *f)
287{
288 struct video_device *dev = video_devdata(file);
Hans Verkuil601e9442008-08-23 07:24:07 -0300289 struct rt_device *rt = video_get_drvdata(dev);
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300290
291 f->type = V4L2_TUNER_RADIO;
292 f->frequency = rt->curfreq;
293 return 0;
294}
295
296static int vidioc_queryctrl(struct file *file, void *priv,
297 struct v4l2_queryctrl *qc)
298{
299 int i;
300
301 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
302 if (qc->id && qc->id == radio_qctrl[i].id) {
303 memcpy(qc, &(radio_qctrl[i]),
304 sizeof(*qc));
305 return 0;
306 }
307 }
308 return -EINVAL;
309}
310
311static int vidioc_g_ctrl(struct file *file, void *priv,
312 struct v4l2_control *ctrl)
313{
314 struct video_device *dev = video_devdata(file);
Hans Verkuil601e9442008-08-23 07:24:07 -0300315 struct rt_device *rt = video_get_drvdata(dev);
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300316
317 switch (ctrl->id) {
318 case V4L2_CID_AUDIO_MUTE:
319 ctrl->value = rt->muted;
320 return 0;
321 case V4L2_CID_AUDIO_VOLUME:
322 ctrl->value = rt->curvol * 6554;
323 return 0;
324 }
325 return -EINVAL;
326}
327
328static int vidioc_s_ctrl(struct file *file, void *priv,
329 struct v4l2_control *ctrl)
330{
331 struct video_device *dev = video_devdata(file);
Hans Verkuil601e9442008-08-23 07:24:07 -0300332 struct rt_device *rt = video_get_drvdata(dev);
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300333
334 switch (ctrl->id) {
335 case V4L2_CID_AUDIO_MUTE:
336 if (ctrl->value)
337 rt_mute(rt);
338 else
339 rt_setvol(rt,rt->curvol);
340 return 0;
341 case V4L2_CID_AUDIO_VOLUME:
342 rt_setvol(rt,ctrl->value);
343 return 0;
344 }
345 return -EINVAL;
346}
347
348static int vidioc_g_audio (struct file *file, void *priv,
349 struct v4l2_audio *a)
350{
351 if (a->index > 1)
352 return -EINVAL;
353
354 strcpy(a->name, "Radio");
355 a->capability = V4L2_AUDCAP_STEREO;
356 return 0;
357}
358
359static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
360{
361 *i = 0;
362 return 0;
363}
364
365static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
366{
367 if (i != 0)
368 return -EINVAL;
369 return 0;
370}
371
372static int vidioc_s_audio(struct file *file, void *priv,
373 struct v4l2_audio *a)
374{
375 if (a->index != 0)
376 return -EINVAL;
377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380static struct rt_device rtrack_unit;
381
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300382static int rtrack_exclusive_open(struct inode *inode, struct file *file)
383{
384 return test_and_set_bit(0, &rtrack_unit.in_use) ? -EBUSY : 0;
385}
386
387static int rtrack_exclusive_release(struct inode *inode, struct file *file)
388{
389 clear_bit(0, &rtrack_unit.in_use);
390 return 0;
391}
392
Arjan van de Venfa027c22007-02-12 00:55:33 -0800393static const struct file_operations rtrack_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 .owner = THIS_MODULE,
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300395 .open = rtrack_exclusive_open,
396 .release = rtrack_exclusive_release,
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300397 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300398#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200399 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300400#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 .llseek = no_llseek,
402};
403
Hans Verkuila3998102008-07-21 02:57:38 -0300404static const struct v4l2_ioctl_ops rtrack_ioctl_ops = {
Douglas Landgraf385e8d82007-04-25 00:14:36 -0300405 .vidioc_querycap = vidioc_querycap,
406 .vidioc_g_tuner = vidioc_g_tuner,
407 .vidioc_s_tuner = vidioc_s_tuner,
408 .vidioc_g_audio = vidioc_g_audio,
409 .vidioc_s_audio = vidioc_s_audio,
410 .vidioc_g_input = vidioc_g_input,
411 .vidioc_s_input = vidioc_s_input,
412 .vidioc_g_frequency = vidioc_g_frequency,
413 .vidioc_s_frequency = vidioc_s_frequency,
414 .vidioc_queryctrl = vidioc_queryctrl,
415 .vidioc_g_ctrl = vidioc_g_ctrl,
416 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417};
418
Hans Verkuila3998102008-07-21 02:57:38 -0300419static struct video_device rtrack_radio = {
Hans Verkuila3998102008-07-21 02:57:38 -0300420 .name = "RadioTrack radio",
Hans Verkuila3998102008-07-21 02:57:38 -0300421 .fops = &rtrack_fops,
422 .ioctl_ops = &rtrack_ioctl_ops,
Hans Verkuilaa5e90a2008-08-23 06:23:55 -0300423 .release = video_device_release_empty,
Hans Verkuila3998102008-07-21 02:57:38 -0300424};
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static int __init rtrack_init(void)
427{
428 if(io==-1)
429 {
430 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
431 return -EINVAL;
432 }
433
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300434 if (!request_region(io, 2, "rtrack"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 {
436 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
437 return -EBUSY;
438 }
439
Hans Verkuil601e9442008-08-23 07:24:07 -0300440 video_set_drvdata(&rtrack_radio, &rtrack_unit);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300441
Hans Verkuilcba99ae2008-09-03 17:11:58 -0300442 if (video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 release_region(io, 2);
444 return -EINVAL;
445 }
446 printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
447
448 /* Set up the I/O locking */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300449
Ingo Molnar3593cab2006-02-07 06:49:14 -0200450 mutex_init(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300451
452 /* mute card - prevents noisy bootups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /* this ensures that the volume is all the way down */
455 outb(0x48, io); /* volume down but still "on" */
456 sleep_delay(2000000); /* make sure it's totally down */
457 outb(0xc0, io); /* steady volume, mute card */
458 rtrack_unit.curvol = 0;
459
460 return 0;
461}
462
463MODULE_AUTHOR("M.Kirkwood");
464MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
465MODULE_LICENSE("GPL");
466
467module_param(io, int, 0);
468MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
469module_param(radio_nr, int, 0);
470
471static void __exit cleanup_rtrack_module(void)
472{
473 video_unregister_device(&rtrack_radio);
474 release_region(io,2);
475}
476
477module_init(rtrack_init);
478module_exit(cleanup_rtrack_module);
479