blob: 7b9043f9d9bcb481f71bf7c2ed92986083737f45 [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#include <asm/semaphore.h> /* Lock for the I/O */
40
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{
54 int port;
55 int curvol;
56 unsigned long curfreq;
57 int muted;
58};
59
60
61/* local things */
62
63static void sleep_delay(long n)
64{
65 /* Sleep nicely for 'n' uS */
66 int d=n/(1000000/HZ);
67 if(!d)
68 udelay(n);
69 else
70 msleep(jiffies_to_msecs(d));
71}
72
73static void rt_decvol(void)
74{
75 outb(0x58, io); /* volume down + sigstr + on */
76 sleep_delay(100000);
77 outb(0xd8, io); /* volume steady + sigstr + on */
78}
79
80static void rt_incvol(void)
81{
82 outb(0x98, io); /* volume up + sigstr + on */
83 sleep_delay(100000);
84 outb(0xd8, io); /* volume steady + sigstr + on */
85}
86
87static void rt_mute(struct rt_device *dev)
88{
89 dev->muted = 1;
Ingo Molnar3593cab2006-02-07 06:49:14 -020090 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 outb(0xd0, io); /* volume steady, off */
Ingo Molnar3593cab2006-02-07 06:49:14 -020092 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95static int rt_setvol(struct rt_device *dev, int vol)
96{
97 int i;
98
Ingo Molnar3593cab2006-02-07 06:49:14 -020099 mutex_lock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if(vol == dev->curvol) { /* requested volume = current */
102 if (dev->muted) { /* user is unmuting the card */
103 dev->muted = 0;
104 outb (0xd8, io); /* enable card */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300105 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200106 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108 }
109
110 if(vol == 0) { /* volume = 0 means mute the card */
111 outb(0x48, io); /* volume down but still "on" */
112 sleep_delay(2000000); /* make sure it's totally down */
113 outb(0xd0, io); /* volume steady, off */
114 dev->curvol = 0; /* track the volume state! */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200115 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117 }
118
119 dev->muted = 0;
120 if(vol > dev->curvol)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300121 for(i = dev->curvol; i < vol; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 rt_incvol();
123 else
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300124 for(i = dev->curvol; i > vol; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 rt_decvol();
126
127 dev->curvol = vol;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200128 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
130}
131
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300132/* the 128+64 on these outb's is to keep the volume stable while tuning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * without them, the volume _will_ creep up with each frequency change
134 * and bit 4 (+16) is to keep the signal strength meter enabled
135 */
136
137static void send_0_byte(int port, struct rt_device *dev)
138{
139 if ((dev->curvol == 0) || (dev->muted)) {
140 outb_p(128+64+16+ 1, port); /* wr-enable + data low */
141 outb_p(128+64+16+2+1, port); /* clock */
142 }
143 else {
144 outb_p(128+64+16+8+ 1, port); /* on + wr-enable + data low */
145 outb_p(128+64+16+8+2+1, port); /* clock */
146 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300147 sleep_delay(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150static void send_1_byte(int port, struct rt_device *dev)
151{
152 if ((dev->curvol == 0) || (dev->muted)) {
153 outb_p(128+64+16+4 +1, port); /* wr-enable+data high */
154 outb_p(128+64+16+4+2+1, port); /* clock */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 else {
157 outb_p(128+64+16+8+4 +1, port); /* on+wr-enable+data high */
158 outb_p(128+64+16+8+4+2+1, port); /* clock */
159 }
160
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300161 sleep_delay(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164static int rt_setfreq(struct rt_device *dev, unsigned long freq)
165{
166 int i;
167
168 /* adapted from radio-aztech.c */
169
170 /* now uses VIDEO_TUNER_LOW for fine tuning */
171
172 freq += 171200; /* Add 10.7 MHz IF */
173 freq /= 800; /* Convert to 50 kHz units */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300174
Ingo Molnar3593cab2006-02-07 06:49:14 -0200175 mutex_lock(&lock); /* Stop other ops interfering */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 send_0_byte (io, dev); /* 0: LSB of frequency */
178
179 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
180 if (freq & (1 << i))
181 send_1_byte (io, dev);
182 else
183 send_0_byte (io, dev);
184
185 send_0_byte (io, dev); /* 14: test bit - always 0 */
186 send_0_byte (io, dev); /* 15: test bit - always 0 */
187
188 send_0_byte (io, dev); /* 16: band data 0 - always 0 */
189 send_0_byte (io, dev); /* 17: band data 1 - always 0 */
190 send_0_byte (io, dev); /* 18: band data 2 - always 0 */
191 send_0_byte (io, dev); /* 19: time base - always 0 */
192
193 send_0_byte (io, dev); /* 20: spacing (0 = 25 kHz) */
194 send_1_byte (io, dev); /* 21: spacing (1 = 25 kHz) */
195 send_0_byte (io, dev); /* 22: spacing (0 = 25 kHz) */
196 send_1_byte (io, dev); /* 23: AM/FM (FM = 1, always) */
197
198 if ((dev->curvol == 0) || (dev->muted))
199 outb (0xd0, io); /* volume steady + sigstr */
200 else
201 outb (0xd8, io); /* volume steady + sigstr + on */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300202
Ingo Molnar3593cab2006-02-07 06:49:14 -0200203 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 return 0;
206}
207
208static int rt_getsigstr(struct rt_device *dev)
209{
210 if (inb(io) & 2) /* bit set = no signal present */
211 return 0;
212 return 1; /* signal present */
213}
214
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300215static struct v4l2_queryctrl radio_qctrl[] = {
216 {
217 .id = V4L2_CID_AUDIO_MUTE,
218 .name = "Mute",
219 .minimum = 0,
220 .maximum = 1,
221 .default_value = 1,
222 .type = V4L2_CTRL_TYPE_BOOLEAN,
223 },{
224 .id = V4L2_CID_AUDIO_VOLUME,
225 .name = "Volume",
226 .minimum = 0,
227 .maximum = 0xff,
228 .step = 1,
229 .default_value = 0xff,
230 .type = V4L2_CTRL_TYPE_INTEGER,
231 }
232};
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int rt_do_ioctl(struct inode *inode, struct file *file,
235 unsigned int cmd, void *arg)
236{
237 struct video_device *dev = video_devdata(file);
238 struct rt_device *rt=dev->priv;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 switch(cmd)
241 {
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300242 case VIDIOC_QUERYCAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 {
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300244 struct v4l2_capability *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 memset(v,0,sizeof(*v));
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300246 strlcpy(v->driver, "radio-aimslab", sizeof (v->driver));
247 strlcpy(v->card, "RadioTrack", sizeof (v->card));
248 sprintf(v->bus_info,"ISA");
249 v->version = RADIO_VERSION;
250 v->capabilities = V4L2_CAP_TUNER;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253 }
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300254 case VIDIOC_G_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 {
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300256 struct v4l2_tuner *v = arg;
257
258 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -EINVAL;
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300260
261 memset(v,0,sizeof(*v));
262 strcpy(v->name, "FM");
263 v->type = V4L2_TUNER_RADIO;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 v->rangelow=(87*16000);
266 v->rangehigh=(108*16000);
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300267 v->rxsubchans =V4L2_TUNER_SUB_MONO;
268 v->capability=V4L2_TUNER_CAP_LOW;
269 v->audmode = V4L2_TUNER_MODE_MONO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 v->signal=0xFFFF*rt_getsigstr(rt);
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
273 }
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300274 case VIDIOC_S_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 {
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300276 struct v4l2_tuner *v = arg;
277
278 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -EINVAL;
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0;
282 }
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300283 case VIDIOC_S_FREQUENCY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 {
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300285 struct v4l2_frequency *f = arg;
286
287 rt->curfreq = f->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 rt_setfreq(rt, rt->curfreq);
289 return 0;
290 }
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300291 case VIDIOC_G_FREQUENCY:
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300292 {
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300293 struct v4l2_frequency *f = arg;
294
295 f->type = V4L2_TUNER_RADIO;
296 f->frequency = rt->curfreq;
297
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300300 case VIDIOC_QUERYCTRL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 {
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300302 struct v4l2_queryctrl *qc = arg;
303 int i;
304
305 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
306 if (qc->id && qc->id == radio_qctrl[i].id) {
307 memcpy(qc, &(radio_qctrl[i]),
308 sizeof(*qc));
309 return (0);
310 }
311 }
312 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300314 case VIDIOC_G_CTRL:
315 {
316 struct v4l2_control *ctrl= arg;
317
318 switch (ctrl->id) {
319 case V4L2_CID_AUDIO_MUTE:
320 ctrl->value=rt->muted;
321 return (0);
322 case V4L2_CID_AUDIO_VOLUME:
323 ctrl->value=rt->curvol * 6554;
324 return (0);
325 }
326 return -EINVAL;
327 }
328 case VIDIOC_S_CTRL:
329 {
330 struct v4l2_control *ctrl= arg;
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 }
339 return (0);
340 case V4L2_CID_AUDIO_VOLUME:
341 rt_setvol(rt,ctrl->value);
342 return (0);
343 }
344 return -EINVAL;
345 }
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 default:
Mauro Carvalho Chehab46ff2c72006-08-08 09:10:01 -0300348 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
349 rt_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351}
352
353static int rt_ioctl(struct inode *inode, struct file *file,
354 unsigned int cmd, unsigned long arg)
355{
356 return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
357}
358
359static struct rt_device rtrack_unit;
360
361static struct file_operations rtrack_fops = {
362 .owner = THIS_MODULE,
363 .open = video_exclusive_open,
364 .release = video_exclusive_release,
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300365 .ioctl = rt_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200366 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 .llseek = no_llseek,
368};
369
370static struct video_device rtrack_radio=
371{
372 .owner = THIS_MODULE,
373 .name = "RadioTrack radio",
374 .type = VID_TYPE_TUNER,
375 .hardware = VID_HARDWARE_RTRACK,
376 .fops = &rtrack_fops,
377};
378
379static int __init rtrack_init(void)
380{
381 if(io==-1)
382 {
383 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
384 return -EINVAL;
385 }
386
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300387 if (!request_region(io, 2, "rtrack"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 {
389 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
390 return -EBUSY;
391 }
392
393 rtrack_radio.priv=&rtrack_unit;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
396 {
397 release_region(io, 2);
398 return -EINVAL;
399 }
400 printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
401
402 /* Set up the I/O locking */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300403
Ingo Molnar3593cab2006-02-07 06:49:14 -0200404 mutex_init(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300405
406 /* mute card - prevents noisy bootups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* this ensures that the volume is all the way down */
409 outb(0x48, io); /* volume down but still "on" */
410 sleep_delay(2000000); /* make sure it's totally down */
411 outb(0xc0, io); /* steady volume, mute card */
412 rtrack_unit.curvol = 0;
413
414 return 0;
415}
416
417MODULE_AUTHOR("M.Kirkwood");
418MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
419MODULE_LICENSE("GPL");
420
421module_param(io, int, 0);
422MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
423module_param(radio_nr, int, 0);
424
425static void __exit cleanup_rtrack_module(void)
426{
427 video_unregister_device(&rtrack_radio);
428 release_region(io,2);
429}
430
431module_init(rtrack_init);
432module_exit(cleanup_rtrack_module);
433