V4L/DVB (7402): add macro validation for v4l_compat_ioctl32
[linux-2.6/x86.git] / drivers / media / radio / radio-maxiradio.c
blob0133ecf3e040b0905f157581283c384f6ec7b876
1 /*
2 * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux
3 * (C) 2001 Dimitromanolakis Apostolos <apdim@grecian.net>
5 * Based in the radio Maestro PCI driver. Actually it uses the same chip
6 * for radio but different pci controller.
8 * I didn't have any specs I reversed engineered the protocol from
9 * the windows driver (radio.dll).
11 * The card uses the TEA5757 chip that includes a search function but it
12 * is useless as I haven't found any way to read back the frequency. If
13 * anybody does please mail me.
15 * For the pdf file see:
16 * http://www.semiconductors.philips.com/pip/TEA5757H/V1
19 * CHANGES:
20 * 0.75b
21 * - better pci interface thanks to Francois Romieu <romieu@cogenit.fr>
23 * 0.75 Sun Feb 4 22:51:27 EET 2001
24 * - tiding up
25 * - removed support for multiple devices as it didn't work anyway
27 * BUGS:
28 * - card unmutes if you change frequency
30 * (c) 2006, 2007 by Mauro Carvalho Chehab <mchehab@infradead.org>:
31 * - Conversion to V4L2 API
32 * - Uses video_ioctl2 for parsing and to add debug support
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/ioport.h>
39 #include <linux/delay.h>
40 #include <asm/io.h>
41 #include <asm/uaccess.h>
42 #include <linux/mutex.h>
44 #include <linux/pci.h>
45 #include <linux/videodev2.h>
46 #include <media/v4l2-common.h>
48 #define DRIVER_VERSION "0.77"
50 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
51 #define RADIO_VERSION KERNEL_VERSION(0,7,7)
53 static struct video_device maxiradio_radio;
55 #define dprintk(num, fmt, arg...) \
56 do { \
57 if (maxiradio_radio.debug >= num) \
58 printk(KERN_DEBUG "%s: " fmt, \
59 maxiradio_radio.name, ## arg); } while (0)
61 static struct v4l2_queryctrl radio_qctrl[] = {
63 .id = V4L2_CID_AUDIO_MUTE,
64 .name = "Mute",
65 .minimum = 0,
66 .maximum = 1,
67 .default_value = 1,
68 .type = V4L2_CTRL_TYPE_BOOLEAN,
72 #ifndef PCI_VENDOR_ID_GUILLEMOT
73 #define PCI_VENDOR_ID_GUILLEMOT 0x5046
74 #endif
76 #ifndef PCI_DEVICE_ID_GUILLEMOT
77 #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
78 #endif
81 /* TEA5757 pin mappings */
82 static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
84 static int radio_nr = -1;
85 module_param(radio_nr, int, 0);
88 #define FREQ_LO 50*16000
89 #define FREQ_HI 150*16000
91 #define FREQ_IF 171200 /* 10.7*16000 */
92 #define FREQ_STEP 200 /* 12.5*16 */
94 /* (x==fmhz*16*1000) -> bits */
95 #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1)) \
96 /(FREQ_STEP<<2))<<2)
98 #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
101 static const struct file_operations maxiradio_fops = {
102 .owner = THIS_MODULE,
103 .open = video_exclusive_open,
104 .release = video_exclusive_release,
105 .ioctl = video_ioctl2,
106 #ifdef CONFIG_COMPAT
107 .compat_ioctl = v4l_compat_ioctl32,
108 #endif
109 .llseek = no_llseek,
112 static struct radio_device
114 __u16 io, /* base of radio io */
115 muted, /* VIDEO_AUDIO_MUTE */
116 stereo, /* VIDEO_TUNER_STEREO_ON */
117 tuned; /* signal strength (0 or 0xffff) */
119 unsigned long freq;
121 struct mutex lock;
122 } radio_unit = {
123 .muted =1,
124 .freq = FREQ_LO,
127 static void outbit(unsigned long bit, __u16 io)
129 if (bit != 0)
131 outb( power|wren|data ,io); udelay(4);
132 outb( power|wren|data|clk ,io); udelay(4);
133 outb( power|wren|data ,io); udelay(4);
135 else
137 outb( power|wren ,io); udelay(4);
138 outb( power|wren|clk ,io); udelay(4);
139 outb( power|wren ,io); udelay(4);
143 static void turn_power(__u16 io, int p)
145 if (p != 0) {
146 dprintk(1, "Radio powered on\n");
147 outb(power, io);
148 } else {
149 dprintk(1, "Radio powered off\n");
150 outb(0,io);
154 static void set_freq(__u16 io, __u32 freq)
156 unsigned long int si;
157 int bl;
158 int data = FREQ2BITS(freq);
160 /* TEA5757 shift register bits (see pdf) */
162 outbit(0,io); // 24 search
163 outbit(1,io); // 23 search up/down
165 outbit(0,io); // 22 stereo/mono
167 outbit(0,io); // 21 band
168 outbit(0,io); // 20 band (only 00=FM works I think)
170 outbit(0,io); // 19 port ?
171 outbit(0,io); // 18 port ?
173 outbit(0,io); // 17 search level
174 outbit(0,io); // 16 search level
176 si = 0x8000;
177 for (bl = 1; bl <= 16 ; bl++) {
178 outbit(data & si,io);
179 si >>=1;
182 dprintk(1, "Radio freq set to %d.%02d MHz\n",
183 freq / 16000,
184 freq % 16000 * 100 / 16000);
186 turn_power(io, 1);
189 static int get_stereo(__u16 io)
191 outb(power,io);
192 udelay(4);
194 return !(inb(io) & mo_st);
197 static int get_tune(__u16 io)
199 outb(power+clk,io);
200 udelay(4);
202 return !(inb(io) & mo_st);
206 static int vidioc_querycap (struct file *file, void *priv,
207 struct v4l2_capability *v)
209 strlcpy(v->driver, "radio-maxiradio", sizeof (v->driver));
210 strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof (v->card));
211 sprintf(v->bus_info,"ISA");
212 v->version = RADIO_VERSION;
213 v->capabilities = V4L2_CAP_TUNER;
215 return 0;
218 static int vidioc_g_tuner (struct file *file, void *priv,
219 struct v4l2_tuner *v)
221 struct video_device *dev = video_devdata(file);
222 struct radio_device *card=dev->priv;
224 if (v->index > 0)
225 return -EINVAL;
227 memset(v,0,sizeof(*v));
228 strcpy(v->name, "FM");
229 v->type = V4L2_TUNER_RADIO;
231 v->rangelow=FREQ_LO;
232 v->rangehigh=FREQ_HI;
233 v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
234 v->capability=V4L2_TUNER_CAP_LOW;
235 if(get_stereo(card->io))
236 v->audmode = V4L2_TUNER_MODE_STEREO;
237 else
238 v->audmode = V4L2_TUNER_MODE_MONO;
239 v->signal=0xffff*get_tune(card->io);
241 return 0;
244 static int vidioc_s_tuner (struct file *file, void *priv,
245 struct v4l2_tuner *v)
247 if (v->index > 0)
248 return -EINVAL;
250 return 0;
253 static int vidioc_g_audio (struct file *file, void *priv,
254 struct v4l2_audio *a)
256 if (a->index > 1)
257 return -EINVAL;
259 strcpy(a->name, "FM");
260 a->capability = V4L2_AUDCAP_STEREO;
261 return 0;
264 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
266 *i = 0;
268 return 0;
271 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
273 if (i != 0)
274 return -EINVAL;
276 return 0;
280 static int vidioc_s_audio (struct file *file, void *priv,
281 struct v4l2_audio *a)
283 if (a->index != 0)
284 return -EINVAL;
286 return 0;
289 static int vidioc_s_frequency (struct file *file, void *priv,
290 struct v4l2_frequency *f)
292 struct video_device *dev = video_devdata(file);
293 struct radio_device *card=dev->priv;
295 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) {
296 dprintk(1, "radio freq (%d.%02d MHz) out of range (%d-%d)\n",
297 f->frequency / 16000,
298 f->frequency % 16000 * 100 / 16000,
299 FREQ_LO / 16000, FREQ_HI / 16000);
301 return -EINVAL;
304 card->freq = f->frequency;
305 set_freq(card->io, card->freq);
306 msleep(125);
308 return 0;
311 static int vidioc_g_frequency (struct file *file, void *priv,
312 struct v4l2_frequency *f)
314 struct video_device *dev = video_devdata(file);
315 struct radio_device *card=dev->priv;
317 f->type = V4L2_TUNER_RADIO;
318 f->frequency = card->freq;
320 dprintk(4, "radio freq is %d.%02d MHz",
321 f->frequency / 16000,
322 f->frequency % 16000 * 100 / 16000);
324 return 0;
327 static int vidioc_queryctrl (struct file *file, void *priv,
328 struct v4l2_queryctrl *qc)
330 int i;
332 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
333 if (qc->id && qc->id == radio_qctrl[i].id) {
334 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
335 return (0);
339 return -EINVAL;
342 static int vidioc_g_ctrl (struct file *file, void *priv,
343 struct v4l2_control *ctrl)
345 struct video_device *dev = video_devdata(file);
346 struct radio_device *card=dev->priv;
348 switch (ctrl->id) {
349 case V4L2_CID_AUDIO_MUTE:
350 ctrl->value=card->muted;
351 return (0);
354 return -EINVAL;
357 static int vidioc_s_ctrl (struct file *file, void *priv,
358 struct v4l2_control *ctrl)
360 struct video_device *dev = video_devdata(file);
361 struct radio_device *card=dev->priv;
363 switch (ctrl->id) {
364 case V4L2_CID_AUDIO_MUTE:
365 card->muted = ctrl->value;
366 if(card->muted)
367 turn_power(card->io, 0);
368 else
369 set_freq(card->io, card->freq);
370 return 0;
373 return -EINVAL;
376 static struct video_device maxiradio_radio =
378 .owner = THIS_MODULE,
379 .name = "Maxi Radio FM2000 radio",
380 .type = VID_TYPE_TUNER,
381 .fops = &maxiradio_fops,
383 .vidioc_querycap = vidioc_querycap,
384 .vidioc_g_tuner = vidioc_g_tuner,
385 .vidioc_s_tuner = vidioc_s_tuner,
386 .vidioc_g_audio = vidioc_g_audio,
387 .vidioc_s_audio = vidioc_s_audio,
388 .vidioc_g_input = vidioc_g_input,
389 .vidioc_s_input = vidioc_s_input,
390 .vidioc_g_frequency = vidioc_g_frequency,
391 .vidioc_s_frequency = vidioc_s_frequency,
392 .vidioc_queryctrl = vidioc_queryctrl,
393 .vidioc_g_ctrl = vidioc_g_ctrl,
394 .vidioc_s_ctrl = vidioc_s_ctrl,
397 static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
399 if(!request_region(pci_resource_start(pdev, 0),
400 pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
401 printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
402 goto err_out;
405 if (pci_enable_device(pdev))
406 goto err_out_free_region;
408 radio_unit.io = pci_resource_start(pdev, 0);
409 mutex_init(&radio_unit.lock);
410 maxiradio_radio.priv = &radio_unit;
412 if (video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
413 printk("radio-maxiradio: can't register device!");
414 goto err_out_free_region;
417 printk(KERN_INFO "radio-maxiradio: version "
418 DRIVER_VERSION
419 " time "
420 __TIME__ " "
421 __DATE__
422 "\n");
424 printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
425 radio_unit.io);
426 return 0;
428 err_out_free_region:
429 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
430 err_out:
431 return -ENODEV;
434 static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
436 video_unregister_device(&maxiradio_radio);
437 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
440 static struct pci_device_id maxiradio_pci_tbl[] = {
441 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
442 PCI_ANY_ID, PCI_ANY_ID, },
443 { 0,}
446 MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
448 static struct pci_driver maxiradio_driver = {
449 .name = "radio-maxiradio",
450 .id_table = maxiradio_pci_tbl,
451 .probe = maxiradio_init_one,
452 .remove = __devexit_p(maxiradio_remove_one),
455 static int __init maxiradio_radio_init(void)
457 return pci_register_driver(&maxiradio_driver);
460 static void __exit maxiradio_radio_exit(void)
462 pci_unregister_driver(&maxiradio_driver);
465 module_init(maxiradio_radio_init);
466 module_exit(maxiradio_radio_exit);
468 MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
469 MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
470 MODULE_LICENSE("GPL");
472 module_param_named(debug,maxiradio_radio.debug, int, 0644);
473 MODULE_PARM_DESC(debug,"activates debug info");