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