Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / media / video / cx231xx / cx231xx-cards.c
blob6bdc0ef18119716dadc5facc2347f22fda5c6192
1 /*
2 cx231xx-cards.c - driver for Conexant Cx23100/101/102
3 USB video capture devices
5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6 Based on em28xx driver
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.
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.
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.
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/i2c.h>
28 #include <linux/usb.h>
29 #include <media/tuner.h>
30 #include <media/tveeprom.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-chip-ident.h>
34 #include <media/cx25840.h>
35 #include "xc5000.h"
37 #include "cx231xx.h"
39 static int tuner = -1;
40 module_param(tuner, int, 0444);
41 MODULE_PARM_DESC(tuner, "tuner type");
43 static unsigned int disable_ir;
44 module_param(disable_ir, int, 0444);
45 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
47 /* Bitmask marking allocated devices from 0 to CX231XX_MAXBOARDS */
48 static unsigned long cx231xx_devused;
51 * Reset sequences for analog/digital modes
54 static struct cx231xx_reg_seq RDE250_XCV_TUNER[] = {
55 {0x03, 0x01, 10},
56 {0x03, 0x00, 30},
57 {0x03, 0x01, 10},
58 {-1, -1, -1},
62 * Board definitions
64 struct cx231xx_board cx231xx_boards[] = {
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 = NULL,
73 }, {
74 .type = CX231XX_VMUX_COMPOSITE1,
75 .vmux = CX231XX_VIN_2_1,
76 .amux = CX231XX_AMUX_LINE_IN,
77 .gpio = NULL,
78 }, {
79 .type = CX231XX_VMUX_SVIDEO,
80 .vmux = CX231XX_VIN_1_1 |
81 (CX231XX_VIN_1_2 << 8) |
82 CX25840_SVIDEO_ON,
83 .amux = CX231XX_AMUX_LINE_IN,
84 .gpio = NULL,
88 [CX231XX_BOARD_CNXT_RDE_250] = {
89 .name = "Conexant Hybrid TV - RDE250",
90 .tuner_type = TUNER_XC5000,
91 .tuner_addr = 0x61,
92 .tuner_gpio = RDE250_XCV_TUNER,
93 .tuner_sif_gpio = 0x05,
94 .tuner_scl_gpio = 0x1a,
95 .tuner_sda_gpio = 0x1b,
96 .decoder = CX231XX_AVDECODER,
97 .demod_xfer_mode = 0,
98 .ctl_pin_status_mask = 0xFFFFFFC4,
99 .agc_analog_digital_select_gpio = 0x0c,
100 .gpio_pin_status_mask = 0x4001000,
101 .tuner_i2c_master = 1,
102 .demod_i2c_master = 2,
103 .has_dvb = 1,
104 .demod_addr = 0x02,
105 .norm = V4L2_STD_PAL,
107 .input = {{
108 .type = CX231XX_VMUX_TELEVISION,
109 .vmux = CX231XX_VIN_3_1,
110 .amux = CX231XX_AMUX_VIDEO,
111 .gpio = NULL,
112 }, {
113 .type = CX231XX_VMUX_COMPOSITE1,
114 .vmux = CX231XX_VIN_2_1,
115 .amux = CX231XX_AMUX_LINE_IN,
116 .gpio = NULL,
117 }, {
118 .type = CX231XX_VMUX_SVIDEO,
119 .vmux = CX231XX_VIN_1_1 |
120 (CX231XX_VIN_1_2 << 8) |
121 CX25840_SVIDEO_ON,
122 .amux = CX231XX_AMUX_LINE_IN,
123 .gpio = NULL,
128 [CX231XX_BOARD_CNXT_RDU_250] = {
129 .name = "Conexant Hybrid TV - RDU250",
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,
147 .input = {{
148 .type = CX231XX_VMUX_TELEVISION,
149 .vmux = CX231XX_VIN_3_1,
150 .amux = CX231XX_AMUX_VIDEO,
151 .gpio = NULL,
152 }, {
153 .type = CX231XX_VMUX_COMPOSITE1,
154 .vmux = CX231XX_VIN_2_1,
155 .amux = CX231XX_AMUX_LINE_IN,
156 .gpio = NULL,
157 }, {
158 .type = CX231XX_VMUX_SVIDEO,
159 .vmux = CX231XX_VIN_1_1 |
160 (CX231XX_VIN_1_2 << 8) |
161 CX25840_SVIDEO_ON,
162 .amux = CX231XX_AMUX_LINE_IN,
163 .gpio = NULL,
168 const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);
170 /* table of devices that work with this driver */
171 struct usb_device_id cx231xx_id_table[] = {
172 {USB_DEVICE(0x0572, 0x5A3C),
173 .driver_info = CX231XX_BOARD_UNKNOWN},
174 {USB_DEVICE(0x0572, 0x58A2),
175 .driver_info = CX231XX_BOARD_CNXT_RDE_250},
176 {USB_DEVICE(0x0572, 0x58A1),
177 .driver_info = CX231XX_BOARD_CNXT_RDU_250},
181 MODULE_DEVICE_TABLE(usb, cx231xx_id_table);
183 /* cx231xx_tuner_callback
184 * will be used to reset XC5000 tuner using GPIO pin
187 int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
189 int rc = 0;
190 struct cx231xx *dev = ptr;
192 if (dev->tuner_type == TUNER_XC5000) {
193 if (command == XC5000_TUNER_RESET) {
194 cx231xx_info
195 ("Tuner CB: RESET: cmd %d : tuner type %d \n",
196 command, dev->tuner_type);
197 cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
199 msleep(10);
200 cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
202 msleep(330);
203 cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
205 msleep(10);
208 return rc;
210 EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);
212 static inline void cx231xx_set_model(struct cx231xx *dev)
214 memcpy(&dev->board, &cx231xx_boards[dev->model], sizeof(dev->board));
217 /* Since cx231xx_pre_card_setup() requires a proper dev->model,
218 * this won't work for boards with generic PCI IDs
220 void cx231xx_pre_card_setup(struct cx231xx *dev)
223 cx231xx_set_model(dev);
225 cx231xx_info("Identified as %s (card=%d)\n",
226 dev->board.name, dev->model);
228 /* set the direction for GPIO pins */
229 cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
230 cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
231 cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
233 /* request some modules if any required */
235 /* reset the Tuner */
236 cx231xx_gpio_set(dev, dev->board.tuner_gpio);
238 /* set the mode to Analog mode initially */
239 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
241 /* Unlock device */
242 /* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
246 static void cx231xx_config_tuner(struct cx231xx *dev)
248 struct tuner_setup tun_setup;
249 struct v4l2_frequency f;
251 if (dev->tuner_type == TUNER_ABSENT)
252 return;
254 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
255 tun_setup.type = dev->tuner_type;
256 tun_setup.addr = dev->tuner_addr;
257 tun_setup.tuner_callback = cx231xx_tuner_callback;
259 tuner_call(dev, tuner, s_type_addr, &tun_setup);
261 #if 0
262 if (tun_setup.type == TUNER_XC5000) {
263 static struct xc2028_ctrl ctrl = {
264 .fname = XC5000_DEFAULT_FIRMWARE,
265 .max_len = 64,
266 .demod = 0;
268 struct v4l2_priv_tun_config cfg = {
269 .tuner = dev->tuner_type,
270 .priv = &ctrl,
272 tuner_call(dev, tuner, s_config, &cfg);
274 #endif
275 /* configure tuner */
276 f.tuner = 0;
277 f.type = V4L2_TUNER_ANALOG_TV;
278 f.frequency = 9076; /* just a magic number */
279 dev->ctl_freq = f.frequency;
280 call_all(dev, tuner, s_frequency, &f);
284 /* ----------------------------------------------------------------------- */
285 void cx231xx_register_i2c_ir(struct cx231xx *dev)
287 if (disable_ir)
288 return;
290 /* REVISIT: instantiate IR device */
292 /* detect & configure */
293 switch (dev->model) {
295 case CX231XX_BOARD_CNXT_RDE_250:
296 break;
297 case CX231XX_BOARD_CNXT_RDU_250:
298 break;
299 default:
300 break;
304 void cx231xx_card_setup(struct cx231xx *dev)
307 cx231xx_set_model(dev);
309 dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
310 if (cx231xx_boards[dev->model].tuner_addr)
311 dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
313 /* request some modules */
314 if (dev->board.decoder == CX231XX_AVDECODER) {
315 dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
316 &dev->i2c_bus[0].i2c_adap,
317 "cx25840", "cx25840", 0x88 >> 1, NULL);
318 if (dev->sd_cx25840 == NULL)
319 cx231xx_info("cx25840 subdev registration failure\n");
320 cx25840_call(dev, core, load_fw);
324 if (dev->board.tuner_type != TUNER_ABSENT) {
325 dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
326 &dev->i2c_bus[1].i2c_adap,
327 "tuner", "tuner", 0xc2 >> 1, NULL);
328 if (dev->sd_tuner == NULL)
329 cx231xx_info("tuner subdev registration failure\n");
331 cx231xx_config_tuner(dev);
334 cx231xx_config_tuner(dev);
336 #if 0
337 /* TBD IR will be added later */
338 cx231xx_ir_init(dev);
339 #endif
343 * cx231xx_config()
344 * inits registers with sane defaults
346 int cx231xx_config(struct cx231xx *dev)
348 /* TBD need to add cx231xx specific code */
349 dev->mute = 1; /* maybe not the right place... */
350 dev->volume = 0x1f;
352 return 0;
356 * cx231xx_config_i2c()
357 * configure i2c attached devices
359 void cx231xx_config_i2c(struct cx231xx *dev)
361 /* u32 input = INPUT(dev->video_input)->vmux; */
363 call_all(dev, video, s_stream, 1);
367 * cx231xx_realease_resources()
368 * unregisters the v4l2,i2c and usb devices
369 * called when the device gets disconected or at module unload
371 void cx231xx_release_resources(struct cx231xx *dev)
374 #if 0 /* TBD IR related */
375 if (dev->ir)
376 cx231xx_ir_fini(dev);
377 #endif
379 cx231xx_release_analog_resources(dev);
381 cx231xx_remove_from_devlist(dev);
383 cx231xx_dev_uninit(dev);
385 usb_put_dev(dev->udev);
387 /* Mark device as unused */
388 cx231xx_devused &= ~(1 << dev->devno);
392 * cx231xx_init_dev()
393 * allocates and inits the device structs, registers i2c bus and v4l device
395 static int cx231xx_init_dev(struct cx231xx **devhandle, struct usb_device *udev,
396 int minor)
398 struct cx231xx *dev = *devhandle;
399 int retval = -ENOMEM;
400 int errCode;
401 unsigned int maxh, maxw;
403 dev->udev = udev;
404 mutex_init(&dev->lock);
405 mutex_init(&dev->ctrl_urb_lock);
406 mutex_init(&dev->gpio_i2c_lock);
408 spin_lock_init(&dev->video_mode.slock);
409 spin_lock_init(&dev->vbi_mode.slock);
410 spin_lock_init(&dev->sliced_cc_mode.slock);
412 init_waitqueue_head(&dev->open);
413 init_waitqueue_head(&dev->wait_frame);
414 init_waitqueue_head(&dev->wait_stream);
416 dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
417 dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
418 dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
419 dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
420 dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
422 /* Query cx231xx to find what pcb config it is related to */
423 initialize_cx231xx(dev);
425 /* Cx231xx pre card setup */
426 cx231xx_pre_card_setup(dev);
428 errCode = cx231xx_config(dev);
429 if (errCode) {
430 cx231xx_errdev("error configuring device\n");
431 return -ENOMEM;
434 /* set default norm */
435 dev->norm = dev->board.norm;
437 /* register i2c bus */
438 errCode = cx231xx_dev_init(dev);
439 if (errCode < 0) {
440 cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
441 __func__, errCode);
442 return errCode;
445 /* Do board specific init */
446 cx231xx_card_setup(dev);
448 /* configure the device */
449 cx231xx_config_i2c(dev);
451 maxw = norm_maxw(dev);
452 maxh = norm_maxh(dev);
454 /* set default image size */
455 dev->width = maxw;
456 dev->height = maxh;
457 dev->interlaced = 0;
458 dev->hscale = 0;
459 dev->vscale = 0;
460 dev->video_input = 0;
462 errCode = cx231xx_config(dev);
463 if (errCode < 0) {
464 cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
465 __func__, errCode);
466 return errCode;
469 /* init video dma queues */
470 INIT_LIST_HEAD(&dev->video_mode.vidq.active);
471 INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
473 /* init vbi dma queues */
474 INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
475 INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
477 /* Reset other chips required if they are tied up with GPIO pins */
479 cx231xx_add_into_devlist(dev);
481 retval = cx231xx_register_analog_devices(dev);
482 if (retval < 0) {
483 cx231xx_release_resources(dev);
484 goto fail_reg_devices;
487 cx231xx_init_extension(dev);
489 return 0;
491 fail_reg_devices:
492 mutex_unlock(&dev->lock);
493 return retval;
496 #if defined(CONFIG_MODULES) && defined(MODULE)
497 static void request_module_async(struct work_struct *work)
499 struct cx231xx *dev = container_of(work,
500 struct cx231xx, request_module_wk);
502 if (dev->has_alsa_audio)
503 request_module("cx231xx-alsa");
505 if (dev->board.has_dvb)
506 request_module("cx231xx-dvb");
510 static void request_modules(struct cx231xx *dev)
512 INIT_WORK(&dev->request_module_wk, request_module_async);
513 schedule_work(&dev->request_module_wk);
515 #else
516 #define request_modules(dev)
517 #endif /* CONFIG_MODULES */
520 * cx231xx_usb_probe()
521 * checks for supported devices
523 static int cx231xx_usb_probe(struct usb_interface *interface,
524 const struct usb_device_id *id)
526 struct usb_device *udev;
527 struct usb_interface *uif;
528 struct cx231xx *dev = NULL;
529 int retval = -ENODEV;
530 int nr = 0, ifnum;
531 int i, isoc_pipe = 0;
532 char *speed;
533 char descr[255] = "";
534 struct usb_interface *lif = NULL;
535 int skip_interface = 0;
536 struct usb_interface_assoc_descriptor *assoc_desc;
538 udev = usb_get_dev(interface_to_usbdev(interface));
539 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
541 if (!ifnum) {
543 * Interface number 0 - IR interface
545 /* Check to see next free device and mark as used */
546 nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
547 cx231xx_devused |= 1 << nr;
549 if (nr >= CX231XX_MAXBOARDS) {
550 cx231xx_err(DRIVER_NAME ": Supports only %i cx231xx boards.\n",
551 CX231XX_MAXBOARDS);
552 cx231xx_devused &= ~(1 << nr);
553 return -ENOMEM;
556 /* allocate memory for our device state and initialize it */
557 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
558 if (dev == NULL) {
559 cx231xx_err(DRIVER_NAME ": out of memory!\n");
560 cx231xx_devused &= ~(1 << nr);
561 return -ENOMEM;
564 snprintf(dev->name, 29, "cx231xx #%d", nr);
565 dev->devno = nr;
566 dev->model = id->driver_info;
567 dev->video_mode.alt = -1;
568 dev->interface_count++;
570 /* reset gpio dir and value */
571 dev->gpio_dir = 0;
572 dev->gpio_val = 0;
573 dev->xc_fw_load_done = 0;
574 dev->has_alsa_audio = 1;
575 dev->power_mode = -1;
577 /* 0 - vbi ; 1 -sliced cc mode */
578 dev->vbi_or_sliced_cc_mode = 0;
580 /* get maximum no.of IAD interfaces */
581 assoc_desc = udev->actconfig->intf_assoc[0];
582 dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
584 /* init CIR module TBD */
586 /* store the current interface */
587 lif = interface;
589 switch (udev->speed) {
590 case USB_SPEED_LOW:
591 speed = "1.5";
592 break;
593 case USB_SPEED_UNKNOWN:
594 case USB_SPEED_FULL:
595 speed = "12";
596 break;
597 case USB_SPEED_HIGH:
598 speed = "480";
599 break;
600 default:
601 speed = "unknown";
604 if (udev->manufacturer)
605 strlcpy(descr, udev->manufacturer, sizeof(descr));
607 if (udev->product) {
608 if (*descr)
609 strlcat(descr, " ", sizeof(descr));
610 strlcat(descr, udev->product, sizeof(descr));
612 if (*descr)
613 strlcat(descr, " ", sizeof(descr));
615 cx231xx_info("New device %s@ %s Mbps "
616 "(%04x:%04x) with %d interfaces\n",
617 descr,
618 speed,
619 le16_to_cpu(udev->descriptor.idVendor),
620 le16_to_cpu(udev->descriptor.idProduct),
621 dev->max_iad_interface_count);
622 } else {
623 /* Get dev structure first */
624 dev = usb_get_intfdata(udev->actconfig->interface[0]);
625 if (dev == NULL) {
626 cx231xx_err(DRIVER_NAME ": out of first interface!\n");
627 return -ENODEV;
630 /* store the interface 0 back */
631 lif = udev->actconfig->interface[0];
633 /* increment interface count */
634 dev->interface_count++;
636 /* get device number */
637 nr = dev->devno;
640 * set skip interface, for all interfaces but
641 * interface 1 and the last one
643 if ((ifnum != 1) && ((dev->interface_count - 1)
644 != dev->max_iad_interface_count))
645 skip_interface = 1;
647 if (ifnum == 1) {
648 assoc_desc = udev->actconfig->intf_assoc[0];
649 if (assoc_desc->bFirstInterface != ifnum) {
650 cx231xx_err(DRIVER_NAME ": Not found "
651 "matching IAD interface\n");
652 return -ENODEV;
657 if (skip_interface)
658 return -ENODEV;
660 cx231xx_info("registering interface %d\n", ifnum);
662 /* save our data pointer in this interface device */
663 usb_set_intfdata(lif, dev);
665 if ((dev->interface_count - 1) != dev->max_iad_interface_count)
666 return 0;
669 * AV device initialization - only done at the last interface
672 /* Create v4l2 device */
673 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
674 if (retval) {
675 cx231xx_errdev("v4l2_device_register failed\n");
676 cx231xx_devused &= ~(1 << nr);
677 kfree(dev);
678 return -EIO;
681 /* allocate device struct */
682 retval = cx231xx_init_dev(&dev, udev, nr);
683 if (retval) {
684 cx231xx_devused &= ~(1 << dev->devno);
685 v4l2_device_unregister(&dev->v4l2_dev);
686 kfree(dev);
687 return retval;
690 /* compute alternate max packet sizes for video */
691 uif = udev->actconfig->interface[dev->current_pcb_config.
692 hs_config_info[0].interface_info.video_index + 1];
694 dev->video_mode.end_point_addr = le16_to_cpu(uif->altsetting[0].
695 endpoint[isoc_pipe].desc.bEndpointAddress);
697 dev->video_mode.num_alt = uif->num_altsetting;
698 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
699 dev->video_mode.end_point_addr,
700 dev->video_mode.num_alt);
701 dev->video_mode.alt_max_pkt_size =
702 kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
704 if (dev->video_mode.alt_max_pkt_size == NULL) {
705 cx231xx_errdev("out of memory!\n");
706 cx231xx_devused &= ~(1 << nr);
707 v4l2_device_unregister(&dev->v4l2_dev);
708 kfree(dev);
709 return -ENOMEM;
712 for (i = 0; i < dev->video_mode.num_alt; i++) {
713 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
714 desc.wMaxPacketSize);
715 dev->video_mode.alt_max_pkt_size[i] =
716 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
717 cx231xx_info("Alternate setting %i, max size= %i\n", i,
718 dev->video_mode.alt_max_pkt_size[i]);
721 /* compute alternate max packet sizes for vbi */
722 uif = udev->actconfig->interface[dev->current_pcb_config.
723 hs_config_info[0].interface_info.
724 vanc_index + 1];
726 dev->vbi_mode.end_point_addr =
727 le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
728 bEndpointAddress);
730 dev->vbi_mode.num_alt = uif->num_altsetting;
731 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
732 dev->vbi_mode.end_point_addr,
733 dev->vbi_mode.num_alt);
734 dev->vbi_mode.alt_max_pkt_size =
735 kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
737 if (dev->vbi_mode.alt_max_pkt_size == NULL) {
738 cx231xx_errdev("out of memory!\n");
739 cx231xx_devused &= ~(1 << nr);
740 v4l2_device_unregister(&dev->v4l2_dev);
741 kfree(dev);
742 return -ENOMEM;
745 for (i = 0; i < dev->vbi_mode.num_alt; i++) {
746 u16 tmp =
747 le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
748 desc.wMaxPacketSize);
749 dev->vbi_mode.alt_max_pkt_size[i] =
750 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
751 cx231xx_info("Alternate setting %i, max size= %i\n", i,
752 dev->vbi_mode.alt_max_pkt_size[i]);
755 /* compute alternate max packet sizes for sliced CC */
756 uif = udev->actconfig->interface[dev->current_pcb_config.
757 hs_config_info[0].interface_info.
758 hanc_index + 1];
760 dev->sliced_cc_mode.end_point_addr =
761 le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
762 bEndpointAddress);
764 dev->sliced_cc_mode.num_alt = uif->num_altsetting;
765 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
766 dev->sliced_cc_mode.end_point_addr,
767 dev->sliced_cc_mode.num_alt);
768 dev->sliced_cc_mode.alt_max_pkt_size =
769 kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
771 if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
772 cx231xx_errdev("out of memory!\n");
773 cx231xx_devused &= ~(1 << nr);
774 v4l2_device_unregister(&dev->v4l2_dev);
775 kfree(dev);
776 return -ENOMEM;
779 for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
780 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
781 desc.wMaxPacketSize);
782 dev->sliced_cc_mode.alt_max_pkt_size[i] =
783 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
784 cx231xx_info("Alternate setting %i, max size= %i\n", i,
785 dev->sliced_cc_mode.alt_max_pkt_size[i]);
788 if (dev->current_pcb_config.ts1_source != 0xff) {
789 /* compute alternate max packet sizes for TS1 */
790 uif = udev->actconfig->interface[dev->current_pcb_config.
791 hs_config_info[0].
792 interface_info.
793 ts1_index + 1];
795 dev->ts1_mode.end_point_addr =
796 le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
797 desc.bEndpointAddress);
799 dev->ts1_mode.num_alt = uif->num_altsetting;
800 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
801 dev->ts1_mode.end_point_addr,
802 dev->ts1_mode.num_alt);
803 dev->ts1_mode.alt_max_pkt_size =
804 kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
806 if (dev->ts1_mode.alt_max_pkt_size == NULL) {
807 cx231xx_errdev("out of memory!\n");
808 cx231xx_devused &= ~(1 << nr);
809 v4l2_device_unregister(&dev->v4l2_dev);
810 kfree(dev);
811 return -ENOMEM;
814 for (i = 0; i < dev->ts1_mode.num_alt; i++) {
815 u16 tmp = le16_to_cpu(uif->altsetting[i].
816 endpoint[isoc_pipe].desc.
817 wMaxPacketSize);
818 dev->ts1_mode.alt_max_pkt_size[i] =
819 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
820 cx231xx_info("Alternate setting %i, max size= %i\n", i,
821 dev->ts1_mode.alt_max_pkt_size[i]);
825 /* load other modules required */
826 request_modules(dev);
828 return 0;
832 * cx231xx_usb_disconnect()
833 * called when the device gets diconencted
834 * video device will be unregistered on v4l2_close in case it is still open
836 static void cx231xx_usb_disconnect(struct usb_interface *interface)
838 struct cx231xx *dev;
840 dev = usb_get_intfdata(interface);
841 usb_set_intfdata(interface, NULL);
843 if (!dev)
844 return;
846 if (!dev->udev)
847 return;
849 /* delete v4l2 device */
850 v4l2_device_unregister(&dev->v4l2_dev);
852 /* wait until all current v4l2 io is finished then deallocate
853 resources */
854 mutex_lock(&dev->lock);
856 wake_up_interruptible_all(&dev->open);
858 if (dev->users) {
859 cx231xx_warn
860 ("device %s is open! Deregistration and memory "
861 "deallocation are deferred on close.\n",
862 video_device_node_name(dev->vdev));
864 dev->state |= DEV_MISCONFIGURED;
865 cx231xx_uninit_isoc(dev);
866 dev->state |= DEV_DISCONNECTED;
867 wake_up_interruptible(&dev->wait_frame);
868 wake_up_interruptible(&dev->wait_stream);
869 } else {
870 dev->state |= DEV_DISCONNECTED;
871 cx231xx_release_resources(dev);
874 cx231xx_close_extension(dev);
876 mutex_unlock(&dev->lock);
878 if (!dev->users) {
879 kfree(dev->video_mode.alt_max_pkt_size);
880 kfree(dev->vbi_mode.alt_max_pkt_size);
881 kfree(dev->sliced_cc_mode.alt_max_pkt_size);
882 kfree(dev->ts1_mode.alt_max_pkt_size);
883 kfree(dev);
887 static struct usb_driver cx231xx_usb_driver = {
888 .name = "cx231xx",
889 .probe = cx231xx_usb_probe,
890 .disconnect = cx231xx_usb_disconnect,
891 .id_table = cx231xx_id_table,
894 static int __init cx231xx_module_init(void)
896 int result;
898 printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
900 /* register this driver with the USB subsystem */
901 result = usb_register(&cx231xx_usb_driver);
902 if (result)
903 cx231xx_err(DRIVER_NAME
904 " usb_register failed. Error number %d.\n", result);
906 return result;
909 static void __exit cx231xx_module_exit(void)
911 /* deregister this driver with the USB subsystem */
912 usb_deregister(&cx231xx_usb_driver);
915 module_init(cx231xx_module_init);
916 module_exit(cx231xx_module_exit);