cx231xx-cards.c 25.0 KB
Newer Older
1
/*
2 3
   cx231xx-cards.c - driver for Conexant Cx23100/101/102
				USB video capture devices
4 5

   Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6
				Based on em28xx driver
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/usb.h>
#include <media/tuner.h>
#include <media/tveeprom.h>
#include <media/v4l2-common.h>
#include <media/v4l2-chip-ident.h>

#include <media/cx25840.h>
#include "xc5000.h"

#include "cx231xx.h"

static int tuner = -1;
module_param(tuner, int, 0444);
MODULE_PARM_DESC(tuner, "tuner type");

static unsigned int disable_ir;
module_param(disable_ir, int, 0444);
MODULE_PARM_DESC(disable_ir, "disable infrared remote support");

/* Bitmask marking allocated devices from 0 to CX231XX_MAXBOARDS */
static unsigned long cx231xx_devused;

/*
 *  Reset sequences for analog/digital modes
 */

static struct cx231xx_reg_seq RDE250_XCV_TUNER[] = {
54 55 56 57
	{0x03, 0x01, 10},
	{0x03, 0x00, 30},
	{0x03, 0x01, 10},
	{-1, -1, -1},
58 59 60 61 62 63 64 65
};

/*
 *  Board definitions
 */
struct cx231xx_board cx231xx_boards[] = {

	[CX231XX_BOARD_UNKNOWN] = {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
				   .name = "Unknown CX231xx video grabber",
				   .tuner_type = TUNER_ABSENT,
				   .input = {{
					      .type = CX231XX_VMUX_TELEVISION,
					      .vmux = CX231XX_VIN_3_1,
					      .amux = CX231XX_AMUX_VIDEO,
					      .gpio = 0,
					      }, {
						  .type =
						  CX231XX_VMUX_COMPOSITE1,
						  .vmux = CX231XX_VIN_2_1,
						  .amux = CX231XX_AMUX_LINE_IN,
						  .gpio = 0,
						  }, {
						      .type =
						      CX231XX_VMUX_SVIDEO,
						      .vmux =
						      CX231XX_VIN_1_1 |
						      (CX231XX_VIN_1_2 << 8) |
						      CX25840_SVIDEO_ON,
						      .amux =
						      CX231XX_AMUX_LINE_IN,
						      .gpio = 0,
89
						      } },
90
				   },
91 92

	[CX231XX_BOARD_CNXT_RDE_250] = {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
					.name = "Conexant Hybrid TV - RDE250",
					.valid = CX231XX_BOARD_VALIDATED,
					.tuner_type = TUNER_XC5000,
					.tuner_addr = 0x61,
					.tuner_gpio = RDE250_XCV_TUNER,
					.tuner_sif_gpio = 0x05,
					.tuner_scl_gpio = 0x1a,
					.tuner_sda_gpio = 0x1b,
					.decoder = CX231XX_AVDECODER,
					.demod_xfer_mode = 0,
					.ctl_pin_status_mask = 0xFFFFFFC4,
					.agc_analog_digital_select_gpio = 0x0c,
					.gpio_pin_status_mask = 0x4001000,
					.tuner_i2c_master = 1,
					.demod_i2c_master = 2,
					.has_dvb = 1,
					.demod_addr = 0x02,
					.norm = V4L2_STD_PAL,

					.input = {{
						   .type =
						   CX231XX_VMUX_TELEVISION,
						   .vmux = CX231XX_VIN_3_1,
						   .amux = CX231XX_AMUX_VIDEO,
						   .gpio = 0,
						   }, {
						       .type =
						       CX231XX_VMUX_COMPOSITE1,
						       .vmux = CX231XX_VIN_2_1,
						       .amux =
						       CX231XX_AMUX_LINE_IN,
						       .gpio = 0,
						       }, {
							   .type =
							   CX231XX_VMUX_SVIDEO,
							   .vmux =
							   CX231XX_VIN_1_1 |
							   (CX231XX_VIN_1_2 <<
							    8) |
							   CX25840_SVIDEO_ON,
							   .amux =
							   CX231XX_AMUX_LINE_IN,
							   .gpio = 0,
136
							   } },
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
					},

