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

   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.
 */

23
#include "cx231xx.h"
24 25
#include <linux/init.h>
#include <linux/module.h>
26
#include <linux/slab.h>
27 28 29 30 31 32
#include <linux/delay.h>
#include <linux/i2c.h>
#include <media/tuner.h>
#include <media/tveeprom.h>
#include <media/v4l2-common.h>

33
#include <media/drv-intf/cx25840.h>
34
#include "dvb-usb-ids.h"
35
#include "xc5000.h"
36
#include "tda18271.h"
37 38 39 40 41 42


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

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

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

/*
 *  Board definitions
 */
struct cx231xx_board cx231xx_boards[] = {
	[CX231XX_BOARD_UNKNOWN] = {
70 71 72 73 74 75
		.name = "Unknown CX231xx video grabber",
		.tuner_type = TUNER_ABSENT,
		.input = {{
				.type = CX231XX_VMUX_TELEVISION,
				.vmux = CX231XX_VIN_3_1,
				.amux = CX231XX_AMUX_VIDEO,
76
				.gpio = NULL,
77 78 79 80
			}, {
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
81
				.gpio = NULL,
82 83 84 85 86 87
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
88
				.gpio = NULL,
89 90 91
			}
		},
	},
92 93
	[CX231XX_BOARD_CNXT_CARRAERA] = {
		.name = "Conexant Hybrid TV - CARRAERA",
94 95 96 97 98 99 100
		.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,
101
		.output_mode = OUT_MODE_VIP11,
102 103 104 105
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
106
		.tuner_i2c_master = I2C_1_MUX_3,
107
		.demod_i2c_master = I2C_2,
108 109 110 111 112 113 114 115
		.has_dvb = 1,
		.demod_addr = 0x02,
		.norm = V4L2_STD_PAL,

		.input = {{
				.type = CX231XX_VMUX_TELEVISION,
				.vmux = CX231XX_VIN_3_1,
				.amux = CX231XX_AMUX_VIDEO,
116
				.gpio = NULL,
117 118 119 120
			}, {
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
121
				.gpio = NULL,
122 123 124 125 126 127
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
128
				.gpio = NULL,
129 130 131
			}
		},
	},
132 133
	[CX231XX_BOARD_CNXT_SHELBY] = {
		.name = "Conexant Hybrid TV - SHELBY",
134 135 136 137 138 139 140
		.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,
141
		.output_mode = OUT_MODE_VIP11,
142 143 144 145
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
146
		.tuner_i2c_master = I2C_1_MUX_3,
147
		.demod_i2c_master = I2C_2,
148 149 150 151 152 153 154 155
		.has_dvb = 1,
		.demod_addr = 0x32,
		.norm = V4L2_STD_NTSC,

		.input = {{
				.type = CX231XX_VMUX_TELEVISION,
				.vmux = CX231XX_VIN_3_1,
				.amux = CX231XX_AMUX_VIDEO,
156
				.gpio = NULL,
157 158 159 160
			}, {
				.type = CX231XX_VMUX_COMPOSITE1,
				.vmux = CX231XX_VIN_2_1,
				.amux = CX231XX_AMUX_LINE_IN,
161
				.gpio = NULL,
162 163 164 165 166 167
			}, {
				.type = CX231XX_VMUX_SVIDEO,
				.vmux = CX231XX_VIN_1_1 |
					(CX231XX_VIN_1_2 << 8) |
					CX25840_SVIDEO_ON,
				.amux = CX231XX_AMUX_LINE_IN,
168
				.gpio = NULL,
169 170 171
			}
		},
	},
172 173 174 175 176 177 178 179 180
	[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,
181
		.output_mode = OUT_MODE_VIP11,
182 183 184 185
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x1c,
		.gpio_pin_status_mask = 0x4001000,
186
		.tuner_i2c_master = I2C_1_MUX_3,
187
		.demod_i2c_master = I2C_2,
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
		.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,
222
		.output_mode = OUT_MODE_VIP11,
223 224 225 226
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x1c,
		.gpio_pin_status_mask = 0x4001000,
227
		.tuner_i2c_master = I2C_1_MUX_3,
228
		.demod_i2c_master = I2C_2,
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
		.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",
255
		.tuner_type = TUNER_ABSENT,
256
		.decoder = CX231XX_AVDECODER,
257
		.output_mode = OUT_MODE_VIP11,
258 259 260 261
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x1c,
		.gpio_pin_status_mask = 0x4001000,
		.norm = V4L2_STD_PAL,
262 263
		.no_alt_vanc = 1,
		.external_av = 1,
264 265 266 267
		/* Actually, it has a 417, but it isn't working correctly.
		 * So set to 0 for now until someone can manage to get this
		 * to work reliably. */
		.has_417 = 0,
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

		.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,
293
		.output_mode = OUT_MODE_VIP11,
294 295 296 297
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
298
		.tuner_i2c_master = I2C_1_MUX_3,
299
		.demod_i2c_master = I2C_2,
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
		.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,
321
		.output_mode = OUT_MODE_VIP11,
322 323 324 325
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
326
		.tuner_i2c_master = I2C_1_MUX_3,
327
		.demod_i2c_master = I2C_2,
328 329 330 331 332 333 334 335 336 337 338 339
		.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,
			}
		},
	},
