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