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 "cx231xx.h"
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include <linux/i2c.h>
29 #include <media/tuner.h>
30 #include <media/tveeprom.h>
31 #include <media/v4l2-common.h>
32 
33 #include <media/drv-intf/cx25840.h>
34 #include <media/dvb-usb-ids.h>
35 #include "xc5000.h"
36 #include "tda18271.h"
37 
38 
39 static int tuner = -1;
40 module_param(tuner, int, 0444);
41 MODULE_PARM_DESC(tuner, "tuner type");
42 
43 static int transfer_mode = 1;
44 module_param(transfer_mode, int, 0444);
45 MODULE_PARM_DESC(transfer_mode, "transfer mode (1-ISO or 0-BULK)");
46 
47 static unsigned int disable_ir;
48 module_param(disable_ir, int, 0444);
49 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
50 
51 /* Bitmask marking allocated devices from 0 to CX231XX_MAXBOARDS */
52 static unsigned long cx231xx_devused;
53 
54 /*
55  *  Reset sequences for analog/digital modes
56  */
57 
58 static struct cx231xx_reg_seq RDE250_XCV_TUNER[] = {
59 	{0x03, 0x01, 10},
60 	{0x03, 0x00, 30},
61 	{0x03, 0x01, 10},
62 	{-1, -1, -1},
63 };
64 
65 /*
66  *  Board definitions
67  */
68 struct cx231xx_board cx231xx_boards[] = {
69 	[CX231XX_BOARD_UNKNOWN] = {
70 		.name = "Unknown CX231xx video grabber",
71 		.tuner_type = TUNER_ABSENT,
72 		.input = {{
73 				.type = CX231XX_VMUX_TELEVISION,
74 				.vmux = CX231XX_VIN_3_1,
75 				.amux = CX231XX_AMUX_VIDEO,
76 				.gpio = NULL,
77 			}, {
78 				.type = CX231XX_VMUX_COMPOSITE1,
79 				.vmux = CX231XX_VIN_2_1,
80 				.amux = CX231XX_AMUX_LINE_IN,
81 				.gpio = NULL,
82 			}, {
83 				.type = CX231XX_VMUX_SVIDEO,
84 				.vmux = CX231XX_VIN_1_1 |
85 					(CX231XX_VIN_1_2 << 8) |
86 					CX25840_SVIDEO_ON,
87 				.amux = CX231XX_AMUX_LINE_IN,
88 				.gpio = NULL,
89 			}
90 		},
91 	},
92 	[CX231XX_BOARD_CNXT_CARRAERA] = {
93 		.name = "Conexant Hybrid TV - CARRAERA",
94 		.tuner_type = TUNER_XC5000,
95 		.tuner_addr = 0x61,
96 		.tuner_gpio = RDE250_XCV_TUNER,
97 		.tuner_sif_gpio = 0x05,
98 		.tuner_scl_gpio = 0x1a,
99 		.tuner_sda_gpio = 0x1b,
100 		.decoder = CX231XX_AVDECODER,
101 		.output_mode = OUT_MODE_VIP11,
102 		.demod_xfer_mode = 0,
103 		.ctl_pin_status_mask = 0xFFFFFFC4,
104 		.agc_analog_digital_select_gpio = 0x0c,
105 		.gpio_pin_status_mask = 0x4001000,
106 		.tuner_i2c_master = I2C_1_MUX_3,
107 		.demod_i2c_master = I2C_2,
108 		.has_dvb = 1,
109 		.demod_addr = 0x02,
110 		.norm = V4L2_STD_PAL,
111 
112 		.input = {{
113 				.type = CX231XX_VMUX_TELEVISION,
114 				.vmux = CX231XX_VIN_3_1,
115 				.amux = CX231XX_AMUX_VIDEO,
116 				.gpio = NULL,
117 			}, {
118 				.type = CX231XX_VMUX_COMPOSITE1,
119 				.vmux = CX231XX_VIN_2_1,
120 				.amux = CX231XX_AMUX_LINE_IN,
121 				.gpio = NULL,
122 			}, {
123 				.type = CX231XX_VMUX_SVIDEO,
124 				.vmux = CX231XX_VIN_1_1 |
125 					(CX231XX_VIN_1_2 << 8) |
126 					CX25840_SVIDEO_ON,
127 				.amux = CX231XX_AMUX_LINE_IN,
128 				.gpio = NULL,
129 			}
130 		},
131 	},
132 	[CX231XX_BOARD_CNXT_SHELBY] = {
133 		.name = "Conexant Hybrid TV - SHELBY",
134 		.tuner_type = TUNER_XC5000,
135 		.tuner_addr = 0x61,
136 		.tuner_gpio = RDE250_XCV_TUNER,
137 		.tuner_sif_gpio = 0x05,
138 		.tuner_scl_gpio = 0x1a,
139 		.tuner_sda_gpio = 0x1b,
140 		.decoder = CX231XX_AVDECODER,
141 		.output_mode = OUT_MODE_VIP11,
142 		.demod_xfer_mode = 0,
143 		.ctl_pin_status_mask = 0xFFFFFFC4,
144 		.agc_analog_digital_select_gpio = 0x0c,
145 		.gpio_pin_status_mask = 0x4001000,
146 		.tuner_i2c_master = I2C_1_MUX_3,
147 		.demod_i2c_master = I2C_2,
148 		.has_dvb = 1,
149 		.demod_addr = 0x32,
150 		.norm = V4L2_STD_NTSC,
151 
152 		.input = {{
153 				.type = CX231XX_VMUX_TELEVISION,
154 				.vmux = CX231XX_VIN_3_1,
155 				.amux = CX231XX_AMUX_VIDEO,
156 				.gpio = NULL,
157 			}, {
158 				.type = CX231XX_VMUX_COMPOSITE1,
159 				.vmux = CX231XX_VIN_2_1,
160 				.amux = CX231XX_AMUX_LINE_IN,
161 				.gpio = NULL,
162 			}, {
163 				.type = CX231XX_VMUX_SVIDEO,
164 				.vmux = CX231XX_VIN_1_1 |
165 					(CX231XX_VIN_1_2 << 8) |
166 					CX25840_SVIDEO_ON,
167 				.amux = CX231XX_AMUX_LINE_IN,
168 				.gpio = NULL,
169 			}
170 		},
171 	},
172 	[CX231XX_BOARD_CNXT_RDE_253S] = {
173 		.name = "Conexant Hybrid TV - RDE253S",
174 		.tuner_type = TUNER_NXP_TDA18271,
175 		.tuner_addr = 0x60,
176 		.tuner_gpio = RDE250_XCV_TUNER,
177 		.tuner_sif_gpio = 0x05,
178 		.tuner_scl_gpio = 0x1a,
179 		.tuner_sda_gpio = 0x1b,
180 		.decoder = CX231XX_AVDECODER,
181 		.output_mode = OUT_MODE_VIP11,
182 		.demod_xfer_mode = 0,
183 		.ctl_pin_status_mask = 0xFFFFFFC4,
184 		.agc_analog_digital_select_gpio = 0x1c,
185 		.gpio_pin_status_mask = 0x4001000,
186 		.tuner_i2c_master = I2C_1_MUX_3,
187 		.demod_i2c_master = I2C_2,
188 		.has_dvb = 1,
189 		.demod_addr = 0x02,
190 		.norm = V4L2_STD_PAL,
191 
192 		.input = {{
193 				.type = CX231XX_VMUX_TELEVISION,
194 				.vmux = CX231XX_VIN_3_1,
195 				.amux = CX231XX_AMUX_VIDEO,
196 				.gpio = NULL,
197 			}, {
198 				.type = CX231XX_VMUX_COMPOSITE1,
199 				.vmux = CX231XX_VIN_2_1,
200 				.amux = CX231XX_AMUX_LINE_IN,
201 				.gpio = NULL,
202 			}, {
203 				.type = CX231XX_VMUX_SVIDEO,
204 				.vmux = CX231XX_VIN_1_1 |
205 					(CX231XX_VIN_1_2 << 8) |
206 					CX25840_SVIDEO_ON,
207 				.amux = CX231XX_AMUX_LINE_IN,
208 				.gpio = NULL,
209 			}
210 		},
211 	},
212 
213 	[CX231XX_BOARD_CNXT_RDU_253S] = {
214 		.name = "Conexant Hybrid TV - RDU253S",
215 		.tuner_type = TUNER_NXP_TDA18271,
216 		.tuner_addr = 0x60,
217 		.tuner_gpio = RDE250_XCV_TUNER,
218 		.tuner_sif_gpio = 0x05,
219 		.tuner_scl_gpio = 0x1a,
220 		.tuner_sda_gpio = 0x1b,
221 		.decoder = CX231XX_AVDECODER,
222 		.output_mode = OUT_MODE_VIP11,
223 		.demod_xfer_mode = 0,
224 		.ctl_pin_status_mask = 0xFFFFFFC4,
225 		.agc_analog_digital_select_gpio = 0x1c,
226 		.gpio_pin_status_mask = 0x4001000,
227 		.tuner_i2c_master = I2C_1_MUX_3,
228 		.demod_i2c_master = I2C_2,
229 		.has_dvb = 1,
230 		.demod_addr = 0x02,
231 		.norm = V4L2_STD_PAL,
232 
233 		.input = {{
234 				.type = CX231XX_VMUX_TELEVISION,
235 				.vmux = CX231XX_VIN_3_1,
236 				.amux = CX231XX_AMUX_VIDEO,
237 				.gpio = NULL,
238 			}, {
239 				.type = CX231XX_VMUX_COMPOSITE1,
240 				.vmux = CX231XX_VIN_2_1,
241 				.amux = CX231XX_AMUX_LINE_IN,
242 				.gpio = NULL,
243 			}, {
244 				.type = CX231XX_VMUX_SVIDEO,
245 				.vmux = CX231XX_VIN_1_1 |
246 					(CX231XX_VIN_1_2 << 8) |
247 					CX25840_SVIDEO_ON,
248 				.amux = CX231XX_AMUX_LINE_IN,
249 				.gpio = NULL,
250 			}
251 		},
252 	},
253 	[CX231XX_BOARD_CNXT_VIDEO_GRABBER] = {
254 		.name = "Conexant VIDEO GRABBER",
255 		.tuner_type = TUNER_ABSENT,
256 		.decoder = CX231XX_AVDECODER,
257 		.output_mode = OUT_MODE_VIP11,
258 		.ctl_pin_status_mask = 0xFFFFFFC4,
259 		.agc_analog_digital_select_gpio = 0x1c,
260 		.gpio_pin_status_mask = 0x4001000,
261 		.norm = V4L2_STD_PAL,
262 		.no_alt_vanc = 1,
263 		.external_av = 1,
264 		/* Actually, it has a 417, but it isn't working correctly.
265 		 * So set to 0 for now until someone can manage to get this
266 		 * to work reliably. */
267 		.has_417 = 0,
268 
269 		.input = {{
270 				.type = CX231XX_VMUX_COMPOSITE1,
271 				.vmux = CX231XX_VIN_2_1,
272 				.amux = CX231XX_AMUX_LINE_IN,
273 				.gpio = NULL,
274 			}, {
275 				.type = CX231XX_VMUX_SVIDEO,
276 				.vmux = CX231XX_VIN_1_1 |
277 					(CX231XX_VIN_1_2 << 8) |
278 					CX25840_SVIDEO_ON,
279 				.amux = CX231XX_AMUX_LINE_IN,
280 				.gpio = NULL,
281 			}
282 		},
283 	},
284 	[CX231XX_BOARD_CNXT_RDE_250] = {
285 		.name = "Conexant Hybrid TV - rde 250",
286 		.tuner_type = TUNER_XC5000,
287 		.tuner_addr = 0x61,
288 		.tuner_gpio = RDE250_XCV_TUNER,
289 		.tuner_sif_gpio = 0x05,
290 		.tuner_scl_gpio = 0x1a,
291 		.tuner_sda_gpio = 0x1b,
292 		.decoder = CX231XX_AVDECODER,
293 		.output_mode = OUT_MODE_VIP11,
294 		.demod_xfer_mode = 0,
295 		.ctl_pin_status_mask = 0xFFFFFFC4,
296 		.agc_analog_digital_select_gpio = 0x0c,
297 		.gpio_pin_status_mask = 0x4001000,
298 		.tuner_i2c_master = I2C_1_MUX_3,
299 		.demod_i2c_master = I2C_2,
300 		.has_dvb = 1,
301 		.demod_addr = 0x02,
302 		.norm = V4L2_STD_PAL,
303 
304 		.input = {{
305 				.type = CX231XX_VMUX_TELEVISION,
306 				.vmux = CX231XX_VIN_2_1,
307 				.amux = CX231XX_AMUX_VIDEO,
308 				.gpio = NULL,
309 			}
310 		},
311 	},
312 	[CX231XX_BOARD_CNXT_RDU_250] = {
313 		.name = "Conexant Hybrid TV - RDU 250",
314 		.tuner_type = TUNER_XC5000,
315 		.tuner_addr = 0x61,
316 		.tuner_gpio = RDE250_XCV_TUNER,
317 		.tuner_sif_gpio = 0x05,
318 		.tuner_scl_gpio = 0x1a,
319 		.tuner_sda_gpio = 0x1b,
320 		.decoder = CX231XX_AVDECODER,
321 		.output_mode = OUT_MODE_VIP11,
322 		.demod_xfer_mode = 0,
323 		.ctl_pin_status_mask = 0xFFFFFFC4,
324 		.agc_analog_digital_select_gpio = 0x0c,
325 		.gpio_pin_status_mask = 0x4001000,
326 		.tuner_i2c_master = I2C_1_MUX_3,
327 		.demod_i2c_master = I2C_2,
328 		.has_dvb = 1,
329 		.demod_addr = 0x32,
330 		.norm = V4L2_STD_NTSC,
331 
332 		.input = {{
333 				.type = CX231XX_VMUX_TELEVISION,
334 				.vmux = CX231XX_VIN_2_1,
335 				.amux = CX231XX_AMUX_VIDEO,
336 				.gpio = NULL,
337 			}
338 		},
339 	},
340 	[CX231XX_BOARD_HAUPPAUGE_EXETER] = {
341 		.name = "Hauppauge EXETER",
342 		.tuner_type = TUNER_NXP_TDA18271,
343 		.tuner_addr = 0x60,
344 		.tuner_gpio = RDE250_XCV_TUNER,
345 		.tuner_sif_gpio = 0x05,
346 		.tuner_scl_gpio = 0x1a,
347 		.tuner_sda_gpio = 0x1b,
348 		.decoder = CX231XX_AVDECODER,
349 		.output_mode = OUT_MODE_VIP11,
350 		.demod_xfer_mode = 0,
351 		.ctl_pin_status_mask = 0xFFFFFFC4,
352 		.agc_analog_digital_select_gpio = 0x0c,
353 		.gpio_pin_status_mask = 0x4001000,
354 		.tuner_i2c_master = I2C_1_MUX_1,
355 		.demod_i2c_master = I2C_1_MUX_1,
356 		.has_dvb = 1,
357 		.demod_addr = 0x0e,
358 		.norm = V4L2_STD_NTSC,
359 
360 		.input = {{
361 			.type = CX231XX_VMUX_TELEVISION,
362 			.vmux = CX231XX_VIN_3_1,
363 			.amux = CX231XX_AMUX_VIDEO,
364 			.gpio = NULL,
365 		}, {
366 			.type = CX231XX_VMUX_COMPOSITE1,
367 			.vmux = CX231XX_VIN_2_1,
368 			.amux = CX231XX_AMUX_LINE_IN,
369 			.gpio = NULL,
370 		}, {
371 			.type = CX231XX_VMUX_SVIDEO,
372 			.vmux = CX231XX_VIN_1_1 |
373 				(CX231XX_VIN_1_2 << 8) |
374 				CX25840_SVIDEO_ON,
375 			.amux = CX231XX_AMUX_LINE_IN,
376 			.gpio = NULL,
377 		} },
378 	},
379 	[CX231XX_BOARD_HAUPPAUGE_USBLIVE2] = {
380 		.name = "Hauppauge USB Live 2",
381 		.tuner_type = TUNER_ABSENT,
382 		.decoder = CX231XX_AVDECODER,
383 		.output_mode = OUT_MODE_VIP11,
384 		.demod_xfer_mode = 0,
385 		.ctl_pin_status_mask = 0xFFFFFFC4,
386 		.agc_analog_digital_select_gpio = 0x0c,
387 		.gpio_pin_status_mask = 0x4001000,
388 		.norm = V4L2_STD_NTSC,
389 		.no_alt_vanc = 1,
390 		.external_av = 1,
391 		.input = {{
392 			.type = CX231XX_VMUX_COMPOSITE1,
393 			.vmux = CX231XX_VIN_2_1,
394 			.amux = CX231XX_AMUX_LINE_IN,
395 			.gpio = NULL,
396 		}, {
397 			.type = CX231XX_VMUX_SVIDEO,
398 			.vmux = CX231XX_VIN_1_1 |
399 				(CX231XX_VIN_1_2 << 8) |
400 				CX25840_SVIDEO_ON,
401 			.amux = CX231XX_AMUX_LINE_IN,
402 			.gpio = NULL,
403 		} },
404 	},
405 	[CX231XX_BOARD_KWORLD_UB430_USB_HYBRID] = {
406 		.name = "Kworld UB430 USB Hybrid",
407 		.tuner_type = TUNER_NXP_TDA18271,
408 		.tuner_addr = 0x60,
409 		.decoder = CX231XX_AVDECODER,
410 		.output_mode = OUT_MODE_VIP11,
411 		.demod_xfer_mode = 0,
412 		.ctl_pin_status_mask = 0xFFFFFFC4,
413 		.agc_analog_digital_select_gpio = 0x11,	/* According with PV cxPolaris.inf file */
414 		.tuner_sif_gpio = -1,
415 		.tuner_scl_gpio = -1,
416 		.tuner_sda_gpio = -1,
417 		.gpio_pin_status_mask = 0x4001000,
418 		.tuner_i2c_master = I2C_2,
419 		.demod_i2c_master = I2C_1_MUX_3,
420 		.ir_i2c_master = I2C_2,
421 		.has_dvb = 1,
422 		.demod_addr = 0x10,
423 		.norm = V4L2_STD_PAL_M,
424 		.input = {{
425 			.type = CX231XX_VMUX_TELEVISION,
426 			.vmux = CX231XX_VIN_3_1,
427 			.amux = CX231XX_AMUX_VIDEO,
428 			.gpio = NULL,
429 		}, {
430 			.type = CX231XX_VMUX_COMPOSITE1,
431 			.vmux = CX231XX_VIN_2_1,
432 			.amux = CX231XX_AMUX_LINE_IN,
433 			.gpio = NULL,
434 		}, {
435 			.type = CX231XX_VMUX_SVIDEO,
436 			.vmux = CX231XX_VIN_1_1 |
437 				(CX231XX_VIN_1_2 << 8) |
438 				CX25840_SVIDEO_ON,
439 			.amux = CX231XX_AMUX_LINE_IN,
440 			.gpio = NULL,
441 		} },
442 	},
443 	[CX231XX_BOARD_KWORLD_UB445_USB_HYBRID] = {
444 		.name = "Kworld UB445 USB Hybrid",
445 		.tuner_type = TUNER_NXP_TDA18271,
446 		.tuner_addr = 0x60,
447 		.decoder = CX231XX_AVDECODER,
448 		.output_mode = OUT_MODE_VIP11,
449 		.demod_xfer_mode = 0,
450 		.ctl_pin_status_mask = 0xFFFFFFC4,
451 		.agc_analog_digital_select_gpio = 0x11,	/* According with PV cxPolaris.inf file */
452 		.tuner_sif_gpio = -1,
453 		.tuner_scl_gpio = -1,
454 		.tuner_sda_gpio = -1,
455 		.gpio_pin_status_mask = 0x4001000,
456 		.tuner_i2c_master = I2C_2,
457 		.demod_i2c_master = I2C_1_MUX_3,
458 		.ir_i2c_master = I2C_2,
459 		.has_dvb = 1,
460 		.demod_addr = 0x10,
461 		.norm = V4L2_STD_NTSC_M,
462 		.input = {{
463 			.type = CX231XX_VMUX_TELEVISION,
464 			.vmux = CX231XX_VIN_3_1,
465 			.amux = CX231XX_AMUX_VIDEO,
466 			.gpio = NULL,
467 		}, {
468 			.type = CX231XX_VMUX_COMPOSITE1,
469 			.vmux = CX231XX_VIN_2_1,
470 			.amux = CX231XX_AMUX_LINE_IN,
471 			.gpio = NULL,
472 		}, {
473 			.type = CX231XX_VMUX_SVIDEO,
474 			.vmux = CX231XX_VIN_1_1 |
475 				(CX231XX_VIN_1_2 << 8) |
476 				CX25840_SVIDEO_ON,
477 			.amux = CX231XX_AMUX_LINE_IN,
478 			.gpio = NULL,
479 		} },
480 	},
481 	[CX231XX_BOARD_PV_PLAYTV_USB_HYBRID] = {
482 		.name = "Pixelview PlayTV USB Hybrid",
483 		.tuner_type = TUNER_NXP_TDA18271,
484 		.tuner_addr = 0x60,
485 		.decoder = CX231XX_AVDECODER,
486 		.output_mode = OUT_MODE_VIP11,
487 		.demod_xfer_mode = 0,
488 		.ctl_pin_status_mask = 0xFFFFFFC4,
489 		.agc_analog_digital_select_gpio = 0x1c,
490 		.tuner_sif_gpio = -1,
491 		.tuner_scl_gpio = -1,
492 		.tuner_sda_gpio = -1,
493 		.gpio_pin_status_mask = 0x4001000,
494 		.tuner_i2c_master = I2C_2,
495 		.demod_i2c_master = I2C_1_MUX_3,
496 		.ir_i2c_master = I2C_2,
497 		.rc_map_name = RC_MAP_PIXELVIEW_002T,
498 		.has_dvb = 1,
499 		.demod_addr = 0x10,
500 		.norm = V4L2_STD_PAL_M,
501 		.input = {{
502 			.type = CX231XX_VMUX_TELEVISION,
503 			.vmux = CX231XX_VIN_3_1,
504 			.amux = CX231XX_AMUX_VIDEO,
505 			.gpio = NULL,
506 		}, {
507 			.type = CX231XX_VMUX_COMPOSITE1,
508 			.vmux = CX231XX_VIN_2_1,
509 			.amux = CX231XX_AMUX_LINE_IN,
510 			.gpio = NULL,
511 		}, {
512 			.type = CX231XX_VMUX_SVIDEO,
513 			.vmux = CX231XX_VIN_1_1 |
514 				(CX231XX_VIN_1_2 << 8) |
515 				CX25840_SVIDEO_ON,
516 			.amux = CX231XX_AMUX_LINE_IN,
517 			.gpio = NULL,
518 		} },
519 	},
520 	[CX231XX_BOARD_PV_XCAPTURE_USB] = {
521 		.name = "Pixelview Xcapture USB",
522 		.tuner_type = TUNER_ABSENT,
523 		.decoder = CX231XX_AVDECODER,
524 		.output_mode = OUT_MODE_VIP11,
525 		.demod_xfer_mode = 0,
526 		.ctl_pin_status_mask = 0xFFFFFFC4,
527 		.agc_analog_digital_select_gpio = 0x0c,
528 		.gpio_pin_status_mask = 0x4001000,
529 		.norm = V4L2_STD_NTSC,
530 		.no_alt_vanc = 1,
531 		.external_av = 1,
532 
533 		.input = {{
534 				.type = CX231XX_VMUX_COMPOSITE1,
535 				.vmux = CX231XX_VIN_2_1,
536 				.amux = CX231XX_AMUX_LINE_IN,
537 				.gpio = NULL,
538 			}, {
539 				.type = CX231XX_VMUX_SVIDEO,
540 				.vmux = CX231XX_VIN_1_1 |
541 					(CX231XX_VIN_1_2 << 8) |
542 					CX25840_SVIDEO_ON,
543 				.amux = CX231XX_AMUX_LINE_IN,
544 				.gpio = NULL,
545 			}
546 		},
547 	},
548 
549 	[CX231XX_BOARD_ICONBIT_U100] = {
550 		.name = "Iconbit Analog Stick U100 FM",
551 		.tuner_type = TUNER_ABSENT,
552 		.decoder = CX231XX_AVDECODER,
553 		.output_mode = OUT_MODE_VIP11,
554 		.demod_xfer_mode = 0,
555 		.ctl_pin_status_mask = 0xFFFFFFC4,
556 		.agc_analog_digital_select_gpio = 0x1C,
557 		.gpio_pin_status_mask = 0x4001000,
558 
559 		.input = {{
560 			.type = CX231XX_VMUX_COMPOSITE1,
561 			.vmux = CX231XX_VIN_2_1,
562 			.amux = CX231XX_AMUX_LINE_IN,
563 			.gpio = NULL,
564 		}, {
565 			.type = CX231XX_VMUX_SVIDEO,
566 			.vmux = CX231XX_VIN_1_1 |
567 				(CX231XX_VIN_1_2 << 8) |
568 				CX25840_SVIDEO_ON,
569 			.amux = CX231XX_AMUX_LINE_IN,
570 			.gpio = NULL,
571 		} },
572 	},
573 	[CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL] = {
574 		.name = "Hauppauge WinTV USB2 FM (PAL)",
575 		.tuner_type = TUNER_NXP_TDA18271,
576 		.tuner_addr = 0x60,
577 		.tuner_gpio = RDE250_XCV_TUNER,
578 		.tuner_sif_gpio = 0x05,
579 		.tuner_scl_gpio = 0x1a,
580 		.tuner_sda_gpio = 0x1b,
581 		.decoder = CX231XX_AVDECODER,
582 		.output_mode = OUT_MODE_VIP11,
583 		.ctl_pin_status_mask = 0xFFFFFFC4,
584 		.agc_analog_digital_select_gpio = 0x0c,
585 		.gpio_pin_status_mask = 0x4001000,
586 		.tuner_i2c_master = I2C_1_MUX_3,
587 		.norm = V4L2_STD_PAL,
588 
589 		.input = {{
590 			.type = CX231XX_VMUX_TELEVISION,
591 			.vmux = CX231XX_VIN_3_1,
592 			.amux = CX231XX_AMUX_VIDEO,
593 			.gpio = NULL,
594 		}, {
595 			.type = CX231XX_VMUX_COMPOSITE1,
596 			.vmux = CX231XX_VIN_2_1,
597 			.amux = CX231XX_AMUX_LINE_IN,
598 			.gpio = NULL,
599 		}, {
600 			.type = CX231XX_VMUX_SVIDEO,
601 			.vmux = CX231XX_VIN_1_1 |
602 				(CX231XX_VIN_1_2 << 8) |
603 				CX25840_SVIDEO_ON,
604 			.amux = CX231XX_AMUX_LINE_IN,
605 			.gpio = NULL,
606 		} },
607 	},
608 	[CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC] = {
609 		.name = "Hauppauge WinTV USB2 FM (NTSC)",
610 		.tuner_type = TUNER_NXP_TDA18271,
611 		.tuner_addr = 0x60,
612 		.tuner_gpio = RDE250_XCV_TUNER,
613 		.tuner_sif_gpio = 0x05,
614 		.tuner_scl_gpio = 0x1a,
615 		.tuner_sda_gpio = 0x1b,
616 		.decoder = CX231XX_AVDECODER,
617 		.output_mode = OUT_MODE_VIP11,
618 		.ctl_pin_status_mask = 0xFFFFFFC4,
619 		.agc_analog_digital_select_gpio = 0x0c,
620 		.gpio_pin_status_mask = 0x4001000,
621 		.tuner_i2c_master = I2C_1_MUX_3,
622 		.norm = V4L2_STD_NTSC,
623 
624 		.input = {{
625 			.type = CX231XX_VMUX_TELEVISION,
626 			.vmux = CX231XX_VIN_3_1,
627 			.amux = CX231XX_AMUX_VIDEO,
628 			.gpio = NULL,
629 		}, {
630 			.type = CX231XX_VMUX_COMPOSITE1,
631 			.vmux = CX231XX_VIN_2_1,
632 			.amux = CX231XX_AMUX_LINE_IN,
633 			.gpio = NULL,
634 		}, {
635 			.type = CX231XX_VMUX_SVIDEO,
636 			.vmux = CX231XX_VIN_1_1 |
637 				(CX231XX_VIN_1_2 << 8) |
638 				CX25840_SVIDEO_ON,
639 			.amux = CX231XX_AMUX_LINE_IN,
640 			.gpio = NULL,
641 		} },
642 	},
643 	[CX231XX_BOARD_ELGATO_VIDEO_CAPTURE_V2] = {
644 		.name = "Elgato Video Capture V2",
645 		.tuner_type = TUNER_ABSENT,
646 		.decoder = CX231XX_AVDECODER,
647 		.output_mode = OUT_MODE_VIP11,
648 		.demod_xfer_mode = 0,
649 		.ctl_pin_status_mask = 0xFFFFFFC4,
650 		.agc_analog_digital_select_gpio = 0x0c,
651 		.gpio_pin_status_mask = 0x4001000,
652 		.norm = V4L2_STD_NTSC,
653 		.no_alt_vanc = 1,
654 		.external_av = 1,
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 	[CX231XX_BOARD_OTG102] = {
670 		.name = "Geniatech OTG102",
671 		.tuner_type = TUNER_ABSENT,
672 		.decoder = CX231XX_AVDECODER,
673 		.output_mode = OUT_MODE_VIP11,
674 		.ctl_pin_status_mask = 0xFFFFFFC4,
675 		.agc_analog_digital_select_gpio = 0x0c,
676 			/* According with PV CxPlrCAP.inf file */
677 		.gpio_pin_status_mask = 0x4001000,
678 		.norm = V4L2_STD_NTSC,
679 		.no_alt_vanc = 1,
680 		.external_av = 1,
681 		/*.has_417 = 1, */
682 		/* This board is believed to have a hardware encoding chip
683 		 * supporting mpeg1/2/4, but as the 417 is apparently not
684 		 * working for the reference board it is not here either. */
685 
686 		.input = {{
687 				.type = CX231XX_VMUX_COMPOSITE1,
688 				.vmux = CX231XX_VIN_2_1,
689 				.amux = CX231XX_AMUX_LINE_IN,
690 				.gpio = NULL,
691 			}, {
692 				.type = CX231XX_VMUX_SVIDEO,
693 				.vmux = CX231XX_VIN_1_1 |
694 					(CX231XX_VIN_1_2 << 8) |
695 					CX25840_SVIDEO_ON,
696 				.amux = CX231XX_AMUX_LINE_IN,
697 				.gpio = NULL,
698 			}
699 		},
700 	},
701 	[CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx] = {
702 		.name = "Hauppauge WinTV 930C-HD (1113xx) / HVR-900H (111xxx) / PCTV QuatroStick 521e",
703 		.tuner_type = TUNER_NXP_TDA18271,
704 		.tuner_addr = 0x60,
705 		.tuner_gpio = RDE250_XCV_TUNER,
706 		.tuner_sif_gpio = 0x05,
707 		.tuner_scl_gpio = 0x1a,
708 		.tuner_sda_gpio = 0x1b,
709 		.decoder = CX231XX_AVDECODER,
710 		.output_mode = OUT_MODE_VIP11,
711 		.demod_xfer_mode = 0,
712 		.ctl_pin_status_mask = 0xFFFFFFC4,
713 		.agc_analog_digital_select_gpio = 0x0c,
714 		.gpio_pin_status_mask = 0x4001000,
715 		.tuner_i2c_master = I2C_1_MUX_3,
716 		.demod_i2c_master = I2C_1_MUX_3,
717 		.has_dvb = 1,
718 		.demod_addr = 0x0e,
719 		.norm = V4L2_STD_PAL,
720 
721 		.input = {{
722 			.type = CX231XX_VMUX_TELEVISION,
723 			.vmux = CX231XX_VIN_3_1,
724 			.amux = CX231XX_AMUX_VIDEO,
725 			.gpio = NULL,
726 		}, {
727 			.type = CX231XX_VMUX_COMPOSITE1,
728 			.vmux = CX231XX_VIN_2_1,
729 			.amux = CX231XX_AMUX_LINE_IN,
730 			.gpio = NULL,
731 		}, {
732 			.type = CX231XX_VMUX_SVIDEO,
733 			.vmux = CX231XX_VIN_1_1 |
734 				(CX231XX_VIN_1_2 << 8) |
735 				CX25840_SVIDEO_ON,
736 			.amux = CX231XX_AMUX_LINE_IN,
737 			.gpio = NULL,
738 		} },
739 	},
740 	[CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx] = {
741 		.name = "Hauppauge WinTV 930C-HD (1114xx) / HVR-901H (1114xx) / PCTV QuatroStick 522e",
742 		.tuner_type = TUNER_ABSENT,
743 		.tuner_addr = 0x60,
744 		.tuner_gpio = RDE250_XCV_TUNER,
745 		.tuner_sif_gpio = 0x05,
746 		.tuner_scl_gpio = 0x1a,
747 		.tuner_sda_gpio = 0x1b,
748 		.decoder = CX231XX_AVDECODER,
749 		.output_mode = OUT_MODE_VIP11,
750 		.demod_xfer_mode = 0,
751 		.ctl_pin_status_mask = 0xFFFFFFC4,
752 		.agc_analog_digital_select_gpio = 0x0c,
753 		.gpio_pin_status_mask = 0x4001000,
754 		.tuner_i2c_master = I2C_1_MUX_3,
755 		.demod_i2c_master = I2C_1_MUX_3,
756 		.has_dvb = 1,
757 		.demod_addr = 0x0e,
758 		.norm = V4L2_STD_PAL,
759 
760 		.input = {{
761 			.type = CX231XX_VMUX_TELEVISION,
762 			.vmux = CX231XX_VIN_3_1,
763 			.amux = CX231XX_AMUX_VIDEO,
764 			.gpio = NULL,
765 		}, {
766 			.type = CX231XX_VMUX_COMPOSITE1,
767 			.vmux = CX231XX_VIN_2_1,
768 			.amux = CX231XX_AMUX_LINE_IN,
769 			.gpio = NULL,
770 		}, {
771 			.type = CX231XX_VMUX_SVIDEO,
772 			.vmux = CX231XX_VIN_1_1 |
773 				(CX231XX_VIN_1_2 << 8) |
774 				CX25840_SVIDEO_ON,
775 			.amux = CX231XX_AMUX_LINE_IN,
776 			.gpio = NULL,
777 		} },
778 	},
779 	[CX231XX_BOARD_HAUPPAUGE_955Q] = {
780 		.name = "Hauppauge WinTV-HVR-955Q (111401)",
781 		.tuner_type = TUNER_ABSENT,
782 		.tuner_addr = 0x60,
783 		.tuner_gpio = RDE250_XCV_TUNER,
784 		.tuner_sif_gpio = 0x05,
785 		.tuner_scl_gpio = 0x1a,
786 		.tuner_sda_gpio = 0x1b,
787 		.decoder = CX231XX_AVDECODER,
788 		.output_mode = OUT_MODE_VIP11,
789 		.demod_xfer_mode = 0,
790 		.ctl_pin_status_mask = 0xFFFFFFC4,
791 		.agc_analog_digital_select_gpio = 0x0c,
792 		.gpio_pin_status_mask = 0x4001000,
793 		.tuner_i2c_master = I2C_1_MUX_3,
794 		.demod_i2c_master = I2C_1_MUX_3,
795 		.has_dvb = 1,
796 		.demod_addr = 0x0e,
797 		.norm = V4L2_STD_NTSC,
798 
799 		.input = {{
800 			.type = CX231XX_VMUX_TELEVISION,
801 			.vmux = CX231XX_VIN_3_1,
802 			.amux = CX231XX_AMUX_VIDEO,
803 			.gpio = NULL,
804 		}, {
805 			.type = CX231XX_VMUX_COMPOSITE1,
806 			.vmux = CX231XX_VIN_2_1,
807 			.amux = CX231XX_AMUX_LINE_IN,
808 			.gpio = NULL,
809 		}, {
810 			.type = CX231XX_VMUX_SVIDEO,
811 			.vmux = CX231XX_VIN_1_1 |
812 				(CX231XX_VIN_1_2 << 8) |
813 				CX25840_SVIDEO_ON,
814 			.amux = CX231XX_AMUX_LINE_IN,
815 			.gpio = NULL,
816 		} },
817 	},
818 	[CX231XX_BOARD_TERRATEC_GRABBY] = {
819 		.name = "Terratec Grabby",
820 		.tuner_type = TUNER_ABSENT,
821 		.decoder = CX231XX_AVDECODER,
822 		.output_mode = OUT_MODE_VIP11,
823 		.demod_xfer_mode = 0,
824 		.ctl_pin_status_mask = 0xFFFFFFC4,
825 		.agc_analog_digital_select_gpio = 0x0c,
826 		.gpio_pin_status_mask = 0x4001000,
827 		.norm = V4L2_STD_PAL,
828 		.no_alt_vanc = 1,
829 		.external_av = 1,
830 		.input = {{
831 			.type = CX231XX_VMUX_COMPOSITE1,
832 			.vmux = CX231XX_VIN_2_1,
833 			.amux = CX231XX_AMUX_LINE_IN,
834 			.gpio = NULL,
835 		}, {
836 			.type = CX231XX_VMUX_SVIDEO,
837 			.vmux = CX231XX_VIN_1_1 |
838 				(CX231XX_VIN_1_2 << 8) |
839 				CX25840_SVIDEO_ON,
840 			.amux = CX231XX_AMUX_LINE_IN,
841 			.gpio = NULL,
842 		} },
843 	},
844 	[CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD] = {
845 		.name = "Evromedia USB Full Hybrid Full HD",
846 		.tuner_type = TUNER_ABSENT,
847 		.demod_addr = 0x64, /* 0xc8 >> 1 */
848 		.demod_i2c_master = I2C_1_MUX_3,
849 		.has_dvb = 1,
850 		.decoder = CX231XX_AVDECODER,
851 		.norm = V4L2_STD_PAL,
852 		.output_mode = OUT_MODE_VIP11,
853 		.tuner_addr = 0x60, /* 0xc0 >> 1 */
854 		.tuner_i2c_master = I2C_2,
855 		.input = {{
856 			.type = CX231XX_VMUX_TELEVISION,
857 			.vmux = 0,
858 			.amux = CX231XX_AMUX_VIDEO,
859 		}, {
860 			.type = CX231XX_VMUX_COMPOSITE1,
861 			.vmux = CX231XX_VIN_2_1,
862 			.amux = CX231XX_AMUX_LINE_IN,
863 		}, {
864 			.type = CX231XX_VMUX_SVIDEO,
865 			.vmux = CX231XX_VIN_1_1 |
866 				(CX231XX_VIN_1_2 << 8) |
867 				CX25840_SVIDEO_ON,
868 			.amux = CX231XX_AMUX_LINE_IN,
869 		} },
870 	},
871 	[CX231XX_BOARD_ASTROMETA_T2HYBRID] = {
872 		.name = "Astrometa T2hybrid",
873 		.tuner_type = TUNER_ABSENT,
874 		.has_dvb = 1,
875 		.decoder = CX231XX_AVDECODER,
876 		.output_mode = OUT_MODE_VIP11,
877 		.agc_analog_digital_select_gpio = 0x01,
878 		.ctl_pin_status_mask = 0xffffffc4,
879 		.demod_addr = 0x18, /* 0x30 >> 1 */
880 		.demod_i2c_master = I2C_1_MUX_1,
881 		.gpio_pin_status_mask = 0xa,
882 		.norm = V4L2_STD_NTSC,
883 		.tuner_addr = 0x3a, /* 0x74 >> 1 */
884 		.tuner_i2c_master = I2C_1_MUX_3,
885 		.tuner_scl_gpio = 0x1a,
886 		.tuner_sda_gpio = 0x1b,
887 		.tuner_sif_gpio = 0x05,
888 		.input = {{
889 				.type = CX231XX_VMUX_TELEVISION,
890 				.vmux = CX231XX_VIN_1_1,
891 				.amux = CX231XX_AMUX_VIDEO,
892 			}, {
893 				.type = CX231XX_VMUX_COMPOSITE1,
894 				.vmux = CX231XX_VIN_2_1,
895 				.amux = CX231XX_AMUX_LINE_IN,
896 			},
897 		},
898 	},
899 	[CX231XX_BOARD_THE_IMAGING_SOURCE_DFG_USB2_PRO] = {
900 		.name = "The Imaging Source DFG/USB2pro",
901 		.tuner_type = TUNER_ABSENT,
902 		.decoder = CX231XX_AVDECODER,
903 		.output_mode = OUT_MODE_VIP11,
904 		.demod_xfer_mode = 0,
905 		.ctl_pin_status_mask = 0xFFFFFFC4,
906 		.agc_analog_digital_select_gpio = 0x0c,
907 		.gpio_pin_status_mask = 0x4001000,
908 		.norm = V4L2_STD_PAL,
909 		.no_alt_vanc = 1,
910 		.external_av = 1,
911 		.input = {{
912 			.type = CX231XX_VMUX_COMPOSITE1,
913 			.vmux = CX231XX_VIN_1_1,
914 			.amux = CX231XX_AMUX_LINE_IN,
915 			.gpio = NULL,
916 		}, {
917 			.type = CX231XX_VMUX_SVIDEO,
918 			.vmux = CX231XX_VIN_2_1 |
919 				(CX231XX_VIN_2_2 << 8) |
920 				CX25840_SVIDEO_ON,
921 			.amux = CX231XX_AMUX_LINE_IN,
922 			.gpio = NULL,
923 		} },
924 	},
925 };
926 const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);
927 
928 /* table of devices that work with this driver */
929 struct usb_device_id cx231xx_id_table[] = {
930 	{USB_DEVICE(0x1D19, 0x6109),
931 	.driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},
932 	{USB_DEVICE(0x0572, 0x5A3C),
933 	 .driver_info = CX231XX_BOARD_UNKNOWN},
934 	{USB_DEVICE(0x0572, 0x58A2),
935 	 .driver_info = CX231XX_BOARD_CNXT_CARRAERA},
936 	{USB_DEVICE(0x0572, 0x58A1),
937 	 .driver_info = CX231XX_BOARD_CNXT_SHELBY},
938 	{USB_DEVICE(0x0572, 0x58A4),
939 	 .driver_info = CX231XX_BOARD_CNXT_RDE_253S},
940 	{USB_DEVICE(0x0572, 0x58A5),
941 	 .driver_info = CX231XX_BOARD_CNXT_RDU_253S},
942 	{USB_DEVICE(0x0572, 0x58A6),
943 	 .driver_info = CX231XX_BOARD_CNXT_VIDEO_GRABBER},
944 	{USB_DEVICE(0x0572, 0x589E),
945 	 .driver_info = CX231XX_BOARD_CNXT_RDE_250},
946 	{USB_DEVICE(0x0572, 0x58A0),
947 	 .driver_info = CX231XX_BOARD_CNXT_RDU_250},
948 	{USB_DEVICE(0x2040, 0xb110),
949 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL},
950 	{USB_DEVICE(0x2040, 0xb111),
951 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC},
952 	{USB_DEVICE(0x2040, 0xb120),
953 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
954 	{USB_DEVICE(0x2040, 0xb123),
955 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_955Q},
956 	{USB_DEVICE(0x2040, 0xb130),
957 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx},
958 	{USB_DEVICE(0x2040, 0xb131),
959 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx},
960 	/* Hauppauge WinTV-HVR-900-H */
961 	{USB_DEVICE(0x2040, 0xb138),
962 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx},
963 	/* Hauppauge WinTV-HVR-901-H */
964 	{USB_DEVICE(0x2040, 0xb139),
965 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx},
966 	{USB_DEVICE(0x2040, 0xb140),
967 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
968 	{USB_DEVICE(0x2040, 0xc200),
969 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_USBLIVE2},
970 	/* PCTV QuatroStick 521e */
971 	{USB_DEVICE(0x2013, 0x0259),
972 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx},
973 	/* PCTV QuatroStick 522e */
974 	{USB_DEVICE(0x2013, 0x025e),
975 	 .driver_info = CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx},
976 	{USB_DEVICE_VER(USB_VID_PIXELVIEW, USB_PID_PIXELVIEW_SBTVD, 0x4000, 0x4001),
977 	 .driver_info = CX231XX_BOARD_PV_PLAYTV_USB_HYBRID},
978 	{USB_DEVICE(USB_VID_PIXELVIEW, 0x5014),
979 	 .driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},
980 	{USB_DEVICE(0x1b80, 0xe424),
981 	 .driver_info = CX231XX_BOARD_KWORLD_UB430_USB_HYBRID},
982 	{USB_DEVICE(0x1b80, 0xe421),
983 	 .driver_info = CX231XX_BOARD_KWORLD_UB445_USB_HYBRID},
984 	{USB_DEVICE(0x1f4d, 0x0237),
985 	 .driver_info = CX231XX_BOARD_ICONBIT_U100},
986 	{USB_DEVICE(0x0fd9, 0x0037),
987 	 .driver_info = CX231XX_BOARD_ELGATO_VIDEO_CAPTURE_V2},
988 	{USB_DEVICE(0x1f4d, 0x0102),
989 	 .driver_info = CX231XX_BOARD_OTG102},
990 	{USB_DEVICE(USB_VID_TERRATEC, 0x00a6),
991 	 .driver_info = CX231XX_BOARD_TERRATEC_GRABBY},
992 	{USB_DEVICE(0x1b80, 0xd3b2),
993 	.driver_info = CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD},
994 	{USB_DEVICE(0x15f4, 0x0135),
995 	.driver_info = CX231XX_BOARD_ASTROMETA_T2HYBRID},
996 	{USB_DEVICE(0x199e, 0x8002),
997 	 .driver_info = CX231XX_BOARD_THE_IMAGING_SOURCE_DFG_USB2_PRO},
998 	{},
999 };
1000 
1001 MODULE_DEVICE_TABLE(usb, cx231xx_id_table);
1002 
1003 /* cx231xx_tuner_callback
1004  * will be used to reset XC5000 tuner using GPIO pin
1005  */
1006 
1007 int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
1008 {
1009 	int rc = 0;
1010 	struct cx231xx *dev = ptr;
1011 
1012 	if (dev->tuner_type == TUNER_XC5000) {
1013 		if (command == XC5000_TUNER_RESET) {
1014 			dev_dbg(dev->dev,
1015 				"Tuner CB: RESET: cmd %d : tuner type %d\n",
1016 				command, dev->tuner_type);
1017 			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
1018 					       1);
1019 			msleep(10);
1020 			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
1021 					       0);
1022 			msleep(330);
1023 			cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
1024 					       1);
1025 			msleep(10);
1026 		}
1027 	} else if (dev->tuner_type == TUNER_NXP_TDA18271) {
1028 		switch (command) {
1029 		case TDA18271_CALLBACK_CMD_AGC_ENABLE:
1030 			if (dev->model == CX231XX_BOARD_PV_PLAYTV_USB_HYBRID)
1031 				rc = cx231xx_set_agc_analog_digital_mux_select(dev, arg);
1032 			break;
1033 		default:
1034 			rc = -EINVAL;
1035 			break;
1036 		}
1037 	}
1038 	return rc;
1039 }
1040 EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);
1041 
1042 static void cx231xx_reset_out(struct cx231xx *dev)
1043 {
1044 	cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
1045 	msleep(200);
1046 	cx231xx_set_gpio_value(dev, CX23417_RESET, 0);
1047 	msleep(200);
1048 	cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
1049 }
1050 
1051 static void cx231xx_enable_OSC(struct cx231xx *dev)
1052 {
1053 	cx231xx_set_gpio_value(dev, CX23417_OSC_EN, 1);
1054 }
1055 
1056 static void cx231xx_sleep_s5h1432(struct cx231xx *dev)
1057 {
1058 	cx231xx_set_gpio_value(dev, SLEEP_S5H1432, 0);
1059 }
1060 
1061 static inline void cx231xx_set_model(struct cx231xx *dev)
1062 {
1063 	dev->board = cx231xx_boards[dev->model];
1064 }
1065 
1066 /* Since cx231xx_pre_card_setup() requires a proper dev->model,
1067  * this won't work for boards with generic PCI IDs
1068  */
1069 void cx231xx_pre_card_setup(struct cx231xx *dev)
1070 {
1071 	dev_info(dev->dev, "Identified as %s (card=%d)\n",
1072 		dev->board.name, dev->model);
1073 
1074 	if (CX231XX_BOARD_ASTROMETA_T2HYBRID == dev->model) {
1075 		/* turn on demodulator chip */
1076 		cx231xx_set_gpio_value(dev, 0x03, 0x01);
1077 	}
1078 
1079 	/* set the direction for GPIO pins */
1080 	if (dev->board.tuner_gpio) {
1081 		cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
1082 		cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
1083 	}
1084 	if (dev->board.tuner_sif_gpio >= 0)
1085 		cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
1086 
1087 	/* request some modules if any required */
1088 
1089 	/* set the mode to Analog mode initially */
1090 	cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1091 
1092 	/* Unlock device */
1093 	/* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
1094 
1095 }
1096 
1097 static void cx231xx_config_tuner(struct cx231xx *dev)
1098 {
1099 	struct tuner_setup tun_setup;
1100 	struct v4l2_frequency f;
1101 
1102 	if (dev->tuner_type == TUNER_ABSENT)
1103 		return;
1104 
1105 	tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
1106 	tun_setup.type = dev->tuner_type;
1107 	tun_setup.addr = dev->tuner_addr;
1108 	tun_setup.tuner_callback = cx231xx_tuner_callback;
1109 
1110 	tuner_call(dev, tuner, s_type_addr, &tun_setup);
1111 
1112 #if 0
1113 	if (tun_setup.type == TUNER_XC5000) {
1114 		static struct xc2028_ctrl ctrl = {
1115 			.fname = XC5000_DEFAULT_FIRMWARE,
1116 			.max_len = 64,
1117 			.demod = 0;
1118 		};
1119 		struct v4l2_priv_tun_config cfg = {
1120 			.tuner = dev->tuner_type,
1121 			.priv = &ctrl,
1122 		};
1123 		tuner_call(dev, tuner, s_config, &cfg);
1124 	}
1125 #endif
1126 	/* configure tuner */
1127 	f.tuner = 0;
1128 	f.type = V4L2_TUNER_ANALOG_TV;
1129 	f.frequency = 9076;	/* just a magic number */
1130 	dev->ctl_freq = f.frequency;
1131 	call_all(dev, tuner, s_frequency, &f);
1132 
1133 }
1134 
1135 static int read_eeprom(struct cx231xx *dev, struct i2c_client *client,
1136 		       u8 *eedata, int len)
1137 {
1138 	int ret = 0;
1139 	u8 start_offset = 0;
1140 	int len_todo = len;
1141 	u8 *eedata_cur = eedata;
1142 	int i;
1143 	struct i2c_msg msg_write = { .addr = client->addr, .flags = 0,
1144 		.buf = &start_offset, .len = 1 };
1145 	struct i2c_msg msg_read = { .addr = client->addr, .flags = I2C_M_RD };
1146 
1147 	/* start reading at offset 0 */
1148 	ret = i2c_transfer(client->adapter, &msg_write, 1);
1149 	if (ret < 0) {
1150 		dev_err(dev->dev, "Can't read eeprom\n");
1151 		return ret;
1152 	}
1153 
1154 	while (len_todo > 0) {
1155 		msg_read.len = (len_todo > 64) ? 64 : len_todo;
1156 		msg_read.buf = eedata_cur;
1157 
1158 		ret = i2c_transfer(client->adapter, &msg_read, 1);
1159 		if (ret < 0) {
1160 			dev_err(dev->dev, "Can't read eeprom\n");
1161 			return ret;
1162 		}
1163 		eedata_cur += msg_read.len;
1164 		len_todo -= msg_read.len;
1165 	}
1166 
1167 	for (i = 0; i + 15 < len; i += 16)
1168 		dev_dbg(dev->dev, "i2c eeprom %02x: %*ph\n",
1169 			i, 16, &eedata[i]);
1170 
1171 	return 0;
1172 }
1173 
1174 void cx231xx_card_setup(struct cx231xx *dev)
1175 {
1176 
1177 	cx231xx_set_model(dev);
1178 
1179 	dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
1180 	if (cx231xx_boards[dev->model].tuner_addr)
1181 		dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
1182 
1183 	/* request some modules */
1184 	if (dev->board.decoder == CX231XX_AVDECODER) {
1185 		dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1186 					cx231xx_get_i2c_adap(dev, I2C_0),
1187 					"cx25840", 0x88 >> 1, NULL);
1188 		if (dev->sd_cx25840 == NULL)
1189 			dev_err(dev->dev,
1190 				"cx25840 subdev registration failure\n");
1191 		cx25840_call(dev, core, load_fw);
1192 
1193 	}
1194 
1195 	/* Initialize the tuner */
1196 	if (dev->board.tuner_type != TUNER_ABSENT) {
1197 		struct i2c_adapter *tuner_i2c = cx231xx_get_i2c_adap(dev,
1198 						dev->board.tuner_i2c_master);
1199 		dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1200 						    tuner_i2c,
1201 						    "tuner",
1202 						    dev->tuner_addr, NULL);
1203 		if (dev->sd_tuner == NULL)
1204 			dev_err(dev->dev,
1205 				"tuner subdev registration failure\n");
1206 		else
1207 			cx231xx_config_tuner(dev);
1208 	}
1209 
1210 	switch (dev->model) {
1211 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
1212 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1213 	case CX231XX_BOARD_HAUPPAUGE_955Q:
1214 		{
1215 			struct eeprom {
1216 				struct tveeprom tvee;
1217 				u8 eeprom[256];
1218 				struct i2c_client client;
1219 			};
1220 			struct eeprom *e = kzalloc(sizeof(*e), GFP_KERNEL);
1221 
1222 			if (e == NULL) {
1223 				dev_err(dev->dev,
1224 					"failed to allocate memory to read eeprom\n");
1225 				break;
1226 			}
1227 			e->client.adapter = cx231xx_get_i2c_adap(dev, I2C_1_MUX_1);
1228 			e->client.addr = 0xa0 >> 1;
1229 
1230 			read_eeprom(dev, &e->client, e->eeprom, sizeof(e->eeprom));
1231 			tveeprom_hauppauge_analog(&e->tvee, e->eeprom + 0xc0);
1232 			kfree(e);
1233 			break;
1234 		}
1235 	}
1236 
1237 }
1238 
1239 /*
1240  * cx231xx_config()
1241  * inits registers with sane defaults
1242  */
1243 int cx231xx_config(struct cx231xx *dev)
1244 {
1245 	/* TBD need to add cx231xx specific code */
1246 
1247 	return 0;
1248 }
1249 
1250 /*
1251  * cx231xx_config_i2c()
1252  * configure i2c attached devices
1253  */
1254 void cx231xx_config_i2c(struct cx231xx *dev)
1255 {
1256 	/* u32 input = INPUT(dev->video_input)->vmux; */
1257 
1258 	call_all(dev, video, s_stream, 1);
1259 }
1260 
1261 static void cx231xx_unregister_media_device(struct cx231xx *dev)
1262 {
1263 #ifdef CONFIG_MEDIA_CONTROLLER
1264 	if (dev->media_dev) {
1265 		media_device_unregister(dev->media_dev);
1266 		media_device_cleanup(dev->media_dev);
1267 		kfree(dev->media_dev);
1268 		dev->media_dev = NULL;
1269 	}
1270 #endif
1271 }
1272 
1273 /*
1274  * cx231xx_realease_resources()
1275  * unregisters the v4l2,i2c and usb devices
1276  * called when the device gets disconected or at module unload
1277 */
1278 void cx231xx_release_resources(struct cx231xx *dev)
1279 {
1280 	cx231xx_ir_exit(dev);
1281 
1282 	cx231xx_release_analog_resources(dev);
1283 
1284 	cx231xx_remove_from_devlist(dev);
1285 
1286 	/* Release I2C buses */
1287 	cx231xx_dev_uninit(dev);
1288 
1289 	/* delete v4l2 device */
1290 	v4l2_device_unregister(&dev->v4l2_dev);
1291 
1292 	cx231xx_unregister_media_device(dev);
1293 
1294 	usb_put_dev(dev->udev);
1295 
1296 	/* Mark device as unused */
1297 	clear_bit(dev->devno, &cx231xx_devused);
1298 }
1299 
1300 static int cx231xx_media_device_init(struct cx231xx *dev,
1301 				      struct usb_device *udev)
1302 {
1303 #ifdef CONFIG_MEDIA_CONTROLLER
1304 	struct media_device *mdev;
1305 
1306 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1307 	if (!mdev)
1308 		return -ENOMEM;
1309 
1310 	media_device_usb_init(mdev, udev, dev->board.name);
1311 
1312 	dev->media_dev = mdev;
1313 #endif
1314 	return 0;
1315 }
1316 
1317 /*
1318  * cx231xx_init_dev()
1319  * allocates and inits the device structs, registers i2c bus and v4l device
1320  */
1321 static int cx231xx_init_dev(struct cx231xx *dev, struct usb_device *udev,
1322 			    int minor)
1323 {
1324 	int retval = -ENOMEM;
1325 	unsigned int maxh, maxw;
1326 
1327 	dev->udev = udev;
1328 	mutex_init(&dev->lock);
1329 	mutex_init(&dev->ctrl_urb_lock);
1330 	mutex_init(&dev->gpio_i2c_lock);
1331 	mutex_init(&dev->i2c_lock);
1332 
1333 	spin_lock_init(&dev->video_mode.slock);
1334 	spin_lock_init(&dev->vbi_mode.slock);
1335 	spin_lock_init(&dev->sliced_cc_mode.slock);
1336 
1337 	init_waitqueue_head(&dev->open);
1338 	init_waitqueue_head(&dev->wait_frame);
1339 	init_waitqueue_head(&dev->wait_stream);
1340 
1341 	dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
1342 	dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
1343 	dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
1344 	dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
1345 	dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
1346 
1347 	/* Query cx231xx to find what pcb config it is related to */
1348 	retval = initialize_cx231xx(dev);
1349 	if (retval < 0) {
1350 		dev_err(dev->dev, "Failed to read PCB config\n");
1351 		return retval;
1352 	}
1353 
1354 	/*To workaround error number=-71 on EP0 for VideoGrabber,
1355 		 need set alt here.*/
1356 	if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER ||
1357 	    dev->model == CX231XX_BOARD_HAUPPAUGE_USBLIVE2) {
1358 		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
1359 		cx231xx_set_alt_setting(dev, INDEX_VANC, 1);
1360 	}
1361 	/* Cx231xx pre card setup */
1362 	cx231xx_pre_card_setup(dev);
1363 
1364 	retval = cx231xx_config(dev);
1365 	if (retval) {
1366 		dev_err(dev->dev, "error configuring device\n");
1367 		return -ENOMEM;
1368 	}
1369 
1370 	/* set default norm */
1371 	dev->norm = dev->board.norm;
1372 
1373 	/* register i2c bus */
1374 	retval = cx231xx_dev_init(dev);
1375 	if (retval) {
1376 		dev_err(dev->dev,
1377 			"%s: cx231xx_i2c_register - errCode [%d]!\n",
1378 			__func__, retval);
1379 		goto err_dev_init;
1380 	}
1381 
1382 	/* Do board specific init */
1383 	cx231xx_card_setup(dev);
1384 
1385 	/* configure the device */
1386 	cx231xx_config_i2c(dev);
1387 
1388 	maxw = norm_maxw(dev);
1389 	maxh = norm_maxh(dev);
1390 
1391 	/* set default image size */
1392 	dev->width = maxw;
1393 	dev->height = maxh;
1394 	dev->interlaced = 0;
1395 	dev->video_input = 0;
1396 
1397 	retval = cx231xx_config(dev);
1398 	if (retval) {
1399 		dev_err(dev->dev, "%s: cx231xx_config - errCode [%d]!\n",
1400 			__func__, retval);
1401 		goto err_dev_init;
1402 	}
1403 
1404 	/* init video dma queues */
1405 	INIT_LIST_HEAD(&dev->video_mode.vidq.active);
1406 	INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
1407 
1408 	/* init vbi dma queues */
1409 	INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
1410 	INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
1411 
1412 	/* Reset other chips required if they are tied up with GPIO pins */
1413 	cx231xx_add_into_devlist(dev);
1414 
1415 	if (dev->board.has_417) {
1416 		dev_info(dev->dev, "attach 417 %d\n", dev->model);
1417 		if (cx231xx_417_register(dev) < 0) {
1418 			dev_err(dev->dev,
1419 				"%s() Failed to register 417 on VID_B\n",
1420 				__func__);
1421 		}
1422 	}
1423 
1424 	retval = cx231xx_register_analog_devices(dev);
1425 	if (retval)
1426 		goto err_analog;
1427 
1428 	cx231xx_ir_init(dev);
1429 
1430 	cx231xx_init_extension(dev);
1431 
1432 	return 0;
1433 err_analog:
1434 	cx231xx_unregister_media_device(dev);
1435 	cx231xx_release_analog_resources(dev);
1436 	cx231xx_remove_from_devlist(dev);
1437 err_dev_init:
1438 	cx231xx_dev_uninit(dev);
1439 	return retval;
1440 }
1441 
1442 #if defined(CONFIG_MODULES) && defined(MODULE)
1443 static void request_module_async(struct work_struct *work)
1444 {
1445 	struct cx231xx *dev = container_of(work,
1446 					   struct cx231xx, request_module_wk);
1447 
1448 	if (dev->has_alsa_audio)
1449 		request_module("cx231xx-alsa");
1450 
1451 	if (dev->board.has_dvb)
1452 		request_module("cx231xx-dvb");
1453 
1454 }
1455 
1456 static void request_modules(struct cx231xx *dev)
1457 {
1458 	INIT_WORK(&dev->request_module_wk, request_module_async);
1459 	schedule_work(&dev->request_module_wk);
1460 }
1461 
1462 static void flush_request_modules(struct cx231xx *dev)
1463 {
1464 	flush_work(&dev->request_module_wk);
1465 }
1466 #else
1467 #define request_modules(dev)
1468 #define flush_request_modules(dev)
1469 #endif /* CONFIG_MODULES */
1470 
1471 static int cx231xx_init_v4l2(struct cx231xx *dev,
1472 			     struct usb_device *udev,
1473 			     struct usb_interface *interface,
1474 			     int isoc_pipe)
1475 {
1476 	struct usb_interface *uif;
1477 	int i, idx;
1478 
1479 	/* Video Init */
1480 
1481 	/* compute alternate max packet sizes for video */
1482 	idx = dev->current_pcb_config.hs_config_info[0].interface_info.video_index + 1;
1483 	if (idx >= dev->max_iad_interface_count) {
1484 		dev_err(dev->dev,
1485 			"Video PCB interface #%d doesn't exist\n", idx);
1486 		return -ENODEV;
1487 	}
1488 
1489 	uif = udev->actconfig->interface[idx];
1490 
1491 	if (uif->altsetting[0].desc.bNumEndpoints < isoc_pipe + 1)
1492 		return -ENODEV;
1493 
1494 	dev->video_mode.end_point_addr = uif->altsetting[0].endpoint[isoc_pipe].desc.bEndpointAddress;
1495 	dev->video_mode.num_alt = uif->num_altsetting;
1496 
1497 	dev_info(dev->dev,
1498 		 "video EndPoint Addr 0x%x, Alternate settings: %i\n",
1499 		 dev->video_mode.end_point_addr,
1500 		 dev->video_mode.num_alt);
1501 
1502 	dev->video_mode.alt_max_pkt_size = devm_kmalloc_array(&udev->dev, 32, dev->video_mode.num_alt, GFP_KERNEL);
1503 	if (dev->video_mode.alt_max_pkt_size == NULL)
1504 		return -ENOMEM;
1505 
1506 	for (i = 0; i < dev->video_mode.num_alt; i++) {
1507 		u16 tmp;
1508 
1509 		if (uif->altsetting[i].desc.bNumEndpoints < isoc_pipe + 1)
1510 			return -ENODEV;
1511 
1512 		tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.wMaxPacketSize);
1513 		dev->video_mode.alt_max_pkt_size[i] = (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1514 		dev_dbg(dev->dev,
1515 			"Alternate setting %i, max size= %i\n", i,
1516 			dev->video_mode.alt_max_pkt_size[i]);
1517 	}
1518 
1519 	/* VBI Init */
1520 
1521 	idx = dev->current_pcb_config.hs_config_info[0].interface_info.vanc_index + 1;
1522 	if (idx >= dev->max_iad_interface_count) {
1523 		dev_err(dev->dev,
1524 			"VBI PCB interface #%d doesn't exist\n", idx);
1525 		return -ENODEV;
1526 	}
1527 	uif = udev->actconfig->interface[idx];
1528 
1529 	if (uif->altsetting[0].desc.bNumEndpoints < isoc_pipe + 1)
1530 		return -ENODEV;
1531 
1532 	dev->vbi_mode.end_point_addr =
1533 	    uif->altsetting[0].endpoint[isoc_pipe].desc.
1534 			bEndpointAddress;
1535 
1536 	dev->vbi_mode.num_alt = uif->num_altsetting;
1537 	dev_info(dev->dev,
1538 		 "VBI EndPoint Addr 0x%x, Alternate settings: %i\n",
1539 		 dev->vbi_mode.end_point_addr,
1540 		 dev->vbi_mode.num_alt);
1541 
1542 	/* compute alternate max packet sizes for vbi */
1543 	dev->vbi_mode.alt_max_pkt_size = devm_kmalloc_array(&udev->dev, 32, dev->vbi_mode.num_alt, GFP_KERNEL);
1544 	if (dev->vbi_mode.alt_max_pkt_size == NULL)
1545 		return -ENOMEM;
1546 
1547 	for (i = 0; i < dev->vbi_mode.num_alt; i++) {
1548 		u16 tmp;
1549 
1550 		if (uif->altsetting[i].desc.bNumEndpoints < isoc_pipe + 1)
1551 			return -ENODEV;
1552 
1553 		tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1554 				desc.wMaxPacketSize);
1555 		dev->vbi_mode.alt_max_pkt_size[i] =
1556 		    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1557 		dev_dbg(dev->dev,
1558 			"Alternate setting %i, max size= %i\n", i,
1559 			dev->vbi_mode.alt_max_pkt_size[i]);
1560 	}
1561 
1562 	/* Sliced CC VBI init */
1563 
1564 	/* compute alternate max packet sizes for sliced CC */
1565 	idx = dev->current_pcb_config.hs_config_info[0].interface_info.hanc_index + 1;
1566 	if (idx >= dev->max_iad_interface_count) {
1567 		dev_err(dev->dev,
1568 			"Sliced CC PCB interface #%d doesn't exist\n", idx);
1569 		return -ENODEV;
1570 	}
1571 	uif = udev->actconfig->interface[idx];
1572 
1573 	if (uif->altsetting[0].desc.bNumEndpoints < isoc_pipe + 1)
1574 		return -ENODEV;
1575 
1576 	dev->sliced_cc_mode.end_point_addr =
1577 	    uif->altsetting[0].endpoint[isoc_pipe].desc.
1578 			bEndpointAddress;
1579 
1580 	dev->sliced_cc_mode.num_alt = uif->num_altsetting;
1581 	dev_info(dev->dev,
1582 		 "sliced CC EndPoint Addr 0x%x, Alternate settings: %i\n",
1583 		 dev->sliced_cc_mode.end_point_addr,
1584 		 dev->sliced_cc_mode.num_alt);
1585 	dev->sliced_cc_mode.alt_max_pkt_size = devm_kmalloc_array(&udev->dev, 32, dev->sliced_cc_mode.num_alt, GFP_KERNEL);
1586 	if (dev->sliced_cc_mode.alt_max_pkt_size == NULL)
1587 		return -ENOMEM;
1588 
1589 	for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
1590 		u16 tmp;
1591 
1592 		if (uif->altsetting[i].desc.bNumEndpoints < isoc_pipe + 1)
1593 			return -ENODEV;
1594 
1595 		tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1596 				desc.wMaxPacketSize);
1597 		dev->sliced_cc_mode.alt_max_pkt_size[i] =
1598 		    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1599 		dev_dbg(dev->dev,
1600 			"Alternate setting %i, max size= %i\n", i,
1601 			dev->sliced_cc_mode.alt_max_pkt_size[i]);
1602 	}
1603 
1604 	return 0;
1605 }
1606 
1607 /*
1608  * cx231xx_usb_probe()
1609  * checks for supported devices
1610  */
1611 static int cx231xx_usb_probe(struct usb_interface *interface,
1612 			     const struct usb_device_id *id)
1613 {
1614 	struct usb_device *udev;
1615 	struct device *d = &interface->dev;
1616 	struct usb_interface *uif;
1617 	struct cx231xx *dev = NULL;
1618 	int retval = -ENODEV;
1619 	int nr = 0, ifnum;
1620 	int i, isoc_pipe = 0;
1621 	char *speed;
1622 	u8 idx;
1623 	struct usb_interface_assoc_descriptor *assoc_desc;
1624 
1625 	ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1626 
1627 	/*
1628 	 * Interface number 0 - IR interface (handled by mceusb driver)
1629 	 * Interface number 1 - AV interface (handled by this driver)
1630 	 */
1631 	if (ifnum != 1)
1632 		return -ENODEV;
1633 
1634 	/* Check to see next free device and mark as used */
1635 	do {
1636 		nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
1637 		if (nr >= CX231XX_MAXBOARDS) {
1638 			/* No free device slots */
1639 			dev_err(d,
1640 				"Supports only %i devices.\n",
1641 				CX231XX_MAXBOARDS);
1642 			return -ENOMEM;
1643 		}
1644 	} while (test_and_set_bit(nr, &cx231xx_devused));
1645 
1646 	udev = usb_get_dev(interface_to_usbdev(interface));
1647 
1648 	/* allocate memory for our device state and initialize it */
1649 	dev = devm_kzalloc(&udev->dev, sizeof(*dev), GFP_KERNEL);
1650 	if (dev == NULL) {
1651 		retval = -ENOMEM;
1652 		goto err_if;
1653 	}
1654 
1655 	snprintf(dev->name, 29, "cx231xx #%d", nr);
1656 	dev->devno = nr;
1657 	dev->model = id->driver_info;
1658 	dev->video_mode.alt = -1;
1659 	dev->dev = d;
1660 
1661 	cx231xx_set_model(dev);
1662 
1663 	dev->interface_count++;
1664 	/* reset gpio dir and value */
1665 	dev->gpio_dir = 0;
1666 	dev->gpio_val = 0;
1667 	dev->xc_fw_load_done = 0;
1668 	dev->has_alsa_audio = 1;
1669 	dev->power_mode = -1;
1670 	atomic_set(&dev->devlist_count, 0);
1671 
1672 	/* 0 - vbi ; 1 -sliced cc mode */
1673 	dev->vbi_or_sliced_cc_mode = 0;
1674 
1675 	/* get maximum no.of IAD interfaces */
1676 	dev->max_iad_interface_count = udev->config->desc.bNumInterfaces;
1677 
1678 	/* init CIR module TBD */
1679 
1680 	/*mode_tv: digital=1 or analog=0*/
1681 	dev->mode_tv = 0;
1682 
1683 	dev->USE_ISO = transfer_mode;
1684 
1685 	switch (udev->speed) {
1686 	case USB_SPEED_LOW:
1687 		speed = "1.5";
1688 		break;
1689 	case USB_SPEED_UNKNOWN:
1690 	case USB_SPEED_FULL:
1691 		speed = "12";
1692 		break;
1693 	case USB_SPEED_HIGH:
1694 		speed = "480";
1695 		break;
1696 	default:
1697 		speed = "unknown";
1698 	}
1699 
1700 	dev_info(d,
1701 		 "New device %s %s @ %s Mbps (%04x:%04x) with %d interfaces\n",
1702 		 udev->manufacturer ? udev->manufacturer : "",
1703 		 udev->product ? udev->product : "",
1704 		 speed,
1705 		 le16_to_cpu(udev->descriptor.idVendor),
1706 		 le16_to_cpu(udev->descriptor.idProduct),
1707 		 dev->max_iad_interface_count);
1708 
1709 	/* increment interface count */
1710 	dev->interface_count++;
1711 
1712 	/* get device number */
1713 	nr = dev->devno;
1714 
1715 	assoc_desc = udev->actconfig->intf_assoc[0];
1716 	if (!assoc_desc || assoc_desc->bFirstInterface != ifnum) {
1717 		dev_err(d, "Not found matching IAD interface\n");
1718 		retval = -ENODEV;
1719 		goto err_if;
1720 	}
1721 
1722 	dev_dbg(d, "registering interface %d\n", ifnum);
1723 
1724 	/* save our data pointer in this interface device */
1725 	usb_set_intfdata(interface, dev);
1726 
1727 	/* Initialize the media controller */
1728 	retval = cx231xx_media_device_init(dev, udev);
1729 	if (retval) {
1730 		dev_err(d, "cx231xx_media_device_init failed\n");
1731 		goto err_media_init;
1732 	}
1733 
1734 	/* Create v4l2 device */
1735 #ifdef CONFIG_MEDIA_CONTROLLER
1736 	dev->v4l2_dev.mdev = dev->media_dev;
1737 #endif
1738 	retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1739 	if (retval) {
1740 		dev_err(d, "v4l2_device_register failed\n");
1741 		goto err_v4l2;
1742 	}
1743 
1744 	/* allocate device struct */
1745 	retval = cx231xx_init_dev(dev, udev, nr);
1746 	if (retval)
1747 		goto err_init;
1748 
1749 	retval = cx231xx_init_v4l2(dev, udev, interface, isoc_pipe);
1750 	if (retval)
1751 		goto err_init;
1752 
1753 	if (dev->current_pcb_config.ts1_source != 0xff) {
1754 		/* compute alternate max packet sizes for TS1 */
1755 		idx = dev->current_pcb_config.hs_config_info[0].interface_info.ts1_index + 1;
1756 		if (idx >= dev->max_iad_interface_count) {
1757 			dev_err(d, "TS1 PCB interface #%d doesn't exist\n",
1758 				idx);
1759 			retval = -ENODEV;
1760 			goto err_video_alt;
1761 		}
1762 		uif = udev->actconfig->interface[idx];
1763 
1764 		if (uif->altsetting[0].desc.bNumEndpoints < isoc_pipe + 1) {
1765 			retval = -ENODEV;
1766 			goto err_video_alt;
1767 		}
1768 
1769 		dev->ts1_mode.end_point_addr =
1770 		    uif->altsetting[0].endpoint[isoc_pipe].
1771 				desc.bEndpointAddress;
1772 
1773 		dev->ts1_mode.num_alt = uif->num_altsetting;
1774 		dev_info(d,
1775 			 "TS EndPoint Addr 0x%x, Alternate settings: %i\n",
1776 			 dev->ts1_mode.end_point_addr,
1777 			 dev->ts1_mode.num_alt);
1778 
1779 		dev->ts1_mode.alt_max_pkt_size = devm_kmalloc_array(&udev->dev, 32, dev->ts1_mode.num_alt, GFP_KERNEL);
1780 		if (dev->ts1_mode.alt_max_pkt_size == NULL) {
1781 			retval = -ENOMEM;
1782 			goto err_video_alt;
1783 		}
1784 
1785 		for (i = 0; i < dev->ts1_mode.num_alt; i++) {
1786 			u16 tmp;
1787 
1788 			if (uif->altsetting[i].desc.bNumEndpoints < isoc_pipe + 1) {
1789 				retval = -ENODEV;
1790 				goto err_video_alt;
1791 			}
1792 
1793 			tmp = le16_to_cpu(uif->altsetting[i].
1794 						endpoint[isoc_pipe].desc.
1795 						wMaxPacketSize);
1796 			dev->ts1_mode.alt_max_pkt_size[i] =
1797 			    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1798 			dev_dbg(d, "Alternate setting %i, max size= %i\n",
1799 				i, dev->ts1_mode.alt_max_pkt_size[i]);
1800 		}
1801 	}
1802 
1803 	if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER) {
1804 		cx231xx_enable_OSC(dev);
1805 		cx231xx_reset_out(dev);
1806 		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
1807 	}
1808 
1809 	if (dev->model == CX231XX_BOARD_CNXT_RDE_253S)
1810 		cx231xx_sleep_s5h1432(dev);
1811 
1812 	/* load other modules required */
1813 	request_modules(dev);
1814 
1815 #ifdef CONFIG_MEDIA_CONTROLLER
1816 	/* Init entities at the Media Controller */
1817 	cx231xx_v4l2_create_entities(dev);
1818 
1819 	retval = v4l2_mc_create_media_graph(dev->media_dev);
1820 	if (!retval)
1821 		retval = media_device_register(dev->media_dev);
1822 #endif
1823 	if (retval < 0)
1824 		cx231xx_release_resources(dev);
1825 	return retval;
1826 
1827 err_video_alt:
1828 	/* cx231xx_uninit_dev: */
1829 	cx231xx_close_extension(dev);
1830 	cx231xx_ir_exit(dev);
1831 	cx231xx_release_analog_resources(dev);
1832 	cx231xx_417_unregister(dev);
1833 	cx231xx_remove_from_devlist(dev);
1834 	cx231xx_dev_uninit(dev);
1835 err_init:
1836 	v4l2_device_unregister(&dev->v4l2_dev);
1837 err_v4l2:
1838 	cx231xx_unregister_media_device(dev);
1839 err_media_init:
1840 	usb_set_intfdata(interface, NULL);
1841 err_if:
1842 	usb_put_dev(udev);
1843 	clear_bit(nr, &cx231xx_devused);
1844 	return retval;
1845 }
1846 
1847 /*
1848  * cx231xx_usb_disconnect()
1849  * called when the device gets diconencted
1850  * video device will be unregistered on v4l2_close in case it is still open
1851  */
1852 static void cx231xx_usb_disconnect(struct usb_interface *interface)
1853 {
1854 	struct cx231xx *dev;
1855 
1856 	dev = usb_get_intfdata(interface);
1857 	usb_set_intfdata(interface, NULL);
1858 
1859 	if (!dev)
1860 		return;
1861 
1862 	if (!dev->udev)
1863 		return;
1864 
1865 	dev->state |= DEV_DISCONNECTED;
1866 
1867 	flush_request_modules(dev);
1868 
1869 	/* wait until all current v4l2 io is finished then deallocate
1870 	   resources */
1871 	mutex_lock(&dev->lock);
1872 
1873 	wake_up_interruptible_all(&dev->open);
1874 
1875 	if (dev->users) {
1876 		dev_warn(dev->dev,
1877 			 "device %s is open! Deregistration and memory deallocation are deferred on close.\n",
1878 			 video_device_node_name(&dev->vdev));
1879 
1880 		/* Even having users, it is safe to remove the RC i2c driver */
1881 		cx231xx_ir_exit(dev);
1882 
1883 		if (dev->USE_ISO)
1884 			cx231xx_uninit_isoc(dev);
1885 		else
1886 			cx231xx_uninit_bulk(dev);
1887 		wake_up_interruptible(&dev->wait_frame);
1888 		wake_up_interruptible(&dev->wait_stream);
1889 	} else {
1890 	}
1891 
1892 	cx231xx_close_extension(dev);
1893 
1894 	mutex_unlock(&dev->lock);
1895 
1896 	if (!dev->users)
1897 		cx231xx_release_resources(dev);
1898 }
1899 
1900 static struct usb_driver cx231xx_usb_driver = {
1901 	.name = "cx231xx",
1902 	.probe = cx231xx_usb_probe,
1903 	.disconnect = cx231xx_usb_disconnect,
1904 	.id_table = cx231xx_id_table,
1905 };
1906 
1907 module_usb_driver(cx231xx_usb_driver);
1908