340 341 342 343 344 345 346 347 348
	[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,
349
		.output_mode = OUT_MODE_VIP11,
350 351 352 353
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
354
		.tuner_i2c_master = I2C_1_MUX_1,
355
		.demod_i2c_master = I2C_1_MUX_1,
356 357 358 359 360 361 362 363
		.has_dvb = 1,
		.demod_addr = 0x0e,
		.norm = V4L2_STD_NTSC,

		.input = {{
			.type = CX231XX_VMUX_TELEVISION,
			.vmux = CX231XX_VIN_3_1,
			.amux = CX231XX_AMUX_VIDEO,
364
			.gpio = NULL,
365 366 367 368
		}, {
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
369
			.gpio = NULL,
370 371 372 373 374 375
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
376
			.gpio = NULL,
377 378
		} },
	},
379 380 381 382
	[CX231XX_BOARD_HAUPPAUGE_USBLIVE2] = {
		.name = "Hauppauge USB Live 2",
		.tuner_type = TUNER_ABSENT,
		.decoder = CX231XX_AVDECODER,
383
		.output_mode = OUT_MODE_VIP11,
384 385 386 387 388
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
		.norm = V4L2_STD_NTSC,
389 390
		.no_alt_vanc = 1,
		.external_av = 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
	[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,
418
		.tuner_i2c_master = I2C_2,
419
		.demod_i2c_master = I2C_1_MUX_3,
420
		.ir_i2c_master = I2C_2,
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
		.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
	[CX231XX_BOARD_KWORLD_UB445_USB_HYBRID] = {
		.name = "Kworld UB445 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,
456
		.tuner_i2c_master = I2C_2,
457
		.demod_i2c_master = I2C_1_MUX_3,
458
		.ir_i2c_master = I2C_2,
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
		.has_dvb = 1,
		.demod_addr = 0x10,
		.norm = V4L2_STD_NTSC_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,
		} },
	},
481 482 483 484 485 486 487 488 489 490 491 492 493
	[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,
494
		.tuner_i2c_master = I2C_2,
495
		.demod_i2c_master = I2C_1_MUX_3,
496
		.ir_i2c_master = I2C_2,
497
		.rc_map_name = RC_MAP_PIXELVIEW_002T,
498
		.has_dvb = 1,
499 500 501 502 503 504
		.demod_addr = 0x10,
		.norm = V4L2_STD_PAL_M,
		.input = {{
			.type = CX231XX_VMUX_TELEVISION,
			.vmux = CX231XX_VIN_3_1,
			.amux = CX231XX_AMUX_VIDEO,
505
			.gpio = NULL,
506 507 508 509
		}, {
			.type = CX231XX_VMUX_COMPOSITE1,
			.vmux = CX231XX_VIN_2_1,
			.amux = CX231XX_AMUX_LINE_IN,
510
			.gpio = NULL,
511 512 513 514 515 516
		}, {
			.type = CX231XX_VMUX_SVIDEO,
			.vmux = CX231XX_VIN_1_1 |
				(CX231XX_VIN_1_2 << 8) |
				CX25840_SVIDEO_ON,
			.amux = CX231XX_AMUX_LINE_IN,
517
			.gpio = NULL,
518 519
		} },
	},
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	[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,

		.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,
			}
		},
	},
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

	[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,
		} },
	},
573 574 575 576 577 578 579 580 581 582 583 584 585
	[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,
586
		.tuner_i2c_master = I2C_1_MUX_3,
587 588 589 590 591 592 593 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
		.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,
621
		.tuner_i2c_master = I2C_1_MUX_3,
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
		.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,
		} },
	},
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	[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,
		.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,
		} },
	},
669 670 671 672 673 674
	[CX231XX_BOARD_OTG102] = {
		.name = "Geniatech OTG102",
		.tuner_type = TUNER_ABSENT,
		.decoder = CX231XX_AVDECODER,
		.output_mode = OUT_MODE_VIP11,
		.ctl_pin_status_mask = 0xFFFFFFC4,
675
		.agc_analog_digital_select_gpio = 0x0c,
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
			/* According with PV CxPlrCAP.inf file */
		.gpio_pin_status_mask = 0x4001000,
		.norm = V4L2_STD_NTSC,
		.no_alt_vanc = 1,
		.external_av = 1,
		/*.has_417 = 1, */
		/* This board is believed to have a hardware encoding chip
		 * supporting mpeg1/2/4, but as the 417 is apparently not
		 * working for the reference board it is not here either. */

		.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,
			}
		},
	},
