blob: 5b93b47be71410a667dd1580a069224f27c7ff4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
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 * TODO: Allow for more than one of these foolish entities :-)
8 *
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -03009 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/module.h> /* Modules */
13#include <linux/init.h> /* Initdata */
Peter Osterlundfb911ee2005-09-13 01:25:15 -070014#include <linux/ioport.h> /* request_region */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/delay.h> /* udelay */
16#include <asm/io.h> /* outb, outb_p */
17#include <asm/uaccess.h> /* copy to/from user */
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030018#include <linux/videodev2.h> /* kernel radio structs */
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030019#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030020#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/spinlock.h>
22
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030023#include <linux/version.h> /* for KERNEL_VERSION MACRO */
24#define RADIO_VERSION KERNEL_VERSION(0,0,2)
25
26static struct v4l2_queryctrl radio_qctrl[] = {
27 {
28 .id = V4L2_CID_AUDIO_MUTE,
29 .name = "Mute",
30 .minimum = 0,
31 .maximum = 1,
32 .default_value = 1,
33 .type = V4L2_CTRL_TYPE_BOOLEAN,
34 },{
35 .id = V4L2_CID_AUDIO_VOLUME,
36 .name = "Volume",
37 .minimum = 0,
38 .maximum = 65535,
39 .step = 65535,
40 .default_value = 0xff,
41 .type = V4L2_CTRL_TYPE_INTEGER,
42 }
43};
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#ifndef CONFIG_RADIO_RTRACK2_PORT
46#define CONFIG_RADIO_RTRACK2_PORT -1
47#endif
48
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030049static int io = CONFIG_RADIO_RTRACK2_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static int radio_nr = -1;
51static spinlock_t lock;
52
53struct rt_device
54{
Hans Verkuil3ca685a2008-08-23 04:49:13 -030055 unsigned long in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int port;
57 unsigned long curfreq;
58 int muted;
59};
60
61
62/* local things */
63
64static void rt_mute(struct rt_device *dev)
65{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030066 if(dev->muted)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return;
68 spin_lock(&lock);
69 outb(1, io);
70 spin_unlock(&lock);
71 dev->muted = 1;
72}
73
74static void rt_unmute(struct rt_device *dev)
75{
76 if(dev->muted == 0)
77 return;
78 spin_lock(&lock);
79 outb(0, io);
80 spin_unlock(&lock);
81 dev->muted = 0;
82}
83
84static void zero(void)
85{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030086 outb_p(1, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 outb_p(3, io);
88 outb_p(1, io);
89}
90
91static void one(void)
92{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030093 outb_p(5, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 outb_p(7, io);
95 outb_p(5, io);
96}
97
98static int rt_setfreq(struct rt_device *dev, unsigned long freq)
99{
100 int i;
101
102 freq = freq / 200 + 856;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 spin_lock(&lock);
105
106 outb_p(0xc8, io);
107 outb_p(0xc9, io);
108 outb_p(0xc9, io);
109
110 for (i = 0; i < 10; i++)
111 zero ();
112
113 for (i = 14; i >= 0; i--)
114 if (freq & (1 << i))
115 one ();
116 else
117 zero ();
118
119 outb_p(0xc8, io);
120 if (!dev->muted)
121 outb_p(0, io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 spin_unlock(&lock);
124 return 0;
125}
126
Douglas Landgraf25f30382007-04-19 16:42:25 -0300127static int vidioc_querycap(struct file *file, void *priv,
128 struct v4l2_capability *v)
129{
130 strlcpy(v->driver, "radio-rtrack2", sizeof(v->driver));
131 strlcpy(v->card, "RadioTrack II", sizeof(v->card));
132 sprintf(v->bus_info, "ISA");
133 v->version = RADIO_VERSION;
134 v->capabilities = V4L2_CAP_TUNER;
135 return 0;
136}
137
138static int vidioc_s_tuner(struct file *file, void *priv,
139 struct v4l2_tuner *v)
140{
141 if (v->index > 0)
142 return -EINVAL;
143
144 return 0;
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int rt_getsigstr(struct rt_device *dev)
148{
149 if (inb(io) & 2) /* bit set = no signal present */
150 return 0;
151 return 1; /* signal present */
152}
153
Douglas Landgraf25f30382007-04-19 16:42:25 -0300154static int vidioc_g_tuner(struct file *file, void *priv,
155 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct video_device *dev = video_devdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300158 struct rt_device *rt = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Douglas Landgraf25f30382007-04-19 16:42:25 -0300160 if (v->index > 0)
161 return -EINVAL;
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -0300162
Douglas Landgraf25f30382007-04-19 16:42:25 -0300163 strcpy(v->name, "FM");
164 v->type = V4L2_TUNER_RADIO;
165 v->rangelow = (88*16000);
166 v->rangehigh = (108*16000);
167 v->rxsubchans = V4L2_TUNER_SUB_MONO;
168 v->capability = V4L2_TUNER_CAP_LOW;
169 v->audmode = V4L2_TUNER_MODE_MONO;
170 v->signal = 0xFFFF*rt_getsigstr(rt);
171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Douglas Landgraf25f30382007-04-19 16:42:25 -0300174static int vidioc_s_frequency(struct file *file, void *priv,
175 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Douglas Landgraf25f30382007-04-19 16:42:25 -0300177 struct video_device *dev = video_devdata(file);
178 struct rt_device *rt = dev->priv;
179
180 rt->curfreq = f->frequency;
181 rt_setfreq(rt, rt->curfreq);
182 return 0;
183}
184
185static int vidioc_g_frequency(struct file *file, void *priv,
186 struct v4l2_frequency *f)
187{
188 struct video_device *dev = video_devdata(file);
189 struct rt_device *rt = dev->priv;
190
191 f->type = V4L2_TUNER_RADIO;
192 f->frequency = rt->curfreq;
193 return 0;
194}
195
196static int vidioc_queryctrl(struct file *file, void *priv,
197 struct v4l2_queryctrl *qc)
198{
199 int i;
200
201 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
202 if (qc->id && qc->id == radio_qctrl[i].id) {
203 memcpy(qc, &(radio_qctrl[i]),
204 sizeof(*qc));
205 return 0;
206 }
207 }
208 return -EINVAL;
209}
210
211static int vidioc_g_ctrl(struct file *file, void *priv,
212 struct v4l2_control *ctrl)
213{
214 struct video_device *dev = video_devdata(file);
215 struct rt_device *rt = dev->priv;
216
217 switch (ctrl->id) {
218 case V4L2_CID_AUDIO_MUTE:
219 ctrl->value = rt->muted;
220 return 0;
221 case V4L2_CID_AUDIO_VOLUME:
222 if (rt->muted)
223 ctrl->value = 0;
224 else
225 ctrl->value = 65535;
226 return 0;
227 }
228 return -EINVAL;
229}
230
231static int vidioc_s_ctrl(struct file *file, void *priv,
232 struct v4l2_control *ctrl)
233{
234 struct video_device *dev = video_devdata(file);
235 struct rt_device *rt = dev->priv;
236
237 switch (ctrl->id) {
238 case V4L2_CID_AUDIO_MUTE:
239 if (ctrl->value)
240 rt_mute(rt);
241 else
242 rt_unmute(rt);
243 return 0;
244 case V4L2_CID_AUDIO_VOLUME:
245 if (ctrl->value)
246 rt_unmute(rt);
247 else
248 rt_mute(rt);
249 return 0;
250 }
251 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300254static int vidioc_g_audio(struct file *file, void *priv,
255 struct v4l2_audio *a)
256{
257 if (a->index > 1)
258 return -EINVAL;
259
260 strcpy(a->name, "Radio");
261 a->capability = V4L2_AUDCAP_STEREO;
262 return 0;
263}
264
265static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
266{
267 *i = 0;
268 return 0;
269}
270
271static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
272{
273 if (i != 0)
274 return -EINVAL;
275 return 0;
276}
277
278static int vidioc_s_audio(struct file *file, void *priv,
279 struct v4l2_audio *a)
280{
281 if (a->index != 0)
282 return -EINVAL;
283 return 0;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static struct rt_device rtrack2_unit;
287
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300288static int rtrack2_exclusive_open(struct inode *inode, struct file *file)
289{
290 return test_and_set_bit(0, &rtrack2_unit.in_use) ? -EBUSY : 0;
291}
292
293static int rtrack2_exclusive_release(struct inode *inode, struct file *file)
294{
295 clear_bit(0, &rtrack2_unit.in_use);
296 return 0;
297}
298
Arjan van de Venfa027c22007-02-12 00:55:33 -0800299static const struct file_operations rtrack2_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 .owner = THIS_MODULE,
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300301 .open = rtrack2_exclusive_open,
302 .release = rtrack2_exclusive_release,
Douglas Landgraf25f30382007-04-19 16:42:25 -0300303 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300304#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200305 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300306#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 .llseek = no_llseek,
308};
309
Hans Verkuila3998102008-07-21 02:57:38 -0300310static const struct v4l2_ioctl_ops rtrack2_ioctl_ops = {
Douglas Landgraf25f30382007-04-19 16:42:25 -0300311 .vidioc_querycap = vidioc_querycap,
312 .vidioc_g_tuner = vidioc_g_tuner,
313 .vidioc_s_tuner = vidioc_s_tuner,
314 .vidioc_g_frequency = vidioc_g_frequency,
315 .vidioc_s_frequency = vidioc_s_frequency,
316 .vidioc_queryctrl = vidioc_queryctrl,
317 .vidioc_g_ctrl = vidioc_g_ctrl,
318 .vidioc_s_ctrl = vidioc_s_ctrl,
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300319 .vidioc_g_audio = vidioc_g_audio,
320 .vidioc_s_audio = vidioc_s_audio,
321 .vidioc_g_input = vidioc_g_input,
322 .vidioc_s_input = vidioc_s_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323};
324
Hans Verkuila3998102008-07-21 02:57:38 -0300325static struct video_device rtrack2_radio = {
Hans Verkuila3998102008-07-21 02:57:38 -0300326 .name = "RadioTrack II radio",
Hans Verkuila3998102008-07-21 02:57:38 -0300327 .fops = &rtrack2_fops,
328 .ioctl_ops = &rtrack2_ioctl_ops,
329};
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static int __init rtrack2_init(void)
332{
333 if(io==-1)
334 {
335 printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
336 return -EINVAL;
337 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300338 if (!request_region(io, 4, "rtrack2"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 {
340 printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
341 return -EBUSY;
342 }
343
344 rtrack2_radio.priv=&rtrack2_unit;
345
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300346 spin_lock_init(&lock);
Hans Verkuilcba99ae2008-09-03 17:11:58 -0300347 if (video_register_device(&rtrack2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 release_region(io, 4);
349 return -EINVAL;
350 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
353
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300354 /* mute card - prevents noisy bootups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 outb(1, io);
356 rtrack2_unit.muted = 1;
357
358 return 0;
359}
360
361MODULE_AUTHOR("Ben Pfaff");
362MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
363MODULE_LICENSE("GPL");
364
365module_param(io, int, 0);
366MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
367module_param(radio_nr, int, 0);
368
369static void __exit rtrack2_cleanup_module(void)
370{
371 video_unregister_device(&rtrack2_radio);
372 release_region(io,4);
373}
374
375module_init(rtrack2_init);
376module_exit(rtrack2_cleanup_module);
377
378/*
379 Local variables:
380 compile-command: "mmake"
381 End:
382*/