cx231xx-cards.c 36.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

   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>
25
#include <linux/slab.h>
26 27 28 29 30 31 32 33 34
#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>
35
#include "dvb-usb-ids.h"
36
#include "xc5000.h"
37
#include "tda18271.h"
38 39 40 41 42 43 44

#include "cx231xx.h"

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

45 46 47 48
static int transfer_mode = 1;
module_param(transfer_mode, int, 0444);
MODULE_PARM_DESC(transfer_mode, "transfer mode (1-ISO or 0-BULK)");

49 50 51 52 53 54 55 56 57 58 59 60
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[] = {
61 62 63 64
	{0x03, 0x01, 10},
	{0x03, 0x00, 30},
	{0x03, 0x01, 10},
	{-1, -1, -1},
65 66 67 68 69 70 71
};

/*
 *  Board definitions
 */
struct cx231xx_board cx231xx_boards[] = {
	[CX231XX_BOARD_UNKNOWN] = {
72 73 74 75 76 77
		.name = "Unknown CX231xx video grabber",
		.tuner_type = TUNER_ABSENT,
		.input = {{
				.type = CX231XX_VMUX_TELEVISION,
				.vmux = CX231XX_VIN_3_1,
				.amux = CX231XX_AMUX_VIDEO,
78
				.gpio = NULL,
79 80 81 82
			}, {
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
83
				.gpio = NULL,
84 85 86 87 88 89
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
90
				.gpio = NULL,
91 92 93
			}
		},
	},
94 95
	[CX231XX_BOARD_CNXT_CARRAERA] = {
		.name = "Conexant Hybrid TV - CARRAERA",
96 97 98 99 100 101 102
		.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,
103
		.output_mode = OUT_MODE_VIP11,
104 105 106 107 108 109 110 111 112 113 114 115 116 117
		.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,
118
				.gpio = NULL,
119 120 121 122
			}, {
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
123
				.gpio = NULL,
124 125 126 127 128 129
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
130
				.gpio = NULL,
131 132 133
			}
		},
	},
134 135
	[CX231XX_BOARD_CNXT_SHELBY] = {
		.name = "Conexant Hybrid TV - SHELBY",
136 137 138 139 140 141 142
		.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,
143
		.output_mode = OUT_MODE_VIP11,
144 145 146 147 148 149 150 151 152 153 154 155 156 157
		.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,
158
				.gpio = NULL,
159 160 161 162
			}, {
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
163
				.gpio = NULL,
164 165 166 167 168 169
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
170
				.gpio = NULL,
171 172 173
			}
		},
	},