701
	[CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx] = {
702
		.name = "Hauppauge WinTV 930C-HD (1113xx) / HVR-900H (111xxx) / PCTV QuatroStick 521e",
703 704 705 706 707 708 709 710 711 712 713 714
		.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,
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
715
		.tuner_i2c_master = I2C_1_MUX_3,
716
		.demod_i2c_master = I2C_1_MUX_3,
717 718 719 720
		.has_dvb = 1,
		.demod_addr = 0x0e,
		.norm = V4L2_STD_PAL,

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
		.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_930C_HD_1114xx] = {
741
		.name = "Hauppauge WinTV 930C-HD (1114xx) / HVR-901H (1114xx) / PCTV QuatroStick 522e",
742 743 744 745 746 747 748 749 750 751 752 753
		.tuner_type = TUNER_ABSENT,
		.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,
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
754
		.tuner_i2c_master = I2C_1_MUX_3,
755
		.demod_i2c_master = I2C_1_MUX_3,
756 757 758 759
		.has_dvb = 1,
		.demod_addr = 0x0e,
		.norm = V4L2_STD_PAL,

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
		.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_955Q] = {
		.name = "Hauppauge WinTV-HVR-955Q (111401)",
		.tuner_type = TUNER_ABSENT,
		.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,
		.demod_xfer_mode = 0,
		.ctl_pin_status_mask = 0xFFFFFFC4,
		.agc_analog_digital_select_gpio = 0x0c,
		.gpio_pin_status_mask = 0x4001000,
		.tuner_i2c_master = I2C_1_MUX_3,
794
		.demod_i2c_master = I2C_1_MUX_3,
795 796 797 798
		.has_dvb = 1,
		.demod_addr = 0x0e,
		.norm = V4L2_STD_NTSC,

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
		.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,
		} },
	},
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
	[CX231XX_BOARD_TERRATEC_GRABBY] = {
		.name = "Terratec Grabby",
		.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_PAL,
		.no_alt_vanc = 1,
		.external_av = 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,
		} },
	},
844 845 846 847
};
const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);

/* table of devices that work with this driver */
848
struct usb_device_id cx231xx_id_table[] = {
849 850
	{USB_DEVICE(0x1D19, 0x6109),
	.driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},
851
	{USB_DEVICE(0x0572, 0x5A3C),
852 853
	 .driver_info = CX231XX_BOARD_UNKNOWN},
	{USB_DEVICE(0x0572, 0x58A2),
854
	 .driver_info = CX231XX_BOARD_CNXT_CARRAERA},
855
	{USB_DEVICE(0x0572, 0x58A1),
856 857 858 859 860 861 862 863 864 865
	 .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),
866
	 .driver_info = CX231XX_BOARD_CNXT_RDU_250},
867 868 869 870
	{USB_DEVICE(0x2040, 0xb110),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL},
	{USB_DEVICE(0x2040, 0xb111),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC},
871 872
	{USB_DEVICE(0x2040, 0xb120),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
873 874
	{USB_DEVICE(0x2040, 0xb123),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_955Q},
875 876
	{USB_DEVICE(0x2040, 0xb130),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx},
877 878
	{USB_DEVICE(0x2040, 0xb131),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx},
879 880 881
	/* Hauppauge WinTV-HVR-900-H */
	{USB_DEVICE(0x2040, 0xb138),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx},
882 883 884
	/* Hauppauge WinTV-HVR-901-H */
	{USB_DEVICE(0x2040, 0xb139),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx},
885 886
	{USB_DEVICE(0x2040, 0xb140),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
887 888
	{USB_DEVICE(0x2040, 0xc200),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USBLIVE2},
889 890 891
	/* PCTV QuatroStick 521e */
	{USB_DEVICE(0x2013, 0x0259),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx},
892 893 894
	/* PCTV QuatroStick 522e */
	{USB_DEVICE(0x2013, 0x025e),
	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx},
895 896
	{USB_DEVICE_VER(USB_VID_PIXELVIEW, USB_PID_PIXELVIEW_SBTVD, 0x4000, 0x4001),
	 .driver_info = CX231XX_BOARD_PV_PLAYTV_USB_HYBRID},
897 898
	{USB_DEVICE(USB_VID_PIXELVIEW, 0x5014),
	 .driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},
899 900
	{USB_DEVICE(0x1b80, 0xe424),
	 .driver_info = CX231XX_BOARD_KWORLD_UB430_USB_HYBRID},
901 902
	{USB_DEVICE(0x1b80, 0xe421),
	 .driver_info = CX231XX_BOARD_KWORLD_UB445_USB_HYBRID},
903 904
	{USB_DEVICE(0x1f4d, 0x0237),
	 .driver_info = CX231XX_BOARD_ICONBIT_U100},
905 906
	{USB_DEVICE(0x0fd9, 0x0037),
	 .driver_info = CX231XX_BOARD_ELGATO_VIDEO_CAPTURE_V2},
907 908
	{USB_DEVICE(0x1f4d, 0x0102),
	 .driver_info = CX231XX_BOARD_OTG102},
909 910
	{USB_DEVICE(USB_VID_TERRATEC, 0x00a6),
	 .driver_info = CX231XX_BOARD_TERRATEC_GRABBY},
911
	{},
912
};
913

914 915 916 917 918 919 920 921 922 923 924
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;

925 926
	if (dev->tuner_type == TUNER_XC5000) {
		if (command == XC5000_TUNER_RESET) {
927
			dev_dbg(dev->dev,
928 929
				"Tuner CB: RESET: cmd %d : tuner type %d\n",
				command, dev->tuner_type);
930 931 932 933 934 935 936 937 938 939
			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);
		}
940 941 942 943 944 945 946 947 948 949
	} 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;
		}
950
	}
951 952 953 954
	return rc;
}
EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);

955
static void cx231xx_reset_out(struct cx231xx *dev)
956 957 958 959 960 961 962
{
	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);
}
963 964

static void cx231xx_enable_OSC(struct cx231xx *dev)
965 966 967
{
	cx231xx_set_gpio_value(dev, CX23417_OSC_EN, 1);
}
968 969

static void cx231xx_sleep_s5h1432(struct cx231xx *dev)
970 971 972 973
{
	cx231xx_set_gpio_value(dev, SLEEP_S5H1432, 0);
}

