blob: 3628be617ee9fb99c395f511b166b61c96798db1 [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>
Hans Verkuil922c78e2009-03-06 13:52:06 -030018#include <linux/io.h> /* outb, outb_p */
Hans Verkuil922c78e2009-03-06 13:52:06 -030019#include <media/v4l2-device.h>
20#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030021
Hans Verkuil922c78e2009-03-06 13:52:06 -030022MODULE_AUTHOR("Ben Pfaff");
23MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
24MODULE_LICENSE("GPL");
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030025MODULE_VERSION("0.0.3");
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#ifndef CONFIG_RADIO_RTRACK2_PORT
28#define CONFIG_RADIO_RTRACK2_PORT -1
29#endif
30
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030031static int io = CONFIG_RADIO_RTRACK2_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int radio_nr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Hans Verkuil922c78e2009-03-06 13:52:06 -030034module_param(io, int, 0);
35MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)");
36module_param(radio_nr, int, 0);
37
Hans Verkuil922c78e2009-03-06 13:52:06 -030038struct rtrack2
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Hans Verkuil922c78e2009-03-06 13:52:06 -030040 struct v4l2_device v4l2_dev;
41 struct video_device vdev;
42 int io;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned long curfreq;
44 int muted;
Hans Verkuil922c78e2009-03-06 13:52:06 -030045 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Hans Verkuil922c78e2009-03-06 13:52:06 -030048static struct rtrack2 rtrack2_card;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* local things */
52
Hans Verkuil922c78e2009-03-06 13:52:06 -030053static void rt_mute(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Hans Verkuil922c78e2009-03-06 13:52:06 -030055 if (dev->muted)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return;
Hans Verkuil922c78e2009-03-06 13:52:06 -030057 mutex_lock(&dev->lock);
58 outb(1, dev->io);
59 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 dev->muted = 1;
61}
62
Hans Verkuil922c78e2009-03-06 13:52:06 -030063static void rt_unmute(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 if(dev->muted == 0)
66 return;
Hans Verkuil922c78e2009-03-06 13:52:06 -030067 mutex_lock(&dev->lock);
68 outb(0, dev->io);
69 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 dev->muted = 0;
71}
72
Hans Verkuil922c78e2009-03-06 13:52:06 -030073static void zero(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Hans Verkuil922c78e2009-03-06 13:52:06 -030075 outb_p(1, dev->io);
76 outb_p(3, dev->io);
77 outb_p(1, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Hans Verkuil922c78e2009-03-06 13:52:06 -030080static void one(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Hans Verkuil922c78e2009-03-06 13:52:06 -030082 outb_p(5, dev->io);
83 outb_p(7, dev->io);
84 outb_p(5, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Hans Verkuil922c78e2009-03-06 13:52:06 -030087static int rt_setfreq(struct rtrack2 *dev, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int i;
90
Hans Verkuil922c78e2009-03-06 13:52:06 -030091 mutex_lock(&dev->lock);
92 dev->curfreq = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 freq = freq / 200 + 856;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030094
Hans Verkuil922c78e2009-03-06 13:52:06 -030095 outb_p(0xc8, dev->io);
96 outb_p(0xc9, dev->io);
97 outb_p(0xc9, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 for (i = 0; i < 10; i++)
Hans Verkuil922c78e2009-03-06 13:52:06 -0300100 zero(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 for (i = 14; i >= 0; i--)
103 if (freq & (1 << i))
Hans Verkuil922c78e2009-03-06 13:52:06 -0300104 one(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 else
Hans Verkuil922c78e2009-03-06 13:52:06 -0300106 zero(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Hans Verkuil922c78e2009-03-06 13:52:06 -0300108 outb_p(0xc8, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (!dev->muted)
Hans Verkuil922c78e2009-03-06 13:52:06 -0300110 outb_p(0, dev->io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300111
Hans Verkuil922c78e2009-03-06 13:52:06 -0300112 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
114}
115
Douglas Landgraf25f30382007-04-19 16:42:25 -0300116static int vidioc_querycap(struct file *file, void *priv,
117 struct v4l2_capability *v)
118{
119 strlcpy(v->driver, "radio-rtrack2", sizeof(v->driver));
120 strlcpy(v->card, "RadioTrack II", sizeof(v->card));
Hans Verkuil922c78e2009-03-06 13:52:06 -0300121 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
Hans Verkuil922c78e2009-03-06 13:52:06 -0300122 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300123 return 0;
124}
125
126static int vidioc_s_tuner(struct file *file, void *priv,
127 struct v4l2_tuner *v)
128{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300129 return v->index ? -EINVAL : 0;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300130}
131
Hans Verkuil922c78e2009-03-06 13:52:06 -0300132static int rt_getsigstr(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300134 int sig = 1;
135
136 mutex_lock(&dev->lock);
137 if (inb(dev->io) & 2) /* bit set = no signal present */
138 sig = 0;
139 mutex_unlock(&dev->lock);
140 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Douglas Landgraf25f30382007-04-19 16:42:25 -0300143static int vidioc_g_tuner(struct file *file, void *priv,
144 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300146 struct rtrack2 *rt = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Douglas Landgraf25f30382007-04-19 16:42:25 -0300148 if (v->index > 0)
149 return -EINVAL;
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -0300150
Hans Verkuil922c78e2009-03-06 13:52:06 -0300151 strlcpy(v->name, "FM", sizeof(v->name));
Douglas Landgraf25f30382007-04-19 16:42:25 -0300152 v->type = V4L2_TUNER_RADIO;
Hans Verkuil922c78e2009-03-06 13:52:06 -0300153 v->rangelow = 88 * 16000;
154 v->rangehigh = 108 * 16000;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300155 v->rxsubchans = V4L2_TUNER_SUB_MONO;
156 v->capability = V4L2_TUNER_CAP_LOW;
157 v->audmode = V4L2_TUNER_MODE_MONO;
Hans Verkuil922c78e2009-03-06 13:52:06 -0300158 v->signal = 0xFFFF * rt_getsigstr(rt);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Douglas Landgraf25f30382007-04-19 16:42:25 -0300162static int vidioc_s_frequency(struct file *file, void *priv,
163 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300165 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300166
Hans Verkuila3a9e282009-11-27 04:33:25 -0300167 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
168 return -EINVAL;
Hans Verkuil922c78e2009-03-06 13:52:06 -0300169 rt_setfreq(rt, f->frequency);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300170 return 0;
171}
172
173static int vidioc_g_frequency(struct file *file, void *priv,
174 struct v4l2_frequency *f)
175{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300176 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300177
Hans Verkuila3a9e282009-11-27 04:33:25 -0300178 if (f->tuner != 0)
179 return -EINVAL;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300180 f->type = V4L2_TUNER_RADIO;
181 f->frequency = rt->curfreq;
182 return 0;
183}
184
185static int vidioc_queryctrl(struct file *file, void *priv,
186 struct v4l2_queryctrl *qc)
187{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300188 switch (qc->id) {
189 case V4L2_CID_AUDIO_MUTE:
190 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
191 case V4L2_CID_AUDIO_VOLUME:
192 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535, 65535);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300193 }
194 return -EINVAL;
195}
196
197static int vidioc_g_ctrl(struct file *file, void *priv,
198 struct v4l2_control *ctrl)
199{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300200 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300201
202 switch (ctrl->id) {
203 case V4L2_CID_AUDIO_MUTE:
204 ctrl->value = rt->muted;
205 return 0;
206 case V4L2_CID_AUDIO_VOLUME:
207 if (rt->muted)
208 ctrl->value = 0;
209 else
210 ctrl->value = 65535;
211 return 0;
212 }
213 return -EINVAL;
214}
215
216static int vidioc_s_ctrl(struct file *file, void *priv,
217 struct v4l2_control *ctrl)
218{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300219 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300220
221 switch (ctrl->id) {
222 case V4L2_CID_AUDIO_MUTE:
223 if (ctrl->value)
224 rt_mute(rt);
225 else
226 rt_unmute(rt);
227 return 0;
228 case V4L2_CID_AUDIO_VOLUME:
229 if (ctrl->value)
230 rt_unmute(rt);
231 else
232 rt_mute(rt);
233 return 0;
234 }
235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300238static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
239{
240 *i = 0;
241 return 0;
242}
243
244static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
245{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300246 return i ? -EINVAL : 0;
247}
248
249static int vidioc_g_audio(struct file *file, void *priv,
250 struct v4l2_audio *a)
251{
252 a->index = 0;
253 strlcpy(a->name, "Radio", sizeof(a->name));
254 a->capability = V4L2_AUDCAP_STEREO;
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300255 return 0;
256}
257
258static int vidioc_s_audio(struct file *file, void *priv,
259 struct v4l2_audio *a)
260{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300261 return a->index ? -EINVAL : 0;
262}
263
Hans Verkuilbec43662008-12-30 06:58:20 -0300264static const struct v4l2_file_operations rtrack2_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .owner = THIS_MODULE,
Hans Verkuil32958fd2010-11-14 09:36:23 -0300266 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267};
268
Hans Verkuila3998102008-07-21 02:57:38 -0300269static const struct v4l2_ioctl_ops rtrack2_ioctl_ops = {
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,
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300278 .vidioc_g_audio = vidioc_g_audio,
279 .vidioc_s_audio = vidioc_s_audio,
280 .vidioc_g_input = vidioc_g_input,
281 .vidioc_s_input = vidioc_s_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
284static int __init rtrack2_init(void)
285{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300286 struct rtrack2 *dev = &rtrack2_card;
287 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
288 int res;
289
290 strlcpy(v4l2_dev->name, "rtrack2", sizeof(v4l2_dev->name));
291 dev->io = io;
292 if (dev->io == -1) {
293 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 -0700294 return -EINVAL;
295 }
Hans Verkuil922c78e2009-03-06 13:52:06 -0300296 if (!request_region(dev->io, 4, "rtrack2")) {
297 v4l2_err(v4l2_dev, "port 0x%x already in use\n", dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return -EBUSY;
299 }
300
Hans Verkuil922c78e2009-03-06 13:52:06 -0300301 res = v4l2_device_register(NULL, v4l2_dev);
302 if (res < 0) {
303 release_region(dev->io, 4);
304 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
305 return res;
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Hans Verkuil922c78e2009-03-06 13:52:06 -0300308 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
309 dev->vdev.v4l2_dev = v4l2_dev;
310 dev->vdev.fops = &rtrack2_fops;
311 dev->vdev.ioctl_ops = &rtrack2_ioctl_ops;
312 dev->vdev.release = video_device_release_empty;
313 video_set_drvdata(&dev->vdev, dev);
314
Hans Verkuil32958fd2010-11-14 09:36:23 -0300315 /* mute card - prevents noisy bootups */
316 outb(1, dev->io);
317 dev->muted = 1;
318
Hans Verkuil922c78e2009-03-06 13:52:06 -0300319 mutex_init(&dev->lock);
320 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
321 v4l2_device_unregister(v4l2_dev);
322 release_region(dev->io, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return -EINVAL;
324 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300325
Hans Verkuil922c78e2009-03-06 13:52:06 -0300326 v4l2_info(v4l2_dev, "AIMSlab Radiotrack II card driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329}
330
Hans Verkuil922c78e2009-03-06 13:52:06 -0300331static void __exit rtrack2_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300333 struct rtrack2 *dev = &rtrack2_card;
334
335 video_unregister_device(&dev->vdev);
336 v4l2_device_unregister(&dev->v4l2_dev);
337 release_region(dev->io, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340module_init(rtrack2_init);
Hans Verkuil922c78e2009-03-06 13:52:06 -0300341module_exit(rtrack2_exit);