linux/drivers/media/usb/au0828/au0828-dvb.c
<<
>>
Prefs
   1/*
   2 *  Driver for the Auvitek USB bridge
   3 *
   4 *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
   5 *
   6 *  This program is free software; you can redistribute it and/or modify
   7 *  it under the terms of the GNU General Public License as published by
   8 *  the Free Software Foundation; either version 2 of the License, or
   9 *  (at your option) any later version.
  10 *
  11 *  This program is distributed in the hope that it will be useful,
  12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *
  15 *  GNU General Public License for more details.
  16 *
  17 *  You should have received a copy of the GNU General Public License
  18 *  along with this program; if not, write to the Free Software
  19 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20 */
  21
  22#include <linux/module.h>
  23#include <linux/slab.h>
  24#include <linux/init.h>
  25#include <linux/device.h>
  26#include <linux/suspend.h>
  27#include <media/v4l2-common.h>
  28#include <media/tuner.h>
  29
  30#include "au0828.h"
  31#include "au8522.h"
  32#include "xc5000.h"
  33#include "mxl5007t.h"
  34#include "tda18271.h"
  35
  36DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  37
  38#define _AU0828_BULKPIPE 0x83
  39#define _BULKPIPESIZE 0xe522
  40
  41static u8 hauppauge_hvr950q_led_states[] = {
  42        0x00, /* off */
  43        0x02, /* yellow */
  44        0x04, /* green */
  45};
  46
  47static struct au8522_led_config hauppauge_hvr950q_led_cfg = {
  48        .gpio_output = 0x00e0,
  49        .gpio_output_enable  = 0x6006,
  50        .gpio_output_disable = 0x0660,
  51
  52        .gpio_leds = 0x00e2,
  53        .led_states  = hauppauge_hvr950q_led_states,
  54        .num_led_states = sizeof(hauppauge_hvr950q_led_states),
  55
  56        .vsb8_strong   = 20 /* dB */ * 10,
  57        .qam64_strong  = 25 /* dB */ * 10,
  58        .qam256_strong = 32 /* dB */ * 10,
  59};
  60
  61static struct au8522_config hauppauge_hvr950q_config = {
  62        .demod_address = 0x8e >> 1,
  63        .status_mode   = AU8522_DEMODLOCKING,
  64        .qam_if        = AU8522_IF_6MHZ,
  65        .vsb_if        = AU8522_IF_6MHZ,
  66        .led_cfg       = &hauppauge_hvr950q_led_cfg,
  67};
  68
  69static struct au8522_config fusionhdtv7usb_config = {
  70        .demod_address = 0x8e >> 1,
  71        .status_mode   = AU8522_DEMODLOCKING,
  72        .qam_if        = AU8522_IF_6MHZ,
  73        .vsb_if        = AU8522_IF_6MHZ,
  74};
  75
  76static struct au8522_config hauppauge_woodbury_config = {
  77        .demod_address = 0x8e >> 1,
  78        .status_mode   = AU8522_DEMODLOCKING,
  79        .qam_if        = AU8522_IF_4MHZ,
  80        .vsb_if        = AU8522_IF_3_25MHZ,
  81};
  82
  83static struct xc5000_config hauppauge_xc5000a_config = {
  84        .i2c_address      = 0x61,
  85        .if_khz           = 6000,
  86        .chip_id          = XC5000A,
  87};
  88
  89static struct xc5000_config hauppauge_xc5000c_config = {
  90        .i2c_address      = 0x61,
  91        .if_khz           = 6000,
  92        .chip_id          = XC5000C,
  93};
  94
  95static struct mxl5007t_config mxl5007t_hvr950q_config = {
  96        .xtal_freq_hz = MxL_XTAL_24_MHZ,
  97        .if_freq_hz = MxL_IF_6_MHZ,
  98};
  99
 100static struct tda18271_config hauppauge_woodbury_tunerconfig = {
 101        .gate    = TDA18271_GATE_DIGITAL,
 102};
 103
 104static void au0828_restart_dvb_streaming(struct work_struct *work);
 105
 106static void au0828_bulk_timeout(struct timer_list *t)
 107{
 108        struct au0828_dev *dev = from_timer(dev, t, bulk_timeout);
 109
 110        dprintk(1, "%s called\n", __func__);
 111        dev->bulk_timeout_running = 0;
 112        schedule_work(&dev->restart_streaming);
 113}
 114
 115/*-------------------------------------------------------------------*/
 116static void urb_completion(struct urb *purb)
 117{
 118        struct au0828_dev *dev = purb->context;
 119        int ptype = usb_pipetype(purb->pipe);
 120        unsigned char *ptr;
 121
 122        dprintk(2, "%s()\n", __func__);
 123
 124        if (!dev)
 125                return;
 126
 127        if (dev->urb_streaming == 0)
 128                return;
 129
 130        if (ptype != PIPE_BULK) {
 131                printk(KERN_ERR "%s() Unsupported URB type %d\n",
 132                       __func__, ptype);
 133                return;
 134        }
 135
 136        /* See if the stream is corrupted (to work around a hardware
 137           bug where the stream gets misaligned */
 138        ptr = purb->transfer_buffer;
 139        if (purb->actual_length > 0 && ptr[0] != 0x47) {
 140                dprintk(1, "Need to restart streaming %02x len=%d!\n",
 141                        ptr[0], purb->actual_length);
 142                schedule_work(&dev->restart_streaming);
 143                return;
 144        } else if (dev->bulk_timeout_running == 1) {
 145                /* The URB handler has fired, so cancel timer which would
 146                 * restart endpoint if we hadn't
 147                 */
 148                dprintk(1, "%s cancelling bulk timeout\n", __func__);
 149                dev->bulk_timeout_running = 0;
 150                del_timer(&dev->bulk_timeout);
 151        }
 152
 153        /* Feed the transport payload into the kernel demux */
 154        dvb_dmx_swfilter_packets(&dev->dvb.demux,
 155                purb->transfer_buffer, purb->actual_length / 188);
 156
 157        /* Clean the buffer before we requeue */
 158        memset(purb->transfer_buffer, 0, URB_BUFSIZE);
 159
 160        /* Requeue URB */
 161        usb_submit_urb(purb, GFP_ATOMIC);
 162}
 163
 164static int stop_urb_transfer(struct au0828_dev *dev)
 165{
 166        int i;
 167
 168        dprintk(2, "%s()\n", __func__);
 169
 170        if (dev->bulk_timeout_running == 1) {
 171                dev->bulk_timeout_running = 0;
 172                del_timer(&dev->bulk_timeout);
 173        }
 174
 175        dev->urb_streaming = 0;
 176        for (i = 0; i < URB_COUNT; i++) {
 177                usb_kill_urb(dev->urbs[i]);
 178                kfree(dev->urbs[i]->transfer_buffer);
 179                usb_free_urb(dev->urbs[i]);
 180        }
 181
 182        return 0;
 183}
 184
 185static int start_urb_transfer(struct au0828_dev *dev)
 186{
 187        struct urb *purb;
 188        int i, ret = -ENOMEM;
 189
 190        dprintk(2, "%s()\n", __func__);
 191
 192        if (dev->urb_streaming) {
 193                dprintk(2, "%s: bulk xfer already running!\n", __func__);
 194                return 0;
 195        }
 196
 197        for (i = 0; i < URB_COUNT; i++) {
 198
 199                dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
 200                if (!dev->urbs[i])
 201                        goto err;
 202
 203                purb = dev->urbs[i];
 204
 205                purb->transfer_buffer = kzalloc(URB_BUFSIZE, GFP_KERNEL);
 206                if (!purb->transfer_buffer) {
 207                        usb_free_urb(purb);
 208                        dev->urbs[i] = NULL;
 209                        goto err;
 210                }
 211
 212                purb->status = -EINPROGRESS;
 213                usb_fill_bulk_urb(purb,
 214                                  dev->usbdev,
 215                                  usb_rcvbulkpipe(dev->usbdev,
 216                                        _AU0828_BULKPIPE),
 217                                  purb->transfer_buffer,
 218                                  URB_BUFSIZE,
 219                                  urb_completion,
 220                                  dev);
 221
 222        }
 223
 224        for (i = 0; i < URB_COUNT; i++) {
 225                ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
 226                if (ret != 0) {
 227                        stop_urb_transfer(dev);
 228                        printk(KERN_ERR "%s: failed urb submission, "
 229                               "err = %d\n", __func__, ret);
 230                        return ret;
 231                }
 232        }
 233
 234        dev->urb_streaming = 1;
 235
 236        /* If we don't valid data within 1 second, restart stream */
 237        mod_timer(&dev->bulk_timeout, jiffies + (HZ));
 238        dev->bulk_timeout_running = 1;
 239
 240        ret = 0;
 241
 242err:
 243        return ret;
 244}
 245
 246static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
 247{
 248        struct dvb_demux *demux = feed->demux;
 249        struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
 250        struct au0828_dvb *dvb = &dev->dvb;
 251        int ret = 0;
 252
 253        dprintk(1, "%s()\n", __func__);
 254
 255        if (!demux->dmx.frontend)
 256                return -EINVAL;
 257
 258        if (dvb) {
 259                mutex_lock(&dvb->lock);
 260                if (dvb->feeding++ == 0) {
 261                        /* Start transport */
 262                        au0828_write(dev, 0x608, 0x90);
 263                        au0828_write(dev, 0x609, 0x72);
 264                        au0828_write(dev, 0x60a, 0x71);
 265                        au0828_write(dev, 0x60b, 0x01);
 266                        ret = start_urb_transfer(dev);
 267                }
 268                mutex_unlock(&dvb->lock);
 269        }
 270
 271        return ret;
 272}
 273
 274static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
 275{
 276        struct dvb_demux *demux = feed->demux;
 277        struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
 278        struct au0828_dvb *dvb = &dev->dvb;
 279        int ret = 0;
 280
 281        dprintk(1, "%s()\n", __func__);
 282
 283        if (dvb) {
 284                mutex_lock(&dvb->lock);
 285                if (--dvb->feeding == 0) {
 286                        /* Stop transport */
 287                        ret = stop_urb_transfer(dev);
 288                        au0828_write(dev, 0x60b, 0x00);
 289                }
 290                mutex_unlock(&dvb->lock);
 291        }
 292
 293        return ret;
 294}
 295
 296static void au0828_restart_dvb_streaming(struct work_struct *work)
 297{
 298        struct au0828_dev *dev = container_of(work, struct au0828_dev,
 299                                              restart_streaming);
 300        struct au0828_dvb *dvb = &dev->dvb;
 301
 302        if (dev->urb_streaming == 0)
 303                return;
 304
 305        dprintk(1, "Restarting streaming...!\n");
 306
 307        mutex_lock(&dvb->lock);
 308
 309        /* Stop transport */
 310        stop_urb_transfer(dev);
 311        au0828_write(dev, 0x608, 0x00);
 312        au0828_write(dev, 0x609, 0x00);
 313        au0828_write(dev, 0x60a, 0x00);
 314        au0828_write(dev, 0x60b, 0x00);
 315
 316        /* Start transport */
 317        au0828_write(dev, 0x608, 0x90);
 318        au0828_write(dev, 0x609, 0x72);
 319        au0828_write(dev, 0x60a, 0x71);
 320        au0828_write(dev, 0x60b, 0x01);
 321        start_urb_transfer(dev);
 322
 323        mutex_unlock(&dvb->lock);
 324}
 325
 326static int dvb_register(struct au0828_dev *dev)
 327{
 328        struct au0828_dvb *dvb = &dev->dvb;
 329        int result;
 330
 331        dprintk(1, "%s()\n", __func__);
 332
 333        INIT_WORK(&dev->restart_streaming, au0828_restart_dvb_streaming);
 334
 335        /* register adapter */
 336        result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
 337                                      &dev->usbdev->dev, adapter_nr);
 338        if (result < 0) {
 339                printk(KERN_ERR "%s: dvb_register_adapter failed "
 340                       "(errno = %d)\n", DRIVER_NAME, result);
 341                goto fail_adapter;
 342        }
 343        dvb->adapter.priv = dev;
 344
 345        /* register frontend */
 346        result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
 347        if (result < 0) {
 348                printk(KERN_ERR "%s: dvb_register_frontend failed "
 349                       "(errno = %d)\n", DRIVER_NAME, result);
 350                goto fail_frontend;
 351        }
 352
 353        /* register demux stuff */
 354        dvb->demux.dmx.capabilities =
 355                DMX_TS_FILTERING | DMX_SECTION_FILTERING |
 356                DMX_MEMORY_BASED_FILTERING;
 357        dvb->demux.priv       = dev;
 358        dvb->demux.filternum  = 256;
 359        dvb->demux.feednum    = 256;
 360        dvb->demux.start_feed = au0828_dvb_start_feed;
 361        dvb->demux.stop_feed  = au0828_dvb_stop_feed;
 362        result = dvb_dmx_init(&dvb->demux);
 363        if (result < 0) {
 364                printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
 365                       DRIVER_NAME, result);
 366                goto fail_dmx;
 367        }
 368
 369        dvb->dmxdev.filternum    = 256;
 370        dvb->dmxdev.demux        = &dvb->demux.dmx;
 371        dvb->dmxdev.capabilities = 0;
 372        result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
 373        if (result < 0) {
 374                printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
 375                       DRIVER_NAME, result);
 376                goto fail_dmxdev;
 377        }
 378
 379        dvb->fe_hw.source = DMX_FRONTEND_0;
 380        result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
 381        if (result < 0) {
 382                printk(KERN_ERR "%s: add_frontend failed "
 383                       "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
 384                goto fail_fe_hw;
 385        }
 386
 387        dvb->fe_mem.source = DMX_MEMORY_FE;
 388        result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
 389        if (result < 0) {
 390                printk(KERN_ERR "%s: add_frontend failed "
 391                       "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
 392                goto fail_fe_mem;
 393        }
 394
 395        result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
 396        if (result < 0) {
 397                printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
 398                       DRIVER_NAME, result);
 399                goto fail_fe_conn;
 400        }
 401
 402        /* register network adapter */
 403        dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
 404        return 0;
 405
 406fail_fe_conn:
 407        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
 408fail_fe_mem:
 409        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
 410fail_fe_hw:
 411        dvb_dmxdev_release(&dvb->dmxdev);
 412fail_dmxdev:
 413        dvb_dmx_release(&dvb->demux);
 414fail_dmx:
 415        dvb_unregister_frontend(dvb->frontend);
 416fail_frontend:
 417        dvb_frontend_detach(dvb->frontend);
 418        dvb_unregister_adapter(&dvb->adapter);
 419fail_adapter:
 420        return result;
 421}
 422
 423void au0828_dvb_unregister(struct au0828_dev *dev)
 424{
 425        struct au0828_dvb *dvb = &dev->dvb;
 426
 427        dprintk(1, "%s()\n", __func__);
 428
 429        if (dvb->frontend == NULL)
 430                return;
 431
 432        dvb_net_release(&dvb->net);
 433        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
 434        dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
 435        dvb_dmxdev_release(&dvb->dmxdev);
 436        dvb_dmx_release(&dvb->demux);
 437        dvb_unregister_frontend(dvb->frontend);
 438        dvb_frontend_detach(dvb->frontend);
 439        dvb_unregister_adapter(&dvb->adapter);
 440}
 441
 442/* All the DVB attach calls go here, this function get's modified
 443 * for each new card. No other function in this file needs
 444 * to change.
 445 */
 446int au0828_dvb_register(struct au0828_dev *dev)
 447{
 448        struct au0828_dvb *dvb = &dev->dvb;
 449        int ret;
 450
 451        dprintk(1, "%s()\n", __func__);
 452
 453        /* init frontend */
 454        switch (dev->boardnr) {
 455        case AU0828_BOARD_HAUPPAUGE_HVR850:
 456        case AU0828_BOARD_HAUPPAUGE_HVR950Q:
 457                dvb->frontend = dvb_attach(au8522_attach,
 458                                &hauppauge_hvr950q_config,
 459                                &dev->i2c_adap);
 460                if (dvb->frontend != NULL)
 461                        switch (dev->board.tuner_type) {
 462                        default:
 463                        case TUNER_XC5000:
 464                                dvb_attach(xc5000_attach, dvb->frontend,
 465                                           &dev->i2c_adap,
 466                                           &hauppauge_xc5000a_config);
 467                                break;
 468                        case TUNER_XC5000C:
 469                                dvb_attach(xc5000_attach, dvb->frontend,
 470                                           &dev->i2c_adap,
 471                                           &hauppauge_xc5000c_config);
 472                                break;
 473                        }
 474                break;
 475        case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
 476                dvb->frontend = dvb_attach(au8522_attach,
 477                                &hauppauge_hvr950q_config,
 478                                &dev->i2c_adap);
 479                if (dvb->frontend != NULL)
 480                        dvb_attach(mxl5007t_attach, dvb->frontend,
 481                                   &dev->i2c_adap, 0x60,
 482                                   &mxl5007t_hvr950q_config);
 483                break;
 484        case AU0828_BOARD_HAUPPAUGE_WOODBURY:
 485                dvb->frontend = dvb_attach(au8522_attach,
 486                                &hauppauge_woodbury_config,
 487                                &dev->i2c_adap);
 488                if (dvb->frontend != NULL)
 489                        dvb_attach(tda18271_attach, dvb->frontend,
 490                                   0x60, &dev->i2c_adap,
 491                                   &hauppauge_woodbury_tunerconfig);
 492                break;
 493        case AU0828_BOARD_DVICO_FUSIONHDTV7:
 494                dvb->frontend = dvb_attach(au8522_attach,
 495                                &fusionhdtv7usb_config,
 496                                &dev->i2c_adap);
 497                if (dvb->frontend != NULL) {
 498                        dvb_attach(xc5000_attach, dvb->frontend,
 499                                &dev->i2c_adap,
 500                                &hauppauge_xc5000a_config);
 501                }
 502                break;
 503        default:
 504                printk(KERN_WARNING "The frontend of your DVB/ATSC card "
 505                       "isn't supported yet\n");
 506                break;
 507        }
 508        if (NULL == dvb->frontend) {
 509                printk(KERN_ERR "%s() Frontend initialization failed\n",
 510                       __func__);
 511                return -1;
 512        }
 513        /* define general-purpose callback pointer */
 514        dvb->frontend->callback = au0828_tuner_callback;
 515
 516        /* register everything */
 517        ret = dvb_register(dev);
 518        if (ret < 0) {
 519                if (dvb->frontend->ops.release)
 520                        dvb->frontend->ops.release(dvb->frontend);
 521                return ret;
 522        }
 523
 524        timer_setup(&dev->bulk_timeout, au0828_bulk_timeout, 0);
 525
 526        return 0;
 527}
 528