974
static inline void cx231xx_set_model(struct cx231xx *dev)
975
{
976
	dev->board = cx231xx_boards[dev->model];
977 978 979 980 981 982 983
}

/* 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)
{
984
	dev_info(dev->dev, "Identified as %s (card=%d)\n",
985
		dev->board.name, dev->model);
986

987
	/* set the direction for GPIO pins */
988 989 990
	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);
991 992
	}
	if (dev->board.tuner_sif_gpio >= 0)
993
		cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
994

995
	/* request some modules if any required */
996

997
	/* set the mode to Analog mode initially */
998 999 1000 1001 1002 1003 1004 1005 1006
	cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);

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

}

static void cx231xx_config_tuner(struct cx231xx *dev)
{
1007 1008
	struct tuner_setup tun_setup;
	struct v4l2_frequency f;
1009 1010 1011 1012 1013 1014 1015 1016 1017

	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;

1018 1019
	tuner_call(dev, tuner, s_type_addr, &tun_setup);

1020
#if 0
1021
	if (tun_setup.type == TUNER_XC5000) {
1022 1023 1024
		static struct xc2028_ctrl ctrl = {
			.fname = XC5000_DEFAULT_FIRMWARE,
			.max_len = 64,
1025
			.demod = 0;
1026 1027 1028 1029 1030
		};
		struct v4l2_priv_tun_config cfg = {
			.tuner = dev->tuner_type,
			.priv = &ctrl,
		};
1031
		tuner_call(dev, tuner, s_config, &cfg);
1032 1033 1034 1035 1036
	}
#endif
	/* configure tuner */
	f.tuner = 0;
	f.type = V4L2_TUNER_ANALOG_TV;
1037
	f.frequency = 9076;	/* just a magic number */
1038
	dev->ctl_freq = f.frequency;
1039
	call_all(dev, tuner, s_frequency, &f);
1040

1041
}
1042

1043 1044
static int read_eeprom(struct cx231xx *dev, struct i2c_client *client,
		       u8 *eedata, int len)
1045 1046 1047 1048 1049 1050
{
	int ret = 0;
	u8 start_offset = 0;
	int len_todo = len;
	u8 *eedata_cur = eedata;
	int i;
1051
	struct i2c_msg msg_write = { .addr = client->addr, .flags = 0,
1052
		.buf = &start_offset, .len = 1 };
1053
	struct i2c_msg msg_read = { .addr = client->addr, .flags = I2C_M_RD };
1054 1055

	/* start reading at offset 0 */
1056
	ret = i2c_transfer(client->adapter, &msg_write, 1);
1057
	if (ret < 0) {
1058
		dev_err(dev->dev, "Can't read eeprom\n");
1059 1060
		return ret;
	}
1061 1062 1063 1064 1065

	while (len_todo > 0) {
		msg_read.len = (len_todo > 64) ? 64 : len_todo;
		msg_read.buf = eedata_cur;

1066
		ret = i2c_transfer(client->adapter, &msg_read, 1);
1067
		if (ret < 0) {
1068
			dev_err(dev->dev, "Can't read eeprom\n");
1069 1070
			return ret;
		}
1071 1072 1073 1074 1075
		eedata_cur += msg_read.len;
		len_todo -= msg_read.len;
	}

	for (i = 0; i + 15 < len; i += 16)
1076
		dev_dbg(dev->dev, "i2c eeprom %02x: %*ph\n",
1077
			i, 16, &eedata[i]);
1078 1079 1080 1081

	return 0;
}

1082 1083
void cx231xx_card_setup(struct cx231xx *dev)
{
1084

1085 1086 1087 1088 1089 1090 1091
	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 */
1092
	if (dev->board.decoder == CX231XX_AVDECODER) {
1093
		dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1094
					cx231xx_get_i2c_adap(dev, I2C_0),
1095
					"cx25840", 0x88 >> 1, NULL);
1096
		if (dev->sd_cx25840 == NULL)
1097
			dev_err(dev->dev,
1098
				"cx25840 subdev registration failure\n");
1099
		cx25840_call(dev, core, load_fw);
1100

1101
	}
1102

1103 1104
	/* Initialize the tuner */
	if (dev->board.tuner_type != TUNER_ABSENT) {
1105 1106
		struct i2c_adapter *tuner_i2c = cx231xx_get_i2c_adap(dev,
						dev->board.tuner_i2c_master);
1107
		dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1108
						    tuner_i2c,
1109
						    "tuner",
1110 1111
						    dev->tuner_addr, NULL);
		if (dev->sd_tuner == NULL)
1112
			dev_err(dev->dev,
1113
				"tuner subdev registration failure\n");
1114
		else
1115
			cx231xx_config_tuner(dev);
1116
	}
1117 1118 1119

	switch (dev->model) {
	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
1120
	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1121
	case CX231XX_BOARD_HAUPPAUGE_955Q:
1122
		{
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
			struct eeprom {
				struct tveeprom tvee;
				u8 eeprom[256];
				struct i2c_client client;
			};
			struct eeprom *e = kzalloc(sizeof(*e), GFP_KERNEL);

			if (e == NULL) {
				dev_err(dev->dev,
					"failed to allocate memory to read eeprom\n");
				break;
			}
			e->client.adapter = cx231xx_get_i2c_adap(dev, I2C_1_MUX_1);
			e->client.addr = 0xa0 >> 1;
1137

1138 1139 1140 1141
			read_eeprom(dev, &e->client, e->eeprom, sizeof(e->eeprom));
			tveeprom_hauppauge_analog(&e->client,
						&e->tvee, e->eeprom + 0xc0);
			kfree(e);
1142 1143 1144 1145
			break;
		}
	}