174 175 176 177 178 179 180 181 182
	[CX231XX_BOARD_CNXT_RDE_253S] = {
		.name = "Conexant Hybrid TV - RDE253S",
		.tuner_type = TUNER_NXP_TDA18271,
		.tuner_addr = 0x60,
		.tuner_gpio = RDE250_XCV_TUNER,
		.tuner_sif_gpio = 0x05,
		.tuner_scl_gpio = 0x1a,
		.tuner_sda_gpio = 0x1b,
		.decoder = CX231XX_AVDECODER,
183
		.output_mode = OUT_MODE_VIP11,
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x1c,
		.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 = NULL,
			}, {
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
				.gpio = NULL,
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
				.gpio = NULL,
			}
		},
	},

	[CX231XX_BOARD_CNXT_RDU_253S] = {
		.name = "Conexant Hybrid TV - RDU253S",
		.tuner_type = TUNER_NXP_TDA18271,
		.tuner_addr = 0x60,
		.tuner_gpio = RDE250_XCV_TUNER,
		.tuner_sif_gpio = 0x05,
		.tuner_scl_gpio = 0x1a,
		.tuner_sda_gpio = 0x1b,
		.decoder = CX231XX_AVDECODER,
224
		.output_mode = OUT_MODE_VIP11,
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x1c,
		.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 = NULL,
			}, {
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
				.gpio = NULL,
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
				.gpio = NULL,
			}
		},
	},
	[CX231XX_BOARD_CNXT_VIDEO_GRABBER] = {
		.name = "Conexant VIDEO GRABBER",
257
		.tuner_type = TUNER_ABSENT,
258
		.decoder = CX231XX_AVDECODER,
259
		.output_mode = OUT_MODE_VIP11,
260 261 262 263
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x1c,
		.gpio_pin_status_mask = 0x4001000,
		.norm = V4L2_STD_PAL,
264 265 266
		.no_alt_vanc = 1,
		.external_av = 1,
		.has_417 = 1,
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

		.input = {{
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
				.gpio = NULL,
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
				.gpio = NULL,
			}
		},
	},
	[CX231XX_BOARD_CNXT_RDE_250] = {
		.name = "Conexant Hybrid TV - rde 250",
		.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,
292
		.output_mode = OUT_MODE_VIP11,
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		.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_2_1,
				.amux = CX231XX_AMUX_VIDEO,
				.gpio = NULL,
			}
		},
	},
	[CX231XX_BOARD_CNXT_RDU_250] = {
		.name = "Conexant Hybrid TV - RDU 250",
		.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,
320
		.output_mode = OUT_MODE_VIP11,
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
		.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_2_1,
				.amux = CX231XX_AMUX_VIDEO,
				.gpio = NULL,
			}
		},
	},
339 340 341 342 343 344 345 346 347
	[CX231XX_BOARD_HAUPPAUGE_EXETER] = {
		.name = "Hauppauge EXETER",
		.tuner_type = TUNER_NXP_TDA18271,
		.tuner_addr = 0x60,
		.tuner_gpio = RDE250_XCV_TUNER,
		.tuner_sif_gpio = 0x05,
		.tuner_scl_gpio = 0x1a,
		.tuner_sda_gpio = 0x1b,
		.decoder = CX231XX_AVDECODER,
348
		.output_mode = OUT_MODE_VIP11,
349 350 351 352 353 354 355 356 357 358 359 360 361 362
		.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 = 0x0e,
		.norm = V4L2_STD_NTSC,

		.input = {{
			.type = CX231XX_VMUX_TELEVISION,
			.vmux = CX231XX_VIN_3_1,
			.amux = CX231XX_AMUX_VIDEO,
363
			.gpio = NULL,
364 365 366 367
		}, {
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
368
			.gpio = NULL,
369 370 371 372 373 374
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
375
			.gpio = NULL,
376 377
		} },
	},
