linux/drivers/media/video/cx231xx/cx231xx-cards.c
<<
>>
Prefs
   1/*
   2   cx231xx-cards.c - driver for Conexant Cx23100/101/102
   3                                USB video capture devices
   4
   5   Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
   6                                Based on em28xx driver
   7
   8   This program is free software; you can redistribute it and/or modify
   9   it under the terms of the GNU General Public License as published by
  10   the Free Software Foundation; either version 2 of the License, or
  11   (at your option) any later version.
  12
  13   This program is distributed in the hope that it will be useful,
  14   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16   GNU General Public License for more details.
  17
  18   You should have received a copy of the GNU General Public License
  19   along with this program; if not, write to the Free Software
  20   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21 */
  22
  23#include <linux/init.h>
  24#include <linux/module.h>
  25#include <linux/slab.h>
  26#include <linux/delay.h>
  27#include <linux/i2c.h>
  28#include <linux/usb.h>
  29#include <media/tuner.h>
  30#include <media/tveeprom.h>
  31#include <media/v4l2-common.h>
  32#include <media/v4l2-chip-ident.h>
  33
  34#include <media/cx25840.h>
  35#include "xc5000.h"
  36
  37#include "cx231xx.h"
  38
  39static int tuner = -1;
  40module_param(tuner, int, 0444);
  41MODULE_PARM_DESC(tuner, "tuner type");
  42
  43static unsigned int disable_ir;
  44module_param(disable_ir, int, 0444);
  45MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
  46
  47/* Bitmask marking allocated devices from 0 to CX231XX_MAXBOARDS */
  48static unsigned long cx231xx_devused;
  49
  50/*
  51 *  Reset sequences for analog/digital modes
  52 */
  53
  54static struct cx231xx_reg_seq RDE250_XCV_TUNER[] = {
  55        {0x03, 0x01, 10},
  56        {0x03, 0x00, 30},
  57        {0x03, 0x01, 10},
  58        {-1, -1, -1},
  59};
  60
  61/*
  62 *  Board definitions
  63 */
  64struct cx231xx_board cx231xx_boards[] = {
  65        [CX231XX_BOARD_UNKNOWN] = {
  66                .name = "Unknown CX231xx video grabber",
  67                .tuner_type = TUNER_ABSENT,
  68                .input = {{
  69                                .type = CX231XX_VMUX_TELEVISION,
  70                                .vmux = CX231XX_VIN_3_1,
  71                                .amux = CX231XX_AMUX_VIDEO,
  72                                .gpio = NULL,
  73                        }, {
  74                                .type = CX231XX_VMUX_COMPOSITE1,
  75                                .vmux = CX231XX_VIN_2_1,
  76                                .amux = CX231XX_AMUX_LINE_IN,
  77                                .gpio = NULL,
  78                        }, {
  79                                .type = CX231XX_VMUX_SVIDEO,
  80                                .vmux = CX231XX_VIN_1_1 |
  81                                        (CX231XX_VIN_1_2 << 8) |
  82                                        CX25840_SVIDEO_ON,
  83                                .amux = CX231XX_AMUX_LINE_IN,
  84                                .gpio = NULL,
  85                        }
  86                },
  87        },
  88        [CX231XX_BOARD_CNXT_RDE_250] = {
  89                .name = "Conexant Hybrid TV - RDE250",
  90                .tuner_type = TUNER_XC5000,
  91                .tuner_addr = 0x61,
  92                .tuner_gpio = RDE250_XCV_TUNER,
  93                .tuner_sif_gpio = 0x05,
  94                .tuner_scl_gpio = 0x1a,
  95                .tuner_sda_gpio = 0x1b,
  96                .decoder = CX231XX_AVDECODER,
  97                .demod_xfer_mode = 0,
  98                .ctl_pin_status_mask = 0xFFFFFFC4,
  99                .agc_analog_digital_select_gpio = 0x0c,
 100                .gpio_pin_status_mask = 0x4001000,
 101                .tuner_i2c_master = 1,
 102                .demod_i2c_master = 2,
 103                .has_dvb = 1,
 104                .demod_addr = 0x02,
 105                .norm = V4L2_STD_PAL,
 106
 107                .input = {{
 108                                .type = CX231XX_VMUX_TELEVISION,
 109                                .vmux = CX231XX_VIN_3_1,
 110                                .amux = CX231XX_AMUX_VIDEO,
 111                                .gpio = NULL,
 112                        }, {
 113                                .type = CX231XX_VMUX_COMPOSITE1,
 114                                .vmux = CX231XX_VIN_2_1,
 115                                .amux = CX231XX_AMUX_LINE_IN,
 116                                .gpio = NULL,
 117                        }, {
 118                                .type = CX231XX_VMUX_SVIDEO,
 119                                .vmux = CX231XX_VIN_1_1 |
 120                                        (CX231XX_VIN_1_2 << 8) |
 121                                        CX25840_SVIDEO_ON,
 122                                .amux = CX231XX_AMUX_LINE_IN,
 123                                .gpio = NULL,
 124                        }
 125                },
 126        },
 127
 128        [CX231XX_BOARD_CNXT_RDU_250] = {
 129                .name = "Conexant Hybrid TV - RDU250",
 130                .tuner_type = TUNER_XC5000,
 131                .tuner_addr = 0x61,
 132                .tuner_gpio = RDE250_XCV_TUNER,
 133                .tuner_sif_gpio = 0x05,
 134                .tuner_scl_gpio = 0x1a,
 135                .tuner_sda_gpio = 0x1b,
 136                .decoder = CX231XX_AVDECODER,
 137                .demod_xfer_mode = 0,
 138                .ctl_pin_status_mask = 0xFFFFFFC4,
 139                .agc_analog_digital_select_gpio = 0x0c,
 140                .gpio_pin_status_mask = 0x4001000,
 141                .tuner_i2c_master = 1,
 142                .demod_i2c_master = 2,
 143                .has_dvb = 1,
 144                .demod_addr = 0x32,
 145                .norm = V4L2_STD_NTSC,
 146
 147                .input = {{
 148                                .type = CX231XX_VMUX_TELEVISION,
 149                                .vmux = CX231XX_VIN_3_1,
 150                                .amux = CX231XX_AMUX_VIDEO,
 151                                .gpio = NULL,
 152                        }, {
 153                                .type = CX231XX_VMUX_COMPOSITE1,
 154                                .vmux = CX231XX_VIN_2_1,
 155                                .amux = CX231XX_AMUX_LINE_IN,
 156                                .gpio = NULL,
 157                        }, {
 158                                .type = CX231XX_VMUX_SVIDEO,
 159                                .vmux = CX231XX_VIN_1_1 |
 160                                        (CX231XX_VIN_1_2 << 8) |
 161                                        CX25840_SVIDEO_ON,
 162                                .amux = CX231XX_AMUX_LINE_IN,
 163                                .gpio = NULL,
 164                        }
 165                },
 166        },
 167};
 168const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);
 169
 170/* table of devices that work with this driver */
 171struct usb_device_id cx231xx_id_table[] = {
 172        {USB_DEVICE(0x0572, 0x5A3C),
 173         .driver_info = CX231XX_BOARD_UNKNOWN},
 174        {USB_DEVICE(0x0572, 0x58A2),
 175         .driver_info = CX231XX_BOARD_CNXT_RDE_250},
 176        {USB_DEVICE(0x0572, 0x58A1),
 177         .driver_info = CX231XX_BOARD_CNXT_RDU_250},
 178        {},
 179};
 180
 181MODULE_DEVICE_TABLE(usb, cx231xx_id_table);
 182
 183/* cx231xx_tuner_callback
 184 * will be used to reset XC5000 tuner using GPIO pin
 185 */
 186
 187int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
 188{
 189        int rc = 0;
 190        struct cx231xx *dev = ptr;
 191
 192        if (dev->tuner_type == TUNER_XC5000) {
 193                if (command == XC5000_TUNER_RESET) {
 194                        cx231xx_info
 195                                ("Tuner CB: RESET: cmd %d : tuner type %d \n",
 196                                 command, dev->tuner_type);
 197                        cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
 198                                               1);
 199                        msleep(10);
 200                        cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
 201                                               0);
 202                        msleep(330);
 203                        cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
 204                                               1);
 205                        msleep(10);
 206                }
 207        }
 208        return rc;
 209}
 210EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);
 211
 212static inline void cx231xx_set_model(struct cx231xx *dev)
 213{
 214        memcpy(&dev->board, &cx231xx_boards[dev->model], sizeof(dev->board));
 215}
 216
 217/* Since cx231xx_pre_card_setup() requires a proper dev->model,
 218 * this won't work for boards with generic PCI IDs
 219 */
 220void cx231xx_pre_card_setup(struct cx231xx *dev)
 221{
 222
 223        cx231xx_set_model(dev);
 224
 225        cx231xx_info("Identified as %s (card=%d)\n",
 226                     dev->board.name, dev->model);
 227
 228        /* set the direction for GPIO pins */
 229        cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
 230        cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
 231        cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
 232
 233        /* request some modules if any required */
 234
 235        /* reset the Tuner */
 236        cx231xx_gpio_set(dev, dev->board.tuner_gpio);
 237
 238        /* set the mode to Analog mode initially */
 239        cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
 240
 241        /* Unlock device */
 242        /* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
 243
 244}
 245
 246static void cx231xx_config_tuner(struct cx231xx *dev)
 247{
 248        struct tuner_setup tun_setup;
 249        struct v4l2_frequency f;
 250
 251        if (dev->tuner_type == TUNER_ABSENT)
 252                return;
 253
 254        tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
 255        tun_setup.type = dev->tuner_type;
 256        tun_setup.addr = dev->tuner_addr;
 257        tun_setup.tuner_callback = cx231xx_tuner_callback;
 258
 259        tuner_call(dev, tuner, s_type_addr, &tun_setup);
 260
 261#if 0
 262        if (tun_setup.type == TUNER_XC5000) {
 263                static struct xc2028_ctrl ctrl = {
 264                        .fname = XC5000_DEFAULT_FIRMWARE,
 265                        .max_len = 64,
 266                        .demod = 0;
 267                };
 268                struct v4l2_priv_tun_config cfg = {
 269                        .tuner = dev->tuner_type,
 270                        .priv = &ctrl,
 271                };
 272                tuner_call(dev, tuner, s_config, &cfg);
 273        }
 274#endif
 275        /* configure tuner */
 276        f.tuner = 0;
 277        f.type = V4L2_TUNER_ANALOG_TV;
 278        f.frequency = 9076;     /* just a magic number */
 279        dev->ctl_freq = f.frequency;
 280        call_all(dev, tuner, s_frequency, &f);
 281
 282}
 283
 284/* ----------------------------------------------------------------------- */
 285void cx231xx_register_i2c_ir(struct cx231xx *dev)
 286{
 287        if (disable_ir)
 288                return;
 289
 290        /* REVISIT: instantiate IR device */
 291
 292        /* detect & configure */
 293        switch (dev->model) {
 294
 295        case CX231XX_BOARD_CNXT_RDE_250:
 296                break;
 297        case CX231XX_BOARD_CNXT_RDU_250:
 298                break;
 299        default:
 300                break;
 301        }
 302}
 303
 304void cx231xx_card_setup(struct cx231xx *dev)
 305{
 306
 307        cx231xx_set_model(dev);
 308
 309        dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
 310        if (cx231xx_boards[dev->model].tuner_addr)
 311                dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
 312
 313        /* request some modules */
 314        if (dev->board.decoder == CX231XX_AVDECODER) {
 315                dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
 316                                        &dev->i2c_bus[0].i2c_adap,
 317                                        "cx25840", "cx25840", 0x88 >> 1, NULL);
 318                if (dev->sd_cx25840 == NULL)
 319                        cx231xx_info("cx25840 subdev registration failure\n");
 320                cx25840_call(dev, core, load_fw);
 321
 322        }
 323
 324        if (dev->board.tuner_type != TUNER_ABSENT) {
 325                dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
 326                                &dev->i2c_bus[1].i2c_adap,
 327                                "tuner", "tuner", 0xc2 >> 1, NULL);
 328                if (dev->sd_tuner == NULL)
 329                        cx231xx_info("tuner subdev registration failure\n");
 330
 331                cx231xx_config_tuner(dev);
 332        }
 333
 334        cx231xx_config_tuner(dev);
 335
 336#if 0
 337        /* TBD  IR will be added later */
 338        cx231xx_ir_init(dev);
 339#endif
 340}
 341
 342/*
 343 * cx231xx_config()
 344 * inits registers with sane defaults
 345 */
 346int cx231xx_config(struct cx231xx *dev)
 347{
 348        /* TBD need to add cx231xx specific code */
 349        dev->mute = 1;          /* maybe not the right place... */
 350        dev->volume = 0x1f;
 351
 352        return 0;
 353}
 354
 355/*
 356 * cx231xx_config_i2c()
 357 * configure i2c attached devices
 358 */
 359void cx231xx_config_i2c(struct cx231xx *dev)
 360{
 361        /* u32 input = INPUT(dev->video_input)->vmux; */
 362
 363        call_all(dev, video, s_stream, 1);
 364}
 365
 366/*
 367 * cx231xx_realease_resources()
 368 * unregisters the v4l2,i2c and usb devices
 369 * called when the device gets disconected or at module unload
 370*/
 371void cx231xx_release_resources(struct cx231xx *dev)
 372{
 373
 374#if 0           /* TBD IR related  */
 375        if (dev->ir)
 376                cx231xx_ir_fini(dev);
 377#endif
 378
 379        cx231xx_release_analog_resources(dev);
 380
 381        cx231xx_remove_from_devlist(dev);
 382
 383        cx231xx_dev_uninit(dev);
 384
 385        usb_put_dev(dev->udev);
 386
 387        /* Mark device as unused */
 388        cx231xx_devused &= ~(1 << dev->devno);
 389}
 390
 391/*
 392 * cx231xx_init_dev()
 393 * allocates and inits the device structs, registers i2c bus and v4l device
 394 */
 395static int cx231xx_init_dev(struct cx231xx **devhandle, struct usb_device *udev,
 396                            int minor)
 397{
 398        struct cx231xx *dev = *devhandle;
 399        int retval = -ENOMEM;
 400        int errCode;
 401        unsigned int maxh, maxw;
 402
 403        dev->udev = udev;
 404        mutex_init(&dev->lock);
 405        mutex_init(&dev->ctrl_urb_lock);
 406        mutex_init(&dev->gpio_i2c_lock);
 407
 408        spin_lock_init(&dev->video_mode.slock);
 409        spin_lock_init(&dev->vbi_mode.slock);
 410        spin_lock_init(&dev->sliced_cc_mode.slock);
 411
 412        init_waitqueue_head(&dev->open);
 413        init_waitqueue_head(&dev->wait_frame);
 414        init_waitqueue_head(&dev->wait_stream);
 415
 416        dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
 417        dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
 418        dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
 419        dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
 420        dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
 421
 422        /* Query cx231xx to find what pcb config it is related to */
 423        initialize_cx231xx(dev);
 424
 425        /* Cx231xx pre card setup */
 426        cx231xx_pre_card_setup(dev);
 427
 428        errCode = cx231xx_config(dev);
 429        if (errCode) {
 430                cx231xx_errdev("error configuring device\n");
 431                return -ENOMEM;
 432        }
 433
 434        /* set default norm */
 435        dev->norm = dev->board.norm;
 436
 437        /* register i2c bus */
 438        errCode = cx231xx_dev_init(dev);
 439        if (errCode < 0) {
 440                cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
 441                               __func__, errCode);
 442                return errCode;
 443        }
 444
 445        /* Do board specific init */
 446        cx231xx_card_setup(dev);
 447
 448        /* configure the device */
 449        cx231xx_config_i2c(dev);
 450
 451        maxw = norm_maxw(dev);
 452        maxh = norm_maxh(dev);
 453
 454        /* set default image size */
 455        dev->width = maxw;
 456        dev->height = maxh;
 457        dev->interlaced = 0;
 458        dev->hscale = 0;
 459        dev->vscale = 0;
 460        dev->video_input = 0;
 461
 462        errCode = cx231xx_config(dev);
 463        if (errCode < 0) {
 464                cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
 465                               __func__, errCode);
 466                return errCode;
 467        }
 468
 469        /* init video dma queues */
 470        INIT_LIST_HEAD(&dev->video_mode.vidq.active);
 471        INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
 472
 473        /* init vbi dma queues */
 474        INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
 475        INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
 476
 477        /* Reset other chips required if they are tied up with GPIO pins */
 478
 479        cx231xx_add_into_devlist(dev);
 480
 481        retval = cx231xx_register_analog_devices(dev);
 482        if (retval < 0) {
 483                cx231xx_release_resources(dev);
 484                goto fail_reg_devices;
 485        }
 486
 487        cx231xx_init_extension(dev);
 488
 489        return 0;
 490
 491fail_reg_devices:
 492        mutex_unlock(&dev->lock);
 493        return retval;
 494}
 495
 496#if defined(CONFIG_MODULES) && defined(MODULE)
 497static void request_module_async(struct work_struct *work)
 498{
 499        struct cx231xx *dev = container_of(work,
 500                                           struct cx231xx, request_module_wk);
 501
 502        if (dev->has_alsa_audio)
 503                request_module("cx231xx-alsa");
 504
 505        if (dev->board.has_dvb)
 506                request_module("cx231xx-dvb");
 507
 508}
 509
 510static void request_modules(struct cx231xx *dev)
 511{
 512        INIT_WORK(&dev->request_module_wk, request_module_async);
 513        schedule_work(&dev->request_module_wk);
 514}
 515#else
 516#define request_modules(dev)
 517#endif /* CONFIG_MODULES */
 518
 519/*
 520 * cx231xx_usb_probe()
 521 * checks for supported devices
 522 */
 523static int cx231xx_usb_probe(struct usb_interface *interface,
 524                             const struct usb_device_id *id)
 525{
 526        struct usb_device *udev;
 527        struct usb_interface *uif;
 528        struct cx231xx *dev = NULL;
 529        int retval = -ENODEV;
 530        int nr = 0, ifnum;
 531        int i, isoc_pipe = 0;
 532        char *speed;
 533        char descr[255] = "";
 534        struct usb_interface *lif = NULL;
 535        int skip_interface = 0;
 536        struct usb_interface_assoc_descriptor *assoc_desc;
 537
 538        udev = usb_get_dev(interface_to_usbdev(interface));
 539        ifnum = interface->altsetting[0].desc.bInterfaceNumber;
 540
 541        if (!ifnum) {
 542                /*
 543                 * Interface number 0 - IR interface
 544                 */
 545                /* Check to see next free device and mark as used */
 546                nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
 547                cx231xx_devused |= 1 << nr;
 548
 549                if (nr >= CX231XX_MAXBOARDS) {
 550                        cx231xx_err(DRIVER_NAME ": Supports only %i cx231xx boards.\n",
 551                                     CX231XX_MAXBOARDS);
 552                        cx231xx_devused &= ~(1 << nr);
 553                        return -ENOMEM;
 554                }
 555
 556                /* allocate memory for our device state and initialize it */
 557                dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 558                if (dev == NULL) {
 559                        cx231xx_err(DRIVER_NAME ": out of memory!\n");
 560                        cx231xx_devused &= ~(1 << nr);
 561                        return -ENOMEM;
 562                }
 563
 564                snprintf(dev->name, 29, "cx231xx #%d", nr);
 565                dev->devno = nr;
 566                dev->model = id->driver_info;
 567                dev->video_mode.alt = -1;
 568                dev->interface_count++;
 569
 570                /* reset gpio dir and value */
 571                dev->gpio_dir = 0;
 572                dev->gpio_val = 0;
 573                dev->xc_fw_load_done = 0;
 574                dev->has_alsa_audio = 1;
 575                dev->power_mode = -1;
 576
 577                /* 0 - vbi ; 1 -sliced cc mode */
 578                dev->vbi_or_sliced_cc_mode = 0;
 579
 580                /* get maximum no.of IAD interfaces */
 581                assoc_desc = udev->actconfig->intf_assoc[0];
 582                dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
 583
 584                /* init CIR module TBD */
 585
 586                /* store the current interface */
 587                lif = interface;
 588
 589                switch (udev->speed) {
 590                case USB_SPEED_LOW:
 591                        speed = "1.5";
 592                        break;
 593                case USB_SPEED_UNKNOWN:
 594                case USB_SPEED_FULL:
 595                        speed = "12";
 596                        break;
 597                case USB_SPEED_HIGH:
 598                        speed = "480";
 599                        break;
 600                default:
 601                        speed = "unknown";
 602                }
 603
 604                if (udev->manufacturer)
 605                        strlcpy(descr, udev->manufacturer, sizeof(descr));
 606
 607                if (udev->product) {
 608                        if (*descr)
 609                                strlcat(descr, " ", sizeof(descr));
 610                        strlcat(descr, udev->product, sizeof(descr));
 611                }
 612                if (*descr)
 613                        strlcat(descr, " ", sizeof(descr));
 614
 615                cx231xx_info("New device %s@ %s Mbps "
 616                     "(%04x:%04x) with %d interfaces\n",
 617                     descr,
 618                     speed,
 619                     le16_to_cpu(udev->descriptor.idVendor),
 620                     le16_to_cpu(udev->descriptor.idProduct),
 621                     dev->max_iad_interface_count);
 622        } else {
 623                /* Get dev structure first */
 624                dev = usb_get_intfdata(udev->actconfig->interface[0]);
 625                if (dev == NULL) {
 626                        cx231xx_err(DRIVER_NAME ": out of first interface!\n");
 627                        return -ENODEV;
 628                }
 629
 630                /* store the interface 0 back */
 631                lif = udev->actconfig->interface[0];
 632
 633                /* increment interface count */
 634                dev->interface_count++;
 635
 636                /* get device number */
 637                nr = dev->devno;
 638
 639                /*
 640                 * set skip interface, for all interfaces but
 641                 * interface 1 and the last one
 642                 */
 643                if ((ifnum != 1) && ((dev->interface_count - 1)
 644                                     != dev->max_iad_interface_count))
 645                        skip_interface = 1;
 646
 647                if (ifnum == 1) {
 648                        assoc_desc = udev->actconfig->intf_assoc[0];
 649                        if (assoc_desc->bFirstInterface != ifnum) {
 650                                cx231xx_err(DRIVER_NAME ": Not found "
 651                                            "matching IAD interface\n");
 652                                return -ENODEV;
 653                        }
 654                }
 655        }
 656
 657        if (skip_interface)
 658                return -ENODEV;
 659
 660        cx231xx_info("registering interface %d\n", ifnum);
 661
 662        /* save our data pointer in this interface device */
 663        usb_set_intfdata(lif, dev);
 664
 665        if ((dev->interface_count - 1) != dev->max_iad_interface_count)
 666                return 0;
 667
 668        /*
 669         * AV device initialization - only done at the last interface
 670         */
 671
 672        /* Create v4l2 device */
 673        retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
 674        if (retval) {
 675                cx231xx_errdev("v4l2_device_register failed\n");
 676                cx231xx_devused &= ~(1 << nr);
 677                kfree(dev);
 678                return -EIO;
 679        }
 680
 681        /* allocate device struct */
 682        retval = cx231xx_init_dev(&dev, udev, nr);
 683        if (retval) {
 684                cx231xx_devused &= ~(1 << dev->devno);
 685                v4l2_device_unregister(&dev->v4l2_dev);
 686                kfree(dev);
 687                return retval;
 688        }
 689
 690        /* compute alternate max packet sizes for video */
 691        uif = udev->actconfig->interface[dev->current_pcb_config.
 692                       hs_config_info[0].interface_info.video_index + 1];
 693
 694        dev->video_mode.end_point_addr = le16_to_cpu(uif->altsetting[0].
 695                        endpoint[isoc_pipe].desc.bEndpointAddress);
 696
 697        dev->video_mode.num_alt = uif->num_altsetting;
 698        cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
 699                     dev->video_mode.end_point_addr,
 700                     dev->video_mode.num_alt);
 701        dev->video_mode.alt_max_pkt_size =
 702                kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
 703
 704        if (dev->video_mode.alt_max_pkt_size == NULL) {
 705                cx231xx_errdev("out of memory!\n");
 706                cx231xx_devused &= ~(1 << nr);
 707                v4l2_device_unregister(&dev->v4l2_dev);
 708                kfree(dev);
 709                return -ENOMEM;
 710        }
 711
 712        for (i = 0; i < dev->video_mode.num_alt; i++) {
 713                u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
 714                                desc.wMaxPacketSize);
 715                dev->video_mode.alt_max_pkt_size[i] =
 716                    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
 717                cx231xx_info("Alternate setting %i, max size= %i\n", i,
 718                             dev->video_mode.alt_max_pkt_size[i]);
 719        }
 720
 721        /* compute alternate max packet sizes for vbi */
 722        uif = udev->actconfig->interface[dev->current_pcb_config.
 723                                       hs_config_info[0].interface_info.
 724                                       vanc_index + 1];
 725
 726        dev->vbi_mode.end_point_addr =
 727            le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
 728                        bEndpointAddress);
 729
 730        dev->vbi_mode.num_alt = uif->num_altsetting;
 731        cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
 732                     dev->vbi_mode.end_point_addr,
 733                     dev->vbi_mode.num_alt);
 734        dev->vbi_mode.alt_max_pkt_size =
 735            kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
 736
 737        if (dev->vbi_mode.alt_max_pkt_size == NULL) {
 738                cx231xx_errdev("out of memory!\n");
 739                cx231xx_devused &= ~(1 << nr);
 740                v4l2_device_unregister(&dev->v4l2_dev);
 741                kfree(dev);
 742                return -ENOMEM;
 743        }
 744
 745        for (i = 0; i < dev->vbi_mode.num_alt; i++) {
 746                u16 tmp =
 747                    le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
 748                                desc.wMaxPacketSize);
 749                dev->vbi_mode.alt_max_pkt_size[i] =
 750                    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
 751                cx231xx_info("Alternate setting %i, max size= %i\n", i,
 752                             dev->vbi_mode.alt_max_pkt_size[i]);
 753        }
 754
 755        /* compute alternate max packet sizes for sliced CC */
 756        uif = udev->actconfig->interface[dev->current_pcb_config.
 757                                       hs_config_info[0].interface_info.
 758                                       hanc_index + 1];
 759
 760        dev->sliced_cc_mode.end_point_addr =
 761            le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
 762                        bEndpointAddress);
 763
 764        dev->sliced_cc_mode.num_alt = uif->num_altsetting;
 765        cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
 766                     dev->sliced_cc_mode.end_point_addr,
 767                     dev->sliced_cc_mode.num_alt);
 768        dev->sliced_cc_mode.alt_max_pkt_size =
 769                kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
 770
 771        if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
 772                cx231xx_errdev("out of memory!\n");
 773                cx231xx_devused &= ~(1 << nr);
 774                v4l2_device_unregister(&dev->v4l2_dev);
 775                kfree(dev);
 776                return -ENOMEM;
 777        }
 778
 779        for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
 780                u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
 781                                desc.wMaxPacketSize);
 782                dev->sliced_cc_mode.alt_max_pkt_size[i] =
 783                    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
 784                cx231xx_info("Alternate setting %i, max size= %i\n", i,
 785                             dev->sliced_cc_mode.alt_max_pkt_size[i]);
 786        }
 787
 788        if (dev->current_pcb_config.ts1_source != 0xff) {
 789                /* compute alternate max packet sizes for TS1 */
 790                uif = udev->actconfig->interface[dev->current_pcb_config.
 791                                               hs_config_info[0].
 792                                               interface_info.
 793                                               ts1_index + 1];
 794
 795                dev->ts1_mode.end_point_addr =
 796                    le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
 797                                desc.bEndpointAddress);
 798
 799                dev->ts1_mode.num_alt = uif->num_altsetting;
 800                cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
 801                             dev->ts1_mode.end_point_addr,
 802                             dev->ts1_mode.num_alt);
 803                dev->ts1_mode.alt_max_pkt_size =
 804                        kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
 805
 806                if (dev->ts1_mode.alt_max_pkt_size == NULL) {
 807                        cx231xx_errdev("out of memory!\n");
 808                        cx231xx_devused &= ~(1 << nr);
 809                        v4l2_device_unregister(&dev->v4l2_dev);
 810                        kfree(dev);
 811                        return -ENOMEM;
 812                }
 813
 814                for (i = 0; i < dev->ts1_mode.num_alt; i++) {
 815                        u16 tmp = le16_to_cpu(uif->altsetting[i].
 816                                                endpoint[isoc_pipe].desc.
 817                                                wMaxPacketSize);
 818                        dev->ts1_mode.alt_max_pkt_size[i] =
 819                            (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
 820                        cx231xx_info("Alternate setting %i, max size= %i\n", i,
 821                                     dev->ts1_mode.alt_max_pkt_size[i]);
 822                }
 823        }
 824
 825        /* load other modules required */
 826        request_modules(dev);
 827
 828        return 0;
 829}
 830
 831/*
 832 * cx231xx_usb_disconnect()
 833 * called when the device gets diconencted
 834 * video device will be unregistered on v4l2_close in case it is still open
 835 */
 836static void cx231xx_usb_disconnect(struct usb_interface *interface)
 837{
 838        struct cx231xx *dev;
 839
 840        dev = usb_get_intfdata(interface);
 841        usb_set_intfdata(interface, NULL);
 842
 843        if (!dev)
 844                return;
 845
 846        if (!dev->udev)
 847                return;
 848
 849        /* delete v4l2 device */
 850        v4l2_device_unregister(&dev->v4l2_dev);
 851
 852        /* wait until all current v4l2 io is finished then deallocate
 853           resources */
 854        mutex_lock(&dev->lock);
 855
 856        wake_up_interruptible_all(&dev->open);
 857
 858        if (dev->users) {
 859                cx231xx_warn
 860                    ("device %s is open! Deregistration and memory "
 861                     "deallocation are deferred on close.\n",
 862                     video_device_node_name(dev->vdev));
 863
 864                dev->state |= DEV_MISCONFIGURED;
 865                cx231xx_uninit_isoc(dev);
 866                dev->state |= DEV_DISCONNECTED;
 867                wake_up_interruptible(&dev->wait_frame);
 868                wake_up_interruptible(&dev->wait_stream);
 869        } else {
 870                dev->state |= DEV_DISCONNECTED;
 871                cx231xx_release_resources(dev);
 872        }
 873
 874        cx231xx_close_extension(dev);
 875
 876        mutex_unlock(&dev->lock);
 877
 878        if (!dev->users) {
 879                kfree(dev->video_mode.alt_max_pkt_size);
 880                kfree(dev->vbi_mode.alt_max_pkt_size);
 881                kfree(dev->sliced_cc_mode.alt_max_pkt_size);
 882                kfree(dev->ts1_mode.alt_max_pkt_size);
 883                kfree(dev);
 884        }
 885}
 886
 887static struct usb_driver cx231xx_usb_driver = {
 888        .name = "cx231xx",
 889        .probe = cx231xx_usb_probe,
 890        .disconnect = cx231xx_usb_disconnect,
 891        .id_table = cx231xx_id_table,
 892};
 893
 894static int __init cx231xx_module_init(void)
 895{
 896        int result;
 897
 898        printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
 899
 900        /* register this driver with the USB subsystem */
 901        result = usb_register(&cx231xx_usb_driver);
 902        if (result)
 903                cx231xx_err(DRIVER_NAME
 904                            " usb_register failed. Error number %d.\n", result);
 905
 906        return result;
 907}
 908
 909static void __exit cx231xx_module_exit(void)
 910{
 911        /* deregister this driver with the USB subsystem */
 912        usb_deregister(&cx231xx_usb_driver);
 913}
 914
 915module_init(cx231xx_module_init);
 916module_exit(cx231xx_module_exit);
 917