V4L/DVB (9490): linux-next: v4l-dvb tree build failure
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / radio / radio-aimslab.c
CommitLineData
1da177e4
LT
1/* radiotrack (radioreveal) driver for Linux radio support
2 * (c) 1997 M. Kirkwood
46ff2c72 3 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
1da177e4
LT
4 * 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);
4286c6f6 28 *
1da177e4
LT
29 */
30
31#include <linux/module.h> /* Modules */
32#include <linux/init.h> /* Initdata */
fb911ee8 33#include <linux/ioport.h> /* request_region */
1da177e4
LT
34#include <linux/delay.h> /* udelay */
35#include <asm/io.h> /* outb, outb_p */
36#include <asm/uaccess.h> /* copy to/from user */
46ff2c72 37#include <linux/videodev2.h> /* kernel radio structs */
5e87efa3 38#include <media/v4l2-common.h>
35ea11ff 39#include <media/v4l2-ioctl.h>
1da177e4 40
46ff2c72
MCC
41#include <linux/version.h> /* for KERNEL_VERSION MACRO */
42#define RADIO_VERSION KERNEL_VERSION(0,0,2)
43
1da177e4
LT
44#ifndef CONFIG_RADIO_RTRACK_PORT
45#define CONFIG_RADIO_RTRACK_PORT -1
46#endif
47
4286c6f6 48static int io = CONFIG_RADIO_RTRACK_PORT;
1da177e4 49static int radio_nr = -1;
3593cab5 50static struct mutex lock;
1da177e4
LT
51
52struct rt_device
53{
3ca685aa 54 unsigned long in_use;
1da177e4
LT
55 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 */
a2d66a37 67 int d=n/msecs_to_jiffies(1000);
1da177e4
LT
68 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;
3593cab5 91 mutex_lock(&lock);
1da177e4 92 outb(0xd0, io); /* volume steady, off */
3593cab5 93 mutex_unlock(&lock);
1da177e4
LT
94}
95
96static int rt_setvol(struct rt_device *dev, int vol)
97{
98 int i;
99
3593cab5 100 mutex_lock(&lock);
4286c6f6 101
1da177e4
LT
102 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 */
4286c6f6 106 }
3593cab5 107 mutex_unlock(&lock);
1da177e4
LT
108 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! */
3593cab5 116 mutex_unlock(&lock);
1da177e4
LT
117 return 0;
118 }
119
120 dev->muted = 0;
121 if(vol > dev->curvol)
4286c6f6 122 for(i = dev->curvol; i < vol; i++)
1da177e4
LT
123 rt_incvol();
124 else
4286c6f6 125 for(i = dev->curvol; i > vol; i--)
1da177e4
LT
126 rt_decvol();
127
128 dev->curvol = vol;
3593cab5 129 mutex_unlock(&lock);
1da177e4
LT
130 return 0;
131}
132
4286c6f6 133/* the 128+64 on these outb's is to keep the volume stable while tuning
1da177e4
LT
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
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 }
4286c6f6 148 sleep_delay(1000);
1da177e4
LT
149}
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 */
4286c6f6 156 }
1da177e4
LT
157 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
4286c6f6 162 sleep_delay(1000);
1da177e4
LT
163}
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 */
4286c6f6 175
3593cab5 176 mutex_lock(&lock); /* Stop other ops interfering */
4286c6f6 177
1da177e4
LT
178 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 */
4286c6f6 203
3593cab5 204 mutex_unlock(&lock);
1da177e4
LT
205
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
46ff2c72
MCC
216static 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
385e8d8f
DL
235static int vidioc_querycap(struct file *file, void *priv,
236 struct v4l2_capability *v)
237{
238 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;
244}
245
246static int vidioc_g_tuner(struct file *file, void *priv,
247 struct v4l2_tuner *v)
1da177e4 248{
c170ecf4 249 struct rt_device *rt = video_drvdata(file);
4286c6f6 250
385e8d8f
DL
251 if (v->index > 0)
252 return -EINVAL;
46ff2c72 253
385e8d8f
DL
254 strcpy(v->name, "FM");
255 v->type = V4L2_TUNER_RADIO;
256 v->rangelow = (87*16000);
257 v->rangehigh = (108*16000);
258 v->rxsubchans = V4L2_TUNER_SUB_MONO;
259 v->capability = V4L2_TUNER_CAP_LOW;
260 v->audmode = V4L2_TUNER_MODE_MONO;
261 v->signal = 0xffff*rt_getsigstr(rt);
262 return 0;
263}
264
265static int vidioc_s_tuner(struct file *file, void *priv,
266 struct v4l2_tuner *v)
267{
268 if (v->index > 0)
269 return -EINVAL;
270 return 0;
271}
46ff2c72 272
385e8d8f
DL
273static int vidioc_s_frequency(struct file *file, void *priv,
274 struct v4l2_frequency *f)
275{
c170ecf4 276 struct rt_device *rt = video_drvdata(file);
46ff2c72 277
385e8d8f
DL
278 rt->curfreq = f->frequency;
279 rt_setfreq(rt, rt->curfreq);
280 return 0;
281}
46ff2c72 282
385e8d8f
DL
283static int vidioc_g_frequency(struct file *file, void *priv,
284 struct v4l2_frequency *f)
285{
c170ecf4 286 struct rt_device *rt = video_drvdata(file);
46ff2c72 287
385e8d8f
DL
288 f->type = V4L2_TUNER_RADIO;
289 f->frequency = rt->curfreq;
290 return 0;
291}
46ff2c72 292
385e8d8f
DL
293static int vidioc_queryctrl(struct file *file, void *priv,
294 struct v4l2_queryctrl *qc)
295{
296 int i;
46ff2c72 297
385e8d8f
DL
298 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
299 if (qc->id && qc->id == radio_qctrl[i].id) {
300 memcpy(qc, &(radio_qctrl[i]),
301 sizeof(*qc));
1da177e4
LT
302 return 0;
303 }
385e8d8f
DL
304 }
305 return -EINVAL;
306}
46ff2c72 307
385e8d8f
DL
308static int vidioc_g_ctrl(struct file *file, void *priv,
309 struct v4l2_control *ctrl)
310{
c170ecf4 311 struct rt_device *rt = video_drvdata(file);
46ff2c72 312
385e8d8f
DL
313 switch (ctrl->id) {
314 case V4L2_CID_AUDIO_MUTE:
315 ctrl->value = rt->muted;
316 return 0;
317 case V4L2_CID_AUDIO_VOLUME:
318 ctrl->value = rt->curvol * 6554;
319 return 0;
320 }
321 return -EINVAL;
322}
46ff2c72 323
385e8d8f
DL
324static int vidioc_s_ctrl(struct file *file, void *priv,
325 struct v4l2_control *ctrl)
326{
c170ecf4 327 struct rt_device *rt = video_drvdata(file);
46ff2c72 328
385e8d8f
DL
329 switch (ctrl->id) {
330 case V4L2_CID_AUDIO_MUTE:
331 if (ctrl->value)
332 rt_mute(rt);
333 else
334 rt_setvol(rt,rt->curvol);
335 return 0;
336 case V4L2_CID_AUDIO_VOLUME:
337 rt_setvol(rt,ctrl->value);
338 return 0;
1da177e4 339 }
385e8d8f
DL
340 return -EINVAL;
341}
342
343static int vidioc_g_audio (struct file *file, void *priv,
344 struct v4l2_audio *a)
345{
346 if (a->index > 1)
347 return -EINVAL;
348
349 strcpy(a->name, "Radio");
350 a->capability = V4L2_AUDCAP_STEREO;
351 return 0;
352}
353
354static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
355{
356 *i = 0;
357 return 0;
358}
359
360static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
361{
362 if (i != 0)
363 return -EINVAL;
364 return 0;
1da177e4
LT
365}
366
385e8d8f
DL
367static int vidioc_s_audio(struct file *file, void *priv,
368 struct v4l2_audio *a)
1da177e4 369{
385e8d8f
DL
370 if (a->index != 0)
371 return -EINVAL;
372 return 0;
1da177e4
LT
373}
374
375static struct rt_device rtrack_unit;
376
3ca685aa
HV
377static int rtrack_exclusive_open(struct inode *inode, struct file *file)
378{
379 return test_and_set_bit(0, &rtrack_unit.in_use) ? -EBUSY : 0;
380}
381
382static int rtrack_exclusive_release(struct inode *inode, struct file *file)
383{
384 clear_bit(0, &rtrack_unit.in_use);
385 return 0;
386}
387
fa027c2a 388static const struct file_operations rtrack_fops = {
1da177e4 389 .owner = THIS_MODULE,
3ca685aa
HV
390 .open = rtrack_exclusive_open,
391 .release = rtrack_exclusive_release,
385e8d8f 392 .ioctl = video_ioctl2,
078ff795 393#ifdef CONFIG_COMPAT
0d0fbf81 394 .compat_ioctl = v4l_compat_ioctl32,
078ff795 395#endif
1da177e4
LT
396 .llseek = no_llseek,
397};
398
a399810c 399static const struct v4l2_ioctl_ops rtrack_ioctl_ops = {
385e8d8f
DL
400 .vidioc_querycap = vidioc_querycap,
401 .vidioc_g_tuner = vidioc_g_tuner,
402 .vidioc_s_tuner = vidioc_s_tuner,
403 .vidioc_g_audio = vidioc_g_audio,
404 .vidioc_s_audio = vidioc_s_audio,
405 .vidioc_g_input = vidioc_g_input,
406 .vidioc_s_input = vidioc_s_input,
407 .vidioc_g_frequency = vidioc_g_frequency,
408 .vidioc_s_frequency = vidioc_s_frequency,
409 .vidioc_queryctrl = vidioc_queryctrl,
410 .vidioc_g_ctrl = vidioc_g_ctrl,
411 .vidioc_s_ctrl = vidioc_s_ctrl,
1da177e4
LT
412};
413
a399810c 414static struct video_device rtrack_radio = {
a399810c 415 .name = "RadioTrack radio",
a399810c
HV
416 .fops = &rtrack_fops,
417 .ioctl_ops = &rtrack_ioctl_ops,
aa5e90af 418 .release = video_device_release_empty,
a399810c
HV
419};
420
1da177e4
LT
421static int __init rtrack_init(void)
422{
423 if(io==-1)
424 {
425 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
426 return -EINVAL;
427 }
428
4286c6f6 429 if (!request_region(io, 2, "rtrack"))
1da177e4
LT
430 {
431 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
432 return -EBUSY;
433 }
434
601e9444 435 video_set_drvdata(&rtrack_radio, &rtrack_unit);
4286c6f6 436
cba99ae8 437 if (video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
1da177e4
LT
438 release_region(io, 2);
439 return -EINVAL;
440 }
441 printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
442
443 /* Set up the I/O locking */
4286c6f6 444
3593cab5 445 mutex_init(&lock);
4286c6f6
MCC
446
447 /* mute card - prevents noisy bootups */
1da177e4
LT
448
449 /* this ensures that the volume is all the way down */
450 outb(0x48, io); /* volume down but still "on" */
451 sleep_delay(2000000); /* make sure it's totally down */
452 outb(0xc0, io); /* steady volume, mute card */
453 rtrack_unit.curvol = 0;
454
455 return 0;
456}
457
458MODULE_AUTHOR("M.Kirkwood");
459MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
460MODULE_LICENSE("GPL");
461
462module_param(io, int, 0);
463MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
464module_param(radio_nr, int, 0);
465
466static void __exit cleanup_rtrack_module(void)
467{
468 video_unregister_device(&rtrack_radio);
469 release_region(io,2);
470}
471
472module_init(rtrack_init);
473module_exit(cleanup_rtrack_module);
474