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