radio-aimslab.c needs #include <linux/delay.h>
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / radio / radio-aimslab.c
bloba91642cd92222817f40ba1f4c171fa4a3f56becc
1 /* radiotrack (radioreveal) driver for Linux radio support
2 * (c) 1997 M. Kirkwood
3 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
4 * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
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
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);
26 * wait(a_wee_while);
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> /* msleep */
35 #include <linux/videodev2.h> /* kernel radio structs */
36 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
37 #include <linux/io.h> /* outb, outb_p */
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
41 MODULE_AUTHOR("M.Kirkwood");
42 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
43 MODULE_LICENSE("GPL");
45 #ifndef CONFIG_RADIO_RTRACK_PORT
46 #define CONFIG_RADIO_RTRACK_PORT -1
47 #endif
49 static int io = CONFIG_RADIO_RTRACK_PORT;
50 static int radio_nr = -1;
52 module_param(io, int, 0);
53 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
54 module_param(radio_nr, int, 0);
56 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
58 struct rtrack
60 struct v4l2_device v4l2_dev;
61 struct video_device vdev;
62 int port;
63 int curvol;
64 unsigned long curfreq;
65 int muted;
66 int io;
67 struct mutex lock;
70 static struct rtrack rtrack_card;
72 /* local things */
74 static void rt_decvol(struct rtrack *rt)
76 outb(0x58, rt->io); /* volume down + sigstr + on */
77 msleep(100);
78 outb(0xd8, rt->io); /* volume steady + sigstr + on */
81 static void rt_incvol(struct rtrack *rt)
83 outb(0x98, rt->io); /* volume up + sigstr + on */
84 msleep(100);
85 outb(0xd8, rt->io); /* volume steady + sigstr + on */
88 static void rt_mute(struct rtrack *rt)
90 rt->muted = 1;
91 mutex_lock(&rt->lock);
92 outb(0xd0, rt->io); /* volume steady, off */
93 mutex_unlock(&rt->lock);
96 static int rt_setvol(struct rtrack *rt, int vol)
98 int i;
100 mutex_lock(&rt->lock);
102 if (vol == rt->curvol) { /* requested volume = current */
103 if (rt->muted) { /* user is unmuting the card */
104 rt->muted = 0;
105 outb(0xd8, rt->io); /* enable card */
107 mutex_unlock(&rt->lock);
108 return 0;
111 if (vol == 0) { /* volume = 0 means mute the card */
112 outb(0x48, rt->io); /* volume down but still "on" */
113 msleep(2000); /* make sure it's totally down */
114 outb(0xd0, rt->io); /* volume steady, off */
115 rt->curvol = 0; /* track the volume state! */
116 mutex_unlock(&rt->lock);
117 return 0;
120 rt->muted = 0;
121 if (vol > rt->curvol)
122 for (i = rt->curvol; i < vol; i++)
123 rt_incvol(rt);
124 else
125 for (i = rt->curvol; i > vol; i--)
126 rt_decvol(rt);
128 rt->curvol = vol;
129 mutex_unlock(&rt->lock);
130 return 0;
133 /* the 128+64 on these outb's is to keep the volume stable while tuning
134 * without them, the volume _will_ creep up with each frequency change
135 * and bit 4 (+16) is to keep the signal strength meter enabled
138 static void send_0_byte(struct rtrack *rt)
140 if (rt->curvol == 0 || rt->muted) {
141 outb_p(128+64+16+ 1, rt->io); /* wr-enable + data low */
142 outb_p(128+64+16+2+1, rt->io); /* clock */
144 else {
145 outb_p(128+64+16+8+ 1, rt->io); /* on + wr-enable + data low */
146 outb_p(128+64+16+8+2+1, rt->io); /* clock */
148 msleep(1);
151 static void send_1_byte(struct rtrack *rt)
153 if (rt->curvol == 0 || rt->muted) {
154 outb_p(128+64+16+4 +1, rt->io); /* wr-enable+data high */
155 outb_p(128+64+16+4+2+1, rt->io); /* clock */
157 else {
158 outb_p(128+64+16+8+4 +1, rt->io); /* on+wr-enable+data high */
159 outb_p(128+64+16+8+4+2+1, rt->io); /* clock */
162 msleep(1);
165 static int rt_setfreq(struct rtrack *rt, unsigned long freq)
167 int i;
169 mutex_lock(&rt->lock); /* Stop other ops interfering */
171 rt->curfreq = freq;
173 /* now uses VIDEO_TUNER_LOW for fine tuning */
175 freq += 171200; /* Add 10.7 MHz IF */
176 freq /= 800; /* Convert to 50 kHz units */
178 send_0_byte(rt); /* 0: LSB of frequency */
180 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
181 if (freq & (1 << i))
182 send_1_byte(rt);
183 else
184 send_0_byte(rt);
186 send_0_byte(rt); /* 14: test bit - always 0 */
187 send_0_byte(rt); /* 15: test bit - always 0 */
189 send_0_byte(rt); /* 16: band data 0 - always 0 */
190 send_0_byte(rt); /* 17: band data 1 - always 0 */
191 send_0_byte(rt); /* 18: band data 2 - always 0 */
192 send_0_byte(rt); /* 19: time base - always 0 */
194 send_0_byte(rt); /* 20: spacing (0 = 25 kHz) */
195 send_1_byte(rt); /* 21: spacing (1 = 25 kHz) */
196 send_0_byte(rt); /* 22: spacing (0 = 25 kHz) */
197 send_1_byte(rt); /* 23: AM/FM (FM = 1, always) */
199 if (rt->curvol == 0 || rt->muted)
200 outb(0xd0, rt->io); /* volume steady + sigstr */
201 else
202 outb(0xd8, rt->io); /* volume steady + sigstr + on */
204 mutex_unlock(&rt->lock);
206 return 0;
209 static int rt_getsigstr(struct rtrack *rt)
211 int sig = 1;
213 mutex_lock(&rt->lock);
214 if (inb(rt->io) & 2) /* bit set = no signal present */
215 sig = 0;
216 mutex_unlock(&rt->lock);
217 return sig;
220 static int vidioc_querycap(struct file *file, void *priv,
221 struct v4l2_capability *v)
223 strlcpy(v->driver, "radio-aimslab", sizeof(v->driver));
224 strlcpy(v->card, "RadioTrack", sizeof(v->card));
225 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
226 v->version = RADIO_VERSION;
227 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
228 return 0;
231 static int vidioc_g_tuner(struct file *file, void *priv,
232 struct v4l2_tuner *v)
234 struct rtrack *rt = video_drvdata(file);
236 if (v->index > 0)
237 return -EINVAL;
239 strlcpy(v->name, "FM", sizeof(v->name));
240 v->type = V4L2_TUNER_RADIO;
241 v->rangelow = 87 * 16000;
242 v->rangehigh = 108 * 16000;
243 v->rxsubchans = V4L2_TUNER_SUB_MONO;
244 v->capability = V4L2_TUNER_CAP_LOW;
245 v->audmode = V4L2_TUNER_MODE_MONO;
246 v->signal = 0xffff * rt_getsigstr(rt);
247 return 0;
250 static int vidioc_s_tuner(struct file *file, void *priv,
251 struct v4l2_tuner *v)
253 return v->index ? -EINVAL : 0;
256 static int vidioc_s_frequency(struct file *file, void *priv,
257 struct v4l2_frequency *f)
259 struct rtrack *rt = video_drvdata(file);
261 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
262 return -EINVAL;
263 rt_setfreq(rt, f->frequency);
264 return 0;
267 static int vidioc_g_frequency(struct file *file, void *priv,
268 struct v4l2_frequency *f)
270 struct rtrack *rt = video_drvdata(file);
272 if (f->tuner != 0)
273 return -EINVAL;
274 f->type = V4L2_TUNER_RADIO;
275 f->frequency = rt->curfreq;
276 return 0;
279 static int vidioc_queryctrl(struct file *file, void *priv,
280 struct v4l2_queryctrl *qc)
282 switch (qc->id) {
283 case V4L2_CID_AUDIO_MUTE:
284 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
285 case V4L2_CID_AUDIO_VOLUME:
286 return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff);
288 return -EINVAL;
291 static int vidioc_g_ctrl(struct file *file, void *priv,
292 struct v4l2_control *ctrl)
294 struct rtrack *rt = video_drvdata(file);
296 switch (ctrl->id) {
297 case V4L2_CID_AUDIO_MUTE:
298 ctrl->value = rt->muted;
299 return 0;
300 case V4L2_CID_AUDIO_VOLUME:
301 ctrl->value = rt->curvol;
302 return 0;
304 return -EINVAL;
307 static int vidioc_s_ctrl(struct file *file, void *priv,
308 struct v4l2_control *ctrl)
310 struct rtrack *rt = video_drvdata(file);
312 switch (ctrl->id) {
313 case V4L2_CID_AUDIO_MUTE:
314 if (ctrl->value)
315 rt_mute(rt);
316 else
317 rt_setvol(rt, rt->curvol);
318 return 0;
319 case V4L2_CID_AUDIO_VOLUME:
320 rt_setvol(rt, ctrl->value);
321 return 0;
323 return -EINVAL;
326 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
328 *i = 0;
329 return 0;
332 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
334 return i ? -EINVAL : 0;
337 static int vidioc_g_audio(struct file *file, void *priv,
338 struct v4l2_audio *a)
340 a->index = 0;
341 strlcpy(a->name, "Radio", sizeof(a->name));
342 a->capability = V4L2_AUDCAP_STEREO;
343 return 0;
346 static int vidioc_s_audio(struct file *file, void *priv,
347 struct v4l2_audio *a)
349 return a->index ? -EINVAL : 0;
352 static const struct v4l2_file_operations rtrack_fops = {
353 .owner = THIS_MODULE,
354 .ioctl = video_ioctl2,
357 static const struct v4l2_ioctl_ops rtrack_ioctl_ops = {
358 .vidioc_querycap = vidioc_querycap,
359 .vidioc_g_tuner = vidioc_g_tuner,
360 .vidioc_s_tuner = vidioc_s_tuner,
361 .vidioc_g_audio = vidioc_g_audio,
362 .vidioc_s_audio = vidioc_s_audio,
363 .vidioc_g_input = vidioc_g_input,
364 .vidioc_s_input = vidioc_s_input,
365 .vidioc_g_frequency = vidioc_g_frequency,
366 .vidioc_s_frequency = vidioc_s_frequency,
367 .vidioc_queryctrl = vidioc_queryctrl,
368 .vidioc_g_ctrl = vidioc_g_ctrl,
369 .vidioc_s_ctrl = vidioc_s_ctrl,
372 static int __init rtrack_init(void)
374 struct rtrack *rt = &rtrack_card;
375 struct v4l2_device *v4l2_dev = &rt->v4l2_dev;
376 int res;
378 strlcpy(v4l2_dev->name, "rtrack", sizeof(v4l2_dev->name));
379 rt->io = io;
381 if (rt->io == -1) {
382 v4l2_err(v4l2_dev, "you must set an I/O address with io=0x20f or 0x30f\n");
383 return -EINVAL;
386 if (!request_region(rt->io, 2, "rtrack")) {
387 v4l2_err(v4l2_dev, "port 0x%x already in use\n", rt->io);
388 return -EBUSY;
391 res = v4l2_device_register(NULL, v4l2_dev);
392 if (res < 0) {
393 release_region(rt->io, 2);
394 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
395 return res;
398 strlcpy(rt->vdev.name, v4l2_dev->name, sizeof(rt->vdev.name));
399 rt->vdev.v4l2_dev = v4l2_dev;
400 rt->vdev.fops = &rtrack_fops;
401 rt->vdev.ioctl_ops = &rtrack_ioctl_ops;
402 rt->vdev.release = video_device_release_empty;
403 video_set_drvdata(&rt->vdev, rt);
405 if (video_register_device(&rt->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
406 v4l2_device_unregister(&rt->v4l2_dev);
407 release_region(rt->io, 2);
408 return -EINVAL;
410 v4l2_info(v4l2_dev, "AIMSlab RadioTrack/RadioReveal card driver.\n");
412 /* Set up the I/O locking */
414 mutex_init(&rt->lock);
416 /* mute card - prevents noisy bootups */
418 /* this ensures that the volume is all the way down */
419 outb(0x48, rt->io); /* volume down but still "on" */
420 msleep(2000); /* make sure it's totally down */
421 outb(0xc0, rt->io); /* steady volume, mute card */
423 return 0;
426 static void __exit rtrack_exit(void)
428 struct rtrack *rt = &rtrack_card;
430 video_unregister_device(&rt->vdev);
431 v4l2_device_unregister(&rt->v4l2_dev);
432 release_region(rt->io, 2);
435 module_init(rtrack_init);
436 module_exit(rtrack_exit);