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