blob: 08bd8d9dc4f1576cd7e3afe0a9450ed6af0d7d67 [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
Alan Coxd9b01442008-10-27 15:13:47 -03004 * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 */
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030016#include <linux/videodev2.h> /* kernel radio structs */
Hans Verkuil922c78e2009-03-06 13:52:06 -030017#include <linux/mutex.h>
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030018#include <linux/version.h> /* for KERNEL_VERSION MACRO */
Hans Verkuil922c78e2009-03-06 13:52:06 -030019#include <linux/io.h> /* outb, outb_p */
20#include <linux/uaccess.h> /* copy to/from user */
21#include <media/v4l2-device.h>
22#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030023
Hans Verkuil922c78e2009-03-06 13:52:06 -030024MODULE_AUTHOR("Ben Pfaff");
25MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
26MODULE_LICENSE("GPL");
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#ifndef CONFIG_RADIO_RTRACK2_PORT
29#define CONFIG_RADIO_RTRACK2_PORT -1
30#endif
31
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030032static int io = CONFIG_RADIO_RTRACK2_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int radio_nr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Hans Verkuil922c78e2009-03-06 13:52:06 -030035module_param(io, int, 0);
36MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
37module_param(radio_nr, int, 0);
38
39#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
40
41struct rtrack2
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Hans Verkuil922c78e2009-03-06 13:52:06 -030043 struct v4l2_device v4l2_dev;
44 struct video_device vdev;
45 int io;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 unsigned long curfreq;
47 int muted;
Hans Verkuil922c78e2009-03-06 13:52:06 -030048 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Hans Verkuil922c78e2009-03-06 13:52:06 -030051static struct rtrack2 rtrack2_card;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* local things */
55
Hans Verkuil922c78e2009-03-06 13:52:06 -030056static void rt_mute(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Hans Verkuil922c78e2009-03-06 13:52:06 -030058 if (dev->muted)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return;
Hans Verkuil922c78e2009-03-06 13:52:06 -030060 mutex_lock(&dev->lock);
61 outb(1, dev->io);
62 mutex_unlock(&dev->lock);
63 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 dev->muted = 1;
65}
66
Hans Verkuil922c78e2009-03-06 13:52:06 -030067static void rt_unmute(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 if(dev->muted == 0)
70 return;
Hans Verkuil922c78e2009-03-06 13:52:06 -030071 mutex_lock(&dev->lock);
72 outb(0, dev->io);
73 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 dev->muted = 0;
75}
76
Hans Verkuil922c78e2009-03-06 13:52:06 -030077static void zero(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Hans Verkuil922c78e2009-03-06 13:52:06 -030079 outb_p(1, dev->io);
80 outb_p(3, dev->io);
81 outb_p(1, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Hans Verkuil922c78e2009-03-06 13:52:06 -030084static void one(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Hans Verkuil922c78e2009-03-06 13:52:06 -030086 outb_p(5, dev->io);
87 outb_p(7, dev->io);
88 outb_p(5, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Hans Verkuil922c78e2009-03-06 13:52:06 -030091static int rt_setfreq(struct rtrack2 *dev, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 int i;
94
Hans Verkuil922c78e2009-03-06 13:52:06 -030095 mutex_lock(&dev->lock);
96 dev->curfreq = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 freq = freq / 200 + 856;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030098
Hans Verkuil922c78e2009-03-06 13:52:06 -030099 outb_p(0xc8, dev->io);
100 outb_p(0xc9, dev->io);
101 outb_p(0xc9, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 for (i = 0; i < 10; i++)
Hans Verkuil922c78e2009-03-06 13:52:06 -0300104 zero(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 for (i = 14; i >= 0; i--)
107 if (freq & (1 << i))
Hans Verkuil922c78e2009-03-06 13:52:06 -0300108 one(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 else
Hans Verkuil922c78e2009-03-06 13:52:06 -0300110 zero(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Hans Verkuil922c78e2009-03-06 13:52:06 -0300112 outb_p(0xc8, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (!dev->muted)
Hans Verkuil922c78e2009-03-06 13:52:06 -0300114 outb_p(0, dev->io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300115
Hans Verkuil922c78e2009-03-06 13:52:06 -0300116 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118}
119
Douglas Landgraf25f30382007-04-19 16:42:25 -0300120static int vidioc_querycap(struct file *file, void *priv,
121 struct v4l2_capability *v)
122{
123 strlcpy(v->driver, "radio-rtrack2", sizeof(v->driver));
124 strlcpy(v->card, "RadioTrack II", sizeof(v->card));
Hans Verkuil922c78e2009-03-06 13:52:06 -0300125 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
Douglas Landgraf25f30382007-04-19 16:42:25 -0300126 v->version = RADIO_VERSION;
Hans Verkuil922c78e2009-03-06 13:52:06 -0300127 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300128 return 0;
129}
130
131static int vidioc_s_tuner(struct file *file, void *priv,
132 struct v4l2_tuner *v)
133{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300134 return v->index ? -EINVAL : 0;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300135}
136
Hans Verkuil922c78e2009-03-06 13:52:06 -0300137static int rt_getsigstr(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300139 int sig = 1;
140
141 mutex_lock(&dev->lock);
142 if (inb(dev->io) & 2) /* bit set = no signal present */
143 sig = 0;
144 mutex_unlock(&dev->lock);
145 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Douglas Landgraf25f30382007-04-19 16:42:25 -0300148static int vidioc_g_tuner(struct file *file, void *priv,
149 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300151 struct rtrack2 *rt = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Douglas Landgraf25f30382007-04-19 16:42:25 -0300153 if (v->index > 0)
154 return -EINVAL;
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -0300155
Hans Verkuil922c78e2009-03-06 13:52:06 -0300156 strlcpy(v->name, "FM", sizeof(v->name));
Douglas Landgraf25f30382007-04-19 16:42:25 -0300157 v->type = V4L2_TUNER_RADIO;
Hans Verkuil922c78e2009-03-06 13:52:06 -0300158 v->rangelow = 88 * 16000;
159 v->rangehigh = 108 * 16000;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300160 v->rxsubchans = V4L2_TUNER_SUB_MONO;
161 v->capability = V4L2_TUNER_CAP_LOW;
162 v->audmode = V4L2_TUNER_MODE_MONO;
Hans Verkuil922c78e2009-03-06 13:52:06 -0300163 v->signal = 0xFFFF * rt_getsigstr(rt);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Douglas Landgraf25f30382007-04-19 16:42:25 -0300167static int vidioc_s_frequency(struct file *file, void *priv,
168 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300170 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300171
Hans Verkuil922c78e2009-03-06 13:52:06 -0300172 rt_setfreq(rt, f->frequency);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300173 return 0;
174}
175
176static int vidioc_g_frequency(struct file *file, void *priv,
177 struct v4l2_frequency *f)
178{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300179 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300180
181 f->type = V4L2_TUNER_RADIO;
182 f->frequency = rt->curfreq;
183 return 0;
184}
185
186static int vidioc_queryctrl(struct file *file, void *priv,
187 struct v4l2_queryctrl *qc)
188{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300189 switch (qc->id) {
190 case V4L2_CID_AUDIO_MUTE:
191 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
192 case V4L2_CID_AUDIO_VOLUME:
193 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535, 65535);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300194 }
195 return -EINVAL;
196}
197
198static int vidioc_g_ctrl(struct file *file, void *priv,
199 struct v4l2_control *ctrl)
200{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300201 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300202
203 switch (ctrl->id) {
204 case V4L2_CID_AUDIO_MUTE:
205 ctrl->value = rt->muted;
206 return 0;
207 case V4L2_CID_AUDIO_VOLUME:
208 if (rt->muted)
209 ctrl->value = 0;
210 else
211 ctrl->value = 65535;
212 return 0;
213 }
214 return -EINVAL;
215}
216
217static int vidioc_s_ctrl(struct file *file, void *priv,
218 struct v4l2_control *ctrl)
219{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300220 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300221
222 switch (ctrl->id) {
223 case V4L2_CID_AUDIO_MUTE:
224 if (ctrl->value)
225 rt_mute(rt);
226 else
227 rt_unmute(rt);
228 return 0;
229 case V4L2_CID_AUDIO_VOLUME:
230 if (ctrl->value)
231 rt_unmute(rt);
232 else
233 rt_mute(rt);
234 return 0;
235 }
236 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300239static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
240{
241 *i = 0;
242 return 0;
243}
244
245static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
246{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300247 return i ? -EINVAL : 0;
248}
249
250static int vidioc_g_audio(struct file *file, void *priv,
251 struct v4l2_audio *a)
252{
253 a->index = 0;
254 strlcpy(a->name, "Radio", sizeof(a->name));
255 a->capability = V4L2_AUDCAP_STEREO;
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300256 return 0;
257}
258
259static int vidioc_s_audio(struct file *file, void *priv,
260 struct v4l2_audio *a)
261{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300262 return a->index ? -EINVAL : 0;
263}
264
265static int rtrack2_open(struct file *file)
266{
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300267 return 0;
268}
269
Hans Verkuil922c78e2009-03-06 13:52:06 -0300270static int rtrack2_release(struct file *file)
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300271{
Hans Verkuil3ca685a2008-08-23 04:49:13 -0300272 return 0;
273}
274
Hans Verkuilbec43662008-12-30 06:58:20 -0300275static const struct v4l2_file_operations rtrack2_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .owner = THIS_MODULE,
Hans Verkuil922c78e2009-03-06 13:52:06 -0300277 .open = rtrack2_open,
278 .release = rtrack2_release,
Douglas Landgraf25f30382007-04-19 16:42:25 -0300279 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280};
281
Hans Verkuila3998102008-07-21 02:57:38 -0300282static const struct v4l2_ioctl_ops rtrack2_ioctl_ops = {
Douglas Landgraf25f30382007-04-19 16:42:25 -0300283 .vidioc_querycap = vidioc_querycap,
284 .vidioc_g_tuner = vidioc_g_tuner,
285 .vidioc_s_tuner = vidioc_s_tuner,
286 .vidioc_g_frequency = vidioc_g_frequency,
287 .vidioc_s_frequency = vidioc_s_frequency,
288 .vidioc_queryctrl = vidioc_queryctrl,
289 .vidioc_g_ctrl = vidioc_g_ctrl,
290 .vidioc_s_ctrl = vidioc_s_ctrl,
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300291 .vidioc_g_audio = vidioc_g_audio,
292 .vidioc_s_audio = vidioc_s_audio,
293 .vidioc_g_input = vidioc_g_input,
294 .vidioc_s_input = vidioc_s_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295};
296
297static int __init rtrack2_init(void)
298{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300299 struct rtrack2 *dev = &rtrack2_card;
300 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
301 int res;
302
303 strlcpy(v4l2_dev->name, "rtrack2", sizeof(v4l2_dev->name));
304 dev->io = io;
305 if (dev->io == -1) {
306 v4l2_err(v4l2_dev, "You must set an I/O address with io=0x20c or io=0x30c\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -EINVAL;
308 }
Hans Verkuil922c78e2009-03-06 13:52:06 -0300309 if (!request_region(dev->io, 4, "rtrack2")) {
310 v4l2_err(v4l2_dev, "port 0x%x already in use\n", dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return -EBUSY;
312 }
313
Hans Verkuil922c78e2009-03-06 13:52:06 -0300314 res = v4l2_device_register(NULL, v4l2_dev);
315 if (res < 0) {
316 release_region(dev->io, 4);
317 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
318 return res;
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Hans Verkuil922c78e2009-03-06 13:52:06 -0300321 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
322 dev->vdev.v4l2_dev = v4l2_dev;
323 dev->vdev.fops = &rtrack2_fops;
324 dev->vdev.ioctl_ops = &rtrack2_ioctl_ops;
325 dev->vdev.release = video_device_release_empty;
326 video_set_drvdata(&dev->vdev, dev);
327
328 mutex_init(&dev->lock);
329 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
330 v4l2_device_unregister(v4l2_dev);
331 release_region(dev->io, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -EINVAL;
333 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300334
Hans Verkuil922c78e2009-03-06 13:52:06 -0300335 v4l2_info(v4l2_dev, "AIMSlab Radiotrack II card driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300337 /* mute card - prevents noisy bootups */
Hans Verkuil922c78e2009-03-06 13:52:06 -0300338 outb(1, dev->io);
339 dev->muted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 return 0;
342}
343
Hans Verkuil922c78e2009-03-06 13:52:06 -0300344static void __exit rtrack2_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300346 struct rtrack2 *dev = &rtrack2_card;
347
348 video_unregister_device(&dev->vdev);
349 v4l2_device_unregister(&dev->v4l2_dev);
350 release_region(dev->io, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353module_init(rtrack2_init);
Hans Verkuil922c78e2009-03-06 13:52:06 -0300354module_exit(rtrack2_exit);