378 379 380 381
	[CX231XX_BOARD_HAUPPAUGE_USBLIVE2] = {
		.name = "Hauppauge USB Live 2",
		.tuner_type = TUNER_ABSENT,
		.decoder = CX231XX_AVDECODER,
382
		.output_mode = OUT_MODE_VIP11,
383 384 385 386 387
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
		.norm = V4L2_STD_NTSC,
388 389
		.no_alt_vanc = 1,
		.external_av = 1,
390
		.dont_use_port_3 = 1,
391 392 393 394
		.input = {{
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
395
			.gpio = NULL,
396 397 398 399 400 401
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
402
			.gpio = NULL,
403 404
		} },
	},
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	[CX231XX_BOARD_KWORLD_UB430_USB_HYBRID] = {
		.name = "Kworld UB430 USB Hybrid",
		.tuner_type = TUNER_NXP_TDA18271,
		.tuner_addr = 0x60,
		.decoder = CX231XX_AVDECODER,
		.output_mode = OUT_MODE_VIP11,
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x11,	/* According with PV cxPolaris.inf file */
		.tuner_sif_gpio = -1,
		.tuner_scl_gpio = -1,
		.tuner_sda_gpio = -1,
		.gpio_pin_status_mask = 0x4001000,
		.tuner_i2c_master = 2,
		.demod_i2c_master = 1,
		.ir_i2c_master = 2,
		.has_dvb = 1,
		.demod_addr = 0x10,
		.norm = V4L2_STD_PAL_M,
		.input = {{
			.type = CX231XX_VMUX_TELEVISION,
			.vmux = CX231XX_VIN_3_1,
			.amux = CX231XX_AMUX_VIDEO,
			.gpio = NULL,
		}, {
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		} },
	},
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	[CX231XX_BOARD_PV_PLAYTV_USB_HYBRID] = {
		.name = "Pixelview PlayTV USB Hybrid",
		.tuner_type = TUNER_NXP_TDA18271,
		.tuner_addr = 0x60,
		.decoder = CX231XX_AVDECODER,
		.output_mode = OUT_MODE_VIP11,
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x00,	/* According with PV cxPolaris.inf file */
		.tuner_sif_gpio = -1,
		.tuner_scl_gpio = -1,
		.tuner_sda_gpio = -1,
		.gpio_pin_status_mask = 0x4001000,
		.tuner_i2c_master = 2,
		.demod_i2c_master = 1,
458
		.ir_i2c_master = 2,
459
		.rc_map_name = RC_MAP_PIXELVIEW_002T,
460
		.has_dvb = 1,
461 462 463 464 465 466
		.demod_addr = 0x10,
		.norm = V4L2_STD_PAL_M,
		.input = {{
			.type = CX231XX_VMUX_TELEVISION,
			.vmux = CX231XX_VIN_3_1,
			.amux = CX231XX_AMUX_VIDEO,
467
			.gpio = NULL,
468 469 470 471
		}, {
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
472
			.gpio = NULL,
473 474 475 476 477 478
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
479
			.gpio = NULL,
480 481
		} },
	},
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	[CX231XX_BOARD_PV_XCAPTURE_USB] = {
		.name = "Pixelview Xcapture USB",
		.tuner_type = TUNER_ABSENT,
		.decoder = CX231XX_AVDECODER,
		.output_mode = OUT_MODE_VIP11,
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
		.norm = V4L2_STD_NTSC,
		.no_alt_vanc = 1,
		.external_av = 1,
		.dont_use_port_3 = 1,

		.input = {{
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
				.gpio = NULL,
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
				.gpio = NULL,
			}
		},
	},
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

	[CX231XX_BOARD_ICONBIT_U100] = {
		.name = "Iconbit Analog Stick U100 FM",
		.tuner_type = TUNER_ABSENT,
		.decoder = CX231XX_AVDECODER,
		.output_mode = OUT_MODE_VIP11,
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x1C,
		.gpio_pin_status_mask = 0x4001000,

		.input = {{
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		} },
	},
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	[CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL] = {
		.name = "Hauppauge WinTV USB2 FM (PAL)",
		.tuner_type = TUNER_NXP_TDA18271,
		.tuner_addr = 0x60,
		.tuner_gpio = RDE250_XCV_TUNER,
		.tuner_sif_gpio = 0x05,
		.tuner_scl_gpio = 0x1a,
		.tuner_sda_gpio = 0x1b,
		.decoder = CX231XX_AVDECODER,
		.output_mode = OUT_MODE_VIP11,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
		.tuner_i2c_master = 1,
		.norm = V4L2_STD_PAL,

		.input = {{
			.type = CX231XX_VMUX_TELEVISION,
			.vmux = CX231XX_VIN_3_1,
			.amux = CX231XX_AMUX_VIDEO,
			.gpio = NULL,
		}, {
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		} },
	},
	[CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC] = {
		.name = "Hauppauge WinTV USB2 FM (NTSC)",
		.tuner_type = TUNER_NXP_TDA18271,
		.tuner_addr = 0x60,
		.tuner_gpio = RDE250_XCV_TUNER,
		.tuner_sif_gpio = 0x05,
		.tuner_scl_gpio = 0x1a,
		.tuner_sda_gpio = 0x1b,
		.decoder = CX231XX_AVDECODER,
		.output_mode = OUT_MODE_VIP11,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
		.tuner_i2c_master = 1,
		.norm = V4L2_STD_NTSC,

		.input = {{
			.type = CX231XX_VMUX_TELEVISION,
			.vmux = CX231XX_VIN_3_1,
			.amux = CX231XX_AMUX_VIDEO,
			.gpio = NULL,
		}, {
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		} },
	},
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
	[CX231XX_BOARD_ELGATO_VIDEO_CAPTURE_V2] = {
		.name = "Elgato Video Capture V2",
		.tuner_type = TUNER_ABSENT,
		.decoder = CX231XX_AVDECODER,
		.output_mode = OUT_MODE_VIP11,
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
		.norm = V4L2_STD_NTSC,
		.no_alt_vanc = 1,
		.external_av = 1,
		.dont_use_port_3 = 1,
		.input = {{
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
			.gpio = NULL,
		} },
	},
