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