blob: 2b834b95f3e7bc2f43f493b929bbee7d685e81d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 ***************************************************************************
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * radio-gemtek-pci.c - Gemtek PCI Radio driver
5 * (C) 2001 Vladimir Shebordaev <vshebordaev@mail.ru>
6 *
7 ***************************************************************************
8 *
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.
13 *
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.
18 *
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,
22 * USA.
23 *
24 ***************************************************************************
25 *
26 * Gemtek Corp still silently refuses to release any specifications
27 * of their multimedia devices, so the protocol still has to be
28 * reverse engineered.
29 *
30 * The v4l code was inspired by Jonas Munsin's Gemtek serial line
31 * radio device driver.
32 *
33 * Please, let me know if this piece of code was useful :)
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030034 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * TODO: multiple device support and portability were not tested
36 *
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -030037 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
38 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 ***************************************************************************
40 */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/types.h>
43#include <linux/list.h>
44#include <linux/module.h>
45#include <linux/init.h>
46#include <linux/pci.h>
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -030047#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030048#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030049#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/errno.h>
51
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -030052#include <linux/version.h> /* for KERNEL_VERSION MACRO */
53#define RADIO_VERSION KERNEL_VERSION(0,0,2)
54
55static struct v4l2_queryctrl radio_qctrl[] = {
56 {
57 .id = V4L2_CID_AUDIO_MUTE,
58 .name = "Mute",
59 .minimum = 0,
60 .maximum = 1,
61 .default_value = 1,
62 .type = V4L2_CTRL_TYPE_BOOLEAN,
63 },{
64 .id = V4L2_CID_AUDIO_VOLUME,
65 .name = "Volume",
66 .minimum = 0,
67 .maximum = 65535,
68 .step = 65535,
69 .default_value = 0xff,
70 .type = V4L2_CTRL_TYPE_INTEGER,
71 }
72};
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <asm/io.h>
75#include <asm/uaccess.h>
76
77#ifndef PCI_VENDOR_ID_GEMTEK
78#define PCI_VENDOR_ID_GEMTEK 0x5046
79#endif
80
81#ifndef PCI_DEVICE_ID_GEMTEK_PR103
82#define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
83#endif
84
85#ifndef GEMTEK_PCI_RANGE_LOW
86#define GEMTEK_PCI_RANGE_LOW (87*16000)
87#endif
88
89#ifndef GEMTEK_PCI_RANGE_HIGH
90#define GEMTEK_PCI_RANGE_HIGH (108*16000)
91#endif
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct gemtek_pci_card {
94 struct video_device *videodev;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 u32 iobase;
97 u32 length;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 u32 current_frequency;
100 u8 mute;
101};
102
103static const char rcsid[] = "$Id: radio-gemtek-pci.c,v 1.1 2001/07/23 08:08:16 ted Exp ted $";
104
105static int nr_radio = -1;
106
107static inline u8 gemtek_pci_out( u16 value, u32 port )
108{
109 outw( value, port );
110
111 return (u8)value;
112}
113
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300114#define _b0( v ) *((u8 *)&v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static void __gemtek_pci_cmd( u16 value, u32 port, u8 *last_byte, int keep )
116{
117 register u8 byte = *last_byte;
118
119 if ( !value ) {
120 if ( !keep )
121 value = (u16)port;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300122 byte &= 0xfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } else
124 byte |= 2;
125
126 _b0( value ) = byte;
127 outw( value, port );
128 byte |= 1;
129 _b0( value ) = byte;
130 outw( value, port );
131 byte &= 0xfe;
132 _b0( value ) = byte;
133 outw( value, port );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *last_byte = byte;
136}
137
138static inline void gemtek_pci_nil( u32 port, u8 *last_byte )
139{
Richard Knutssonfd4bc442007-02-06 21:55:07 -0300140 __gemtek_pci_cmd( 0x00, port, last_byte, false );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static inline void gemtek_pci_cmd( u16 cmd, u32 port, u8 *last_byte )
144{
Richard Knutssonfd4bc442007-02-06 21:55:07 -0300145 __gemtek_pci_cmd( cmd, port, last_byte, true );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148static void gemtek_pci_setfrequency( struct gemtek_pci_card *card, unsigned long frequency )
149{
150 register int i;
151 register u32 value = frequency / 200 + 856;
152 register u16 mask = 0x8000;
153 u8 last_byte;
154 u32 port = card->iobase;
155
156 last_byte = gemtek_pci_out( 0x06, port );
157
158 i = 0;
159 do {
160 gemtek_pci_nil( port, &last_byte );
161 i++;
162 } while ( i < 9 );
163
164 i = 0;
165 do {
166 gemtek_pci_cmd( value & mask, port, &last_byte );
167 mask >>= 1;
168 i++;
169 } while ( i < 16 );
170
171 outw( 0x10, port );
172}
173
174
175static inline void gemtek_pci_mute( struct gemtek_pci_card *card )
176{
177 outb( 0x1f, card->iobase );
Richard Knutssonfd4bc442007-02-06 21:55:07 -0300178 card->mute = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181static inline void gemtek_pci_unmute( struct gemtek_pci_card *card )
182{
183 if ( card->mute ) {
184 gemtek_pci_setfrequency( card, card->current_frequency );
Richard Knutssonfd4bc442007-02-06 21:55:07 -0300185 card->mute = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187}
188
189static inline unsigned int gemtek_pci_getsignal( struct gemtek_pci_card *card )
190{
191 return ( inb( card->iobase ) & 0x08 ) ? 0 : 1;
192}
193
Douglas Landgraf6f664462007-04-26 10:42:12 -0300194static int vidioc_querycap(struct file *file, void *priv,
195 struct v4l2_capability *v)
196{
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;
202 return 0;
203}
204
205static int vidioc_g_tuner(struct file *file, void *priv,
206 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct video_device *dev = video_devdata(file);
209 struct gemtek_pci_card *card = dev->priv;
210
Douglas Landgraf6f664462007-04-26 10:42:12 -0300211 if (v->index > 0)
212 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Douglas Landgraf6f664462007-04-26 10:42:12 -0300214 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);
222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Douglas Landgraf6f664462007-04-26 10:42:12 -0300225static int vidioc_s_tuner(struct file *file, void *priv,
226 struct v4l2_tuner *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Douglas Landgraf6f664462007-04-26 10:42:12 -0300228 if (v->index > 0)
229 return -EINVAL;
230 return 0;
231}
232
233static int vidioc_s_frequency(struct file *file, void *priv,
234 struct v4l2_frequency *f)
235{
236 struct video_device *dev = video_devdata(file);
237 struct gemtek_pci_card *card = dev->priv;
238
239 if ( (f->frequency < GEMTEK_PCI_RANGE_LOW) ||
240 (f->frequency > GEMTEK_PCI_RANGE_HIGH) )
241 return -EINVAL;
242 gemtek_pci_setfrequency(card, f->frequency);
243 card->current_frequency = f->frequency;
244 card->mute = false;
245 return 0;
246}
247
248static int vidioc_g_frequency(struct file *file, void *priv,
249 struct v4l2_frequency *f)
250{
251 struct video_device *dev = video_devdata(file);
252 struct gemtek_pci_card *card = dev->priv;
253
254 f->type = V4L2_TUNER_RADIO;
255 f->frequency = card->current_frequency;
256 return 0;
257}
258
259static int vidioc_queryctrl(struct file *file, void *priv,
260 struct v4l2_queryctrl *qc)
261{
262 int i;
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]),
266 sizeof(*qc));
267 return 0;
268 }
269 }
270 return -EINVAL;
271}
272
273static int vidioc_g_ctrl(struct file *file, void *priv,
274 struct v4l2_control *ctrl)
275{
276 struct video_device *dev = video_devdata(file);
277 struct gemtek_pci_card *card = dev->priv;
278
279 switch (ctrl->id) {
280 case V4L2_CID_AUDIO_MUTE:
281 ctrl->value = card->mute;
282 return 0;
283 case V4L2_CID_AUDIO_VOLUME:
284 if (card->mute)
285 ctrl->value = 0;
286 else
287 ctrl->value = 65535;
288 return 0;
289 }
290 return -EINVAL;
291}
292
293static int vidioc_s_ctrl(struct file *file, void *priv,
294 struct v4l2_control *ctrl)
295{
296 struct video_device *dev = video_devdata(file);
297 struct gemtek_pci_card *card = dev->priv;
298
299 switch (ctrl->id) {
300 case V4L2_CID_AUDIO_MUTE:
301 if (ctrl->value)
302 gemtek_pci_mute(card);
303 else
304 gemtek_pci_unmute(card);
305 return 0;
306 case V4L2_CID_AUDIO_VOLUME:
307 if (ctrl->value)
308 gemtek_pci_unmute(card);
309 else
310 gemtek_pci_mute(card);
311 return 0;
312 }
313 return -EINVAL;
314}
315
316static int vidioc_g_audio(struct file *file, void *priv,
317 struct v4l2_audio *a)
318{
319 if (a->index > 1)
320 return -EINVAL;
321
322 strcpy(a->name, "Radio");
323 a->capability = V4L2_AUDCAP_STEREO;
324 return 0;
325}
326
327static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
328{
329 *i = 0;
330 return 0;
331}
332
333static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
334{
335 if (i != 0)
336 return -EINVAL;
337 return 0;
338}
339
340static int vidioc_s_audio(struct file *file, void *priv,
341 struct v4l2_audio *a)
342{
343 if (a->index != 0)
344 return -EINVAL;
345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348enum {
349 GEMTEK_PR103
350};
351
352static char *card_names[] __devinitdata = {
353 "GEMTEK_PR103"
354};
355
356static struct pci_device_id gemtek_pci_id[] =
357{
358 { PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
359 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
360 { 0 }
361};
362
363MODULE_DEVICE_TABLE( pci, gemtek_pci_id );
364
365static int mx = 1;
366
Arjan van de Venfa027c22007-02-12 00:55:33 -0800367static const struct file_operations gemtek_pci_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 .owner = THIS_MODULE,
369 .open = video_exclusive_open,
370 .release = video_exclusive_release,
Douglas Landgraf6f664462007-04-26 10:42:12 -0300371 .ioctl = video_ioctl2,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300372#ifdef CONFIG_COMPAT
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200373 .compat_ioctl = v4l_compat_ioctl32,
Douglas Schilling Landgraf078ff792008-04-22 14:46:11 -0300374#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 .llseek = no_llseek,
376};
377
378static struct video_device vdev_template = {
379 .owner = THIS_MODULE,
380 .name = "Gemtek PCI Radio",
381 .type = VID_TYPE_TUNER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 .fops = &gemtek_pci_fops,
Douglas Landgraf6f664462007-04-26 10:42:12 -0300383 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
397static int __devinit gemtek_pci_probe( struct pci_dev *pci_dev, const struct pci_device_id *pci_id )
398{
399 struct gemtek_pci_card *card;
400 struct video_device *devradio;
401
Panagiotis Issaris74081872006-01-11 19:40:56 -0200402 if ( (card = kzalloc( sizeof( struct gemtek_pci_card ), GFP_KERNEL )) == NULL ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 printk( KERN_ERR "gemtek_pci: out of memory\n" );
404 return -ENOMEM;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300407 if ( pci_enable_device( pci_dev ) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto err_pci;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 card->iobase = pci_resource_start( pci_dev, 0 );
411 card->length = pci_resource_len( pci_dev, 0 );
412
413 if ( request_region( card->iobase, card->length, card_names[pci_id->driver_data] ) == NULL ) {
414 printk( KERN_ERR "gemtek_pci: i/o port already in use\n" );
415 goto err_pci;
416 }
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 pci_set_drvdata( pci_dev, card );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if ( (devradio = kmalloc( sizeof( struct video_device ), GFP_KERNEL )) == NULL ) {
421 printk( KERN_ERR "gemtek_pci: out of memory\n" );
422 goto err_video;
423 }
424 *devradio = vdev_template;
425
426 if ( video_register_device( devradio, VFL_TYPE_RADIO , nr_radio) == -1 ) {
427 kfree( devradio );
428 goto err_video;
429 }
430
431 card->videodev = devradio;
432 devradio->priv = card;
433 gemtek_pci_mute( card );
434
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300435 printk( KERN_INFO "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
Auke Kok44c10132007-06-08 15:46:36 -0700436 pci_dev->revision, card->iobase, card->iobase + card->length - 1 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 return 0;
439
440err_video:
441 release_region( card->iobase, card->length );
442
443err_pci:
444 kfree( card );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300445 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448static void __devexit gemtek_pci_remove( struct pci_dev *pci_dev )
449{
450 struct gemtek_pci_card *card = pci_get_drvdata( pci_dev );
451
452 video_unregister_device( card->videodev );
453 kfree( card->videodev );
454
455 release_region( card->iobase, card->length );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if ( mx )
458 gemtek_pci_mute( card );
459
460 kfree( card );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 pci_set_drvdata( pci_dev, NULL );
463}
464
465static struct pci_driver gemtek_pci_driver =
466{
467 .name = "gemtek_pci",
468 .id_table = gemtek_pci_id,
469 .probe = gemtek_pci_probe,
470 .remove = __devexit_p(gemtek_pci_remove),
471};
472
473static int __init gemtek_pci_init_module( void )
474{
Richard Knutsson9bfab8c2005-11-30 00:59:34 +0100475 return pci_register_driver( &gemtek_pci_driver );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478static void __exit gemtek_pci_cleanup_module( void )
479{
Tobias Klauser13962752006-10-04 08:09:10 -0300480 pci_unregister_driver(&gemtek_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483MODULE_AUTHOR( "Vladimir Shebordaev <vshebordaev@mail.ru>" );
484MODULE_DESCRIPTION( "The video4linux driver for the Gemtek PCI Radio Card" );
485MODULE_LICENSE("GPL");
486
487module_param(mx, bool, 0);
488MODULE_PARM_DESC( mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not" );
489module_param(nr_radio, int, 0);
490MODULE_PARM_DESC( nr_radio, "video4linux device number to use");
491
492module_init( gemtek_pci_init_module );
493module_exit( gemtek_pci_cleanup_module );
494