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