linux/drivers/media/radio/radio-gemtek-pci.c
<<
>>
Prefs
   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 *     Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  38 *
  39 ***************************************************************************
  40 */
  41
  42#include <linux/types.h>
  43#include <linux/list.h>
  44#include <linux/module.h>
  45#include <linux/init.h>
  46#include <linux/pci.h>
  47#include <linux/videodev2.h>
  48#include <media/v4l2-common.h>
  49#include <linux/errno.h>
  50
  51#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
  73#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
  92struct gemtek_pci_card {
  93        struct video_device *videodev;
  94
  95        u32 iobase;
  96        u32 length;
  97
  98        u32 current_frequency;
  99        u8  mute;
 100};
 101
 102static const char rcsid[] = "$Id: radio-gemtek-pci.c,v 1.1 2001/07/23 08:08:16 ted Exp ted $";
 103
 104static int nr_radio = -1;
 105
 106static inline u8 gemtek_pci_out( u16 value, u32 port )
 107{
 108        outw( value, port );
 109
 110        return (u8)value;
 111}
 112
 113#define _b0( v ) *((u8 *)&v)
 114static void __gemtek_pci_cmd( u16 value, u32 port, u8 *last_byte, int keep )
 115{
 116        register u8 byte = *last_byte;
 117
 118        if ( !value ) {
 119                if ( !keep )
 120                        value = (u16)port;
 121                byte &= 0xfd;
 122        } else
 123                byte |= 2;
 124
 125        _b0( value ) = byte;
 126        outw( value, port );
 127        byte |= 1;
 128        _b0( value ) = byte;
 129        outw( value, port );
 130        byte &= 0xfe;
 131        _b0( value ) = byte;
 132        outw( value, port );
 133
 134        *last_byte = byte;
 135}
 136
 137static inline void gemtek_pci_nil( u32 port, u8 *last_byte )
 138{
 139        __gemtek_pci_cmd( 0x00, port, last_byte, false );
 140}
 141
 142static inline void gemtek_pci_cmd( u16 cmd, u32 port, u8 *last_byte )
 143{
 144        __gemtek_pci_cmd( cmd, port, last_byte, true );
 145}
 146
 147static void gemtek_pci_setfrequency( struct gemtek_pci_card *card, unsigned long frequency )
 148{
 149        register int i;
 150        register u32 value = frequency / 200 + 856;
 151        register u16 mask = 0x8000;
 152        u8 last_byte;
 153        u32 port = card->iobase;
 154
 155        last_byte = gemtek_pci_out( 0x06, port );
 156
 157        i = 0;
 158        do {
 159                gemtek_pci_nil( port, &last_byte );
 160                i++;
 161        } while ( i < 9 );
 162
 163        i = 0;
 164        do {
 165                gemtek_pci_cmd( value & mask, port, &last_byte );
 166                mask >>= 1;
 167                i++;
 168        } while ( i < 16 );
 169
 170        outw( 0x10, port );
 171}
 172
 173
 174static inline void gemtek_pci_mute( struct gemtek_pci_card *card )
 175{
 176        outb( 0x1f, card->iobase );
 177        card->mute = true;
 178}
 179
 180static inline void gemtek_pci_unmute( struct gemtek_pci_card *card )
 181{
 182        if ( card->mute ) {
 183                gemtek_pci_setfrequency( card, card->current_frequency );
 184                card->mute = false;
 185        }
 186}
 187
 188static inline unsigned int gemtek_pci_getsignal( struct gemtek_pci_card *card )
 189{
 190        return ( inb( card->iobase ) & 0x08 ) ? 0 : 1;
 191}
 192
 193static int vidioc_querycap(struct file *file, void *priv,
 194                                        struct v4l2_capability *v)
 195{
 196        strlcpy(v->driver, "radio-gemtek-pci", sizeof(v->driver));
 197        strlcpy(v->card, "GemTek PCI Radio", sizeof(v->card));
 198        sprintf(v->bus_info, "ISA");
 199        v->version = RADIO_VERSION;
 200        v->capabilities = V4L2_CAP_TUNER;
 201        return 0;
 202}
 203
 204static int vidioc_g_tuner(struct file *file, void *priv,
 205                                        struct v4l2_tuner *v)
 206{
 207        struct video_device *dev = video_devdata(file);
 208        struct gemtek_pci_card *card = dev->priv;
 209
 210        if (v->index > 0)
 211                return -EINVAL;
 212
 213        strcpy(v->name, "FM");
 214        v->type = V4L2_TUNER_RADIO;
 215        v->rangelow = GEMTEK_PCI_RANGE_LOW;
 216        v->rangehigh = GEMTEK_PCI_RANGE_HIGH;
 217        v->rxsubchans = V4L2_TUNER_SUB_MONO;
 218        v->capability = V4L2_TUNER_CAP_LOW;
 219        v->audmode = V4L2_TUNER_MODE_MONO;
 220        v->signal = 0xffff * gemtek_pci_getsignal(card);
 221        return 0;
 222}
 223
 224static int vidioc_s_tuner(struct file *file, void *priv,
 225                                        struct v4l2_tuner *v)
 226{
 227        if (v->index > 0)
 228                return -EINVAL;
 229        return 0;
 230}
 231
 232static int vidioc_s_frequency(struct file *file, void *priv,
 233                                        struct v4l2_frequency *f)
 234{
 235        struct video_device *dev = video_devdata(file);
 236        struct gemtek_pci_card *card = dev->priv;
 237
 238        if ( (f->frequency < GEMTEK_PCI_RANGE_LOW) ||
 239                        (f->frequency > GEMTEK_PCI_RANGE_HIGH) )
 240                return -EINVAL;
 241        gemtek_pci_setfrequency(card, f->frequency);
 242        card->current_frequency = f->frequency;
 243        card->mute = false;
 244        return 0;
 245}
 246
 247static int vidioc_g_frequency(struct file *file, void *priv,
 248                                        struct v4l2_frequency *f)
 249{
 250        struct video_device *dev = video_devdata(file);
 251        struct gemtek_pci_card *card = dev->priv;
 252
 253        f->type = V4L2_TUNER_RADIO;
 254        f->frequency = card->current_frequency;
 255        return 0;
 256}
 257
 258static int vidioc_queryctrl(struct file *file, void *priv,
 259                                        struct v4l2_queryctrl *qc)
 260{
 261        int i;
 262        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;
 270}
 271
 272static int vidioc_g_ctrl(struct file *file, void *priv,
 273                                        struct v4l2_control *ctrl)
 274{
 275        struct video_device *dev = video_devdata(file);
 276        struct gemtek_pci_card *card = dev->priv;
 277
 278        switch (ctrl->id) {
 279        case V4L2_CID_AUDIO_MUTE:
 280                ctrl->value = card->mute;
 281                return 0;
 282        case V4L2_CID_AUDIO_VOLUME:
 283                if (card->mute)
 284                        ctrl->value = 0;
 285                else
 286                        ctrl->value = 65535;
 287                return 0;
 288        }
 289        return -EINVAL;
 290}
 291
 292static int vidioc_s_ctrl(struct file *file, void *priv,
 293                                        struct v4l2_control *ctrl)
 294{
 295        struct video_device *dev = video_devdata(file);
 296        struct gemtek_pci_card *card = dev->priv;
 297
 298        switch (ctrl->id) {
 299        case V4L2_CID_AUDIO_MUTE:
 300                if (ctrl->value)
 301                        gemtek_pci_mute(card);
 302                else
 303                        gemtek_pci_unmute(card);
 304                return 0;
 305        case V4L2_CID_AUDIO_VOLUME:
 306                if (ctrl->value)
 307                        gemtek_pci_unmute(card);
 308                else
 309                        gemtek_pci_mute(card);
 310                return 0;
 311        }
 312        return -EINVAL;
 313}
 314
 315static int vidioc_g_audio(struct file *file, void *priv,
 316                                        struct v4l2_audio *a)
 317{
 318        if (a->index > 1)
 319                return -EINVAL;
 320
 321        strcpy(a->name, "Radio");
 322        a->capability = V4L2_AUDCAP_STEREO;
 323        return 0;
 324}
 325
 326static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
 327{
 328        *i = 0;
 329        return 0;
 330}
 331
 332static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
 333{
 334        if (i != 0)
 335                return -EINVAL;
 336        return 0;
 337}
 338
 339static int vidioc_s_audio(struct file *file, void *priv,
 340                                        struct v4l2_audio *a)
 341{
 342        if (a->index != 0)
 343                return -EINVAL;
 344        return 0;
 345}
 346
 347enum {
 348        GEMTEK_PR103
 349};
 350
 351static char *card_names[] __devinitdata = {
 352        "GEMTEK_PR103"
 353};
 354
 355static struct pci_device_id gemtek_pci_id[] =
 356{
 357        { PCI_VENDOR_ID_GEMTEK, PCI_DEVICE_ID_GEMTEK_PR103,
 358          PCI_ANY_ID, PCI_ANY_ID, 0, 0, GEMTEK_PR103 },
 359        { 0 }
 360};
 361
 362MODULE_DEVICE_TABLE( pci, gemtek_pci_id );
 363
 364static int mx = 1;
 365
 366static const struct file_operations gemtek_pci_fops = {
 367        .owner          = THIS_MODULE,
 368        .open           = video_exclusive_open,
 369        .release        = video_exclusive_release,
 370        .ioctl          = video_ioctl2,
 371        .compat_ioctl   = v4l_compat_ioctl32,
 372        .llseek         = no_llseek,
 373};
 374
 375static struct video_device vdev_template = {
 376        .owner         = THIS_MODULE,
 377        .name          = "Gemtek PCI Radio",
 378        .type          = VID_TYPE_TUNER,
 379        .fops          = &gemtek_pci_fops,
 380        .vidioc_querycap    = vidioc_querycap,
 381        .vidioc_g_tuner     = vidioc_g_tuner,
 382        .vidioc_s_tuner     = vidioc_s_tuner,
 383        .vidioc_g_audio     = vidioc_g_audio,
 384        .vidioc_s_audio     = vidioc_s_audio,
 385        .vidioc_g_input     = vidioc_g_input,
 386        .vidioc_s_input     = vidioc_s_input,
 387        .vidioc_g_frequency = vidioc_g_frequency,
 388        .vidioc_s_frequency = vidioc_s_frequency,
 389        .vidioc_queryctrl   = vidioc_queryctrl,
 390        .vidioc_g_ctrl      = vidioc_g_ctrl,
 391        .vidioc_s_ctrl      = vidioc_s_ctrl,
 392};
 393
 394static int __devinit gemtek_pci_probe( struct pci_dev *pci_dev, const struct pci_device_id *pci_id )
 395{
 396        struct gemtek_pci_card *card;
 397        struct video_device *devradio;
 398
 399        if ( (card = kzalloc( sizeof( struct gemtek_pci_card ), GFP_KERNEL )) == NULL ) {
 400                printk( KERN_ERR "gemtek_pci: out of memory\n" );
 401                return -ENOMEM;
 402        }
 403
 404        if ( pci_enable_device( pci_dev ) )
 405                goto err_pci;
 406
 407        card->iobase = pci_resource_start( pci_dev, 0 );
 408        card->length = pci_resource_len( pci_dev, 0 );
 409
 410        if ( request_region( card->iobase, card->length, card_names[pci_id->driver_data] ) == NULL ) {
 411                printk( KERN_ERR "gemtek_pci: i/o port already in use\n" );
 412                goto err_pci;
 413        }
 414
 415        pci_set_drvdata( pci_dev, card );
 416
 417        if ( (devradio = kmalloc( sizeof( struct video_device ), GFP_KERNEL )) == NULL ) {
 418                printk( KERN_ERR "gemtek_pci: out of memory\n" );
 419                goto err_video;
 420        }
 421        *devradio = vdev_template;
 422
 423        if ( video_register_device( devradio, VFL_TYPE_RADIO , nr_radio) == -1 ) {
 424                kfree( devradio );
 425                goto err_video;
 426        }
 427
 428        card->videodev = devradio;
 429        devradio->priv = card;
 430        gemtek_pci_mute( card );
 431
 432        printk( KERN_INFO "Gemtek PCI Radio (rev. %d) found at 0x%04x-0x%04x.\n",
 433                pci_dev->revision, card->iobase, card->iobase + card->length - 1 );
 434
 435        return 0;
 436
 437err_video:
 438        release_region( card->iobase, card->length );
 439
 440err_pci:
 441        kfree( card );
 442        return -ENODEV;
 443}
 444
 445static void __devexit gemtek_pci_remove( struct pci_dev *pci_dev )
 446{
 447        struct gemtek_pci_card *card = pci_get_drvdata( pci_dev );
 448
 449        video_unregister_device( card->videodev );
 450        kfree( card->videodev );
 451
 452        release_region( card->iobase, card->length );
 453
 454        if ( mx )
 455                gemtek_pci_mute( card );
 456
 457        kfree( card );
 458
 459        pci_set_drvdata( pci_dev, NULL );
 460}
 461
 462static struct pci_driver gemtek_pci_driver =
 463{
 464        .name           = "gemtek_pci",
 465        .id_table       = gemtek_pci_id,
 466        .probe          = gemtek_pci_probe,
 467        .remove         = __devexit_p(gemtek_pci_remove),
 468};
 469
 470static int __init gemtek_pci_init_module( void )
 471{
 472        return pci_register_driver( &gemtek_pci_driver );
 473}
 474
 475static void __exit gemtek_pci_cleanup_module( void )
 476{
 477        pci_unregister_driver(&gemtek_pci_driver);
 478}
 479
 480MODULE_AUTHOR( "Vladimir Shebordaev <vshebordaev@mail.ru>" );
 481MODULE_DESCRIPTION( "The video4linux driver for the Gemtek PCI Radio Card" );
 482MODULE_LICENSE("GPL");
 483
 484module_param(mx, bool, 0);
 485MODULE_PARM_DESC( mx, "single digit: 1 - turn off the turner upon module exit (default), 0 - do not" );
 486module_param(nr_radio, int, 0);
 487MODULE_PARM_DESC( nr_radio, "video4linux device number to use");
 488
 489module_init( gemtek_pci_init_module );
 490module_exit( gemtek_pci_cleanup_module );
 491
 492
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.