blob: 95e6322133ee43289c1077af080cf50a57825db8 [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 Chehab4286c6f2006-04-08 16:06:16 -03003 * Adapted to support the Video for Linux API by
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by:
5 *
6 * Quay Ly
7 * Donald Song
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03008 * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
10 * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
11 *
12 * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
13 * along with more information on the card itself.
14 *
15 * History:
16 * 1999-02-24 Russell Kroll <rkroll@exploits.org>
17 * Fine tuning/VIDEO_TUNER_LOW
18 * Range expanded to 87-108 MHz (from 87.9-107.8)
19 *
20 * Notable changes from the original source:
21 * - includes stripped down to the essentials
22 * - for loops used as delays replaced with udelay()
23 * - #defines removed, changed to static values
24 * - tuning structure changed - no more character arrays, other changes
25*/
26
27#include <linux/module.h> /* Modules */
28#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070029#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h> /* udelay */
31#include <asm/io.h> /* outb, outb_p */
32#include <asm/uaccess.h> /* copy to/from user */
33#include <linux/videodev.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030034#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/config.h> /* CONFIG_RADIO_AZTECH_PORT */
36
37/* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
38
39#ifndef CONFIG_RADIO_AZTECH_PORT
40#define CONFIG_RADIO_AZTECH_PORT -1
41#endif
42
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030043static int io = CONFIG_RADIO_AZTECH_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int radio_nr = -1;
45static int radio_wait_time = 1000;
Ingo Molnar3593cab2006-02-07 06:49:14 -020046static struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48struct az_device
49{
50 int curvol;
51 unsigned long curfreq;
52 int stereo;
53};
54
55static int volconvert(int level)
56{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030057 level>>=14; /* Map 16bits down to 2 bit */
58 level&=3;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* convert to card-friendly values */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030061 switch (level)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 {
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030063 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030065 case 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 1;
67 case 2:
68 return 4;
69 case 3:
70 return 5;
71 }
72 return 0; /* Quieten gcc */
73}
74
75static void send_0_byte (struct az_device *dev)
76{
77 udelay(radio_wait_time);
78 outb_p(2+volconvert(dev->curvol), io);
79 outb_p(64+2+volconvert(dev->curvol), io);
80}
81
82static void send_1_byte (struct az_device *dev)
83{
84 udelay (radio_wait_time);
85 outb_p(128+2+volconvert(dev->curvol), io);
86 outb_p(128+64+2+volconvert(dev->curvol), io);
87}
88
89static int az_setvol(struct az_device *dev, int vol)
90{
Ingo Molnar3593cab2006-02-07 06:49:14 -020091 mutex_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 outb (volconvert(vol), io);
Ingo Molnar3593cab2006-02-07 06:49:14 -020093 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
95}
96
97/* thanks to Michael Dwyer for giving me a dose of clues in
98 * the signal strength department..
99 *
100 * This card has a stereo bit - bit 0 set = mono, not set = stereo
101 * It also has a "signal" bit - bit 1 set = bad signal, not set = good
102 *
103 */
104
105static int az_getsigstr(struct az_device *dev)
106{
107 if (inb(io) & 2) /* bit set = no signal present */
108 return 0;
109 return 1; /* signal present */
110}
111
112static int az_getstereo(struct az_device *dev)
113{
114 if (inb(io) & 1) /* bit set = mono */
115 return 0;
116 return 1; /* stereo */
117}
118
119static int az_setfreq(struct az_device *dev, unsigned long frequency)
120{
121 int i;
122
123 frequency += 171200; /* Add 10.7 MHz IF */
124 frequency /= 800; /* Convert to 50 kHz units */
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300125
Ingo Molnar3593cab2006-02-07 06:49:14 -0200126 mutex_lock(&lock);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 send_0_byte (dev); /* 0: LSB of frequency */
129
130 for (i = 0; i < 13; i++) /* : frequency bits (1-13) */
131 if (frequency & (1 << i))
132 send_1_byte (dev);
133 else
134 send_0_byte (dev);
135
136 send_0_byte (dev); /* 14: test bit - always 0 */
137 send_0_byte (dev); /* 15: test bit - always 0 */
138 send_0_byte (dev); /* 16: band data 0 - always 0 */
139 if (dev->stereo) /* 17: stereo (1 to enable) */
140 send_1_byte (dev);
141 else
142 send_0_byte (dev);
143
144 send_1_byte (dev); /* 18: band data 1 - unknown */
145 send_0_byte (dev); /* 19: time base - always 0 */
146 send_0_byte (dev); /* 20: spacing (0 = 25 kHz) */
147 send_1_byte (dev); /* 21: spacing (1 = 25 kHz) */
148 send_0_byte (dev); /* 22: spacing (0 = 25 kHz) */
149 send_1_byte (dev); /* 23: AM/FM (FM = 1, always) */
150
151 /* latch frequency */
152
153 udelay (radio_wait_time);
154 outb_p(128+64+volconvert(dev->curvol), io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300155
Ingo Molnar3593cab2006-02-07 06:49:14 -0200156 mutex_unlock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 return 0;
159}
160
161static int az_do_ioctl(struct inode *inode, struct file *file,
162 unsigned int cmd, void *arg)
163{
164 struct video_device *dev = video_devdata(file);
165 struct az_device *az = dev->priv;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 switch(cmd)
168 {
169 case VIDIOCGCAP:
170 {
171 struct video_capability *v = arg;
172 memset(v,0,sizeof(*v));
173 v->type=VID_TYPE_TUNER;
174 v->channels=1;
175 v->audios=1;
176 strcpy(v->name, "Aztech Radio");
177 return 0;
178 }
179 case VIDIOCGTUNER:
180 {
181 struct video_tuner *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300182 if(v->tuner) /* Only 1 tuner */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -EINVAL;
184 v->rangelow=(87*16000);
185 v->rangehigh=(108*16000);
186 v->flags=VIDEO_TUNER_LOW;
187 v->mode=VIDEO_MODE_AUTO;
188 v->signal=0xFFFF*az_getsigstr(az);
189 if(az_getstereo(az))
190 v->flags|=VIDEO_TUNER_STEREO_ON;
191 strcpy(v->name, "FM");
192 return 0;
193 }
194 case VIDIOCSTUNER:
195 {
196 struct video_tuner *v = arg;
197 if(v->tuner!=0)
198 return -EINVAL;
199 return 0;
200 }
201 case VIDIOCGFREQ:
202 {
203 unsigned long *freq = arg;
204 *freq = az->curfreq;
205 return 0;
206 }
207 case VIDIOCSFREQ:
208 {
209 unsigned long *freq = arg;
210 az->curfreq = *freq;
211 az_setfreq(az, az->curfreq);
212 return 0;
213 }
214 case VIDIOCGAUDIO:
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300215 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct video_audio *v = arg;
217 memset(v,0, sizeof(*v));
218 v->flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
219 if(az->stereo)
220 v->mode=VIDEO_SOUND_STEREO;
221 else
222 v->mode=VIDEO_SOUND_MONO;
223 v->volume=az->curvol;
224 v->step=16384;
225 strcpy(v->name, "Radio");
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 case VIDIOCSAUDIO:
229 {
230 struct video_audio *v = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300231 if(v->audio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -EINVAL;
233 az->curvol=v->volume;
234
235 az->stereo=(v->mode&VIDEO_SOUND_STEREO)?1:0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300236 if(v->flags&VIDEO_AUDIO_MUTE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 az_setvol(az,0);
238 else
239 az_setvol(az,az->curvol);
240 return 0;
241 }
242 default:
243 return -ENOIOCTLCMD;
244 }
245}
246
247static int az_ioctl(struct inode *inode, struct file *file,
248 unsigned int cmd, unsigned long arg)
249{
250 return video_usercopy(inode, file, cmd, arg, az_do_ioctl);
251}
252
253static struct az_device aztech_unit;
254
255static struct file_operations aztech_fops = {
256 .owner = THIS_MODULE,
257 .open = video_exclusive_open,
258 .release = video_exclusive_release,
259 .ioctl = az_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200260 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .llseek = no_llseek,
262};
263
264static struct video_device aztech_radio=
265{
266 .owner = THIS_MODULE,
267 .name = "Aztech radio",
268 .type = VID_TYPE_TUNER,
269 .hardware = VID_HARDWARE_AZTECH,
270 .fops = &aztech_fops,
271};
272
273static int __init aztech_init(void)
274{
275 if(io==-1)
276 {
277 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
278 return -EINVAL;
279 }
280
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300281 if (!request_region(io, 2, "aztech"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 {
283 printk(KERN_ERR "aztech: port 0x%x already in use\n", io);
284 return -EBUSY;
285 }
286
Ingo Molnar3593cab2006-02-07 06:49:14 -0200287 mutex_init(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 aztech_radio.priv=&aztech_unit;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if(video_register_device(&aztech_radio, VFL_TYPE_RADIO, radio_nr)==-1)
291 {
292 release_region(io,2);
293 return -EINVAL;
294 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 printk(KERN_INFO "Aztech radio card driver v1.00/19990224 rkroll@exploits.org\n");
297 /* mute card - prevents noisy bootups */
298 outb (0, io);
299 return 0;
300}
301
302MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
303MODULE_DESCRIPTION("A driver for the Aztech radio card.");
304MODULE_LICENSE("GPL");
305
306module_param(io, int, 0);
307module_param(radio_nr, int, 0);
308MODULE_PARM_DESC(io, "I/O address of the Aztech card (0x350 or 0x358)");
309
310static void __exit aztech_cleanup(void)
311{
312 video_unregister_device(&aztech_radio);
313 release_region(io,2);
314}
315
316module_init(aztech_init);
317module_exit(aztech_cleanup);