Merge branch 'sh-latest' of git://git.kernel.org/pub/scm/linux/kernel/git/lethal...
[pandora-kernel.git] / drivers / media / video / cx231xx / cx231xx-cards.c
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_KWORLD_UB430_USB_HYBRID] = {
405                 .name = "Kworld UB430 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 = 0x11, /* 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                 .has_dvb = 1,
421                 .demod_addr = 0x10,
422                 .norm = V4L2_STD_PAL_M,
423                 .input = {{
424                         .type = CX231XX_VMUX_TELEVISION,
425                         .vmux = CX231XX_VIN_3_1,
426                         .amux = CX231XX_AMUX_VIDEO,
427                         .gpio = NULL,
428                 }, {
429                         .type = CX231XX_VMUX_COMPOSITE1,
430                         .vmux = CX231XX_VIN_2_1,
431                         .amux = CX231XX_AMUX_LINE_IN,
432                         .gpio = NULL,
433                 }, {
434                         .type = CX231XX_VMUX_SVIDEO,
435                         .vmux = CX231XX_VIN_1_1 |
436                                 (CX231XX_VIN_1_2 << 8) |
437                                 CX25840_SVIDEO_ON,
438                         .amux = CX231XX_AMUX_LINE_IN,
439                         .gpio = NULL,
440                 } },
441         },
442         [CX231XX_BOARD_PV_PLAYTV_USB_HYBRID] = {
443                 .name = "Pixelview PlayTV USB Hybrid",
444                 .tuner_type = TUNER_NXP_TDA18271,
445                 .tuner_addr = 0x60,
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 = 0x00, /* According with PV cxPolaris.inf file */
451                 .tuner_sif_gpio = -1,
452                 .tuner_scl_gpio = -1,
453                 .tuner_sda_gpio = -1,
454                 .gpio_pin_status_mask = 0x4001000,
455                 .tuner_i2c_master = 2,
456                 .demod_i2c_master = 1,
457                 .ir_i2c_master = 2,
458                 .rc_map_name = RC_MAP_PIXELVIEW_002T,
459                 .has_dvb = 1,
460                 .demod_addr = 0x10,
461                 .norm = V4L2_STD_PAL_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_XCAPTURE_USB] = {
482                 .name = "Pixelview Xcapture USB",
483                 .tuner_type = TUNER_ABSENT,
484                 .decoder = CX231XX_AVDECODER,
485                 .output_mode = OUT_MODE_VIP11,
486                 .demod_xfer_mode = 0,
487                 .ctl_pin_status_mask = 0xFFFFFFC4,
488                 .agc_analog_digital_select_gpio = 0x0c,
489                 .gpio_pin_status_mask = 0x4001000,
490                 .norm = V4L2_STD_NTSC,
491                 .no_alt_vanc = 1,
492                 .external_av = 1,
493                 .dont_use_port_3 = 1,
494
495                 .input = {{
496                                 .type = CX231XX_VMUX_COMPOSITE1,
497                                 .vmux = CX231XX_VIN_2_1,
498                                 .amux = CX231XX_AMUX_LINE_IN,
499                                 .gpio = NULL,
500                         }, {
501                                 .type = CX231XX_VMUX_SVIDEO,
502                                 .vmux = CX231XX_VIN_1_1 |
503                                         (CX231XX_VIN_1_2 << 8) |
504                                         CX25840_SVIDEO_ON,
505                                 .amux = CX231XX_AMUX_LINE_IN,
506                                 .gpio = NULL,
507                         }
508                 },
509         },
510
511         [CX231XX_BOARD_ICONBIT_U100] = {
512                 .name = "Iconbit Analog Stick U100 FM",
513                 .tuner_type = TUNER_ABSENT,
514                 .decoder = CX231XX_AVDECODER,
515                 .output_mode = OUT_MODE_VIP11,
516                 .demod_xfer_mode = 0,
517                 .ctl_pin_status_mask = 0xFFFFFFC4,
518                 .agc_analog_digital_select_gpio = 0x1C,
519                 .gpio_pin_status_mask = 0x4001000,
520
521                 .input = {{
522                         .type = CX231XX_VMUX_COMPOSITE1,
523                         .vmux = CX231XX_VIN_2_1,
524                         .amux = CX231XX_AMUX_LINE_IN,
525                         .gpio = NULL,
526                 }, {
527                         .type = CX231XX_VMUX_SVIDEO,
528                         .vmux = CX231XX_VIN_1_1 |
529                                 (CX231XX_VIN_1_2 << 8) |
530                                 CX25840_SVIDEO_ON,
531                         .amux = CX231XX_AMUX_LINE_IN,
532                         .gpio = NULL,
533                 } },
534         },
535 };
536 const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);
537
538 /* table of devices that work with this driver */
539 struct usb_device_id cx231xx_id_table[] = {
540         {USB_DEVICE(0x0572, 0x5A3C),
541          .driver_info = CX231XX_BOARD_UNKNOWN},
542         {USB_DEVICE(0x0572, 0x58A2),
543          .driver_info = CX231XX_BOARD_CNXT_CARRAERA},
544         {USB_DEVICE(0x0572, 0x58A1),
545          .driver_info = CX231XX_BOARD_CNXT_SHELBY},
546         {USB_DEVICE(0x0572, 0x58A4),
547          .driver_info = CX231XX_BOARD_CNXT_RDE_253S},
548         {USB_DEVICE(0x0572, 0x58A5),
549          .driver_info = CX231XX_BOARD_CNXT_RDU_253S},
550         {USB_DEVICE(0x0572, 0x58A6),
551          .driver_info = CX231XX_BOARD_CNXT_VIDEO_GRABBER},
552         {USB_DEVICE(0x0572, 0x589E),
553          .driver_info = CX231XX_BOARD_CNXT_RDE_250},
554         {USB_DEVICE(0x0572, 0x58A0),
555          .driver_info = CX231XX_BOARD_CNXT_RDU_250},
556         {USB_DEVICE(0x2040, 0xb120),
557          .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
558         {USB_DEVICE(0x2040, 0xb140),
559          .driver_info = CX231XX_BOARD_HAUPPAUGE_EXETER},
560         {USB_DEVICE(0x2040, 0xc200),
561          .driver_info = CX231XX_BOARD_HAUPPAUGE_USBLIVE2},
562         {USB_DEVICE_VER(USB_VID_PIXELVIEW, USB_PID_PIXELVIEW_SBTVD, 0x4000, 0x4001),
563          .driver_info = CX231XX_BOARD_PV_PLAYTV_USB_HYBRID},
564         {USB_DEVICE(USB_VID_PIXELVIEW, 0x5014),
565          .driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},
566         {USB_DEVICE(0x1b80, 0xe424),
567          .driver_info = CX231XX_BOARD_KWORLD_UB430_USB_HYBRID},
568         {USB_DEVICE(0x1f4d, 0x0237),
569          .driver_info = CX231XX_BOARD_ICONBIT_U100},
570         {},
571 };
572
573 MODULE_DEVICE_TABLE(usb, cx231xx_id_table);
574
575 /* cx231xx_tuner_callback
576  * will be used to reset XC5000 tuner using GPIO pin
577  */
578
579 int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
580 {
581         int rc = 0;
582         struct cx231xx *dev = ptr;
583
584         if (dev->tuner_type == TUNER_XC5000) {
585                 if (command == XC5000_TUNER_RESET) {
586                         cx231xx_info
587                                 ("Tuner CB: RESET: cmd %d : tuner type %d \n",
588                                  command, dev->tuner_type);
589                         cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
590                                                1);
591                         msleep(10);
592                         cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
593                                                0);
594                         msleep(330);
595                         cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
596                                                1);
597                         msleep(10);
598                 }
599         } else if (dev->tuner_type == TUNER_NXP_TDA18271) {
600                 switch (command) {
601                 case TDA18271_CALLBACK_CMD_AGC_ENABLE:
602                         if (dev->model == CX231XX_BOARD_PV_PLAYTV_USB_HYBRID)
603                                 rc = cx231xx_set_agc_analog_digital_mux_select(dev, arg);
604                         break;
605                 default:
606                         rc = -EINVAL;
607                         break;
608                 }
609         }
610         return rc;
611 }
612 EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);
613
614 void cx231xx_reset_out(struct cx231xx *dev)
615 {
616         cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
617         msleep(200);
618         cx231xx_set_gpio_value(dev, CX23417_RESET, 0);
619         msleep(200);
620         cx231xx_set_gpio_value(dev, CX23417_RESET, 1);
621 }
622 void cx231xx_enable_OSC(struct cx231xx *dev)
623 {
624         cx231xx_set_gpio_value(dev, CX23417_OSC_EN, 1);
625 }
626 void cx231xx_sleep_s5h1432(struct cx231xx *dev)
627 {
628         cx231xx_set_gpio_value(dev, SLEEP_S5H1432, 0);
629 }
630
631 static inline void cx231xx_set_model(struct cx231xx *dev)
632 {
633         memcpy(&dev->board, &cx231xx_boards[dev->model], sizeof(dev->board));
634 }
635
636 /* Since cx231xx_pre_card_setup() requires a proper dev->model,
637  * this won't work for boards with generic PCI IDs
638  */
639 void cx231xx_pre_card_setup(struct cx231xx *dev)
640 {
641
642         cx231xx_set_model(dev);
643
644         cx231xx_info("Identified as %s (card=%d)\n",
645                      dev->board.name, dev->model);
646
647         /* set the direction for GPIO pins */
648         if (dev->board.tuner_gpio) {
649                 cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
650                 cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
651         }
652         if (dev->board.tuner_sif_gpio >= 0)
653                 cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
654
655         /* request some modules if any required */
656
657         /* set the mode to Analog mode initially */
658         cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
659
660         /* Unlock device */
661         /* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
662
663 }
664
665 static void cx231xx_config_tuner(struct cx231xx *dev)
666 {
667         struct tuner_setup tun_setup;
668         struct v4l2_frequency f;
669
670         if (dev->tuner_type == TUNER_ABSENT)
671                 return;
672
673         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
674         tun_setup.type = dev->tuner_type;
675         tun_setup.addr = dev->tuner_addr;
676         tun_setup.tuner_callback = cx231xx_tuner_callback;
677
678         tuner_call(dev, tuner, s_type_addr, &tun_setup);
679
680 #if 0
681         if (tun_setup.type == TUNER_XC5000) {
682                 static struct xc2028_ctrl ctrl = {
683                         .fname = XC5000_DEFAULT_FIRMWARE,
684                         .max_len = 64,
685                         .demod = 0;
686                 };
687                 struct v4l2_priv_tun_config cfg = {
688                         .tuner = dev->tuner_type,
689                         .priv = &ctrl,
690                 };
691                 tuner_call(dev, tuner, s_config, &cfg);
692         }
693 #endif
694         /* configure tuner */
695         f.tuner = 0;
696         f.type = V4L2_TUNER_ANALOG_TV;
697         f.frequency = 9076;     /* just a magic number */
698         dev->ctl_freq = f.frequency;
699         call_all(dev, tuner, s_frequency, &f);
700
701 }
702
703 void cx231xx_card_setup(struct cx231xx *dev)
704 {
705
706         cx231xx_set_model(dev);
707
708         dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
709         if (cx231xx_boards[dev->model].tuner_addr)
710                 dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
711
712         /* request some modules */
713         if (dev->board.decoder == CX231XX_AVDECODER) {
714                 dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
715                                         &dev->i2c_bus[0].i2c_adap,
716                                         "cx25840", 0x88 >> 1, NULL);
717                 if (dev->sd_cx25840 == NULL)
718                         cx231xx_info("cx25840 subdev registration failure\n");
719                 cx25840_call(dev, core, load_fw);
720
721         }
722
723         /* Initialize the tuner */
724         if (dev->board.tuner_type != TUNER_ABSENT) {
725                 dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
726                                                     &dev->i2c_bus[dev->board.tuner_i2c_master].i2c_adap,
727                                                     "tuner",
728                                                     dev->tuner_addr, NULL);
729                 if (dev->sd_tuner == NULL)
730                         cx231xx_info("tuner subdev registration failure\n");
731                 else
732                         cx231xx_config_tuner(dev);
733         }
734 }
735
736 /*
737  * cx231xx_config()
738  * inits registers with sane defaults
739  */
740 int cx231xx_config(struct cx231xx *dev)
741 {
742         /* TBD need to add cx231xx specific code */
743         dev->mute = 1;          /* maybe not the right place... */
744         dev->volume = 0x1f;
745
746         return 0;
747 }
748
749 /*
750  * cx231xx_config_i2c()
751  * configure i2c attached devices
752  */
753 void cx231xx_config_i2c(struct cx231xx *dev)
754 {
755         /* u32 input = INPUT(dev->video_input)->vmux; */
756
757         call_all(dev, video, s_stream, 1);
758 }
759
760 /*
761  * cx231xx_realease_resources()
762  * unregisters the v4l2,i2c and usb devices
763  * called when the device gets disconected or at module unload
764 */
765 void cx231xx_release_resources(struct cx231xx *dev)
766 {
767         cx231xx_release_analog_resources(dev);
768
769         cx231xx_remove_from_devlist(dev);
770
771         /* Release I2C buses */
772         cx231xx_dev_uninit(dev);
773
774         cx231xx_ir_exit(dev);
775
776         usb_put_dev(dev->udev);
777
778         /* Mark device as unused */
779         cx231xx_devused &= ~(1 << dev->devno);
780 }
781
782 /*
783  * cx231xx_init_dev()
784  * allocates and inits the device structs, registers i2c bus and v4l device
785  */
786 static int cx231xx_init_dev(struct cx231xx **devhandle, struct usb_device *udev,
787                             int minor)
788 {
789         struct cx231xx *dev = *devhandle;
790         int retval = -ENOMEM;
791         int errCode;
792         unsigned int maxh, maxw;
793
794         dev->udev = udev;
795         mutex_init(&dev->lock);
796         mutex_init(&dev->ctrl_urb_lock);
797         mutex_init(&dev->gpio_i2c_lock);
798         mutex_init(&dev->i2c_lock);
799
800         spin_lock_init(&dev->video_mode.slock);
801         spin_lock_init(&dev->vbi_mode.slock);
802         spin_lock_init(&dev->sliced_cc_mode.slock);
803
804         init_waitqueue_head(&dev->open);
805         init_waitqueue_head(&dev->wait_frame);
806         init_waitqueue_head(&dev->wait_stream);
807
808         dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
809         dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
810         dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
811         dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
812         dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
813
814         /* Query cx231xx to find what pcb config it is related to */
815         initialize_cx231xx(dev);
816
817         /*To workaround error number=-71 on EP0 for VideoGrabber,
818                  need set alt here.*/
819         if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER ||
820             dev->model == CX231XX_BOARD_HAUPPAUGE_USBLIVE2) {
821                 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
822                 cx231xx_set_alt_setting(dev, INDEX_VANC, 1);
823         }
824         /* Cx231xx pre card setup */
825         cx231xx_pre_card_setup(dev);
826
827         errCode = cx231xx_config(dev);
828         if (errCode) {
829                 cx231xx_errdev("error configuring device\n");
830                 return -ENOMEM;
831         }
832
833         /* set default norm */
834         dev->norm = dev->board.norm;
835
836         /* register i2c bus */
837         errCode = cx231xx_dev_init(dev);
838         if (errCode < 0) {
839                 cx231xx_dev_uninit(dev);
840                 cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
841                                __func__, errCode);
842                 return errCode;
843         }
844
845         /* Do board specific init */
846         cx231xx_card_setup(dev);
847
848         /* configure the device */
849         cx231xx_config_i2c(dev);
850
851         maxw = norm_maxw(dev);
852         maxh = norm_maxh(dev);
853
854         /* set default image size */
855         dev->width = maxw;
856         dev->height = maxh;
857         dev->interlaced = 0;
858         dev->video_input = 0;
859
860         errCode = cx231xx_config(dev);
861         if (errCode < 0) {
862                 cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
863                                __func__, errCode);
864                 return errCode;
865         }
866
867         /* init video dma queues */
868         INIT_LIST_HEAD(&dev->video_mode.vidq.active);
869         INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
870
871         /* init vbi dma queues */
872         INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
873         INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
874
875         /* Reset other chips required if they are tied up with GPIO pins */
876         cx231xx_add_into_devlist(dev);
877
878         if (dev->board.has_417) {
879                 printk(KERN_INFO "attach 417 %d\n", dev->model);
880                 if (cx231xx_417_register(dev) < 0) {
881                         printk(KERN_ERR
882                                 "%s() Failed to register 417 on VID_B\n",
883                                __func__);
884                 }
885         }
886
887         retval = cx231xx_register_analog_devices(dev);
888         if (retval < 0) {
889                 cx231xx_release_resources(dev);
890                 return retval;
891         }
892
893         cx231xx_ir_init(dev);
894
895         cx231xx_init_extension(dev);
896
897         return 0;
898 }
899
900 #if defined(CONFIG_MODULES) && defined(MODULE)
901 static void request_module_async(struct work_struct *work)
902 {
903         struct cx231xx *dev = container_of(work,
904                                            struct cx231xx, request_module_wk);
905
906         if (dev->has_alsa_audio)
907                 request_module("cx231xx-alsa");
908
909         if (dev->board.has_dvb)
910                 request_module("cx231xx-dvb");
911
912 }
913
914 static void request_modules(struct cx231xx *dev)
915 {
916         INIT_WORK(&dev->request_module_wk, request_module_async);
917         schedule_work(&dev->request_module_wk);
918 }
919
920 static void flush_request_modules(struct cx231xx *dev)
921 {
922         flush_work_sync(&dev->request_module_wk);
923 }
924 #else
925 #define request_modules(dev)
926 #define flush_request_modules(dev)
927 #endif /* CONFIG_MODULES */
928
929 /*
930  * cx231xx_usb_probe()
931  * checks for supported devices
932  */
933 static int cx231xx_usb_probe(struct usb_interface *interface,
934                              const struct usb_device_id *id)
935 {
936         struct usb_device *udev;
937         struct usb_interface *uif;
938         struct cx231xx *dev = NULL;
939         int retval = -ENODEV;
940         int nr = 0, ifnum;
941         int i, isoc_pipe = 0;
942         char *speed;
943         char descr[255] = "";
944         struct usb_interface *lif = NULL;
945         struct usb_interface_assoc_descriptor *assoc_desc;
946
947         udev = usb_get_dev(interface_to_usbdev(interface));
948         ifnum = interface->altsetting[0].desc.bInterfaceNumber;
949
950         /*
951          * Interface number 0 - IR interface (handled by mceusb driver)
952          * Interface number 1 - AV interface (handled by this driver)
953          */
954         if (ifnum != 1)
955                 return -ENODEV;
956
957         /* Check to see next free device and mark as used */
958         nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
959         cx231xx_devused |= 1 << nr;
960
961         if (nr >= CX231XX_MAXBOARDS) {
962                 cx231xx_err(DRIVER_NAME
963                  ": Supports only %i cx231xx boards.\n", CX231XX_MAXBOARDS);
964                 cx231xx_devused &= ~(1 << nr);
965                 return -ENOMEM;
966         }
967
968         /* allocate memory for our device state and initialize it */
969         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
970         if (dev == NULL) {
971                 cx231xx_err(DRIVER_NAME ": out of memory!\n");
972                 cx231xx_devused &= ~(1 << nr);
973                 return -ENOMEM;
974         }
975
976         snprintf(dev->name, 29, "cx231xx #%d", nr);
977         dev->devno = nr;
978         dev->model = id->driver_info;
979         dev->video_mode.alt = -1;
980
981         dev->interface_count++;
982         /* reset gpio dir and value */
983         dev->gpio_dir = 0;
984         dev->gpio_val = 0;
985         dev->xc_fw_load_done = 0;
986         dev->has_alsa_audio = 1;
987         dev->power_mode = -1;
988         atomic_set(&dev->devlist_count, 0);
989
990         /* 0 - vbi ; 1 -sliced cc mode */
991         dev->vbi_or_sliced_cc_mode = 0;
992
993         /* get maximum no.of IAD interfaces */
994         assoc_desc = udev->actconfig->intf_assoc[0];
995         dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
996
997         /* init CIR module TBD */
998
999         /* store the current interface */
1000         lif = interface;
1001
1002         /*mode_tv: digital=1 or analog=0*/
1003         dev->mode_tv = 0;
1004
1005         dev->USE_ISO = transfer_mode;
1006
1007         switch (udev->speed) {
1008         case USB_SPEED_LOW:
1009                 speed = "1.5";
1010                 break;
1011         case USB_SPEED_UNKNOWN:
1012         case USB_SPEED_FULL:
1013                 speed = "12";
1014                 break;
1015         case USB_SPEED_HIGH:
1016                 speed = "480";
1017                 break;
1018         default:
1019                 speed = "unknown";
1020         }
1021
1022         if (udev->manufacturer)
1023                 strlcpy(descr, udev->manufacturer, sizeof(descr));
1024
1025         if (udev->product) {
1026                 if (*descr)
1027                         strlcat(descr, " ", sizeof(descr));
1028                 strlcat(descr, udev->product, sizeof(descr));
1029         }
1030         if (*descr)
1031                 strlcat(descr, " ", sizeof(descr));
1032
1033         cx231xx_info("New device %s@ %s Mbps "
1034              "(%04x:%04x) with %d interfaces\n",
1035              descr,
1036              speed,
1037              le16_to_cpu(udev->descriptor.idVendor),
1038              le16_to_cpu(udev->descriptor.idProduct),
1039              dev->max_iad_interface_count);
1040
1041         /* store the interface 0 back */
1042         lif = udev->actconfig->interface[0];
1043
1044         /* increment interface count */
1045         dev->interface_count++;
1046
1047         /* get device number */
1048         nr = dev->devno;
1049
1050         assoc_desc = udev->actconfig->intf_assoc[0];
1051         if (assoc_desc->bFirstInterface != ifnum) {
1052                 cx231xx_err(DRIVER_NAME ": Not found "
1053                             "matching IAD interface\n");
1054                 return -ENODEV;
1055         }
1056
1057         cx231xx_info("registering interface %d\n", ifnum);
1058
1059         /* save our data pointer in this interface device */
1060         usb_set_intfdata(lif, dev);
1061
1062         /*
1063          * AV device initialization - only done at the last interface
1064          */
1065
1066         /* Create v4l2 device */
1067         retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1068         if (retval) {
1069                 cx231xx_errdev("v4l2_device_register failed\n");
1070                 cx231xx_devused &= ~(1 << nr);
1071                 kfree(dev);
1072                 dev = NULL;
1073                 return -EIO;
1074         }
1075         /* allocate device struct */
1076         retval = cx231xx_init_dev(&dev, udev, nr);
1077         if (retval) {
1078                 cx231xx_devused &= ~(1 << dev->devno);
1079                 v4l2_device_unregister(&dev->v4l2_dev);
1080                 kfree(dev);
1081                 dev = NULL;
1082                 usb_set_intfdata(lif, NULL);
1083
1084                 return retval;
1085         }
1086
1087         /* compute alternate max packet sizes for video */
1088         uif = udev->actconfig->interface[dev->current_pcb_config.
1089                        hs_config_info[0].interface_info.video_index + 1];
1090
1091         dev->video_mode.end_point_addr = le16_to_cpu(uif->altsetting[0].
1092                         endpoint[isoc_pipe].desc.bEndpointAddress);
1093
1094         dev->video_mode.num_alt = uif->num_altsetting;
1095         cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1096                      dev->video_mode.end_point_addr,
1097                      dev->video_mode.num_alt);
1098         dev->video_mode.alt_max_pkt_size =
1099                 kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
1100
1101         if (dev->video_mode.alt_max_pkt_size == NULL) {
1102                 cx231xx_errdev("out of memory!\n");
1103                 cx231xx_devused &= ~(1 << nr);
1104                 v4l2_device_unregister(&dev->v4l2_dev);
1105                 kfree(dev);
1106                 dev = NULL;
1107                 return -ENOMEM;
1108         }
1109
1110         for (i = 0; i < dev->video_mode.num_alt; i++) {
1111                 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1112                                 desc.wMaxPacketSize);
1113                 dev->video_mode.alt_max_pkt_size[i] =
1114                     (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1115                 cx231xx_info("Alternate setting %i, max size= %i\n", i,
1116                              dev->video_mode.alt_max_pkt_size[i]);
1117         }
1118
1119         /* compute alternate max packet sizes for vbi */
1120         uif = udev->actconfig->interface[dev->current_pcb_config.
1121                                        hs_config_info[0].interface_info.
1122                                        vanc_index + 1];
1123
1124         dev->vbi_mode.end_point_addr =
1125             le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
1126                         bEndpointAddress);
1127
1128         dev->vbi_mode.num_alt = uif->num_altsetting;
1129         cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1130                      dev->vbi_mode.end_point_addr,
1131                      dev->vbi_mode.num_alt);
1132         dev->vbi_mode.alt_max_pkt_size =
1133             kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
1134
1135         if (dev->vbi_mode.alt_max_pkt_size == NULL) {
1136                 cx231xx_errdev("out of memory!\n");
1137                 cx231xx_devused &= ~(1 << nr);
1138                 v4l2_device_unregister(&dev->v4l2_dev);
1139                 kfree(dev);
1140                 dev = NULL;
1141                 return -ENOMEM;
1142         }
1143
1144         for (i = 0; i < dev->vbi_mode.num_alt; i++) {
1145                 u16 tmp =
1146                     le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1147                                 desc.wMaxPacketSize);
1148                 dev->vbi_mode.alt_max_pkt_size[i] =
1149                     (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1150                 cx231xx_info("Alternate setting %i, max size= %i\n", i,
1151                              dev->vbi_mode.alt_max_pkt_size[i]);
1152         }
1153
1154         /* compute alternate max packet sizes for sliced CC */
1155         uif = udev->actconfig->interface[dev->current_pcb_config.
1156                                        hs_config_info[0].interface_info.
1157                                        hanc_index + 1];
1158
1159         dev->sliced_cc_mode.end_point_addr =
1160             le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
1161                         bEndpointAddress);
1162
1163         dev->sliced_cc_mode.num_alt = uif->num_altsetting;
1164         cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1165                      dev->sliced_cc_mode.end_point_addr,
1166                      dev->sliced_cc_mode.num_alt);
1167         dev->sliced_cc_mode.alt_max_pkt_size =
1168                 kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
1169
1170         if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
1171                 cx231xx_errdev("out of memory!\n");
1172                 cx231xx_devused &= ~(1 << nr);
1173                 v4l2_device_unregister(&dev->v4l2_dev);
1174                 kfree(dev);
1175                 dev = NULL;
1176                 return -ENOMEM;
1177         }
1178
1179         for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
1180                 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
1181                                 desc.wMaxPacketSize);
1182                 dev->sliced_cc_mode.alt_max_pkt_size[i] =
1183                     (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1184                 cx231xx_info("Alternate setting %i, max size= %i\n", i,
1185                              dev->sliced_cc_mode.alt_max_pkt_size[i]);
1186         }
1187
1188         if (dev->current_pcb_config.ts1_source != 0xff) {
1189                 /* compute alternate max packet sizes for TS1 */
1190                 uif = udev->actconfig->interface[dev->current_pcb_config.
1191                                                hs_config_info[0].
1192                                                interface_info.
1193                                                ts1_index + 1];
1194
1195                 dev->ts1_mode.end_point_addr =
1196                     le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
1197                                 desc.bEndpointAddress);
1198
1199                 dev->ts1_mode.num_alt = uif->num_altsetting;
1200                 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
1201                              dev->ts1_mode.end_point_addr,
1202                              dev->ts1_mode.num_alt);
1203                 dev->ts1_mode.alt_max_pkt_size =
1204                         kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
1205
1206                 if (dev->ts1_mode.alt_max_pkt_size == NULL) {
1207                         cx231xx_errdev("out of memory!\n");
1208                         cx231xx_devused &= ~(1 << nr);
1209                         v4l2_device_unregister(&dev->v4l2_dev);
1210                         kfree(dev);
1211                         dev = NULL;
1212                         return -ENOMEM;
1213                 }
1214
1215                 for (i = 0; i < dev->ts1_mode.num_alt; i++) {
1216                         u16 tmp = le16_to_cpu(uif->altsetting[i].
1217                                                 endpoint[isoc_pipe].desc.
1218                                                 wMaxPacketSize);
1219                         dev->ts1_mode.alt_max_pkt_size[i] =
1220                             (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1221                         cx231xx_info("Alternate setting %i, max size= %i\n", i,
1222                                      dev->ts1_mode.alt_max_pkt_size[i]);
1223                 }
1224         }
1225
1226         if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER) {
1227                 cx231xx_enable_OSC(dev);
1228                 cx231xx_reset_out(dev);
1229                 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 3);
1230         }
1231
1232         if (dev->model == CX231XX_BOARD_CNXT_RDE_253S)
1233                 cx231xx_sleep_s5h1432(dev);
1234
1235         /* load other modules required */
1236         request_modules(dev);
1237
1238         return 0;
1239 }
1240
1241 /*
1242  * cx231xx_usb_disconnect()
1243  * called when the device gets diconencted
1244  * video device will be unregistered on v4l2_close in case it is still open
1245  */
1246 static void cx231xx_usb_disconnect(struct usb_interface *interface)
1247 {
1248         struct cx231xx *dev;
1249
1250         dev = usb_get_intfdata(interface);
1251         usb_set_intfdata(interface, NULL);
1252
1253         if (!dev)
1254                 return;
1255
1256         if (!dev->udev)
1257                 return;
1258
1259         flush_request_modules(dev);
1260
1261         /* delete v4l2 device */
1262         v4l2_device_unregister(&dev->v4l2_dev);
1263
1264         /* wait until all current v4l2 io is finished then deallocate
1265            resources */
1266         mutex_lock(&dev->lock);
1267
1268         wake_up_interruptible_all(&dev->open);
1269
1270         if (dev->users) {
1271                 cx231xx_warn
1272                     ("device %s is open! Deregistration and memory "
1273                      "deallocation are deferred on close.\n",
1274                      video_device_node_name(dev->vdev));
1275
1276                 dev->state |= DEV_MISCONFIGURED;
1277                 if (dev->USE_ISO)
1278                         cx231xx_uninit_isoc(dev);
1279                 else
1280                         cx231xx_uninit_bulk(dev);
1281                 dev->state |= DEV_DISCONNECTED;
1282                 wake_up_interruptible(&dev->wait_frame);
1283                 wake_up_interruptible(&dev->wait_stream);
1284         } else {
1285                 dev->state |= DEV_DISCONNECTED;
1286                 cx231xx_release_resources(dev);
1287         }
1288
1289         cx231xx_close_extension(dev);
1290
1291         mutex_unlock(&dev->lock);
1292
1293         if (!dev->users) {
1294                 kfree(dev->video_mode.alt_max_pkt_size);
1295                 kfree(dev->vbi_mode.alt_max_pkt_size);
1296                 kfree(dev->sliced_cc_mode.alt_max_pkt_size);
1297                 kfree(dev->ts1_mode.alt_max_pkt_size);
1298                 kfree(dev);
1299                 dev = NULL;
1300         }
1301 }
1302
1303 static struct usb_driver cx231xx_usb_driver = {
1304         .name = "cx231xx",
1305         .probe = cx231xx_usb_probe,
1306         .disconnect = cx231xx_usb_disconnect,
1307         .id_table = cx231xx_id_table,
1308 };
1309
1310 static int __init cx231xx_module_init(void)
1311 {
1312         int result;
1313
1314         printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
1315
1316         /* register this driver with the USB subsystem */
1317         result = usb_register(&cx231xx_usb_driver);
1318         if (result)
1319                 cx231xx_err(DRIVER_NAME
1320                             " usb_register failed. Error number %d.\n", result);
1321
1322         return result;
1323 }
1324
1325 static void __exit cx231xx_module_exit(void)
1326 {
1327         /* deregister this driver with the USB subsystem */
1328         usb_deregister(&cx231xx_usb_driver);
1329 }
1330
1331 module_init(cx231xx_module_init);
1332 module_exit(cx231xx_module_exit);