633 634 635 636
};
const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);

/* table of devices that work with this driver */
637
struct usb_device_id cx231xx_id_table[] = {
638
	{USB_DEVICE(0x0572, 0x5A3C),
639 640
	 .driver_info = CX231XX_BOARD_UNKNOWN},
	{USB_DEVICE(0x0572, 0x58A2),
641
	 .driver_info = CX231XX_BOARD_CNXT_CARRAERA},
642
	{USB_DEVICE(0x0572, 0x58A1),
643 644 645 646 647 648 649 650 651 652
	 .driver_info = CX231XX_BOARD_CNXT_SHELBY},
	{USB_DEVICE(0x0572, 0x58A4),
	 .driver_info = CX231XX_BOARD_CNXT_RDE_253S},
	{USB_DEVICE(0x0572, 0x58A5),
	 .driver_info = CX231XX_BOARD_CNXT_RDU_253S},
	{USB_DEVICE(0x0572, 0x58A6),
	 .driver_info = CX231XX_BOARD_CNXT_VIDEO_GRABBER},
	{USB_DEVICE(0x0572, 0x589E),
	 .driver_info = CX231XX_BOARD_CNXT_RDE_250},
	{USB_DEVICE(0x0572, 0x58A0),
653
	 .driver_info = CX231XX_BOARD_CNXT_RDU_250},
654 655 656 657
	{USB_DEVICE(0x2040, 0xb110),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL},
	{USB_DEVICE(0x2040, 0xb111),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC},
658 659
	{USB_DEVICE(0x2040, 0xb120),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
660 661
	{USB_DEVICE(0x2040, 0xb140),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
662 663
	{USB_DEVICE(0x2040, 0xc200),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USBLIVE2},
664 665
	{USB_DEVICE_VER(USB_VID_PIXELVIEW, USB_PID_PIXELVIEW_SBTVD, 0x4000, 0x4001),
	 .driver_info = CX231XX_BOARD_PV_PLAYTV_USB_HYBRID},
666 667
	{USB_DEVICE(USB_VID_PIXELVIEW, 0x5014),
	 .driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},
668 669
	{USB_DEVICE(0x1b80, 0xe424),
	 .driver_info = CX231XX_BOARD_KWORLD_UB430_USB_HYBRID},
670 671
	{USB_DEVICE(0x1f4d, 0x0237),
	 .driver_info = CX231XX_BOARD_ICONBIT_U100},
672 673
	{USB_DEVICE(0x0fd9, 0x0037),
	 .driver_info = CX231XX_BOARD_ELGATO_VIDEO_CAPTURE_V2},
674
	{},
675
};
676

677 678 679 680 681 682 683 684 685 686 687
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;

688 689 690
	if (dev->tuner_type == TUNER_XC5000) {
		if (command == XC5000_TUNER_RESET) {
			cx231xx_info
691 692
				("Tuner CB: RESET: cmd %d : tuner type %d \n",
				 command, dev->tuner_type);
693 694 695 696 697 698 699 700 701 702
			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);
		}
703 704 705 706 707 708 709 710 711 712
	} else if (dev->tuner_type == TUNER_NXP_TDA18271) {
		switch (command) {
		case TDA18271_CALLBACK_CMD_AGC_ENABLE:
			if (dev->model == CX231XX_BOARD_PV_PLAYTV_USB_HYBRID)
				rc = cx231xx_set_agc_analog_digital_mux_select(dev, arg);
			break;
		default:
			rc = -EINVAL;
			break;
		}
713
	}
714 715 716 717
	return rc;
}
EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);