	[CX231XX_BOARD_CNXT_RDU_250] = {
					.name = "Conexant Hybrid TV - RDU250",
					.valid = CX231XX_BOARD_VALIDATED,
					.tuner_type = TUNER_XC5000,
					.tuner_addr = 0x61,
					.tuner_gpio = RDE250_XCV_TUNER,
					.tuner_sif_gpio = 0x05,
					.tuner_scl_gpio = 0x1a,
					.tuner_sda_gpio = 0x1b,
					.decoder = CX231XX_AVDECODER,
					.demod_xfer_mode = 0,
					.ctl_pin_status_mask = 0xFFFFFFC4,
					.agc_analog_digital_select_gpio = 0x0c,
					.gpio_pin_status_mask = 0x4001000,
					.tuner_i2c_master = 1,
					.demod_i2c_master = 2,
					.has_dvb = 1,
					.demod_addr = 0x32,
					.norm = V4L2_STD_NTSC,

					.input = {{
						   .type =
						   CX231XX_VMUX_TELEVISION,
						   .vmux = CX231XX_VIN_3_1,
						   .amux = CX231XX_AMUX_VIDEO,
						   .gpio = 0,
						   }, {
						       .type =
						       CX231XX_VMUX_COMPOSITE1,
						       .vmux = CX231XX_VIN_2_1,
						       .amux =
						       CX231XX_AMUX_LINE_IN,
						       .gpio = 0,
						       }, {
							   .type =
							   CX231XX_VMUX_SVIDEO,
							   .vmux =
							   CX231XX_VIN_1_1 |
							   (CX231XX_VIN_1_2 <<
							    8) |
							   CX25840_SVIDEO_ON,
							   .amux =
							   CX231XX_AMUX_LINE_IN,
							   .gpio = 0,
183
							   } },
184
					},
185 186 187 188
};
const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);

/* table of devices that work with this driver */
189 190 191 192 193 194 195 196
struct usb_device_id cx231xx_id_table[] = {
	{USB_DEVICE(0x0572, 0x58A0),
	 .driver_info = CX231XX_BOARD_UNKNOWN},
	{USB_DEVICE(0x0572, 0x58A2),
	 .driver_info = CX231XX_BOARD_CNXT_RDE_250},
	{USB_DEVICE(0x0572, 0x5A3C),
	 .driver_info = CX231XX_BOARD_CNXT_RDU_250},
	{},
197
};
198

199 200 201 202 203 204 205 206 207 208 209
MODULE_DEVICE_TABLE(usb, cx231xx_id_table);

/* cx231xx_tuner_callback
 * will be used to reset XC5000 tuner using GPIO pin
 */

int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
{
	int rc = 0;
	struct cx231xx *dev = ptr;

210 211 212
	if (dev->tuner_type == TUNER_XC5000) {
		if (command == XC5000_TUNER_RESET) {
			cx231xx_info
213 214
				("Tuner CB: RESET: cmd %d : tuner type %d \n",
				 command, dev->tuner_type);
215 216 217 218 219 220 221 222 223 224 225
			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
					       1);
			msleep(10);
			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
					       0);
			msleep(330);
			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
					       1);
			msleep(10);
		}
	}
226 227 228 229
	return rc;
}
EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);

230
static inline void cx231xx_set_model(struct cx231xx *dev)
231 232 233 234 235 236 237 238 239 240 241 242 243
{
	memcpy(&dev->board, &cx231xx_boards[dev->model], sizeof(dev->board));
}

/* Since cx231xx_pre_card_setup() requires a proper dev->model,
 * this won't work for boards with generic PCI IDs
 */
