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