718
static void cx231xx_reset_out(struct cx231xx *dev)
719 720 721 722 723 724 725
{
	cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
	msleep(200);
	cx231xx_set_gpio_value(dev, CX23417_RESET, 0);
	msleep(200);
	cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
}
726 727

static void cx231xx_enable_OSC(struct cx231xx *dev)
728 729 730
{
	cx231xx_set_gpio_value(dev, CX23417_OSC_EN, 1);
}
731 732

static void cx231xx_sleep_s5h1432(struct cx231xx *dev)
733 734 735 736
{
	cx231xx_set_gpio_value(dev, SLEEP_S5H1432, 0);
}

737
static inline void cx231xx_set_model(struct cx231xx *dev)
738
{
739
	dev->board = cx231xx_boards[dev->model];
740 741 742 743 744 745 746 747 748 749 750
}

/* 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",
751
		     dev->board.name, dev->model);
752

753
	/* set the direction for GPIO pins */
754 755 756
	if (dev->board.tuner_gpio) {
		cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
		cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
757 758
	}
	if (dev->board.tuner_sif_gpio >= 0)
759
		cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
760

761
	/* request some modules if any required */
762

763
	/* set the mode to Analog mode initially */
764 765 766 767 768 769 770 771 772
	cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);

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

}

static void cx231xx_config_tuner(struct cx231xx *dev)
{
773 774
	struct tuner_setup tun_setup;
	struct v4l2_frequency f;
775 776 777 778 779 780 781 782 783

	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;

784 785
	tuner_call(dev, tuner, s_type_addr, &tun_setup);

786
#if 0
787
	if (tun_setup.type == TUNER_XC5000) {
788 789 790
		static struct xc2028_ctrl ctrl = {
			.fname = XC5000_DEFAULT_FIRMWARE,
			.max_len = 64,
791
			.demod = 0;
792 793 794 795 796
		};
		struct v4l2_priv_tun_config cfg = {
			.tuner = dev->tuner_type,
			.priv = &ctrl,
		};
797
		tuner_call(dev, tuner, s_config, &cfg);
798 799 800 801 802
	}
#endif
	/* configure tuner */
	f.tuner = 0;
	f.type = V4L2_TUNER_ANALOG_TV;
803
	f.frequency = 9076;	/* just a magic number */
804
	dev->ctl_freq = f.frequency;
805
	call_all(dev, tuner, s_frequency, &f);
806

807
}
808 809 810

void cx231xx_card_setup(struct cx231xx *dev)
{
811

812 813 814 815 816 817 818
	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;

	/* request some modules */
819
	if (dev->board.decoder == CX231XX_AVDECODER) {
820 821
		dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
					&dev->i2c_bus[0].i2c_adap,
822
					"cx25840", 0x88 >> 1, NULL);
823 824
		if (dev->sd_cx25840 == NULL)
			cx231xx_info("cx25840 subdev registration failure\n");
825
		cx25840_call(dev, core, load_fw);
826

827
	}
828

829 830 831
	/* Initialize the tuner */
	if (dev->board.tuner_type != TUNER_ABSENT) {
		dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
832
						    &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
833
						    "tuner",
834 835 836 837
						    dev->tuner_addr, NULL);
		if (dev->sd_tuner == NULL)
			cx231xx_info("tuner subdev registration failure\n");
		else
838
			cx231xx_config_tuner(dev);
839
	}
840 841 842 843 844 845 846 847
}