void cx231xx_pre_card_setup(struct cx231xx *dev)
{

	cx231xx_set_model(dev);

	cx231xx_info("Identified as %s (card=%d)\n",
244
		     dev->board.name, dev->model);
245 246 247

	/* Do card specific if any */
	switch (dev->model) {
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	case CX231XX_BOARD_CNXT_RDE_250:
		/* do card specific GPIO settings if required */
		cx231xx_info("Precard: Board is Conexnat RDE 250\n");
		/* set the direction for GPIO pins */
		cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
		cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
		cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
		break;
	case CX231XX_BOARD_CNXT_RDU_250:
		/* do card specific GPIO settings if required */
		cx231xx_info("Precard: Board is Conexnat RDU 250\n");
		/* set the direction for GPIO pins */
		cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
		cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
		cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
		break;
264 265 266 267
	}

	/* request some modules if any required */

268
	/* reset the Tuner */
269 270
	cx231xx_gpio_set(dev, dev->board.tuner_gpio);

271
	/* set the mode to Analog mode initially */
272 273 274 275 276 277 278 279 280 281 282
	cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);

	/* Unlock device */
	/* cx231xx_set_mode(dev, CX231XX_SUSPEND); */

}

#if 0

static void cx231xx_config_tuner(struct cx231xx *dev)
{
283 284
	struct tuner_setup tun_setup;
	struct v4l2_frequency f;
285 286 287 288 289 290 291 292 293

	if (dev->tuner_type == TUNER_ABSENT)
		return;

	tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
	tun_setup.type = dev->tuner_type;
	tun_setup.addr = dev->tuner_addr;
	tun_setup.tuner_callback = cx231xx_tuner_callback;

294 295
	cx231xx_i2c_call_clients(&dev->i2c_bus[1], TUNER_SET_TYPE_ADDR,
				 &tun_setup);
296
#if 0
297
	if (tun_setup.type == TUNER_XC5000) {
298 299 300
		static struct xc2028_ctrl ctrl = {
			.fname = XC5000_DEFAULT_FIRMWARE,
			.max_len = 64,
301
			.demod = 0;
302 303 304 305 306
		};
		struct v4l2_priv_tun_config cfg = {
			.tuner = dev->tuner_type,
			.priv = &ctrl,
		};
307 308
		cx231xx_i2c_call_clients(&dev->i2c_bus[1], TUNER_SET_CONFIG,
					 &cfg);
309 310 311 312 313 314
	}
#endif

	/* configure tuner */
	f.tuner = 0;
	f.type = V4L2_TUNER_ANALOG_TV;
315
	f.frequency = 9076;	/* just a magic number */
316 317 318 319 320 321 322 323 324 325 326
	dev->ctl_freq = f.frequency;
	cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
}

#endif

/* ----------------------------------------------------------------------- */
void cx231xx_set_ir(struct cx231xx *dev, struct IR_i2c *ir)
{
	if (disable_ir) {
		ir->get_key = NULL;
327
		return;
328 329 330 331 332
	}

	/* detect & configure */
	switch (dev->model) {

333 334 335 336 337 338
	case CX231XX_BOARD_CNXT_RDE_250:
		break;
	case CX231XX_BOARD_CNXT_RDU_250:
		break;
	default:
		break;
339 340 341 342 343 344 345 346 347 348 349
	}
}

