2 ***************************************************************************
4 * radio-gemtek-pci.c - Gemtek PCI Radio driver
5 * (C) 2001 Vladimir Shebordaev <vshebordaev@mail.ru>
7 ***************************************************************************
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
24 ***************************************************************************
26 * Gemtek Corp still silently refuses to release any specifications
27 * of their multimedia devices, so the protocol still has to be
30 * The v4l code was inspired by Jonas Munsin's Gemtek serial line
31 * radio device driver.
33 * Please, let me know if this piece of code was useful :)
35 * TODO: multiple device support and portability were not tested
37 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
39 ***************************************************************************
42 #include <linux/types.h>
43 #include <linux/list.h>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/pci.h>
47 #include <linux/videodev2.h>
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-ioctl.h>
50 #include <linux/errno.h>
52 #include <linux/version.h> /* for KERNEL_VERSION MACRO */
53 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
55 static struct v4l2_queryctrl radio_qctrl[] = {
57 .id = V4L2_CID_AUDIO_MUTE,
62 .type = V4L2_CTRL_TYPE_BOOLEAN,
64 .id = V4L2_CID_AUDIO_VOLUME,
69 .default_value = 0xff,
70 .type = V4L2_CTRL_TYPE_INTEGER,
75 #include <asm/uaccess.h>
77 #ifndef PCI_VENDOR_ID_GEMTEK
78 #define PCI_VENDOR_ID_GEMTEK 0x5046
81 #ifndef PCI_DEVICE_ID_GEMTEK_PR103
82 #define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
85 #ifndef GEMTEK_PCI_RANGE_LOW
86 #define GEMTEK_PCI_RANGE_LOW (87*16000)
89 #ifndef GEMTEK_PCI_RANGE_HIGH
90 #define GEMTEK_PCI_RANGE_HIGH (108*16000)
93 struct gemtek_pci_card {
94 struct video_device *videodev;
99 u32 current_frequency;
103 static const char rcsid[] = "$Id: radio-gemtek-pci.c,v 1.1 2001/07/23 08:08:16 ted Exp ted $";
105 static int nr_radio = -1;
107 static inline u8 gemtek_pci_out( u16 value, u32 port )
114 #define _b0( v ) *((u8 *)&v)
115 static void __gemtek_pci_cmd( u16 value, u32 port, u8 *last_byte, int keep )
117 register u8 byte = *last_byte;
138 static inline void gemtek_pci_nil( u32 port, u8 *last_byte )
140 __gemtek_pci_cmd( 0x00, port, last_byte, false );
143 static inline void gemtek_pci_cmd( u16 cmd, u32 port, u8 *last_byte )
145 __gemtek_pci_cmd( cmd, port, last_byte, true );
148 static void gemtek_pci_setfrequency( struct gemtek_pci_card *card, unsigned long frequency )
151 register u32 value = frequency / 200 + 856;
152 register u16 mask = 0x8000;
154 u32 port = card->iobase;
156 last_byte = gemtek_pci_out( 0x06, port );
160 gemtek_pci_nil( port, &last_byte );
166 gemtek_pci_cmd( value & mask, port, &last_byte );
175 static inline void gemtek_pci_mute( struct gemtek_pci_card *card )
177 outb( 0x1f, card->iobase );
181 static inline void gemtek_pci_unmute( struct gemtek_pci_card *card )
184 gemtek_pci_setfrequency( card, card->current_frequency );
189 static inline unsigned int gemtek_pci_getsignal( struct gemtek_pci_card *card )
191 return ( inb( card->iobase ) & 0x08 ) ? 0 : 1;
194 static int vidioc_querycap(struct file *file, void *priv,
195 struct v4l2_capability *v)
197 strlcpy(v->driver, "radio-gemtek-pci", sizeof(v->driver));
198 strlcpy(v->card, "GemTek PCI Radio", sizeof(v->card));
199 sprintf(v->bus_info, "ISA");
200 v->version = RADIO_VERSION;
201 v->capabilities = V4L2_CAP_TUNER;
205 static int vidioc_g_tuner(struct file *file, void *priv,
206 struct v4l2_tuner *v)
208 struct video_device *dev = video_devdata(file);
209 struct gemtek_pci_card *card = dev->priv;
214 strcpy(v->name, "FM");
215 v->type = V4L2_TUNER_RADIO;
216 v->rangelow = GEMTEK_PCI_RANGE_LOW;
217 v->rangehigh = GEMTEK_PCI_RANGE_HIGH;
218 v->rxsubchans = V4L2_TUNER_SUB_MONO;
219 v->capability = V4L2_TUNER_CAP_LOW;
220 v->audmode = V4L2_TUNER_MODE_MONO;
221 v->signal = 0xffff * gemtek_pci_getsignal(card);
225 static int vidioc_s_tuner(struct file *file, void *priv,
226 struct v4l2_tuner *v)
233 static int vidioc_s_frequency(struct file *file, void *priv,
234 struct v4l2_frequency *f)
236 struct video_device *dev = video_devdata(file);
237 struct gemtek_pci_card *card = dev->priv;
239 if ( (f->frequency < GEMTEK_PCI_RANGE_LOW) ||
240 (f->frequency > GEMTEK_PCI_RANGE_HIGH) )
242 gemtek_pci_setfrequency(card, f->frequency);
243 card->current_frequency = f->frequency;
248 static int vidioc_g_frequency(struct file *file, void *priv,
249 struct v4l2_frequency *f)
251 struct video_device *dev = video_devdata(file);
252 struct gemtek_pci_card *card = dev->priv;
254 f->type = V4L2_TUNER_RADIO;
255 f->frequency = card->current_frequency;
259 static int vidioc_queryctrl(struct file *file, void *priv,
260 struct v4l2_queryctrl *qc)
263 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
264 if (qc->id && qc->id == radio_qctrl[i].id) {
265 memcpy(qc, &(radio_qctrl[i]),
273 static int vidioc_g_ctrl(struct file *file, void *priv,
274 struct v4l2_control *ctrl)
276 struct video_device *dev = video_devdata(file);
277 struct gemtek_pci_card *card = dev->priv;
280 case V4L2_CID_AUDIO_MUTE:
281 ctrl->value = card->mute;
283 case V4L2_CID_AUDIO_VOLUME:
293 static int vidioc_s_ctrl(struct file *file, void *priv,
294 struct v4l2_control *ctrl)
296 struct video_device *dev = video_devdata(file);
297 struct gemtek_pci_card *card = dev->priv;
300 case V4L2_CID_AUDIO_MUTE:
302 gemtek_pci_mute(card);
304 gemtek_pci_unmute(card);
306 case V4L2_CID_AUDIO_VOLUME:
308 gemtek_pci_unmute(card);
310 gemtek_pci_mute(card);
316 static int vidioc_g_audio(struct file *file, void *priv,
317 struct v4l2_audio *a)
322 strcpy(a->name, "Radio");
323 a->capability = V4L2_AUDCAP_STEREO;
327 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
333 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
340 static int vidioc_s_audio(struct file *file, void *priv,
341 struct v4l2_audio *a)
352 static char *card_names[] __devinitdata = {
356 static struct pci_device_id gemtek_pci_id[] =
358 { PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
359 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
363 MODULE_DEVICE_TABLE( pci, gemtek_pci_id );
367 static const struct file_operations gemtek_pci_fops = {
368 .owner = THIS_MODULE,
369 .open = video_exclusive_open,
370 .release = video_exclusive_release,
371 .ioctl = video_ioctl2,
373 .compat_ioctl = v4l_compat_ioctl32,
378 static const struct v4l2_ioctl_ops gemtek_pci_ioctl_ops = {
379 .vidioc_querycap = vidioc_querycap,
380 .vidioc_g_tuner = vidioc_g_tuner,
381 .vidioc_s_tuner = vidioc_s_tuner,
382 .vidioc_g_audio = vidioc_g_audio,
383 .vidioc_s_audio = vidioc_s_audio,
384 .vidioc_g_input = vidioc_g_input,
385 .vidioc_s_input = vidioc_s_input,
386 .vidioc_g_frequency = vidioc_g_frequency,
387 .vidioc_s_frequency = vidioc_s_frequency,
388 .vidioc_queryctrl = vidioc_queryctrl,
389 .vidioc_g_ctrl = vidioc_g_ctrl,
390 .vidioc_s_ctrl = vidioc_s_ctrl,
393 static struct video_device vdev_template = {
394 .name = "Gemtek PCI Radio",
395 .fops = &gemtek_pci_fops,
396 .ioctl_ops = &gemtek_pci_ioctl_ops,
399 static int __devinit gemtek_pci_probe( struct pci_dev *pci_dev, const struct pci_device_id *pci_id )
401 struct gemtek_pci_card *card;
402 struct video_device *devradio;
404 if ( (card = kzalloc( sizeof( struct gemtek_pci_card ), GFP_KERNEL )) == NULL ) {
405 printk( KERN_ERR "gemtek_pci: out of memory\n" );
409 if ( pci_enable_device( pci_dev ) )
412 card->iobase = pci_resource_start( pci_dev, 0 );
413 card->length = pci_resource_len( pci_dev, 0 );
415 if ( request_region( card->iobase, card->length, card_names[pci_id->driver_data] ) == NULL ) {
416 printk( KERN_ERR "gemtek_pci: i/o port already in use\n" );
420 pci_set_drvdata( pci_dev, card );
422 if ( (devradio = kmalloc( sizeof( struct video_device ), GFP_KERNEL )) == NULL ) {
423 printk( KERN_ERR "gemtek_pci: out of memory\n" );
426 *devradio = vdev_template;
428 if (video_register_device(devradio, VFL_TYPE_RADIO, nr_radio) < 0) {
433 card->videodev = devradio;
434 devradio->priv = card;
435 gemtek_pci_mute( card );
437 printk( KERN_INFO "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
438 pci_dev->revision, card->iobase, card->iobase + card->length - 1 );
443 release_region( card->iobase, card->length );
450 static void __devexit gemtek_pci_remove( struct pci_dev *pci_dev )
452 struct gemtek_pci_card *card = pci_get_drvdata( pci_dev );
454 video_unregister_device( card->videodev );
455 kfree( card->videodev );
457 release_region( card->iobase, card->length );
460 gemtek_pci_mute( card );
464 pci_set_drvdata( pci_dev, NULL );
467 static struct pci_driver gemtek_pci_driver =
469 .name = "gemtek_pci",
470 .id_table = gemtek_pci_id,
471 .probe = gemtek_pci_probe,
472 .remove = __devexit_p(gemtek_pci_remove),
475 static int __init gemtek_pci_init_module( void )
477 return pci_register_driver( &gemtek_pci_driver );
480 static void __exit gemtek_pci_cleanup_module( void )
482 pci_unregister_driver(&gemtek_pci_driver);
485 MODULE_AUTHOR( "Vladimir Shebordaev <vshebordaev@mail.ru>" );
486 MODULE_DESCRIPTION( "The video4linux driver for the Gemtek PCI Radio Card" );
487 MODULE_LICENSE("GPL");
489 module_param(mx, bool, 0);
490 MODULE_PARM_DESC( mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not" );
491 module_param(nr_radio, int, 0);
492 MODULE_PARM_DESC( nr_radio, "video4linux device number to use");
494 module_init( gemtek_pci_init_module );
495 module_exit( gemtek_pci_cleanup_module );