/*
 * cx231xx_config()
 * inits registers with sane defaults
 */
int cx231xx_config(struct cx231xx *dev)
{
848
	/* TBD need to add cx231xx specific code */
849 850 851 852 853 854 855 856 857 858 859 860
	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)
{
861
	/* u32 input = INPUT(dev->video_input)->vmux; */
862

863
	call_all(dev, video, s_stream, 1);
864 865 866 867 868 869 870 871 872 873 874
}

/*
 * 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)
{
	cx231xx_release_analog_resources(dev);

875
	cx231xx_remove_from_devlist(dev);
876

877 878
	cx231xx_ir_exit(dev);

879
	/* Release I2C buses */
880
	cx231xx_dev_uninit(dev);
881

882 883
	/* delete v4l2 device */
	v4l2_device_unregister(&dev->v4l2_dev);
884

885 886 887
	usb_put_dev(dev->udev);

	/* Mark device as unused */
888
	clear_bit(dev->devno, &cx231xx_devused);
889 890 891 892 893 894

	kfree(dev->video_mode.alt_max_pkt_size);
	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);
	kfree(dev);
895 896 897 898 899 900
}

/*
 * cx231xx_init_dev()
 * allocates and inits the device structs, registers i2c bus and v4l device
 */
901
static int cx231xx_init_dev(struct cx231xx *dev, struct usb_device *udev,
902
			    int minor)
903 904 905 906 907 908 909 910
{
	int retval = -ENOMEM;
	int errCode;
	unsigned int maxh, maxw;

	dev->udev = udev;
	mutex_init(&dev->lock);
	mutex_init(&dev->ctrl_urb_lock);
911
	mutex_init(&dev->gpio_i2c_lock);
912
	mutex_init(&dev->i2c_lock);
913

914 915 916
	spin_lock_init(&dev->video_mode.slock);
	spin_lock_init(&dev->vbi_mode.slock);
	spin_lock_init(&dev->sliced_cc_mode.slock);
917 918 919 920 921

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

922 923 924 925 926
	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;
927

928 929
	/* Query cx231xx to find what pcb config it is related to */
	initialize_cx231xx(dev);
930

931 932
	/*To workaround error number=-71 on EP0 for VideoGrabber,
		 need set alt here.*/
933 934
	if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER ||
	    dev->model == CX231XX_BOARD_HAUPPAUGE_USBLIVE2) {
935 936 937
		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
		cx231xx_set_alt_setting(dev, INDEX_VANC, 1);
	}
938
	/* Cx231xx pre card setup */
939 940 941 942 943 944 945 946
	cx231xx_pre_card_setup(dev);

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

947
	/* set default norm */
948 949 950 951 952
	dev->norm = dev->board.norm;

	/* register i2c bus */
	errCode = cx231xx_dev_init(dev);
	if (errCode < 0) {
953
		cx231xx_dev_uninit(dev);
954
		cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
955
			       __func__, errCode);
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
		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->video_input = 0;

	errCode = cx231xx_config(dev);
	if (errCode < 0) {
		cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
977
			       __func__, errCode);
978 979 980 981 982 983 984
		return errCode;
	}

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

985
	/* init vbi dma queues */
986 987 988 989
	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 */
990
	cx231xx_add_into_devlist(dev);
991

992
	if (dev->board.has_417) {
993
		printk(KERN_INFO "attach 417 %d\n", dev->model);
994 995 996 997 998 999 1000
		if (cx231xx_417_register(dev) < 0) {
			printk(KERN_ERR
				"%s() Failed to register 417 on VID_B\n",
			       __func__);
		}
	}

1001 1002 1003
	retval = cx231xx_register_analog_devices(dev);
	if (retval < 0) {
		cx231xx_release_resources(dev);
1004
		return retval;
1005 1006
	}

1007 1008
	cx231xx_ir_init(dev);

1009 1010 1011 1012 1013 1014 1015 1016 1017
	cx231xx_init_extension(dev);

	return 0;
}

