blob: 4239a7f3f7e54844ebc049abdec20671da61af74 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/spinlock.h>
21
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030022#include <linux/version.h> /* for KERNEL_VERSION MACRO */
23#define RADIO_VERSION KERNEL_VERSION(0,0,2)
24
25static struct v4l2_queryctrl radio_qctrl[] = {
26 {
27 .id = V4L2_CID_AUDIO_MUTE,
28 .name = "Mute",
29 .minimum = 0,
30 .maximum = 1,
31 .default_value = 1,
32 .type = V4L2_CTRL_TYPE_BOOLEAN,
33 },{
34 .id = V4L2_CID_AUDIO_VOLUME,
35 .name = "Volume",
36 .minimum = 0,
37 .maximum = 65535,
38 .step = 65535,
39 .default_value = 0xff,
40 .type = V4L2_CTRL_TYPE_INTEGER,
41 }
42};
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#ifndef CONFIG_RADIO_RTRACK2_PORT
45#define CONFIG_RADIO_RTRACK2_PORT -1
46#endif
47
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030048static int io = CONFIG_RADIO_RTRACK2_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int radio_nr = -1;
50static spinlock_t lock;
51
52struct rt_device
53{
54 int port;
55 unsigned long curfreq;
56 int muted;
57};
58
59
60/* local things */
61
62static void rt_mute(struct rt_device *dev)
63{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030064 if(dev->muted)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return;
66 spin_lock(&lock);
67 outb(1, io);
68 spin_unlock(&lock);
69 dev->muted = 1;
70}
71
72static void rt_unmute(struct rt_device *dev)
73{
74 if(dev->muted == 0)
75 return;
76 spin_lock(&lock);
77 outb(0, io);
78 spin_unlock(&lock);
79 dev->muted = 0;
80}
81
82static void zero(void)
83{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030084 outb_p(1, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 outb_p(3, io);
86 outb_p(1, io);
87}
88
89static void one(void)
90{
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030091 outb_p(5, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 outb_p(7, io);
93 outb_p(5, io);
94}
95
96static int rt_setfreq(struct rt_device *dev, unsigned long freq)
97{
98 int i;
99
100 freq = freq / 200 + 856;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 spin_lock(&lock);
103
104 outb_p(0xc8, io);
105 outb_p(0xc9, io);
106 outb_p(0xc9, io);
107
108 for (i = 0; i < 10; i++)
109 zero ();
110
111 for (i = 14; i >= 0; i--)
112 if (freq & (1 << i))
113 one ();
114 else
115 zero ();
116
117 outb_p(0xc8, io);
118 if (!dev->muted)
119 outb_p(0, io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 spin_unlock(&lock);
122 return 0;
123}
124
Douglas Landgraf25f30382007-04-19 16:42:25 -0300125static int vidioc_querycap(struct file *file, void *priv,
126 struct v4l2_capability *v)
127{
128 strlcpy(v->driver, "radio-rtrack2", sizeof(v->driver));
129 strlcpy(v->card, "RadioTrack II", sizeof(v->card));
130 sprintf(v->bus_info, "ISA");
131 v->version = RADIO_VERSION;
132 v->capabilities = V4L2_CAP_TUNER;
133 return 0;
134}
135
136static int vidioc_s_tuner(struct file *file, void *priv,
137 struct v4l2_tuner *v)
138{
139 if (v->index > 0)
140 return -EINVAL;
141
142 return 0;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static int rt_getsigstr(struct rt_device *dev)
146{
147 if (inb(io) & 2) /* bit set = no signal present */
148 return 0;
149 return 1; /* signal present */
150}
151
Douglas Landgraf25f30382007-04-19 16:42:25 -0300152static int vidioc_g_tuner(struct file *file, void *priv,
153 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct video_device *dev = video_devdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300156 struct rt_device *rt = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Douglas Landgraf25f30382007-04-19 16:42:25 -0300158 if (v->index > 0)
159 return -EINVAL;
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -0300160
Douglas Landgraf25f30382007-04-19 16:42:25 -0300161 strcpy(v->name, "FM");
162 v->type = V4L2_TUNER_RADIO;
163 v->rangelow = (88*16000);
164 v->rangehigh = (108*16000);
165 v->rxsubchans = V4L2_TUNER_SUB_MONO;
166 v->capability = V4L2_TUNER_CAP_LOW;
167 v->audmode = V4L2_TUNER_MODE_MONO;
168 v->signal = 0xFFFF*rt_getsigstr(rt);
169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Douglas Landgraf25f30382007-04-19 16:42:25 -0300172static int vidioc_s_frequency(struct file *file, void *priv,
173 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Douglas Landgraf25f30382007-04-19 16:42:25 -0300175 struct video_device *dev = video_devdata(file);
176 struct rt_device *rt = dev->priv;
177
178 rt->curfreq = f->frequency;
179 rt_setfreq(rt, rt->curfreq);
180 return 0;
181}
182
183static int vidioc_g_frequency(struct file *file, void *priv,
184 struct v4l2_frequency *f)
185{
186 struct video_device *dev = video_devdata(file);
187 struct rt_device *rt = dev->priv;
188
189 f->type = V4L2_TUNER_RADIO;
190 f->frequency = rt->curfreq;
191 return 0;
192}
193
194static int vidioc_queryctrl(struct file *file, void *priv,
195 struct v4l2_queryctrl *qc)
196{
197 int i;
198
199 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
200 if (qc->id && qc->id == radio_qctrl[i].id) {
201 memcpy(qc, &(radio_qctrl[i]),
202 sizeof(*qc));
203 return 0;
204 }
205 }
206 return -EINVAL;
207}
208
209static int vidioc_g_ctrl(struct file *file, void *priv,
210 struct v4l2_control *ctrl)
211{
212 struct video_device *dev = video_devdata(file);
213 struct rt_device *rt = dev->priv;
214
215 switch (ctrl->id) {
216 case V4L2_CID_AUDIO_MUTE:
217 ctrl->value = rt->muted;
218 return 0;
219 case V4L2_CID_AUDIO_VOLUME:
220 if (rt->muted)
221 ctrl->value = 0;
222 else
223 ctrl->value = 65535;
224 return 0;
225 }
226 return -EINVAL;
227}
228
229static int vidioc_s_ctrl(struct file *file, void *priv,
230 struct v4l2_control *ctrl)
231{
232 struct video_device *dev = video_devdata(file);
233 struct rt_device *rt = dev->priv;
234
235 switch (ctrl->id) {
236 case V4L2_CID_AUDIO_MUTE:
237 if (ctrl->value)
238 rt_mute(rt);
239 else
240 rt_unmute(rt);
241 return 0;
242 case V4L2_CID_AUDIO_VOLUME:
243 if (ctrl->value)
244 rt_unmute(rt);
245 else
246 rt_mute(rt);
247 return 0;
248 }
249 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252static struct rt_device rtrack2_unit;
253
Arjan van de Venfa027c22007-02-12 00:55:33 -0800254static const struct file_operations rtrack2_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .owner = THIS_MODULE,
256 .open = video_exclusive_open,
257 .release = video_exclusive_release,
Douglas Landgraf25f30382007-04-19 16:42:25 -0300258 .ioctl = video_ioctl2,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200259 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .llseek = no_llseek,
261};
262
263static struct video_device rtrack2_radio=
264{
265 .owner = THIS_MODULE,
266 .name = "RadioTrack II radio",
267 .type = VID_TYPE_TUNER,
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -0300268 .hardware = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 .fops = &rtrack2_fops,
Douglas Landgraf25f30382007-04-19 16:42:25 -0300270 .vidioc_querycap = vidioc_querycap,
271 .vidioc_g_tuner = vidioc_g_tuner,
272 .vidioc_s_tuner = vidioc_s_tuner,
273 .vidioc_g_frequency = vidioc_g_frequency,
274 .vidioc_s_frequency = vidioc_s_frequency,
275 .vidioc_queryctrl = vidioc_queryctrl,
276 .vidioc_g_ctrl = vidioc_g_ctrl,
277 .vidioc_s_ctrl = vidioc_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278};
279
280static int __init rtrack2_init(void)
281{
282 if(io==-1)
283 {
284 printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n");
285 return -EINVAL;
286 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300287 if (!request_region(io, 4, "rtrack2"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 {
289 printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io);
290 return -EBUSY;
291 }
292
293 rtrack2_radio.priv=&rtrack2_unit;
294
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300295 spin_lock_init(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if(video_register_device(&rtrack2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
297 {
298 release_region(io, 4);
299 return -EINVAL;
300 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n");
303
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300304 /* mute card - prevents noisy bootups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 outb(1, io);
306 rtrack2_unit.muted = 1;
307
308 return 0;
309}
310
311MODULE_AUTHOR("Ben Pfaff");
312MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
313MODULE_LICENSE("GPL");
314
315module_param(io, int, 0);
316MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
317module_param(radio_nr, int, 0);
318
319static void __exit rtrack2_cleanup_module(void)
320{
321 video_unregister_device(&rtrack2_radio);
322 release_region(io,4);
323}
324
325module_init(rtrack2_init);
326module_exit(rtrack2_cleanup_module);
327
328/*
329 Local variables:
330 compile-command: "mmake"
331 End:
332*/