V4L/DVB (10894): ISA radio drivers: improve kernel log message
[deliverable/linux.git] / drivers / media / radio / radio-zoltrix.c
1 /* zoltrix radio plus driver for Linux radio support
2 * (c) 1998 C. van Schaik <carl@leg.uct.ac.za>
3 *
4 * BUGS
5 * Due to the inconsistency in reading from the signal flags
6 * it is difficult to get an accurate tuned signal.
7 *
8 * It seems that the card is not linear to 0 volume. It cuts off
9 * at a low volume, and it is not possible (at least I have not found)
10 * to get fine volume control over the low volume range.
11 *
12 * Some code derived from code by Romolo Manfredini
13 * romolo@bicnet.it
14 *
15 * 1999-05-06 - (C. van Schaik)
16 * - Make signal strength and stereo scans
17 * kinder to cpu while in delay
18 * 1999-01-05 - (C. van Schaik)
19 * - Changed tuning to 1/160Mhz accuracy
20 * - Added stereo support
21 * (card defaults to stereo)
22 * (can explicitly force mono on the card)
23 * (can detect if station is in stereo)
24 * - Added unmute function
25 * - Reworked ioctl functions
26 * 2002-07-15 - Fix Stereo typo
27 *
28 * 2006-07-24 - Converted to V4L2 API
29 * by Mauro Carvalho Chehab <mchehab@infradead.org>
30 */
31
32 #include <linux/module.h> /* Modules */
33 #include <linux/init.h> /* Initdata */
34 #include <linux/ioport.h> /* request_region */
35 #include <linux/delay.h> /* udelay, msleep */
36 #include <linux/videodev2.h> /* kernel radio structs */
37 #include <linux/mutex.h>
38 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
39 #include <linux/io.h> /* outb, outb_p */
40 #include <linux/uaccess.h> /* copy to/from user */
41 #include <media/v4l2-device.h>
42 #include <media/v4l2-ioctl.h>
43
44 MODULE_AUTHOR("C.van Schaik");
45 MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
46 MODULE_LICENSE("GPL");
47
48 #ifndef CONFIG_RADIO_ZOLTRIX_PORT
49 #define CONFIG_RADIO_ZOLTRIX_PORT -1
50 #endif
51
52 static int io = CONFIG_RADIO_ZOLTRIX_PORT;
53 static int radio_nr = -1;
54
55 module_param(io, int, 0);
56 MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
57 module_param(radio_nr, int, 0);
58
59 #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
60
61 struct zoltrix {
62 struct v4l2_device v4l2_dev;
63 struct video_device vdev;
64 int io;
65 int curvol;
66 unsigned long curfreq;
67 int muted;
68 unsigned int stereo;
69 struct mutex lock;
70 };
71
72 static struct zoltrix zoltrix_card;
73
74 static int zol_setvol(struct zoltrix *zol, int vol)
75 {
76 zol->curvol = vol;
77 if (zol->muted)
78 return 0;
79
80 mutex_lock(&zol->lock);
81 if (vol == 0) {
82 outb(0, zol->io);
83 outb(0, zol->io);
84 inb(zol->io + 3); /* Zoltrix needs to be read to confirm */
85 mutex_unlock(&zol->lock);
86 return 0;
87 }
88
89 outb(zol->curvol-1, zol->io);
90 msleep(10);
91 inb(zol->io + 2);
92 mutex_unlock(&zol->lock);
93 return 0;
94 }
95
96 static void zol_mute(struct zoltrix *zol)
97 {
98 zol->muted = 1;
99 mutex_lock(&zol->lock);
100 outb(0, zol->io);
101 outb(0, zol->io);
102 inb(zol->io + 3); /* Zoltrix needs to be read to confirm */
103 mutex_unlock(&zol->lock);
104 }
105
106 static void zol_unmute(struct zoltrix *zol)
107 {
108 zol->muted = 0;
109 zol_setvol(zol, zol->curvol);
110 }
111
112 static int zol_setfreq(struct zoltrix *zol, unsigned long freq)
113 {
114 /* tunes the radio to the desired frequency */
115 struct v4l2_device *v4l2_dev = &zol->v4l2_dev;
116 unsigned long long bitmask, f, m;
117 unsigned int stereo = zol->stereo;
118 int i;
119
120 if (freq == 0) {
121 v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
122 return -EINVAL;
123 }
124
125 m = (freq / 160 - 8800) * 2;
126 f = (unsigned long long)m + 0x4d1c;
127
128 bitmask = 0xc480402c10080000ull;
129 i = 45;
130
131 mutex_lock(&zol->lock);
132
133 zol->curfreq = freq;
134
135 outb(0, zol->io);
136 outb(0, zol->io);
137 inb(zol->io + 3); /* Zoltrix needs to be read to confirm */
138
139 outb(0x40, zol->io);
140 outb(0xc0, zol->io);
141
142 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
143 while (i--) {
144 if ((bitmask & 0x8000000000000000ull) != 0) {
145 outb(0x80, zol->io);
146 udelay(50);
147 outb(0x00, zol->io);
148 udelay(50);
149 outb(0x80, zol->io);
150 udelay(50);
151 } else {
152 outb(0xc0, zol->io);
153 udelay(50);
154 outb(0x40, zol->io);
155 udelay(50);
156 outb(0xc0, zol->io);
157 udelay(50);
158 }
159 bitmask *= 2;
160 }
161 /* termination sequence */
162 outb(0x80, zol->io);
163 outb(0xc0, zol->io);
164 outb(0x40, zol->io);
165 udelay(1000);
166 inb(zol->io + 2);
167
168 udelay(1000);
169
170 if (zol->muted) {
171 outb(0, zol->io);
172 outb(0, zol->io);
173 inb(zol->io + 3);
174 udelay(1000);
175 }
176
177 mutex_unlock(&zol->lock);
178
179 if (!zol->muted)
180 zol_setvol(zol, zol->curvol);
181 return 0;
182 }
183
184 /* Get signal strength */
185 static int zol_getsigstr(struct zoltrix *zol)
186 {
187 int a, b;
188
189 mutex_lock(&zol->lock);
190 outb(0x00, zol->io); /* This stuff I found to do nothing */
191 outb(zol->curvol, zol->io);
192 msleep(20);
193
194 a = inb(zol->io);
195 msleep(10);
196 b = inb(zol->io);
197
198 mutex_unlock(&zol->lock);
199
200 if (a != b)
201 return 0;
202
203 /* I found this out by playing with a binary scanner on the card io */
204 return a == 0xcf || a == 0xdf || a == 0xef;
205 }
206
207 static int zol_is_stereo(struct zoltrix *zol)
208 {
209 int x1, x2;
210
211 mutex_lock(&zol->lock);
212
213 outb(0x00, zol->io);
214 outb(zol->curvol, zol->io);
215 msleep(20);
216
217 x1 = inb(zol->io);
218 msleep(10);
219 x2 = inb(zol->io);
220
221 mutex_unlock(&zol->lock);
222
223 return x1 == x2 && x1 == 0xcf;
224 }
225
226 static int vidioc_querycap(struct file *file, void *priv,
227 struct v4l2_capability *v)
228 {
229 strlcpy(v->driver, "radio-zoltrix", sizeof(v->driver));
230 strlcpy(v->card, "Zoltrix Radio", sizeof(v->card));
231 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
232 v->version = RADIO_VERSION;
233 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
234 return 0;
235 }
236
237 static int vidioc_g_tuner(struct file *file, void *priv,
238 struct v4l2_tuner *v)
239 {
240 struct zoltrix *zol = video_drvdata(file);
241
242 if (v->index > 0)
243 return -EINVAL;
244
245 strlcpy(v->name, "FM", sizeof(v->name));
246 v->type = V4L2_TUNER_RADIO;
247 v->rangelow = 88 * 16000;
248 v->rangehigh = 108 * 16000;
249 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
250 v->capability = V4L2_TUNER_CAP_LOW;
251 if (zol_is_stereo(zol))
252 v->audmode = V4L2_TUNER_MODE_STEREO;
253 else
254 v->audmode = V4L2_TUNER_MODE_MONO;
255 v->signal = 0xFFFF * zol_getsigstr(zol);
256 return 0;
257 }
258
259 static int vidioc_s_tuner(struct file *file, void *priv,
260 struct v4l2_tuner *v)
261 {
262 return v->index ? -EINVAL : 0;
263 }
264
265 static int vidioc_s_frequency(struct file *file, void *priv,
266 struct v4l2_frequency *f)
267 {
268 struct zoltrix *zol = video_drvdata(file);
269
270 if (zol_setfreq(zol, f->frequency) != 0)
271 return -EINVAL;
272 return 0;
273 }
274
275 static int vidioc_g_frequency(struct file *file, void *priv,
276 struct v4l2_frequency *f)
277 {
278 struct zoltrix *zol = video_drvdata(file);
279
280 f->type = V4L2_TUNER_RADIO;
281 f->frequency = zol->curfreq;
282 return 0;
283 }
284
285 static int vidioc_queryctrl(struct file *file, void *priv,
286 struct v4l2_queryctrl *qc)
287 {
288 switch (qc->id) {
289 case V4L2_CID_AUDIO_MUTE:
290 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
291 case V4L2_CID_AUDIO_VOLUME:
292 return v4l2_ctrl_query_fill(qc, 0, 65535, 4096, 65535);
293 }
294 return -EINVAL;
295 }
296
297 static int vidioc_g_ctrl(struct file *file, void *priv,
298 struct v4l2_control *ctrl)
299 {
300 struct zoltrix *zol = video_drvdata(file);
301
302 switch (ctrl->id) {
303 case V4L2_CID_AUDIO_MUTE:
304 ctrl->value = zol->muted;
305 return 0;
306 case V4L2_CID_AUDIO_VOLUME:
307 ctrl->value = zol->curvol * 4096;
308 return 0;
309 }
310 return -EINVAL;
311 }
312
313 static int vidioc_s_ctrl(struct file *file, void *priv,
314 struct v4l2_control *ctrl)
315 {
316 struct zoltrix *zol = video_drvdata(file);
317
318 switch (ctrl->id) {
319 case V4L2_CID_AUDIO_MUTE:
320 if (ctrl->value)
321 zol_mute(zol);
322 else {
323 zol_unmute(zol);
324 zol_setvol(zol, zol->curvol);
325 }
326 return 0;
327 case V4L2_CID_AUDIO_VOLUME:
328 zol_setvol(zol, ctrl->value / 4096);
329 return 0;
330 }
331 zol->stereo = 1;
332 if (zol_setfreq(zol, zol->curfreq) != 0)
333 return -EINVAL;
334 #if 0
335 /* FIXME: Implement stereo/mono switch on V4L2 */
336 if (v->mode & VIDEO_SOUND_STEREO) {
337 zol->stereo = 1;
338 zol_setfreq(zol, zol->curfreq);
339 }
340 if (v->mode & VIDEO_SOUND_MONO) {
341 zol->stereo = 0;
342 zol_setfreq(zol, zol->curfreq);
343 }
344 #endif
345 return -EINVAL;
346 }
347
348 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
349 {
350 *i = 0;
351 return 0;
352 }
353
354 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
355 {
356 return i ? -EINVAL : 0;
357 }
358
359 static int vidioc_g_audio(struct file *file, void *priv,
360 struct v4l2_audio *a)
361 {
362 a->index = 0;
363 strlcpy(a->name, "Radio", sizeof(a->name));
364 a->capability = V4L2_AUDCAP_STEREO;
365 return 0;
366 }
367
368 static int vidioc_s_audio(struct file *file, void *priv,
369 struct v4l2_audio *a)
370 {
371 return a->index ? -EINVAL : 0;
372 }
373
374 static int zoltrix_open(struct file *file)
375 {
376 return 0;
377 }
378
379 static int zoltrix_release(struct file *file)
380 {
381 return 0;
382 }
383
384 static const struct v4l2_file_operations zoltrix_fops =
385 {
386 .owner = THIS_MODULE,
387 .open = zoltrix_open,
388 .release = zoltrix_release,
389 .ioctl = video_ioctl2,
390 };
391
392 static const struct v4l2_ioctl_ops zoltrix_ioctl_ops = {
393 .vidioc_querycap = vidioc_querycap,
394 .vidioc_g_tuner = vidioc_g_tuner,
395 .vidioc_s_tuner = vidioc_s_tuner,
396 .vidioc_g_audio = vidioc_g_audio,
397 .vidioc_s_audio = vidioc_s_audio,
398 .vidioc_g_input = vidioc_g_input,
399 .vidioc_s_input = vidioc_s_input,
400 .vidioc_g_frequency = vidioc_g_frequency,
401 .vidioc_s_frequency = vidioc_s_frequency,
402 .vidioc_queryctrl = vidioc_queryctrl,
403 .vidioc_g_ctrl = vidioc_g_ctrl,
404 .vidioc_s_ctrl = vidioc_s_ctrl,
405 };
406
407 static int __init zoltrix_init(void)
408 {
409 struct zoltrix *zol = &zoltrix_card;
410 struct v4l2_device *v4l2_dev = &zol->v4l2_dev;
411 int res;
412
413 strlcpy(v4l2_dev->name, "zoltrix", sizeof(v4l2_dev->name));
414 zol->io = io;
415 if (zol->io == -1) {
416 v4l2_err(v4l2_dev, "You must set an I/O address with io=0x20c or 0x30c\n");
417 return -EINVAL;
418 }
419 if (zol->io != 0x20c && zol->io != 0x30c) {
420 v4l2_err(v4l2_dev, "invalid port, try 0x20c or 0x30c\n");
421 return -ENXIO;
422 }
423
424 if (!request_region(zol->io, 2, "zoltrix")) {
425 v4l2_err(v4l2_dev, "port 0x%x already in use\n", zol->io);
426 return -EBUSY;
427 }
428
429 res = v4l2_device_register(NULL, v4l2_dev);
430 if (res < 0) {
431 release_region(zol->io, 2);
432 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
433 return res;
434 }
435
436 strlcpy(zol->vdev.name, v4l2_dev->name, sizeof(zol->vdev.name));
437 zol->vdev.v4l2_dev = v4l2_dev;
438 zol->vdev.fops = &zoltrix_fops;
439 zol->vdev.ioctl_ops = &zoltrix_ioctl_ops;
440 zol->vdev.release = video_device_release_empty;
441 video_set_drvdata(&zol->vdev, zol);
442
443 if (video_register_device(&zol->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
444 v4l2_device_unregister(v4l2_dev);
445 release_region(zol->io, 2);
446 return -EINVAL;
447 }
448 v4l2_info(v4l2_dev, "Zoltrix Radio Plus card driver.\n");
449
450 mutex_init(&zol->lock);
451
452 /* mute card - prevents noisy bootups */
453
454 /* this ensures that the volume is all the way down */
455
456 outb(0, zol->io);
457 outb(0, zol->io);
458 msleep(20);
459 inb(zol->io + 3);
460
461 zol->curvol = 0;
462 zol->stereo = 1;
463
464 return 0;
465 }
466
467 static void __exit zoltrix_exit(void)
468 {
469 struct zoltrix *zol = &zoltrix_card;
470
471 video_unregister_device(&zol->vdev);
472 v4l2_device_unregister(&zol->v4l2_dev);
473 release_region(zol->io, 2);
474 }
475
476 module_init(zoltrix_init);
477 module_exit(zoltrix_exit);
478
This page took 0.049278 seconds and 5 git commands to generate.