blob: 9f249e7e60c9b16d2a0d9b4081d18ea28a478d20 [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 *
37 ***************************************************************************
38 */
39
40#include <linux/config.h>
41#include <linux/types.h>
42#include <linux/list.h>
43#include <linux/module.h>
44#include <linux/init.h>
45#include <linux/pci.h>
46#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030047#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/errno.h>
49
50#include <asm/io.h>
51#include <asm/uaccess.h>
52
53#ifndef PCI_VENDOR_ID_GEMTEK
54#define PCI_VENDOR_ID_GEMTEK 0x5046
55#endif
56
57#ifndef PCI_DEVICE_ID_GEMTEK_PR103
58#define PCI_DEVICE_ID_GEMTEK_PR103 0x1001
59#endif
60
61#ifndef GEMTEK_PCI_RANGE_LOW
62#define GEMTEK_PCI_RANGE_LOW (87*16000)
63#endif
64
65#ifndef GEMTEK_PCI_RANGE_HIGH
66#define GEMTEK_PCI_RANGE_HIGH (108*16000)
67#endif
68
69#ifndef TRUE
70#define TRUE (1)
71#endif
72
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030073#ifndef FALSE
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define FALSE (0)
75#endif
76
77struct gemtek_pci_card {
78 struct video_device *videodev;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 u32 iobase;
81 u32 length;
82 u8 chiprev;
83 u16 model;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 u32 current_frequency;
86 u8 mute;
87};
88
89static const char rcsid[] = "$Id: radio-gemtek-pci.c,v 1.1 2001/07/23 08:08:16 ted Exp ted $";
90
91static int nr_radio = -1;
92
93static inline u8 gemtek_pci_out( u16 value, u32 port )
94{
95 outw( value, port );
96
97 return (u8)value;
98}
99
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300100#define _b0( v ) *((u8 *)&v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static void __gemtek_pci_cmd( u16 value, u32 port, u8 *last_byte, int keep )
102{
103 register u8 byte = *last_byte;
104
105 if ( !value ) {
106 if ( !keep )
107 value = (u16)port;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300108 byte &= 0xfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 } else
110 byte |= 2;
111
112 _b0( value ) = byte;
113 outw( value, port );
114 byte |= 1;
115 _b0( value ) = byte;
116 outw( value, port );
117 byte &= 0xfe;
118 _b0( value ) = byte;
119 outw( value, port );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *last_byte = byte;
122}
123
124static inline void gemtek_pci_nil( u32 port, u8 *last_byte )
125{
126 __gemtek_pci_cmd( 0x00, port, last_byte, FALSE );
127}
128
129static inline void gemtek_pci_cmd( u16 cmd, u32 port, u8 *last_byte )
130{
131 __gemtek_pci_cmd( cmd, port, last_byte, TRUE );
132}
133
134static void gemtek_pci_setfrequency( struct gemtek_pci_card *card, unsigned long frequency )
135{
136 register int i;
137 register u32 value = frequency / 200 + 856;
138 register u16 mask = 0x8000;
139 u8 last_byte;
140 u32 port = card->iobase;
141
142 last_byte = gemtek_pci_out( 0x06, port );
143
144 i = 0;
145 do {
146 gemtek_pci_nil( port, &last_byte );
147 i++;
148 } while ( i < 9 );
149
150 i = 0;
151 do {
152 gemtek_pci_cmd( value & mask, port, &last_byte );
153 mask >>= 1;
154 i++;
155 } while ( i < 16 );
156
157 outw( 0x10, port );
158}
159
160
161static inline void gemtek_pci_mute( struct gemtek_pci_card *card )
162{
163 outb( 0x1f, card->iobase );
164 card->mute = TRUE;
165}
166
167static inline void gemtek_pci_unmute( struct gemtek_pci_card *card )
168{
169 if ( card->mute ) {
170 gemtek_pci_setfrequency( card, card->current_frequency );
171 card->mute = FALSE;
172 }
173}
174
175static inline unsigned int gemtek_pci_getsignal( struct gemtek_pci_card *card )
176{
177 return ( inb( card->iobase ) & 0x08 ) ? 0 : 1;
178}
179
180static int gemtek_pci_do_ioctl(struct inode *inode, struct file *file,
181 unsigned int cmd, void *arg)
182{
183 struct video_device *dev = video_devdata(file);
184 struct gemtek_pci_card *card = dev->priv;
185
186 switch ( cmd ) {
187 case VIDIOCGCAP:
188 {
189 struct video_capability *c = arg;
190
191 memset(c,0,sizeof(*c));
192 c->type = VID_TYPE_TUNER;
193 c->channels = 1;
194 c->audios = 1;
195 strcpy( c->name, "Gemtek PCI Radio" );
196 return 0;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 case VIDIOCGTUNER:
200 {
201 struct video_tuner *t = arg;
202
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300203 if ( t->tuner )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EINVAL;
205
206 t->rangelow = GEMTEK_PCI_RANGE_LOW;
207 t->rangehigh = GEMTEK_PCI_RANGE_HIGH;
208 t->flags = VIDEO_TUNER_LOW;
209 t->mode = VIDEO_MODE_AUTO;
210 t->signal = 0xFFFF * gemtek_pci_getsignal( card );
211 strcpy( t->name, "FM" );
212 return 0;
213 }
214
215 case VIDIOCSTUNER:
216 {
217 struct video_tuner *t = arg;
218 if ( t->tuner )
219 return -EINVAL;
220 return 0;
221 }
222
223 case VIDIOCGFREQ:
224 {
225 unsigned long *freq = arg;
226 *freq = card->current_frequency;
227 return 0;
228 }
229 case VIDIOCSFREQ:
230 {
231 unsigned long *freq = arg;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if ( (*freq < GEMTEK_PCI_RANGE_LOW) ||
234 (*freq > GEMTEK_PCI_RANGE_HIGH) )
235 return -EINVAL;
236
237 gemtek_pci_setfrequency( card, *freq );
238 card->current_frequency = *freq;
239 card->mute = FALSE;
240
241 return 0;
242 }
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 case VIDIOCGAUDIO:
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300245 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct video_audio *a = arg;
247
248 memset( a, 0, sizeof( *a ) );
249 a->flags |= VIDEO_AUDIO_MUTABLE;
250 a->volume = 1;
251 a->step = 65535;
252 strcpy( a->name, "Radio" );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
256 case VIDIOCSAUDIO:
257 {
258 struct video_audio *a = arg;
259
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300260 if ( a->audio )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -EINVAL;
262
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300263 if ( a->flags & VIDEO_AUDIO_MUTE )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 gemtek_pci_mute( card );
265 else
266 gemtek_pci_unmute( card );
267 return 0;
268 }
269
270 default:
271 return -ENOIOCTLCMD;
272 }
273}
274
275static int gemtek_pci_ioctl(struct inode *inode, struct file *file,
276 unsigned int cmd, unsigned long arg)
277{
278 return video_usercopy(inode, file, cmd, arg, gemtek_pci_do_ioctl);
279}
280
281enum {
282 GEMTEK_PR103
283};
284
285static char *card_names[] __devinitdata = {
286 "GEMTEK_PR103"
287};
288
289static struct pci_device_id gemtek_pci_id[] =
290{
291 { PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
292 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
293 { 0 }
294};
295
296MODULE_DEVICE_TABLE( pci, gemtek_pci_id );
297
298static int mx = 1;
299
300static struct file_operations gemtek_pci_fops = {
301 .owner = THIS_MODULE,
302 .open = video_exclusive_open,
303 .release = video_exclusive_release,
304 .ioctl = gemtek_pci_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200305 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .llseek = no_llseek,
307};
308
309static struct video_device vdev_template = {
310 .owner = THIS_MODULE,
311 .name = "Gemtek PCI Radio",
312 .type = VID_TYPE_TUNER,
313 .hardware = VID_HARDWARE_GEMTEK,
314 .fops = &gemtek_pci_fops,
315};
316
317static int __devinit gemtek_pci_probe( struct pci_dev *pci_dev, const struct pci_device_id *pci_id )
318{
319 struct gemtek_pci_card *card;
320 struct video_device *devradio;
321
Panagiotis Issaris74081872006-01-11 19:40:56 -0200322 if ( (card = kzalloc( sizeof( struct gemtek_pci_card ), GFP_KERNEL )) == NULL ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 printk( KERN_ERR "gemtek_pci: out of memory\n" );
324 return -ENOMEM;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300327 if ( pci_enable_device( pci_dev ) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 goto err_pci;
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 card->iobase = pci_resource_start( pci_dev, 0 );
331 card->length = pci_resource_len( pci_dev, 0 );
332
333 if ( request_region( card->iobase, card->length, card_names[pci_id->driver_data] ) == NULL ) {
334 printk( KERN_ERR "gemtek_pci: i/o port already in use\n" );
335 goto err_pci;
336 }
337
338 pci_read_config_byte( pci_dev, PCI_REVISION_ID, &card->chiprev );
339 pci_read_config_word( pci_dev, PCI_SUBSYSTEM_ID, &card->model );
340
341 pci_set_drvdata( pci_dev, card );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if ( (devradio = kmalloc( sizeof( struct video_device ), GFP_KERNEL )) == NULL ) {
344 printk( KERN_ERR "gemtek_pci: out of memory\n" );
345 goto err_video;
346 }
347 *devradio = vdev_template;
348
349 if ( video_register_device( devradio, VFL_TYPE_RADIO , nr_radio) == -1 ) {
350 kfree( devradio );
351 goto err_video;
352 }
353
354 card->videodev = devradio;
355 devradio->priv = card;
356 gemtek_pci_mute( card );
357
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300358 printk( KERN_INFO "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 card->chiprev, card->iobase, card->iobase + card->length - 1 );
360
361 return 0;
362
363err_video:
364 release_region( card->iobase, card->length );
365
366err_pci:
367 kfree( card );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300368 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371static void __devexit gemtek_pci_remove( struct pci_dev *pci_dev )
372{
373 struct gemtek_pci_card *card = pci_get_drvdata( pci_dev );
374
375 video_unregister_device( card->videodev );
376 kfree( card->videodev );
377
378 release_region( card->iobase, card->length );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if ( mx )
381 gemtek_pci_mute( card );
382
383 kfree( card );
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 pci_set_drvdata( pci_dev, NULL );
386}
387
388static struct pci_driver gemtek_pci_driver =
389{
390 .name = "gemtek_pci",
391 .id_table = gemtek_pci_id,
392 .probe = gemtek_pci_probe,
393 .remove = __devexit_p(gemtek_pci_remove),
394};
395
396static int __init gemtek_pci_init_module( void )
397{
Richard Knutsson9bfab8c2005-11-30 00:59:34 +0100398 return pci_register_driver( &gemtek_pci_driver );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401static void __exit gemtek_pci_cleanup_module( void )
402{
403 return pci_unregister_driver( &gemtek_pci_driver );
404}
405
406MODULE_AUTHOR( "Vladimir Shebordaev <vshebordaev@mail.ru>" );
407MODULE_DESCRIPTION( "The video4linux driver for the Gemtek PCI Radio Card" );
408MODULE_LICENSE("GPL");
409
410module_param(mx, bool, 0);
411MODULE_PARM_DESC( mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not" );
412module_param(nr_radio, int, 0);
413MODULE_PARM_DESC( nr_radio, "video4linux device number to use");
414
415module_init( gemtek_pci_init_module );
416module_exit( gemtek_pci_cleanup_module );
417