void cx231xx_card_setup(struct cx231xx *dev)
{
	cx231xx_set_model(dev);

	dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
	if (cx231xx_boards[dev->model].tuner_addr)
		dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;

350 351
	cx231xx_info(": tuner type %d, tuner address %d \n",
		     dev->tuner_type, dev->tuner_addr);
352 353 354

	/* Do card specific if any */
	switch (dev->model) {
355 356 357 358 359 360 361 362
	case CX231XX_BOARD_CNXT_RDE_250:
		/* do card specific GPIO settings if required */
		cx231xx_info("Board is Conexnat RDE 250\n");
		break;
	case CX231XX_BOARD_CNXT_RDU_250:
		/* do card specific GPIO settings if required */
		cx231xx_info("Board is Conexnat RDU 250\n");
		break;
363 364 365 366 367
	}

	if (dev->board.valid == CX231XX_BOARD_NOT_VALIDATED) {
		cx231xx_errdev("\n\n");
		cx231xx_errdev("The support for this board weren't "
368
			       "valid yet.\n");
369 370
		cx231xx_errdev("Please send a report of having this working\n");
		cx231xx_errdev("not to V4L mailing list (and/or to other "
371
			       "addresses)\n\n");
372 373 374
	}

	/* request some modules */
375 376
	if (dev->board.decoder == CX231XX_AVDECODER) {
		cx231xx_info(": Requesting cx25840 module\n");
377
		request_module("cx25840");
378
	}
379
#if 0
380 381
	if (dev->board.tuner_type != TUNER_ABSENT) {
		cx231xx_info(": Requesting Tuner module\n");
382
		request_module("tuner");
383
	}
384 385 386

	cx231xx_config_tuner(dev);

387
	/* TBD  IR will be added later */
388 389 390 391 392 393 394 395 396 397
	cx231xx_ir_init(dev);
#endif
}

/*
 * cx231xx_config()
 * inits registers with sane defaults
 */
int cx231xx_config(struct cx231xx *dev)
{
398
	/* TBD need to add cx231xx specific code */
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	dev->mute = 1;		/* maybe not the right place... */
	dev->volume = 0x1f;

	return 0;
}

/*
 * cx231xx_config_i2c()
 * configure i2c attached devices
 */
void cx231xx_config_i2c(struct cx231xx *dev)
{
	struct v4l2_routing route;

	route.input = INPUT(dev->video_input)->vmux;
	route.output = 0;

	cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_STREAMON, NULL);
}

/*
 * cx231xx_realease_resources()
 * unregisters the v4l2,i2c and usb devices
 * called when the device gets disconected or at module unload
*/
void cx231xx_release_resources(struct cx231xx *dev)
{

427
#if 0		/* TBD IR related  */
428 429 430 431 432 433
	if (dev->ir)
		cx231xx_ir_fini(dev);
#endif

	cx231xx_release_analog_resources(dev);

434
	cx231xx_remove_from_devlist(dev);
435

436
	cx231xx_dev_uninit(dev);
437 438 439 440

	usb_put_dev(dev->udev);

	/* Mark device as unused */
441
	cx231xx_devused &= ~(1 << dev->devno);
442 443 444 445 446 447 448
}

/*
 * cx231xx_init_dev()
 * allocates and inits the device structs, registers i2c bus and v4l device
 */
static int cx231xx_init_dev(struct cx231xx **devhandle, struct usb_device *udev,
449
			    int minor)
450 451 452 453 454 455 456 457 458
{
	struct cx231xx *dev = *devhandle;
	int retval = -ENOMEM;
	int errCode;
	unsigned int maxh, maxw;

	dev->udev = udev;
	mutex_init(&dev->lock);
	mutex_init(&dev->ctrl_urb_lock);
459
	mutex_init(&dev->gpio_i2c_lock);
460

461 462 463
	spin_lock_init(&dev->video_mode.slock);
	spin_lock_init(&dev->vbi_mode.slock);
	spin_lock_init(&dev->sliced_cc_mode.slock);
464 465 466 467 468

	init_waitqueue_head(&dev->open);
	init_waitqueue_head(&dev->wait_frame);
	init_waitqueue_head(&dev->wait_stream);

469 470 471 472 473
	dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
	dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
	dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
	dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
	dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
474

475 476
	/* Query cx231xx to find what pcb config it is related to */
	initialize_cx231xx(dev);
477

478
	/* Cx231xx pre card setup */
479 480 481 482 483 484 485 486
	cx231xx_pre_card_setup(dev);

	errCode = cx231xx_config(dev);
	if (errCode) {
		cx231xx_errdev("error configuring device\n");
		return -ENOMEM;
	}

487
	/* set default norm */
488 489 490 491 492 493
	dev->norm = dev->board.norm;

	/* register i2c bus */
	errCode = cx231xx_dev_init(dev);
	if (errCode < 0) {
		cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
494
			       __func__, errCode);
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
		return errCode;
	}

	/* Do board specific init */
	cx231xx_card_setup(dev);

	/* configure the device */
	cx231xx_config_i2c(dev);

	maxw = norm_maxw(dev);
	maxh = norm_maxh(dev);

	/* set default image size */
	dev->width = maxw;
	dev->height = maxh;
	dev->interlaced = 0;
	dev->hscale = 0;
	dev->vscale = 0;
	dev->video_input = 0;

	errCode = cx231xx_config(dev);
	if (errCode < 0) {
		cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
518
			       __func__, errCode);
