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