blob: 9cb193fa6e335c367920e2079b67716e17378b6c [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 */
Hans Verkuil922c78e2009-03-06 13:52:06 -030020#include <media/v4l2-device.h>
21#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -030022
Hans Verkuil922c78e2009-03-06 13:52:06 -030023MODULE_AUTHOR("Ben Pfaff");
24MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
25MODULE_LICENSE("GPL");
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
38#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
39
40struct rtrack2
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Hans Verkuil922c78e2009-03-06 13:52:06 -030042 struct v4l2_device v4l2_dev;
43 struct video_device vdev;
44 int io;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned long curfreq;
46 int muted;
Hans Verkuil922c78e2009-03-06 13:52:06 -030047 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Hans Verkuil922c78e2009-03-06 13:52:06 -030050static struct rtrack2 rtrack2_card;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* local things */
54
Hans Verkuil922c78e2009-03-06 13:52:06 -030055static void rt_mute(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Hans Verkuil922c78e2009-03-06 13:52:06 -030057 if (dev->muted)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return;
Hans Verkuil922c78e2009-03-06 13:52:06 -030059 mutex_lock(&dev->lock);
60 outb(1, dev->io);
61 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 dev->muted = 1;
63}
64
Hans Verkuil922c78e2009-03-06 13:52:06 -030065static void rt_unmute(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 if(dev->muted == 0)
68 return;
Hans Verkuil922c78e2009-03-06 13:52:06 -030069 mutex_lock(&dev->lock);
70 outb(0, dev->io);
71 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 dev->muted = 0;
73}
74
Hans Verkuil922c78e2009-03-06 13:52:06 -030075static void zero(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Hans Verkuil922c78e2009-03-06 13:52:06 -030077 outb_p(1, dev->io);
78 outb_p(3, dev->io);
79 outb_p(1, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Hans Verkuil922c78e2009-03-06 13:52:06 -030082static void one(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Hans Verkuil922c78e2009-03-06 13:52:06 -030084 outb_p(5, dev->io);
85 outb_p(7, dev->io);
86 outb_p(5, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Hans Verkuil922c78e2009-03-06 13:52:06 -030089static int rt_setfreq(struct rtrack2 *dev, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 int i;
92
Hans Verkuil922c78e2009-03-06 13:52:06 -030093 mutex_lock(&dev->lock);
94 dev->curfreq = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 freq = freq / 200 + 856;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030096
Hans Verkuil922c78e2009-03-06 13:52:06 -030097 outb_p(0xc8, dev->io);
98 outb_p(0xc9, dev->io);
99 outb_p(0xc9, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 for (i = 0; i < 10; i++)
Hans Verkuil922c78e2009-03-06 13:52:06 -0300102 zero(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 for (i = 14; i >= 0; i--)
105 if (freq & (1 << i))
Hans Verkuil922c78e2009-03-06 13:52:06 -0300106 one(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 else
Hans Verkuil922c78e2009-03-06 13:52:06 -0300108 zero(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Hans Verkuil922c78e2009-03-06 13:52:06 -0300110 outb_p(0xc8, dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (!dev->muted)
Hans Verkuil922c78e2009-03-06 13:52:06 -0300112 outb_p(0, dev->io);
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300113
Hans Verkuil922c78e2009-03-06 13:52:06 -0300114 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116}
117
Douglas Landgraf25f30382007-04-19 16:42:25 -0300118static int vidioc_querycap(struct file *file, void *priv,
119 struct v4l2_capability *v)
120{
121 strlcpy(v->driver, "radio-rtrack2", sizeof(v->driver));
122 strlcpy(v->card, "RadioTrack II", sizeof(v->card));
Hans Verkuil922c78e2009-03-06 13:52:06 -0300123 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
Douglas Landgraf25f30382007-04-19 16:42:25 -0300124 v->version = RADIO_VERSION;
Hans Verkuil922c78e2009-03-06 13:52:06 -0300125 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300126 return 0;
127}
128
129static int vidioc_s_tuner(struct file *file, void *priv,
130 struct v4l2_tuner *v)
131{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300132 return v->index ? -EINVAL : 0;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300133}
134
Hans Verkuil922c78e2009-03-06 13:52:06 -0300135static int rt_getsigstr(struct rtrack2 *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300137 int sig = 1;
138
139 mutex_lock(&dev->lock);
140 if (inb(dev->io) & 2) /* bit set = no signal present */
141 sig = 0;
142 mutex_unlock(&dev->lock);
143 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Douglas Landgraf25f30382007-04-19 16:42:25 -0300146static int vidioc_g_tuner(struct file *file, void *priv,
147 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300149 struct rtrack2 *rt = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Douglas Landgraf25f30382007-04-19 16:42:25 -0300151 if (v->index > 0)
152 return -EINVAL;
Mauro Carvalho Chehabf8c559f2006-08-08 09:10:02 -0300153
Hans Verkuil922c78e2009-03-06 13:52:06 -0300154 strlcpy(v->name, "FM", sizeof(v->name));
Douglas Landgraf25f30382007-04-19 16:42:25 -0300155 v->type = V4L2_TUNER_RADIO;
Hans Verkuil922c78e2009-03-06 13:52:06 -0300156 v->rangelow = 88 * 16000;
157 v->rangehigh = 108 * 16000;
Douglas Landgraf25f30382007-04-19 16:42:25 -0300158 v->rxsubchans = V4L2_TUNER_SUB_MONO;
159 v->capability = V4L2_TUNER_CAP_LOW;
160 v->audmode = V4L2_TUNER_MODE_MONO;
Hans Verkuil922c78e2009-03-06 13:52:06 -0300161 v->signal = 0xFFFF * rt_getsigstr(rt);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Douglas Landgraf25f30382007-04-19 16:42:25 -0300165static int vidioc_s_frequency(struct file *file, void *priv,
166 struct v4l2_frequency *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300168 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300169
Hans Verkuil922c78e2009-03-06 13:52:06 -0300170 rt_setfreq(rt, f->frequency);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300171 return 0;
172}
173
174static int vidioc_g_frequency(struct file *file, void *priv,
175 struct v4l2_frequency *f)
176{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300177 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300178
179 f->type = V4L2_TUNER_RADIO;
180 f->frequency = rt->curfreq;
181 return 0;
182}
183
184static int vidioc_queryctrl(struct file *file, void *priv,
185 struct v4l2_queryctrl *qc)
186{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300187 switch (qc->id) {
188 case V4L2_CID_AUDIO_MUTE:
189 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
190 case V4L2_CID_AUDIO_VOLUME:
191 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535, 65535);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300192 }
193 return -EINVAL;
194}
195
196static int vidioc_g_ctrl(struct file *file, void *priv,
197 struct v4l2_control *ctrl)
198{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300199 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300200
201 switch (ctrl->id) {
202 case V4L2_CID_AUDIO_MUTE:
203 ctrl->value = rt->muted;
204 return 0;
205 case V4L2_CID_AUDIO_VOLUME:
206 if (rt->muted)
207 ctrl->value = 0;
208 else
209 ctrl->value = 65535;
210 return 0;
211 }
212 return -EINVAL;
213}
214
215static int vidioc_s_ctrl(struct file *file, void *priv,
216 struct v4l2_control *ctrl)
217{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300218 struct rtrack2 *rt = video_drvdata(file);
Douglas Landgraf25f30382007-04-19 16:42:25 -0300219
220 switch (ctrl->id) {
221 case V4L2_CID_AUDIO_MUTE:
222 if (ctrl->value)
223 rt_mute(rt);
224 else
225 rt_unmute(rt);
226 return 0;
227 case V4L2_CID_AUDIO_VOLUME:
228 if (ctrl->value)
229 rt_unmute(rt);
230 else
231 rt_mute(rt);
232 return 0;
233 }
234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300237static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
238{
239 *i = 0;
240 return 0;
241}
242
243static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
244{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300245 return i ? -EINVAL : 0;
246}
247
248static int vidioc_g_audio(struct file *file, void *priv,
249 struct v4l2_audio *a)
250{
251 a->index = 0;
252 strlcpy(a->name, "Radio", sizeof(a->name));
253 a->capability = V4L2_AUDCAP_STEREO;
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300254 return 0;
255}
256
257static int vidioc_s_audio(struct file *file, void *priv,
258 struct v4l2_audio *a)
259{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300260 return a->index ? -EINVAL : 0;
261}
262
Hans Verkuilbec43662008-12-30 06:58:20 -0300263static const struct v4l2_file_operations rtrack2_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .owner = THIS_MODULE,
Douglas Landgraf25f30382007-04-19 16:42:25 -0300265 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
Hans Verkuila3998102008-07-21 02:57:38 -0300268static const struct v4l2_ioctl_ops rtrack2_ioctl_ops = {
Douglas Landgraf25f30382007-04-19 16:42:25 -0300269 .vidioc_querycap = vidioc_querycap,
270 .vidioc_g_tuner = vidioc_g_tuner,
271 .vidioc_s_tuner = vidioc_s_tuner,
272 .vidioc_g_frequency = vidioc_g_frequency,
273 .vidioc_s_frequency = vidioc_s_frequency,
274 .vidioc_queryctrl = vidioc_queryctrl,
275 .vidioc_g_ctrl = vidioc_g_ctrl,
276 .vidioc_s_ctrl = vidioc_s_ctrl,
Douglas Landgraf8b811cf2007-04-20 06:37:36 -0300277 .vidioc_g_audio = vidioc_g_audio,
278 .vidioc_s_audio = vidioc_s_audio,
279 .vidioc_g_input = vidioc_g_input,
280 .vidioc_s_input = vidioc_s_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
283static int __init rtrack2_init(void)
284{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300285 struct rtrack2 *dev = &rtrack2_card;
286 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
287 int res;
288
289 strlcpy(v4l2_dev->name, "rtrack2", sizeof(v4l2_dev->name));
290 dev->io = io;
291 if (dev->io == -1) {
292 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 -0700293 return -EINVAL;
294 }
Hans Verkuil922c78e2009-03-06 13:52:06 -0300295 if (!request_region(dev->io, 4, "rtrack2")) {
296 v4l2_err(v4l2_dev, "port 0x%x already in use\n", dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return -EBUSY;
298 }
299
Hans Verkuil922c78e2009-03-06 13:52:06 -0300300 res = v4l2_device_register(NULL, v4l2_dev);
301 if (res < 0) {
302 release_region(dev->io, 4);
303 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
304 return res;
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Hans Verkuil922c78e2009-03-06 13:52:06 -0300307 strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
308 dev->vdev.v4l2_dev = v4l2_dev;
309 dev->vdev.fops = &rtrack2_fops;
310 dev->vdev.ioctl_ops = &rtrack2_ioctl_ops;
311 dev->vdev.release = video_device_release_empty;
312 video_set_drvdata(&dev->vdev, dev);
313
314 mutex_init(&dev->lock);
315 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
316 v4l2_device_unregister(v4l2_dev);
317 release_region(dev->io, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EINVAL;
319 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300320
Hans Verkuil922c78e2009-03-06 13:52:06 -0300321 v4l2_info(v4l2_dev, "AIMSlab Radiotrack II card driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300323 /* mute card - prevents noisy bootups */
Hans Verkuil922c78e2009-03-06 13:52:06 -0300324 outb(1, dev->io);
325 dev->muted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return 0;
328}
329
Hans Verkuil922c78e2009-03-06 13:52:06 -0300330static void __exit rtrack2_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Hans Verkuil922c78e2009-03-06 13:52:06 -0300332 struct rtrack2 *dev = &rtrack2_card;
333
334 video_unregister_device(&dev->vdev);
335 v4l2_device_unregister(&dev->v4l2_dev);
336 release_region(dev->io, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339module_init(rtrack2_init);
Hans Verkuil922c78e2009-03-06 13:52:06 -0300340module_exit(rtrack2_exit);