519 520 521 522 523 524 525
		return errCode;
	}

	/* init video dma queues */
	INIT_LIST_HEAD(&dev->video_mode.vidq.active);
	INIT_LIST_HEAD(&dev->video_mode.vidq.queued);

526
	/* init vbi dma queues */
527 528 529 530 531
	INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
	INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);

	/* Reset other chips required if they are tied up with GPIO pins */

532
	cx231xx_add_into_devlist(dev);
533 534 535 536 537 538 539 540 541 542 543

	retval = cx231xx_register_analog_devices(dev);
	if (retval < 0) {
		cx231xx_release_resources(dev);
		goto fail_reg_devices;
	}

	cx231xx_init_extension(dev);

	return 0;

544
fail_reg_devices:
545 546 547 548 549 550 551 552
	mutex_unlock(&dev->lock);
	return retval;
}

#if defined(CONFIG_MODULES) && defined(MODULE)
static void request_module_async(struct work_struct *work)
{
	struct cx231xx *dev = container_of(work,
553
					   struct cx231xx, request_module_wk);
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

	if (dev->has_alsa_audio)
		request_module("cx231xx-alsa");

	if (dev->board.has_dvb)
		request_module("cx231xx-dvb");

}

static void request_modules(struct cx231xx *dev)
{
	INIT_WORK(&dev->request_module_wk, request_module_async);
	schedule_work(&dev->request_module_wk);
}
#else
#define request_modules(dev)
#endif /* CONFIG_MODULES */

/*
 * cx231xx_usb_probe()
 * checks for supported devices
 */
static int cx231xx_usb_probe(struct usb_interface *interface,
577
			     const struct usb_device_id *id)