1146 1147 1148 1149 1150 1151 1152 1153
}

/*
 * cx231xx_config()
 * inits registers with sane defaults
 */
int cx231xx_config(struct cx231xx *dev)
{
1154
	/* TBD need to add cx231xx specific code */
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

	return 0;
}

/*
 * cx231xx_config_i2c()
 * configure i2c attached devices
 */
void cx231xx_config_i2c(struct cx231xx *dev)
{
1165
	/* u32 input = INPUT(dev->video_input)->vmux; */
1166

1167
	call_all(dev, video, s_stream, 1);
1168 1169
}

1170 1171 1172 1173 1174
static void cx231xx_unregister_media_device(struct cx231xx *dev)
{
#ifdef CONFIG_MEDIA_CONTROLLER
	if (dev->media_dev) {
		media_device_unregister(dev->media_dev);
1175
		media_device_cleanup(dev->media_dev);
1176 1177 1178 1179 1180 1181
		kfree(dev->media_dev);
		dev->media_dev = NULL;
	}
#endif
}

1182 1183 1184 1185 1186 1187 1188 1189 1190
/*
 * 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);

1191
	cx231xx_remove_from_devlist(dev);
1192

1193 1194
	cx231xx_ir_exit(dev);

1195
	/* Release I2C buses */
1196
	cx231xx_dev_uninit(dev);
1197

1198 1199
	/* delete v4l2 device */
	v4l2_device_unregister(&dev->v4l2_dev);
1200

1201 1202
	cx231xx_unregister_media_device(dev);

1203 1204 1205
	usb_put_dev(dev->udev);

	/* Mark device as unused */
1206
	clear_bit(dev->devno, &cx231xx_devused);
1207 1208
}

1209 1210
static void cx231xx_media_device_init(struct cx231xx *dev,
				      struct usb_device *udev)
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
{
#ifdef CONFIG_MEDIA_CONTROLLER
	struct media_device *mdev;

	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
	if (!mdev)
		return;

	mdev->dev = dev->dev;
	strlcpy(mdev->model, dev->board.name, sizeof(mdev->model));
	if (udev->serial)
		strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
	strcpy(mdev->bus_info, udev->devpath);
	mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
	mdev->driver_version = LINUX_VERSION_CODE;

1227
	media_device_init(mdev);
1228 1229 1230 1231 1232

	dev->media_dev = mdev;
#endif
}

1233
static int cx231xx_create_media_graph(struct cx231xx *dev)
1234 1235 1236 1237 1238
{
#ifdef CONFIG_MEDIA_CONTROLLER
	struct media_device *mdev = dev->media_dev;
	struct media_entity *entity;
	struct media_entity *tuner = NULL, *decoder = NULL;
1239
	int ret;
1240 1241

	if (!mdev)
1242
		return 0;
1243 1244

	media_device_for_each_entity(entity, mdev) {
1245
		switch (entity->function) {
1246
		case MEDIA_ENT_F_TUNER:
1247 1248
			tuner = entity;
			break;
1249
		case MEDIA_ENT_F_ATV_DECODER:
1250 1251 1252 1253 1254 1255 1256 1257
			decoder = entity;
			break;
		}
	}

	/* Analog setup, using tuner as a link */

	if (!decoder)
1258
		return 0;
1259

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	if (tuner) {
		ret = media_create_pad_link(tuner, TUNER_PAD_IF_OUTPUT, decoder, 0,
					    MEDIA_LNK_FL_ENABLED);
		if (ret < 0)
			return ret;
	}
	ret = media_create_pad_link(decoder, 1, &dev->vdev.entity, 0,
				    MEDIA_LNK_FL_ENABLED);
	if (ret < 0)
		return ret;
	ret = media_create_pad_link(decoder, 2, &dev->vbi_dev.entity, 0,
				    MEDIA_LNK_FL_ENABLED);
	if (ret < 0)
		return ret;
1274
#endif
1275
	return 0;
1276 1277
}

1278 1279 1280 1281
/*
 * cx231xx_init_dev()
 * allocates and inits the device structs, registers i2c bus and v4l device
 */
1282
static int cx231xx_init_dev(struct cx231xx *dev, struct usb_device *udev,
1283
			    int minor)
1284 1285 1286 1287 1288 1289 1290
{
	int retval = -ENOMEM;
	unsigned int maxh, maxw;

	dev->udev = udev;
	mutex_init(&dev->lock);
	mutex_init(&dev->ctrl_urb_lock);
1291
	mutex_init(&dev->gpio_i2c_lock);
1292
	mutex_init(&dev->i2c_lock);
1293

1294 1295 1296
	spin_lock_init(&dev->video_mode.slock);
	spin_lock_init(&dev->vbi_mode.slock);
	spin_lock_init(&dev->sliced_cc_mode.slock);
1297 1298 1299 1300 1301

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

1302 1303 1304 1305 1306
	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;
1307

1308
	/* Query cx231xx to find what pcb config it is related to */
1309 1310
	retval = initialize_cx231xx(dev);
	if (retval < 0) {
1311
		dev_err(dev->dev, "Failed to read PCB config\n");
1312 1313
		return retval;
	}
1314

1315 1316
	/*To workaround error number=-71 on EP0 for VideoGrabber,
		 need set alt here.*/
1317 1318
	if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER ||
	    dev->model == CX231XX_BOARD_HAUPPAUGE_USBLIVE2) {
1319 1320 1321
		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
		cx231xx_set_alt_setting(dev, INDEX_VANC, 1);
	}
