Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* radiotrack (radioreveal) driver for Linux radio support |
2 | * (c) 1997 M. Kirkwood | |
46ff2c72 | 3 | * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org> |
d9b01449 | 4 | * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
5 | * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org> |
6 | * | |
7 | * History: | |
8 | * 1999-02-24 Russell Kroll <rkroll@exploits.org> | |
9 | * Fine tuning/VIDEO_TUNER_LOW | |
10 | * Frequency range expanded to start at 87 MHz | |
11 | * | |
12 | * TODO: Allow for more than one of these foolish entities :-) | |
13 | * | |
14 | * Notes on the hardware (reverse engineered from other peoples' | |
15 | * reverse engineering of AIMS' code :-) | |
16 | * | |
17 | * Frequency control is done digitally -- ie out(port,encodefreq(95.8)); | |
18 | * | |
19 | * The signal strength query is unsurprisingly inaccurate. And it seems | |
20 | * to indicate that (on my card, at least) the frequency setting isn't | |
21 | * too great. (I have to tune up .025MHz from what the freq should be | |
22 | * to get a report that the thing is tuned.) | |
23 | * | |
24 | * Volume control is (ugh) analogue: | |
25 | * out(port, start_increasing_volume); | |
26 | * wait(a_wee_while); | |
27 | * out(port, stop_changing_the_volume); | |
4286c6f6 | 28 | * |
1da177e4 LT |
29 | */ |
30 | ||
31 | #include <linux/module.h> /* Modules */ | |
32 | #include <linux/init.h> /* Initdata */ | |
fb911ee8 | 33 | #include <linux/ioport.h> /* request_region */ |
2400982a | 34 | #include <linux/delay.h> /* msleep */ |
46ff2c72 | 35 | #include <linux/videodev2.h> /* kernel radio structs */ |
151c3f82 HV |
36 | #include <linux/version.h> /* for KERNEL_VERSION MACRO */ |
37 | #include <linux/io.h> /* outb, outb_p */ | |
151c3f82 | 38 | #include <media/v4l2-device.h> |
35ea11ff | 39 | #include <media/v4l2-ioctl.h> |
1da177e4 | 40 | |
151c3f82 HV |
41 | MODULE_AUTHOR("M.Kirkwood"); |
42 | MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card."); | |
43 | MODULE_LICENSE("GPL"); | |
46ff2c72 | 44 | |
1da177e4 LT |
45 | #ifndef CONFIG_RADIO_RTRACK_PORT |
46 | #define CONFIG_RADIO_RTRACK_PORT -1 | |
47 | #endif | |
48 | ||
4286c6f6 | 49 | static int io = CONFIG_RADIO_RTRACK_PORT; |
1da177e4 | 50 | static int radio_nr = -1; |
1da177e4 | 51 | |
151c3f82 HV |
52 | module_param(io, int, 0); |
53 | MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)"); | |
54 | module_param(radio_nr, int, 0); | |
55 | ||
56 | #define RADIO_VERSION KERNEL_VERSION(0, 0, 2) | |
57 | ||
58 | struct rtrack | |
1da177e4 | 59 | { |
151c3f82 HV |
60 | struct v4l2_device v4l2_dev; |
61 | struct video_device vdev; | |
1da177e4 LT |
62 | int port; |
63 | int curvol; | |
64 | unsigned long curfreq; | |
65 | int muted; | |
151c3f82 HV |
66 | int io; |
67 | struct mutex lock; | |
1da177e4 LT |
68 | }; |
69 | ||
151c3f82 | 70 | static struct rtrack rtrack_card; |
1da177e4 LT |
71 | |
72 | /* local things */ | |
73 | ||
151c3f82 | 74 | static void rt_decvol(struct rtrack *rt) |
1da177e4 | 75 | { |
151c3f82 | 76 | outb(0x58, rt->io); /* volume down + sigstr + on */ |
e3c92215 | 77 | msleep(100); |
151c3f82 | 78 | outb(0xd8, rt->io); /* volume steady + sigstr + on */ |
1da177e4 LT |
79 | } |
80 | ||
151c3f82 | 81 | static void rt_incvol(struct rtrack *rt) |
1da177e4 | 82 | { |
151c3f82 | 83 | outb(0x98, rt->io); /* volume up + sigstr + on */ |
e3c92215 | 84 | msleep(100); |
151c3f82 | 85 | outb(0xd8, rt->io); /* volume steady + sigstr + on */ |
1da177e4 LT |
86 | } |
87 | ||
151c3f82 | 88 | static void rt_mute(struct rtrack *rt) |
1da177e4 | 89 | { |
151c3f82 HV |
90 | rt->muted = 1; |
91 | mutex_lock(&rt->lock); | |
92 | outb(0xd0, rt->io); /* volume steady, off */ | |
93 | mutex_unlock(&rt->lock); | |
1da177e4 LT |
94 | } |
95 | ||
151c3f82 | 96 | static int rt_setvol(struct rtrack *rt, int vol) |
1da177e4 LT |
97 | { |
98 | int i; | |
99 | ||
151c3f82 | 100 | mutex_lock(&rt->lock); |
4286c6f6 | 101 | |
151c3f82 HV |
102 | if (vol == rt->curvol) { /* requested volume = current */ |
103 | if (rt->muted) { /* user is unmuting the card */ | |
104 | rt->muted = 0; | |
105 | outb(0xd8, rt->io); /* enable card */ | |
4286c6f6 | 106 | } |
151c3f82 | 107 | mutex_unlock(&rt->lock); |
1da177e4 LT |
108 | return 0; |
109 | } | |
110 | ||
151c3f82 HV |
111 | if (vol == 0) { /* volume = 0 means mute the card */ |
112 | outb(0x48, rt->io); /* volume down but still "on" */ | |
e3c92215 | 113 | msleep(2000); /* make sure it's totally down */ |
151c3f82 HV |
114 | outb(0xd0, rt->io); /* volume steady, off */ |
115 | rt->curvol = 0; /* track the volume state! */ | |
116 | mutex_unlock(&rt->lock); | |
1da177e4 LT |
117 | return 0; |
118 | } | |
119 | ||
151c3f82 HV |
120 | rt->muted = 0; |
121 | if (vol > rt->curvol) | |
122 | for (i = rt->curvol; i < vol; i++) | |
123 | rt_incvol(rt); | |
1da177e4 | 124 | else |
151c3f82 HV |
125 | for (i = rt->curvol; i > vol; i--) |
126 | rt_decvol(rt); | |
1da177e4 | 127 | |
151c3f82 HV |
128 | rt->curvol = vol; |
129 | mutex_unlock(&rt->lock); | |
1da177e4 LT |
130 | return 0; |
131 | } | |
132 | ||
4286c6f6 | 133 | /* the 128+64 on these outb's is to keep the volume stable while tuning |
1da177e4 LT |
134 | * without them, the volume _will_ creep up with each frequency change |
135 | * and bit 4 (+16) is to keep the signal strength meter enabled | |
136 | */ | |
137 | ||
151c3f82 | 138 | static void send_0_byte(struct rtrack *rt) |
1da177e4 | 139 | { |
151c3f82 HV |
140 | if (rt->curvol == 0 || rt->muted) { |
141 | outb_p(128+64+16+ 1, rt->io); /* wr-enable + data low */ | |
142 | outb_p(128+64+16+2+1, rt->io); /* clock */ | |
1da177e4 LT |
143 | } |
144 | else { | |
151c3f82 HV |
145 | outb_p(128+64+16+8+ 1, rt->io); /* on + wr-enable + data low */ |
146 | outb_p(128+64+16+8+2+1, rt->io); /* clock */ | |
1da177e4 | 147 | } |
e3c92215 | 148 | msleep(1); |
1da177e4 LT |
149 | } |
150 | ||
151c3f82 | 151 | static void send_1_byte(struct rtrack *rt) |
1da177e4 | 152 | { |
151c3f82 HV |
153 | if (rt->curvol == 0 || rt->muted) { |
154 | outb_p(128+64+16+4 +1, rt->io); /* wr-enable+data high */ | |
155 | outb_p(128+64+16+4+2+1, rt->io); /* clock */ | |
4286c6f6 | 156 | } |
1da177e4 | 157 | else { |
151c3f82 HV |
158 | outb_p(128+64+16+8+4 +1, rt->io); /* on+wr-enable+data high */ |
159 | outb_p(128+64+16+8+4+2+1, rt->io); /* clock */ | |
1da177e4 LT |
160 | } |
161 | ||
e3c92215 | 162 | msleep(1); |
1da177e4 LT |
163 | } |
164 | ||
151c3f82 | 165 | static int rt_setfreq(struct rtrack *rt, unsigned long freq) |
1da177e4 LT |
166 | { |
167 | int i; | |
168 | ||
151c3f82 HV |
169 | mutex_lock(&rt->lock); /* Stop other ops interfering */ |
170 | ||
171 | rt->curfreq = freq; | |
1da177e4 LT |
172 | |
173 | /* now uses VIDEO_TUNER_LOW for fine tuning */ | |
174 | ||
175 | freq += 171200; /* Add 10.7 MHz IF */ | |
176 | freq /= 800; /* Convert to 50 kHz units */ | |
4286c6f6 | 177 | |
151c3f82 | 178 | send_0_byte(rt); /* 0: LSB of frequency */ |
1da177e4 LT |
179 | |
180 | for (i = 0; i < 13; i++) /* : frequency bits (1-13) */ | |
181 | if (freq & (1 << i)) | |
151c3f82 | 182 | send_1_byte(rt); |
1da177e4 | 183 | else |
151c3f82 | 184 | send_0_byte(rt); |
1da177e4 | 185 | |
151c3f82 HV |
186 | send_0_byte(rt); /* 14: test bit - always 0 */ |
187 | send_0_byte(rt); /* 15: test bit - always 0 */ | |
1da177e4 | 188 | |
151c3f82 HV |
189 | send_0_byte(rt); /* 16: band data 0 - always 0 */ |
190 | send_0_byte(rt); /* 17: band data 1 - always 0 */ | |
191 | send_0_byte(rt); /* 18: band data 2 - always 0 */ | |
192 | send_0_byte(rt); /* 19: time base - always 0 */ | |
1da177e4 | 193 | |
151c3f82 HV |
194 | send_0_byte(rt); /* 20: spacing (0 = 25 kHz) */ |
195 | send_1_byte(rt); /* 21: spacing (1 = 25 kHz) */ | |
196 | send_0_byte(rt); /* 22: spacing (0 = 25 kHz) */ | |
197 | send_1_byte(rt); /* 23: AM/FM (FM = 1, always) */ | |
1da177e4 | 198 | |
151c3f82 HV |
199 | if (rt->curvol == 0 || rt->muted) |
200 | outb(0xd0, rt->io); /* volume steady + sigstr */ | |
1da177e4 | 201 | else |
151c3f82 | 202 | outb(0xd8, rt->io); /* volume steady + sigstr + on */ |
4286c6f6 | 203 | |
151c3f82 | 204 | mutex_unlock(&rt->lock); |
1da177e4 LT |
205 | |
206 | return 0; | |
207 | } | |
208 | ||
151c3f82 | 209 | static int rt_getsigstr(struct rtrack *rt) |
1da177e4 | 210 | { |
151c3f82 | 211 | int sig = 1; |
1da177e4 | 212 | |
151c3f82 HV |
213 | mutex_lock(&rt->lock); |
214 | if (inb(rt->io) & 2) /* bit set = no signal present */ | |
215 | sig = 0; | |
216 | mutex_unlock(&rt->lock); | |
217 | return sig; | |
218 | } | |
46ff2c72 | 219 | |
385e8d8f DL |
220 | static int vidioc_querycap(struct file *file, void *priv, |
221 | struct v4l2_capability *v) | |
222 | { | |
223 | strlcpy(v->driver, "radio-aimslab", sizeof(v->driver)); | |
224 | strlcpy(v->card, "RadioTrack", sizeof(v->card)); | |
151c3f82 | 225 | strlcpy(v->bus_info, "ISA", sizeof(v->bus_info)); |
385e8d8f | 226 | v->version = RADIO_VERSION; |
151c3f82 | 227 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
385e8d8f DL |
228 | return 0; |
229 | } | |
230 | ||
231 | static int vidioc_g_tuner(struct file *file, void *priv, | |
232 | struct v4l2_tuner *v) | |
1da177e4 | 233 | { |
151c3f82 | 234 | struct rtrack *rt = video_drvdata(file); |
4286c6f6 | 235 | |
385e8d8f DL |
236 | if (v->index > 0) |
237 | return -EINVAL; | |
46ff2c72 | 238 | |
151c3f82 | 239 | strlcpy(v->name, "FM", sizeof(v->name)); |
385e8d8f | 240 | v->type = V4L2_TUNER_RADIO; |
151c3f82 HV |
241 | v->rangelow = 87 * 16000; |
242 | v->rangehigh = 108 * 16000; | |
385e8d8f DL |
243 | v->rxsubchans = V4L2_TUNER_SUB_MONO; |
244 | v->capability = V4L2_TUNER_CAP_LOW; | |
245 | v->audmode = V4L2_TUNER_MODE_MONO; | |
151c3f82 | 246 | v->signal = 0xffff * rt_getsigstr(rt); |
385e8d8f DL |
247 | return 0; |
248 | } | |
249 | ||
250 | static int vidioc_s_tuner(struct file *file, void *priv, | |
251 | struct v4l2_tuner *v) | |
252 | { | |
151c3f82 | 253 | return v->index ? -EINVAL : 0; |
385e8d8f | 254 | } |
46ff2c72 | 255 | |
385e8d8f DL |
256 | static int vidioc_s_frequency(struct file *file, void *priv, |
257 | struct v4l2_frequency *f) | |
258 | { | |
151c3f82 | 259 | struct rtrack *rt = video_drvdata(file); |
46ff2c72 | 260 | |
a3a9e287 HV |
261 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
262 | return -EINVAL; | |
151c3f82 | 263 | rt_setfreq(rt, f->frequency); |
385e8d8f DL |
264 | return 0; |
265 | } | |
46ff2c72 | 266 | |
385e8d8f DL |
267 | static int vidioc_g_frequency(struct file *file, void *priv, |
268 | struct v4l2_frequency *f) | |
269 | { | |
151c3f82 | 270 | struct rtrack *rt = video_drvdata(file); |
46ff2c72 | 271 | |
a3a9e287 HV |
272 | if (f->tuner != 0) |
273 | return -EINVAL; | |
385e8d8f DL |
274 | f->type = V4L2_TUNER_RADIO; |
275 | f->frequency = rt->curfreq; | |
276 | return 0; | |
277 | } | |
46ff2c72 | 278 | |
385e8d8f DL |
279 | static int vidioc_queryctrl(struct file *file, void *priv, |
280 | struct v4l2_queryctrl *qc) | |
281 | { | |
151c3f82 HV |
282 | switch (qc->id) { |
283 | case V4L2_CID_AUDIO_MUTE: | |
284 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); | |
285 | case V4L2_CID_AUDIO_VOLUME: | |
286 | return v4l2_ctrl_query_fill(qc, 0, 0xff, 1, 0xff); | |
385e8d8f DL |
287 | } |
288 | return -EINVAL; | |
289 | } | |
46ff2c72 | 290 | |
385e8d8f DL |
291 | static int vidioc_g_ctrl(struct file *file, void *priv, |
292 | struct v4l2_control *ctrl) | |
293 | { | |
151c3f82 | 294 | struct rtrack *rt = video_drvdata(file); |
46ff2c72 | 295 | |
385e8d8f DL |
296 | switch (ctrl->id) { |
297 | case V4L2_CID_AUDIO_MUTE: | |
298 | ctrl->value = rt->muted; | |
299 | return 0; | |
300 | case V4L2_CID_AUDIO_VOLUME: | |
151c3f82 | 301 | ctrl->value = rt->curvol; |
385e8d8f DL |
302 | return 0; |
303 | } | |
304 | return -EINVAL; | |
305 | } | |
46ff2c72 | 306 | |
385e8d8f DL |
307 | static int vidioc_s_ctrl(struct file *file, void *priv, |
308 | struct v4l2_control *ctrl) | |
309 | { | |
151c3f82 | 310 | struct rtrack *rt = video_drvdata(file); |
46ff2c72 | 311 | |
385e8d8f DL |
312 | switch (ctrl->id) { |
313 | case V4L2_CID_AUDIO_MUTE: | |
314 | if (ctrl->value) | |
315 | rt_mute(rt); | |
316 | else | |
151c3f82 | 317 | rt_setvol(rt, rt->curvol); |
385e8d8f DL |
318 | return 0; |
319 | case V4L2_CID_AUDIO_VOLUME: | |
151c3f82 | 320 | rt_setvol(rt, ctrl->value); |
385e8d8f | 321 | return 0; |
1da177e4 | 322 | } |
385e8d8f DL |
323 | return -EINVAL; |
324 | } | |
325 | ||
385e8d8f DL |
326 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
327 | { | |
328 | *i = 0; | |
329 | return 0; | |
330 | } | |
331 | ||
332 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) | |
333 | { | |
151c3f82 | 334 | return i ? -EINVAL : 0; |
1da177e4 LT |
335 | } |
336 | ||
151c3f82 | 337 | static int vidioc_g_audio(struct file *file, void *priv, |
385e8d8f | 338 | struct v4l2_audio *a) |
1da177e4 | 339 | { |
151c3f82 HV |
340 | a->index = 0; |
341 | strlcpy(a->name, "Radio", sizeof(a->name)); | |
342 | a->capability = V4L2_AUDCAP_STEREO; | |
385e8d8f | 343 | return 0; |
1da177e4 LT |
344 | } |
345 | ||
151c3f82 HV |
346 | static int vidioc_s_audio(struct file *file, void *priv, |
347 | struct v4l2_audio *a) | |
348 | { | |
349 | return a->index ? -EINVAL : 0; | |
350 | } | |
1da177e4 | 351 | |
bec43661 | 352 | static const struct v4l2_file_operations rtrack_fops = { |
1da177e4 | 353 | .owner = THIS_MODULE, |
32958fdd | 354 | .unlocked_ioctl = video_ioctl2, |
1da177e4 LT |
355 | }; |
356 | ||
a399810c | 357 | static const struct v4l2_ioctl_ops rtrack_ioctl_ops = { |
385e8d8f DL |
358 | .vidioc_querycap = vidioc_querycap, |
359 | .vidioc_g_tuner = vidioc_g_tuner, | |
360 | .vidioc_s_tuner = vidioc_s_tuner, | |
361 | .vidioc_g_audio = vidioc_g_audio, | |
362 | .vidioc_s_audio = vidioc_s_audio, | |
363 | .vidioc_g_input = vidioc_g_input, | |
364 | .vidioc_s_input = vidioc_s_input, | |
365 | .vidioc_g_frequency = vidioc_g_frequency, | |
366 | .vidioc_s_frequency = vidioc_s_frequency, | |
367 | .vidioc_queryctrl = vidioc_queryctrl, | |
368 | .vidioc_g_ctrl = vidioc_g_ctrl, | |
369 | .vidioc_s_ctrl = vidioc_s_ctrl, | |
1da177e4 LT |
370 | }; |
371 | ||
372 | static int __init rtrack_init(void) | |
373 | { | |
151c3f82 HV |
374 | struct rtrack *rt = &rtrack_card; |
375 | struct v4l2_device *v4l2_dev = &rt->v4l2_dev; | |
376 | int res; | |
377 | ||
378 | strlcpy(v4l2_dev->name, "rtrack", sizeof(v4l2_dev->name)); | |
379 | rt->io = io; | |
380 | ||
381 | if (rt->io == -1) { | |
b24c20cc | 382 | v4l2_err(v4l2_dev, "you must set an I/O address with io=0x20f or 0x30f\n"); |
1da177e4 LT |
383 | return -EINVAL; |
384 | } | |
385 | ||
151c3f82 HV |
386 | if (!request_region(rt->io, 2, "rtrack")) { |
387 | v4l2_err(v4l2_dev, "port 0x%x already in use\n", rt->io); | |
1da177e4 LT |
388 | return -EBUSY; |
389 | } | |
390 | ||
151c3f82 HV |
391 | res = v4l2_device_register(NULL, v4l2_dev); |
392 | if (res < 0) { | |
393 | release_region(rt->io, 2); | |
394 | v4l2_err(v4l2_dev, "could not register v4l2_device\n"); | |
395 | return res; | |
396 | } | |
4286c6f6 | 397 | |
151c3f82 HV |
398 | strlcpy(rt->vdev.name, v4l2_dev->name, sizeof(rt->vdev.name)); |
399 | rt->vdev.v4l2_dev = v4l2_dev; | |
400 | rt->vdev.fops = &rtrack_fops; | |
401 | rt->vdev.ioctl_ops = &rtrack_ioctl_ops; | |
402 | rt->vdev.release = video_device_release_empty; | |
403 | video_set_drvdata(&rt->vdev, rt); | |
404 | ||
1da177e4 | 405 | /* Set up the I/O locking */ |
4286c6f6 | 406 | |
151c3f82 | 407 | mutex_init(&rt->lock); |
4286c6f6 MCC |
408 | |
409 | /* mute card - prevents noisy bootups */ | |
1da177e4 LT |
410 | |
411 | /* this ensures that the volume is all the way down */ | |
151c3f82 | 412 | outb(0x48, rt->io); /* volume down but still "on" */ |
e3c92215 | 413 | msleep(2000); /* make sure it's totally down */ |
151c3f82 | 414 | outb(0xc0, rt->io); /* steady volume, mute card */ |
1da177e4 | 415 | |
32958fdd HV |
416 | if (video_register_device(&rt->vdev, VFL_TYPE_RADIO, radio_nr) < 0) { |
417 | v4l2_device_unregister(&rt->v4l2_dev); | |
418 | release_region(rt->io, 2); | |
419 | return -EINVAL; | |
420 | } | |
421 | v4l2_info(v4l2_dev, "AIMSlab RadioTrack/RadioReveal card driver.\n"); | |
422 | ||
1da177e4 LT |
423 | return 0; |
424 | } | |
425 | ||
151c3f82 | 426 | static void __exit rtrack_exit(void) |
1da177e4 | 427 | { |
151c3f82 HV |
428 | struct rtrack *rt = &rtrack_card; |
429 | ||
430 | video_unregister_device(&rt->vdev); | |
431 | v4l2_device_unregister(&rt->v4l2_dev); | |
432 | release_region(rt->io, 2); | |
1da177e4 LT |
433 | } |
434 | ||
435 | module_init(rtrack_init); | |
151c3f82 | 436 | module_exit(rtrack_exit); |
1da177e4 | 437 |