578 579 580 581 582
{
	struct usb_device *udev;
	struct usb_interface *uif;
	struct cx231xx *dev = NULL;
	int retval = -ENODEV;
583
	int nr, ifnum;
584 585 586
	int i, isoc_pipe = 0;
	char *speed;
	char descr[255] = "";
587 588 589
	struct usb_interface *lif = NULL;
	int skip_interface = 0;
	struct usb_interface_assoc_descriptor *assoc_desc;
590 591 592 593

	udev = usb_get_dev(interface_to_usbdev(interface));
	ifnum = interface->altsetting[0].desc.bInterfaceNumber;

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
	cx231xx_info(": Interface Number %d\n", ifnum);

	/* Interface number 0 - IR interface */
	if (ifnum == 0) {
		/* Check to see next free device and mark as used */
		nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
		cx231xx_devused |= 1 << nr;

		if (nr >= CX231XX_MAXBOARDS) {
			cx231xx_info(": Supports only %i cx231xx boards.\n",
				     CX231XX_MAXBOARDS);
			cx231xx_devused &= ~(1 << nr);
			return -ENOMEM;
		}

		/* allocate memory for our device state and initialize it */
		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
		if (dev == NULL) {
			cx231xx_err(DRIVER_NAME ": out of memory!\n");
			cx231xx_devused &= ~(1 << nr);
			return -ENOMEM;
		}

		snprintf(dev->name, 29, "cx231xx #%d", nr);
		dev->devno = nr;
		dev->model = id->driver_info;
		dev->video_mode.alt = -1;
		dev->interface_count++;

		/* reset gpio dir and value */
		dev->gpio_dir = 0;
		dev->gpio_val = 0;
		dev->xc_fw_load_done = 0;
627
		dev->has_alsa_audio = 1;
628 629
		dev->power_mode = -1;

630 631
		/* 0 - vbi ; 1 -sliced cc mode */
		dev->vbi_or_sliced_cc_mode = 0;
632 633 634 635

		/* get maximum no.of IAD interfaces */
		assoc_desc = udev->actconfig->intf_assoc[0];
		dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
636
		cx231xx_info("Found IAD interface count %d\n",
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
			     dev->max_iad_interface_count);

		/* init CIR module TBD */

		/* store the current interface */
		lif = interface;

	} else if (ifnum == 1) {

		/* Get dev structure first */
		dev = usb_get_intfdata(udev->actconfig->interface[0]);
		if (dev == NULL) {
			cx231xx_err(DRIVER_NAME ": out of first interface!\n");
			return -ENODEV;
		}

		/* store the interface 0 back */
		lif = udev->actconfig->interface[0];

		/* increment interface count */
		dev->interface_count++;

		/* get device number */
		nr = dev->devno;

		assoc_desc = udev->actconfig->intf_assoc[0];
		if (assoc_desc->bFirstInterface == ifnum) {
			cx231xx_info
665
			("Found IAD interface match: AV Desc Start!! \n");
666
		} else {
667
			cx231xx_err(" Not found matching interface\n");
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
			return -ENODEV;
		}

	} else if (ifnum >= 2) {
		/* Get dev structure first */
		dev = usb_get_intfdata(udev->actconfig->interface[0]);
		if (dev == NULL) {
			cx231xx_err(DRIVER_NAME ": out of first interface!\n");
			return -ENODEV;
		}

		/* store the interface 0 back */
		lif = udev->actconfig->interface[0];

		/* increment interface count */
		dev->interface_count++;

		/* get device number */
		nr = dev->devno;

		/* set skip interface */
		if ((dev->interface_count - 1) != dev->max_iad_interface_count)
			skip_interface = 1;	/* set skipping */
		else {
			cx231xx_info
693
			("Found IAD interface no. match with AV Device no.!\n");
694 695
		}
	}
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

	switch (udev->speed) {
	case USB_SPEED_LOW:
		speed = "1.5";
		break;
	case USB_SPEED_UNKNOWN:
	case USB_SPEED_FULL:
		speed = "12";
		break;
	case USB_SPEED_HIGH:
		speed = "480";
		break;
	default:
		speed = "unknown";
	}

	if (udev->manufacturer)
		strlcpy(descr, udev->manufacturer, sizeof(descr));

	if (udev->product) {
		if (*descr)
			strlcat(descr, " ", sizeof(descr));
		strlcat(descr, udev->product, sizeof(descr));
	}
	if (*descr)
		strlcat(descr, " ", sizeof(descr));

	cx231xx_info("New device %s@ %s Mbps "
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
		     "(%04x:%04x, interface %d, class %d)\n",
		     descr,
		     speed,
		     le16_to_cpu(udev->descriptor.idVendor),
		     le16_to_cpu(udev->descriptor.idProduct),
		     ifnum, interface->altsetting->desc.bInterfaceNumber);

	/* AV device initialization */
	if ((dev->interface_count - 1) == dev->max_iad_interface_count) {
		cx231xx_info(" Calling init_dev\n");
		/* allocate device struct */
		retval = cx231xx_init_dev(&dev, udev, nr);
		if (retval) {
			cx231xx_devused &= ~(1 << dev->devno);
			kfree(dev);

			return retval;
		}

		/* compute alternate max packet sizes for video */
		uif =
		    udev->actconfig->interface[dev->current_pcb_config.
					       hs_config_info[0].interface_info.
					       video_index + 1];

		dev->video_mode.end_point_addr =
		    le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
				bEndpointAddress);

		dev->video_mode.num_alt = uif->num_altsetting;
		cx231xx_info(": EndPoint Addr 0x%x, Alternate settings: %i\n",
			     dev->video_mode.end_point_addr,
			     dev->video_mode.num_alt);
		dev->video_mode.alt_max_pkt_size =
		    kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);

		if (dev->video_mode.alt_max_pkt_size == NULL) {
			cx231xx_errdev("out of memory!\n");
			cx231xx_devused &= ~(1 << nr);
			kfree(dev);
			return -ENOMEM;
		}

		for (i = 0; i < dev->video_mode.num_alt; i++) {
			u16 tmp =
			    le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
					desc.wMaxPacketSize);
			dev->video_mode.alt_max_pkt_size[i] =
			    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
			cx231xx_info("Alternate setting %i, max size= %i\n", i,
				     dev->video_mode.alt_max_pkt_size[i]);
		}

		/* compute alternate max packet sizes for vbi */
		uif =
		    udev->actconfig->interface[dev->current_pcb_config.
					       hs_config_info[0].interface_info.
					       vanc_index + 1];

		dev->vbi_mode.end_point_addr =
		    le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
				bEndpointAddress);

		dev->vbi_mode.num_alt = uif->num_altsetting;
		cx231xx_info(": EndPoint Addr 0x%x, Alternate settings: %i\n",
			     dev->vbi_mode.end_point_addr,
			     dev->vbi_mode.num_alt);
		dev->vbi_mode.alt_max_pkt_size =
		    kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);

		if (dev->vbi_mode.alt_max_pkt_size == NULL) {
			cx231xx_errdev("out of memory!\n");
			cx231xx_devused &= ~(1 << nr);
			kfree(dev);
			return -ENOMEM;
		}

		for (i = 0; i < dev->vbi_mode.num_alt; i++) {
			u16 tmp =
			    le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
					desc.wMaxPacketSize);
			dev->vbi_mode.alt_max_pkt_size[i] =
			    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
			cx231xx_info("Alternate setting %i, max size= %i\n", i,
				     dev->vbi_mode.alt_max_pkt_size[i]);
		}

		/* compute alternate max packet sizes for sliced CC */
		uif =
		    udev->actconfig->interface[dev->current_pcb_config.
					       hs_config_info[0].interface_info.
					       hanc_index + 1];

		dev->sliced_cc_mode.end_point_addr =
		    le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
				bEndpointAddress);

		dev->sliced_cc_mode.num_alt = uif->num_altsetting;
		cx231xx_info(": EndPoint Addr 0x%x, Alternate settings: %i\n",
			     dev->sliced_cc_mode.end_point_addr,
			     dev->sliced_cc_mode.num_alt);
		dev->sliced_cc_mode.alt_max_pkt_size =
		    kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);

		if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
			cx231xx_errdev("out of memory!\n");
			cx231xx_devused &= ~(1 << nr);
			kfree(dev);
			return -ENOMEM;
		}

		for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
			u16 tmp =
			    le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
					desc.wMaxPacketSize);
			dev->sliced_cc_mode.alt_max_pkt_size[i] =
			    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
			cx231xx_info("Alternate setting %i, max size= %i\n", i,
				     dev->sliced_cc_mode.alt_max_pkt_size[i]);
		}

		if (dev->current_pcb_config.ts1_source != 0xff) {

			/* compute alternate max packet sizes for TS1 */
			uif =
			    udev->actconfig->interface[dev->current_pcb_config.
						       hs_config_info[0].
						       interface_info.
						       ts1_index + 1];

			dev->ts1_mode.end_point_addr =
			    le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
					desc.bEndpointAddress);

			dev->ts1_mode.num_alt = uif->num_altsetting;
			cx231xx_info
			    (": EndPoint Addr 0x%x, Alternate settings: %i\n",
			     dev->ts1_mode.end_point_addr,
			     dev->ts1_mode.num_alt);
			dev->ts1_mode.alt_max_pkt_size =
			    kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);

			if (dev->ts1_mode.alt_max_pkt_size == NULL) {
				cx231xx_errdev("out of memory!\n");
				cx231xx_devused &= ~(1 << nr);
				kfree(dev);
				return -ENOMEM;
			}

			for (i = 0; i < dev->ts1_mode.num_alt; i++) {
				u16 tmp =
				    le16_to_cpu(uif->altsetting[i].
						endpoint[isoc_pipe].desc.
						wMaxPacketSize);
				dev->ts1_mode.alt_max_pkt_size[i] =
				    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) +
						      1);
				cx231xx_info
				    ("Alternate setting %i, max size= %i\n", i,
				     dev->ts1_mode.alt_max_pkt_size[i]);
			}
		}

	}
