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