blob: f21d918b3f99d62e0f1b5f18f2924d21a40db7a7 [file] [log] [blame]
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03001/* radio-aztech.c - Aztech radio card driver for Linux 2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -03003 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03004 * Adapted to support the Video for Linux API by
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by:
6 *
7 * Quay Ly
8 * Donald Song
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03009 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
11 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
12 *
13 * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
14 * along with more information on the card itself.
15 *
16 * History:
17 * 1999-02-24 Russell Kroll <rkroll@exploits.org>
18 * Fine tuning/VIDEO_TUNER_LOW
19 * Range expanded to 87-108 MHz (from 87.9-107.8)
20 *
21 * Notable changes from the original source:
22 * - includes stripped down to the essentials
23 * - for loops used as delays replaced with udelay()
24 * - #defines removed, changed to static values
25 * - tuning structure changed - no more character arrays, other changes
26*/
27
28#include <linux/module.h> /* Modules */
29#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070030#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h> /* udelay */
32#include <asm/io.h> /* outb, outb_p */
33#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -030034#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030035#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -030037#include <linux/version.h> /* for KERNEL_VERSION MACRO */
38#define RADIO_VERSION KERNEL_VERSION(0,0,2)
39
40static struct v4l2_queryctrl radio_qctrl[] = {
41 {
42 .id = V4L2_CID_AUDIO_MUTE,
43 .name = "Mute",
44 .minimum = 0,
45 .maximum = 1,
46 .default_value = 1,
47 .type = V4L2_CTRL_TYPE_BOOLEAN,
48 },{
49 .id = V4L2_CID_AUDIO_VOLUME,
50 .name = "Volume",
51 .minimum = 0,
52 .maximum = 0xff,
53 .step = 1,
54 .default_value = 0xff,
55 .type = V4L2_CTRL_TYPE_INTEGER,
56 }
57};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
60
61#ifndef CONFIG_RADIO_AZTECH_PORT
62#define CONFIG_RADIO_AZTECH_PORT -1
63#endif
64
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030065static int io = CONFIG_RADIO_AZTECH_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static int radio_nr = -1;
67static int radio_wait_time = 1000;
Ingo Molnar3593cab2006-02-07 06:49:14 -020068static struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70struct az_device
71{
72 int curvol;
73 unsigned long curfreq;
74 int stereo;
75};
76
77static int volconvert(int level)
78{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030079 level>>=14; /* Map 16bits down to 2 bit */
80 level&=3;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 /* convert to card-friendly values */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030083 switch (level)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030085 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030087 case 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 1;
89 case 2:
90 return 4;
91 case 3:
92 return 5;
93 }
94 return 0; /* Quieten gcc */
95}
96
97static void send_0_byte (struct az_device *dev)
98{
99 udelay(radio_wait_time);
100 outb_p(2+volconvert(dev->curvol), io);
101 outb_p(64+2+volconvert(dev->curvol), io);
102}
103
104static void send_1_byte (struct az_device *dev)
105{
106 udelay (radio_wait_time);
107 outb_p(128+2+volconvert(dev->curvol), io);
108 outb_p(128+64+2+volconvert(dev->curvol), io);
109}
110
111static int az_setvol(struct az_device *dev, int vol)
112{
Ingo Molnar3593cab2006-02-07 06:49:14 -0200113 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 outb (volconvert(vol), io);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200115 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117}
118
119/* thanks to Michael Dwyer for giving me a dose of clues in
120 * the signal strength department..
121 *
122 * This card has a stereo bit - bit 0 set = mono, not set = stereo
123 * It also has a "signal" bit - bit 1 set = bad signal, not set = good
124 *
125 */
126
127static int az_getsigstr(struct az_device *dev)
128{
129 if (inb(io) & 2) /* bit set = no signal present */
130 return 0;
131 return 1; /* signal present */
132}
133
134static int az_getstereo(struct az_device *dev)
135{
136 if (inb(io) & 1) /* bit set = mono */
137 return 0;
138 return 1; /* stereo */
139}
140
141static int az_setfreq(struct az_device *dev, unsigned long frequency)
142{
143 int i;
144
145 frequency += 171200; /* Add 10.7 MHz IF */
146 frequency /= 800; /* Convert to 50 kHz units */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300147
Ingo Molnar3593cab2006-02-07 06:49:14 -0200148 mutex_lock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 send_0_byte (dev); /* 0: LSB of frequency */
151
152 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
153 if (frequency & (1 << i))
154 send_1_byte (dev);
155 else
156 send_0_byte (dev);
157
158 send_0_byte (dev); /* 14: test bit - always 0 */
159 send_0_byte (dev); /* 15: test bit - always 0 */
160 send_0_byte (dev); /* 16: band data 0 - always 0 */
161 if (dev->stereo) /* 17: stereo (1 to enable) */
162 send_1_byte (dev);
163 else
164 send_0_byte (dev);
165
166 send_1_byte (dev); /* 18: band data 1 - unknown */
167 send_0_byte (dev); /* 19: time base - always 0 */
168 send_0_byte (dev); /* 20: spacing (0 = 25 kHz) */
169 send_1_byte (dev); /* 21: spacing (1 = 25 kHz) */
170 send_0_byte (dev); /* 22: spacing (0 = 25 kHz) */
171 send_1_byte (dev); /* 23: AM/FM (FM = 1, always) */
172
173 /* latch frequency */
174
175 udelay (radio_wait_time);
176 outb_p(128+64+volconvert(dev->curvol), io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300177
Ingo Molnar3593cab2006-02-07 06:49:14 -0200178 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 return 0;
181}
182
183static int az_do_ioctl(struct inode *inode, struct file *file,
184 unsigned int cmd, void *arg)
185{
186 struct video_device *dev = video_devdata(file);
187 struct az_device *az = dev->priv;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 switch(cmd)
190 {
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300191 case VIDIOC_QUERYCAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 {
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300193 struct v4l2_capability *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 memset(v,0,sizeof(*v));
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300195 strlcpy(v->driver, "radio-aztech", sizeof (v->driver));
196 strlcpy(v->card, "Aztech Radio", sizeof (v->card));
197 sprintf(v->bus_info,"ISA");
198 v->version = RADIO_VERSION;
199 v->capabilities = V4L2_CAP_TUNER;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202 }
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300203 case VIDIOC_G_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 {
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300205 struct v4l2_tuner *v = arg;
206
207 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -EINVAL;
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300209
210 memset(v,0,sizeof(*v));
211 strcpy(v->name, "FM");
212 v->type = V4L2_TUNER_RADIO;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 v->rangelow=(87*16000);
215 v->rangehigh=(108*16000);
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300216 v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
217 v->capability=V4L2_TUNER_CAP_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if(az_getstereo(az))
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300219 v->audmode = V4L2_TUNER_MODE_STEREO;
220 else
221 v->audmode = V4L2_TUNER_MODE_MONO;
222 v->signal=0xFFFF*az_getsigstr(az);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
225 }
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300226 case VIDIOC_S_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 {
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300228 struct v4l2_tuner *v = arg;
229
230 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -EINVAL;
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
234 }
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300235 case VIDIOC_S_FREQUENCY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 {
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300237 struct v4l2_frequency *f = arg;
238
239 az->curfreq = f->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 az_setfreq(az, az->curfreq);
241 return 0;
242 }
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300243 case VIDIOC_G_FREQUENCY:
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300244 {
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300245 struct v4l2_frequency *f = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300247 f->type = V4L2_TUNER_RADIO;
248 f->frequency = az->curfreq;
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return 0;
251 }
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300252
253 case VIDIOC_QUERYCTRL:
254 {
255 struct v4l2_queryctrl *qc = arg;
256 int i;
257
258 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
259 if (qc->id && qc->id == radio_qctrl[i].id) {
260 memcpy(qc, &(radio_qctrl[i]),
261 sizeof(*qc));
262 return (0);
263 }
264 }
265 return -EINVAL;
266 }
267 case VIDIOC_G_CTRL:
268 {
269 struct v4l2_control *ctrl= arg;
270
271 switch (ctrl->id) {
272 case V4L2_CID_AUDIO_MUTE:
273 if (az->curvol==0)
274 ctrl->value=1;
275 else
276 ctrl->value=0;
277 return (0);
278 case V4L2_CID_AUDIO_VOLUME:
279 ctrl->value=az->curvol * 6554;
280 return (0);
281 }
282 return -EINVAL;
283 }
284 case VIDIOC_S_CTRL:
285 {
286 struct v4l2_control *ctrl= arg;
287
288 switch (ctrl->id) {
289 case V4L2_CID_AUDIO_MUTE:
290 if (ctrl->value) {
291 az_setvol(az,0);
292 } else {
293 az_setvol(az,az->curvol);
294 }
295 return (0);
296 case V4L2_CID_AUDIO_VOLUME:
297 az_setvol(az,ctrl->value);
298 return (0);
299 }
300 return -EINVAL;
301 }
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 default:
Mauro Carvalho Chehaba4366af2006-08-08 09:10:01 -0300304 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
305 az_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307}
308
309static int az_ioctl(struct inode *inode, struct file *file,
310 unsigned int cmd, unsigned long arg)
311{
312 return video_usercopy(inode, file, cmd, arg, az_do_ioctl);
313}
314
315static struct az_device aztech_unit;
316
317static struct file_operations aztech_fops = {
318 .owner = THIS_MODULE,
319 .open = video_exclusive_open,
320 .release = video_exclusive_release,
321 .ioctl = az_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200322 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .llseek = no_llseek,
324};
325
326static struct video_device aztech_radio=
327{
328 .owner = THIS_MODULE,
329 .name = "Aztech radio",
330 .type = VID_TYPE_TUNER,
331 .hardware = VID_HARDWARE_AZTECH,
332 .fops = &aztech_fops,
333};
334
335static int __init aztech_init(void)
336{
337 if(io==-1)
338 {
339 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
340 return -EINVAL;
341 }
342
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300343 if (!request_region(io, 2, "aztech"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 {
345 printk(KERN_ERR "aztech: port 0x%x already in use\n", io);
346 return -EBUSY;
347 }
348
Ingo Molnar3593cab2006-02-07 06:49:14 -0200349 mutex_init(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 aztech_radio.priv=&aztech_unit;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if(video_register_device(&aztech_radio, VFL_TYPE_RADIO, radio_nr)==-1)
353 {
354 release_region(io,2);
355 return -EINVAL;
356 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printk(KERN_INFO "Aztech radio card driver v1.00/19990224 rkroll@exploits.org\n");
359 /* mute card - prevents noisy bootups */
360 outb (0, io);
361 return 0;
362}
363
364MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
365MODULE_DESCRIPTION("A driver for the Aztech radio card.");
366MODULE_LICENSE("GPL");
367
368module_param(io, int, 0);
369module_param(radio_nr, int, 0);
370MODULE_PARM_DESC(io, "I/O address of the Aztech card (0x350 or 0x358)");
371
372static void __exit aztech_cleanup(void)
373{
374 video_unregister_device(&aztech_radio);
375 release_region(io,2);
376}
377
378module_init(aztech_init);
379module_exit(aztech_cleanup);