Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[linux-2.6] / drivers / media / radio / radio-gemtek-pci.c
1 /*
2  ***************************************************************************
3  *
4  *     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 :)
34  *
35  *     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>
47 #include <media/v4l2-common.h>
48 #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
73 #ifndef FALSE
74 #define FALSE (0)
75 #endif
76
77 struct gemtek_pci_card {
78         struct video_device *videodev;
79
80         u32 iobase;
81         u32 length;
82         u8  chiprev;
83         u16 model;
84
85         u32 current_frequency;
86         u8  mute;
87 };
88
89 static const char rcsid[] = "$Id: radio-gemtek-pci.c,v 1.1 2001/07/23 08:08:16 ted Exp ted $";
90
91 static int nr_radio = -1;
92
93 static inline u8 gemtek_pci_out( u16 value, u32 port )
94 {
95         outw( value, port );
96
97         return (u8)value;
98 }
99
100 #define _b0( v ) *((u8 *)&v)
101 static 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;
108                 byte &= 0xfd;
109         } 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 );
120
121         *last_byte = byte;
122 }
123
124 static inline void gemtek_pci_nil( u32 port, u8 *last_byte )
125 {
126         __gemtek_pci_cmd( 0x00, port, last_byte, FALSE );
127 }
128
129 static inline void gemtek_pci_cmd( u16 cmd, u32 port, u8 *last_byte )
130 {
131         __gemtek_pci_cmd( cmd, port, last_byte, TRUE );
132 }
133
134 static 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
161 static inline void gemtek_pci_mute( struct gemtek_pci_card *card )
162 {
163         outb( 0x1f, card->iobase );
164         card->mute = TRUE;
165 }
166
167 static 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
175 static inline unsigned int gemtek_pci_getsignal( struct gemtek_pci_card *card )
176 {
177         return ( inb( card->iobase ) & 0x08 ) ? 0 : 1;
178 }
179
180 static 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;
197                 }
198
199                 case VIDIOCGTUNER:
200                 {
201                         struct video_tuner *t = arg;
202
203                         if ( t->tuner )
204                                 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;
232
233                         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                 }
243
244                 case VIDIOCGAUDIO:
245                 {
246                         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" );
253                         return 0;
254                 }
255
256                 case VIDIOCSAUDIO:
257                 {
258                         struct video_audio *a = arg;
259
260                         if ( a->audio )
261                                 return -EINVAL;
262
263                         if ( a->flags & VIDEO_AUDIO_MUTE )
264                                 gemtek_pci_mute( card );
265                         else
266                                 gemtek_pci_unmute( card );
267                         return 0;
268                 }
269
270                 default:
271                         return -ENOIOCTLCMD;
272         }
273 }
274
275 static 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
281 enum {
282         GEMTEK_PR103
283 };
284
285 static char *card_names[] __devinitdata = {
286         "GEMTEK_PR103"
287 };
288
289 static 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
296 MODULE_DEVICE_TABLE( pci, gemtek_pci_id );
297
298 static int mx = 1;
299
300 static 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,
305         .compat_ioctl   = v4l_compat_ioctl32,
306         .llseek         = no_llseek,
307 };
308
309 static 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
317 static 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
322         if ( (card = kzalloc( sizeof( struct gemtek_pci_card ), GFP_KERNEL )) == NULL ) {
323                 printk( KERN_ERR "gemtek_pci: out of memory\n" );
324                 return -ENOMEM;
325         }
326
327         if ( pci_enable_device( pci_dev ) )
328                 goto err_pci;
329
330         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 );
342
343         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
358         printk( KERN_INFO "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
359                 card->chiprev, card->iobase, card->iobase + card->length - 1 );
360
361         return 0;
362
363 err_video:
364         release_region( card->iobase, card->length );
365
366 err_pci:
367         kfree( card );
368         return -ENODEV;
369 }
370
371 static 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 );
379
380         if ( mx )
381                 gemtek_pci_mute( card );
382
383         kfree( card );
384
385         pci_set_drvdata( pci_dev, NULL );
386 }
387
388 static 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
396 static int __init gemtek_pci_init_module( void )
397 {
398         return pci_register_driver( &gemtek_pci_driver );
399 }
400
401 static void __exit gemtek_pci_cleanup_module( void )
402 {
403         return pci_unregister_driver( &gemtek_pci_driver );
404 }
405
406 MODULE_AUTHOR( "Vladimir Shebordaev <vshebordaev@mail.ru>" );
407 MODULE_DESCRIPTION( "The video4linux driver for the Gemtek PCI Radio Card" );
408 MODULE_LICENSE("GPL");
409
410 module_param(mx, bool, 0);
411 MODULE_PARM_DESC( mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not" );
412 module_param(nr_radio, int, 0);
413 MODULE_PARM_DESC( nr_radio, "video4linux device number to use");
414
415 module_init( gemtek_pci_init_module );
416 module_exit( gemtek_pci_cleanup_module );
417