• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/media/video/cx231xx/
1/*
2   cx231xx-core.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/list.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/usb.h>
28#include <linux/vmalloc.h>
29#include <media/v4l2-common.h>
30
31#include "cx231xx.h"
32#include "cx231xx-reg.h"
33
34/* #define ENABLE_DEBUG_ISOC_FRAMES */
35
36static unsigned int core_debug;
37module_param(core_debug, int, 0644);
38MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
39
40#define cx231xx_coredbg(fmt, arg...) do {\
41	if (core_debug) \
42		printk(KERN_INFO "%s %s :"fmt, \
43			 dev->name, __func__ , ##arg); } while (0)
44
45static unsigned int reg_debug;
46module_param(reg_debug, int, 0644);
47MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
48
49#define cx231xx_regdbg(fmt, arg...) do {\
50	if (reg_debug) \
51		printk(KERN_INFO "%s %s :"fmt, \
52			 dev->name, __func__ , ##arg); } while (0)
53
54static int alt = CX231XX_PINOUT;
55module_param(alt, int, 0644);
56MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
57
58#define cx231xx_isocdbg(fmt, arg...) do {\
59	if (core_debug) \
60		printk(KERN_INFO "%s %s :"fmt, \
61			 dev->name, __func__ , ##arg); } while (0)
62
63/*****************************************************************
64*             Device control list functions     				 *
65******************************************************************/
66
67static LIST_HEAD(cx231xx_devlist);
68static DEFINE_MUTEX(cx231xx_devlist_mutex);
69
70/*
71 * cx231xx_realease_resources()
72 * unregisters the v4l2,i2c and usb devices
73 * called when the device gets disconected or at module unload
74*/
75void cx231xx_remove_from_devlist(struct cx231xx *dev)
76{
77	mutex_lock(&cx231xx_devlist_mutex);
78	list_del(&dev->devlist);
79	mutex_unlock(&cx231xx_devlist_mutex);
80};
81
82void cx231xx_add_into_devlist(struct cx231xx *dev)
83{
84	mutex_lock(&cx231xx_devlist_mutex);
85	list_add_tail(&dev->devlist, &cx231xx_devlist);
86	mutex_unlock(&cx231xx_devlist_mutex);
87};
88
89static LIST_HEAD(cx231xx_extension_devlist);
90static DEFINE_MUTEX(cx231xx_extension_devlist_lock);
91
92int cx231xx_register_extension(struct cx231xx_ops *ops)
93{
94	struct cx231xx *dev = NULL;
95
96	mutex_lock(&cx231xx_devlist_mutex);
97	mutex_lock(&cx231xx_extension_devlist_lock);
98	list_add_tail(&ops->next, &cx231xx_extension_devlist);
99	list_for_each_entry(dev, &cx231xx_devlist, devlist)
100		ops->init(dev);
101
102	printk(KERN_INFO DRIVER_NAME ": %s initialized\n", ops->name);
103	mutex_unlock(&cx231xx_extension_devlist_lock);
104	mutex_unlock(&cx231xx_devlist_mutex);
105	return 0;
106}
107EXPORT_SYMBOL(cx231xx_register_extension);
108
109void cx231xx_unregister_extension(struct cx231xx_ops *ops)
110{
111	struct cx231xx *dev = NULL;
112
113	mutex_lock(&cx231xx_devlist_mutex);
114	list_for_each_entry(dev, &cx231xx_devlist, devlist)
115		ops->fini(dev);
116
117	mutex_lock(&cx231xx_extension_devlist_lock);
118	printk(KERN_INFO DRIVER_NAME ": %s removed\n", ops->name);
119	list_del(&ops->next);
120	mutex_unlock(&cx231xx_extension_devlist_lock);
121	mutex_unlock(&cx231xx_devlist_mutex);
122}
123EXPORT_SYMBOL(cx231xx_unregister_extension);
124
125void cx231xx_init_extension(struct cx231xx *dev)
126{
127	struct cx231xx_ops *ops = NULL;
128
129	mutex_lock(&cx231xx_extension_devlist_lock);
130	if (!list_empty(&cx231xx_extension_devlist)) {
131		list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
132			if (ops->init)
133				ops->init(dev);
134		}
135	}
136	mutex_unlock(&cx231xx_extension_devlist_lock);
137}
138
139void cx231xx_close_extension(struct cx231xx *dev)
140{
141	struct cx231xx_ops *ops = NULL;
142
143	mutex_lock(&cx231xx_extension_devlist_lock);
144	if (!list_empty(&cx231xx_extension_devlist)) {
145		list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
146			if (ops->fini)
147				ops->fini(dev);
148		}
149	}
150	mutex_unlock(&cx231xx_extension_devlist_lock);
151}
152
153/****************************************************************
154*               U S B related functions                         *
155*****************************************************************/
156int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
157			     struct cx231xx_i2c_xfer_data *req_data)
158{
159	int status = 0;
160	struct cx231xx *dev = i2c_bus->dev;
161	struct VENDOR_REQUEST_IN ven_req;
162
163	u8 saddr_len = 0;
164	u8 _i2c_period = 0;
165	u8 _i2c_nostop = 0;
166	u8 _i2c_reserve = 0;
167
168	/* Get the I2C period, nostop and reserve parameters */
169	_i2c_period = i2c_bus->i2c_period;
170	_i2c_nostop = i2c_bus->i2c_nostop;
171	_i2c_reserve = i2c_bus->i2c_reserve;
172
173	saddr_len = req_data->saddr_len;
174
175	/* Set wValue */
176	if (saddr_len == 1)	/* need check saddr_len == 0  */
177		ven_req.wValue =
178		    req_data->
179		    dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
180		    _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
181	else
182		ven_req.wValue =
183		    req_data->
184		    dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
185		    _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
186
187	/* set channel number */
188	if (req_data->direction & I2C_M_RD) {
189		/* channel number, for read,spec required channel_num +4 */
190		ven_req.bRequest = i2c_bus->nr + 4;
191	} else
192		ven_req.bRequest = i2c_bus->nr;	/* channel number,  */
193
194	/* set index value */
195	switch (saddr_len) {
196	case 0:
197		ven_req.wIndex = 0;	/* need check */
198		break;
199	case 1:
200		ven_req.wIndex = (req_data->saddr_dat & 0xff);
201		break;
202	case 2:
203		ven_req.wIndex = req_data->saddr_dat;
204		break;
205	}
206
207	/* set wLength value */
208	ven_req.wLength = req_data->buf_size;
209
210	/* set bData value */
211	ven_req.bData = 0;
212
213	/* set the direction */
214	if (req_data->direction) {
215		ven_req.direction = USB_DIR_IN;
216		memset(req_data->p_buffer, 0x00, ven_req.wLength);
217	} else
218		ven_req.direction = USB_DIR_OUT;
219
220	/* set the buffer for read / write */
221	ven_req.pBuff = req_data->p_buffer;
222
223
224	/* call common vendor command request */
225	status = cx231xx_send_vendor_cmd(dev, &ven_req);
226	if (status < 0) {
227		cx231xx_info
228		    ("UsbInterface::sendCommand, failed with status -%d\n",
229		     status);
230	}
231
232	return status;
233}
234EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
235
236/*
237 * cx231xx_read_ctrl_reg()
238 * reads data from the usb device specifying bRequest and wValue
239 */
240int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
241			  char *buf, int len)
242{
243	u8 val = 0;
244	int ret;
245	int pipe = usb_rcvctrlpipe(dev->udev, 0);
246
247	if (dev->state & DEV_DISCONNECTED)
248		return -ENODEV;
249
250	if (len > URB_MAX_CTRL_SIZE)
251		return -EINVAL;
252
253	switch (len) {
254	case 1:
255		val = ENABLE_ONE_BYTE;
256		break;
257	case 2:
258		val = ENABLE_TWE_BYTE;
259		break;
260	case 3:
261		val = ENABLE_THREE_BYTE;
262		break;
263	case 4:
264		val = ENABLE_FOUR_BYTE;
265		break;
266	default:
267		val = 0xFF;	/* invalid option */
268	}
269
270	if (val == 0xFF)
271		return -EINVAL;
272
273	if (reg_debug) {
274		cx231xx_isocdbg("(pipe 0x%08x): "
275				"IN:  %02x %02x %02x %02x %02x %02x %02x %02x ",
276				pipe,
277				USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
278				req, 0, val,
279				reg & 0xff, reg >> 8, len & 0xff, len >> 8);
280	}
281
282	mutex_lock(&dev->ctrl_urb_lock);
283	ret = usb_control_msg(dev->udev, pipe, req,
284			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
285			      val, reg, dev->urb_buf, len, HZ);
286	if (ret < 0) {
287		cx231xx_isocdbg(" failed!\n");
288		/* mutex_unlock(&dev->ctrl_urb_lock); */
289		return ret;
290	}
291
292	if (len)
293		memcpy(buf, dev->urb_buf, len);
294
295	mutex_unlock(&dev->ctrl_urb_lock);
296
297	if (reg_debug) {
298		int byte;
299
300		cx231xx_isocdbg("<<<");
301		for (byte = 0; byte < len; byte++)
302			cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
303		cx231xx_isocdbg("\n");
304	}
305
306	return ret;
307}
308
309int cx231xx_send_vendor_cmd(struct cx231xx *dev,
310				struct VENDOR_REQUEST_IN *ven_req)
311{
312	int ret;
313	int pipe = 0;
314
315	if (dev->state & DEV_DISCONNECTED)
316		return -ENODEV;
317
318	if ((ven_req->wLength > URB_MAX_CTRL_SIZE))
319		return -EINVAL;
320
321	if (ven_req->direction)
322		pipe = usb_rcvctrlpipe(dev->udev, 0);
323	else
324		pipe = usb_sndctrlpipe(dev->udev, 0);
325
326	if (reg_debug) {
327		int byte;
328
329		cx231xx_isocdbg("(pipe 0x%08x): "
330				"OUT: %02x %02x %02x %04x %04x %04x >>>",
331				pipe,
332				ven_req->
333				direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
334				ven_req->bRequest, 0, ven_req->wValue,
335				ven_req->wIndex, ven_req->wLength);
336
337		for (byte = 0; byte < ven_req->wLength; byte++)
338			cx231xx_isocdbg(" %02x",
339					(unsigned char)ven_req->pBuff[byte]);
340		cx231xx_isocdbg("\n");
341	}
342
343	mutex_lock(&dev->ctrl_urb_lock);
344	ret = usb_control_msg(dev->udev, pipe, ven_req->bRequest,
345			      ven_req->
346			      direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
347			      ven_req->wValue, ven_req->wIndex, ven_req->pBuff,
348			      ven_req->wLength, HZ);
349	mutex_unlock(&dev->ctrl_urb_lock);
350
351	return ret;
352}
353
354/*
355 * cx231xx_write_ctrl_reg()
356 * sends data to the usb device, specifying bRequest
357 */
358int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf,
359			   int len)
360{
361	u8 val = 0;
362	int ret;
363	int pipe = usb_sndctrlpipe(dev->udev, 0);
364
365	if (dev->state & DEV_DISCONNECTED)
366		return -ENODEV;
367
368	if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
369		return -EINVAL;
370
371	switch (len) {
372	case 1:
373		val = ENABLE_ONE_BYTE;
374		break;
375	case 2:
376		val = ENABLE_TWE_BYTE;
377		break;
378	case 3:
379		val = ENABLE_THREE_BYTE;
380		break;
381	case 4:
382		val = ENABLE_FOUR_BYTE;
383		break;
384	default:
385		val = 0xFF;	/* invalid option */
386	}
387
388	if (val == 0xFF)
389		return -EINVAL;
390
391	if (reg_debug) {
392		int byte;
393
394		cx231xx_isocdbg("(pipe 0x%08x): "
395			"OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
396			pipe,
397			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
398			req, 0, val, reg & 0xff,
399			reg >> 8, len & 0xff, len >> 8);
400
401		for (byte = 0; byte < len; byte++)
402			cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
403		cx231xx_isocdbg("\n");
404	}
405
406	mutex_lock(&dev->ctrl_urb_lock);
407	memcpy(dev->urb_buf, buf, len);
408	ret = usb_control_msg(dev->udev, pipe, req,
409			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
410			      val, reg, dev->urb_buf, len, HZ);
411	mutex_unlock(&dev->ctrl_urb_lock);
412
413	return ret;
414}
415
416/****************************************************************
417*           USB Alternate Setting functions                     *
418*****************************************************************/
419
420int cx231xx_set_video_alternate(struct cx231xx *dev)
421{
422	int errCode, prev_alt = dev->video_mode.alt;
423	unsigned int min_pkt_size = dev->width * 2 + 4;
424	u32 usb_interface_index = 0;
425
426	/* When image size is bigger than a certain value,
427	   the frame size should be increased, otherwise, only
428	   green screen will be received.
429	 */
430	if (dev->width * 2 * dev->height > 720 * 240 * 2)
431		min_pkt_size *= 2;
432
433	if (dev->width > 360) {
434		/* resolutions: 720,704,640 */
435		dev->video_mode.alt = 3;
436	} else if (dev->width > 180) {
437		/* resolutions: 360,352,320,240 */
438		dev->video_mode.alt = 2;
439	} else if (dev->width > 0) {
440		/* resolutions: 180,176,160,128,88 */
441		dev->video_mode.alt = 1;
442	} else {
443		/* Change to alt0 BULK to release USB bandwidth */
444		dev->video_mode.alt = 0;
445	}
446
447	/* Get the correct video interface Index */
448	usb_interface_index =
449	    dev->current_pcb_config.hs_config_info[0].interface_info.
450	    video_index + 1;
451
452	if (dev->video_mode.alt != prev_alt) {
453		cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
454				min_pkt_size, dev->video_mode.alt);
455		dev->video_mode.max_pkt_size =
456		    dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
457		cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
458				dev->video_mode.alt,
459				dev->video_mode.max_pkt_size);
460		cx231xx_info
461		    (" setting alt %d with wMaxPktSize=%u , Interface = %d\n",
462		     dev->video_mode.alt, dev->video_mode.max_pkt_size,
463		     usb_interface_index);
464		errCode =
465		    usb_set_interface(dev->udev, usb_interface_index,
466				      dev->video_mode.alt);
467		if (errCode < 0) {
468			cx231xx_errdev
469			    ("cannot change alt number to %d (error=%i)\n",
470			     dev->video_mode.alt, errCode);
471			return errCode;
472		}
473	}
474	return 0;
475}
476
477int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
478{
479	int status = 0;
480	u32 usb_interface_index = 0;
481	u32 max_pkt_size = 0;
482
483	switch (index) {
484	case INDEX_TS1:
485		usb_interface_index =
486		    dev->current_pcb_config.hs_config_info[0].interface_info.
487		    ts1_index + 1;
488		dev->video_mode.alt = alt;
489		if (dev->ts1_mode.alt_max_pkt_size != NULL)
490			max_pkt_size = dev->ts1_mode.max_pkt_size =
491			    dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt];
492		break;
493	case INDEX_TS2:
494		usb_interface_index =
495		    dev->current_pcb_config.hs_config_info[0].interface_info.
496		    ts2_index + 1;
497		break;
498	case INDEX_AUDIO:
499		usb_interface_index =
500		    dev->current_pcb_config.hs_config_info[0].interface_info.
501		    audio_index + 1;
502		dev->adev.alt = alt;
503		if (dev->adev.alt_max_pkt_size != NULL)
504			max_pkt_size = dev->adev.max_pkt_size =
505			    dev->adev.alt_max_pkt_size[dev->adev.alt];
506		break;
507	case INDEX_VIDEO:
508		usb_interface_index =
509		    dev->current_pcb_config.hs_config_info[0].interface_info.
510		    video_index + 1;
511		dev->video_mode.alt = alt;
512		if (dev->video_mode.alt_max_pkt_size != NULL)
513			max_pkt_size = dev->video_mode.max_pkt_size =
514			    dev->video_mode.alt_max_pkt_size[dev->video_mode.
515							     alt];
516		break;
517	case INDEX_VANC:
518		usb_interface_index =
519		    dev->current_pcb_config.hs_config_info[0].interface_info.
520		    vanc_index + 1;
521		dev->vbi_mode.alt = alt;
522		if (dev->vbi_mode.alt_max_pkt_size != NULL)
523			max_pkt_size = dev->vbi_mode.max_pkt_size =
524			    dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt];
525		break;
526	case INDEX_HANC:
527		usb_interface_index =
528		    dev->current_pcb_config.hs_config_info[0].interface_info.
529		    hanc_index + 1;
530		dev->sliced_cc_mode.alt = alt;
531		if (dev->sliced_cc_mode.alt_max_pkt_size != NULL)
532			max_pkt_size = dev->sliced_cc_mode.max_pkt_size =
533			    dev->sliced_cc_mode.alt_max_pkt_size[dev->
534								 sliced_cc_mode.
535								 alt];
536		break;
537	default:
538		break;
539	}
540
541	if (alt > 0 && max_pkt_size == 0) {
542		cx231xx_errdev
543		("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
544		usb_interface_index, alt);
545		return -1;
546	}
547
548	cx231xx_info
549	    (" setting alternate %d with wMaxPacketSize=%u , Interface = %d\n",
550	     alt, max_pkt_size, usb_interface_index);
551
552	if (usb_interface_index > 0) {
553		status = usb_set_interface(dev->udev, usb_interface_index, alt);
554		if (status < 0) {
555			cx231xx_errdev
556			("can't change interface %d alt no. to %d (err=%i)\n",
557			usb_interface_index, alt, status);
558			return status;
559		}
560	}
561
562	return status;
563}
564EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting);
565
566int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio)
567{
568	int rc = 0;
569
570	if (!gpio)
571		return rc;
572
573	/* Send GPIO reset sequences specified at board entry */
574	while (gpio->sleep >= 0) {
575		rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
576		if (rc < 0)
577			return rc;
578
579		if (gpio->sleep > 0)
580			msleep(gpio->sleep);
581
582		gpio++;
583	}
584	return rc;
585}
586
587int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
588{
589	if (dev->mode == set_mode)
590		return 0;
591
592	if (set_mode == CX231XX_SUSPEND) {
593		/* Set the chip in power saving mode */
594		dev->mode = set_mode;
595	}
596
597	/* Resource is locked */
598	if (dev->mode != CX231XX_SUSPEND)
599		return -EINVAL;
600
601	dev->mode = set_mode;
602
603	if (dev->mode == CX231XX_DIGITAL_MODE)
604		;/* Set Digital power mode */
605	else
606		;/* Set Analog Power mode */
607
608	return 0;
609}
610EXPORT_SYMBOL_GPL(cx231xx_set_mode);
611
612/*****************************************************************
613*                URB Streaming functions                         *
614******************************************************************/
615
616/*
617 * IRQ callback, called by URB callback
618 */
619static void cx231xx_irq_callback(struct urb *urb)
620{
621	struct cx231xx_dmaqueue *dma_q = urb->context;
622	struct cx231xx_video_mode *vmode =
623	    container_of(dma_q, struct cx231xx_video_mode, vidq);
624	struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
625	int rc, i;
626
627	switch (urb->status) {
628	case 0:		/* success */
629	case -ETIMEDOUT:	/* NAK */
630		break;
631	case -ECONNRESET:	/* kill */
632	case -ENOENT:
633	case -ESHUTDOWN:
634		return;
635	default:		/* error */
636		cx231xx_isocdbg("urb completition error %d.\n", urb->status);
637		break;
638	}
639
640	/* Copy data from URB */
641	spin_lock(&dev->video_mode.slock);
642	rc = dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
643	spin_unlock(&dev->video_mode.slock);
644
645	/* Reset urb buffers */
646	for (i = 0; i < urb->number_of_packets; i++) {
647		urb->iso_frame_desc[i].status = 0;
648		urb->iso_frame_desc[i].actual_length = 0;
649	}
650	urb->status = 0;
651
652	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
653	if (urb->status) {
654		cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
655				urb->status);
656	}
657}
658
659/*
660 * Stop and Deallocate URBs
661 */
662void cx231xx_uninit_isoc(struct cx231xx *dev)
663{
664	struct urb *urb;
665	int i;
666
667	cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
668
669	dev->video_mode.isoc_ctl.nfields = -1;
670	for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
671		urb = dev->video_mode.isoc_ctl.urb[i];
672		if (urb) {
673			if (!irqs_disabled())
674				usb_kill_urb(urb);
675			else
676				usb_unlink_urb(urb);
677
678			if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
679				usb_free_coherent(dev->udev,
680						  urb->transfer_buffer_length,
681						  dev->video_mode.isoc_ctl.
682						  transfer_buffer[i],
683						  urb->transfer_dma);
684			}
685			usb_free_urb(urb);
686			dev->video_mode.isoc_ctl.urb[i] = NULL;
687		}
688		dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL;
689	}
690
691	kfree(dev->video_mode.isoc_ctl.urb);
692	kfree(dev->video_mode.isoc_ctl.transfer_buffer);
693
694	dev->video_mode.isoc_ctl.urb = NULL;
695	dev->video_mode.isoc_ctl.transfer_buffer = NULL;
696	dev->video_mode.isoc_ctl.num_bufs = 0;
697
698	cx231xx_capture_start(dev, 0, Raw_Video);
699}
700EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);
701
702/*
703 * Allocate URBs and start IRQ
704 */
705int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
706		      int num_bufs, int max_pkt_size,
707		      int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
708{
709	struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
710	int i;
711	int sb_size, pipe;
712	struct urb *urb;
713	int j, k;
714	int rc;
715
716	cx231xx_isocdbg("cx231xx: called cx231xx_prepare_isoc\n");
717
718	dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
719
720	cx231xx_info("Setting Video mux to %d\n", dev->video_input);
721	video_mux(dev, dev->video_input);
722
723	/* De-allocates all pending stuff */
724	cx231xx_uninit_isoc(dev);
725
726	dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
727	dev->video_mode.isoc_ctl.num_bufs = num_bufs;
728	dma_q->pos = 0;
729	dma_q->is_partial_line = 0;
730	dma_q->last_sav = 0;
731	dma_q->current_field = -1;
732	dma_q->field1_done = 0;
733	dma_q->lines_per_field = dev->height / 2;
734	dma_q->bytes_left_in_line = dev->width << 1;
735	dma_q->lines_completed = 0;
736	for (i = 0; i < 8; i++)
737		dma_q->partial_buf[i] = 0;
738
739	dev->video_mode.isoc_ctl.urb =
740	    kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
741	if (!dev->video_mode.isoc_ctl.urb) {
742		cx231xx_errdev("cannot alloc memory for usb buffers\n");
743		return -ENOMEM;
744	}
745
746	dev->video_mode.isoc_ctl.transfer_buffer =
747	    kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
748	if (!dev->video_mode.isoc_ctl.transfer_buffer) {
749		cx231xx_errdev("cannot allocate memory for usbtransfer\n");
750		kfree(dev->video_mode.isoc_ctl.urb);
751		return -ENOMEM;
752	}
753
754	dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size;
755	dev->video_mode.isoc_ctl.buf = NULL;
756
757	sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size;
758
759	/* allocate urbs and transfer buffers */
760	for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
761		urb = usb_alloc_urb(max_packets, GFP_KERNEL);
762		if (!urb) {
763			cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i);
764			cx231xx_uninit_isoc(dev);
765			return -ENOMEM;
766		}
767		dev->video_mode.isoc_ctl.urb[i] = urb;
768
769		dev->video_mode.isoc_ctl.transfer_buffer[i] =
770		    usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
771				       &urb->transfer_dma);
772		if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
773			cx231xx_err("unable to allocate %i bytes for transfer"
774				    " buffer %i%s\n",
775				    sb_size, i,
776				    in_interrupt() ? " while in int" : "");
777			cx231xx_uninit_isoc(dev);
778			return -ENOMEM;
779		}
780		memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);
781
782		pipe =
783		    usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
784
785		usb_fill_int_urb(urb, dev->udev, pipe,
786				 dev->video_mode.isoc_ctl.transfer_buffer[i],
787				 sb_size, cx231xx_irq_callback, dma_q, 1);
788
789		urb->number_of_packets = max_packets;
790		urb->transfer_flags = URB_ISO_ASAP;
791
792		k = 0;
793		for (j = 0; j < max_packets; j++) {
794			urb->iso_frame_desc[j].offset = k;
795			urb->iso_frame_desc[j].length =
796			    dev->video_mode.isoc_ctl.max_pkt_size;
797			k += dev->video_mode.isoc_ctl.max_pkt_size;
798		}
799	}
800
801	init_waitqueue_head(&dma_q->wq);
802
803	/* submit urbs and enables IRQ */
804	for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
805		rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
806				    GFP_ATOMIC);
807		if (rc) {
808			cx231xx_err("submit of urb %i failed (error=%i)\n", i,
809				    rc);
810			cx231xx_uninit_isoc(dev);
811			return rc;
812		}
813	}
814
815	cx231xx_capture_start(dev, 1, Raw_Video);
816
817	return 0;
818}
819EXPORT_SYMBOL_GPL(cx231xx_init_isoc);
820
821/*****************************************************************
822*             Device Init/UnInit functions                       *
823******************************************************************/
824int cx231xx_dev_init(struct cx231xx *dev)
825{
826	int errCode = 0;
827
828	/* Initialize I2C bus */
829
830	/* External Master 1 Bus */
831	dev->i2c_bus[0].nr = 0;
832	dev->i2c_bus[0].dev = dev;
833	dev->i2c_bus[0].i2c_period = I2C_SPEED_1M;	/* 1MHz */
834	dev->i2c_bus[0].i2c_nostop = 0;
835	dev->i2c_bus[0].i2c_reserve = 0;
836
837	/* External Master 2 Bus */
838	dev->i2c_bus[1].nr = 1;
839	dev->i2c_bus[1].dev = dev;
840	dev->i2c_bus[1].i2c_period = I2C_SPEED_1M;	/* 1MHz */
841	dev->i2c_bus[1].i2c_nostop = 0;
842	dev->i2c_bus[1].i2c_reserve = 0;
843
844	/* Internal Master 3 Bus */
845	dev->i2c_bus[2].nr = 2;
846	dev->i2c_bus[2].dev = dev;
847	dev->i2c_bus[2].i2c_period = I2C_SPEED_400K;	/* 400kHz */
848	dev->i2c_bus[2].i2c_nostop = 0;
849	dev->i2c_bus[2].i2c_reserve = 0;
850
851	/* register I2C buses */
852	cx231xx_i2c_register(&dev->i2c_bus[0]);
853	cx231xx_i2c_register(&dev->i2c_bus[1]);
854	cx231xx_i2c_register(&dev->i2c_bus[2]);
855
856	/* init hardware */
857	/* Note : with out calling set power mode function,
858	afe can not be set up correctly */
859	errCode = cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
860	if (errCode < 0) {
861		cx231xx_errdev
862		    ("%s: Failed to set Power - errCode [%d]!\n",
863		     __func__, errCode);
864		return errCode;
865	}
866
867	/* initialize Colibri block */
868	errCode = cx231xx_afe_init_super_block(dev, 0x23c);
869	if (errCode < 0) {
870		cx231xx_errdev
871		    ("%s: cx231xx_afe init super block - errCode [%d]!\n",
872		     __func__, errCode);
873		return errCode;
874	}
875	errCode = cx231xx_afe_init_channels(dev);
876	if (errCode < 0) {
877		cx231xx_errdev
878		    ("%s: cx231xx_afe init channels - errCode [%d]!\n",
879		     __func__, errCode);
880		return errCode;
881	}
882
883	/* Set DIF in By pass mode */
884	errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
885	if (errCode < 0) {
886		cx231xx_errdev
887		    ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
888		     __func__, errCode);
889		return errCode;
890	}
891
892	/* I2S block related functions */
893	errCode = cx231xx_i2s_blk_initialize(dev);
894	if (errCode < 0) {
895		cx231xx_errdev
896		    ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
897		     __func__, errCode);
898		return errCode;
899	}
900
901	/* init control pins */
902	errCode = cx231xx_init_ctrl_pin_status(dev);
903	if (errCode < 0) {
904		cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
905			       __func__, errCode);
906		return errCode;
907	}
908
909	/* set AGC mode to Analog */
910	errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
911	if (errCode < 0) {
912		cx231xx_errdev
913		    ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
914		     __func__, errCode);
915		return errCode;
916	}
917
918	/* set all alternate settings to zero initially */
919	cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
920	cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
921	cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
922	if (dev->board.has_dvb)
923		cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
924
925	/* set the I2C master port to 3 on channel 1 */
926	errCode = cx231xx_enable_i2c_for_tuner(dev, I2C_3);
927
928	return errCode;
929}
930EXPORT_SYMBOL_GPL(cx231xx_dev_init);
931
932void cx231xx_dev_uninit(struct cx231xx *dev)
933{
934	/* Un Initialize I2C bus */
935	cx231xx_i2c_unregister(&dev->i2c_bus[2]);
936	cx231xx_i2c_unregister(&dev->i2c_bus[1]);
937	cx231xx_i2c_unregister(&dev->i2c_bus[0]);
938}
939EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
940
941/*****************************************************************
942*              G P I O related functions                         *
943******************************************************************/
944int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 * gpio_val,
945			  u8 len, u8 request, u8 direction)
946{
947	int status = 0;
948	struct VENDOR_REQUEST_IN ven_req;
949
950	/* Set wValue */
951	ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff);
952
953	/* set request */
954	if (!request) {
955		if (direction)
956			ven_req.bRequest = VRT_GET_GPIO;	/* 0x8 gpio */
957		else
958			ven_req.bRequest = VRT_SET_GPIO;	/* 0x9 gpio */
959	} else {
960		if (direction)
961			ven_req.bRequest = VRT_GET_GPIE;	/* 0xa gpie */
962		else
963			ven_req.bRequest = VRT_SET_GPIE;	/* 0xb gpie */
964	}
965
966	/* set index value */
967	ven_req.wIndex = (u16) (gpio_bit & 0xffff);
968
969	/* set wLength value */
970	ven_req.wLength = len;
971
972	/* set bData value */
973	ven_req.bData = 0;
974
975	/* set the buffer for read / write */
976	ven_req.pBuff = gpio_val;
977
978	/* set the direction */
979	if (direction) {
980		ven_req.direction = USB_DIR_IN;
981		memset(ven_req.pBuff, 0x00, ven_req.wLength);
982	} else
983		ven_req.direction = USB_DIR_OUT;
984
985
986	/* call common vendor command request */
987	status = cx231xx_send_vendor_cmd(dev, &ven_req);
988	if (status < 0) {
989		cx231xx_info
990		    ("UsbInterface::sendCommand, failed with status -%d\n",
991		     status);
992	}
993
994	return status;
995}
996EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);
997
998/*****************************************************************
999 *    C O N T R O L - Register R E A D / W R I T E functions     *
1000 *****************************************************************/
1001int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
1002{
1003	u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
1004	u32 tmp = 0;
1005	int status = 0;
1006
1007	status =
1008	    cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
1009	if (status < 0)
1010		return status;
1011
1012	tmp = *((u32 *) value);
1013	tmp |= mode;
1014
1015	value[0] = (u8) tmp;
1016	value[1] = (u8) (tmp >> 8);
1017	value[2] = (u8) (tmp >> 16);
1018	value[3] = (u8) (tmp >> 24);
1019
1020	status =
1021	    cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
1022
1023	return status;
1024}
1025
1026/*****************************************************************
1027 *            I 2 C Internal C O N T R O L   functions           *
1028 *****************************************************************/
1029int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1030			  u8 saddr_len, u32 *data, u8 data_len)
1031{
1032	int status = 0;
1033	struct cx231xx_i2c_xfer_data req_data;
1034	u8 value[4] = { 0, 0, 0, 0 };
1035
1036	if (saddr_len == 0)
1037		saddr = 0;
1038	else if (saddr_len == 0)
1039		saddr &= 0xff;
1040
1041	/* prepare xfer_data struct */
1042	req_data.dev_addr = dev_addr >> 1;
1043	req_data.direction = I2C_M_RD;
1044	req_data.saddr_len = saddr_len;
1045	req_data.saddr_dat = saddr;
1046	req_data.buf_size = data_len;
1047	req_data.p_buffer = (u8 *) value;
1048
1049	/* usb send command */
1050	status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1051
1052	if (status >= 0) {
1053		/* Copy the data read back to main buffer */
1054		if (data_len == 1)
1055			*data = value[0];
1056		else
1057			*data =
1058			    value[0] | value[1] << 8 | value[2] << 16 | value[3]
1059			    << 24;
1060	}
1061
1062	return status;
1063}
1064
1065int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1066			   u8 saddr_len, u32 data, u8 data_len)
1067{
1068	int status = 0;
1069	u8 value[4] = { 0, 0, 0, 0 };
1070	struct cx231xx_i2c_xfer_data req_data;
1071
1072	value[0] = (u8) data;
1073	value[1] = (u8) (data >> 8);
1074	value[2] = (u8) (data >> 16);
1075	value[3] = (u8) (data >> 24);
1076
1077	if (saddr_len == 0)
1078		saddr = 0;
1079	else if (saddr_len == 0)
1080		saddr &= 0xff;
1081
1082	/* prepare xfer_data struct */
1083	req_data.dev_addr = dev_addr >> 1;
1084	req_data.direction = 0;
1085	req_data.saddr_len = saddr_len;
1086	req_data.saddr_dat = saddr;
1087	req_data.buf_size = data_len;
1088	req_data.p_buffer = value;
1089
1090	/* usb send command */
1091	status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1092
1093	return status;
1094}
1095
1096int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
1097			   u16 register_address, u8 bit_start, u8 bit_end,
1098			   u32 value)
1099{
1100	int status = 0;
1101	u32 tmp;
1102	u32 mask = 0;
1103	int i;
1104
1105	if (bit_start > (size - 1) || bit_end > (size - 1))
1106		return -1;
1107
1108	if (size == 8) {
1109		status =
1110		    cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1111					  &tmp, 1);
1112	} else {
1113		status =
1114		    cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1115					  &tmp, 4);
1116	}
1117
1118	if (status < 0)
1119		return status;
1120
1121	mask = 1 << bit_end;
1122	for (i = bit_end; i > bit_start && i > 0; i--)
1123		mask = mask + (1 << (i - 1));
1124
1125	value <<= bit_start;
1126
1127	if (size == 8) {
1128		tmp &= ~mask;
1129		tmp |= value;
1130		tmp &= 0xff;
1131		status =
1132		    cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1133					   tmp, 1);
1134	} else {
1135		tmp &= ~mask;
1136		tmp |= value;
1137		status =
1138		    cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1139					   tmp, 4);
1140	}
1141
1142	return status;
1143}
1144
1145int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
1146					u16 saddr, u32 mask, u32 value)
1147{
1148	u32 temp;
1149	int status = 0;
1150
1151	status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
1152
1153	if (status < 0)
1154		return status;
1155
1156	temp &= ~mask;
1157	temp |= value;
1158
1159	status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
1160
1161	return status;
1162}
1163
1164u32 cx231xx_set_field(u32 field_mask, u32 data)
1165{
1166	u32 temp;
1167
1168	for (temp = field_mask; (temp & 1) == 0; temp >>= 1)
1169		data <<= 1;
1170
1171	return data;
1172}
1173