blob: 74976cba869f521ae0d558b136f1979a991d46c0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/errno.h>
50
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -030051#include <linux/version.h> /* for KERNEL_VERSION MACRO */
52#define RADIO_VERSION KERNEL_VERSION(0,0,2)
53
54static struct v4l2_queryctrl radio_qctrl[] = {
55 {
56 .id = V4L2_CID_AUDIO_MUTE,
57 .name = "Mute",
58 .minimum = 0,
59 .maximum = 1,
60 .default_value = 1,
61 .type = V4L2_CTRL_TYPE_BOOLEAN,
62 },{
63 .id = V4L2_CID_AUDIO_VOLUME,
64 .name = "Volume",
65 .minimum = 0,
66 .maximum = 65535,
67 .step = 65535,
68 .default_value = 0xff,
69 .type = V4L2_CTRL_TYPE_INTEGER,
70 }
71};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <asm/io.h>
74#include <asm/uaccess.h>
75
76#ifndef PCI_VENDOR_ID_GEMTEK
77#define PCI_VENDOR_ID_GEMTEK 0x5046
78#endif
79
80#ifndef PCI_DEVICE_ID_GEMTEK_PR103
81#define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
82#endif
83
84#ifndef GEMTEK_PCI_RANGE_LOW
85#define GEMTEK_PCI_RANGE_LOW (87*16000)
86#endif
87
88#ifndef GEMTEK_PCI_RANGE_HIGH
89#define GEMTEK_PCI_RANGE_HIGH (108*16000)
90#endif
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct gemtek_pci_card {
93 struct video_device *videodev;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 u32 iobase;
96 u32 length;
97 u8 chiprev;
98 u16 model;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 u32 current_frequency;
101 u8 mute;
102};
103
104static const char rcsid[] = "$Id: radio-gemtek-pci.c,v 1.1 2001/07/23 08:08:16 ted Exp ted $";
105
106static int nr_radio = -1;
107
108static inline u8 gemtek_pci_out( u16 value, u32 port )
109{
110 outw( value, port );
111
112 return (u8)value;
113}
114
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300115#define _b0( v ) *((u8 *)&v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static void __gemtek_pci_cmd( u16 value, u32 port, u8 *last_byte, int keep )
117{
118 register u8 byte = *last_byte;
119
120 if ( !value ) {
121 if ( !keep )
122 value = (u16)port;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300123 byte &= 0xfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 } else
125 byte |= 2;
126
127 _b0( value ) = byte;
128 outw( value, port );
129 byte |= 1;
130 _b0( value ) = byte;
131 outw( value, port );
132 byte &= 0xfe;
133 _b0( value ) = byte;
134 outw( value, port );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *last_byte = byte;
137}
138
139static inline void gemtek_pci_nil( u32 port, u8 *last_byte )
140{
Richard Knutssonfd4bc442007-02-06 21:55:07 -0300141 __gemtek_pci_cmd( 0x00, port, last_byte, false );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static inline void gemtek_pci_cmd( u16 cmd, u32 port, u8 *last_byte )
145{
Richard Knutssonfd4bc442007-02-06 21:55:07 -0300146 __gemtek_pci_cmd( cmd, port, last_byte, true );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static void gemtek_pci_setfrequency( struct gemtek_pci_card *card, unsigned long frequency )
150{
151 register int i;
152 register u32 value = frequency / 200 + 856;
153 register u16 mask = 0x8000;
154 u8 last_byte;
155 u32 port = card->iobase;
156
157 last_byte = gemtek_pci_out( 0x06, port );
158
159 i = 0;
160 do {
161 gemtek_pci_nil( port, &last_byte );
162 i++;
163 } while ( i < 9 );
164
165 i = 0;
166 do {
167 gemtek_pci_cmd( value & mask, port, &last_byte );
168 mask >>= 1;
169 i++;
170 } while ( i < 16 );
171
172 outw( 0x10, port );
173}
174
175
176static inline void gemtek_pci_mute( struct gemtek_pci_card *card )
177{
178 outb( 0x1f, card->iobase );
Richard Knutssonfd4bc442007-02-06 21:55:07 -0300179 card->mute = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182static inline void gemtek_pci_unmute( struct gemtek_pci_card *card )
183{
184 if ( card->mute ) {
185 gemtek_pci_setfrequency( card, card->current_frequency );
Richard Knutssonfd4bc442007-02-06 21:55:07 -0300186 card->mute = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188}
189
190static inline unsigned int gemtek_pci_getsignal( struct gemtek_pci_card *card )
191{
192 return ( inb( card->iobase ) & 0x08 ) ? 0 : 1;
193}
194
195static int gemtek_pci_do_ioctl(struct inode *inode, struct file *file,
196 unsigned int cmd, void *arg)
197{
198 struct video_device *dev = video_devdata(file);
199 struct gemtek_pci_card *card = dev->priv;
200
201 switch ( cmd ) {
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300202 case VIDIOC_QUERYCAP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 {
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300204 struct v4l2_capability *v = arg;
205 memset(v,0,sizeof(*v));
206 strlcpy(v->driver, "radio-gemtek-pci", sizeof (v->driver));
207 strlcpy(v->card, "GemTek PCI Radio", sizeof (v->card));
208 sprintf(v->bus_info,"ISA");
209 v->version = RADIO_VERSION;
210 v->capabilities = V4L2_CAP_TUNER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300213 }
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300214 case VIDIOC_G_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 {
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300216 struct v4l2_tuner *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300218 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -EINVAL;
220
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300221 memset(v,0,sizeof(*v));
222 strcpy(v->name, "FM");
223 v->type = V4L2_TUNER_RADIO;
224
225 v->rangelow = GEMTEK_PCI_RANGE_LOW;
226 v->rangehigh = GEMTEK_PCI_RANGE_HIGH;
227 v->rxsubchans =V4L2_TUNER_SUB_MONO;
228 v->capability=V4L2_TUNER_CAP_LOW;
229 v->audmode = V4L2_TUNER_MODE_MONO;
230 v->signal=0xFFFF*gemtek_pci_getsignal( card );
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233 }
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300234 case VIDIOC_S_TUNER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 {
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300236 struct v4l2_tuner *v = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300238 if (v->index > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -EINVAL;
240
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300241 return 0;
242 }
243 case VIDIOC_S_FREQUENCY:
244 {
245 struct v4l2_frequency *f = arg;
246
247 if ( (f->frequency < GEMTEK_PCI_RANGE_LOW) ||
248 (f->frequency > GEMTEK_PCI_RANGE_HIGH) )
249 return -EINVAL;
250
251
252 gemtek_pci_setfrequency( card, f->frequency );
253 card->current_frequency = f->frequency;
Richard Knutssonfd4bc442007-02-06 21:55:07 -0300254 card->mute = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
256 }
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300257 case VIDIOC_QUERYCTRL:
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300258 {
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300259 struct v4l2_queryctrl *qc = arg;
260 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300262 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
263 if (qc->id && qc->id == radio_qctrl[i].id) {
264 memcpy(qc, &(radio_qctrl[i]),
265 sizeof(*qc));
266 return (0);
267 }
268 }
269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300271 case VIDIOC_G_CTRL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 {
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300273 struct v4l2_control *ctrl= arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300275 switch (ctrl->id) {
276 case V4L2_CID_AUDIO_MUTE:
277 ctrl->value=card->mute;
278 return (0);
279 case V4L2_CID_AUDIO_VOLUME:
280 if (card->mute)
281 ctrl->value=0;
282 else
283 ctrl->value=65535;
284 return (0);
285 }
286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300288 case VIDIOC_S_CTRL:
289 {
290 struct v4l2_control *ctrl= arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300292 switch (ctrl->id) {
293 case V4L2_CID_AUDIO_MUTE:
294 if (ctrl->value) {
295 gemtek_pci_mute(card);
296 } else {
297 gemtek_pci_unmute(card);
298 }
299 return (0);
300 case V4L2_CID_AUDIO_VOLUME:
301 if (ctrl->value) {
302 gemtek_pci_unmute(card);
303 } else {
304 gemtek_pci_mute(card);
305 }
306 return (0);
307 }
308 return -EINVAL;
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 default:
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300311 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
Mauro Carvalho Chehab3ca57162006-08-08 09:10:06 -0300312 gemtek_pci_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314}
315
316static int gemtek_pci_ioctl(struct inode *inode, struct file *file,
317 unsigned int cmd, unsigned long arg)
318{
319 return video_usercopy(inode, file, cmd, arg, gemtek_pci_do_ioctl);
320}
321
322enum {
323 GEMTEK_PR103
324};
325
326static char *card_names[] __devinitdata = {
327 "GEMTEK_PR103"
328};
329
330static struct pci_device_id gemtek_pci_id[] =
331{
332 { PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
333 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
334 { 0 }
335};
336
337MODULE_DEVICE_TABLE( pci, gemtek_pci_id );
338
339static int mx = 1;
340
Arjan van de Venfa027c22007-02-12 00:55:33 -0800341static const struct file_operations gemtek_pci_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 .owner = THIS_MODULE,
343 .open = video_exclusive_open,
344 .release = video_exclusive_release,
345 .ioctl = gemtek_pci_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200346 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 .llseek = no_llseek,
348};
349
350static struct video_device vdev_template = {
351 .owner = THIS_MODULE,
352 .name = "Gemtek PCI Radio",
353 .type = VID_TYPE_TUNER,
Mauro Carvalho Chehab52afbc22006-08-08 09:10:01 -0300354 .hardware = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 .fops = &gemtek_pci_fops,
356};
357
358static int __devinit gemtek_pci_probe( struct pci_dev *pci_dev, const struct pci_device_id *pci_id )
359{
360 struct gemtek_pci_card *card;
361 struct video_device *devradio;
362
Panagiotis Issaris74081872006-01-11 19:40:56 -0200363 if ( (card = kzalloc( sizeof( struct gemtek_pci_card ), GFP_KERNEL )) == NULL ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 printk( KERN_ERR "gemtek_pci: out of memory\n" );
365 return -ENOMEM;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300368 if ( pci_enable_device( pci_dev ) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto err_pci;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 card->iobase = pci_resource_start( pci_dev, 0 );
372 card->length = pci_resource_len( pci_dev, 0 );
373
374 if ( request_region( card->iobase, card->length, card_names[pci_id->driver_data] ) == NULL ) {
375 printk( KERN_ERR "gemtek_pci: i/o port already in use\n" );
376 goto err_pci;
377 }
378
379 pci_read_config_byte( pci_dev, PCI_REVISION_ID, &card->chiprev );
380 pci_read_config_word( pci_dev, PCI_SUBSYSTEM_ID, &card->model );
381
382 pci_set_drvdata( pci_dev, card );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if ( (devradio = kmalloc( sizeof( struct video_device ), GFP_KERNEL )) == NULL ) {
385 printk( KERN_ERR "gemtek_pci: out of memory\n" );
386 goto err_video;
387 }
388 *devradio = vdev_template;
389
390 if ( video_register_device( devradio, VFL_TYPE_RADIO , nr_radio) == -1 ) {
391 kfree( devradio );
392 goto err_video;
393 }
394
395 card->videodev = devradio;
396 devradio->priv = card;
397 gemtek_pci_mute( card );
398
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300399 printk( KERN_INFO "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 card->chiprev, card->iobase, card->iobase + card->length - 1 );
401
402 return 0;
403
404err_video:
405 release_region( card->iobase, card->length );
406
407err_pci:
408 kfree( card );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300409 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412static void __devexit gemtek_pci_remove( struct pci_dev *pci_dev )
413{
414 struct gemtek_pci_card *card = pci_get_drvdata( pci_dev );
415
416 video_unregister_device( card->videodev );
417 kfree( card->videodev );
418
419 release_region( card->iobase, card->length );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if ( mx )
422 gemtek_pci_mute( card );
423
424 kfree( card );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 pci_set_drvdata( pci_dev, NULL );
427}
428
429static struct pci_driver gemtek_pci_driver =
430{
431 .name = "gemtek_pci",
432 .id_table = gemtek_pci_id,
433 .probe = gemtek_pci_probe,
434 .remove = __devexit_p(gemtek_pci_remove),
435};
436
437static int __init gemtek_pci_init_module( void )
438{
Richard Knutsson9bfab8c2005-11-30 00:59:34 +0100439 return pci_register_driver( &gemtek_pci_driver );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442static void __exit gemtek_pci_cleanup_module( void )
443{
Tobias Klauser13962752006-10-04 08:09:10 -0300444 pci_unregister_driver(&gemtek_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447MODULE_AUTHOR( "Vladimir Shebordaev <vshebordaev@mail.ru>" );
448MODULE_DESCRIPTION( "The video4linux driver for the Gemtek PCI Radio Card" );
449MODULE_LICENSE("GPL");
450
451module_param(mx, bool, 0);
452MODULE_PARM_DESC( mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not" );
453module_param(nr_radio, int, 0);
454MODULE_PARM_DESC( nr_radio, "video4linux device number to use");
455
456module_init( gemtek_pci_init_module );
457module_exit( gemtek_pci_cleanup_module );
458