#if defined(CONFIG_MODULES) && defined(MODULE)
static void request_module_async(struct work_struct *work)
{
	struct cx231xx *dev = container_of(work,
1018
					   struct cx231xx, request_module_wk);
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

	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);
}
1033 1034 1035

static void flush_request_modules(struct cx231xx *dev)
{
1036
	flush_work(&dev->request_module_wk);
1037
}
1038 1039
#else
#define request_modules(dev)
1040
#define flush_request_modules(dev)
1041 1042 1043 1044 1045 1046 1047
#endif /* CONFIG_MODULES */

/*
 * cx231xx_usb_probe()
 * checks for supported devices
 */
static int cx231xx_usb_probe(struct usb_interface *interface,
1048
			     const struct usb_device_id *id)
1049 1050 1051 1052 1053
{
	struct usb_device *udev;
	struct usb_interface *uif;
	struct cx231xx *dev = NULL;
	int retval = -ENODEV;
1054
	int nr = 0, ifnum;
1055 1056
	int i, isoc_pipe = 0;
	char *speed;
1057
	struct usb_interface_assoc_descriptor *assoc_desc;
1058 1059 1060 1061

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

1062 1063 1064 1065 1066 1067
	/*
	 * Interface number 0 - IR interface (handled by mceusb driver)
	 * Interface number 1 - AV interface (handled by this driver)
	 */
	if (ifnum != 1)
		return -ENODEV;
1068

1069
	/* Check to see next free device and mark as used */
1070 1071 1072 1073 1074 1075 1076 1077 1078
	do {
		nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
		if (nr >= CX231XX_MAXBOARDS) {
			/* No free device slots */
			cx231xx_err(DRIVER_NAME ": Supports only %i devices.\n",
					CX231XX_MAXBOARDS);
			return -ENOMEM;
		}
	} while (test_and_set_bit(nr, &cx231xx_devused));
1079

1080 1081 1082 1083
	/* 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");
1084
		clear_bit(nr, &cx231xx_devused);
1085 1086
		return -ENOMEM;
	}
1087

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
	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;
	dev->has_alsa_audio = 1;
	dev->power_mode = -1;
	atomic_set(&dev->devlist_count, 0);

	/* 0 - vbi ; 1 -sliced cc mode */
	dev->vbi_or_sliced_cc_mode = 0;

	/* get maximum no.of IAD interfaces */
	assoc_desc = udev->actconfig->intf_assoc[0];
	dev->max_iad_interface_count = assoc_desc->bInterfaceCount;

	/* init CIR module TBD */

	/*mode_tv: digital=1 or analog=0*/
	dev->mode_tv = 0;

	dev->USE_ISO = transfer_mode;

	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";
	}
1130

1131
	cx231xx_info("New device %s %s @ %s Mbps "
1132
	     "(%04x:%04x) with %d interfaces\n",
1133 1134
	     udev->manufacturer ? udev->manufacturer : "",
	     udev->product ? udev->product : "",
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	     speed,
	     le16_to_cpu(udev->descriptor.idVendor),
	     le16_to_cpu(udev->descriptor.idProduct),
	     dev->max_iad_interface_count);

	/* 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_err(DRIVER_NAME ": Not found "
			    "matching IAD interface\n");
1150
		clear_bit(dev->devno, &cx231xx_devused);
1151 1152
		kfree(dev);
		dev = NULL;
1153
		return -ENODEV;
1154
	}
1155

1156
	cx231xx_info("registering interface %d\n", ifnum);
1157

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

1161 1162 1163
	/*
	 * AV device initialization - only done at the last interface
	 */
1164

1165
	/* Create v4l2 device */
1166
	retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1167 1168
	if (retval) {
		cx231xx_errdev("v4l2_device_register failed\n");
1169
		clear_bit(dev->devno, &cx231xx_devused);
1170
		kfree(dev);
1171
		dev = NULL;
1172 1173 1174
		return -EIO;
	}
	/* allocate device struct */
