USB: usbsevseg: fix max length
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / radio / radio-zoltrix.c
blobf5613b94820353f1744d4f7fff50c42e9be77a69
1 /* zoltrix radio plus driver for Linux radio support
2 * (c) 1998 C. van Schaik <carl@leg.uct.ac.za>
4 * BUGS
5 * Due to the inconsistency in reading from the signal flags
6 * it is difficult to get an accurate tuned signal.
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.
12 * Some code derived from code by Romolo Manfredini
13 * romolo@bicnet.it
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
28 * 2006-07-24 - Converted to V4L2 API
29 * by Mauro Carvalho Chehab <mchehab@infradead.org>
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/io.h> /* outb, outb_p */
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-ioctl.h>
42 MODULE_AUTHOR("C.van Schaik");
43 MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION("0.0.3");
47 #ifndef CONFIG_RADIO_ZOLTRIX_PORT
48 #define CONFIG_RADIO_ZOLTRIX_PORT -1
49 #endif
51 static int io = CONFIG_RADIO_ZOLTRIX_PORT;
52 static int radio_nr = -1;
54 module_param(io, int, 0);
55 MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
56 module_param(radio_nr, int, 0);
58 struct zoltrix {
59 struct v4l2_device v4l2_dev;
60 struct video_device vdev;
61 int io;
62 int curvol;
63 unsigned long curfreq;
64 int muted;
65 unsigned int stereo;
66 struct mutex lock;
69 static struct zoltrix zoltrix_card;
71 static int zol_setvol(struct zoltrix *zol, int vol)
73 zol->curvol = vol;
74 if (zol->muted)
75 return 0;
77 mutex_lock(&zol->lock);
78 if (vol == 0) {
79 outb(0, zol->io);
80 outb(0, zol->io);
81 inb(zol->io + 3); /* Zoltrix needs to be read to confirm */
82 mutex_unlock(&zol->lock);
83 return 0;
86 outb(zol->curvol-1, zol->io);
87 msleep(10);
88 inb(zol->io + 2);
89 mutex_unlock(&zol->lock);
90 return 0;
93 static void zol_mute(struct zoltrix *zol)
95 zol->muted = 1;
96 mutex_lock(&zol->lock);
97 outb(0, zol->io);
98 outb(0, zol->io);
99 inb(zol->io + 3); /* Zoltrix needs to be read to confirm */
100 mutex_unlock(&zol->lock);
103 static void zol_unmute(struct zoltrix *zol)
105 zol->muted = 0;
106 zol_setvol(zol, zol->curvol);
109 static int zol_setfreq(struct zoltrix *zol, unsigned long freq)
111 /* tunes the radio to the desired frequency */
112 struct v4l2_device *v4l2_dev = &zol->v4l2_dev;
113 unsigned long long bitmask, f, m;
114 unsigned int stereo = zol->stereo;
115 int i;
117 if (freq == 0) {
118 v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
119 return -EINVAL;
122 m = (freq / 160 - 8800) * 2;
123 f = (unsigned long long)m + 0x4d1c;
125 bitmask = 0xc480402c10080000ull;
126 i = 45;
128 mutex_lock(&zol->lock);
130 zol->curfreq = freq;
132 outb(0, zol->io);
133 outb(0, zol->io);
134 inb(zol->io + 3); /* Zoltrix needs to be read to confirm */
136 outb(0x40, zol->io);
137 outb(0xc0, zol->io);
139 bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
140 while (i--) {
141 if ((bitmask & 0x8000000000000000ull) != 0) {
142 outb(0x80, zol->io);
143 udelay(50);
144 outb(0x00, zol->io);
145 udelay(50);
146 outb(0x80, zol->io);
147 udelay(50);
148 } else {
149 outb(0xc0, zol->io);
150 udelay(50);
151 outb(0x40, zol->io);
152 udelay(50);
153 outb(0xc0, zol->io);
154 udelay(50);
156 bitmask *= 2;
158 /* termination sequence */
159 outb(0x80, zol->io);
160 outb(0xc0, zol->io);
161 outb(0x40, zol->io);
162 udelay(1000);
163 inb(zol->io + 2);
165 udelay(1000);
167 if (zol->muted) {
168 outb(0, zol->io);
169 outb(0, zol->io);
170 inb(zol->io + 3);
171 udelay(1000);
174 mutex_unlock(&zol->lock);
176 if (!zol->muted)
177 zol_setvol(zol, zol->curvol);
178 return 0;
181 /* Get signal strength */
182 static int zol_getsigstr(struct zoltrix *zol)
184 int a, b;
186 mutex_lock(&zol->lock);
187 outb(0x00, zol->io); /* This stuff I found to do nothing */
188 outb(zol->curvol, zol->io);
189 msleep(20);
191 a = inb(zol->io);
192 msleep(10);
193 b = inb(zol->io);
195 mutex_unlock(&zol->lock);
197 if (a != b)
198 return 0;
200 /* I found this out by playing with a binary scanner on the card io */
201 return a == 0xcf || a == 0xdf || a == 0xef;
204 static int zol_is_stereo(struct zoltrix *zol)
206 int x1, x2;
208 mutex_lock(&zol->lock);
210 outb(0x00, zol->io);
211 outb(zol->curvol, zol->io);
212 msleep(20);
214 x1 = inb(zol->io);
215 msleep(10);
216 x2 = inb(zol->io);
218 mutex_unlock(&zol->lock);
220 return x1 == x2 && x1 == 0xcf;
223 static int vidioc_querycap(struct file *file, void *priv,
224 struct v4l2_capability *v)
226 strlcpy(v->driver, "radio-zoltrix", sizeof(v->driver));
227 strlcpy(v->card, "Zoltrix Radio", sizeof(v->card));
228 strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
229 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
230 return 0;
233 static int vidioc_g_tuner(struct file *file, void *priv,
234 struct v4l2_tuner *v)
236 struct zoltrix *zol = video_drvdata(file);
238 if (v->index > 0)
239 return -EINVAL;
241 strlcpy(v->name, "FM", sizeof(v->name));
242 v->type = V4L2_TUNER_RADIO;
243 v->rangelow = 88 * 16000;
244 v->rangehigh = 108 * 16000;
245 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
246 v->capability = V4L2_TUNER_CAP_LOW;
247 if (zol_is_stereo(zol))
248 v->audmode = V4L2_TUNER_MODE_STEREO;
249 else
250 v->audmode = V4L2_TUNER_MODE_MONO;
251 v->signal = 0xFFFF * zol_getsigstr(zol);
252 return 0;
255 static int vidioc_s_tuner(struct file *file, void *priv,
256 struct v4l2_tuner *v)
258 return v->index ? -EINVAL : 0;
261 static int vidioc_s_frequency(struct file *file, void *priv,
262 struct v4l2_frequency *f)
264 struct zoltrix *zol = video_drvdata(file);
266 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
267 return -EINVAL;
268 if (zol_setfreq(zol, f->frequency) != 0)
269 return -EINVAL;
270 return 0;
273 static int vidioc_g_frequency(struct file *file, void *priv,
274 struct v4l2_frequency *f)
276 struct zoltrix *zol = video_drvdata(file);
278 if (f->tuner != 0)
279 return -EINVAL;
280 f->type = V4L2_TUNER_RADIO;
281 f->frequency = zol->curfreq;
282 return 0;
285 static int vidioc_queryctrl(struct file *file, void *priv,
286 struct v4l2_queryctrl *qc)
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);
294 return -EINVAL;
297 static int vidioc_g_ctrl(struct file *file, void *priv,
298 struct v4l2_control *ctrl)
300 struct zoltrix *zol = video_drvdata(file);
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;
310 return -EINVAL;
313 static int vidioc_s_ctrl(struct file *file, void *priv,
314 struct v4l2_control *ctrl)
316 struct zoltrix *zol = video_drvdata(file);
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);
326 return 0;
327 case V4L2_CID_AUDIO_VOLUME:
328 zol_setvol(zol, ctrl->value / 4096);
329 return 0;
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);
340 if (v->mode & VIDEO_SOUND_MONO) {
341 zol->stereo = 0;
342 zol_setfreq(zol, zol->curfreq);
344 #endif
345 return -EINVAL;
348 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
350 *i = 0;
351 return 0;
354 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
356 return i ? -EINVAL : 0;
359 static int vidioc_g_audio(struct file *file, void *priv,
360 struct v4l2_audio *a)
362 a->index = 0;
363 strlcpy(a->name, "Radio", sizeof(a->name));
364 a->capability = V4L2_AUDCAP_STEREO;
365 return 0;
368 static int vidioc_s_audio(struct file *file, void *priv,
369 struct v4l2_audio *a)
371 return a->index ? -EINVAL : 0;
374 static const struct v4l2_file_operations zoltrix_fops =
376 .owner = THIS_MODULE,
377 .unlocked_ioctl = video_ioctl2,
380 static const struct v4l2_ioctl_ops zoltrix_ioctl_ops = {
381 .vidioc_querycap = vidioc_querycap,
382 .vidioc_g_tuner = vidioc_g_tuner,
383 .vidioc_s_tuner = vidioc_s_tuner,
384 .vidioc_g_audio = vidioc_g_audio,
385 .vidioc_s_audio = vidioc_s_audio,
386 .vidioc_g_input = vidioc_g_input,
387 .vidioc_s_input = vidioc_s_input,
388 .vidioc_g_frequency = vidioc_g_frequency,
389 .vidioc_s_frequency = vidioc_s_frequency,
390 .vidioc_queryctrl = vidioc_queryctrl,
391 .vidioc_g_ctrl = vidioc_g_ctrl,
392 .vidioc_s_ctrl = vidioc_s_ctrl,
395 static int __init zoltrix_init(void)
397 struct zoltrix *zol = &zoltrix_card;
398 struct v4l2_device *v4l2_dev = &zol->v4l2_dev;
399 int res;
401 strlcpy(v4l2_dev->name, "zoltrix", sizeof(v4l2_dev->name));
402 zol->io = io;
403 if (zol->io == -1) {
404 v4l2_err(v4l2_dev, "You must set an I/O address with io=0x20c or 0x30c\n");
405 return -EINVAL;
407 if (zol->io != 0x20c && zol->io != 0x30c) {
408 v4l2_err(v4l2_dev, "invalid port, try 0x20c or 0x30c\n");
409 return -ENXIO;
412 if (!request_region(zol->io, 2, "zoltrix")) {
413 v4l2_err(v4l2_dev, "port 0x%x already in use\n", zol->io);
414 return -EBUSY;
417 res = v4l2_device_register(NULL, v4l2_dev);
418 if (res < 0) {
419 release_region(zol->io, 2);
420 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
421 return res;
424 mutex_init(&zol->lock);
426 /* mute card - prevents noisy bootups */
428 /* this ensures that the volume is all the way down */
430 outb(0, zol->io);
431 outb(0, zol->io);
432 msleep(20);
433 inb(zol->io + 3);
435 zol->curvol = 0;
436 zol->stereo = 1;
438 strlcpy(zol->vdev.name, v4l2_dev->name, sizeof(zol->vdev.name));
439 zol->vdev.v4l2_dev = v4l2_dev;
440 zol->vdev.fops = &zoltrix_fops;
441 zol->vdev.ioctl_ops = &zoltrix_ioctl_ops;
442 zol->vdev.release = video_device_release_empty;
443 video_set_drvdata(&zol->vdev, zol);
445 if (video_register_device(&zol->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
446 v4l2_device_unregister(v4l2_dev);
447 release_region(zol->io, 2);
448 return -EINVAL;
450 v4l2_info(v4l2_dev, "Zoltrix Radio Plus card driver.\n");
452 return 0;
455 static void __exit zoltrix_exit(void)
457 struct zoltrix *zol = &zoltrix_card;
459 video_unregister_device(&zol->vdev);
460 v4l2_device_unregister(&zol->v4l2_dev);
461 release_region(zol->io, 2);
464 module_init(zoltrix_init);
465 module_exit(zoltrix_exit);