1322
	/* Cx231xx pre card setup */
1323 1324
	cx231xx_pre_card_setup(dev);

1325 1326
	retval = cx231xx_config(dev);
	if (retval) {
1327
		dev_err(dev->dev, "error configuring device\n");
1328 1329 1330
		return -ENOMEM;
	}

1331
	/* set default norm */
1332 1333 1334
	dev->norm = dev->board.norm;

	/* register i2c bus */
1335 1336
	retval = cx231xx_dev_init(dev);
	if (retval) {
1337
		dev_err(dev->dev,
1338 1339
			"%s: cx231xx_i2c_register - errCode [%d]!\n",
			__func__, retval);
1340
		goto err_dev_init;
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	}

	/* 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;

1358 1359
	retval = cx231xx_config(dev);
	if (retval) {
1360 1361
		dev_err(dev->dev, "%s: cx231xx_config - errCode [%d]!\n",
			__func__, retval);
1362
		goto err_dev_init;
1363 1364 1365 1366 1367 1368
	}

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

1369
	/* init vbi dma queues */
1370 1371 1372 1373
	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 */
1374
	cx231xx_add_into_devlist(dev);
1375

1376
	if (dev->board.has_417) {
1377
		dev_info(dev->dev, "attach 417 %d\n", dev->model);
1378
		if (cx231xx_417_register(dev) < 0) {
1379
			dev_err(dev->dev,
1380
				"%s() Failed to register 417 on VID_B\n",
1381
				__func__);
1382 1383 1384
		}
	}

1385
	retval = cx231xx_register_analog_devices(dev);
1386
	if (retval)
1387
		goto err_analog;
1388

1389 1390
	cx231xx_ir_init(dev);

1391 1392 1393
	cx231xx_init_extension(dev);

	return 0;
1394
err_analog:
1395 1396
	cx231xx_unregister_media_device(dev);
	cx231xx_release_analog_resources(dev);
1397 1398 1399 1400
	cx231xx_remove_from_devlist(dev);
err_dev_init:
	cx231xx_dev_uninit(dev);
	return retval;
1401 1402 1403 1404 1405 1406
}

#if defined(CONFIG_MODULES) && defined(MODULE)
static void request_module_async(struct work_struct *work)
{
	struct cx231xx *dev = container_of(work,
1407
					   struct cx231xx, request_module_wk);
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421

	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);
}
1422 1423 1424

static void flush_request_modules(struct cx231xx *dev)
{
1425
	flush_work(&dev->request_module_wk);
1426
}
1427 1428
#else
#define request_modules(dev)
1429
#define flush_request_modules(dev)
1430 1431
#endif /* CONFIG_MODULES */

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
static int cx231xx_init_v4l2(struct cx231xx *dev,
			     struct usb_device *udev,
			     struct usb_interface *interface,
			     int isoc_pipe)
{
	struct usb_interface *uif;
	int i, idx;

	/* Video Init */

	/* compute alternate max packet sizes for video */
	idx = dev->current_pcb_config.hs_config_info[0].interface_info.video_index + 1;
	if (idx >= dev->max_iad_interface_count) {
1445
		dev_err(dev->dev,
1446
			"Video PCB interface #%d doesn't exist\n", idx);
1447 1448 1449 1450 1451 1452 1453 1454
		return -ENODEV;
	}

	uif = udev->actconfig->interface[idx];

	dev->video_mode.end_point_addr = uif->altsetting[0].endpoint[isoc_pipe].desc.bEndpointAddress;
	dev->video_mode.num_alt = uif->num_altsetting;

1455
	dev_info(dev->dev,
1456 1457 1458
		 "video EndPoint Addr 0x%x, Alternate settings: %i\n",
		 dev->video_mode.end_point_addr,
		 dev->video_mode.num_alt);
1459 1460

	dev->video_mode.alt_max_pkt_size = devm_kmalloc_array(&udev->dev, 32, dev->video_mode.num_alt, GFP_KERNEL);
1461
	if (dev->video_mode.alt_max_pkt_size == NULL)
1462 1463 1464 1465 1466
		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);
1467
		dev_dbg(dev->dev,
1468 1469
			"Alternate setting %i, max size= %i\n", i,
			dev->video_mode.alt_max_pkt_size[i]);
1470 1471 1472 1473 1474 1475
	}

	/* VBI Init */

	idx = dev->current_pcb_config.hs_config_info[0].interface_info.vanc_index + 1;
	if (idx >= dev->max_iad_interface_count) {
1476
		dev_err(dev->dev,
1477
			"VBI PCB interface #%d doesn't exist\n", idx);
1478 1479 1480 1481 1482 1483 1484 1485 1486
		return -ENODEV;
	}
	uif = udev->actconfig->interface[idx];

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

	dev->vbi_mode.num_alt = uif->num_altsetting;
1487
	dev_info(dev->dev,
1488 1489 1490
		 "VBI EndPoint Addr 0x%x, Alternate settings: %i\n",
		 dev->vbi_mode.end_point_addr,
		 dev->vbi_mode.num_alt);
