1 /*
2    cx231xx-cards.c - driver for Conexant Cx23100/101/102
3 				USB video capture devices
4 
5    Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6 				Based on em28xx driver
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/i2c.h>
28 #include <linux/usb.h>
29 #include <media/tuner.h>
30 #include <media/tveeprom.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-chip-ident.h>
33 
34 #include <media/cx25840.h>
35 #include "dvb-usb-ids.h"
36 #include "xc5000.h"
37 #include "tda18271.h"
38 
39 #include "cx231xx.h"
40 
41 static int tuner = -1;
42 module_param(tuner, int, 0444);
43 MODULE_PARM_DESC(tuner, "tuner type");
44 
45 static int transfer_mode = 1;
46 module_param(transfer_mode, int, 0444);
47 MODULE_PARM_DESC(transfer_mode, "transfer mode (1-ISO or 0-BULK)");
48 
49 static unsigned int disable_ir;
50 module_param(disable_ir, int, 0444);
51 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
52 
53 /* Bitmask marking allocated devices from 0 to CX231XX_MAXBOARDS */
54 static unsigned long cx231xx_devused;
55 
56 /*
57  *  Reset sequences for analog/digital modes
58  */
59 
60 static struct cx231xx_reg_seq RDE250_XCV_TUNER[] = {
61 	{0x03, 0x01, 10},
62 	{0x03, 0x00, 30},
63 	{0x03, 0x01, 10},
64 	{-1, -1, -1},
65 };
66 
67 /*
68  *  Board definitions
69  */
70 struct cx231xx_board cx231xx_boards[] = {
71 	[CX231XX_BOARD_UNKNOWN] = {
72 		.name = "Unknown CX231xx video grabber",
73 		.tuner_type = TUNER_ABSENT,
74 		.input = {{
75 				.type = CX231XX_VMUX_TELEVISION,
76 				.vmux = CX231XX_VIN_3_1,
77 				.amux = CX231XX_AMUX_VIDEO,
78 				.gpio = NULL,
79 			}, {
80 				.type = CX231XX_VMUX_COMPOSITE1,
81 				.vmux = CX231XX_VIN_2_1,
82 				.amux = CX231XX_AMUX_LINE_IN,
83 				.gpio = NULL,
84 			}, {
85 				.type = CX231XX_VMUX_SVIDEO,
86 				.vmux = CX231XX_VIN_1_1 |
87 					(CX231XX_VIN_1_2 << 8) |
88 					CX25840_SVIDEO_ON,
89 				.amux = CX231XX_AMUX_LINE_IN,
90 				.gpio = NULL,
91 			}
92 		},
93 	},
94 	[CX231XX_BOARD_CNXT_CARRAERA] = {
95 		.name = "Conexant Hybrid TV - CARRAERA",
96 		.tuner_type = TUNER_XC5000,
97 		.tuner_addr = 0x61,
98 		.tuner_gpio = RDE250_XCV_TUNER,
99 		.tuner_sif_gpio = 0x05,
100 		.tuner_scl_gpio = 0x1a,
101 		.tuner_sda_gpio = 0x1b,
102 		.decoder = CX231XX_AVDECODER,
103 		.output_mode = OUT_MODE_VIP11,
104 		.demod_xfer_mode = 0,
105 		.ctl_pin_status_mask = 0xFFFFFFC4,
106 		.agc_analog_digital_select_gpio = 0x0c,
107 		.gpio_pin_status_mask = 0x4001000,
108 		.tuner_i2c_master = 1,
109 		.demod_i2c_master = 2,
110 		.has_dvb = 1,
111 		.demod_addr = 0x02,
112 		.norm = V4L2_STD_PAL,
113 
114 		.input = {{
115 				.type = CX231XX_VMUX_TELEVISION,
116 				.vmux = CX231XX_VIN_3_1,
117 				.amux = CX231XX_AMUX_VIDEO,
118 				.gpio = NULL,
119 			}, {
120 				.type = CX231XX_VMUX_COMPOSITE1,
121 				.vmux = CX231XX_VIN_2_1,
122 				.amux = CX231XX_AMUX_LINE_IN,
123 				.gpio = NULL,
124 			}, {
125 				.type = CX231XX_VMUX_SVIDEO,
126 				.vmux = CX231XX_VIN_1_1 |
127 					(CX231XX_VIN_1_2 << 8) |
128 					CX25840_SVIDEO_ON,
129 				.amux = CX231XX_AMUX_LINE_IN,
130 				.gpio = NULL,
131 			}
132 		},
133 	},
134 	[CX231XX_BOARD_CNXT_SHELBY] = {
135 		.name = "Conexant Hybrid TV - SHELBY",
136 		.tuner_type = TUNER_XC5000,
137 		.tuner_addr = 0x61,
138 		.tuner_gpio = RDE250_XCV_TUNER,
139 		.tuner_sif_gpio = 0x05,
140 		.tuner_scl_gpio = 0x1a,
141 		.tuner_sda_gpio = 0x1b,
142 		.decoder = CX231XX_AVDECODER,
143 		.output_mode = OUT_MODE_VIP11,
144 		.demod_xfer_mode = 0,
145 		.ctl_pin_status_mask = 0xFFFFFFC4,
146 		.agc_analog_digital_select_gpio = 0x0c,
147 		.gpio_pin_status_mask = 0x4001000,
148 		.tuner_i2c_master = 1,
149 		.demod_i2c_master = 2,
150 		.has_dvb = 1,
151 		.demod_addr = 0x32,
152 		.norm = V4L2_STD_NTSC,
153 
154 		.input = {{
155 				.type = CX231XX_VMUX_TELEVISION,
156 				.vmux = CX231XX_VIN_3_1,
157 				.amux = CX231XX_AMUX_VIDEO,
158 				.gpio = NULL,
159 			}, {
160 				.type = CX231XX_VMUX_COMPOSITE1,
161 				.vmux = CX231XX_VIN_2_1,
162 				.amux = CX231XX_AMUX_LINE_IN,
163 				.gpio = NULL,
164 			}, {
165 				.type = CX231XX_VMUX_SVIDEO,
166 				.vmux = CX231XX_VIN_1_1 |
167 					(CX231XX_VIN_1_2 << 8) |
168 					CX25840_SVIDEO_ON,
169 				.amux = CX231XX_AMUX_LINE_IN,
170 				.gpio = NULL,
171 			}
172 		},
173 	},
174 	[CX231XX_BOARD_CNXT_RDE_253S] = {
175 		.name = "Conexant Hybrid TV - RDE253S",
176 		.tuner_type = TUNER_NXP_TDA18271,
177 		.tuner_addr = 0x60,
178 		.tuner_gpio = RDE250_XCV_TUNER,
179 		.tuner_sif_gpio = 0x05,
180 		.tuner_scl_gpio = 0x1a,
181 		.tuner_sda_gpio = 0x1b,
182 		.decoder = CX231XX_AVDECODER,
183 		.output_mode = OUT_MODE_VIP11,
184 		.demod_xfer_mode = 0,
185 		.ctl_pin_status_mask = 0xFFFFFFC4,
186 		.agc_analog_digital_select_gpio = 0x1c,
187 		.gpio_pin_status_mask = 0x4001000,
188 		.tuner_i2c_master = 1,
189 		.demod_i2c_master = 2,
190 		.has_dvb = 1,
191 		.demod_addr = 0x02,
192 		.norm = V4L2_STD_PAL,
193 
194 		.input = {{
195 				.type = CX231XX_VMUX_TELEVISION,
196 				.vmux = CX231XX_VIN_3_1,
197 				.amux = CX231XX_AMUX_VIDEO,
198 				.gpio = NULL,
199 			}, {
200 				.type = CX231XX_VMUX_COMPOSITE1,
201 				.vmux = CX231XX_VIN_2_1,
202 				.amux = CX231XX_AMUX_LINE_IN,
203 				.gpio = NULL,
204 			}, {
205 				.type = CX231XX_VMUX_SVIDEO,
206 				.vmux = CX231XX_VIN_1_1 |
207 					(CX231XX_VIN_1_2 << 8) |
208 					CX25840_SVIDEO_ON,
209 				.amux = CX231XX_AMUX_LINE_IN,
210 				.gpio = NULL,
211 			}
212 		},
213 	},
214 
215 	[CX231XX_BOARD_CNXT_RDU_253S] = {
216 		.name = "Conexant Hybrid TV - RDU253S",
217 		.tuner_type = TUNER_NXP_TDA18271,
218 		.tuner_addr = 0x60,
219 		.tuner_gpio = RDE250_XCV_TUNER,
220 		.tuner_sif_gpio = 0x05,
221 		.tuner_scl_gpio = 0x1a,
222 		.tuner_sda_gpio = 0x1b,
223 		.decoder = CX231XX_AVDECODER,
224 		.output_mode = OUT_MODE_VIP11,
225 		.demod_xfer_mode = 0,
226 		.ctl_pin_status_mask = 0xFFFFFFC4,
227 		.agc_analog_digital_select_gpio = 0x1c,
228 		.gpio_pin_status_mask = 0x4001000,
229 		.tuner_i2c_master = 1,
230 		.demod_i2c_master = 2,
231 		.has_dvb = 1,
232 		.demod_addr = 0x02,
233 		.norm = V4L2_STD_PAL,
234 
235 		.input = {{
236 				.type = CX231XX_VMUX_TELEVISION,
237 				.vmux = CX231XX_VIN_3_1,
238 				.amux = CX231XX_AMUX_VIDEO,
239 				.gpio = NULL,
240 			}, {
241 				.type = CX231XX_VMUX_COMPOSITE1,
242 				.vmux = CX231XX_VIN_2_1,
243 				.amux = CX231XX_AMUX_LINE_IN,
244 				.gpio = NULL,
245 			}, {
246 				.type = CX231XX_VMUX_SVIDEO,
247 				.vmux = CX231XX_VIN_1_1 |
248 					(CX231XX_VIN_1_2 << 8) |
249 					CX25840_SVIDEO_ON,
250 				.amux = CX231XX_AMUX_LINE_IN,
251 				.gpio = NULL,
252 			}
253 		},
254 	},
255 	[CX231XX_BOARD_CNXT_VIDEO_GRABBER] = {
256 		.name = "Conexant VIDEO GRABBER",
257 		.tuner_type = TUNER_ABSENT,
258 		.decoder = CX231XX_AVDECODER,
259 		.output_mode = OUT_MODE_VIP11,
260 		.ctl_pin_status_mask = 0xFFFFFFC4,
261 		.agc_analog_digital_select_gpio = 0x1c,
262 		.gpio_pin_status_mask = 0x4001000,
263 		.norm = V4L2_STD_PAL,
264 		.no_alt_vanc = 1,
265 		.external_av = 1,
266 		.dont_use_port_3 = 1,
267 		/* Actually, it has a 417, but it isn't working correctly.
268 		 * So set to 0 for now until someone can manage to get this
269 		 * to work reliably. */
270 		.has_417 = 0,
271 
272 		.input = {{
273 				.type = CX231XX_VMUX_COMPOSITE1,
274 				.vmux = CX231XX_VIN_2_1,
275 				.amux = CX231XX_AMUX_LINE_IN,
276 				.gpio = NULL,
277 			}, {
278 				.type = CX231XX_VMUX_SVIDEO,
279 				.vmux = CX231XX_VIN_1_1 |
280 					(CX231XX_VIN_1_2 << 8) |
281 					CX25840_SVIDEO_ON,
282 				.amux = CX231XX_AMUX_LINE_IN,
283 				.gpio = NULL,
284 			}
285 		},
286 	},
287 	[CX231XX_BOARD_CNXT_RDE_250] = {
288 		.name = "Conexant Hybrid TV - rde 250",
289 		.tuner_type = TUNER_XC5000,
290 		.tuner_addr = 0x61,
291 		.tuner_gpio = RDE250_XCV_TUNER,
292 		.tuner_sif_gpio = 0x05,
293 		.tuner_scl_gpio = 0x1a,
294 		.tuner_sda_gpio = 0x1b,
295 		.decoder = CX231XX_AVDECODER,
296 		.output_mode = OUT_MODE_VIP11,
297 		.demod_xfer_mode = 0,
298 		.ctl_pin_status_mask = 0xFFFFFFC4,
299 		.agc_analog_digital_select_gpio = 0x0c,
300 		.gpio_pin_status_mask = 0x4001000,
301 		.tuner_i2c_master = 1,
302 		.demod_i2c_master = 2,
303 		.has_dvb = 1,
304 		.demod_addr = 0x02,
305 		.norm = V4L2_STD_PAL,
306 
307 		.input = {{
308 				.type = CX231XX_VMUX_TELEVISION,
309 				.vmux = CX231XX_VIN_2_1,
310 				.amux = CX231XX_AMUX_VIDEO,
311 				.gpio = NULL,
312 			}
313 		},
314 	},
315 	[CX231XX_BOARD_CNXT_RDU_250] = {
316 		.name = "Conexant Hybrid TV - RDU 250",
317 		.tuner_type = TUNER_XC5000,
318 		.tuner_addr = 0x61,
319 		.tuner_gpio = RDE250_XCV_TUNER,
320 		.tuner_sif_gpio = 0x05,
321 		.tuner_scl_gpio = 0x1a,
322 		.tuner_sda_gpio = 0x1b,
323 		.decoder = CX231XX_AVDECODER,
324 		.output_mode = OUT_MODE_VIP11,
325 		.demod_xfer_mode = 0,
326 		.ctl_pin_status_mask = 0xFFFFFFC4,
327 		.agc_analog_digital_select_gpio = 0x0c,
328 		.gpio_pin_status_mask = 0x4001000,
329 		.tuner_i2c_master = 1,
330 		.demod_i2c_master = 2,
331 		.has_dvb = 1,
332 		.demod_addr = 0x32,
333 		.norm = V4L2_STD_NTSC,
334 
335 		.input = {{
336 				.type = CX231XX_VMUX_TELEVISION,
337 				.vmux = CX231XX_VIN_2_1,
338 				.amux = CX231XX_AMUX_VIDEO,
339 				.gpio = NULL,
340 			}
341 		},
342 	},
343 	[CX231XX_BOARD_HAUPPAUGE_EXETER] = {
344 		.name = "Hauppauge EXETER",
345 		.tuner_type = TUNER_NXP_TDA18271,
346 		.tuner_addr = 0x60,
347 		.tuner_gpio = RDE250_XCV_TUNER,
348 		.tuner_sif_gpio = 0x05,
349 		.tuner_scl_gpio = 0x1a,
350 		.tuner_sda_gpio = 0x1b,
351 		.decoder = CX231XX_AVDECODER,
352 		.output_mode = OUT_MODE_VIP11,
353 		.demod_xfer_mode = 0,
354 		.ctl_pin_status_mask = 0xFFFFFFC4,
355 		.agc_analog_digital_select_gpio = 0x0c,
356 		.gpio_pin_status_mask = 0x4001000,
357 		.tuner_i2c_master = 1,
358 		.demod_i2c_master = 2,
359 		.has_dvb = 1,
360 		.demod_addr = 0x0e,
361 		.norm = V4L2_STD_NTSC,
362 
363 		.input = {{
364 			.type = CX231XX_VMUX_TELEVISION,
365 			.vmux = CX231XX_VIN_3_1,
366 			.amux = CX231XX_AMUX_VIDEO,
367 			.gpio = NULL,
368 		}, {
369 			.type = CX231XX_VMUX_COMPOSITE1,
370 			.vmux = CX231XX_VIN_2_1,
371 			.amux = CX231XX_AMUX_LINE_IN,
372 			.gpio = NULL,
373 		}, {
374 			.type = CX231XX_VMUX_SVIDEO,
375 			.vmux = CX231XX_VIN_1_1 |
376 				(CX231XX_VIN_1_2 << 8) |
377 				CX25840_SVIDEO_ON,
378 			.amux = CX231XX_AMUX_LINE_IN,
379 			.gpio = NULL,
380 		} },
381 	},
382 	[CX231XX_BOARD_HAUPPAUGE_USBLIVE2] = {
383 		.name = "Hauppauge USB Live 2",
384 		.tuner_type = TUNER_ABSENT,
385 		.decoder = CX231XX_AVDECODER,
386 		.output_mode = OUT_MODE_VIP11,
387 		.demod_xfer_mode = 0,
388 		.ctl_pin_status_mask = 0xFFFFFFC4,
389 		.agc_analog_digital_select_gpio = 0x0c,
390 		.gpio_pin_status_mask = 0x4001000,
391 		.norm = V4L2_STD_NTSC,
392 		.no_alt_vanc = 1,
393 		.external_av = 1,
394 		.dont_use_port_3 = 1,
395 		.input = {{
396 			.type = CX231XX_VMUX_COMPOSITE1,
397 			.vmux = CX231XX_VIN_2_1,
398 			.amux = CX231XX_AMUX_LINE_IN,
399 			.gpio = NULL,
400 		}, {
401 			.type = CX231XX_VMUX_SVIDEO,
402 			.vmux = CX231XX_VIN_1_1 |
403 				(CX231XX_VIN_1_2 << 8) |
404 				CX25840_SVIDEO_ON,
405 			.amux = CX231XX_AMUX_LINE_IN,
406 			.gpio = NULL,
407 		} },
408 	},
409 	[CX231XX_BOARD_KWORLD_UB430_USB_HYBRID] = {
410 		.name = "Kworld UB430 USB Hybrid",
411 		.tuner_type = TUNER_NXP_TDA18271,
412 		.tuner_addr = 0x60,
413 		.decoder = CX231XX_AVDECODER,
414 		.output_mode = OUT_MODE_VIP11,
415 		.demod_xfer_mode = 0,
416 		.ctl_pin_status_mask = 0xFFFFFFC4,
417 		.agc_analog_digital_select_gpio = 0x11,	/* According with PV cxPolaris.inf file */
418 		.tuner_sif_gpio = -1,
419 		.tuner_scl_gpio = -1,
420 		.tuner_sda_gpio = -1,
421 		.gpio_pin_status_mask = 0x4001000,
422 		.tuner_i2c_master = 2,
423 		.demod_i2c_master = 1,
424 		.ir_i2c_master = 2,
425 		.has_dvb = 1,
426 		.demod_addr = 0x10,
427 		.norm = V4L2_STD_PAL_M,
428 		.input = {{
429 			.type = CX231XX_VMUX_TELEVISION,
430 			.vmux = CX231XX_VIN_3_1,
431 			.amux = CX231XX_AMUX_VIDEO,
432 			.gpio = NULL,
433 		}, {
434 			.type = CX231XX_VMUX_COMPOSITE1,
435 			.vmux = CX231XX_VIN_2_1,
436 			.amux = CX231XX_AMUX_LINE_IN,
437 			.gpio = NULL,
438 		}, {
439 			.type = CX231XX_VMUX_SVIDEO,
440 			.vmux = CX231XX_VIN_1_1 |
441 				(CX231XX_VIN_1_2 << 8) |
442 				CX25840_SVIDEO_ON,
443 			.amux = CX231XX_AMUX_LINE_IN,
444 			.gpio = NULL,
445 		} },
446 	},
447 	[CX231XX_BOARD_PV_PLAYTV_USB_HYBRID] = {
448 		.name = "Pixelview PlayTV USB Hybrid",
449 		.tuner_type = TUNER_NXP_TDA18271,
450 		.tuner_addr = 0x60,
451 		.decoder = CX231XX_AVDECODER,
452 		.output_mode = OUT_MODE_VIP11,
453 		.demod_xfer_mode = 0,
454 		.ctl_pin_status_mask = 0xFFFFFFC4,
455 		.agc_analog_digital_select_gpio = 0x00,	/* According with PV cxPolaris.inf file */
456 		.tuner_sif_gpio = -1,
457 		.tuner_scl_gpio = -1,
458 		.tuner_sda_gpio = -1,
459 		.gpio_pin_status_mask = 0x4001000,
460 		.tuner_i2c_master = 2,
461 		.demod_i2c_master = 1,
462 		.ir_i2c_master = 2,
463 		.rc_map_name = RC_MAP_PIXELVIEW_002T,
464 		.has_dvb = 1,
465 		.demod_addr = 0x10,
466 		.norm = V4L2_STD_PAL_M,
467 		.input = {{
468 			.type = CX231XX_VMUX_TELEVISION,
469 			.vmux = CX231XX_VIN_3_1,
470 			.amux = CX231XX_AMUX_VIDEO,
471 			.gpio = NULL,
472 		}, {
473 			.type = CX231XX_VMUX_COMPOSITE1,
474 			.vmux = CX231XX_VIN_2_1,
475 			.amux = CX231XX_AMUX_LINE_IN,
476 			.gpio = NULL,
477 		}, {
478 			.type = CX231XX_VMUX_SVIDEO,
479 			.vmux = CX231XX_VIN_1_1 |
480 				(CX231XX_VIN_1_2 << 8) |
481 				CX25840_SVIDEO_ON,
482 			.amux = CX231XX_AMUX_LINE_IN,
483 			.gpio = NULL,
484 		} },
485 	},
486 	[CX231XX_BOARD_PV_XCAPTURE_USB] = {
487 		.name = "Pixelview Xcapture USB",
488 		.tuner_type = TUNER_ABSENT,
489 		.decoder = CX231XX_AVDECODER,
490 		.output_mode = OUT_MODE_VIP11,
491 		.demod_xfer_mode = 0,
492 		.ctl_pin_status_mask = 0xFFFFFFC4,
493 		.agc_analog_digital_select_gpio = 0x0c,
494 		.gpio_pin_status_mask = 0x4001000,
495 		.norm = V4L2_STD_NTSC,
496 		.no_alt_vanc = 1,
497 		.external_av = 1,
498 		.dont_use_port_3 = 1,
499 
500 		.input = {{
501 				.type = CX231XX_VMUX_COMPOSITE1,
502 				.vmux = CX231XX_VIN_2_1,
503 				.amux = CX231XX_AMUX_LINE_IN,
504 				.gpio = NULL,
505 			}, {
506 				.type = CX231XX_VMUX_SVIDEO,
507 				.vmux = CX231XX_VIN_1_1 |
508 					(CX231XX_VIN_1_2 << 8) |
509 					CX25840_SVIDEO_ON,
510 				.amux = CX231XX_AMUX_LINE_IN,
511 				.gpio = NULL,
512 			}
513 		},
514 	},
515 
516 	[CX231XX_BOARD_ICONBIT_U100] = {
517 		.name = "Iconbit Analog Stick U100 FM",
518 		.tuner_type = TUNER_ABSENT,
519 		.decoder = CX231XX_AVDECODER,
520 		.output_mode = OUT_MODE_VIP11,
521 		.demod_xfer_mode = 0,
522 		.ctl_pin_status_mask = 0xFFFFFFC4,
523 		.agc_analog_digital_select_gpio = 0x1C,
524 		.gpio_pin_status_mask = 0x4001000,
525 
526 		.input = {{
527 			.type = CX231XX_VMUX_COMPOSITE1,
528 			.vmux = CX231XX_VIN_2_1,
529 			.amux = CX231XX_AMUX_LINE_IN,
530 			.gpio = NULL,
531 		}, {
532 			.type = CX231XX_VMUX_SVIDEO,
533 			.vmux = CX231XX_VIN_1_1 |
534 				(CX231XX_VIN_1_2 << 8) |
535 				CX25840_SVIDEO_ON,
536 			.amux = CX231XX_AMUX_LINE_IN,
537 			.gpio = NULL,
538 		} },
539 	},
540 	[CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL] = {
541 		.name = "Hauppauge WinTV USB2 FM (PAL)",
542 		.tuner_type = TUNER_NXP_TDA18271,
543 		.tuner_addr = 0x60,
544 		.tuner_gpio = RDE250_XCV_TUNER,
545 		.tuner_sif_gpio = 0x05,
546 		.tuner_scl_gpio = 0x1a,
547 		.tuner_sda_gpio = 0x1b,
548 		.decoder = CX231XX_AVDECODER,
549 		.output_mode = OUT_MODE_VIP11,
550 		.ctl_pin_status_mask = 0xFFFFFFC4,
551 		.agc_analog_digital_select_gpio = 0x0c,
552 		.gpio_pin_status_mask = 0x4001000,
553 		.tuner_i2c_master = 1,
554 		.norm = V4L2_STD_PAL,
555 
556 		.input = {{
557 			.type = CX231XX_VMUX_TELEVISION,
558 			.vmux = CX231XX_VIN_3_1,
559 			.amux = CX231XX_AMUX_VIDEO,
560 			.gpio = NULL,
561 		}, {
562 			.type = CX231XX_VMUX_COMPOSITE1,
563 			.vmux = CX231XX_VIN_2_1,
564 			.amux = CX231XX_AMUX_LINE_IN,
565 			.gpio = NULL,
566 		}, {
567 			.type = CX231XX_VMUX_SVIDEO,
568 			.vmux = CX231XX_VIN_1_1 |
569 				(CX231XX_VIN_1_2 << 8) |
570 				CX25840_SVIDEO_ON,
571 			.amux = CX231XX_AMUX_LINE_IN,
572 			.gpio = NULL,
573 		} },
574 	},
575 	[CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC] = {
576 		.name = "Hauppauge WinTV USB2 FM (NTSC)",
577 		.tuner_type = TUNER_NXP_TDA18271,
578 		.tuner_addr = 0x60,
579 		.tuner_gpio = RDE250_XCV_TUNER,
580 		.tuner_sif_gpio = 0x05,
581 		.tuner_scl_gpio = 0x1a,
582 		.tuner_sda_gpio = 0x1b,
583 		.decoder = CX231XX_AVDECODER,
584 		.output_mode = OUT_MODE_VIP11,
585 		.ctl_pin_status_mask = 0xFFFFFFC4,
586 		.agc_analog_digital_select_gpio = 0x0c,
587 		.gpio_pin_status_mask = 0x4001000,
588 		.tuner_i2c_master = 1,
589 		.norm = V4L2_STD_NTSC,
590 
591 		.input = {{
592 			.type = CX231XX_VMUX_TELEVISION,
593 			.vmux = CX231XX_VIN_3_1,
594 			.amux = CX231XX_AMUX_VIDEO,
595 			.gpio = NULL,
596 		}, {
597 			.type = CX231XX_VMUX_COMPOSITE1,
598 			.vmux = CX231XX_VIN_2_1,
599 			.amux = CX231XX_AMUX_LINE_IN,
600 			.gpio = NULL,
601 		}, {
602 			.type = CX231XX_VMUX_SVIDEO,
603 			.vmux = CX231XX_VIN_1_1 |
604 				(CX231XX_VIN_1_2 << 8) |
605 				CX25840_SVIDEO_ON,
606 			.amux = CX231XX_AMUX_LINE_IN,
607 			.gpio = NULL,
608 		} },
609 	},
610 	[CX231XX_BOARD_ELGATO_VIDEO_CAPTURE_V2] = {
611 		.name = "Elgato Video Capture V2",
612 		.tuner_type = TUNER_ABSENT,
613 		.decoder = CX231XX_AVDECODER,
614 		.output_mode = OUT_MODE_VIP11,
615 		.demod_xfer_mode = 0,
616 		.ctl_pin_status_mask = 0xFFFFFFC4,
617 		.agc_analog_digital_select_gpio = 0x0c,
618 		.gpio_pin_status_mask = 0x4001000,
619 		.norm = V4L2_STD_NTSC,
620 		.no_alt_vanc = 1,
621 		.external_av = 1,
622 		.dont_use_port_3 = 1,
623 		.input = {{
624 			.type = CX231XX_VMUX_COMPOSITE1,
625 			.vmux = CX231XX_VIN_2_1,
626 			.amux = CX231XX_AMUX_LINE_IN,
627 			.gpio = NULL,
628 		}, {
629 			.type = CX231XX_VMUX_SVIDEO,
630 			.vmux = CX231XX_VIN_1_1 |
631 				(CX231XX_VIN_1_2 << 8) |
632 				CX25840_SVIDEO_ON,
633 			.amux = CX231XX_AMUX_LINE_IN,
634 			.gpio = NULL,
635 		} },
636 	},
637 };
638 const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);
639 
640 /* table of devices that work with this driver */
641 struct usb_device_id cx231xx_id_table[] = {
642 	{USB_DEVICE(0x0572, 0x5A3C),
643 	 .driver_info = CX231XX_BOARD_UNKNOWN},
644 	{USB_DEVICE(0x0572, 0x58A2),
645 	 .driver_info = CX231XX_BOARD_CNXT_CARRAERA},
646 	{USB_DEVICE(0x0572, 0x58A1),
647 	 .driver_info = CX231XX_BOARD_CNXT_SHELBY},
648 	{USB_DEVICE(0x0572, 0x58A4),
649 	 .driver_info = CX231XX_BOARD_CNXT_RDE_253S},
650 	{USB_DEVICE(0x0572, 0x58A5),
651 	 .driver_info = CX231XX_BOARD_CNXT_RDU_253S},
652 	{USB_DEVICE(0x0572, 0x58A6),
653 	 .driver_info = CX231XX_BOARD_CNXT_VIDEO_GRABBER},
654 	{USB_DEVICE(0x0572, 0x589E),
655 	 .driver_info = CX231XX_BOARD_CNXT_RDE_250},
656 	{USB_DEVICE(0x0572, 0x58A0),
657 	 .driver_info = CX231XX_BOARD_CNXT_RDU_250},
658 	{USB_DEVICE(0x2040, 0xb110),
659 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL},
660 	{USB_DEVICE(0x2040, 0xb111),
661 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC},
662 	{USB_DEVICE(0x2040, 0xb120),
663 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
664 	{USB_DEVICE(0x2040, 0xb140),
665 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
666 	{USB_DEVICE(0x2040, 0xc200),
667 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USBLIVE2},
668 	{USB_DEVICE_VER(USB_VID_PIXELVIEW, USB_PID_PIXELVIEW_SBTVD, 0x4000, 0x4001),
669 	 .driver_info = CX231XX_BOARD_PV_PLAYTV_USB_HYBRID},
670 	{USB_DEVICE(USB_VID_PIXELVIEW, 0x5014),
671 	 .driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},
672 	{USB_DEVICE(0x1b80, 0xe424),
673 	 .driver_info = CX231XX_BOARD_KWORLD_UB430_USB_HYBRID},
674 	{USB_DEVICE(0x1f4d, 0x0237),
675 	 .driver_info = CX231XX_BOARD_ICONBIT_U100},
676 	{USB_DEVICE(0x0fd9, 0x0037),
677 	 .driver_info = CX231XX_BOARD_ELGATO_VIDEO_CAPTURE_V2},
678 	{},
679 };
680 
681 MODULE_DEVICE_TABLE(usb, cx231xx_id_table);
682 
683 /* cx231xx_tuner_callback
684  * will be used to reset XC5000 tuner using GPIO pin
685  */
686 
687 int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
688 {
689 	int rc = 0;
690 	struct cx231xx *dev = ptr;
691 
692 	if (dev->tuner_type == TUNER_XC5000) {
693 		if (command == XC5000_TUNER_RESET) {
694 			cx231xx_info
695 				("Tuner CB: RESET: cmd %d : tuner type %d \n",
696 				 command, dev->tuner_type);
697 			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
698 					       1);
699 			msleep(10);
700 			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
701 					       0);
702 			msleep(330);
703 			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
704 					       1);
705 			msleep(10);
706 		}
707 	} else if (dev->tuner_type == TUNER_NXP_TDA18271) {
708 		switch (command) {
709 		case TDA18271_CALLBACK_CMD_AGC_ENABLE:
710 			if (dev->model == CX231XX_BOARD_PV_PLAYTV_USB_HYBRID)
711 				rc = cx231xx_set_agc_analog_digital_mux_select(dev, arg);
712 			break;
713 		default:
714 			rc = -EINVAL;
715 			break;
716 		}
717 	}
718 	return rc;
719 }
720 EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);
721 
722 static void cx231xx_reset_out(struct cx231xx *dev)
723 {
724 	cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
725 	msleep(200);
726 	cx231xx_set_gpio_value(dev, CX23417_RESET, 0);
727 	msleep(200);
728 	cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
729 }
730 
731 static void cx231xx_enable_OSC(struct cx231xx *dev)
732 {
733 	cx231xx_set_gpio_value(dev, CX23417_OSC_EN, 1);
734 }
735 
736 static void cx231xx_sleep_s5h1432(struct cx231xx *dev)
737 {
738 	cx231xx_set_gpio_value(dev, SLEEP_S5H1432, 0);
739 }
740 
741 static inline void cx231xx_set_model(struct cx231xx *dev)
742 {
743 	dev->board = cx231xx_boards[dev->model];
744 }
745 
746 /* Since cx231xx_pre_card_setup() requires a proper dev->model,
747  * this won't work for boards with generic PCI IDs
748  */
749 void cx231xx_pre_card_setup(struct cx231xx *dev)
750 {
751 
752 	cx231xx_set_model(dev);
753 
754 	cx231xx_info("Identified as %s (card=%d)\n",
755 		     dev->board.name, dev->model);
756 
757 	/* set the direction for GPIO pins */
758 	if (dev->board.tuner_gpio) {
759 		cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
760 		cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
761 	}
762 	if (dev->board.tuner_sif_gpio >= 0)
763 		cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
764 
765 	/* request some modules if any required */
766 
767 	/* set the mode to Analog mode initially */
768 	cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
769 
770 	/* Unlock device */
771 	/* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
772 
773 }
774 
775 static void cx231xx_config_tuner(struct cx231xx *dev)
776 {
777 	struct tuner_setup tun_setup;
778 	struct v4l2_frequency f;
779 
780 	if (dev->tuner_type == TUNER_ABSENT)
781 		return;
782 
783 	tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
784 	tun_setup.type = dev->tuner_type;
785 	tun_setup.addr = dev->tuner_addr;
786 	tun_setup.tuner_callback = cx231xx_tuner_callback;
787 
788 	tuner_call(dev, tuner, s_type_addr, &tun_setup);
789 
790 #if 0
791 	if (tun_setup.type == TUNER_XC5000) {
792 		static struct xc2028_ctrl ctrl = {
793 			.fname = XC5000_DEFAULT_FIRMWARE,
794 			.max_len = 64,
795 			.demod = 0;
796 		};
797 		struct v4l2_priv_tun_config cfg = {
798 			.tuner = dev->tuner_type,
799 			.priv = &ctrl,
800 		};
801 		tuner_call(dev, tuner, s_config, &cfg);
802 	}
803 #endif
804 	/* configure tuner */
805 	f.tuner = 0;
806 	f.type = V4L2_TUNER_ANALOG_TV;
807 	f.frequency = 9076;	/* just a magic number */
808 	dev->ctl_freq = f.frequency;
809 	call_all(dev, tuner, s_frequency, &f);
810 
811 }
812 
813 void cx231xx_card_setup(struct cx231xx *dev)
814 {
815 
816 	cx231xx_set_model(dev);
817 
818 	dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
819 	if (cx231xx_boards[dev->model].tuner_addr)
820 		dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
821 
822 	/* request some modules */
823 	if (dev->board.decoder == CX231XX_AVDECODER) {
824 		dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
825 					&dev->i2c_bus[0].i2c_adap,
826 					"cx25840", 0x88 >> 1, NULL);
827 		if (dev->sd_cx25840 == NULL)
828 			cx231xx_info("cx25840 subdev registration failure\n");
829 		cx25840_call(dev, core, load_fw);
830 
831 	}
832 
833 	/* Initialize the tuner */
834 	if (dev->board.tuner_type != TUNER_ABSENT) {
835 		dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
836 						    &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
837 						    "tuner",
838 						    dev->tuner_addr, NULL);
839 		if (dev->sd_tuner == NULL)
840 			cx231xx_info("tuner subdev registration failure\n");
841 		else
842 			cx231xx_config_tuner(dev);
843 	}
844 }
845 
846 /*
847  * cx231xx_config()
848  * inits registers with sane defaults
849  */
850 int cx231xx_config(struct cx231xx *dev)
851 {
852 	/* TBD need to add cx231xx specific code */
853 
854 	return 0;
855 }
856 
857 /*
858  * cx231xx_config_i2c()
859  * configure i2c attached devices
860  */
861 void cx231xx_config_i2c(struct cx231xx *dev)
862 {
863 	/* u32 input = INPUT(dev->video_input)->vmux; */
864 
865 	call_all(dev, video, s_stream, 1);
866 }
867 
868 /*
869  * cx231xx_realease_resources()
870  * unregisters the v4l2,i2c and usb devices
871  * called when the device gets disconected or at module unload
872 */
873 void cx231xx_release_resources(struct cx231xx *dev)
874 {
875 	cx231xx_release_analog_resources(dev);
876 
877 	cx231xx_remove_from_devlist(dev);
878 
879 	cx231xx_ir_exit(dev);
880 
881 	/* Release I2C buses */
882 	cx231xx_dev_uninit(dev);
883 
884 	/* delete v4l2 device */
885 	v4l2_device_unregister(&dev->v4l2_dev);
886 
887 	usb_put_dev(dev->udev);
888 
889 	/* Mark device as unused */
890 	clear_bit(dev->devno, &cx231xx_devused);
891 
892 	kfree(dev->video_mode.alt_max_pkt_size);
893 	kfree(dev->vbi_mode.alt_max_pkt_size);
894 	kfree(dev->sliced_cc_mode.alt_max_pkt_size);
895 	kfree(dev->ts1_mode.alt_max_pkt_size);
896 	kfree(dev);
897 }
898 
899 /*
900  * cx231xx_init_dev()
901  * allocates and inits the device structs, registers i2c bus and v4l device
902  */
903 static int cx231xx_init_dev(struct cx231xx *dev, struct usb_device *udev,
904 			    int minor)
905 {
906 	int retval = -ENOMEM;
907 	int errCode;
908 	unsigned int maxh, maxw;
909 
910 	dev->udev = udev;
911 	mutex_init(&dev->lock);
912 	mutex_init(&dev->ctrl_urb_lock);
913 	mutex_init(&dev->gpio_i2c_lock);
914 	mutex_init(&dev->i2c_lock);
915 
916 	spin_lock_init(&dev->video_mode.slock);
917 	spin_lock_init(&dev->vbi_mode.slock);
918 	spin_lock_init(&dev->sliced_cc_mode.slock);
919 
920 	init_waitqueue_head(&dev->open);
921 	init_waitqueue_head(&dev->wait_frame);
922 	init_waitqueue_head(&dev->wait_stream);
923 
924 	dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
925 	dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
926 	dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
927 	dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
928 	dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
929 
930 	/* Query cx231xx to find what pcb config it is related to */
931 	initialize_cx231xx(dev);
932 
933 	/*To workaround error number=-71 on EP0 for VideoGrabber,
934 		 need set alt here.*/
935 	if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER ||
936 	    dev->model == CX231XX_BOARD_HAUPPAUGE_USBLIVE2) {
937 		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
938 		cx231xx_set_alt_setting(dev, INDEX_VANC, 1);
939 	}
940 	/* Cx231xx pre card setup */
941 	cx231xx_pre_card_setup(dev);
942 
943 	errCode = cx231xx_config(dev);
944 	if (errCode) {
945 		cx231xx_errdev("error configuring device\n");
946 		return -ENOMEM;
947 	}
948 
949 	/* set default norm */
950 	dev->norm = dev->board.norm;
951 
952 	/* register i2c bus */
953 	errCode = cx231xx_dev_init(dev);
954 	if (errCode < 0) {
955 		cx231xx_dev_uninit(dev);
956 		cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
957 			       __func__, errCode);
958 		return errCode;
959 	}
960 
961 	/* Do board specific init */
962 	cx231xx_card_setup(dev);
963 
964 	/* configure the device */
965 	cx231xx_config_i2c(dev);
966 
967 	maxw = norm_maxw(dev);
968 	maxh = norm_maxh(dev);
969 
970 	/* set default image size */
971 	dev->width = maxw;
972 	dev->height = maxh;
973 	dev->interlaced = 0;
974 	dev->video_input = 0;
975 
976 	errCode = cx231xx_config(dev);
977 	if (errCode < 0) {
978 		cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
979 			       __func__, errCode);
980 		return errCode;
981 	}
982 
983 	/* init video dma queues */
984 	INIT_LIST_HEAD(&dev->video_mode.vidq.active);
985 	INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
986 
987 	/* init vbi dma queues */
988 	INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
989 	INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
990 
991 	/* Reset other chips required if they are tied up with GPIO pins */
992 	cx231xx_add_into_devlist(dev);
993 
994 	if (dev->board.has_417) {
995 		printk(KERN_INFO "attach 417 %d\n", dev->model);
996 		if (cx231xx_417_register(dev) < 0) {
997 			printk(KERN_ERR
998 				"%s() Failed to register 417 on VID_B\n",
999 			       __func__);
1000 		}
1001 	}
1002 
1003 	retval = cx231xx_register_analog_devices(dev);
1004 	if (retval < 0) {
1005 		cx231xx_release_resources(dev);
1006 		return retval;
1007 	}
1008 
1009 	cx231xx_ir_init(dev);
1010 
1011 	cx231xx_init_extension(dev);
1012 
1013 	return 0;
1014 }
1015 
1016 #if defined(CONFIG_MODULES) && defined(MODULE)
1017 static void request_module_async(struct work_struct *work)
1018 {
1019 	struct cx231xx *dev = container_of(work,
1020 					   struct cx231xx, request_module_wk);
1021 
1022 	if (dev->has_alsa_audio)
1023 		request_module("cx231xx-alsa");
1024 
1025 	if (dev->board.has_dvb)
1026 		request_module("cx231xx-dvb");
1027 
1028 }
1029 
1030 static void request_modules(struct cx231xx *dev)
1031 {
1032 	INIT_WORK(&dev->request_module_wk, request_module_async);
1033 	schedule_work(&dev->request_module_wk);
1034 }
1035 
1036 static void flush_request_modules(struct cx231xx *dev)
1037 {
1038 	flush_work(&dev->request_module_wk);
1039 }
1040 #else
1041 #define request_modules(dev)
1042 #define flush_request_modules(dev)
1043 #endif /* CONFIG_MODULES */
1044 
1045 /*
1046  * cx231xx_usb_probe()
1047  * checks for supported devices
1048  */
1049 static int cx231xx_usb_probe(struct usb_interface *interface,
1050 			     const struct usb_device_id *id)
1051 {
1052 	struct usb_device *udev;
1053 	struct usb_interface *uif;
1054 	struct cx231xx *dev = NULL;
1055 	int retval = -ENODEV;
1056 	int nr = 0, ifnum;
1057 	int i, isoc_pipe = 0;
1058 	char *speed;
1059 	struct usb_interface_assoc_descriptor *assoc_desc;
1060 
1061 	udev = usb_get_dev(interface_to_usbdev(interface));
1062 	ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1063 
1064 	/*
1065 	 * Interface number 0 - IR interface (handled by mceusb driver)
1066 	 * Interface number 1 - AV interface (handled by this driver)
1067 	 */
1068 	if (ifnum != 1)
1069 		return -ENODEV;
1070 
1071 	/* Check to see next free device and mark as used */
1072 	do {
1073 		nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
1074 		if (nr >= CX231XX_MAXBOARDS) {
1075 			/* No free device slots */
1076 			cx231xx_err(DRIVER_NAME ": Supports only %i devices.\n",
1077 					CX231XX_MAXBOARDS);
1078 			return -ENOMEM;
1079 		}
1080 	} while (test_and_set_bit(nr, &cx231xx_devused));
1081 
1082 	/* allocate memory for our device state and initialize it */
1083 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1084 	if (dev == NULL) {
1085 		cx231xx_err(DRIVER_NAME ": out of memory!\n");
1086 		clear_bit(nr, &cx231xx_devused);
1087 		return -ENOMEM;
1088 	}
1089 
1090 	snprintf(dev->name, 29, "cx231xx #%d", nr);
1091 	dev->devno = nr;
1092 	dev->model = id->driver_info;
1093 	dev->video_mode.alt = -1;
1094 
1095 	dev->interface_count++;
1096 	/* reset gpio dir and value */
1097 	dev->gpio_dir = 0;
1098 	dev->gpio_val = 0;
1099 	dev->xc_fw_load_done = 0;
1100 	dev->has_alsa_audio = 1;
1101 	dev->power_mode = -1;
1102 	atomic_set(&dev->devlist_count, 0);
1103 
1104 	/* 0 - vbi ; 1 -sliced cc mode */
1105 	dev->vbi_or_sliced_cc_mode = 0;
1106 
1107 	/* get maximum no.of IAD interfaces */
1108 	assoc_desc = udev->actconfig->intf_assoc[0];
1109 	dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
1110 
1111 	/* init CIR module TBD */
1112 
1113 	/*mode_tv: digital=1 or analog=0*/
1114 	dev->mode_tv = 0;
1115 
1116 	dev->USE_ISO = transfer_mode;
1117 
1118 	switch (udev->speed) {
1119 	case USB_SPEED_LOW:
1120 		speed = "1.5";
1121 		break;
1122 	case USB_SPEED_UNKNOWN:
1123 	case USB_SPEED_FULL:
1124 		speed = "12";
1125 		break;
1126 	case USB_SPEED_HIGH:
1127 		speed = "480";
1128 		break;
1129 	default:
1130 		speed = "unknown";
1131 	}
1132 
1133 	cx231xx_info("New device %s %s @ %s Mbps "
1134 	     "(%04x:%04x) with %d interfaces\n",
1135 	     udev->manufacturer ? udev->manufacturer : "",
1136 	     udev->product ? udev->product : "",
1137 	     speed,
1138 	     le16_to_cpu(udev->descriptor.idVendor),
1139 	     le16_to_cpu(udev->descriptor.idProduct),
1140 	     dev->max_iad_interface_count);
1141 
1142 	/* increment interface count */
1143 	dev->interface_count++;
1144 
1145 	/* get device number */
1146 	nr = dev->devno;
1147 
1148 	assoc_desc = udev->actconfig->intf_assoc[0];
1149 	if (assoc_desc->bFirstInterface != ifnum) {
1150 		cx231xx_err(DRIVER_NAME ": Not found "
1151 			    "matching IAD interface\n");
1152 		clear_bit(dev->devno, &cx231xx_devused);
1153 		kfree(dev);
1154 		dev = NULL;
1155 		return -ENODEV;
1156 	}
1157 
1158 	cx231xx_info("registering interface %d\n", ifnum);
1159 
1160 	/* save our data pointer in this interface device */
1161 	usb_set_intfdata(interface, dev);
1162 
1163 	/*
1164 	 * AV device initialization - only done at the last interface
1165 	 */
1166 
1167 	/* Create v4l2 device */
1168 	retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1169 	if (retval) {
1170 		cx231xx_errdev("v4l2_device_register failed\n");
1171 		clear_bit(dev->devno, &cx231xx_devused);
1172 		kfree(dev);
1173 		dev = NULL;
1174 		return -EIO;
1175 	}
1176 	/* allocate device struct */
1177 	retval = cx231xx_init_dev(dev, udev, nr);
1178 	if (retval) {
1179 		clear_bit(dev->devno, &cx231xx_devused);
1180 		v4l2_device_unregister(&dev->v4l2_dev);
1181 		kfree(dev);
1182 		dev = NULL;
1183 		usb_set_intfdata(interface, NULL);
1184 
1185 		return retval;
1186 	}
1187 
1188 	/* compute alternate max packet sizes for video */
1189 	uif = udev->actconfig->interface[dev->current_pcb_config.
1190 		       hs_config_info[0].interface_info.video_index + 1];
1191 
1192 	dev->video_mode.end_point_addr = uif->altsetting[0].
1193 			endpoint[isoc_pipe].desc.bEndpointAddress;
1194 
1195 	dev->video_mode.num_alt = uif->num_altsetting;
1196 	cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1197 		     dev->video_mode.end_point_addr,
1198 		     dev->video_mode.num_alt);
1199 	dev->video_mode.alt_max_pkt_size =
1200 		kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
1201 
1202 	if (dev->video_mode.alt_max_pkt_size == NULL) {
1203 		cx231xx_errdev("out of memory!\n");
1204 		clear_bit(dev->devno, &cx231xx_devused);
1205 		v4l2_device_unregister(&dev->v4l2_dev);
1206 		kfree(dev);
1207 		dev = NULL;
1208 		return -ENOMEM;
1209 	}
1210 
1211 	for (i = 0; i < dev->video_mode.num_alt; i++) {
1212 		u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1213 				desc.wMaxPacketSize);
1214 		dev->video_mode.alt_max_pkt_size[i] =
1215 		    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1216 		cx231xx_info("Alternate setting %i, max size= %i\n", i,
1217 			     dev->video_mode.alt_max_pkt_size[i]);
1218 	}
1219 
1220 	/* compute alternate max packet sizes for vbi */
1221 	uif = udev->actconfig->interface[dev->current_pcb_config.
1222 				       hs_config_info[0].interface_info.
1223 				       vanc_index + 1];
1224 
1225 	dev->vbi_mode.end_point_addr =
1226 	    uif->altsetting[0].endpoint[isoc_pipe].desc.
1227 			bEndpointAddress;
1228 
1229 	dev->vbi_mode.num_alt = uif->num_altsetting;
1230 	cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1231 		     dev->vbi_mode.end_point_addr,
1232 		     dev->vbi_mode.num_alt);
1233 	dev->vbi_mode.alt_max_pkt_size =
1234 	    kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
1235 
1236 	if (dev->vbi_mode.alt_max_pkt_size == NULL) {
1237 		cx231xx_errdev("out of memory!\n");
1238 		clear_bit(dev->devno, &cx231xx_devused);
1239 		v4l2_device_unregister(&dev->v4l2_dev);
1240 		kfree(dev);
1241 		dev = NULL;
1242 		return -ENOMEM;
1243 	}
1244 
1245 	for (i = 0; i < dev->vbi_mode.num_alt; i++) {
1246 		u16 tmp =
1247 		    le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1248 				desc.wMaxPacketSize);
1249 		dev->vbi_mode.alt_max_pkt_size[i] =
1250 		    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1251 		cx231xx_info("Alternate setting %i, max size= %i\n", i,
1252 			     dev->vbi_mode.alt_max_pkt_size[i]);
1253 	}
1254 
1255 	/* compute alternate max packet sizes for sliced CC */
1256 	uif = udev->actconfig->interface[dev->current_pcb_config.
1257 				       hs_config_info[0].interface_info.
1258 				       hanc_index + 1];
1259 
1260 	dev->sliced_cc_mode.end_point_addr =
1261 	    uif->altsetting[0].endpoint[isoc_pipe].desc.
1262 			bEndpointAddress;
1263 
1264 	dev->sliced_cc_mode.num_alt = uif->num_altsetting;
1265 	cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1266 		     dev->sliced_cc_mode.end_point_addr,
1267 		     dev->sliced_cc_mode.num_alt);
1268 	dev->sliced_cc_mode.alt_max_pkt_size =
1269 		kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
1270 
1271 	if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
1272 		cx231xx_errdev("out of memory!\n");
1273 		clear_bit(dev->devno, &cx231xx_devused);
1274 		v4l2_device_unregister(&dev->v4l2_dev);
1275 		kfree(dev);
1276 		dev = NULL;
1277 		return -ENOMEM;
1278 	}
1279 
1280 	for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
1281 		u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1282 				desc.wMaxPacketSize);
1283 		dev->sliced_cc_mode.alt_max_pkt_size[i] =
1284 		    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1285 		cx231xx_info("Alternate setting %i, max size= %i\n", i,
1286 			     dev->sliced_cc_mode.alt_max_pkt_size[i]);
1287 	}
1288 
1289 	if (dev->current_pcb_config.ts1_source != 0xff) {
1290 		/* compute alternate max packet sizes for TS1 */
1291 		uif = udev->actconfig->interface[dev->current_pcb_config.
1292 					       hs_config_info[0].
1293 					       interface_info.
1294 					       ts1_index + 1];
1295 
1296 		dev->ts1_mode.end_point_addr =
1297 		    uif->altsetting[0].endpoint[isoc_pipe].
1298 				desc.bEndpointAddress;
1299 
1300 		dev->ts1_mode.num_alt = uif->num_altsetting;
1301 		cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1302 			     dev->ts1_mode.end_point_addr,
1303 			     dev->ts1_mode.num_alt);
1304 		dev->ts1_mode.alt_max_pkt_size =
1305 			kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
1306 
1307 		if (dev->ts1_mode.alt_max_pkt_size == NULL) {
1308 			cx231xx_errdev("out of memory!\n");
1309 			clear_bit(dev->devno, &cx231xx_devused);
1310 			v4l2_device_unregister(&dev->v4l2_dev);
1311 			kfree(dev);
1312 			dev = NULL;
1313 			return -ENOMEM;
1314 		}
1315 
1316 		for (i = 0; i < dev->ts1_mode.num_alt; i++) {
1317 			u16 tmp = le16_to_cpu(uif->altsetting[i].
1318 						endpoint[isoc_pipe].desc.
1319 						wMaxPacketSize);
1320 			dev->ts1_mode.alt_max_pkt_size[i] =
1321 			    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1322 			cx231xx_info("Alternate setting %i, max size= %i\n", i,
1323 				     dev->ts1_mode.alt_max_pkt_size[i]);
1324 		}
1325 	}
1326 
1327 	if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER) {
1328 		cx231xx_enable_OSC(dev);
1329 		cx231xx_reset_out(dev);
1330 		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
1331 	}
1332 
1333 	if (dev->model == CX231XX_BOARD_CNXT_RDE_253S)
1334 		cx231xx_sleep_s5h1432(dev);
1335 
1336 	/* load other modules required */
1337 	request_modules(dev);
1338 
1339 	return 0;
1340 }
1341 
1342 /*
1343  * cx231xx_usb_disconnect()
1344  * called when the device gets diconencted
1345  * video device will be unregistered on v4l2_close in case it is still open
1346  */
1347 static void cx231xx_usb_disconnect(struct usb_interface *interface)
1348 {
1349 	struct cx231xx *dev;
1350 
1351 	dev = usb_get_intfdata(interface);
1352 	usb_set_intfdata(interface, NULL);
1353 
1354 	if (!dev)
1355 		return;
1356 
1357 	if (!dev->udev)
1358 		return;
1359 
1360 	dev->state |= DEV_DISCONNECTED;
1361 
1362 	flush_request_modules(dev);
1363 
1364 	/* wait until all current v4l2 io is finished then deallocate
1365 	   resources */
1366 	mutex_lock(&dev->lock);
1367 
1368 	wake_up_interruptible_all(&dev->open);
1369 
1370 	if (dev->users) {
1371 		cx231xx_warn
1372 		    ("device %s is open! Deregistration and memory "
1373 		     "deallocation are deferred on close.\n",
1374 		     video_device_node_name(dev->vdev));
1375 
1376 		/* Even having users, it is safe to remove the RC i2c driver */
1377 		cx231xx_ir_exit(dev);
1378 
1379 		if (dev->USE_ISO)
1380 			cx231xx_uninit_isoc(dev);
1381 		else
1382 			cx231xx_uninit_bulk(dev);
1383 		wake_up_interruptible(&dev->wait_frame);
1384 		wake_up_interruptible(&dev->wait_stream);
1385 	} else {
1386 	}
1387 
1388 	cx231xx_close_extension(dev);
1389 
1390 	mutex_unlock(&dev->lock);
1391 
1392 	if (!dev->users)
1393 		cx231xx_release_resources(dev);
1394 }
1395 
1396 static struct usb_driver cx231xx_usb_driver = {
1397 	.name = "cx231xx",
1398 	.probe = cx231xx_usb_probe,
1399 	.disconnect = cx231xx_usb_disconnect,
1400 	.id_table = cx231xx_id_table,
1401 };
1402 
1403 module_usb_driver(cx231xx_usb_driver);
1404