888 889 890 891

	/* save our data pointer in this interface device */
	usb_set_intfdata(lif, dev);

892 893 894 895 896
	/* load other modules required */
	if ((dev->interface_count - 1) == dev->max_iad_interface_count) {
		cx231xx_info("Calling request modules\n");
		request_modules(dev);
	}
897

898 899 900 901
	if (skip_interface) {
		cx231xx_info("Skipping the interface\n");
		return -ENODEV;
	}
902 903 904 905 906 907 908 909 910 911 912 913 914

	return 0;
}

/*
 * cx231xx_usb_disconnect()
 * called when the device gets diconencted
 * video device will be unregistered on v4l2_close in case it is still open
 */
static void cx231xx_usb_disconnect(struct usb_interface *interface)
{
	struct cx231xx *dev;

915
	dev = usb_get_intfdata(interface);
916 917 918 919 920 921 922 923 924 925 926 927 928 929
	usb_set_intfdata(interface, NULL);

	if (!dev)
		return;

	/* wait until all current v4l2 io is finished then deallocate
	   resources */
	mutex_lock(&dev->lock);

	wake_up_interruptible_all(&dev->open);

	if (dev->users) {
		cx231xx_warn
		    ("device /dev/video%d is open! Deregistration and memory "
930
		     "deallocation are deferred on close.\n", dev->vdev->num);
931 932 933 934 935 936 937 938 939 940 941

		dev->state |= DEV_MISCONFIGURED;
		cx231xx_uninit_isoc(dev);
		dev->state |= DEV_DISCONNECTED;
		wake_up_interruptible(&dev->wait_frame);
		wake_up_interruptible(&dev->wait_stream);
	} else {
		dev->state |= DEV_DISCONNECTED;
		cx231xx_release_resources(dev);
	}

942
	cx231xx_close_extension(dev);
943 944 945 946 947

	mutex_unlock(&dev->lock);

	if (!dev->users) {
		kfree(dev->video_mode.alt_max_pkt_size);
948 949 950
		kfree(dev->vbi_mode.alt_max_pkt_size);
		kfree(dev->sliced_cc_mode.alt_max_pkt_size);
		kfree(dev->ts1_mode.alt_max_pkt_size);
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
		kfree(dev);
	}
}

static struct usb_driver cx231xx_usb_driver = {
	.name = "cx231xx",
	.probe = cx231xx_usb_probe,
	.disconnect = cx231xx_usb_disconnect,
	.id_table = cx231xx_id_table,
};

static int __init cx231xx_module_init(void)
{
	int result;

966
	printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
967 968 969 970 971

	/* register this driver with the USB subsystem */
	result = usb_register(&cx231xx_usb_driver);
	if (result)
		cx231xx_err(DRIVER_NAME
972
			    " usb_register failed. Error number %d.\n", result);
973 974 975 976 977 978 979 980 981 982 983 984

	return result;
}

static void __exit cx231xx_module_exit(void)
{
	/* deregister this driver with the USB subsystem */
	usb_deregister(&cx231xx_usb_driver);
}

module_init(cx231xx_module_init);
module_exit(cx231xx_module_exit);