1491 1492 1493

	/* compute alternate max packet sizes for vbi */
	dev->vbi_mode.alt_max_pkt_size = devm_kmalloc_array(&udev->dev, 32, dev->vbi_mode.num_alt, GFP_KERNEL);
1494
	if (dev->vbi_mode.alt_max_pkt_size == NULL)
1495 1496 1497 1498 1499 1500 1501 1502
		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);
1503
		dev_dbg(dev->dev,
1504 1505
			"Alternate setting %i, max size= %i\n", i,
			dev->vbi_mode.alt_max_pkt_size[i]);
1506 1507 1508 1509 1510 1511 1512
	}

	/* Sliced CC VBI init */

	/* compute alternate max packet sizes for sliced CC */
	idx = dev->current_pcb_config.hs_config_info[0].interface_info.hanc_index + 1;
	if (idx >= dev->max_iad_interface_count) {
1513
		dev_err(dev->dev,
1514
			"Sliced CC PCB interface #%d doesn't exist\n", idx);
1515 1516 1517 1518 1519 1520 1521 1522 1523
		return -ENODEV;
	}
	uif = udev->actconfig->interface[idx];

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

	dev->sliced_cc_mode.num_alt = uif->num_altsetting;
1524
	dev_info(dev->dev,
1525 1526 1527
		 "sliced CC EndPoint Addr 0x%x, Alternate settings: %i\n",
		 dev->sliced_cc_mode.end_point_addr,
		 dev->sliced_cc_mode.num_alt);
1528
	dev->sliced_cc_mode.alt_max_pkt_size = devm_kmalloc_array(&udev->dev, 32, dev->sliced_cc_mode.num_alt, GFP_KERNEL);
1529
	if (dev->sliced_cc_mode.alt_max_pkt_size == NULL)
1530 1531 1532 1533 1534 1535 1536
		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);
1537
		dev_dbg(dev->dev,
1538 1539
			"Alternate setting %i, max size= %i\n", i,
			dev->sliced_cc_mode.alt_max_pkt_size[i]);
1540 1541 1542 1543 1544
	}

	return 0;
}

1545 1546 1547 1548 1549
/*
 * cx231xx_usb_probe()
 * checks for supported devices
 */
static int cx231xx_usb_probe(struct usb_interface *interface,
1550
			     const struct usb_device_id *id)
1551 1552
{
	struct usb_device *udev;
1553
	struct device *d = &interface->dev;
1554 1555 1556
	struct usb_interface *uif;
	struct cx231xx *dev = NULL;
	int retval = -ENODEV;
1557
	int nr = 0, ifnum;
1558 1559
	int i, isoc_pipe = 0;
	char *speed;
1560
	u8 idx;
1561
	struct usb_interface_assoc_descriptor *assoc_desc;
1562 1563 1564

	ifnum = interface->altsetting[0].desc.bInterfaceNumber;

1565 1566 1567 1568 1569 1570
	/*
	 * Interface number 0 - IR interface (handled by mceusb driver)
	 * Interface number 1 - AV interface (handled by this driver)
	 */
	if (ifnum != 1)
		return -ENODEV;
1571

1572
	/* Check to see next free device and mark as used */
1573 1574 1575 1576
	do {
		nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
		if (nr >= CX231XX_MAXBOARDS) {
			/* No free device slots */
1577
			dev_err(d,
1578 1579
				"Supports only %i devices.\n",
				CX231XX_MAXBOARDS);
1580 1581 1582
			return -ENOMEM;
		}
	} while (test_and_set_bit(nr, &cx231xx_devused));
1583

1584 1585
	udev = usb_get_dev(interface_to_usbdev(interface));

1586
	/* allocate memory for our device state and initialize it */
1587
	dev = devm_kzalloc(&udev->dev, sizeof(*dev), GFP_KERNEL);
1588
	if (dev == NULL) {
1589 1590
		retval = -ENOMEM;
		goto err_if;
1591
	}
1592

1593 1594 1595 1596
	snprintf(dev->name, 29, "cx231xx #%d", nr);
	dev->devno = nr;
	dev->model = id->driver_info;
	dev->video_mode.alt = -1;
1597
	dev->dev = d;
1598

1599 1600
	cx231xx_set_model(dev);

1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
	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 */
1614
	dev->max_iad_interface_count = udev->config->desc.bNumInterfaces;
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636

	/* 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";
	}
1637

1638
	dev_info(d,
1639 1640 1641 1642 1643 1644 1645
		 "New device %s %s @ %s Mbps (%04x:%04x) with %d interfaces\n",
		 udev->manufacturer ? udev->manufacturer : "",
		 udev->product ? udev->product : "",
		 speed,
		 le16_to_cpu(udev->descriptor.idVendor),
		 le16_to_cpu(udev->descriptor.idProduct),
		 dev->max_iad_interface_count);
1646 1647 1648 1649 1650 1651 1652 1653 1654

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

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

	assoc_desc = udev->actconfig->intf_assoc[0];
	if (assoc_desc->bFirstInterface != ifnum) {
1655
		dev_err(d, "Not found matching IAD interface\n");
1656 1657
		retval = -ENODEV;
		goto err_if;
1658
	}
1659

1660
	dev_dbg(d, "registering interface %d\n", ifnum);
1661

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

1665 1666
	/* Initialize the media controller */
	cx231xx_media_device_init(dev, udev);
1667

1668
	/* Create v4l2 device */
1669
#ifdef CONFIG_MEDIA_CONTROLLER
1670
	dev->v4l2_dev.mdev = dev->media_dev;
1671
#endif
1672
	retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1673
	if (retval) {
1674
		dev_err(d, "v4l2_device_register failed\n");
1675
		goto err_v4l2;
1676
	}
1677

1678
	/* allocate device struct */
1679
	retval = cx231xx_init_dev(dev, udev, nr);
1680 1681
	if (retval)
		goto err_init;
1682

1683 1684
	retval = cx231xx_init_v4l2(dev, udev, interface, isoc_pipe);
	if (retval)
1685
		goto err_init;
1686

1687 1688
	if (dev->current_pcb_config.ts1_source != 0xff) {
		/* compute alternate max packet sizes for TS1 */
1689 1690
		idx = dev->current_pcb_config.hs_config_info[0].interface_info.ts1_index + 1;
		if (idx >= dev->max_iad_interface_count) {
1691 1692
			dev_err(d, "TS1 PCB interface #%d doesn't exist\n",
				idx);
1693
			retval = -ENODEV;
1694
			goto err_video_alt;
1695 1696
		}
		uif = udev->actconfig->interface[idx];
1697

1698
		dev->ts1_mode.end_point_addr =
1699 1700
		    uif->altsetting[0].endpoint[isoc_pipe].
				desc.bEndpointAddress;
1701

1702
		dev->ts1_mode.num_alt = uif->num_altsetting;
1703
		dev_info(d,
1704 1705 1706
			 "TS EndPoint Addr 0x%x, Alternate settings: %i\n",
			 dev->ts1_mode.end_point_addr,
			 dev->ts1_mode.num_alt);
1707

1708
		dev->ts1_mode.alt_max_pkt_size = devm_kmalloc_array(&udev->dev, 32, dev->ts1_mode.num_alt, GFP_KERNEL);
1709
		if (dev->ts1_mode.alt_max_pkt_size == NULL) {
1710
			retval = -ENOMEM;
1711
			goto err_video_alt;
1712 1713
		}

1714 1715
		for (i = 0; i < dev->ts1_mode.num_alt; i++) {
			u16 tmp = le16_to_cpu(uif->altsetting[i].
1716 1717
						endpoint[isoc_pipe].desc.
						wMaxPacketSize);
1718 1719
			dev->ts1_mode.alt_max_pkt_size[i] =
			    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1720 1721
			dev_dbg(d, "Alternate setting %i, max size= %i\n",
				i, dev->ts1_mode.alt_max_pkt_size[i]);
1722 1723
		}
	}
1724

1725 1726 1727 1728 1729 1730 1731 1732 1733
	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);