1175
	retval = cx231xx_init_dev(dev, udev, nr);
1176
	if (retval) {
1177
		clear_bit(dev->devno, &cx231xx_devused);
1178 1179
		v4l2_device_unregister(&dev->v4l2_dev);
		kfree(dev);
1180
		dev = NULL;
1181
		usb_set_intfdata(interface, NULL);
1182

1183 1184
		return retval;
	}
1185

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
	/* 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");
1202
		clear_bit(dev->devno, &cx231xx_devused);
1203 1204
		v4l2_device_unregister(&dev->v4l2_dev);
		kfree(dev);
1205
		dev = NULL;
1206 1207
		return -ENOMEM;
	}
1208

1209 1210 1211 1212 1213 1214 1215 1216
	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]);
	}
1217

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	/* 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");
1236
		clear_bit(dev->devno, &cx231xx_devused);
1237 1238
		v4l2_device_unregister(&dev->v4l2_dev);
		kfree(dev);
1239
		dev = NULL;
1240 1241
		return -ENOMEM;
	}
1242

1243 1244 1245 1246 1247 1248 1249 1250 1251
	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]);
	}
1252

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	/* 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");
1271
		clear_bit(dev->devno, &cx231xx_devused);
1272 1273
		v4l2_device_unregister(&dev->v4l2_dev);
		kfree(dev);
1274
		dev = NULL;
1275 1276
		return -ENOMEM;
	}
1277

1278 1279 1280 1281 1282 1283 1284 1285
	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]);
	}
1286

1287 1288 1289 1290 1291 1292
	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];
1293

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

1298 1299 1300 1301 1302 1303
		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);
1304

1305
		if (dev->ts1_mode.alt_max_pkt_size == NULL) {
1306
			cx231xx_errdev("out of memory!\n");
1307
			clear_bit(dev->devno, &cx231xx_devused);
1308
			v4l2_device_unregister(&dev->v4l2_dev);
1309
			kfree(dev);
1310
			dev = NULL;
1311 1312 1313
			return -ENOMEM;
		}

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

1325 1326 1327 1328 1329 1330 1331 1332 1333
	if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER) {
		cx231xx_enable_OSC(dev);
		cx231xx_reset_out(dev);
		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
	}

	if (dev->model == CX231XX_BOARD_CNXT_RDE_253S)
		cx231xx_sleep_s5h1432(dev);

1334
	/* load other modules required */
1335
	request_modules(dev);
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348

	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;

1349
	dev = usb_get_intfdata(interface);
1350 1351 1352 1353 1354
	usb_set_intfdata(interface, NULL);

	if (!dev)
		return;

1355 1356 1357
	if (!dev->udev)
		return;

1358
	dev->state |= DEV_DISCONNECTED;
1359 1360

	flush_request_modules(dev);
1361

1362 1363 1364 1365 1366 1367 1368 1369
	/* 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
1370 1371 1372
		    ("device %s is open! Deregistration and memory "
		     "deallocation are deferred on close.\n",
		     video_device_node_name(dev->vdev));
1373

1374 1375 1376
		/* Even having users, it is safe to remove the RC i2c driver */
		cx231xx_ir_exit(dev);

1377 1378 1379 1380
		if (dev->USE_ISO)
			cx231xx_uninit_isoc(dev);
		else
			cx231xx_uninit_bulk(dev);
1381 1382 1383 1384 1385
		wake_up_interruptible(&dev->wait_frame);
		wake_up_interruptible(&dev->wait_stream);
	} else {
	}

1386
	cx231xx_close_extension(dev);
1387 1388 1389

	mutex_unlock(&dev->lock);

1390 1391
	if (!dev->users)
		cx231xx_release_resources(dev);
1392 1393 1394 1395 1396 1397 1398 1399 1400
}

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

1401
module_usb_driver(cx231xx_usb_driver);