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 	[CX231XX_BOARD_OTG102] = {
638 		.name = "Geniatech OTG102",
639 		.tuner_type = TUNER_ABSENT,
640 		.decoder = CX231XX_AVDECODER,
641 		.output_mode = OUT_MODE_VIP11,
642 		.ctl_pin_status_mask = 0xFFFFFFC4,
643 		.agc_analog_digital_select_gpio = 0x0c,
644 			/* According with PV CxPlrCAP.inf file */
645 		.gpio_pin_status_mask = 0x4001000,
646 		.norm = V4L2_STD_NTSC,
647 		.no_alt_vanc = 1,
648 		.external_av = 1,
649 		.dont_use_port_3 = 1,
650 		/*.has_417 = 1, */
651 		/* This board is believed to have a hardware encoding chip
652 		 * supporting mpeg1/2/4, but as the 417 is apparently not
653 		 * working for the reference board it is not here either. */
654 
655 		.input = {{
656 				.type = CX231XX_VMUX_COMPOSITE1,
657 				.vmux = CX231XX_VIN_2_1,
658 				.amux = CX231XX_AMUX_LINE_IN,
659 				.gpio = NULL,
660 			}, {
661 				.type = CX231XX_VMUX_SVIDEO,
662 				.vmux = CX231XX_VIN_1_1 |
663 					(CX231XX_VIN_1_2 << 8) |
664 					CX25840_SVIDEO_ON,
665 				.amux = CX231XX_AMUX_LINE_IN,
666 				.gpio = NULL,
667 			}
668 		},
669 	},
670 };
671 const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);
672 
673 /* table of devices that work with this driver */
674 struct usb_device_id cx231xx_id_table[] = {
675 	{USB_DEVICE(0x0572, 0x5A3C),
676 	 .driver_info = CX231XX_BOARD_UNKNOWN},
677 	{USB_DEVICE(0x0572, 0x58A2),
678 	 .driver_info = CX231XX_BOARD_CNXT_CARRAERA},
679 	{USB_DEVICE(0x0572, 0x58A1),
680 	 .driver_info = CX231XX_BOARD_CNXT_SHELBY},
681 	{USB_DEVICE(0x0572, 0x58A4),
682 	 .driver_info = CX231XX_BOARD_CNXT_RDE_253S},
683 	{USB_DEVICE(0x0572, 0x58A5),
684 	 .driver_info = CX231XX_BOARD_CNXT_RDU_253S},
685 	{USB_DEVICE(0x0572, 0x58A6),
686 	 .driver_info = CX231XX_BOARD_CNXT_VIDEO_GRABBER},
687 	{USB_DEVICE(0x0572, 0x589E),
688 	 .driver_info = CX231XX_BOARD_CNXT_RDE_250},
689 	{USB_DEVICE(0x0572, 0x58A0),
690 	 .driver_info = CX231XX_BOARD_CNXT_RDU_250},
691 	{USB_DEVICE(0x2040, 0xb110),
692 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL},
693 	{USB_DEVICE(0x2040, 0xb111),
694 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC},
695 	{USB_DEVICE(0x2040, 0xb120),
696 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
697 	{USB_DEVICE(0x2040, 0xb140),
698 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
699 	{USB_DEVICE(0x2040, 0xc200),
700 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USBLIVE2},
701 	{USB_DEVICE_VER(USB_VID_PIXELVIEW, USB_PID_PIXELVIEW_SBTVD, 0x4000, 0x4001),
702 	 .driver_info = CX231XX_BOARD_PV_PLAYTV_USB_HYBRID},
703 	{USB_DEVICE(USB_VID_PIXELVIEW, 0x5014),
704 	 .driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},
705 	{USB_DEVICE(0x1b80, 0xe424),
706 	 .driver_info = CX231XX_BOARD_KWORLD_UB430_USB_HYBRID},
707 	{USB_DEVICE(0x1f4d, 0x0237),
708 	 .driver_info = CX231XX_BOARD_ICONBIT_U100},
709 	{USB_DEVICE(0x0fd9, 0x0037),
710 	 .driver_info = CX231XX_BOARD_ELGATO_VIDEO_CAPTURE_V2},
711 	{USB_DEVICE(0x1f4d, 0x0102),
712 	 .driver_info = CX231XX_BOARD_OTG102},
713 	{},
714 };
715 
716 MODULE_DEVICE_TABLE(usb, cx231xx_id_table);
717 
718 /* cx231xx_tuner_callback
719  * will be used to reset XC5000 tuner using GPIO pin
720  */
721 
722 int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
723 {
724 	int rc = 0;
725 	struct cx231xx *dev = ptr;
726 
727 	if (dev->tuner_type == TUNER_XC5000) {
728 		if (command == XC5000_TUNER_RESET) {
729 			cx231xx_info
730 				("Tuner CB: RESET: cmd %d : tuner type %d \n",
731 				 command, dev->tuner_type);
732 			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
733 					       1);
734 			msleep(10);
735 			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
736 					       0);
737 			msleep(330);
738 			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
739 					       1);
740 			msleep(10);
741 		}
742 	} else if (dev->tuner_type == TUNER_NXP_TDA18271) {
743 		switch (command) {
744 		case TDA18271_CALLBACK_CMD_AGC_ENABLE:
745 			if (dev->model == CX231XX_BOARD_PV_PLAYTV_USB_HYBRID)
746 				rc = cx231xx_set_agc_analog_digital_mux_select(dev, arg);
747 			break;
748 		default:
749 			rc = -EINVAL;
750 			break;
751 		}
752 	}
753 	return rc;
754 }
755 EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);
756 
757 static void cx231xx_reset_out(struct cx231xx *dev)
758 {
759 	cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
760 	msleep(200);
761 	cx231xx_set_gpio_value(dev, CX23417_RESET, 0);
762 	msleep(200);
763 	cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
764 }
765 
766 static void cx231xx_enable_OSC(struct cx231xx *dev)
767 {
768 	cx231xx_set_gpio_value(dev, CX23417_OSC_EN, 1);
769 }
770 
771 static void cx231xx_sleep_s5h1432(struct cx231xx *dev)
772 {
773 	cx231xx_set_gpio_value(dev, SLEEP_S5H1432, 0);
774 }
775 
776 static inline void cx231xx_set_model(struct cx231xx *dev)
777 {
778 	dev->board = cx231xx_boards[dev->model];
779 }
780 
781 /* Since cx231xx_pre_card_setup() requires a proper dev->model,
782  * this won't work for boards with generic PCI IDs
783  */
784 void cx231xx_pre_card_setup(struct cx231xx *dev)
785 {
786 
787 	cx231xx_set_model(dev);
788 
789 	cx231xx_info("Identified as %s (card=%d)\n",
790 		     dev->board.name, dev->model);
791 
792 	/* set the direction for GPIO pins */
793 	if (dev->board.tuner_gpio) {
794 		cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
795 		cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
796 	}
797 	if (dev->board.tuner_sif_gpio >= 0)
798 		cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
799 
800 	/* request some modules if any required */
801 
802 	/* set the mode to Analog mode initially */
803 	cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
804 
805 	/* Unlock device */
806 	/* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
807 
808 }
809 
810 static void cx231xx_config_tuner(struct cx231xx *dev)
811 {
812 	struct tuner_setup tun_setup;
813 	struct v4l2_frequency f;
814 
815 	if (dev->tuner_type == TUNER_ABSENT)
816 		return;
817 
818 	tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
819 	tun_setup.type = dev->tuner_type;
820 	tun_setup.addr = dev->tuner_addr;
821 	tun_setup.tuner_callback = cx231xx_tuner_callback;
822 
823 	tuner_call(dev, tuner, s_type_addr, &tun_setup);
824 
825 #if 0
826 	if (tun_setup.type == TUNER_XC5000) {
827 		static struct xc2028_ctrl ctrl = {
828 			.fname = XC5000_DEFAULT_FIRMWARE,
829 			.max_len = 64,
830 			.demod = 0;
831 		};
832 		struct v4l2_priv_tun_config cfg = {
833 			.tuner = dev->tuner_type,
834 			.priv = &ctrl,
835 		};
836 		tuner_call(dev, tuner, s_config, &cfg);
837 	}
838 #endif
839 	/* configure tuner */
840 	f.tuner = 0;
841 	f.type = V4L2_TUNER_ANALOG_TV;
842 	f.frequency = 9076;	/* just a magic number */
843 	dev->ctl_freq = f.frequency;
844 	call_all(dev, tuner, s_frequency, &f);
845 
846 }
847 
848 void cx231xx_card_setup(struct cx231xx *dev)
849 {
850 
851 	cx231xx_set_model(dev);
852 
853 	dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
854 	if (cx231xx_boards[dev->model].tuner_addr)
855 		dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
856 
857 	/* request some modules */
858 	if (dev->board.decoder == CX231XX_AVDECODER) {
859 		dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
860 					&dev->i2c_bus[0].i2c_adap,
861 					"cx25840", 0x88 >> 1, NULL);
862 		if (dev->sd_cx25840 == NULL)
863 			cx231xx_info("cx25840 subdev registration failure\n");
864 		cx25840_call(dev, core, load_fw);
865 
866 	}
867 
868 	/* Initialize the tuner */
869 	if (dev->board.tuner_type != TUNER_ABSENT) {
870 		dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
871 						    &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
872 						    "tuner",
873 						    dev->tuner_addr, NULL);
874 		if (dev->sd_tuner == NULL)
875 			cx231xx_info("tuner subdev registration failure\n");
876 		else
877 			cx231xx_config_tuner(dev);
878 	}
879 }
880 
881 /*
882  * cx231xx_config()
883  * inits registers with sane defaults
884  */
885 int cx231xx_config(struct cx231xx *dev)
886 {
887 	/* TBD need to add cx231xx specific code */
888 
889 	return 0;
890 }
891 
892 /*
893  * cx231xx_config_i2c()
894  * configure i2c attached devices
895  */
896 void cx231xx_config_i2c(struct cx231xx *dev)
897 {
898 	/* u32 input = INPUT(dev->video_input)->vmux; */
899 
900 	call_all(dev, video, s_stream, 1);
901 }
902 
903 /*
904  * cx231xx_realease_resources()
905  * unregisters the v4l2,i2c and usb devices
906  * called when the device gets disconected or at module unload
907 */
908 void cx231xx_release_resources(struct cx231xx *dev)
909 {
910 	cx231xx_release_analog_resources(dev);
911 
912 	cx231xx_remove_from_devlist(dev);
913 
914 	cx231xx_ir_exit(dev);
915 
916 	/* Release I2C buses */
917 	cx231xx_dev_uninit(dev);
918 
919 	/* delete v4l2 device */
920 	v4l2_device_unregister(&dev->v4l2_dev);
921 
922 	usb_put_dev(dev->udev);
923 
924 	/* Mark device as unused */
925 	clear_bit(dev->devno, &cx231xx_devused);
926 
927 	kfree(dev->video_mode.alt_max_pkt_size);
928 	kfree(dev->vbi_mode.alt_max_pkt_size);
929 	kfree(dev->sliced_cc_mode.alt_max_pkt_size);
930 	kfree(dev->ts1_mode.alt_max_pkt_size);
931 	kfree(dev);
932 }
933 
934 /*
935  * cx231xx_init_dev()
936  * allocates and inits the device structs, registers i2c bus and v4l device
937  */
938 static int cx231xx_init_dev(struct cx231xx *dev, struct usb_device *udev,
939 			    int minor)
940 {
941 	int retval = -ENOMEM;
942 	int errCode;
943 	unsigned int maxh, maxw;
944 
945 	dev->udev = udev;
946 	mutex_init(&dev->lock);
947 	mutex_init(&dev->ctrl_urb_lock);
948 	mutex_init(&dev->gpio_i2c_lock);
949 	mutex_init(&dev->i2c_lock);
950 
951 	spin_lock_init(&dev->video_mode.slock);
952 	spin_lock_init(&dev->vbi_mode.slock);
953 	spin_lock_init(&dev->sliced_cc_mode.slock);
954 
955 	init_waitqueue_head(&dev->open);
956 	init_waitqueue_head(&dev->wait_frame);
957 	init_waitqueue_head(&dev->wait_stream);
958 
959 	dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
960 	dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
961 	dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
962 	dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
963 	dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
964 
965 	/* Query cx231xx to find what pcb config it is related to */
966 	initialize_cx231xx(dev);
967 
968 	/*To workaround error number=-71 on EP0 for VideoGrabber,
969 		 need set alt here.*/
970 	if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER ||
971 	    dev->model == CX231XX_BOARD_HAUPPAUGE_USBLIVE2) {
972 		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
973 		cx231xx_set_alt_setting(dev, INDEX_VANC, 1);
974 	}
975 	/* Cx231xx pre card setup */
976 	cx231xx_pre_card_setup(dev);
977 
978 	errCode = cx231xx_config(dev);
979 	if (errCode) {
980 		cx231xx_errdev("error configuring device\n");
981 		return -ENOMEM;
982 	}
983 
984 	/* set default norm */
985 	dev->norm = dev->board.norm;
986 
987 	/* register i2c bus */
988 	errCode = cx231xx_dev_init(dev);
989 	if (errCode < 0) {
990 		cx231xx_dev_uninit(dev);
991 		cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
992 			       __func__, errCode);
993 		return errCode;
994 	}
995 
996 	/* Do board specific init */
997 	cx231xx_card_setup(dev);
998 
999 	/* configure the device */
1000 	cx231xx_config_i2c(dev);
1001 
1002 	maxw = norm_maxw(dev);
1003 	maxh = norm_maxh(dev);
1004 
1005 	/* set default image size */
1006 	dev->width = maxw;
1007 	dev->height = maxh;
1008 	dev->interlaced = 0;
1009 	dev->video_input = 0;
1010 
1011 	errCode = cx231xx_config(dev);
1012 	if (errCode < 0) {
1013 		cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
1014 			       __func__, errCode);
1015 		return errCode;
1016 	}
1017 
1018 	/* init video dma queues */
1019 	INIT_LIST_HEAD(&dev->video_mode.vidq.active);
1020 	INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
1021 
1022 	/* init vbi dma queues */
1023 	INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
1024 	INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
1025 
1026 	/* Reset other chips required if they are tied up with GPIO pins */
1027 	cx231xx_add_into_devlist(dev);
1028 
1029 	if (dev->board.has_417) {
1030 		printk(KERN_INFO "attach 417 %d\n", dev->model);
1031 		if (cx231xx_417_register(dev) < 0) {
1032 			printk(KERN_ERR
1033 				"%s() Failed to register 417 on VID_B\n",
1034 			       __func__);
1035 		}
1036 	}
1037 
1038 	retval = cx231xx_register_analog_devices(dev);
1039 	if (retval < 0) {
1040 		cx231xx_release_resources(dev);
1041 		return retval;
1042 	}
1043 
1044 	cx231xx_ir_init(dev);
1045 
1046 	cx231xx_init_extension(dev);
1047 
1048 	return 0;
1049 }
1050 
1051 #if defined(CONFIG_MODULES) && defined(MODULE)
1052 static void request_module_async(struct work_struct *work)
1053 {
1054 	struct cx231xx *dev = container_of(work,
1055 					   struct cx231xx, request_module_wk);
1056 
1057 	if (dev->has_alsa_audio)
1058 		request_module("cx231xx-alsa");
1059 
1060 	if (dev->board.has_dvb)
1061 		request_module("cx231xx-dvb");
1062 
1063 }
1064 
1065 static void request_modules(struct cx231xx *dev)
1066 {
1067 	INIT_WORK(&dev->request_module_wk, request_module_async);
1068 	schedule_work(&dev->request_module_wk);
1069 }
1070 
1071 static void flush_request_modules(struct cx231xx *dev)
1072 {
1073 	flush_work(&dev->request_module_wk);
1074 }
1075 #else
1076 #define request_modules(dev)
1077 #define flush_request_modules(dev)
1078 #endif /* CONFIG_MODULES */
1079 
1080 /*
1081  * cx231xx_usb_probe()
1082  * checks for supported devices
1083  */
1084 static int cx231xx_usb_probe(struct usb_interface *interface,
1085 			     const struct usb_device_id *id)
1086 {
1087 	struct usb_device *udev;
1088 	struct usb_interface *uif;
1089 	struct cx231xx *dev = NULL;
1090 	int retval = -ENODEV;
1091 	int nr = 0, ifnum;
1092 	int i, isoc_pipe = 0;
1093 	char *speed;
1094 	struct usb_interface_assoc_descriptor *assoc_desc;
1095 
1096 	udev = usb_get_dev(interface_to_usbdev(interface));
1097 	ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1098 
1099 	/*
1100 	 * Interface number 0 - IR interface (handled by mceusb driver)
1101 	 * Interface number 1 - AV interface (handled by this driver)
1102 	 */
1103 	if (ifnum != 1)
1104 		return -ENODEV;
1105 
1106 	/* Check to see next free device and mark as used */
1107 	do {
1108 		nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
1109 		if (nr >= CX231XX_MAXBOARDS) {
1110 			/* No free device slots */
1111 			cx231xx_err(DRIVER_NAME ": Supports only %i devices.\n",
1112 					CX231XX_MAXBOARDS);
1113 			return -ENOMEM;
1114 		}
1115 	} while (test_and_set_bit(nr, &cx231xx_devused));
1116 
1117 	/* allocate memory for our device state and initialize it */
1118 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1119 	if (dev == NULL) {
1120 		cx231xx_err(DRIVER_NAME ": out of memory!\n");
1121 		clear_bit(nr, &cx231xx_devused);
1122 		return -ENOMEM;
1123 	}
1124 
1125 	snprintf(dev->name, 29, "cx231xx #%d", nr);
1126 	dev->devno = nr;
1127 	dev->model = id->driver_info;
1128 	dev->video_mode.alt = -1;
1129 
1130 	dev->interface_count++;
1131 	/* reset gpio dir and value */
1132 	dev->gpio_dir = 0;
1133 	dev->gpio_val = 0;
1134 	dev->xc_fw_load_done = 0;
1135 	dev->has_alsa_audio = 1;
1136 	dev->power_mode = -1;
1137 	atomic_set(&dev->devlist_count, 0);
1138 
1139 	/* 0 - vbi ; 1 -sliced cc mode */
1140 	dev->vbi_or_sliced_cc_mode = 0;
1141 
1142 	/* get maximum no.of IAD interfaces */
1143 	assoc_desc = udev->actconfig->intf_assoc[0];
1144 	dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
1145 
1146 	/* init CIR module TBD */
1147 
1148 	/*mode_tv: digital=1 or analog=0*/
1149 	dev->mode_tv = 0;
1150 
1151 	dev->USE_ISO = transfer_mode;
1152 
1153 	switch (udev->speed) {
1154 	case USB_SPEED_LOW:
1155 		speed = "1.5";
1156 		break;
1157 	case USB_SPEED_UNKNOWN:
1158 	case USB_SPEED_FULL:
1159 		speed = "12";
1160 		break;
1161 	case USB_SPEED_HIGH:
1162 		speed = "480";
1163 		break;
1164 	default:
1165 		speed = "unknown";
1166 	}
1167 
1168 	cx231xx_info("New device %s %s @ %s Mbps "
1169 	     "(%04x:%04x) with %d interfaces\n",
1170 	     udev->manufacturer ? udev->manufacturer : "",
1171 	     udev->product ? udev->product : "",
1172 	     speed,
1173 	     le16_to_cpu(udev->descriptor.idVendor),
1174 	     le16_to_cpu(udev->descriptor.idProduct),
1175 	     dev->max_iad_interface_count);
1176 
1177 	/* increment interface count */
1178 	dev->interface_count++;
1179 
1180 	/* get device number */
1181 	nr = dev->devno;
1182 
1183 	assoc_desc = udev->actconfig->intf_assoc[0];
1184 	if (assoc_desc->bFirstInterface != ifnum) {
1185 		cx231xx_err(DRIVER_NAME ": Not found "
1186 			    "matching IAD interface\n");
1187 		clear_bit(dev->devno, &cx231xx_devused);
1188 		kfree(dev);
1189 		dev = NULL;
1190 		return -ENODEV;
1191 	}
1192 
1193 	cx231xx_info("registering interface %d\n", ifnum);
1194 
1195 	/* save our data pointer in this interface device */
1196 	usb_set_intfdata(interface, dev);
1197 
1198 	/*
1199 	 * AV device initialization - only done at the last interface
1200 	 */
1201 
1202 	/* Create v4l2 device */
1203 	retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1204 	if (retval) {
1205 		cx231xx_errdev("v4l2_device_register failed\n");
1206 		clear_bit(dev->devno, &cx231xx_devused);
1207 		kfree(dev);
1208 		dev = NULL;
1209 		return -EIO;
1210 	}
1211 	/* allocate device struct */
1212 	retval = cx231xx_init_dev(dev, udev, nr);
1213 	if (retval) {
1214 		clear_bit(dev->devno, &cx231xx_devused);
1215 		v4l2_device_unregister(&dev->v4l2_dev);
1216 		kfree(dev);
1217 		dev = NULL;
1218 		usb_set_intfdata(interface, NULL);
1219 
1220 		return retval;
1221 	}
1222 
1223 	/* compute alternate max packet sizes for video */
1224 	uif = udev->actconfig->interface[dev->current_pcb_config.
1225 		       hs_config_info[0].interface_info.video_index + 1];
1226 
1227 	dev->video_mode.end_point_addr = uif->altsetting[0].
1228 			endpoint[isoc_pipe].desc.bEndpointAddress;
1229 
1230 	dev->video_mode.num_alt = uif->num_altsetting;
1231 	cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1232 		     dev->video_mode.end_point_addr,
1233 		     dev->video_mode.num_alt);
1234 	dev->video_mode.alt_max_pkt_size =
1235 		kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
1236 
1237 	if (dev->video_mode.alt_max_pkt_size == NULL) {
1238 		cx231xx_errdev("out of memory!\n");
1239 		clear_bit(dev->devno, &cx231xx_devused);
1240 		v4l2_device_unregister(&dev->v4l2_dev);
1241 		kfree(dev);
1242 		dev = NULL;
1243 		return -ENOMEM;
1244 	}
1245 
1246 	for (i = 0; i < dev->video_mode.num_alt; i++) {
1247 		u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1248 				desc.wMaxPacketSize);
1249 		dev->video_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->video_mode.alt_max_pkt_size[i]);
1253 	}
1254 
1255 	/* compute alternate max packet sizes for vbi */
1256 	uif = udev->actconfig->interface[dev->current_pcb_config.
1257 				       hs_config_info[0].interface_info.
1258 				       vanc_index + 1];
1259 
1260 	dev->vbi_mode.end_point_addr =
1261 	    uif->altsetting[0].endpoint[isoc_pipe].desc.
1262 			bEndpointAddress;
1263 
1264 	dev->vbi_mode.num_alt = uif->num_altsetting;
1265 	cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1266 		     dev->vbi_mode.end_point_addr,
1267 		     dev->vbi_mode.num_alt);
1268 	dev->vbi_mode.alt_max_pkt_size =
1269 	    kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
1270 
1271 	if (dev->vbi_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->vbi_mode.num_alt; i++) {
1281 		u16 tmp =
1282 		    le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1283 				desc.wMaxPacketSize);
1284 		dev->vbi_mode.alt_max_pkt_size[i] =
1285 		    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1286 		cx231xx_info("Alternate setting %i, max size= %i\n", i,
1287 			     dev->vbi_mode.alt_max_pkt_size[i]);
1288 	}
1289 
1290 	/* compute alternate max packet sizes for sliced CC */
1291 	uif = udev->actconfig->interface[dev->current_pcb_config.
1292 				       hs_config_info[0].interface_info.
1293 				       hanc_index + 1];
1294 
1295 	dev->sliced_cc_mode.end_point_addr =
1296 	    uif->altsetting[0].endpoint[isoc_pipe].desc.
1297 			bEndpointAddress;
1298 
1299 	dev->sliced_cc_mode.num_alt = uif->num_altsetting;
1300 	cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1301 		     dev->sliced_cc_mode.end_point_addr,
1302 		     dev->sliced_cc_mode.num_alt);
1303 	dev->sliced_cc_mode.alt_max_pkt_size =
1304 		kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
1305 
1306 	if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
1307 		cx231xx_errdev("out of memory!\n");
1308 		clear_bit(dev->devno, &cx231xx_devused);
1309 		v4l2_device_unregister(&dev->v4l2_dev);
1310 		kfree(dev);
1311 		dev = NULL;
1312 		return -ENOMEM;
1313 	}
1314 
1315 	for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
1316 		u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1317 				desc.wMaxPacketSize);
1318 		dev->sliced_cc_mode.alt_max_pkt_size[i] =
1319 		    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1320 		cx231xx_info("Alternate setting %i, max size= %i\n", i,
1321 			     dev->sliced_cc_mode.alt_max_pkt_size[i]);
1322 	}
1323 
1324 	if (dev->current_pcb_config.ts1_source != 0xff) {
1325 		/* compute alternate max packet sizes for TS1 */
1326 		uif = udev->actconfig->interface[dev->current_pcb_config.
1327 					       hs_config_info[0].
1328 					       interface_info.
1329 					       ts1_index + 1];
1330 
1331 		dev->ts1_mode.end_point_addr =
1332 		    uif->altsetting[0].endpoint[isoc_pipe].
1333 				desc.bEndpointAddress;
1334 
1335 		dev->ts1_mode.num_alt = uif->num_altsetting;
1336 		cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1337 			     dev->ts1_mode.end_point_addr,
1338 			     dev->ts1_mode.num_alt);
1339 		dev->ts1_mode.alt_max_pkt_size =
1340 			kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
1341 
1342 		if (dev->ts1_mode.alt_max_pkt_size == NULL) {
1343 			cx231xx_errdev("out of memory!\n");
1344 			clear_bit(dev->devno, &cx231xx_devused);
1345 			v4l2_device_unregister(&dev->v4l2_dev);
1346 			kfree(dev);
1347 			dev = NULL;
1348 			return -ENOMEM;
1349 		}
1350 
1351 		for (i = 0; i < dev->ts1_mode.num_alt; i++) {
1352 			u16 tmp = le16_to_cpu(uif->altsetting[i].
1353 						endpoint[isoc_pipe].desc.
1354 						wMaxPacketSize);
1355 			dev->ts1_mode.alt_max_pkt_size[i] =
1356 			    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1357 			cx231xx_info("Alternate setting %i, max size= %i\n", i,
1358 				     dev->ts1_mode.alt_max_pkt_size[i]);
1359 		}
1360 	}
1361 
1362 	if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER) {
1363 		cx231xx_enable_OSC(dev);
1364 		cx231xx_reset_out(dev);
1365 		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
1366 	}
1367 
1368 	if (dev->model == CX231XX_BOARD_CNXT_RDE_253S)
1369 		cx231xx_sleep_s5h1432(dev);
1370 
1371 	/* load other modules required */
1372 	request_modules(dev);
1373 
1374 	return 0;
1375 }
1376 
1377 /*
1378  * cx231xx_usb_disconnect()
1379  * called when the device gets diconencted
1380  * video device will be unregistered on v4l2_close in case it is still open
1381  */
1382 static void cx231xx_usb_disconnect(struct usb_interface *interface)
1383 {
1384 	struct cx231xx *dev;
1385 
1386 	dev = usb_get_intfdata(interface);
1387 	usb_set_intfdata(interface, NULL);
1388 
1389 	if (!dev)
1390 		return;
1391 
1392 	if (!dev->udev)
1393 		return;
1394 
1395 	dev->state |= DEV_DISCONNECTED;
1396 
1397 	flush_request_modules(dev);
1398 
1399 	/* wait until all current v4l2 io is finished then deallocate
1400 	   resources */
1401 	mutex_lock(&dev->lock);
1402 
1403 	wake_up_interruptible_all(&dev->open);
1404 
1405 	if (dev->users) {
1406 		cx231xx_warn
1407 		    ("device %s is open! Deregistration and memory "
1408 		     "deallocation are deferred on close.\n",
1409 		     video_device_node_name(dev->vdev));
1410 
1411 		/* Even having users, it is safe to remove the RC i2c driver */
1412 		cx231xx_ir_exit(dev);
1413 
1414 		if (dev->USE_ISO)
1415 			cx231xx_uninit_isoc(dev);
1416 		else
1417 			cx231xx_uninit_bulk(dev);
1418 		wake_up_interruptible(&dev->wait_frame);
1419 		wake_up_interruptible(&dev->wait_stream);
1420 	} else {
1421 	}
1422 
1423 	cx231xx_close_extension(dev);
1424 
1425 	mutex_unlock(&dev->lock);
1426 
1427 	if (!dev->users)
1428 		cx231xx_release_resources(dev);
1429 }
1430 
1431 static struct usb_driver cx231xx_usb_driver = {
1432 	.name = "cx231xx",
1433 	.probe = cx231xx_usb_probe,
1434 	.disconnect = cx231xx_usb_disconnect,
1435 	.id_table = cx231xx_id_table,
1436 };
1437 
1438 module_usb_driver(cx231xx_usb_driver);
1439