1734
	/* load other modules required */
1735
	request_modules(dev);
1736

1737
	retval = cx231xx_create_media_graph(dev);
1738 1739 1740 1741 1742 1743
	if (retval < 0)
		goto done;

#ifdef CONFIG_MEDIA_CONTROLLER
	retval = media_device_register(dev->media_dev);
#endif
1744

1745 1746 1747
done:
	if (retval < 0)
		cx231xx_release_resources(dev);
1748
	return retval;
1749

1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
err_video_alt:
	/* cx231xx_uninit_dev: */
	cx231xx_close_extension(dev);
	cx231xx_ir_exit(dev);
	cx231xx_release_analog_resources(dev);
	cx231xx_417_unregister(dev);
	cx231xx_remove_from_devlist(dev);
	cx231xx_dev_uninit(dev);
err_init:
	v4l2_device_unregister(&dev->v4l2_dev);
err_v4l2:
	usb_set_intfdata(interface, NULL);
err_if:
	usb_put_dev(udev);
1764
	clear_bit(nr, &cx231xx_devused);
1765
	return retval;
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
}

/*
 * 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;

1777
	dev = usb_get_intfdata(interface);
1778 1779 1780 1781 1782
	usb_set_intfdata(interface, NULL);

	if (!dev)
		return;

1783 1784 1785
	if (!dev->udev)
		return;

1786
	dev->state |= DEV_DISCONNECTED;
1787 1788

	flush_request_modules(dev);
1789

1790 1791 1792 1793 1794 1795 1796
	/* wait until all current v4l2 io is finished then deallocate
	   resources */
	mutex_lock(&dev->lock);

	wake_up_interruptible_all(&dev->open);

	if (dev->users) {
1797
		dev_warn(dev->dev,
1798
			 "device %s is open! Deregistration and memory deallocation are deferred on close.\n",
1799
			 video_device_node_name(&dev->vdev));
1800

1801 1802 1803
		/* Even having users, it is safe to remove the RC i2c driver */
		cx231xx_ir_exit(dev);

1804 1805 1806 1807
		if (dev->USE_ISO)
			cx231xx_uninit_isoc(dev);
		else
			cx231xx_uninit_bulk(dev);
1808 1809 1810 1811 1812
		wake_up_interruptible(&dev->wait_frame);
		wake_up_interruptible(&dev->wait_stream);
	} else {
	}

1813
	cx231xx_close_extension(dev);
1814 1815 1816

	mutex_unlock(&dev->lock);

1817 1818
	if (!dev->users)
		cx231xx_release_resources(dev);
1819 1820 1821 1822 1823 1824 1825 1826 1827
}

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

1828
module_usb_driver(cx231xx_usb_driver);