blob: df22a582e7a2fae4762b07801313eec005bbc66d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* radiotrack (radioreveal) driver for Linux radio support
2 * (c) 1997 M. Kirkwood
3 * Converted to new API by Alan Cox <Alan.Cox@linux.org>
4 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
5 *
6 * History:
7 * 1999-02-24 Russell Kroll <rkroll@exploits.org>
8 * Fine tuning/VIDEO_TUNER_LOW
9 * Frequency range expanded to start at 87 MHz
10 *
11 * TODO: Allow for more than one of these foolish entities :-)
12 *
13 * Notes on the hardware (reverse engineered from other peoples'
14 * reverse engineering of AIMS' code :-)
15 *
16 * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
17 *
18 * The signal strength query is unsurprisingly inaccurate. And it seems
19 * to indicate that (on my card, at least) the frequency setting isn't
20 * too great. (I have to tune up .025MHz from what the freq should be
21 * to get a report that the thing is tuned.)
22 *
23 * Volume control is (ugh) analogue:
24 * out(port, start_increasing_volume);
25 * wait(a_wee_while);
26 * out(port, stop_changing_the_volume);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030027 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 */
29
30#include <linux/module.h> /* Modules */
31#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070032#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h> /* udelay */
34#include <asm/io.h> /* outb, outb_p */
35#include <asm/uaccess.h> /* copy to/from user */
36#include <linux/videodev.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030037#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/config.h> /* CONFIG_RADIO_RTRACK_PORT */
39#include <asm/semaphore.h> /* Lock for the I/O */
40
41#ifndef CONFIG_RADIO_RTRACK_PORT
42#define CONFIG_RADIO_RTRACK_PORT -1
43#endif
44
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030045static int io = CONFIG_RADIO_RTRACK_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static int radio_nr = -1;
Ingo Molnar3593cab2006-02-07 06:49:14 -020047static struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49struct rt_device
50{
51 int port;
52 int curvol;
53 unsigned long curfreq;
54 int muted;
55};
56
57
58/* local things */
59
60static void sleep_delay(long n)
61{
62 /* Sleep nicely for 'n' uS */
63 int d=n/(1000000/HZ);
64 if(!d)
65 udelay(n);
66 else
67 msleep(jiffies_to_msecs(d));
68}
69
70static void rt_decvol(void)
71{
72 outb(0x58, io); /* volume down + sigstr + on */
73 sleep_delay(100000);
74 outb(0xd8, io); /* volume steady + sigstr + on */
75}
76
77static void rt_incvol(void)
78{
79 outb(0x98, io); /* volume up + sigstr + on */
80 sleep_delay(100000);
81 outb(0xd8, io); /* volume steady + sigstr + on */
82}
83
84static void rt_mute(struct rt_device *dev)
85{
86 dev->muted = 1;
Ingo Molnar3593cab2006-02-07 06:49:14 -020087 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 outb(0xd0, io); /* volume steady, off */
Ingo Molnar3593cab2006-02-07 06:49:14 -020089 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92static int rt_setvol(struct rt_device *dev, int vol)
93{
94 int i;
95
Ingo Molnar3593cab2006-02-07 06:49:14 -020096 mutex_lock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if(vol == dev->curvol) { /* requested volume = current */
99 if (dev->muted) { /* user is unmuting the card */
100 dev->muted = 0;
101 outb (0xd8, io); /* enable card */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300102 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200103 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 0;
105 }
106
107 if(vol == 0) { /* volume = 0 means mute the card */
108 outb(0x48, io); /* volume down but still "on" */
109 sleep_delay(2000000); /* make sure it's totally down */
110 outb(0xd0, io); /* volume steady, off */
111 dev->curvol = 0; /* track the volume state! */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200112 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
114 }
115
116 dev->muted = 0;
117 if(vol > dev->curvol)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300118 for(i = dev->curvol; i < vol; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 rt_incvol();
120 else
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_decvol();
123
124 dev->curvol = vol;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200125 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 0;
127}
128
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300129/* the 128+64 on these outb's is to keep the volume stable while tuning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * without them, the volume _will_ creep up with each frequency change
131 * and bit 4 (+16) is to keep the signal strength meter enabled
132 */
133
134static void send_0_byte(int port, struct rt_device *dev)
135{
136 if ((dev->curvol == 0) || (dev->muted)) {
137 outb_p(128+64+16+ 1, port); /* wr-enable + data low */
138 outb_p(128+64+16+2+1, port); /* clock */
139 }
140 else {
141 outb_p(128+64+16+8+ 1, port); /* on + wr-enable + data low */
142 outb_p(128+64+16+8+2+1, port); /* clock */
143 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300144 sleep_delay(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static void send_1_byte(int port, struct rt_device *dev)
148{
149 if ((dev->curvol == 0) || (dev->muted)) {
150 outb_p(128+64+16+4 +1, port); /* wr-enable+data high */
151 outb_p(128+64+16+4+2+1, port); /* clock */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 else {
154 outb_p(128+64+16+8+4 +1, port); /* on+wr-enable+data high */
155 outb_p(128+64+16+8+4+2+1, port); /* clock */
156 }
157
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300158 sleep_delay(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static int rt_setfreq(struct rt_device *dev, unsigned long freq)
162{
163 int i;
164
165 /* adapted from radio-aztech.c */
166
167 /* now uses VIDEO_TUNER_LOW for fine tuning */
168
169 freq += 171200; /* Add 10.7 MHz IF */
170 freq /= 800; /* Convert to 50 kHz units */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300171
Ingo Molnar3593cab2006-02-07 06:49:14 -0200172 mutex_lock(&lock); /* Stop other ops interfering */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 send_0_byte (io, dev); /* 0: LSB of frequency */
175
176 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
177 if (freq & (1 << i))
178 send_1_byte (io, dev);
179 else
180 send_0_byte (io, dev);
181
182 send_0_byte (io, dev); /* 14: test bit - always 0 */
183 send_0_byte (io, dev); /* 15: test bit - always 0 */
184
185 send_0_byte (io, dev); /* 16: band data 0 - always 0 */
186 send_0_byte (io, dev); /* 17: band data 1 - always 0 */
187 send_0_byte (io, dev); /* 18: band data 2 - always 0 */
188 send_0_byte (io, dev); /* 19: time base - always 0 */
189
190 send_0_byte (io, dev); /* 20: spacing (0 = 25 kHz) */
191 send_1_byte (io, dev); /* 21: spacing (1 = 25 kHz) */
192 send_0_byte (io, dev); /* 22: spacing (0 = 25 kHz) */
193 send_1_byte (io, dev); /* 23: AM/FM (FM = 1, always) */
194
195 if ((dev->curvol == 0) || (dev->muted))
196 outb (0xd0, io); /* volume steady + sigstr */
197 else
198 outb (0xd8, io); /* volume steady + sigstr + on */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300199
Ingo Molnar3593cab2006-02-07 06:49:14 -0200200 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 return 0;
203}
204
205static int rt_getsigstr(struct rt_device *dev)
206{
207 if (inb(io) & 2) /* bit set = no signal present */
208 return 0;
209 return 1; /* signal present */
210}
211
212static int rt_do_ioctl(struct inode *inode, struct file *file,
213 unsigned int cmd, void *arg)
214{
215 struct video_device *dev = video_devdata(file);
216 struct rt_device *rt=dev->priv;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 switch(cmd)
219 {
220 case VIDIOCGCAP:
221 {
222 struct video_capability *v = arg;
223 memset(v,0,sizeof(*v));
224 v->type=VID_TYPE_TUNER;
225 v->channels=1;
226 v->audios=1;
227 strcpy(v->name, "RadioTrack");
228 return 0;
229 }
230 case VIDIOCGTUNER:
231 {
232 struct video_tuner *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300233 if(v->tuner) /* Only 1 tuner */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EINVAL;
235 v->rangelow=(87*16000);
236 v->rangehigh=(108*16000);
237 v->flags=VIDEO_TUNER_LOW;
238 v->mode=VIDEO_MODE_AUTO;
239 strcpy(v->name, "FM");
240 v->signal=0xFFFF*rt_getsigstr(rt);
241 return 0;
242 }
243 case VIDIOCSTUNER:
244 {
245 struct video_tuner *v = arg;
246 if(v->tuner!=0)
247 return -EINVAL;
248 /* Only 1 tuner so no setting needed ! */
249 return 0;
250 }
251 case VIDIOCGFREQ:
252 {
253 unsigned long *freq = arg;
254 *freq = rt->curfreq;
255 return 0;
256 }
257 case VIDIOCSFREQ:
258 {
259 unsigned long *freq = arg;
260 rt->curfreq = *freq;
261 rt_setfreq(rt, rt->curfreq);
262 return 0;
263 }
264 case VIDIOCGAUDIO:
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300265 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct video_audio *v = arg;
267 memset(v,0, sizeof(*v));
268 v->flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
269 v->volume=rt->curvol * 6554;
270 v->step=6554;
271 strcpy(v->name, "Radio");
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 case VIDIOCSAUDIO:
275 {
276 struct video_audio *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300277 if(v->audio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -EINVAL;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300279 if(v->flags&VIDEO_AUDIO_MUTE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 rt_mute(rt);
281 else
282 rt_setvol(rt,v->volume/6554);
283 return 0;
284 }
285 default:
286 return -ENOIOCTLCMD;
287 }
288}
289
290static int rt_ioctl(struct inode *inode, struct file *file,
291 unsigned int cmd, unsigned long arg)
292{
293 return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
294}
295
296static struct rt_device rtrack_unit;
297
298static struct file_operations rtrack_fops = {
299 .owner = THIS_MODULE,
300 .open = video_exclusive_open,
301 .release = video_exclusive_release,
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300302 .ioctl = rt_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200303 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .llseek = no_llseek,
305};
306
307static struct video_device rtrack_radio=
308{
309 .owner = THIS_MODULE,
310 .name = "RadioTrack radio",
311 .type = VID_TYPE_TUNER,
312 .hardware = VID_HARDWARE_RTRACK,
313 .fops = &rtrack_fops,
314};
315
316static int __init rtrack_init(void)
317{
318 if(io==-1)
319 {
320 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
321 return -EINVAL;
322 }
323
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300324 if (!request_region(io, 2, "rtrack"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 {
326 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
327 return -EBUSY;
328 }
329
330 rtrack_radio.priv=&rtrack_unit;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
333 {
334 release_region(io, 2);
335 return -EINVAL;
336 }
337 printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
338
339 /* Set up the I/O locking */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300340
Ingo Molnar3593cab2006-02-07 06:49:14 -0200341 mutex_init(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300342
343 /* mute card - prevents noisy bootups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* this ensures that the volume is all the way down */
346 outb(0x48, io); /* volume down but still "on" */
347 sleep_delay(2000000); /* make sure it's totally down */
348 outb(0xc0, io); /* steady volume, mute card */
349 rtrack_unit.curvol = 0;
350
351 return 0;
352}
353
354MODULE_AUTHOR("M.Kirkwood");
355MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
356MODULE_LICENSE("GPL");
357
358module_param(io, int, 0);
359MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
360module_param(radio_nr, int, 0);
361
362static void __exit cleanup_rtrack_module(void)
363{
364 video_unregister_device(&rtrack_radio);
365 release_region(io,2);
366}
367
368module_init(rtrack_init);
369module_exit(cleanup_rtrack_module);
370