V4L/DVB (11038): Fix the issue with audio module & correction of Names
[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/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         cx231xx_info("Precard: Board is %s\n", dev->board.name);
228         /* set the direction for GPIO pins */
229         cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
230         cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
231         cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
232
233         /* request some modules if any required */
234
235         /* reset the Tuner */
236         cx231xx_gpio_set(dev, dev->board.tuner_gpio);
237
238         /* set the mode to Analog mode initially */
239         cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
240
241         /* Unlock device */
242         /* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
243
244 }
245
246 #if 0
247
248 static void cx231xx_config_tuner(struct cx231xx *dev)
249 {
250         struct tuner_setup tun_setup;
251         struct v4l2_frequency f;
252
253         if (dev->tuner_type == TUNER_ABSENT)
254                 return;
255
256         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
257         tun_setup.type = dev->tuner_type;
258         tun_setup.addr = dev->tuner_addr;
259         tun_setup.tuner_callback = cx231xx_tuner_callback;
260
261         cx231xx_i2c_call_clients(&dev->i2c_bus[1], TUNER_SET_TYPE_ADDR,
262                                  &tun_setup);
263 #if 0
264         if (tun_setup.type == TUNER_XC5000) {
265                 static struct xc2028_ctrl ctrl = {
266                         .fname = XC5000_DEFAULT_FIRMWARE,
267                         .max_len = 64,
268                         .demod = 0;
269                 };
270                 struct v4l2_priv_tun_config cfg = {
271                         .tuner = dev->tuner_type,
272                         .priv = &ctrl,
273                 };
274                 cx231xx_i2c_call_clients(&dev->i2c_bus[1], TUNER_SET_CONFIG,
275                                          &cfg);
276         }
277 #endif
278
279         /* configure tuner */
280         f.tuner = 0;
281         f.type = V4L2_TUNER_ANALOG_TV;
282         f.frequency = 9076;     /* just a magic number */
283         dev->ctl_freq = f.frequency;
284         cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
285 }
286
287 #endif
288
289 /* ----------------------------------------------------------------------- */
290 void cx231xx_set_ir(struct cx231xx *dev, struct IR_i2c *ir)
291 {
292         if (disable_ir) {
293                 ir->get_key = NULL;
294                 return;
295         }
296
297         /* detect & configure */
298         switch (dev->model) {
299
300         case CX231XX_BOARD_CNXT_RDE_250:
301                 break;
302         case CX231XX_BOARD_CNXT_RDU_250:
303                 break;
304         default:
305                 break;
306         }
307 }
308
309 void cx231xx_card_setup(struct cx231xx *dev)
310 {
311         cx231xx_set_model(dev);
312
313         dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
314         if (cx231xx_boards[dev->model].tuner_addr)
315                 dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
316
317         cx231xx_info(": tuner type %d, tuner address %d \n",
318                      dev->tuner_type, dev->tuner_addr);
319
320         /* Do card specific if any */
321         switch (dev->model) {
322         case CX231XX_BOARD_CNXT_RDE_250:
323                 /* do card specific GPIO settings if required */
324                 cx231xx_info("Board is Conexant RDE 250\n");
325                 break;
326         case CX231XX_BOARD_CNXT_RDU_250:
327                 /* do card specific GPIO settings if required */
328                 cx231xx_info("Board is Conexant RDU 250\n");
329                 break;
330         }
331
332         /* request some modules */
333         if (dev->board.decoder == CX231XX_AVDECODER) {
334                 cx231xx_info(": Requesting cx25840 module\n");
335                 request_module("cx25840");
336         }
337 #if 0
338         if (dev->board.tuner_type != TUNER_ABSENT) {
339                 cx231xx_info(": Requesting Tuner module\n");
340                 request_module("tuner");
341         }
342
343         cx231xx_config_tuner(dev);
344
345         /* TBD  IR will be added later */
346         cx231xx_ir_init(dev);
347 #endif
348 }
349
350 /*
351  * cx231xx_config()
352  * inits registers with sane defaults
353  */
354 int cx231xx_config(struct cx231xx *dev)
355 {
356         /* TBD need to add cx231xx specific code */
357         dev->mute = 1;          /* maybe not the right place... */
358         dev->volume = 0x1f;
359
360         return 0;
361 }
362
363 /*
364  * cx231xx_config_i2c()
365  * configure i2c attached devices
366  */
367 void cx231xx_config_i2c(struct cx231xx *dev)
368 {
369         struct v4l2_routing route;
370
371         route.input = INPUT(dev->video_input)->vmux;
372         route.output = 0;
373
374         cx231xx_i2c_call_clients(&dev->i2c_bus[0], VIDIOC_STREAMON, NULL);
375 }
376
377 /*
378  * cx231xx_realease_resources()
379  * unregisters the v4l2,i2c and usb devices
380  * called when the device gets disconected or at module unload
381 */
382 void cx231xx_release_resources(struct cx231xx *dev)
383 {
384
385 #if 0           /* TBD IR related  */
386         if (dev->ir)
387                 cx231xx_ir_fini(dev);
388 #endif
389
390         cx231xx_release_analog_resources(dev);
391
392         cx231xx_remove_from_devlist(dev);
393
394         cx231xx_dev_uninit(dev);
395
396         usb_put_dev(dev->udev);
397
398         /* Mark device as unused */
399         cx231xx_devused &= ~(1 << dev->devno);
400 }
401
402 /*
403  * cx231xx_init_dev()
404  * allocates and inits the device structs, registers i2c bus and v4l device
405  */
406 static int cx231xx_init_dev(struct cx231xx **devhandle, struct usb_device *udev,
407                             int minor)
408 {
409         struct cx231xx *dev = *devhandle;
410         int retval = -ENOMEM;
411         int errCode;
412         unsigned int maxh, maxw;
413
414         dev->udev = udev;
415         mutex_init(&dev->lock);
416         mutex_init(&dev->ctrl_urb_lock);
417         mutex_init(&dev->gpio_i2c_lock);
418
419         spin_lock_init(&dev->video_mode.slock);
420         spin_lock_init(&dev->vbi_mode.slock);
421         spin_lock_init(&dev->sliced_cc_mode.slock);
422
423         init_waitqueue_head(&dev->open);
424         init_waitqueue_head(&dev->wait_frame);
425         init_waitqueue_head(&dev->wait_stream);
426
427         dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
428         dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
429         dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
430         dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
431         dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
432
433         /* Query cx231xx to find what pcb config it is related to */
434         initialize_cx231xx(dev);
435
436         /* Cx231xx pre card setup */
437         cx231xx_pre_card_setup(dev);
438
439         errCode = cx231xx_config(dev);
440         if (errCode) {
441                 cx231xx_errdev("error configuring device\n");
442                 return -ENOMEM;
443         }
444
445         /* set default norm */
446         dev->norm = dev->board.norm;
447
448         /* register i2c bus */
449         errCode = cx231xx_dev_init(dev);
450         if (errCode < 0) {
451                 cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
452                                __func__, errCode);
453                 return errCode;
454         }
455
456         /* Do board specific init */
457         cx231xx_card_setup(dev);
458
459         /* configure the device */
460         cx231xx_config_i2c(dev);
461
462         maxw = norm_maxw(dev);
463         maxh = norm_maxh(dev);
464
465         /* set default image size */
466         dev->width = maxw;
467         dev->height = maxh;
468         dev->interlaced = 0;
469         dev->hscale = 0;
470         dev->vscale = 0;
471         dev->video_input = 0;
472
473         errCode = cx231xx_config(dev);
474         if (errCode < 0) {
475                 cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
476                                __func__, errCode);
477                 return errCode;
478         }
479
480         /* init video dma queues */
481         INIT_LIST_HEAD(&dev->video_mode.vidq.active);
482         INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
483
484         /* init vbi dma queues */
485         INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
486         INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
487
488         /* Reset other chips required if they are tied up with GPIO pins */
489
490         cx231xx_add_into_devlist(dev);
491
492         retval = cx231xx_register_analog_devices(dev);
493         if (retval < 0) {
494                 cx231xx_release_resources(dev);
495                 goto fail_reg_devices;
496         }
497
498         cx231xx_init_extension(dev);
499
500         return 0;
501
502 fail_reg_devices:
503         mutex_unlock(&dev->lock);
504         return retval;
505 }
506
507 #if defined(CONFIG_MODULES) && defined(MODULE)
508 static void request_module_async(struct work_struct *work)
509 {
510         struct cx231xx *dev = container_of(work,
511                                            struct cx231xx, request_module_wk);
512
513         if (dev->has_alsa_audio)
514                 request_module("cx231xx-alsa");
515
516         if (dev->board.has_dvb)
517                 request_module("cx231xx-dvb");
518
519 }
520
521 static void request_modules(struct cx231xx *dev)
522 {
523         INIT_WORK(&dev->request_module_wk, request_module_async);
524         schedule_work(&dev->request_module_wk);
525 }
526 #else
527 #define request_modules(dev)
528 #endif /* CONFIG_MODULES */
529
530 /*
531  * cx231xx_usb_probe()
532  * checks for supported devices
533  */
534 static int cx231xx_usb_probe(struct usb_interface *interface,
535                              const struct usb_device_id *id)
536 {
537         struct usb_device *udev;
538         struct usb_interface *uif;
539         struct cx231xx *dev = NULL;
540         int retval = -ENODEV;
541         int nr = 0, ifnum;
542         int i, isoc_pipe = 0;
543         char *speed;
544         char descr[255] = "";
545         struct usb_interface *lif = NULL;
546         int skip_interface = 0;
547         struct usb_interface_assoc_descriptor *assoc_desc;
548
549         udev = usb_get_dev(interface_to_usbdev(interface));
550         ifnum = interface->altsetting[0].desc.bInterfaceNumber;
551
552         cx231xx_info(": Interface Number %d\n", ifnum);
553
554         /* Interface number 0 - IR interface */
555         if (ifnum == 0) {
556                 /* Check to see next free device and mark as used */
557                 nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
558                 cx231xx_devused |= 1 << nr;
559
560                 if (nr >= CX231XX_MAXBOARDS) {
561                         cx231xx_info(": Supports only %i cx231xx boards.\n",
562                                      CX231XX_MAXBOARDS);
563                         cx231xx_devused &= ~(1 << nr);
564                         return -ENOMEM;
565                 }
566
567                 /* allocate memory for our device state and initialize it */
568                 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
569                 if (dev == NULL) {
570                         cx231xx_err(DRIVER_NAME ": out of memory!\n");
571                         cx231xx_devused &= ~(1 << nr);
572                         return -ENOMEM;
573                 }
574
575                 snprintf(dev->name, 29, "cx231xx #%d", nr);
576                 dev->devno = nr;
577                 dev->model = id->driver_info;
578                 dev->video_mode.alt = -1;
579                 dev->interface_count++;
580
581                 /* reset gpio dir and value */
582                 dev->gpio_dir = 0;
583                 dev->gpio_val = 0;
584                 dev->xc_fw_load_done = 0;
585                 dev->has_alsa_audio = 1;
586                 dev->power_mode = -1;
587
588                 /* 0 - vbi ; 1 -sliced cc mode */
589                 dev->vbi_or_sliced_cc_mode = 0;
590
591                 /* get maximum no.of IAD interfaces */
592                 assoc_desc = udev->actconfig->intf_assoc[0];
593                 dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
594                 cx231xx_info("Found IAD interface count %d\n",
595                              dev->max_iad_interface_count);
596
597                 /* init CIR module TBD */
598
599                 /* store the current interface */
600                 lif = interface;
601
602         } else if (ifnum == 1) {
603
604                 /* Get dev structure first */
605                 dev = usb_get_intfdata(udev->actconfig->interface[0]);
606                 if (dev == NULL) {
607                         cx231xx_err(DRIVER_NAME ": out of first interface!\n");
608                         return -ENODEV;
609                 }
610
611                 /* store the interface 0 back */
612                 lif = udev->actconfig->interface[0];
613
614                 /* increment interface count */
615                 dev->interface_count++;
616
617                 /* get device number */
618                 nr = dev->devno;
619
620                 assoc_desc = udev->actconfig->intf_assoc[0];
621                 if (assoc_desc->bFirstInterface == ifnum) {
622                         cx231xx_info
623                         ("Found IAD interface match: AV Desc Start!! \n");
624                 } else {
625                         cx231xx_err(" Not found matching interface\n");
626                         return -ENODEV;
627                 }
628
629         } else if (ifnum >= 2) {
630                 /* Get dev structure first */
631                 dev = usb_get_intfdata(udev->actconfig->interface[0]);
632                 if (dev == NULL) {
633                         cx231xx_err(DRIVER_NAME ": out of first interface!\n");
634                         return -ENODEV;
635                 }
636
637                 /* store the interface 0 back */
638                 lif = udev->actconfig->interface[0];
639
640                 /* increment interface count */
641                 dev->interface_count++;
642
643                 /* get device number */
644                 nr = dev->devno;
645
646                 /* set skip interface */
647                 if ((dev->interface_count - 1) != dev->max_iad_interface_count)
648                         skip_interface = 1;     /* set skipping */
649                 else {
650                         cx231xx_info
651                         ("Found IAD interface no. match with AV Device no.!\n");
652                 }
653         }
654
655         switch (udev->speed) {
656         case USB_SPEED_LOW:
657                 speed = "1.5";
658                 break;
659         case USB_SPEED_UNKNOWN:
660         case USB_SPEED_FULL:
661                 speed = "12";
662                 break;
663         case USB_SPEED_HIGH:
664                 speed = "480";
665                 break;
666         default:
667                 speed = "unknown";
668         }
669
670         if (udev->manufacturer)
671                 strlcpy(descr, udev->manufacturer, sizeof(descr));
672
673         if (udev->product) {
674                 if (*descr)
675                         strlcat(descr, " ", sizeof(descr));
676                 strlcat(descr, udev->product, sizeof(descr));
677         }
678         if (*descr)
679                 strlcat(descr, " ", sizeof(descr));
680
681         cx231xx_info("New device %s@ %s Mbps "
682                      "(%04x:%04x, interface %d, class %d)\n",
683                      descr,
684                      speed,
685                      le16_to_cpu(udev->descriptor.idVendor),
686                      le16_to_cpu(udev->descriptor.idProduct),
687                      ifnum, interface->altsetting->desc.bInterfaceNumber);
688
689         /* AV device initialization */
690         if ((dev->interface_count - 1) == dev->max_iad_interface_count) {
691                 cx231xx_info(" Calling init_dev\n");
692                 /* allocate device struct */
693                 retval = cx231xx_init_dev(&dev, udev, nr);
694                 if (retval) {
695                         cx231xx_devused &= ~(1 << dev->devno);
696                         kfree(dev);
697
698                         return retval;
699                 }
700
701                 /* compute alternate max packet sizes for video */
702                 uif =
703                     udev->actconfig->interface[dev->current_pcb_config.
704                                                hs_config_info[0].interface_info.
705                                                video_index + 1];
706
707                 dev->video_mode.end_point_addr =
708                     le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
709                                 bEndpointAddress);
710
711                 dev->video_mode.num_alt = uif->num_altsetting;
712                 cx231xx_info(": EndPoint Addr 0x%x, Alternate settings: %i\n",
713                              dev->video_mode.end_point_addr,
714                              dev->video_mode.num_alt);
715                 dev->video_mode.alt_max_pkt_size =
716                     kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
717
718                 if (dev->video_mode.alt_max_pkt_size == NULL) {
719                         cx231xx_errdev("out of memory!\n");
720                         cx231xx_devused &= ~(1 << nr);
721                         kfree(dev);
722                         return -ENOMEM;
723                 }
724
725                 for (i = 0; i < dev->video_mode.num_alt; i++) {
726                         u16 tmp =
727                             le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
728                                         desc.wMaxPacketSize);
729                         dev->video_mode.alt_max_pkt_size[i] =
730                             (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
731                         cx231xx_info("Alternate setting %i, max size= %i\n", i,
732                                      dev->video_mode.alt_max_pkt_size[i]);
733                 }
734
735                 /* compute alternate max packet sizes for vbi */
736                 uif =
737                     udev->actconfig->interface[dev->current_pcb_config.
738                                                hs_config_info[0].interface_info.
739                                                vanc_index + 1];
740
741                 dev->vbi_mode.end_point_addr =
742                     le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
743                                 bEndpointAddress);
744
745                 dev->vbi_mode.num_alt = uif->num_altsetting;
746                 cx231xx_info(": EndPoint Addr 0x%x, Alternate settings: %i\n",
747                              dev->vbi_mode.end_point_addr,
748                              dev->vbi_mode.num_alt);
749                 dev->vbi_mode.alt_max_pkt_size =
750                     kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
751
752                 if (dev->vbi_mode.alt_max_pkt_size == NULL) {
753                         cx231xx_errdev("out of memory!\n");
754                         cx231xx_devused &= ~(1 << nr);
755                         kfree(dev);
756                         return -ENOMEM;
757                 }
758
759                 for (i = 0; i < dev->vbi_mode.num_alt; i++) {
760                         u16 tmp =
761                             le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
762                                         desc.wMaxPacketSize);
763                         dev->vbi_mode.alt_max_pkt_size[i] =
764                             (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
765                         cx231xx_info("Alternate setting %i, max size= %i\n", i,
766                                      dev->vbi_mode.alt_max_pkt_size[i]);
767                 }
768
769                 /* compute alternate max packet sizes for sliced CC */
770                 uif =
771                     udev->actconfig->interface[dev->current_pcb_config.
772                                                hs_config_info[0].interface_info.
773                                                hanc_index + 1];
774
775                 dev->sliced_cc_mode.end_point_addr =
776                     le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
777                                 bEndpointAddress);
778
779                 dev->sliced_cc_mode.num_alt = uif->num_altsetting;
780                 cx231xx_info(": EndPoint Addr 0x%x, Alternate settings: %i\n",
781                              dev->sliced_cc_mode.end_point_addr,
782                              dev->sliced_cc_mode.num_alt);
783                 dev->sliced_cc_mode.alt_max_pkt_size =
784                     kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
785
786                 if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
787                         cx231xx_errdev("out of memory!\n");
788                         cx231xx_devused &= ~(1 << nr);
789                         kfree(dev);
790                         return -ENOMEM;
791                 }
792
793                 for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
794                         u16 tmp =
795                             le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
796                                         desc.wMaxPacketSize);
797                         dev->sliced_cc_mode.alt_max_pkt_size[i] =
798                             (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
799                         cx231xx_info("Alternate setting %i, max size= %i\n", i,
800                                      dev->sliced_cc_mode.alt_max_pkt_size[i]);
801                 }
802
803                 if (dev->current_pcb_config.ts1_source != 0xff) {
804
805                         /* compute alternate max packet sizes for TS1 */
806                         uif =
807                             udev->actconfig->interface[dev->current_pcb_config.
808                                                        hs_config_info[0].
809                                                        interface_info.
810                                                        ts1_index + 1];
811
812                         dev->ts1_mode.end_point_addr =
813                             le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
814                                         desc.bEndpointAddress);
815
816                         dev->ts1_mode.num_alt = uif->num_altsetting;
817                         cx231xx_info
818                             (": EndPoint Addr 0x%x, Alternate settings: %i\n",
819                              dev->ts1_mode.end_point_addr,
820                              dev->ts1_mode.num_alt);
821                         dev->ts1_mode.alt_max_pkt_size =
822                             kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
823
824                         if (dev->ts1_mode.alt_max_pkt_size == NULL) {
825                                 cx231xx_errdev("out of memory!\n");
826                                 cx231xx_devused &= ~(1 << nr);
827                                 kfree(dev);
828                                 return -ENOMEM;
829                         }
830
831                         for (i = 0; i < dev->ts1_mode.num_alt; i++) {
832                                 u16 tmp =
833                                     le16_to_cpu(uif->altsetting[i].
834                                                 endpoint[isoc_pipe].desc.
835                                                 wMaxPacketSize);
836                                 dev->ts1_mode.alt_max_pkt_size[i] =
837                                     (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) +
838                                                       1);
839                                 cx231xx_info
840                                     ("Alternate setting %i, max size= %i\n", i,
841                                      dev->ts1_mode.alt_max_pkt_size[i]);
842                         }
843                 }
844
845         }
846
847         /* save our data pointer in this interface device */
848         usb_set_intfdata(lif, dev);
849
850         /* load other modules required */
851         if ((dev->interface_count - 1) == dev->max_iad_interface_count) {
852                 cx231xx_info("Calling request modules\n");
853                 request_modules(dev);
854         }
855
856         if (skip_interface) {
857                 cx231xx_info("Skipping the interface\n");
858                 return -ENODEV;
859         }
860
861         return 0;
862 }
863
864 /*
865  * cx231xx_usb_disconnect()
866  * called when the device gets diconencted
867  * video device will be unregistered on v4l2_close in case it is still open
868  */
869 static void cx231xx_usb_disconnect(struct usb_interface *interface)
870 {
871         struct cx231xx *dev;
872
873         dev = usb_get_intfdata(interface);
874         usb_set_intfdata(interface, NULL);
875
876         if (!dev)
877                 return;
878
879         /* wait until all current v4l2 io is finished then deallocate
880            resources */
881         mutex_lock(&dev->lock);
882
883         wake_up_interruptible_all(&dev->open);
884
885         if (dev->users) {
886                 cx231xx_warn
887                     ("device /dev/video%d is open! Deregistration and memory "
888                      "deallocation are deferred on close.\n", dev->vdev->num);
889
890                 dev->state |= DEV_MISCONFIGURED;
891                 cx231xx_uninit_isoc(dev);
892                 dev->state |= DEV_DISCONNECTED;
893                 wake_up_interruptible(&dev->wait_frame);
894                 wake_up_interruptible(&dev->wait_stream);
895         } else {
896                 dev->state |= DEV_DISCONNECTED;
897                 cx231xx_release_resources(dev);
898         }
899
900         cx231xx_close_extension(dev);
901
902         mutex_unlock(&dev->lock);
903
904         if (!dev->users) {
905                 kfree(dev->video_mode.alt_max_pkt_size);
906                 kfree(dev->vbi_mode.alt_max_pkt_size);
907                 kfree(dev->sliced_cc_mode.alt_max_pkt_size);
908                 kfree(dev->ts1_mode.alt_max_pkt_size);
909                 kfree(dev);
910         }
911 }
912
913 static struct usb_driver cx231xx_usb_driver = {
914         .name = "cx231xx",
915         .probe = cx231xx_usb_probe,
916         .disconnect = cx231xx_usb_disconnect,
917         .id_table = cx231xx_id_table,
918 };
919
920 static int __init cx231xx_module_init(void)
921 {
922         int result;
923
924         printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
925
926         /* register this driver with the USB subsystem */
927         result = usb_register(&cx231xx_usb_driver);
928         if (result)
929                 cx231xx_err(DRIVER_NAME
930                             " usb_register failed. Error number %d.\n", result);
931
932         return result;
933 }
934
935 static void __exit cx231xx_module_exit(void)
936 {
937         /* deregister this driver with the USB subsystem */
938         usb_deregister(&cx231xx_usb_driver);
939 }
940
941 module_init(cx231xx_module_init);
942 module_exit(cx231xx_module_exit);