V4L/DVB (11135): cx231xx: use usb_make_path() for bus_info
[linux-2.6] / 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/delay.h>
26 #include <linux/i2c.h>
27 #include <linux/usb.h>
28 #include <media/tuner.h>
29 #include <media/tveeprom.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-chip-ident.h>
32
33 #include <media/cx25840.h>
34 #include "xc5000.h"
35
36 #include "cx231xx.h"
37
38 static int tuner = -1;
39 module_param(tuner, int, 0444);
40 MODULE_PARM_DESC(tuner, "tuner type");
41
42 static unsigned int disable_ir;
43 module_param(disable_ir, int, 0444);
44 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
45
46 /* Bitmask marking allocated devices from 0 to CX231XX_MAXBOARDS */
47 static unsigned long cx231xx_devused;
48
49 /*
50  *  Reset sequences for analog/digital modes
51  */
52
53 static struct cx231xx_reg_seq RDE250_XCV_TUNER[] = {
54         {0x03, 0x01, 10},
55         {0x03, 0x00, 30},
56         {0x03, 0x01, 10},
57         {-1, -1, -1},
58 };
59
60 /*
61  *  Board definitions
62  */
63 struct cx231xx_board cx231xx_boards[] = {
64         [CX231XX_BOARD_UNKNOWN] = {
65                 .name = "Unknown CX231xx video grabber",
66                 .tuner_type = TUNER_ABSENT,
67                 .input = {{
68                                 .type = CX231XX_VMUX_TELEVISION,
69                                 .vmux = CX231XX_VIN_3_1,
70                                 .amux = CX231XX_AMUX_VIDEO,
71                                 .gpio = 0,
72                         }, {
73                                 .type = CX231XX_VMUX_COMPOSITE1,
74                                 .vmux = CX231XX_VIN_2_1,
75                                 .amux = CX231XX_AMUX_LINE_IN,
76                                 .gpio = 0,
77                         }, {
78                                 .type = CX231XX_VMUX_SVIDEO,
79                                 .vmux = CX231XX_VIN_1_1 |
80                                         (CX231XX_VIN_1_2 << 8) |
81                                         CX25840_SVIDEO_ON,
82                                 .amux = CX231XX_AMUX_LINE_IN,
83                                 .gpio = 0,
84                         }
85                 },
86         },
87         [CX231XX_BOARD_CNXT_RDE_250] = {
88                 .name = "Conexant Hybrid TV - RDE250",
89                 .tuner_type = TUNER_XC5000,
90                 .tuner_addr = 0x61,
91                 .tuner_gpio = RDE250_XCV_TUNER,
92                 .tuner_sif_gpio = 0x05,
93                 .tuner_scl_gpio = 0x1a,
94                 .tuner_sda_gpio = 0x1b,
95                 .decoder = CX231XX_AVDECODER,
96                 .demod_xfer_mode = 0,
97                 .ctl_pin_status_mask = 0xFFFFFFC4,
98                 .agc_analog_digital_select_gpio = 0x0c,
99                 .gpio_pin_status_mask = 0x4001000,
100                 .tuner_i2c_master = 1,
101                 .demod_i2c_master = 2,
102                 .has_dvb = 1,
103                 .demod_addr = 0x02,
104                 .norm = V4L2_STD_PAL,
105
106                 .input = {{
107                                 .type = CX231XX_VMUX_TELEVISION,
108                                 .vmux = CX231XX_VIN_3_1,
109                                 .amux = CX231XX_AMUX_VIDEO,
110                                 .gpio = 0,
111                         }, {
112                                 .type = CX231XX_VMUX_COMPOSITE1,
113                                 .vmux = CX231XX_VIN_2_1,
114                                 .amux = CX231XX_AMUX_LINE_IN,
115                                 .gpio = 0,
116                         }, {
117                                 .type = CX231XX_VMUX_SVIDEO,
118                                 .vmux = CX231XX_VIN_1_1 |
119                                         (CX231XX_VIN_1_2 << 8) |
120                                         CX25840_SVIDEO_ON,
121                                 .amux = CX231XX_AMUX_LINE_IN,
122                                 .gpio = 0,
123                         }
124                 },
125         },
126
127         [CX231XX_BOARD_CNXT_RDU_250] = {
128                 .name = "Conexant Hybrid TV - RDU250",
129                 .tuner_type = TUNER_XC5000,
130                 .tuner_addr = 0x61,
131                 .tuner_gpio = RDE250_XCV_TUNER,
132                 .tuner_sif_gpio = 0x05,
133                 .tuner_scl_gpio = 0x1a,
134                 .tuner_sda_gpio = 0x1b,
135                 .decoder = CX231XX_AVDECODER,
136                 .demod_xfer_mode = 0,
137                 .ctl_pin_status_mask = 0xFFFFFFC4,
138                 .agc_analog_digital_select_gpio = 0x0c,
139                 .gpio_pin_status_mask = 0x4001000,
140                 .tuner_i2c_master = 1,
141                 .demod_i2c_master = 2,
142                 .has_dvb = 1,
143                 .demod_addr = 0x32,
144                 .norm = V4L2_STD_NTSC,
145
146                 .input = {{
147                                 .type = CX231XX_VMUX_TELEVISION,
148                                 .vmux = CX231XX_VIN_3_1,
149                                 .amux = CX231XX_AMUX_VIDEO,
150                                 .gpio = 0,
151                         }, {
152                                 .type = CX231XX_VMUX_COMPOSITE1,
153                                 .vmux = CX231XX_VIN_2_1,
154                                 .amux = CX231XX_AMUX_LINE_IN,
155                                 .gpio = 0,
156                         }, {
157                                 .type = CX231XX_VMUX_SVIDEO,
158                                 .vmux = CX231XX_VIN_1_1 |
159                                         (CX231XX_VIN_1_2 << 8) |
160                                         CX25840_SVIDEO_ON,
161                                 .amux = CX231XX_AMUX_LINE_IN,
162                                 .gpio = 0,
163                         }
164                 },
165         },
166 };
167 const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);
168
169 /* table of devices that work with this driver */
170 struct usb_device_id cx231xx_id_table[] = {
171         {USB_DEVICE(0x0572, 0x5A3C),
172          .driver_info = CX231XX_BOARD_UNKNOWN},
173         {USB_DEVICE(0x0572, 0x58A2),
174          .driver_info = CX231XX_BOARD_CNXT_RDE_250},
175         {USB_DEVICE(0x0572, 0x58A1),
176          .driver_info = CX231XX_BOARD_CNXT_RDU_250},
177         {},
178 };
179
180 MODULE_DEVICE_TABLE(usb, cx231xx_id_table);
181
182 /* cx231xx_tuner_callback
183  * will be used to reset XC5000 tuner using GPIO pin
184  */
185
186 int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
187 {
188         int rc = 0;
189         struct cx231xx *dev = ptr;
190
191         if (dev->tuner_type == TUNER_XC5000) {
192                 if (command == XC5000_TUNER_RESET) {
193                         cx231xx_info
194                                 ("Tuner CB: RESET: cmd %d : tuner type %d \n",
195                                  command, dev->tuner_type);
196                         cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
197                                                1);
198                         msleep(10);
199                         cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
200                                                0);
201                         msleep(330);
202                         cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
203                                                1);
204                         msleep(10);
205                 }
206         }
207         return rc;
208 }
209 EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);
210
211 static inline void cx231xx_set_model(struct cx231xx *dev)
212 {
213         memcpy(&dev->board, &cx231xx_boards[dev->model], sizeof(dev->board));
214 }
215
216 /* Since cx231xx_pre_card_setup() requires a proper dev->model,
217  * this won't work for boards with generic PCI IDs
218  */
219 void cx231xx_pre_card_setup(struct cx231xx *dev)
220 {
221
222         cx231xx_set_model(dev);
223
224         cx231xx_info("Identified as %s (card=%d)\n",
225                      dev->board.name, dev->model);
226
227         /* set the direction for GPIO pins */
228         cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
229         cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
230         cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
231
232         /* request some modules if any required */
233
234         /* reset the Tuner */
235         cx231xx_gpio_set(dev, dev->board.tuner_gpio);
236
237         /* set the mode to Analog mode initially */
238         cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
239
240         /* Unlock device */
241         /* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
242
243 }
244
245 static void cx231xx_config_tuner(struct cx231xx *dev)
246 {
247         struct tuner_setup tun_setup;
248         struct v4l2_frequency f;
249
250         if (dev->tuner_type == TUNER_ABSENT)
251                 return;
252
253         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
254         tun_setup.type = dev->tuner_type;
255         tun_setup.addr = dev->tuner_addr;
256         tun_setup.tuner_callback = cx231xx_tuner_callback;
257
258         tuner_call(dev, tuner, s_type_addr, &tun_setup);
259
260 #if 0
261         if (tun_setup.type == TUNER_XC5000) {
262                 static struct xc2028_ctrl ctrl = {
263                         .fname = XC5000_DEFAULT_FIRMWARE,
264                         .max_len = 64,
265                         .demod = 0;
266                 };
267                 struct v4l2_priv_tun_config cfg = {
268                         .tuner = dev->tuner_type,
269                         .priv = &ctrl,
270                 };
271                 tuner_call(dev, tuner, s_config, &cfg);
272         }
273 #endif
274         /* configure tuner */
275         f.tuner = 0;
276         f.type = V4L2_TUNER_ANALOG_TV;
277         f.frequency = 9076;     /* just a magic number */
278         dev->ctl_freq = f.frequency;
279         call_all(dev, tuner, s_frequency, &f);
280
281 }
282
283 /* ----------------------------------------------------------------------- */
284 void cx231xx_set_ir(struct cx231xx *dev, struct IR_i2c *ir)
285 {
286         if (disable_ir) {
287                 ir->get_key = NULL;
288                 return;
289         }
290
291         /* detect & configure */
292         switch (dev->model) {
293
294         case CX231XX_BOARD_CNXT_RDE_250:
295                 break;
296         case CX231XX_BOARD_CNXT_RDU_250:
297                 break;
298         default:
299                 break;
300         }
301 }
302
303 void cx231xx_card_setup(struct cx231xx *dev)
304 {
305
306         cx231xx_set_model(dev);
307
308         dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
309         if (cx231xx_boards[dev->model].tuner_addr)
310                 dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
311
312         /* request some modules */
313         if (dev->board.decoder == CX231XX_AVDECODER) {
314                 dev->sd_cx25840 =
315                         v4l2_i2c_new_subdev(&dev->i2c_bus[0].i2c_adap,
316                                         "cx25840", "cx25840", 0x88 >> 1);
317                 if (dev->sd_cx25840 == NULL)
318                         cx231xx_info("cx25840 subdev registration failure\n");
319                 cx25840_call(dev, core, init, 0);
320
321         }
322
323         if (dev->board.tuner_type != TUNER_ABSENT) {
324                 dev->sd_tuner =
325                         v4l2_i2c_new_subdev(&dev->i2c_bus[1].i2c_adap,
326                                 "tuner", "tuner", 0xc2 >> 1);
327                 if (dev->sd_tuner == NULL)
328                         cx231xx_info("tuner subdev registration failure\n");
329
330                 cx231xx_config_tuner(dev);
331         }
332
333         cx231xx_config_tuner(dev);
334
335 #if 0
336         /* TBD  IR will be added later */
337         cx231xx_ir_init(dev);
338 #endif
339 }
340
341 /*
342  * cx231xx_config()
343  * inits registers with sane defaults
344  */
345 int cx231xx_config(struct cx231xx *dev)
346 {
347         /* TBD need to add cx231xx specific code */
348         dev->mute = 1;          /* maybe not the right place... */
349         dev->volume = 0x1f;
350
351         return 0;
352 }
353
354 /*
355  * cx231xx_config_i2c()
356  * configure i2c attached devices
357  */
358 void cx231xx_config_i2c(struct cx231xx *dev)
359 {
360         struct v4l2_routing route;
361
362         route.input = INPUT(dev->video_input)->vmux;
363         route.output = 0;
364
365         call_all(dev, video, s_stream, 1);
366 }
367
368 /*
369  * cx231xx_realease_resources()
370  * unregisters the v4l2,i2c and usb devices
371  * called when the device gets disconected or at module unload
372 */
373 void cx231xx_release_resources(struct cx231xx *dev)
374 {
375
376 #if 0           /* TBD IR related  */
377         if (dev->ir)
378                 cx231xx_ir_fini(dev);
379 #endif
380
381         cx231xx_release_analog_resources(dev);
382
383         cx231xx_remove_from_devlist(dev);
384
385         cx231xx_dev_uninit(dev);
386
387         usb_put_dev(dev->udev);
388
389         /* Mark device as unused */
390         cx231xx_devused &= ~(1 << dev->devno);
391 }
392
393 /*
394  * cx231xx_init_dev()
395  * allocates and inits the device structs, registers i2c bus and v4l device
396  */
397 static int cx231xx_init_dev(struct cx231xx **devhandle, struct usb_device *udev,
398                             int minor)
399 {
400         struct cx231xx *dev = *devhandle;
401         int retval = -ENOMEM;
402         int errCode;
403         unsigned int maxh, maxw;
404
405         dev->udev = udev;
406         mutex_init(&dev->lock);
407         mutex_init(&dev->ctrl_urb_lock);
408         mutex_init(&dev->gpio_i2c_lock);
409
410         spin_lock_init(&dev->video_mode.slock);
411         spin_lock_init(&dev->vbi_mode.slock);
412         spin_lock_init(&dev->sliced_cc_mode.slock);
413
414         init_waitqueue_head(&dev->open);
415         init_waitqueue_head(&dev->wait_frame);
416         init_waitqueue_head(&dev->wait_stream);
417
418         dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
419         dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
420         dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
421         dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
422         dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
423
424         /* Query cx231xx to find what pcb config it is related to */
425         initialize_cx231xx(dev);
426
427         /* Cx231xx pre card setup */
428         cx231xx_pre_card_setup(dev);
429
430         errCode = cx231xx_config(dev);
431         if (errCode) {
432                 cx231xx_errdev("error configuring device\n");
433                 return -ENOMEM;
434         }
435
436         /* set default norm */
437         dev->norm = dev->board.norm;
438
439         /* register i2c bus */
440         errCode = cx231xx_dev_init(dev);
441         if (errCode < 0) {
442                 cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
443                                __func__, errCode);
444                 return errCode;
445         }
446
447         /* Do board specific init */
448         cx231xx_card_setup(dev);
449
450         /* configure the device */
451         cx231xx_config_i2c(dev);
452
453         maxw = norm_maxw(dev);
454         maxh = norm_maxh(dev);
455
456         /* set default image size */
457         dev->width = maxw;
458         dev->height = maxh;
459         dev->interlaced = 0;
460         dev->hscale = 0;
461         dev->vscale = 0;
462         dev->video_input = 0;
463
464         errCode = cx231xx_config(dev);
465         if (errCode < 0) {
466                 cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
467                                __func__, errCode);
468                 return errCode;
469         }
470
471         /* init video dma queues */
472         INIT_LIST_HEAD(&dev->video_mode.vidq.active);
473         INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
474
475         /* init vbi dma queues */
476         INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
477         INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
478
479         /* Reset other chips required if they are tied up with GPIO pins */
480
481         cx231xx_add_into_devlist(dev);
482
483         retval = cx231xx_register_analog_devices(dev);
484         if (retval < 0) {
485                 cx231xx_release_resources(dev);
486                 goto fail_reg_devices;
487         }
488
489         cx231xx_init_extension(dev);
490
491         return 0;
492
493 fail_reg_devices:
494         mutex_unlock(&dev->lock);
495         return retval;
496 }
497
498 #if defined(CONFIG_MODULES) && defined(MODULE)
499 static void request_module_async(struct work_struct *work)
500 {
501         struct cx231xx *dev = container_of(work,
502                                            struct cx231xx, request_module_wk);
503
504         if (dev->has_alsa_audio)
505                 request_module("cx231xx-alsa");
506
507         if (dev->board.has_dvb)
508                 request_module("cx231xx-dvb");
509
510 }
511
512 static void request_modules(struct cx231xx *dev)
513 {
514         INIT_WORK(&dev->request_module_wk, request_module_async);
515         schedule_work(&dev->request_module_wk);
516 }
517 #else
518 #define request_modules(dev)
519 #endif /* CONFIG_MODULES */
520
521 /*
522  * cx231xx_usb_probe()
523  * checks for supported devices
524  */
525 static int cx231xx_usb_probe(struct usb_interface *interface,
526                              const struct usb_device_id *id)
527 {
528         struct usb_device *udev;
529         struct usb_interface *uif;
530         struct cx231xx *dev = NULL;
531         int retval = -ENODEV;
532         int nr = 0, ifnum;
533         int i, isoc_pipe = 0;
534         char *speed;
535         char descr[255] = "";
536         struct usb_interface *lif = NULL;
537         int skip_interface = 0;
538         struct usb_interface_assoc_descriptor *assoc_desc;
539
540         udev = usb_get_dev(interface_to_usbdev(interface));
541         ifnum = interface->altsetting[0].desc.bInterfaceNumber;
542
543         if (!ifnum) {
544                 /*
545                  * Interface number 0 - IR interface
546                  */
547                 /* Check to see next free device and mark as used */
548                 nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
549                 cx231xx_devused |= 1 << nr;
550
551                 if (nr >= CX231XX_MAXBOARDS) {
552                         cx231xx_err(DRIVER_NAME ": Supports only %i cx231xx boards.\n",
553                                      CX231XX_MAXBOARDS);
554                         cx231xx_devused &= ~(1 << nr);
555                         return -ENOMEM;
556                 }
557
558                 /* allocate memory for our device state and initialize it */
559                 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
560                 if (dev == NULL) {
561                         cx231xx_err(DRIVER_NAME ": out of memory!\n");
562                         cx231xx_devused &= ~(1 << nr);
563                         return -ENOMEM;
564                 }
565
566                 snprintf(dev->name, 29, "cx231xx #%d", nr);
567                 dev->devno = nr;
568                 dev->model = id->driver_info;
569                 dev->video_mode.alt = -1;
570                 dev->interface_count++;
571
572                 /* reset gpio dir and value */
573                 dev->gpio_dir = 0;
574                 dev->gpio_val = 0;
575                 dev->xc_fw_load_done = 0;
576                 dev->has_alsa_audio = 1;
577                 dev->power_mode = -1;
578
579                 /* 0 - vbi ; 1 -sliced cc mode */
580                 dev->vbi_or_sliced_cc_mode = 0;
581
582                 /* get maximum no.of IAD interfaces */
583                 assoc_desc = udev->actconfig->intf_assoc[0];
584                 dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
585
586                 /* init CIR module TBD */
587
588                 /* store the current interface */
589                 lif = interface;
590
591                 switch (udev->speed) {
592                 case USB_SPEED_LOW:
593                         speed = "1.5";
594                         break;
595                 case USB_SPEED_UNKNOWN:
596                 case USB_SPEED_FULL:
597                         speed = "12";
598                         break;
599                 case USB_SPEED_HIGH:
600                         speed = "480";
601                         break;
602                 default:
603                         speed = "unknown";
604                 }
605
606                 if (udev->manufacturer)
607                         strlcpy(descr, udev->manufacturer, sizeof(descr));
608
609                 if (udev->product) {
610                         if (*descr)
611                                 strlcat(descr, " ", sizeof(descr));
612                         strlcat(descr, udev->product, sizeof(descr));
613                 }
614                 if (*descr)
615                         strlcat(descr, " ", sizeof(descr));
616
617                 cx231xx_info("New device %s@ %s Mbps "
618                      "(%04x:%04x) with %d interfaces\n",
619                      descr,
620                      speed,
621                      le16_to_cpu(udev->descriptor.idVendor),
622                      le16_to_cpu(udev->descriptor.idProduct),
623                      dev->max_iad_interface_count);
624         } else {
625                 /* Get dev structure first */
626                 dev = usb_get_intfdata(udev->actconfig->interface[0]);
627                 if (dev == NULL) {
628                         cx231xx_err(DRIVER_NAME ": out of first interface!\n");
629                         return -ENODEV;
630                 }
631
632                 /* store the interface 0 back */
633                 lif = udev->actconfig->interface[0];
634
635                 /* increment interface count */
636                 dev->interface_count++;
637
638                 /* get device number */
639                 nr = dev->devno;
640
641                 /*
642                  * set skip interface, for all interfaces but
643                  * interface 1 and the last one
644                  */
645                 if ((ifnum != 1) && ((dev->interface_count - 1)
646                                      != dev->max_iad_interface_count))
647                         skip_interface = 1;
648
649                 if (ifnum == 1) {
650                         assoc_desc = udev->actconfig->intf_assoc[0];
651                         if (assoc_desc->bFirstInterface != ifnum) {
652                                 cx231xx_err(DRIVER_NAME ": Not found "
653                                             "matching IAD interface\n");
654                                 return -ENODEV;
655                         }
656                 }
657         }
658
659         if (skip_interface)
660                 return -ENODEV;
661
662         cx231xx_info("registering interface %d\n", ifnum);
663
664         /* save our data pointer in this interface device */
665         usb_set_intfdata(lif, dev);
666
667         if ((dev->interface_count - 1) != dev->max_iad_interface_count)
668                 return 0;
669
670         /*
671          * AV device initialization - only done at the last interface
672          */
673
674         /* Create v4l2 device */
675         snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
676                                         "%s-%03d", "cx231xx", nr);
677         retval = v4l2_device_register(&udev->dev, &dev->v4l2_dev);
678         if (retval) {
679                 cx231xx_errdev("v4l2_device_register failed\n");
680                 cx231xx_devused &= ~(1 << nr);
681                 kfree(dev);
682                 return -EIO;
683         }
684
685         /* allocate device struct */
686         retval = cx231xx_init_dev(&dev, udev, nr);
687         if (retval) {
688                 cx231xx_devused &= ~(1 << dev->devno);
689                 v4l2_device_unregister(&dev->v4l2_dev);
690                 kfree(dev);
691                 return retval;
692         }
693
694         /* compute alternate max packet sizes for video */
695         uif = udev->actconfig->interface[dev->current_pcb_config.
696                        hs_config_info[0].interface_info.video_index + 1];
697
698         dev->video_mode.end_point_addr = le16_to_cpu(uif->altsetting[0].
699                         endpoint[isoc_pipe].desc.bEndpointAddress);
700
701         dev->video_mode.num_alt = uif->num_altsetting;
702         cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
703                      dev->video_mode.end_point_addr,
704                      dev->video_mode.num_alt);
705         dev->video_mode.alt_max_pkt_size =
706                 kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
707
708         if (dev->video_mode.alt_max_pkt_size == NULL) {
709                 cx231xx_errdev("out of memory!\n");
710                 cx231xx_devused &= ~(1 << nr);
711                 v4l2_device_unregister(&dev->v4l2_dev);
712                 kfree(dev);
713                 return -ENOMEM;
714         }
715
716         for (i = 0; i < dev->video_mode.num_alt; i++) {
717                 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
718                                 desc.wMaxPacketSize);
719                 dev->video_mode.alt_max_pkt_size[i] =
720                     (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
721                 cx231xx_info("Alternate setting %i, max size= %i\n", i,
722                              dev->video_mode.alt_max_pkt_size[i]);
723         }
724
725         /* compute alternate max packet sizes for vbi */
726         uif = udev->actconfig->interface[dev->current_pcb_config.
727                                        hs_config_info[0].interface_info.
728                                        vanc_index + 1];
729
730         dev->vbi_mode.end_point_addr =
731             le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
732                         bEndpointAddress);
733
734         dev->vbi_mode.num_alt = uif->num_altsetting;
735         cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
736                      dev->vbi_mode.end_point_addr,
737                      dev->vbi_mode.num_alt);
738         dev->vbi_mode.alt_max_pkt_size =
739             kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
740
741         if (dev->vbi_mode.alt_max_pkt_size == NULL) {
742                 cx231xx_errdev("out of memory!\n");
743                 cx231xx_devused &= ~(1 << nr);
744                 v4l2_device_unregister(&dev->v4l2_dev);
745                 kfree(dev);
746                 return -ENOMEM;
747         }
748
749         for (i = 0; i < dev->vbi_mode.num_alt; i++) {
750                 u16 tmp =
751                     le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
752                                 desc.wMaxPacketSize);
753                 dev->vbi_mode.alt_max_pkt_size[i] =
754                     (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
755                 cx231xx_info("Alternate setting %i, max size= %i\n", i,
756                              dev->vbi_mode.alt_max_pkt_size[i]);
757         }
758
759         /* compute alternate max packet sizes for sliced CC */
760         uif = udev->actconfig->interface[dev->current_pcb_config.
761                                        hs_config_info[0].interface_info.
762                                        hanc_index + 1];
763
764         dev->sliced_cc_mode.end_point_addr =
765             le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
766                         bEndpointAddress);
767
768         dev->sliced_cc_mode.num_alt = uif->num_altsetting;
769         cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
770                      dev->sliced_cc_mode.end_point_addr,
771                      dev->sliced_cc_mode.num_alt);
772         dev->sliced_cc_mode.alt_max_pkt_size =
773                 kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
774
775         if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
776                 cx231xx_errdev("out of memory!\n");
777                 cx231xx_devused &= ~(1 << nr);
778                 v4l2_device_unregister(&dev->v4l2_dev);
779                 kfree(dev);
780                 return -ENOMEM;
781         }
782
783         for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
784                 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
785                                 desc.wMaxPacketSize);
786                 dev->sliced_cc_mode.alt_max_pkt_size[i] =
787                     (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
788                 cx231xx_info("Alternate setting %i, max size= %i\n", i,
789                              dev->sliced_cc_mode.alt_max_pkt_size[i]);
790         }
791
792         if (dev->current_pcb_config.ts1_source != 0xff) {
793                 /* compute alternate max packet sizes for TS1 */
794                 uif = udev->actconfig->interface[dev->current_pcb_config.
795                                                hs_config_info[0].
796                                                interface_info.
797                                                ts1_index + 1];
798
799                 dev->ts1_mode.end_point_addr =
800                     le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
801                                 desc.bEndpointAddress);
802
803                 dev->ts1_mode.num_alt = uif->num_altsetting;
804                 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
805                              dev->ts1_mode.end_point_addr,
806                              dev->ts1_mode.num_alt);
807                 dev->ts1_mode.alt_max_pkt_size =
808                         kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
809
810                 if (dev->ts1_mode.alt_max_pkt_size == NULL) {
811                         cx231xx_errdev("out of memory!\n");
812                         cx231xx_devused &= ~(1 << nr);
813                         v4l2_device_unregister(&dev->v4l2_dev);
814                         kfree(dev);
815                         return -ENOMEM;
816                 }
817
818                 for (i = 0; i < dev->ts1_mode.num_alt; i++) {
819                         u16 tmp = le16_to_cpu(uif->altsetting[i].
820                                                 endpoint[isoc_pipe].desc.
821                                                 wMaxPacketSize);
822                         dev->ts1_mode.alt_max_pkt_size[i] =
823                             (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
824                         cx231xx_info("Alternate setting %i, max size= %i\n", i,
825                                      dev->ts1_mode.alt_max_pkt_size[i]);
826                 }
827         }
828
829         /* load other modules required */
830         request_modules(dev);
831
832         return 0;
833 }
834
835 /*
836  * cx231xx_usb_disconnect()
837  * called when the device gets diconencted
838  * video device will be unregistered on v4l2_close in case it is still open
839  */
840 static void cx231xx_usb_disconnect(struct usb_interface *interface)
841 {
842         struct cx231xx *dev;
843
844         dev = usb_get_intfdata(interface);
845         usb_set_intfdata(interface, NULL);
846
847         if (!dev)
848                 return;
849
850         if (!dev->udev)
851                 return;
852
853         /* delete v4l2 device */
854         v4l2_device_unregister(&dev->v4l2_dev);
855
856         /* wait until all current v4l2 io is finished then deallocate
857            resources */
858         mutex_lock(&dev->lock);
859
860         wake_up_interruptible_all(&dev->open);
861
862         if (dev->users) {
863                 cx231xx_warn
864                     ("device /dev/video%d is open! Deregistration and memory "
865                      "deallocation are deferred on close.\n", dev->vdev->num);
866
867                 dev->state |= DEV_MISCONFIGURED;
868                 cx231xx_uninit_isoc(dev);
869                 dev->state |= DEV_DISCONNECTED;
870                 wake_up_interruptible(&dev->wait_frame);
871                 wake_up_interruptible(&dev->wait_stream);
872         } else {
873                 dev->state |= DEV_DISCONNECTED;
874                 cx231xx_release_resources(dev);
875         }
876
877         cx231xx_close_extension(dev);
878
879         mutex_unlock(&dev->lock);
880
881         if (!dev->users) {
882                 kfree(dev->video_mode.alt_max_pkt_size);
883                 kfree(dev->vbi_mode.alt_max_pkt_size);
884                 kfree(dev->sliced_cc_mode.alt_max_pkt_size);
885                 kfree(dev->ts1_mode.alt_max_pkt_size);
886                 kfree(dev);
887         }
888 }
889
890 static struct usb_driver cx231xx_usb_driver = {
891         .name = "cx231xx",
892         .probe = cx231xx_usb_probe,
893         .disconnect = cx231xx_usb_disconnect,
894         .id_table = cx231xx_id_table,
895 };
896
897 static int __init cx231xx_module_init(void)
898 {
899         int result;
900
901         printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
902
903         /* register this driver with the USB subsystem */
904         result = usb_register(&cx231xx_usb_driver);
905         if (result)
906                 cx231xx_err(DRIVER_NAME
907                             " usb_register failed. Error number %d.\n", result);
908
909         return result;
910 }
911
912 static void __exit cx231xx_module_exit(void)
913 {
914         /* deregister this driver with the USB subsystem */
915         usb_deregister(&cx231xx_usb_driver);
916 }
917
918 module_init(cx231xx_module_init);
919 module_exit(cx231xx_module_exit);