xref: /openbmc/linux/drivers/media/usb/cx231xx/cx231xx-core.c (revision 1188f7f111c61394ec56beb8e30322305a8220b6)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab    cx231xx-core.c - driver for Conexant Cx23100/101/102
40c0d06caSMauro Carvalho Chehab 				USB video capture devices
50c0d06caSMauro Carvalho Chehab 
60c0d06caSMauro Carvalho Chehab    Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
70c0d06caSMauro Carvalho Chehab 				Based on em28xx driver
80c0d06caSMauro Carvalho Chehab 
90c0d06caSMauro Carvalho Chehab  */
100c0d06caSMauro Carvalho Chehab 
11589dadf2SMauro Carvalho Chehab #include "cx231xx.h"
120c0d06caSMauro Carvalho Chehab #include <linux/init.h>
130c0d06caSMauro Carvalho Chehab #include <linux/list.h>
140c0d06caSMauro Carvalho Chehab #include <linux/module.h>
150c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
160c0d06caSMauro Carvalho Chehab #include <linux/vmalloc.h>
170c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h>
180c0d06caSMauro Carvalho Chehab #include <media/tuner.h>
190c0d06caSMauro Carvalho Chehab 
200c0d06caSMauro Carvalho Chehab #include "cx231xx-reg.h"
210c0d06caSMauro Carvalho Chehab 
220c0d06caSMauro Carvalho Chehab /* #define ENABLE_DEBUG_ISOC_FRAMES */
230c0d06caSMauro Carvalho Chehab 
240c0d06caSMauro Carvalho Chehab static unsigned int core_debug;
250c0d06caSMauro Carvalho Chehab module_param(core_debug, int, 0644);
260c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
270c0d06caSMauro Carvalho Chehab 
280c0d06caSMauro Carvalho Chehab #define cx231xx_coredbg(fmt, arg...) do {\
290c0d06caSMauro Carvalho Chehab 	if (core_debug) \
300c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s %s :"fmt, \
310c0d06caSMauro Carvalho Chehab 			 dev->name, __func__ , ##arg); } while (0)
320c0d06caSMauro Carvalho Chehab 
330c0d06caSMauro Carvalho Chehab static unsigned int reg_debug;
340c0d06caSMauro Carvalho Chehab module_param(reg_debug, int, 0644);
350c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
360c0d06caSMauro Carvalho Chehab 
370c0d06caSMauro Carvalho Chehab static int alt = CX231XX_PINOUT;
380c0d06caSMauro Carvalho Chehab module_param(alt, int, 0644);
390c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
400c0d06caSMauro Carvalho Chehab 
410c0d06caSMauro Carvalho Chehab #define cx231xx_isocdbg(fmt, arg...) do {\
420c0d06caSMauro Carvalho Chehab 	if (core_debug) \
430c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s %s :"fmt, \
440c0d06caSMauro Carvalho Chehab 			 dev->name, __func__ , ##arg); } while (0)
450c0d06caSMauro Carvalho Chehab 
460c0d06caSMauro Carvalho Chehab /*****************************************************************
470c0d06caSMauro Carvalho Chehab *             Device control list functions					 *
480c0d06caSMauro Carvalho Chehab ******************************************************************/
490c0d06caSMauro Carvalho Chehab 
500c0d06caSMauro Carvalho Chehab LIST_HEAD(cx231xx_devlist);
510c0d06caSMauro Carvalho Chehab static DEFINE_MUTEX(cx231xx_devlist_mutex);
520c0d06caSMauro Carvalho Chehab 
530c0d06caSMauro Carvalho Chehab /*
540c0d06caSMauro Carvalho Chehab  * cx231xx_realease_resources()
550c0d06caSMauro Carvalho Chehab  * unregisters the v4l2,i2c and usb devices
566b338c72SGeert Uytterhoeven  * called when the device gets disconnected or at module unload
570c0d06caSMauro Carvalho Chehab */
cx231xx_remove_from_devlist(struct cx231xx * dev)580c0d06caSMauro Carvalho Chehab void cx231xx_remove_from_devlist(struct cx231xx *dev)
590c0d06caSMauro Carvalho Chehab {
600c0d06caSMauro Carvalho Chehab 	if (dev == NULL)
610c0d06caSMauro Carvalho Chehab 		return;
620c0d06caSMauro Carvalho Chehab 	if (dev->udev == NULL)
630c0d06caSMauro Carvalho Chehab 		return;
640c0d06caSMauro Carvalho Chehab 
650c0d06caSMauro Carvalho Chehab 	if (atomic_read(&dev->devlist_count) > 0) {
660c0d06caSMauro Carvalho Chehab 		mutex_lock(&cx231xx_devlist_mutex);
670c0d06caSMauro Carvalho Chehab 		list_del(&dev->devlist);
680c0d06caSMauro Carvalho Chehab 		atomic_dec(&dev->devlist_count);
690c0d06caSMauro Carvalho Chehab 		mutex_unlock(&cx231xx_devlist_mutex);
700c0d06caSMauro Carvalho Chehab 	}
710c0d06caSMauro Carvalho Chehab };
720c0d06caSMauro Carvalho Chehab 
cx231xx_add_into_devlist(struct cx231xx * dev)730c0d06caSMauro Carvalho Chehab void cx231xx_add_into_devlist(struct cx231xx *dev)
740c0d06caSMauro Carvalho Chehab {
750c0d06caSMauro Carvalho Chehab 	mutex_lock(&cx231xx_devlist_mutex);
760c0d06caSMauro Carvalho Chehab 	list_add_tail(&dev->devlist, &cx231xx_devlist);
770c0d06caSMauro Carvalho Chehab 	atomic_inc(&dev->devlist_count);
780c0d06caSMauro Carvalho Chehab 	mutex_unlock(&cx231xx_devlist_mutex);
790c0d06caSMauro Carvalho Chehab };
800c0d06caSMauro Carvalho Chehab 
810c0d06caSMauro Carvalho Chehab static LIST_HEAD(cx231xx_extension_devlist);
820c0d06caSMauro Carvalho Chehab 
cx231xx_register_extension(struct cx231xx_ops * ops)830c0d06caSMauro Carvalho Chehab int cx231xx_register_extension(struct cx231xx_ops *ops)
840c0d06caSMauro Carvalho Chehab {
850c0d06caSMauro Carvalho Chehab 	struct cx231xx *dev = NULL;
860c0d06caSMauro Carvalho Chehab 
870c0d06caSMauro Carvalho Chehab 	mutex_lock(&cx231xx_devlist_mutex);
880c0d06caSMauro Carvalho Chehab 	list_add_tail(&ops->next, &cx231xx_extension_devlist);
89fb1817e4SMauro Carvalho Chehab 	list_for_each_entry(dev, &cx231xx_devlist, devlist) {
900c0d06caSMauro Carvalho Chehab 		ops->init(dev);
91336fea92SMauro Carvalho Chehab 		dev_info(dev->dev, "%s initialized\n", ops->name);
92fb1817e4SMauro Carvalho Chehab 	}
930c0d06caSMauro Carvalho Chehab 	mutex_unlock(&cx231xx_devlist_mutex);
940c0d06caSMauro Carvalho Chehab 	return 0;
950c0d06caSMauro Carvalho Chehab }
960c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL(cx231xx_register_extension);
970c0d06caSMauro Carvalho Chehab 
cx231xx_unregister_extension(struct cx231xx_ops * ops)980c0d06caSMauro Carvalho Chehab void cx231xx_unregister_extension(struct cx231xx_ops *ops)
990c0d06caSMauro Carvalho Chehab {
1000c0d06caSMauro Carvalho Chehab 	struct cx231xx *dev = NULL;
1010c0d06caSMauro Carvalho Chehab 
1020c0d06caSMauro Carvalho Chehab 	mutex_lock(&cx231xx_devlist_mutex);
103fb1817e4SMauro Carvalho Chehab 	list_for_each_entry(dev, &cx231xx_devlist, devlist) {
1040c0d06caSMauro Carvalho Chehab 		ops->fini(dev);
105336fea92SMauro Carvalho Chehab 		dev_info(dev->dev, "%s removed\n", ops->name);
106fb1817e4SMauro Carvalho Chehab 	}
1070c0d06caSMauro Carvalho Chehab 
1080c0d06caSMauro Carvalho Chehab 	list_del(&ops->next);
1090c0d06caSMauro Carvalho Chehab 	mutex_unlock(&cx231xx_devlist_mutex);
1100c0d06caSMauro Carvalho Chehab }
1110c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL(cx231xx_unregister_extension);
1120c0d06caSMauro Carvalho Chehab 
cx231xx_init_extension(struct cx231xx * dev)1130c0d06caSMauro Carvalho Chehab void cx231xx_init_extension(struct cx231xx *dev)
1140c0d06caSMauro Carvalho Chehab {
1150c0d06caSMauro Carvalho Chehab 	struct cx231xx_ops *ops = NULL;
1160c0d06caSMauro Carvalho Chehab 
1170c0d06caSMauro Carvalho Chehab 	mutex_lock(&cx231xx_devlist_mutex);
1180c0d06caSMauro Carvalho Chehab 	list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
1190c0d06caSMauro Carvalho Chehab 		if (ops->init)
1200c0d06caSMauro Carvalho Chehab 			ops->init(dev);
1210c0d06caSMauro Carvalho Chehab 	}
1220c0d06caSMauro Carvalho Chehab 	mutex_unlock(&cx231xx_devlist_mutex);
1230c0d06caSMauro Carvalho Chehab }
1240c0d06caSMauro Carvalho Chehab 
cx231xx_close_extension(struct cx231xx * dev)1250c0d06caSMauro Carvalho Chehab void cx231xx_close_extension(struct cx231xx *dev)
1260c0d06caSMauro Carvalho Chehab {
1270c0d06caSMauro Carvalho Chehab 	struct cx231xx_ops *ops = NULL;
1280c0d06caSMauro Carvalho Chehab 
1290c0d06caSMauro Carvalho Chehab 	mutex_lock(&cx231xx_devlist_mutex);
1300c0d06caSMauro Carvalho Chehab 	list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
1310c0d06caSMauro Carvalho Chehab 		if (ops->fini)
1320c0d06caSMauro Carvalho Chehab 			ops->fini(dev);
1330c0d06caSMauro Carvalho Chehab 	}
1340c0d06caSMauro Carvalho Chehab 	mutex_unlock(&cx231xx_devlist_mutex);
1350c0d06caSMauro Carvalho Chehab }
1360c0d06caSMauro Carvalho Chehab 
1370c0d06caSMauro Carvalho Chehab /****************************************************************
1380c0d06caSMauro Carvalho Chehab *               U S B related functions                         *
1390c0d06caSMauro Carvalho Chehab *****************************************************************/
cx231xx_send_usb_command(struct cx231xx_i2c * i2c_bus,struct cx231xx_i2c_xfer_data * req_data)1400c0d06caSMauro Carvalho Chehab int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
1410c0d06caSMauro Carvalho Chehab 			     struct cx231xx_i2c_xfer_data *req_data)
1420c0d06caSMauro Carvalho Chehab {
1430c0d06caSMauro Carvalho Chehab 	int status = 0;
1440c0d06caSMauro Carvalho Chehab 	struct cx231xx *dev = i2c_bus->dev;
1450c0d06caSMauro Carvalho Chehab 	struct VENDOR_REQUEST_IN ven_req;
1460c0d06caSMauro Carvalho Chehab 
1470c0d06caSMauro Carvalho Chehab 	u8 saddr_len = 0;
1480c0d06caSMauro Carvalho Chehab 	u8 _i2c_period = 0;
1490c0d06caSMauro Carvalho Chehab 	u8 _i2c_nostop = 0;
1500c0d06caSMauro Carvalho Chehab 	u8 _i2c_reserve = 0;
1510c0d06caSMauro Carvalho Chehab 
1520c0d06caSMauro Carvalho Chehab 	if (dev->state & DEV_DISCONNECTED)
1530c0d06caSMauro Carvalho Chehab 		return -ENODEV;
1540c0d06caSMauro Carvalho Chehab 
1550c0d06caSMauro Carvalho Chehab 	/* Get the I2C period, nostop and reserve parameters */
1560c0d06caSMauro Carvalho Chehab 	_i2c_period = i2c_bus->i2c_period;
1570c0d06caSMauro Carvalho Chehab 	_i2c_nostop = i2c_bus->i2c_nostop;
1580c0d06caSMauro Carvalho Chehab 	_i2c_reserve = i2c_bus->i2c_reserve;
1590c0d06caSMauro Carvalho Chehab 
1600c0d06caSMauro Carvalho Chehab 	saddr_len = req_data->saddr_len;
1610c0d06caSMauro Carvalho Chehab 
1620c0d06caSMauro Carvalho Chehab 	/* Set wValue */
1639713883bSNicholas Mc Guire 	ven_req.wValue = (req_data->dev_addr << 9 | _i2c_period << 4 |
1649713883bSNicholas Mc Guire 			  saddr_len << 2 | _i2c_nostop << 1 | I2C_SYNC |
1659713883bSNicholas Mc Guire 			  _i2c_reserve << 6);
1660c0d06caSMauro Carvalho Chehab 
1670c0d06caSMauro Carvalho Chehab 	/* set channel number */
1680c0d06caSMauro Carvalho Chehab 	if (req_data->direction & I2C_M_RD) {
1690c0d06caSMauro Carvalho Chehab 		/* channel number, for read,spec required channel_num +4 */
1700c0d06caSMauro Carvalho Chehab 		ven_req.bRequest = i2c_bus->nr + 4;
1710c0d06caSMauro Carvalho Chehab 	} else
1720c0d06caSMauro Carvalho Chehab 		ven_req.bRequest = i2c_bus->nr;	/* channel number,  */
1730c0d06caSMauro Carvalho Chehab 
1740c0d06caSMauro Carvalho Chehab 	/* set index value */
1750c0d06caSMauro Carvalho Chehab 	switch (saddr_len) {
1760c0d06caSMauro Carvalho Chehab 	case 0:
1770c0d06caSMauro Carvalho Chehab 		ven_req.wIndex = 0;	/* need check */
1780c0d06caSMauro Carvalho Chehab 		break;
1790c0d06caSMauro Carvalho Chehab 	case 1:
1800c0d06caSMauro Carvalho Chehab 		ven_req.wIndex = (req_data->saddr_dat & 0xff);
1810c0d06caSMauro Carvalho Chehab 		break;
1820c0d06caSMauro Carvalho Chehab 	case 2:
1830c0d06caSMauro Carvalho Chehab 		ven_req.wIndex = req_data->saddr_dat;
1840c0d06caSMauro Carvalho Chehab 		break;
1850c0d06caSMauro Carvalho Chehab 	}
1860c0d06caSMauro Carvalho Chehab 
1870c0d06caSMauro Carvalho Chehab 	/* set wLength value */
1880c0d06caSMauro Carvalho Chehab 	ven_req.wLength = req_data->buf_size;
1890c0d06caSMauro Carvalho Chehab 
1900c0d06caSMauro Carvalho Chehab 	/* set bData value */
1910c0d06caSMauro Carvalho Chehab 	ven_req.bData = 0;
1920c0d06caSMauro Carvalho Chehab 
1930c0d06caSMauro Carvalho Chehab 	/* set the direction */
1940c0d06caSMauro Carvalho Chehab 	if (req_data->direction) {
1950c0d06caSMauro Carvalho Chehab 		ven_req.direction = USB_DIR_IN;
1960c0d06caSMauro Carvalho Chehab 		memset(req_data->p_buffer, 0x00, ven_req.wLength);
1970c0d06caSMauro Carvalho Chehab 	} else
1980c0d06caSMauro Carvalho Chehab 		ven_req.direction = USB_DIR_OUT;
1990c0d06caSMauro Carvalho Chehab 
2000c0d06caSMauro Carvalho Chehab 	/* set the buffer for read / write */
2010c0d06caSMauro Carvalho Chehab 	ven_req.pBuff = req_data->p_buffer;
2020c0d06caSMauro Carvalho Chehab 
2030c0d06caSMauro Carvalho Chehab 
2040c0d06caSMauro Carvalho Chehab 	/* call common vendor command request */
2050c0d06caSMauro Carvalho Chehab 	status = cx231xx_send_vendor_cmd(dev, &ven_req);
20677e97ba2SMauro Carvalho Chehab 	if (status < 0 && !dev->i2c_scan_running) {
207336fea92SMauro Carvalho Chehab 		dev_err(dev->dev, "%s: failed with status -%d\n",
208ed0e3729SMauro Carvalho Chehab 			__func__, status);
2090c0d06caSMauro Carvalho Chehab 	}
2100c0d06caSMauro Carvalho Chehab 
2110c0d06caSMauro Carvalho Chehab 	return status;
2120c0d06caSMauro Carvalho Chehab }
2130c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
2140c0d06caSMauro Carvalho Chehab 
2150c0d06caSMauro Carvalho Chehab /*
2160c0d06caSMauro Carvalho Chehab  * Sends/Receives URB control messages, assuring to use a kalloced buffer
2170c0d06caSMauro Carvalho Chehab  * for all operations (dev->urb_buf), to avoid using stacked buffers, as
2180c0d06caSMauro Carvalho Chehab  * they aren't safe for usage with USB, due to DMA restrictions.
2190c0d06caSMauro Carvalho Chehab  * Also implements the debug code for control URB's.
2200c0d06caSMauro Carvalho Chehab  */
__usb_control_msg(struct cx231xx * dev,unsigned int pipe,__u8 request,__u8 requesttype,__u16 value,__u16 index,void * data,__u16 size,int timeout)2210c0d06caSMauro Carvalho Chehab static int __usb_control_msg(struct cx231xx *dev, unsigned int pipe,
2220c0d06caSMauro Carvalho Chehab 	__u8 request, __u8 requesttype, __u16 value, __u16 index,
2230c0d06caSMauro Carvalho Chehab 	void *data, __u16 size, int timeout)
2240c0d06caSMauro Carvalho Chehab {
2250c0d06caSMauro Carvalho Chehab 	int rc, i;
2260c0d06caSMauro Carvalho Chehab 
2270c0d06caSMauro Carvalho Chehab 	if (reg_debug) {
228ba1da97eSMauro Carvalho Chehab 		printk(KERN_DEBUG "%s: (pipe 0x%08x): %s:  %02x %02x %02x %02x %02x %02x %02x %02x ",
2290c0d06caSMauro Carvalho Chehab 				dev->name,
2300c0d06caSMauro Carvalho Chehab 				pipe,
2310c0d06caSMauro Carvalho Chehab 				(requesttype & USB_DIR_IN) ? "IN" : "OUT",
2320c0d06caSMauro Carvalho Chehab 				requesttype,
2330c0d06caSMauro Carvalho Chehab 				request,
2340c0d06caSMauro Carvalho Chehab 				value & 0xff, value >> 8,
2350c0d06caSMauro Carvalho Chehab 				index & 0xff, index >> 8,
2360c0d06caSMauro Carvalho Chehab 				size & 0xff, size >> 8);
2370c0d06caSMauro Carvalho Chehab 		if (!(requesttype & USB_DIR_IN)) {
2380c0d06caSMauro Carvalho Chehab 			printk(KERN_CONT ">>>");
2390c0d06caSMauro Carvalho Chehab 			for (i = 0; i < size; i++)
2400c0d06caSMauro Carvalho Chehab 				printk(KERN_CONT " %02x",
2410c0d06caSMauro Carvalho Chehab 				       ((unsigned char *)data)[i]);
2420c0d06caSMauro Carvalho Chehab 		}
2430c0d06caSMauro Carvalho Chehab 	}
2440c0d06caSMauro Carvalho Chehab 
2450c0d06caSMauro Carvalho Chehab 	/* Do the real call to usb_control_msg */
2460c0d06caSMauro Carvalho Chehab 	mutex_lock(&dev->ctrl_urb_lock);
2470c0d06caSMauro Carvalho Chehab 	if (!(requesttype & USB_DIR_IN) && size)
2480c0d06caSMauro Carvalho Chehab 		memcpy(dev->urb_buf, data, size);
2490c0d06caSMauro Carvalho Chehab 	rc = usb_control_msg(dev->udev, pipe, request, requesttype, value,
2500c0d06caSMauro Carvalho Chehab 			     index, dev->urb_buf, size, timeout);
2510c0d06caSMauro Carvalho Chehab 	if ((requesttype & USB_DIR_IN) && size)
2520c0d06caSMauro Carvalho Chehab 		memcpy(data, dev->urb_buf, size);
2530c0d06caSMauro Carvalho Chehab 	mutex_unlock(&dev->ctrl_urb_lock);
2540c0d06caSMauro Carvalho Chehab 
2550c0d06caSMauro Carvalho Chehab 	if (reg_debug) {
2560c0d06caSMauro Carvalho Chehab 		if (unlikely(rc < 0)) {
2570c0d06caSMauro Carvalho Chehab 			printk(KERN_CONT "FAILED!\n");
2580c0d06caSMauro Carvalho Chehab 			return rc;
2590c0d06caSMauro Carvalho Chehab 		}
2600c0d06caSMauro Carvalho Chehab 
2610c0d06caSMauro Carvalho Chehab 		if ((requesttype & USB_DIR_IN)) {
2620c0d06caSMauro Carvalho Chehab 			printk(KERN_CONT "<<<");
2630c0d06caSMauro Carvalho Chehab 			for (i = 0; i < size; i++)
2640c0d06caSMauro Carvalho Chehab 				printk(KERN_CONT " %02x",
2650c0d06caSMauro Carvalho Chehab 				       ((unsigned char *)data)[i]);
2660c0d06caSMauro Carvalho Chehab 		}
2670c0d06caSMauro Carvalho Chehab 		printk(KERN_CONT "\n");
2680c0d06caSMauro Carvalho Chehab 	}
2690c0d06caSMauro Carvalho Chehab 
2700c0d06caSMauro Carvalho Chehab 	return rc;
2710c0d06caSMauro Carvalho Chehab }
2720c0d06caSMauro Carvalho Chehab 
2730c0d06caSMauro Carvalho Chehab 
2740c0d06caSMauro Carvalho Chehab /*
2750c0d06caSMauro Carvalho Chehab  * cx231xx_read_ctrl_reg()
2760c0d06caSMauro Carvalho Chehab  * reads data from the usb device specifying bRequest and wValue
2770c0d06caSMauro Carvalho Chehab  */
cx231xx_read_ctrl_reg(struct cx231xx * dev,u8 req,u16 reg,char * buf,int len)2780c0d06caSMauro Carvalho Chehab int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
2790c0d06caSMauro Carvalho Chehab 			  char *buf, int len)
2800c0d06caSMauro Carvalho Chehab {
2810c0d06caSMauro Carvalho Chehab 	u8 val = 0;
2820c0d06caSMauro Carvalho Chehab 	int ret;
2830c0d06caSMauro Carvalho Chehab 	int pipe = usb_rcvctrlpipe(dev->udev, 0);
2840c0d06caSMauro Carvalho Chehab 
2850c0d06caSMauro Carvalho Chehab 	if (dev->state & DEV_DISCONNECTED)
2860c0d06caSMauro Carvalho Chehab 		return -ENODEV;
2870c0d06caSMauro Carvalho Chehab 
2880c0d06caSMauro Carvalho Chehab 	if (len > URB_MAX_CTRL_SIZE)
2890c0d06caSMauro Carvalho Chehab 		return -EINVAL;
2900c0d06caSMauro Carvalho Chehab 
2910c0d06caSMauro Carvalho Chehab 	switch (len) {
2920c0d06caSMauro Carvalho Chehab 	case 1:
2930c0d06caSMauro Carvalho Chehab 		val = ENABLE_ONE_BYTE;
2940c0d06caSMauro Carvalho Chehab 		break;
2950c0d06caSMauro Carvalho Chehab 	case 2:
2960c0d06caSMauro Carvalho Chehab 		val = ENABLE_TWE_BYTE;
2970c0d06caSMauro Carvalho Chehab 		break;
2980c0d06caSMauro Carvalho Chehab 	case 3:
2990c0d06caSMauro Carvalho Chehab 		val = ENABLE_THREE_BYTE;
3000c0d06caSMauro Carvalho Chehab 		break;
3010c0d06caSMauro Carvalho Chehab 	case 4:
3020c0d06caSMauro Carvalho Chehab 		val = ENABLE_FOUR_BYTE;
3030c0d06caSMauro Carvalho Chehab 		break;
3040c0d06caSMauro Carvalho Chehab 	default:
3050c0d06caSMauro Carvalho Chehab 		val = 0xFF;	/* invalid option */
3060c0d06caSMauro Carvalho Chehab 	}
3070c0d06caSMauro Carvalho Chehab 
3080c0d06caSMauro Carvalho Chehab 	if (val == 0xFF)
3090c0d06caSMauro Carvalho Chehab 		return -EINVAL;
3100c0d06caSMauro Carvalho Chehab 
3110c0d06caSMauro Carvalho Chehab 	ret = __usb_control_msg(dev, pipe, req,
3120c0d06caSMauro Carvalho Chehab 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3130c0d06caSMauro Carvalho Chehab 			      val, reg, buf, len, HZ);
3140c0d06caSMauro Carvalho Chehab 	return ret;
3150c0d06caSMauro Carvalho Chehab }
3160c0d06caSMauro Carvalho Chehab 
cx231xx_send_vendor_cmd(struct cx231xx * dev,struct VENDOR_REQUEST_IN * ven_req)3170c0d06caSMauro Carvalho Chehab int cx231xx_send_vendor_cmd(struct cx231xx *dev,
3180c0d06caSMauro Carvalho Chehab 				struct VENDOR_REQUEST_IN *ven_req)
3190c0d06caSMauro Carvalho Chehab {
3200c0d06caSMauro Carvalho Chehab 	int ret;
3210c0d06caSMauro Carvalho Chehab 	int pipe = 0;
3220c0d06caSMauro Carvalho Chehab 	int unsend_size = 0;
3230c0d06caSMauro Carvalho Chehab 	u8 *pdata;
3240c0d06caSMauro Carvalho Chehab 
3250c0d06caSMauro Carvalho Chehab 	if (dev->state & DEV_DISCONNECTED)
3260c0d06caSMauro Carvalho Chehab 		return -ENODEV;
3270c0d06caSMauro Carvalho Chehab 
3280c0d06caSMauro Carvalho Chehab 	if ((ven_req->wLength > URB_MAX_CTRL_SIZE))
3290c0d06caSMauro Carvalho Chehab 		return -EINVAL;
3300c0d06caSMauro Carvalho Chehab 
3310c0d06caSMauro Carvalho Chehab 	if (ven_req->direction)
3320c0d06caSMauro Carvalho Chehab 		pipe = usb_rcvctrlpipe(dev->udev, 0);
3330c0d06caSMauro Carvalho Chehab 	else
3340c0d06caSMauro Carvalho Chehab 		pipe = usb_sndctrlpipe(dev->udev, 0);
3350c0d06caSMauro Carvalho Chehab 
3360c0d06caSMauro Carvalho Chehab 	/*
3370c0d06caSMauro Carvalho Chehab 	 * If the cx23102 read more than 4 bytes with i2c bus,
3380c0d06caSMauro Carvalho Chehab 	 * need chop to 4 byte per request
3390c0d06caSMauro Carvalho Chehab 	 */
3400c0d06caSMauro Carvalho Chehab 	if ((ven_req->wLength > 4) && ((ven_req->bRequest == 0x4) ||
3410c0d06caSMauro Carvalho Chehab 					(ven_req->bRequest == 0x5) ||
3426c5da803SOleh Kravchenko 					(ven_req->bRequest == 0x6) ||
3436c5da803SOleh Kravchenko 
3446c5da803SOleh Kravchenko 					/* Internal Master 3 Bus can send
3456c5da803SOleh Kravchenko 					 * and receive only 4 bytes per time
3466c5da803SOleh Kravchenko 					 */
3476c5da803SOleh Kravchenko 					(ven_req->bRequest == 0x2))) {
3480c0d06caSMauro Carvalho Chehab 		unsend_size = 0;
3490c0d06caSMauro Carvalho Chehab 		pdata = ven_req->pBuff;
3500c0d06caSMauro Carvalho Chehab 
3510c0d06caSMauro Carvalho Chehab 
3520c0d06caSMauro Carvalho Chehab 		unsend_size = ven_req->wLength;
3530c0d06caSMauro Carvalho Chehab 
3540c0d06caSMauro Carvalho Chehab 		/* the first package */
3550c0d06caSMauro Carvalho Chehab 		ven_req->wValue = ven_req->wValue & 0xFFFB;
3560c0d06caSMauro Carvalho Chehab 		ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x2;
3570c0d06caSMauro Carvalho Chehab 		ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
3580c0d06caSMauro Carvalho Chehab 			ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3590c0d06caSMauro Carvalho Chehab 			ven_req->wValue, ven_req->wIndex, pdata,
3600c0d06caSMauro Carvalho Chehab 			0x0004, HZ);
3610c0d06caSMauro Carvalho Chehab 		unsend_size = unsend_size - 4;
3620c0d06caSMauro Carvalho Chehab 
3630c0d06caSMauro Carvalho Chehab 		/* the middle package */
3640c0d06caSMauro Carvalho Chehab 		ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x42;
3650c0d06caSMauro Carvalho Chehab 		while (unsend_size - 4 > 0) {
3660c0d06caSMauro Carvalho Chehab 			pdata = pdata + 4;
3670c0d06caSMauro Carvalho Chehab 			ret = __usb_control_msg(dev, pipe,
3680c0d06caSMauro Carvalho Chehab 				ven_req->bRequest,
3690c0d06caSMauro Carvalho Chehab 				ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3700c0d06caSMauro Carvalho Chehab 				ven_req->wValue, ven_req->wIndex, pdata,
3710c0d06caSMauro Carvalho Chehab 				0x0004, HZ);
3720c0d06caSMauro Carvalho Chehab 			unsend_size = unsend_size - 4;
3730c0d06caSMauro Carvalho Chehab 		}
3740c0d06caSMauro Carvalho Chehab 
3750c0d06caSMauro Carvalho Chehab 		/* the last package */
3760c0d06caSMauro Carvalho Chehab 		ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x40;
3770c0d06caSMauro Carvalho Chehab 		pdata = pdata + 4;
3780c0d06caSMauro Carvalho Chehab 		ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
3790c0d06caSMauro Carvalho Chehab 			ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3800c0d06caSMauro Carvalho Chehab 			ven_req->wValue, ven_req->wIndex, pdata,
3810c0d06caSMauro Carvalho Chehab 			unsend_size, HZ);
3820c0d06caSMauro Carvalho Chehab 	} else {
3830c0d06caSMauro Carvalho Chehab 		ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
3840c0d06caSMauro Carvalho Chehab 				ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3850c0d06caSMauro Carvalho Chehab 				ven_req->wValue, ven_req->wIndex,
3860c0d06caSMauro Carvalho Chehab 				ven_req->pBuff, ven_req->wLength, HZ);
3870c0d06caSMauro Carvalho Chehab 	}
3880c0d06caSMauro Carvalho Chehab 
3890c0d06caSMauro Carvalho Chehab 	return ret;
3900c0d06caSMauro Carvalho Chehab }
3910c0d06caSMauro Carvalho Chehab 
3920c0d06caSMauro Carvalho Chehab /*
3930c0d06caSMauro Carvalho Chehab  * cx231xx_write_ctrl_reg()
3940c0d06caSMauro Carvalho Chehab  * sends data to the usb device, specifying bRequest
3950c0d06caSMauro Carvalho Chehab  */
cx231xx_write_ctrl_reg(struct cx231xx * dev,u8 req,u16 reg,char * buf,int len)3960c0d06caSMauro Carvalho Chehab int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf,
3970c0d06caSMauro Carvalho Chehab 			   int len)
3980c0d06caSMauro Carvalho Chehab {
3990c0d06caSMauro Carvalho Chehab 	u8 val = 0;
4000c0d06caSMauro Carvalho Chehab 	int ret;
4010c0d06caSMauro Carvalho Chehab 	int pipe = usb_sndctrlpipe(dev->udev, 0);
4020c0d06caSMauro Carvalho Chehab 
4030c0d06caSMauro Carvalho Chehab 	if (dev->state & DEV_DISCONNECTED)
4040c0d06caSMauro Carvalho Chehab 		return -ENODEV;
4050c0d06caSMauro Carvalho Chehab 
4060c0d06caSMauro Carvalho Chehab 	if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
4070c0d06caSMauro Carvalho Chehab 		return -EINVAL;
4080c0d06caSMauro Carvalho Chehab 
4090c0d06caSMauro Carvalho Chehab 	switch (len) {
4100c0d06caSMauro Carvalho Chehab 	case 1:
4110c0d06caSMauro Carvalho Chehab 		val = ENABLE_ONE_BYTE;
4120c0d06caSMauro Carvalho Chehab 		break;
4130c0d06caSMauro Carvalho Chehab 	case 2:
4140c0d06caSMauro Carvalho Chehab 		val = ENABLE_TWE_BYTE;
4150c0d06caSMauro Carvalho Chehab 		break;
4160c0d06caSMauro Carvalho Chehab 	case 3:
4170c0d06caSMauro Carvalho Chehab 		val = ENABLE_THREE_BYTE;
4180c0d06caSMauro Carvalho Chehab 		break;
4190c0d06caSMauro Carvalho Chehab 	case 4:
4200c0d06caSMauro Carvalho Chehab 		val = ENABLE_FOUR_BYTE;
4210c0d06caSMauro Carvalho Chehab 		break;
4220c0d06caSMauro Carvalho Chehab 	default:
4230c0d06caSMauro Carvalho Chehab 		val = 0xFF;	/* invalid option */
4240c0d06caSMauro Carvalho Chehab 	}
4250c0d06caSMauro Carvalho Chehab 
4260c0d06caSMauro Carvalho Chehab 	if (val == 0xFF)
4270c0d06caSMauro Carvalho Chehab 		return -EINVAL;
4280c0d06caSMauro Carvalho Chehab 
4290c0d06caSMauro Carvalho Chehab 	if (reg_debug) {
4300c0d06caSMauro Carvalho Chehab 		int byte;
4310c0d06caSMauro Carvalho Chehab 
432ba1da97eSMauro Carvalho Chehab 		cx231xx_isocdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
4330c0d06caSMauro Carvalho Chehab 			pipe,
4340c0d06caSMauro Carvalho Chehab 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
4350c0d06caSMauro Carvalho Chehab 			req, 0, val, reg & 0xff,
4360c0d06caSMauro Carvalho Chehab 			reg >> 8, len & 0xff, len >> 8);
4370c0d06caSMauro Carvalho Chehab 
4380c0d06caSMauro Carvalho Chehab 		for (byte = 0; byte < len; byte++)
4390c0d06caSMauro Carvalho Chehab 			cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
4400c0d06caSMauro Carvalho Chehab 		cx231xx_isocdbg("\n");
4410c0d06caSMauro Carvalho Chehab 	}
4420c0d06caSMauro Carvalho Chehab 
4430c0d06caSMauro Carvalho Chehab 	ret = __usb_control_msg(dev, pipe, req,
4440c0d06caSMauro Carvalho Chehab 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
4450c0d06caSMauro Carvalho Chehab 			      val, reg, buf, len, HZ);
4460c0d06caSMauro Carvalho Chehab 
4470c0d06caSMauro Carvalho Chehab 	return ret;
4480c0d06caSMauro Carvalho Chehab }
4490c0d06caSMauro Carvalho Chehab 
4500c0d06caSMauro Carvalho Chehab /****************************************************************
4510c0d06caSMauro Carvalho Chehab *           USB Alternate Setting functions                     *
4520c0d06caSMauro Carvalho Chehab *****************************************************************/
4530c0d06caSMauro Carvalho Chehab 
cx231xx_set_video_alternate(struct cx231xx * dev)4540c0d06caSMauro Carvalho Chehab int cx231xx_set_video_alternate(struct cx231xx *dev)
4550c0d06caSMauro Carvalho Chehab {
4560c0d06caSMauro Carvalho Chehab 	int errCode, prev_alt = dev->video_mode.alt;
4570c0d06caSMauro Carvalho Chehab 	unsigned int min_pkt_size = dev->width * 2 + 4;
4580c0d06caSMauro Carvalho Chehab 	u32 usb_interface_index = 0;
4590c0d06caSMauro Carvalho Chehab 
4600c0d06caSMauro Carvalho Chehab 	/* When image size is bigger than a certain value,
4610c0d06caSMauro Carvalho Chehab 	   the frame size should be increased, otherwise, only
4620c0d06caSMauro Carvalho Chehab 	   green screen will be received.
4630c0d06caSMauro Carvalho Chehab 	 */
4640c0d06caSMauro Carvalho Chehab 	if (dev->width * 2 * dev->height > 720 * 240 * 2)
4650c0d06caSMauro Carvalho Chehab 		min_pkt_size *= 2;
4660c0d06caSMauro Carvalho Chehab 
4670c0d06caSMauro Carvalho Chehab 	if (dev->width > 360) {
4680c0d06caSMauro Carvalho Chehab 		/* resolutions: 720,704,640 */
4690c0d06caSMauro Carvalho Chehab 		dev->video_mode.alt = 3;
4700c0d06caSMauro Carvalho Chehab 	} else if (dev->width > 180) {
4710c0d06caSMauro Carvalho Chehab 		/* resolutions: 360,352,320,240 */
4720c0d06caSMauro Carvalho Chehab 		dev->video_mode.alt = 2;
4730c0d06caSMauro Carvalho Chehab 	} else if (dev->width > 0) {
4740c0d06caSMauro Carvalho Chehab 		/* resolutions: 180,176,160,128,88 */
4750c0d06caSMauro Carvalho Chehab 		dev->video_mode.alt = 1;
4760c0d06caSMauro Carvalho Chehab 	} else {
4770c0d06caSMauro Carvalho Chehab 		/* Change to alt0 BULK to release USB bandwidth */
4780c0d06caSMauro Carvalho Chehab 		dev->video_mode.alt = 0;
4790c0d06caSMauro Carvalho Chehab 	}
4800c0d06caSMauro Carvalho Chehab 
4810c0d06caSMauro Carvalho Chehab 	if (dev->USE_ISO == 0)
4820c0d06caSMauro Carvalho Chehab 		dev->video_mode.alt = 0;
4830c0d06caSMauro Carvalho Chehab 
4840c0d06caSMauro Carvalho Chehab 	cx231xx_coredbg("dev->video_mode.alt= %d\n", dev->video_mode.alt);
4850c0d06caSMauro Carvalho Chehab 
4860c0d06caSMauro Carvalho Chehab 	/* Get the correct video interface Index */
4870c0d06caSMauro Carvalho Chehab 	usb_interface_index =
4880c0d06caSMauro Carvalho Chehab 	    dev->current_pcb_config.hs_config_info[0].interface_info.
4890c0d06caSMauro Carvalho Chehab 	    video_index + 1;
4900c0d06caSMauro Carvalho Chehab 
4910c0d06caSMauro Carvalho Chehab 	if (dev->video_mode.alt != prev_alt) {
4920c0d06caSMauro Carvalho Chehab 		cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
4930c0d06caSMauro Carvalho Chehab 				min_pkt_size, dev->video_mode.alt);
4940c0d06caSMauro Carvalho Chehab 
4950c0d06caSMauro Carvalho Chehab 		if (dev->video_mode.alt_max_pkt_size != NULL)
4960c0d06caSMauro Carvalho Chehab 			dev->video_mode.max_pkt_size =
4970c0d06caSMauro Carvalho Chehab 			dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
4980c0d06caSMauro Carvalho Chehab 		cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
4990c0d06caSMauro Carvalho Chehab 				dev->video_mode.alt,
5000c0d06caSMauro Carvalho Chehab 				dev->video_mode.max_pkt_size);
5010c0d06caSMauro Carvalho Chehab 		errCode =
5020c0d06caSMauro Carvalho Chehab 		    usb_set_interface(dev->udev, usb_interface_index,
5030c0d06caSMauro Carvalho Chehab 				      dev->video_mode.alt);
5040c0d06caSMauro Carvalho Chehab 		if (errCode < 0) {
505336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
506b7085c08SMauro Carvalho Chehab 				"cannot change alt number to %d (error=%i)\n",
5070c0d06caSMauro Carvalho Chehab 				dev->video_mode.alt, errCode);
5080c0d06caSMauro Carvalho Chehab 			return errCode;
5090c0d06caSMauro Carvalho Chehab 		}
5100c0d06caSMauro Carvalho Chehab 	}
5110c0d06caSMauro Carvalho Chehab 	return 0;
5120c0d06caSMauro Carvalho Chehab }
5130c0d06caSMauro Carvalho Chehab 
cx231xx_set_alt_setting(struct cx231xx * dev,u8 index,u8 alt)5140c0d06caSMauro Carvalho Chehab int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
5150c0d06caSMauro Carvalho Chehab {
5160c0d06caSMauro Carvalho Chehab 	int status = 0;
5170c0d06caSMauro Carvalho Chehab 	u32 usb_interface_index = 0;
5180c0d06caSMauro Carvalho Chehab 	u32 max_pkt_size = 0;
5190c0d06caSMauro Carvalho Chehab 
5200c0d06caSMauro Carvalho Chehab 	switch (index) {
5210c0d06caSMauro Carvalho Chehab 	case INDEX_TS1:
5220c0d06caSMauro Carvalho Chehab 		usb_interface_index =
5230c0d06caSMauro Carvalho Chehab 		    dev->current_pcb_config.hs_config_info[0].interface_info.
5240c0d06caSMauro Carvalho Chehab 		    ts1_index + 1;
5250c0d06caSMauro Carvalho Chehab 		dev->ts1_mode.alt = alt;
5260c0d06caSMauro Carvalho Chehab 		if (dev->ts1_mode.alt_max_pkt_size != NULL)
5270c0d06caSMauro Carvalho Chehab 			max_pkt_size = dev->ts1_mode.max_pkt_size =
5280c0d06caSMauro Carvalho Chehab 			    dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt];
5290c0d06caSMauro Carvalho Chehab 		break;
5300c0d06caSMauro Carvalho Chehab 	case INDEX_TS2:
5310c0d06caSMauro Carvalho Chehab 		usb_interface_index =
5320c0d06caSMauro Carvalho Chehab 		    dev->current_pcb_config.hs_config_info[0].interface_info.
5330c0d06caSMauro Carvalho Chehab 		    ts2_index + 1;
5340c0d06caSMauro Carvalho Chehab 		break;
5350c0d06caSMauro Carvalho Chehab 	case INDEX_AUDIO:
5360c0d06caSMauro Carvalho Chehab 		usb_interface_index =
5370c0d06caSMauro Carvalho Chehab 		    dev->current_pcb_config.hs_config_info[0].interface_info.
5380c0d06caSMauro Carvalho Chehab 		    audio_index + 1;
5390c0d06caSMauro Carvalho Chehab 		dev->adev.alt = alt;
5400c0d06caSMauro Carvalho Chehab 		if (dev->adev.alt_max_pkt_size != NULL)
5410c0d06caSMauro Carvalho Chehab 			max_pkt_size = dev->adev.max_pkt_size =
5420c0d06caSMauro Carvalho Chehab 			    dev->adev.alt_max_pkt_size[dev->adev.alt];
5430c0d06caSMauro Carvalho Chehab 		break;
5440c0d06caSMauro Carvalho Chehab 	case INDEX_VIDEO:
5450c0d06caSMauro Carvalho Chehab 		usb_interface_index =
5460c0d06caSMauro Carvalho Chehab 		    dev->current_pcb_config.hs_config_info[0].interface_info.
5470c0d06caSMauro Carvalho Chehab 		    video_index + 1;
5480c0d06caSMauro Carvalho Chehab 		dev->video_mode.alt = alt;
5490c0d06caSMauro Carvalho Chehab 		if (dev->video_mode.alt_max_pkt_size != NULL)
5500c0d06caSMauro Carvalho Chehab 			max_pkt_size = dev->video_mode.max_pkt_size =
5510c0d06caSMauro Carvalho Chehab 			    dev->video_mode.alt_max_pkt_size[dev->video_mode.
5520c0d06caSMauro Carvalho Chehab 							     alt];
5530c0d06caSMauro Carvalho Chehab 		break;
5540c0d06caSMauro Carvalho Chehab 	case INDEX_VANC:
5550c0d06caSMauro Carvalho Chehab 		if (dev->board.no_alt_vanc)
5560c0d06caSMauro Carvalho Chehab 			return 0;
5570c0d06caSMauro Carvalho Chehab 		usb_interface_index =
5580c0d06caSMauro Carvalho Chehab 		    dev->current_pcb_config.hs_config_info[0].interface_info.
5590c0d06caSMauro Carvalho Chehab 		    vanc_index + 1;
5600c0d06caSMauro Carvalho Chehab 		dev->vbi_mode.alt = alt;
5610c0d06caSMauro Carvalho Chehab 		if (dev->vbi_mode.alt_max_pkt_size != NULL)
5620c0d06caSMauro Carvalho Chehab 			max_pkt_size = dev->vbi_mode.max_pkt_size =
5630c0d06caSMauro Carvalho Chehab 			    dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt];
5640c0d06caSMauro Carvalho Chehab 		break;
5650c0d06caSMauro Carvalho Chehab 	case INDEX_HANC:
5660c0d06caSMauro Carvalho Chehab 		usb_interface_index =
5670c0d06caSMauro Carvalho Chehab 		    dev->current_pcb_config.hs_config_info[0].interface_info.
5680c0d06caSMauro Carvalho Chehab 		    hanc_index + 1;
5690c0d06caSMauro Carvalho Chehab 		dev->sliced_cc_mode.alt = alt;
5700c0d06caSMauro Carvalho Chehab 		if (dev->sliced_cc_mode.alt_max_pkt_size != NULL)
5710c0d06caSMauro Carvalho Chehab 			max_pkt_size = dev->sliced_cc_mode.max_pkt_size =
5720c0d06caSMauro Carvalho Chehab 			    dev->sliced_cc_mode.alt_max_pkt_size[dev->
5730c0d06caSMauro Carvalho Chehab 								 sliced_cc_mode.
5740c0d06caSMauro Carvalho Chehab 								 alt];
5750c0d06caSMauro Carvalho Chehab 		break;
5760c0d06caSMauro Carvalho Chehab 	default:
5770c0d06caSMauro Carvalho Chehab 		break;
5780c0d06caSMauro Carvalho Chehab 	}
5790c0d06caSMauro Carvalho Chehab 
5800c0d06caSMauro Carvalho Chehab 	if (alt > 0 && max_pkt_size == 0) {
581336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
582b7085c08SMauro Carvalho Chehab 			"can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
5830c0d06caSMauro Carvalho Chehab 			usb_interface_index, alt);
5840c0d06caSMauro Carvalho Chehab 		/*To workaround error number=-71 on EP0 for videograbber,
5850c0d06caSMauro Carvalho Chehab 		 need add following codes.*/
5860c0d06caSMauro Carvalho Chehab 		if (dev->board.no_alt_vanc)
5870c0d06caSMauro Carvalho Chehab 			return -1;
5880c0d06caSMauro Carvalho Chehab 	}
5890c0d06caSMauro Carvalho Chehab 
590ba1da97eSMauro Carvalho Chehab 	cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u,Interface = %d\n",
591ba1da97eSMauro Carvalho Chehab 			alt, max_pkt_size,
5920c0d06caSMauro Carvalho Chehab 			usb_interface_index);
5930c0d06caSMauro Carvalho Chehab 
5940c0d06caSMauro Carvalho Chehab 	if (usb_interface_index > 0) {
5950c0d06caSMauro Carvalho Chehab 		status = usb_set_interface(dev->udev, usb_interface_index, alt);
5960c0d06caSMauro Carvalho Chehab 		if (status < 0) {
597336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
598b7085c08SMauro Carvalho Chehab 				"can't change interface %d alt no. to %d (err=%i)\n",
5990c0d06caSMauro Carvalho Chehab 				usb_interface_index, alt, status);
6000c0d06caSMauro Carvalho Chehab 			return status;
6010c0d06caSMauro Carvalho Chehab 		}
6020c0d06caSMauro Carvalho Chehab 	}
6030c0d06caSMauro Carvalho Chehab 
6040c0d06caSMauro Carvalho Chehab 	return status;
6050c0d06caSMauro Carvalho Chehab }
6060c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting);
6070c0d06caSMauro Carvalho Chehab 
cx231xx_gpio_set(struct cx231xx * dev,struct cx231xx_reg_seq * gpio)6080c0d06caSMauro Carvalho Chehab int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio)
6090c0d06caSMauro Carvalho Chehab {
6100c0d06caSMauro Carvalho Chehab 	int rc = 0;
6110c0d06caSMauro Carvalho Chehab 
6120c0d06caSMauro Carvalho Chehab 	if (!gpio)
6130c0d06caSMauro Carvalho Chehab 		return rc;
6140c0d06caSMauro Carvalho Chehab 
6150c0d06caSMauro Carvalho Chehab 	/* Send GPIO reset sequences specified at board entry */
6160c0d06caSMauro Carvalho Chehab 	while (gpio->sleep >= 0) {
6170c0d06caSMauro Carvalho Chehab 		rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
6180c0d06caSMauro Carvalho Chehab 		if (rc < 0)
6190c0d06caSMauro Carvalho Chehab 			return rc;
6200c0d06caSMauro Carvalho Chehab 
6210c0d06caSMauro Carvalho Chehab 		if (gpio->sleep > 0)
6220c0d06caSMauro Carvalho Chehab 			msleep(gpio->sleep);
6230c0d06caSMauro Carvalho Chehab 
6240c0d06caSMauro Carvalho Chehab 		gpio++;
6250c0d06caSMauro Carvalho Chehab 	}
6260c0d06caSMauro Carvalho Chehab 	return rc;
6270c0d06caSMauro Carvalho Chehab }
6280c0d06caSMauro Carvalho Chehab 
cx231xx_demod_reset(struct cx231xx * dev)6290c0d06caSMauro Carvalho Chehab int cx231xx_demod_reset(struct cx231xx *dev)
6300c0d06caSMauro Carvalho Chehab {
6310c0d06caSMauro Carvalho Chehab 
6320c0d06caSMauro Carvalho Chehab 	u8 status = 0;
6330c0d06caSMauro Carvalho Chehab 	u8 value[4] = { 0, 0, 0, 0 };
6340c0d06caSMauro Carvalho Chehab 
6350c0d06caSMauro Carvalho Chehab 	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
6360c0d06caSMauro Carvalho Chehab 				 value, 4);
6370c0d06caSMauro Carvalho Chehab 
6380c0d06caSMauro Carvalho Chehab 	cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
6390c0d06caSMauro Carvalho Chehab 			value[0], value[1], value[2], value[3]);
6400c0d06caSMauro Carvalho Chehab 
6410c0d06caSMauro Carvalho Chehab 	cx231xx_coredbg("Enter cx231xx_demod_reset()\n");
6420c0d06caSMauro Carvalho Chehab 
6430c0d06caSMauro Carvalho Chehab 	value[1] = (u8) 0x3;
6440c0d06caSMauro Carvalho Chehab 	status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
6450c0d06caSMauro Carvalho Chehab 					PWR_CTL_EN, value, 4);
6460c0d06caSMauro Carvalho Chehab 	msleep(10);
6470c0d06caSMauro Carvalho Chehab 
6480c0d06caSMauro Carvalho Chehab 	value[1] = (u8) 0x0;
6490c0d06caSMauro Carvalho Chehab 	status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
6500c0d06caSMauro Carvalho Chehab 					PWR_CTL_EN, value, 4);
6510c0d06caSMauro Carvalho Chehab 	msleep(10);
6520c0d06caSMauro Carvalho Chehab 
6530c0d06caSMauro Carvalho Chehab 	value[1] = (u8) 0x3;
6540c0d06caSMauro Carvalho Chehab 	status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
6550c0d06caSMauro Carvalho Chehab 					PWR_CTL_EN, value, 4);
6560c0d06caSMauro Carvalho Chehab 	msleep(10);
6570c0d06caSMauro Carvalho Chehab 
6580c0d06caSMauro Carvalho Chehab 	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
6590c0d06caSMauro Carvalho Chehab 				 value, 4);
6600c0d06caSMauro Carvalho Chehab 
6610c0d06caSMauro Carvalho Chehab 	cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
6620c0d06caSMauro Carvalho Chehab 			value[0], value[1], value[2], value[3]);
6630c0d06caSMauro Carvalho Chehab 
6640c0d06caSMauro Carvalho Chehab 	return status;
6650c0d06caSMauro Carvalho Chehab }
6660c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_demod_reset);
is_fw_load(struct cx231xx * dev)6670c0d06caSMauro Carvalho Chehab int is_fw_load(struct cx231xx *dev)
6680c0d06caSMauro Carvalho Chehab {
6690c0d06caSMauro Carvalho Chehab 	return cx231xx_check_fw(dev);
6700c0d06caSMauro Carvalho Chehab }
6710c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(is_fw_load);
6720c0d06caSMauro Carvalho Chehab 
cx231xx_set_mode(struct cx231xx * dev,enum cx231xx_mode set_mode)6730c0d06caSMauro Carvalho Chehab int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
6740c0d06caSMauro Carvalho Chehab {
6750c0d06caSMauro Carvalho Chehab 	int errCode = 0;
6760c0d06caSMauro Carvalho Chehab 
6770c0d06caSMauro Carvalho Chehab 	if (dev->mode == set_mode)
6780c0d06caSMauro Carvalho Chehab 		return 0;
6790c0d06caSMauro Carvalho Chehab 
6800c0d06caSMauro Carvalho Chehab 	if (set_mode == CX231XX_SUSPEND) {
6810c0d06caSMauro Carvalho Chehab 		/* Set the chip in power saving mode */
6820c0d06caSMauro Carvalho Chehab 		dev->mode = set_mode;
6830c0d06caSMauro Carvalho Chehab 	}
6840c0d06caSMauro Carvalho Chehab 
6850c0d06caSMauro Carvalho Chehab 	/* Resource is locked */
6860c0d06caSMauro Carvalho Chehab 	if (dev->mode != CX231XX_SUSPEND)
6870c0d06caSMauro Carvalho Chehab 		return -EINVAL;
6880c0d06caSMauro Carvalho Chehab 
6890c0d06caSMauro Carvalho Chehab 	dev->mode = set_mode;
6900c0d06caSMauro Carvalho Chehab 
6910c0d06caSMauro Carvalho Chehab 	if (dev->mode == CX231XX_DIGITAL_MODE)/* Set Digital power mode */ {
6920c0d06caSMauro Carvalho Chehab 	/* set AGC mode to Digital */
6930c0d06caSMauro Carvalho Chehab 		switch (dev->model) {
6940c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_CARRAERA:
6950c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_RDE_250:
6960c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_SHELBY:
6970c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_RDU_250:
6980c0d06caSMauro Carvalho Chehab 		errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
6990c0d06caSMauro Carvalho Chehab 			break;
7000c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_RDE_253S:
7010c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_RDU_253S:
70224b923f0SMauro Carvalho Chehab 		case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
7030c0d06caSMauro Carvalho Chehab 			errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
7040c0d06caSMauro Carvalho Chehab 			break;
7050c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_HAUPPAUGE_EXETER:
706dd2e7dd2SMatthias Schwarzott 		case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
7070c0d06caSMauro Carvalho Chehab 			errCode = cx231xx_set_power_mode(dev,
7080c0d06caSMauro Carvalho Chehab 						POLARIS_AVMODE_DIGITAL);
7090c0d06caSMauro Carvalho Chehab 			break;
7100c0d06caSMauro Carvalho Chehab 		default:
7110c0d06caSMauro Carvalho Chehab 			break;
7120c0d06caSMauro Carvalho Chehab 		}
7130c0d06caSMauro Carvalho Chehab 	} else/* Set Analog Power mode */ {
7140c0d06caSMauro Carvalho Chehab 	/* set AGC mode to Analog */
7150c0d06caSMauro Carvalho Chehab 		switch (dev->model) {
7160c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_CARRAERA:
7170c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_RDE_250:
7180c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_SHELBY:
7190c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_RDU_250:
7200c0d06caSMauro Carvalho Chehab 		errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
7210c0d06caSMauro Carvalho Chehab 			break;
7220c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_RDE_253S:
7230c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_CNXT_RDU_253S:
7240c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_HAUPPAUGE_EXETER:
725dd2e7dd2SMatthias Schwarzott 		case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
7260c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
7270c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
7280c0d06caSMauro Carvalho Chehab 		case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
7290c0d06caSMauro Carvalho Chehab 			errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
7300c0d06caSMauro Carvalho Chehab 			break;
7310c0d06caSMauro Carvalho Chehab 		default:
7320c0d06caSMauro Carvalho Chehab 			break;
7330c0d06caSMauro Carvalho Chehab 		}
7340c0d06caSMauro Carvalho Chehab 	}
7350c0d06caSMauro Carvalho Chehab 
736dc4af782SMauro Carvalho Chehab 	if (errCode < 0) {
737dc4af782SMauro Carvalho Chehab 		dev_err(dev->dev, "Failed to set devmode to %s: error: %i",
738dc4af782SMauro Carvalho Chehab 			dev->mode == CX231XX_DIGITAL_MODE ? "digital" : "analog",
739dc4af782SMauro Carvalho Chehab 			errCode);
740dc4af782SMauro Carvalho Chehab 		return errCode;
741dc4af782SMauro Carvalho Chehab 	}
742dc4af782SMauro Carvalho Chehab 
743dc4af782SMauro Carvalho Chehab 	return 0;
7440c0d06caSMauro Carvalho Chehab }
7450c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_set_mode);
7460c0d06caSMauro Carvalho Chehab 
cx231xx_ep5_bulkout(struct cx231xx * dev,u8 * firmware,u16 size)7470c0d06caSMauro Carvalho Chehab int cx231xx_ep5_bulkout(struct cx231xx *dev, u8 *firmware, u16 size)
7480c0d06caSMauro Carvalho Chehab {
7490c0d06caSMauro Carvalho Chehab 	int errCode = 0;
75020e01b26SDan Carpenter 	int actlen = -1;
75120e01b26SDan Carpenter 	int ret = -ENOMEM;
7520c0d06caSMauro Carvalho Chehab 	u32 *buffer;
7530c0d06caSMauro Carvalho Chehab 
7540c0d06caSMauro Carvalho Chehab 	buffer = kzalloc(4096, GFP_KERNEL);
755ed0e3729SMauro Carvalho Chehab 	if (buffer == NULL)
7560c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
7570c0d06caSMauro Carvalho Chehab 	memcpy(&buffer[0], firmware, 4096);
7580c0d06caSMauro Carvalho Chehab 
7590c0d06caSMauro Carvalho Chehab 	ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 5),
7600c0d06caSMauro Carvalho Chehab 			buffer, 4096, &actlen, 2000);
7610c0d06caSMauro Carvalho Chehab 
7620c0d06caSMauro Carvalho Chehab 	if (ret)
763336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
764b7085c08SMauro Carvalho Chehab 			"bulk message failed: %d (%d/%d)", ret,
7650c0d06caSMauro Carvalho Chehab 			size, actlen);
7660c0d06caSMauro Carvalho Chehab 	else {
7670c0d06caSMauro Carvalho Chehab 		errCode = actlen != size ? -1 : 0;
7680c0d06caSMauro Carvalho Chehab 	}
7690c0d06caSMauro Carvalho Chehab 	kfree(buffer);
7700c0d06caSMauro Carvalho Chehab 	return errCode;
7710c0d06caSMauro Carvalho Chehab }
7720c0d06caSMauro Carvalho Chehab 
7730c0d06caSMauro Carvalho Chehab /*****************************************************************
7740c0d06caSMauro Carvalho Chehab *                URB Streaming functions                         *
7750c0d06caSMauro Carvalho Chehab ******************************************************************/
7760c0d06caSMauro Carvalho Chehab 
7770c0d06caSMauro Carvalho Chehab /*
7780c0d06caSMauro Carvalho Chehab  * IRQ callback, called by URB callback
7790c0d06caSMauro Carvalho Chehab  */
cx231xx_isoc_irq_callback(struct urb * urb)7800c0d06caSMauro Carvalho Chehab static void cx231xx_isoc_irq_callback(struct urb *urb)
7810c0d06caSMauro Carvalho Chehab {
7820c0d06caSMauro Carvalho Chehab 	struct cx231xx_dmaqueue *dma_q = urb->context;
7830c0d06caSMauro Carvalho Chehab 	struct cx231xx_video_mode *vmode =
7840c0d06caSMauro Carvalho Chehab 	    container_of(dma_q, struct cx231xx_video_mode, vidq);
7850c0d06caSMauro Carvalho Chehab 	struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
786aa53bf0bSSebastian Andrzej Siewior 	unsigned long flags;
7870c0d06caSMauro Carvalho Chehab 	int i;
7880c0d06caSMauro Carvalho Chehab 
7890c0d06caSMauro Carvalho Chehab 	switch (urb->status) {
7900c0d06caSMauro Carvalho Chehab 	case 0:		/* success */
7910c0d06caSMauro Carvalho Chehab 	case -ETIMEDOUT:	/* NAK */
7920c0d06caSMauro Carvalho Chehab 		break;
7930c0d06caSMauro Carvalho Chehab 	case -ECONNRESET:	/* kill */
7940c0d06caSMauro Carvalho Chehab 	case -ENOENT:
7950c0d06caSMauro Carvalho Chehab 	case -ESHUTDOWN:
7960c0d06caSMauro Carvalho Chehab 		return;
7970c0d06caSMauro Carvalho Chehab 	default:		/* error */
79869b17abfSTerry Heo 		cx231xx_isocdbg("urb completion error %d.\n", urb->status);
7990c0d06caSMauro Carvalho Chehab 		break;
8000c0d06caSMauro Carvalho Chehab 	}
8010c0d06caSMauro Carvalho Chehab 
8020c0d06caSMauro Carvalho Chehab 	/* Copy data from URB */
803aa53bf0bSSebastian Andrzej Siewior 	spin_lock_irqsave(&dev->video_mode.slock, flags);
8040c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
805aa53bf0bSSebastian Andrzej Siewior 	spin_unlock_irqrestore(&dev->video_mode.slock, flags);
8060c0d06caSMauro Carvalho Chehab 
8070c0d06caSMauro Carvalho Chehab 	/* Reset urb buffers */
8080c0d06caSMauro Carvalho Chehab 	for (i = 0; i < urb->number_of_packets; i++) {
8090c0d06caSMauro Carvalho Chehab 		urb->iso_frame_desc[i].status = 0;
8100c0d06caSMauro Carvalho Chehab 		urb->iso_frame_desc[i].actual_length = 0;
8110c0d06caSMauro Carvalho Chehab 	}
8120c0d06caSMauro Carvalho Chehab 
8130c0d06caSMauro Carvalho Chehab 	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
8140c0d06caSMauro Carvalho Chehab 	if (urb->status) {
8150c0d06caSMauro Carvalho Chehab 		cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
8160c0d06caSMauro Carvalho Chehab 				urb->status);
8170c0d06caSMauro Carvalho Chehab 	}
8180c0d06caSMauro Carvalho Chehab }
8190c0d06caSMauro Carvalho Chehab /*****************************************************************
8200c0d06caSMauro Carvalho Chehab *                URB Streaming functions                         *
8210c0d06caSMauro Carvalho Chehab ******************************************************************/
8220c0d06caSMauro Carvalho Chehab 
8230c0d06caSMauro Carvalho Chehab /*
8240c0d06caSMauro Carvalho Chehab  * IRQ callback, called by URB callback
8250c0d06caSMauro Carvalho Chehab  */
cx231xx_bulk_irq_callback(struct urb * urb)8260c0d06caSMauro Carvalho Chehab static void cx231xx_bulk_irq_callback(struct urb *urb)
8270c0d06caSMauro Carvalho Chehab {
8280c0d06caSMauro Carvalho Chehab 	struct cx231xx_dmaqueue *dma_q = urb->context;
8290c0d06caSMauro Carvalho Chehab 	struct cx231xx_video_mode *vmode =
8300c0d06caSMauro Carvalho Chehab 	    container_of(dma_q, struct cx231xx_video_mode, vidq);
8310c0d06caSMauro Carvalho Chehab 	struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
832aa53bf0bSSebastian Andrzej Siewior 	unsigned long flags;
8330c0d06caSMauro Carvalho Chehab 
8340c0d06caSMauro Carvalho Chehab 	switch (urb->status) {
8350c0d06caSMauro Carvalho Chehab 	case 0:		/* success */
8360c0d06caSMauro Carvalho Chehab 	case -ETIMEDOUT:	/* NAK */
8370c0d06caSMauro Carvalho Chehab 		break;
8380c0d06caSMauro Carvalho Chehab 	case -ECONNRESET:	/* kill */
8390c0d06caSMauro Carvalho Chehab 	case -ENOENT:
8400c0d06caSMauro Carvalho Chehab 	case -ESHUTDOWN:
8410c0d06caSMauro Carvalho Chehab 		return;
84269b17abfSTerry Heo 	case -EPIPE:		/* stall */
84369b17abfSTerry Heo 		cx231xx_isocdbg("urb completion error - device is stalled.\n");
84469b17abfSTerry Heo 		return;
8450c0d06caSMauro Carvalho Chehab 	default:		/* error */
84669b17abfSTerry Heo 		cx231xx_isocdbg("urb completion error %d.\n", urb->status);
8470c0d06caSMauro Carvalho Chehab 		break;
8480c0d06caSMauro Carvalho Chehab 	}
8490c0d06caSMauro Carvalho Chehab 
8500c0d06caSMauro Carvalho Chehab 	/* Copy data from URB */
851aa53bf0bSSebastian Andrzej Siewior 	spin_lock_irqsave(&dev->video_mode.slock, flags);
8520c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.bulk_copy(dev, urb);
853aa53bf0bSSebastian Andrzej Siewior 	spin_unlock_irqrestore(&dev->video_mode.slock, flags);
8540c0d06caSMauro Carvalho Chehab 
8550c0d06caSMauro Carvalho Chehab 	/* Reset urb buffers */
8560c0d06caSMauro Carvalho Chehab 	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
8570c0d06caSMauro Carvalho Chehab 	if (urb->status) {
8580c0d06caSMauro Carvalho Chehab 		cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
8590c0d06caSMauro Carvalho Chehab 				urb->status);
8600c0d06caSMauro Carvalho Chehab 	}
8610c0d06caSMauro Carvalho Chehab }
8620c0d06caSMauro Carvalho Chehab /*
8630c0d06caSMauro Carvalho Chehab  * Stop and Deallocate URBs
8640c0d06caSMauro Carvalho Chehab  */
cx231xx_uninit_isoc(struct cx231xx * dev)8650c0d06caSMauro Carvalho Chehab void cx231xx_uninit_isoc(struct cx231xx *dev)
8660c0d06caSMauro Carvalho Chehab {
8670c0d06caSMauro Carvalho Chehab 	struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
8680c0d06caSMauro Carvalho Chehab 	struct urb *urb;
8690c0d06caSMauro Carvalho Chehab 	int i;
87069b17abfSTerry Heo 	bool broken_pipe = false;
8710c0d06caSMauro Carvalho Chehab 
8720c0d06caSMauro Carvalho Chehab 	cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
8730c0d06caSMauro Carvalho Chehab 
8740c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.nfields = -1;
8750c0d06caSMauro Carvalho Chehab 	for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
8760c0d06caSMauro Carvalho Chehab 		urb = dev->video_mode.isoc_ctl.urb[i];
8770c0d06caSMauro Carvalho Chehab 		if (urb) {
8780c0d06caSMauro Carvalho Chehab 			if (!irqs_disabled())
8790c0d06caSMauro Carvalho Chehab 				usb_kill_urb(urb);
8800c0d06caSMauro Carvalho Chehab 			else
8810c0d06caSMauro Carvalho Chehab 				usb_unlink_urb(urb);
8820c0d06caSMauro Carvalho Chehab 
8830c0d06caSMauro Carvalho Chehab 			if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
8840c0d06caSMauro Carvalho Chehab 				usb_free_coherent(dev->udev,
8850c0d06caSMauro Carvalho Chehab 						  urb->transfer_buffer_length,
8860c0d06caSMauro Carvalho Chehab 						  dev->video_mode.isoc_ctl.
8870c0d06caSMauro Carvalho Chehab 						  transfer_buffer[i],
8880c0d06caSMauro Carvalho Chehab 						  urb->transfer_dma);
8890c0d06caSMauro Carvalho Chehab 			}
89069b17abfSTerry Heo 			if (urb->status == -EPIPE) {
89169b17abfSTerry Heo 				broken_pipe = true;
89269b17abfSTerry Heo 			}
8930c0d06caSMauro Carvalho Chehab 			usb_free_urb(urb);
8940c0d06caSMauro Carvalho Chehab 			dev->video_mode.isoc_ctl.urb[i] = NULL;
8950c0d06caSMauro Carvalho Chehab 		}
8960c0d06caSMauro Carvalho Chehab 		dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL;
8970c0d06caSMauro Carvalho Chehab 	}
8980c0d06caSMauro Carvalho Chehab 
89969b17abfSTerry Heo 	if (broken_pipe) {
90069b17abfSTerry Heo 		cx231xx_isocdbg("Reset endpoint to recover broken pipe.");
90169b17abfSTerry Heo 		usb_reset_endpoint(dev->udev, dev->video_mode.end_point_addr);
90269b17abfSTerry Heo 	}
9030c0d06caSMauro Carvalho Chehab 	kfree(dev->video_mode.isoc_ctl.urb);
9040c0d06caSMauro Carvalho Chehab 	kfree(dev->video_mode.isoc_ctl.transfer_buffer);
9050c0d06caSMauro Carvalho Chehab 	kfree(dma_q->p_left_data);
9060c0d06caSMauro Carvalho Chehab 
9070c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.urb = NULL;
9080c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.transfer_buffer = NULL;
9090c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.num_bufs = 0;
9100c0d06caSMauro Carvalho Chehab 	dma_q->p_left_data = NULL;
9110c0d06caSMauro Carvalho Chehab 
9120c0d06caSMauro Carvalho Chehab 	if (dev->mode_tv == 0)
9130c0d06caSMauro Carvalho Chehab 		cx231xx_capture_start(dev, 0, Raw_Video);
9140c0d06caSMauro Carvalho Chehab 	else
9150c0d06caSMauro Carvalho Chehab 		cx231xx_capture_start(dev, 0, TS1_serial_mode);
9160c0d06caSMauro Carvalho Chehab 
9170c0d06caSMauro Carvalho Chehab 
9180c0d06caSMauro Carvalho Chehab }
9190c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);
9200c0d06caSMauro Carvalho Chehab 
9210c0d06caSMauro Carvalho Chehab /*
9220c0d06caSMauro Carvalho Chehab  * Stop and Deallocate URBs
9230c0d06caSMauro Carvalho Chehab  */
cx231xx_uninit_bulk(struct cx231xx * dev)9240c0d06caSMauro Carvalho Chehab void cx231xx_uninit_bulk(struct cx231xx *dev)
9250c0d06caSMauro Carvalho Chehab {
926ce3556bdSTerry Heo 	struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
9270c0d06caSMauro Carvalho Chehab 	struct urb *urb;
9280c0d06caSMauro Carvalho Chehab 	int i;
92969b17abfSTerry Heo 	bool broken_pipe = false;
9300c0d06caSMauro Carvalho Chehab 
9310c0d06caSMauro Carvalho Chehab 	cx231xx_isocdbg("cx231xx: called cx231xx_uninit_bulk\n");
9320c0d06caSMauro Carvalho Chehab 
9330c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.nfields = -1;
9340c0d06caSMauro Carvalho Chehab 	for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
9350c0d06caSMauro Carvalho Chehab 		urb = dev->video_mode.bulk_ctl.urb[i];
9360c0d06caSMauro Carvalho Chehab 		if (urb) {
9370c0d06caSMauro Carvalho Chehab 			if (!irqs_disabled())
9380c0d06caSMauro Carvalho Chehab 				usb_kill_urb(urb);
9390c0d06caSMauro Carvalho Chehab 			else
9400c0d06caSMauro Carvalho Chehab 				usb_unlink_urb(urb);
9410c0d06caSMauro Carvalho Chehab 
9420c0d06caSMauro Carvalho Chehab 			if (dev->video_mode.bulk_ctl.transfer_buffer[i]) {
9430c0d06caSMauro Carvalho Chehab 				usb_free_coherent(dev->udev,
9440c0d06caSMauro Carvalho Chehab 						urb->transfer_buffer_length,
945ce3556bdSTerry Heo 						dev->video_mode.bulk_ctl.
9460c0d06caSMauro Carvalho Chehab 						transfer_buffer[i],
9470c0d06caSMauro Carvalho Chehab 						urb->transfer_dma);
9480c0d06caSMauro Carvalho Chehab 			}
94969b17abfSTerry Heo 			if (urb->status == -EPIPE) {
95069b17abfSTerry Heo 				broken_pipe = true;
95169b17abfSTerry Heo 			}
9520c0d06caSMauro Carvalho Chehab 			usb_free_urb(urb);
9530c0d06caSMauro Carvalho Chehab 			dev->video_mode.bulk_ctl.urb[i] = NULL;
9540c0d06caSMauro Carvalho Chehab 		}
9550c0d06caSMauro Carvalho Chehab 		dev->video_mode.bulk_ctl.transfer_buffer[i] = NULL;
9560c0d06caSMauro Carvalho Chehab 	}
9570c0d06caSMauro Carvalho Chehab 
95869b17abfSTerry Heo 	if (broken_pipe) {
95969b17abfSTerry Heo 		cx231xx_isocdbg("Reset endpoint to recover broken pipe.");
96069b17abfSTerry Heo 		usb_reset_endpoint(dev->udev, dev->video_mode.end_point_addr);
96169b17abfSTerry Heo 	}
9620c0d06caSMauro Carvalho Chehab 	kfree(dev->video_mode.bulk_ctl.urb);
9630c0d06caSMauro Carvalho Chehab 	kfree(dev->video_mode.bulk_ctl.transfer_buffer);
964ce3556bdSTerry Heo 	kfree(dma_q->p_left_data);
9650c0d06caSMauro Carvalho Chehab 
9660c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.urb = NULL;
9670c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.transfer_buffer = NULL;
9680c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.num_bufs = 0;
969ce3556bdSTerry Heo 	dma_q->p_left_data = NULL;
9700c0d06caSMauro Carvalho Chehab 
9710c0d06caSMauro Carvalho Chehab 	if (dev->mode_tv == 0)
9720c0d06caSMauro Carvalho Chehab 		cx231xx_capture_start(dev, 0, Raw_Video);
9730c0d06caSMauro Carvalho Chehab 	else
9740c0d06caSMauro Carvalho Chehab 		cx231xx_capture_start(dev, 0, TS1_serial_mode);
9750c0d06caSMauro Carvalho Chehab 
9760c0d06caSMauro Carvalho Chehab 
9770c0d06caSMauro Carvalho Chehab }
9780c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_uninit_bulk);
9790c0d06caSMauro Carvalho Chehab 
9800c0d06caSMauro Carvalho Chehab /*
9810c0d06caSMauro Carvalho Chehab  * Allocate URBs and start IRQ
9820c0d06caSMauro Carvalho Chehab  */
cx231xx_init_isoc(struct cx231xx * dev,int max_packets,int num_bufs,int max_pkt_size,int (* isoc_copy)(struct cx231xx * dev,struct urb * urb))9830c0d06caSMauro Carvalho Chehab int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
9840c0d06caSMauro Carvalho Chehab 		      int num_bufs, int max_pkt_size,
9850c0d06caSMauro Carvalho Chehab 		      int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
9860c0d06caSMauro Carvalho Chehab {
9870c0d06caSMauro Carvalho Chehab 	struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
9880c0d06caSMauro Carvalho Chehab 	int i;
9890c0d06caSMauro Carvalho Chehab 	int sb_size, pipe;
9900c0d06caSMauro Carvalho Chehab 	struct urb *urb;
9910c0d06caSMauro Carvalho Chehab 	int j, k;
9920c0d06caSMauro Carvalho Chehab 	int rc;
9930c0d06caSMauro Carvalho Chehab 
9940c0d06caSMauro Carvalho Chehab 	/* De-allocates all pending stuff */
9950c0d06caSMauro Carvalho Chehab 	cx231xx_uninit_isoc(dev);
9960c0d06caSMauro Carvalho Chehab 
9970c0d06caSMauro Carvalho Chehab 	dma_q->p_left_data = kzalloc(4096, GFP_KERNEL);
998ed0e3729SMauro Carvalho Chehab 	if (dma_q->p_left_data == NULL)
9990c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
10000c0d06caSMauro Carvalho Chehab 
10010c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
10020c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.num_bufs = num_bufs;
10030c0d06caSMauro Carvalho Chehab 	dma_q->pos = 0;
10040c0d06caSMauro Carvalho Chehab 	dma_q->is_partial_line = 0;
10050c0d06caSMauro Carvalho Chehab 	dma_q->last_sav = 0;
10060c0d06caSMauro Carvalho Chehab 	dma_q->current_field = -1;
10070c0d06caSMauro Carvalho Chehab 	dma_q->field1_done = 0;
10080c0d06caSMauro Carvalho Chehab 	dma_q->lines_per_field = dev->height / 2;
10090c0d06caSMauro Carvalho Chehab 	dma_q->bytes_left_in_line = dev->width << 1;
10100c0d06caSMauro Carvalho Chehab 	dma_q->lines_completed = 0;
10110c0d06caSMauro Carvalho Chehab 	dma_q->mpeg_buffer_done = 0;
10120c0d06caSMauro Carvalho Chehab 	dma_q->left_data_count = 0;
10130c0d06caSMauro Carvalho Chehab 	dma_q->mpeg_buffer_completed = 0;
10140c0d06caSMauro Carvalho Chehab 	dma_q->add_ps_package_head = CX231XX_NEED_ADD_PS_PACKAGE_HEAD;
10150c0d06caSMauro Carvalho Chehab 	dma_q->ps_head[0] = 0x00;
10160c0d06caSMauro Carvalho Chehab 	dma_q->ps_head[1] = 0x00;
10170c0d06caSMauro Carvalho Chehab 	dma_q->ps_head[2] = 0x01;
10180c0d06caSMauro Carvalho Chehab 	dma_q->ps_head[3] = 0xBA;
10190c0d06caSMauro Carvalho Chehab 	for (i = 0; i < 8; i++)
10200c0d06caSMauro Carvalho Chehab 		dma_q->partial_buf[i] = 0;
10210c0d06caSMauro Carvalho Chehab 
10220c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.urb =
10236396bb22SKees Cook 	    kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
10240c0d06caSMauro Carvalho Chehab 	if (!dev->video_mode.isoc_ctl.urb) {
1025336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1026b7085c08SMauro Carvalho Chehab 			"cannot alloc memory for usb buffers\n");
1027*1e2c9e74SZhipeng Lu 		kfree(dma_q->p_left_data);
10280c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
10290c0d06caSMauro Carvalho Chehab 	}
10300c0d06caSMauro Carvalho Chehab 
10310c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.transfer_buffer =
10326396bb22SKees Cook 	    kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
10330c0d06caSMauro Carvalho Chehab 	if (!dev->video_mode.isoc_ctl.transfer_buffer) {
1034336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1035b7085c08SMauro Carvalho Chehab 			"cannot allocate memory for usbtransfer\n");
10360c0d06caSMauro Carvalho Chehab 		kfree(dev->video_mode.isoc_ctl.urb);
1037*1e2c9e74SZhipeng Lu 		kfree(dma_q->p_left_data);
10380c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
10390c0d06caSMauro Carvalho Chehab 	}
10400c0d06caSMauro Carvalho Chehab 
10410c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size;
10420c0d06caSMauro Carvalho Chehab 	dev->video_mode.isoc_ctl.buf = NULL;
10430c0d06caSMauro Carvalho Chehab 
10440c0d06caSMauro Carvalho Chehab 	sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size;
10450c0d06caSMauro Carvalho Chehab 
10460c0d06caSMauro Carvalho Chehab 	if (dev->mode_tv == 1)
10470c0d06caSMauro Carvalho Chehab 		dev->video_mode.end_point_addr = 0x81;
10480c0d06caSMauro Carvalho Chehab 	else
10490c0d06caSMauro Carvalho Chehab 		dev->video_mode.end_point_addr = 0x84;
10500c0d06caSMauro Carvalho Chehab 
10510c0d06caSMauro Carvalho Chehab 
10520c0d06caSMauro Carvalho Chehab 	/* allocate urbs and transfer buffers */
10530c0d06caSMauro Carvalho Chehab 	for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
10540c0d06caSMauro Carvalho Chehab 		urb = usb_alloc_urb(max_packets, GFP_KERNEL);
10550c0d06caSMauro Carvalho Chehab 		if (!urb) {
10560c0d06caSMauro Carvalho Chehab 			cx231xx_uninit_isoc(dev);
10570c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
10580c0d06caSMauro Carvalho Chehab 		}
10590c0d06caSMauro Carvalho Chehab 		dev->video_mode.isoc_ctl.urb[i] = urb;
10600c0d06caSMauro Carvalho Chehab 
10610c0d06caSMauro Carvalho Chehab 		dev->video_mode.isoc_ctl.transfer_buffer[i] =
10620c0d06caSMauro Carvalho Chehab 		    usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
10630c0d06caSMauro Carvalho Chehab 				       &urb->transfer_dma);
10640c0d06caSMauro Carvalho Chehab 		if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
1065336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
10668b77d1f9SThomas Gleixner 				"unable to allocate %i bytes for transfer buffer %i\n",
10678b77d1f9SThomas Gleixner 				sb_size, i);
10680c0d06caSMauro Carvalho Chehab 			cx231xx_uninit_isoc(dev);
10690c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
10700c0d06caSMauro Carvalho Chehab 		}
10710c0d06caSMauro Carvalho Chehab 		memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);
10720c0d06caSMauro Carvalho Chehab 
10730c0d06caSMauro Carvalho Chehab 		pipe =
10740c0d06caSMauro Carvalho Chehab 		    usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
10750c0d06caSMauro Carvalho Chehab 
10760c0d06caSMauro Carvalho Chehab 		usb_fill_int_urb(urb, dev->udev, pipe,
10770c0d06caSMauro Carvalho Chehab 				 dev->video_mode.isoc_ctl.transfer_buffer[i],
10780c0d06caSMauro Carvalho Chehab 				 sb_size, cx231xx_isoc_irq_callback, dma_q, 1);
10790c0d06caSMauro Carvalho Chehab 
10800c0d06caSMauro Carvalho Chehab 		urb->number_of_packets = max_packets;
10810c0d06caSMauro Carvalho Chehab 		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
10820c0d06caSMauro Carvalho Chehab 
10830c0d06caSMauro Carvalho Chehab 		k = 0;
10840c0d06caSMauro Carvalho Chehab 		for (j = 0; j < max_packets; j++) {
10850c0d06caSMauro Carvalho Chehab 			urb->iso_frame_desc[j].offset = k;
10860c0d06caSMauro Carvalho Chehab 			urb->iso_frame_desc[j].length =
10870c0d06caSMauro Carvalho Chehab 			    dev->video_mode.isoc_ctl.max_pkt_size;
10880c0d06caSMauro Carvalho Chehab 			k += dev->video_mode.isoc_ctl.max_pkt_size;
10890c0d06caSMauro Carvalho Chehab 		}
10900c0d06caSMauro Carvalho Chehab 	}
10910c0d06caSMauro Carvalho Chehab 
10920c0d06caSMauro Carvalho Chehab 	init_waitqueue_head(&dma_q->wq);
10930c0d06caSMauro Carvalho Chehab 
10940c0d06caSMauro Carvalho Chehab 	/* submit urbs and enables IRQ */
10950c0d06caSMauro Carvalho Chehab 	for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
10960c0d06caSMauro Carvalho Chehab 		rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
10970c0d06caSMauro Carvalho Chehab 				    GFP_ATOMIC);
10980c0d06caSMauro Carvalho Chehab 		if (rc) {
1099336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
1100b7085c08SMauro Carvalho Chehab 				"submit of urb %i failed (error=%i)\n", i,
11010c0d06caSMauro Carvalho Chehab 				rc);
11020c0d06caSMauro Carvalho Chehab 			cx231xx_uninit_isoc(dev);
11030c0d06caSMauro Carvalho Chehab 			return rc;
11040c0d06caSMauro Carvalho Chehab 		}
11050c0d06caSMauro Carvalho Chehab 	}
11060c0d06caSMauro Carvalho Chehab 
11070c0d06caSMauro Carvalho Chehab 	if (dev->mode_tv == 0)
11080c0d06caSMauro Carvalho Chehab 		cx231xx_capture_start(dev, 1, Raw_Video);
11090c0d06caSMauro Carvalho Chehab 	else
11100c0d06caSMauro Carvalho Chehab 		cx231xx_capture_start(dev, 1, TS1_serial_mode);
11110c0d06caSMauro Carvalho Chehab 
11120c0d06caSMauro Carvalho Chehab 	return 0;
11130c0d06caSMauro Carvalho Chehab }
11140c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_init_isoc);
11150c0d06caSMauro Carvalho Chehab 
11160c0d06caSMauro Carvalho Chehab /*
11170c0d06caSMauro Carvalho Chehab  * Allocate URBs and start IRQ
11180c0d06caSMauro Carvalho Chehab  */
cx231xx_init_bulk(struct cx231xx * dev,int max_packets,int num_bufs,int max_pkt_size,int (* bulk_copy)(struct cx231xx * dev,struct urb * urb))11190c0d06caSMauro Carvalho Chehab int cx231xx_init_bulk(struct cx231xx *dev, int max_packets,
11200c0d06caSMauro Carvalho Chehab 		      int num_bufs, int max_pkt_size,
11210c0d06caSMauro Carvalho Chehab 		      int (*bulk_copy) (struct cx231xx *dev, struct urb *urb))
11220c0d06caSMauro Carvalho Chehab {
11230c0d06caSMauro Carvalho Chehab 	struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
11240c0d06caSMauro Carvalho Chehab 	int i;
11250c0d06caSMauro Carvalho Chehab 	int sb_size, pipe;
11260c0d06caSMauro Carvalho Chehab 	struct urb *urb;
11270c0d06caSMauro Carvalho Chehab 	int rc;
11280c0d06caSMauro Carvalho Chehab 
11290c0d06caSMauro Carvalho Chehab 	dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
11300c0d06caSMauro Carvalho Chehab 
11310c0d06caSMauro Carvalho Chehab 	cx231xx_coredbg("Setting Video mux to %d\n", dev->video_input);
11320c0d06caSMauro Carvalho Chehab 
11330c0d06caSMauro Carvalho Chehab 	video_mux(dev, dev->video_input);
11340c0d06caSMauro Carvalho Chehab 
11350c0d06caSMauro Carvalho Chehab 	/* De-allocates all pending stuff */
11360c0d06caSMauro Carvalho Chehab 	cx231xx_uninit_bulk(dev);
11370c0d06caSMauro Carvalho Chehab 
11380c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.bulk_copy = bulk_copy;
11390c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.num_bufs = num_bufs;
11400c0d06caSMauro Carvalho Chehab 	dma_q->pos = 0;
11410c0d06caSMauro Carvalho Chehab 	dma_q->is_partial_line = 0;
11420c0d06caSMauro Carvalho Chehab 	dma_q->last_sav = 0;
11430c0d06caSMauro Carvalho Chehab 	dma_q->current_field = -1;
11440c0d06caSMauro Carvalho Chehab 	dma_q->field1_done = 0;
11450c0d06caSMauro Carvalho Chehab 	dma_q->lines_per_field = dev->height / 2;
11460c0d06caSMauro Carvalho Chehab 	dma_q->bytes_left_in_line = dev->width << 1;
11470c0d06caSMauro Carvalho Chehab 	dma_q->lines_completed = 0;
11480c0d06caSMauro Carvalho Chehab 	dma_q->mpeg_buffer_done = 0;
11490c0d06caSMauro Carvalho Chehab 	dma_q->left_data_count = 0;
11500c0d06caSMauro Carvalho Chehab 	dma_q->mpeg_buffer_completed = 0;
11510c0d06caSMauro Carvalho Chehab 	dma_q->ps_head[0] = 0x00;
11520c0d06caSMauro Carvalho Chehab 	dma_q->ps_head[1] = 0x00;
11530c0d06caSMauro Carvalho Chehab 	dma_q->ps_head[2] = 0x01;
11540c0d06caSMauro Carvalho Chehab 	dma_q->ps_head[3] = 0xBA;
11550c0d06caSMauro Carvalho Chehab 	for (i = 0; i < 8; i++)
11560c0d06caSMauro Carvalho Chehab 		dma_q->partial_buf[i] = 0;
11570c0d06caSMauro Carvalho Chehab 
11580c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.urb =
11596396bb22SKees Cook 	    kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
11600c0d06caSMauro Carvalho Chehab 	if (!dev->video_mode.bulk_ctl.urb) {
1161336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1162b7085c08SMauro Carvalho Chehab 			"cannot alloc memory for usb buffers\n");
11630c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
11640c0d06caSMauro Carvalho Chehab 	}
11650c0d06caSMauro Carvalho Chehab 
11660c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.transfer_buffer =
11676396bb22SKees Cook 	    kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
11680c0d06caSMauro Carvalho Chehab 	if (!dev->video_mode.bulk_ctl.transfer_buffer) {
1169336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1170b7085c08SMauro Carvalho Chehab 			"cannot allocate memory for usbtransfer\n");
11710c0d06caSMauro Carvalho Chehab 		kfree(dev->video_mode.bulk_ctl.urb);
11720c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
11730c0d06caSMauro Carvalho Chehab 	}
11740c0d06caSMauro Carvalho Chehab 
11750c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.max_pkt_size = max_pkt_size;
11760c0d06caSMauro Carvalho Chehab 	dev->video_mode.bulk_ctl.buf = NULL;
11770c0d06caSMauro Carvalho Chehab 
11780c0d06caSMauro Carvalho Chehab 	sb_size = max_packets * dev->video_mode.bulk_ctl.max_pkt_size;
11790c0d06caSMauro Carvalho Chehab 
11800c0d06caSMauro Carvalho Chehab 	if (dev->mode_tv == 1)
11810c0d06caSMauro Carvalho Chehab 		dev->video_mode.end_point_addr = 0x81;
11820c0d06caSMauro Carvalho Chehab 	else
11830c0d06caSMauro Carvalho Chehab 		dev->video_mode.end_point_addr = 0x84;
11840c0d06caSMauro Carvalho Chehab 
11850c0d06caSMauro Carvalho Chehab 
11860c0d06caSMauro Carvalho Chehab 	/* allocate urbs and transfer buffers */
11870c0d06caSMauro Carvalho Chehab 	for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
11880c0d06caSMauro Carvalho Chehab 		urb = usb_alloc_urb(0, GFP_KERNEL);
11890c0d06caSMauro Carvalho Chehab 		if (!urb) {
11900c0d06caSMauro Carvalho Chehab 			cx231xx_uninit_bulk(dev);
11910c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
11920c0d06caSMauro Carvalho Chehab 		}
11930c0d06caSMauro Carvalho Chehab 		dev->video_mode.bulk_ctl.urb[i] = urb;
11940c0d06caSMauro Carvalho Chehab 		urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
11950c0d06caSMauro Carvalho Chehab 
11960c0d06caSMauro Carvalho Chehab 		dev->video_mode.bulk_ctl.transfer_buffer[i] =
11970c0d06caSMauro Carvalho Chehab 		    usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
11980c0d06caSMauro Carvalho Chehab 				     &urb->transfer_dma);
11990c0d06caSMauro Carvalho Chehab 		if (!dev->video_mode.bulk_ctl.transfer_buffer[i]) {
1200336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
12018b77d1f9SThomas Gleixner 				"unable to allocate %i bytes for transfer buffer %i\n",
12028b77d1f9SThomas Gleixner 				sb_size, i);
12030c0d06caSMauro Carvalho Chehab 			cx231xx_uninit_bulk(dev);
12040c0d06caSMauro Carvalho Chehab 			return -ENOMEM;
12050c0d06caSMauro Carvalho Chehab 		}
12060c0d06caSMauro Carvalho Chehab 		memset(dev->video_mode.bulk_ctl.transfer_buffer[i], 0, sb_size);
12070c0d06caSMauro Carvalho Chehab 
12080c0d06caSMauro Carvalho Chehab 		pipe = usb_rcvbulkpipe(dev->udev,
12090c0d06caSMauro Carvalho Chehab 				 dev->video_mode.end_point_addr);
12100c0d06caSMauro Carvalho Chehab 		usb_fill_bulk_urb(urb, dev->udev, pipe,
12110c0d06caSMauro Carvalho Chehab 				  dev->video_mode.bulk_ctl.transfer_buffer[i],
12120c0d06caSMauro Carvalho Chehab 				  sb_size, cx231xx_bulk_irq_callback, dma_q);
12130c0d06caSMauro Carvalho Chehab 	}
12140c0d06caSMauro Carvalho Chehab 
1215ce3556bdSTerry Heo 	/* clear halt */
1216ce3556bdSTerry Heo 	rc = usb_clear_halt(dev->udev, dev->video_mode.bulk_ctl.urb[0]->pipe);
1217ce3556bdSTerry Heo 	if (rc < 0) {
1218ce3556bdSTerry Heo 		dev_err(dev->dev,
1219ce3556bdSTerry Heo 			"failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1220ce3556bdSTerry Heo 			rc);
1221ce3556bdSTerry Heo 		cx231xx_uninit_bulk(dev);
1222ce3556bdSTerry Heo 		return rc;
1223ce3556bdSTerry Heo 	}
1224ce3556bdSTerry Heo 
12250c0d06caSMauro Carvalho Chehab 	init_waitqueue_head(&dma_q->wq);
12260c0d06caSMauro Carvalho Chehab 
12270c0d06caSMauro Carvalho Chehab 	/* submit urbs and enables IRQ */
12280c0d06caSMauro Carvalho Chehab 	for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
12290c0d06caSMauro Carvalho Chehab 		rc = usb_submit_urb(dev->video_mode.bulk_ctl.urb[i],
12300c0d06caSMauro Carvalho Chehab 				    GFP_ATOMIC);
12310c0d06caSMauro Carvalho Chehab 		if (rc) {
1232336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
1233b7085c08SMauro Carvalho Chehab 				"submit of urb %i failed (error=%i)\n", i, rc);
12340c0d06caSMauro Carvalho Chehab 			cx231xx_uninit_bulk(dev);
12350c0d06caSMauro Carvalho Chehab 			return rc;
12360c0d06caSMauro Carvalho Chehab 		}
12370c0d06caSMauro Carvalho Chehab 	}
12380c0d06caSMauro Carvalho Chehab 
12390c0d06caSMauro Carvalho Chehab 	if (dev->mode_tv == 0)
12400c0d06caSMauro Carvalho Chehab 		cx231xx_capture_start(dev, 1, Raw_Video);
12410c0d06caSMauro Carvalho Chehab 	else
12420c0d06caSMauro Carvalho Chehab 		cx231xx_capture_start(dev, 1, TS1_serial_mode);
12430c0d06caSMauro Carvalho Chehab 
12440c0d06caSMauro Carvalho Chehab 	return 0;
12450c0d06caSMauro Carvalho Chehab }
12460c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_init_bulk);
cx231xx_stop_TS1(struct cx231xx * dev)12470c0d06caSMauro Carvalho Chehab void cx231xx_stop_TS1(struct cx231xx *dev)
12480c0d06caSMauro Carvalho Chehab {
12490c0d06caSMauro Carvalho Chehab 	u8 val[4] = { 0, 0, 0, 0 };
12500c0d06caSMauro Carvalho Chehab 
12510c0d06caSMauro Carvalho Chehab 	val[0] = 0x00;
12520c0d06caSMauro Carvalho Chehab 	val[1] = 0x03;
12530c0d06caSMauro Carvalho Chehab 	val[2] = 0x00;
12540c0d06caSMauro Carvalho Chehab 	val[3] = 0x00;
12550c0d06caSMauro Carvalho Chehab 	cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
12560c0d06caSMauro Carvalho Chehab 			TS_MODE_REG, val, 4);
12570c0d06caSMauro Carvalho Chehab 
12580c0d06caSMauro Carvalho Chehab 	val[0] = 0x00;
12590c0d06caSMauro Carvalho Chehab 	val[1] = 0x70;
12600c0d06caSMauro Carvalho Chehab 	val[2] = 0x04;
12610c0d06caSMauro Carvalho Chehab 	val[3] = 0x00;
12620c0d06caSMauro Carvalho Chehab 	cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
12630c0d06caSMauro Carvalho Chehab 			TS1_CFG_REG, val, 4);
12640c0d06caSMauro Carvalho Chehab }
12650c0d06caSMauro Carvalho Chehab /* EXPORT_SYMBOL_GPL(cx231xx_stop_TS1); */
cx231xx_start_TS1(struct cx231xx * dev)12660c0d06caSMauro Carvalho Chehab void cx231xx_start_TS1(struct cx231xx *dev)
12670c0d06caSMauro Carvalho Chehab {
12680c0d06caSMauro Carvalho Chehab 	u8 val[4] = { 0, 0, 0, 0 };
12690c0d06caSMauro Carvalho Chehab 
12700c0d06caSMauro Carvalho Chehab 	val[0] = 0x03;
12710c0d06caSMauro Carvalho Chehab 	val[1] = 0x03;
12720c0d06caSMauro Carvalho Chehab 	val[2] = 0x00;
12730c0d06caSMauro Carvalho Chehab 	val[3] = 0x00;
12740c0d06caSMauro Carvalho Chehab 	cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
12750c0d06caSMauro Carvalho Chehab 			TS_MODE_REG, val, 4);
12760c0d06caSMauro Carvalho Chehab 
12770c0d06caSMauro Carvalho Chehab 	val[0] = 0x04;
12780c0d06caSMauro Carvalho Chehab 	val[1] = 0xA3;
12790c0d06caSMauro Carvalho Chehab 	val[2] = 0x3B;
12800c0d06caSMauro Carvalho Chehab 	val[3] = 0x00;
12810c0d06caSMauro Carvalho Chehab 	cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
12820c0d06caSMauro Carvalho Chehab 			TS1_CFG_REG, val, 4);
12830c0d06caSMauro Carvalho Chehab }
12840c0d06caSMauro Carvalho Chehab /* EXPORT_SYMBOL_GPL(cx231xx_start_TS1); */
12850c0d06caSMauro Carvalho Chehab /*****************************************************************
12860c0d06caSMauro Carvalho Chehab *             Device Init/UnInit functions                       *
12870c0d06caSMauro Carvalho Chehab ******************************************************************/
cx231xx_dev_init(struct cx231xx * dev)12880c0d06caSMauro Carvalho Chehab int cx231xx_dev_init(struct cx231xx *dev)
12890c0d06caSMauro Carvalho Chehab {
12900c0d06caSMauro Carvalho Chehab 	int errCode = 0;
12910c0d06caSMauro Carvalho Chehab 
12920c0d06caSMauro Carvalho Chehab 	/* Initialize I2C bus */
12930c0d06caSMauro Carvalho Chehab 
12940c0d06caSMauro Carvalho Chehab 	/* External Master 1 Bus */
12950c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[0].nr = 0;
12960c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[0].dev = dev;
12970c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[0].i2c_period = I2C_SPEED_100K;	/* 100 KHz */
12980c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[0].i2c_nostop = 0;
12990c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[0].i2c_reserve = 0;
13008e2803ffSPeter Rosin 	dev->i2c_bus[0].i2c_rc = -ENODEV;
13010c0d06caSMauro Carvalho Chehab 
13020c0d06caSMauro Carvalho Chehab 	/* External Master 2 Bus */
13030c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[1].nr = 1;
13040c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[1].dev = dev;
13050c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[1].i2c_period = I2C_SPEED_100K;	/* 100 KHz */
13060c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[1].i2c_nostop = 0;
13070c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[1].i2c_reserve = 0;
13088e2803ffSPeter Rosin 	dev->i2c_bus[1].i2c_rc = -ENODEV;
13090c0d06caSMauro Carvalho Chehab 
13100c0d06caSMauro Carvalho Chehab 	/* Internal Master 3 Bus */
13110c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[2].nr = 2;
13120c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[2].dev = dev;
13130c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[2].i2c_period = I2C_SPEED_100K;	/* 100kHz */
13140c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[2].i2c_nostop = 0;
13150c0d06caSMauro Carvalho Chehab 	dev->i2c_bus[2].i2c_reserve = 0;
13168e2803ffSPeter Rosin 	dev->i2c_bus[2].i2c_rc = -ENODEV;
13170c0d06caSMauro Carvalho Chehab 
13180c0d06caSMauro Carvalho Chehab 	/* register I2C buses */
1319461af077SMauro Carvalho Chehab 	errCode = cx231xx_i2c_register(&dev->i2c_bus[0]);
132005e0dfd0SPeter Rosin 	if (errCode < 0)
132105e0dfd0SPeter Rosin 		return errCode;
1322461af077SMauro Carvalho Chehab 	errCode = cx231xx_i2c_register(&dev->i2c_bus[1]);
1323461af077SMauro Carvalho Chehab 	if (errCode < 0)
1324461af077SMauro Carvalho Chehab 		return errCode;
1325461af077SMauro Carvalho Chehab 	errCode = cx231xx_i2c_register(&dev->i2c_bus[2]);
1326461af077SMauro Carvalho Chehab 	if (errCode < 0)
1327461af077SMauro Carvalho Chehab 		return errCode;
1328461af077SMauro Carvalho Chehab 
1329461af077SMauro Carvalho Chehab 	errCode = cx231xx_i2c_mux_create(dev);
1330461af077SMauro Carvalho Chehab 	if (errCode < 0) {
1331461af077SMauro Carvalho Chehab 		dev_err(dev->dev,
1332461af077SMauro Carvalho Chehab 			"%s: Failed to create I2C mux\n", __func__);
1333461af077SMauro Carvalho Chehab 		return errCode;
1334461af077SMauro Carvalho Chehab 	}
1335461af077SMauro Carvalho Chehab 	errCode = cx231xx_i2c_mux_register(dev, 0);
1336461af077SMauro Carvalho Chehab 	if (errCode < 0)
1337461af077SMauro Carvalho Chehab 		return errCode;
1338461af077SMauro Carvalho Chehab 
1339461af077SMauro Carvalho Chehab 	errCode = cx231xx_i2c_mux_register(dev, 1);
1340461af077SMauro Carvalho Chehab 	if (errCode < 0)
1341461af077SMauro Carvalho Chehab 		return errCode;
134215c212ddSMatthias Schwarzott 
1343e4de03f2SMatthias Schwarzott 	/* scan the real bus segments in the order of physical port numbers */
1344e4de03f2SMatthias Schwarzott 	cx231xx_do_i2c_scan(dev, I2C_0);
1345e4de03f2SMatthias Schwarzott 	cx231xx_do_i2c_scan(dev, I2C_1_MUX_1);
1346e4de03f2SMatthias Schwarzott 	cx231xx_do_i2c_scan(dev, I2C_2);
1347e4de03f2SMatthias Schwarzott 	cx231xx_do_i2c_scan(dev, I2C_1_MUX_3);
1348e4de03f2SMatthias Schwarzott 
13490c0d06caSMauro Carvalho Chehab 	/* init hardware */
13500c0d06caSMauro Carvalho Chehab 	/* Note : with out calling set power mode function,
13510c0d06caSMauro Carvalho Chehab 	afe can not be set up correctly */
13520c0d06caSMauro Carvalho Chehab 	if (dev->board.external_av) {
13530c0d06caSMauro Carvalho Chehab 		errCode = cx231xx_set_power_mode(dev,
13540c0d06caSMauro Carvalho Chehab 				 POLARIS_AVMODE_ENXTERNAL_AV);
13550c0d06caSMauro Carvalho Chehab 		if (errCode < 0) {
1356336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
1357b7085c08SMauro Carvalho Chehab 				"%s: Failed to set Power - errCode [%d]!\n",
13580c0d06caSMauro Carvalho Chehab 				__func__, errCode);
13590c0d06caSMauro Carvalho Chehab 			return errCode;
13600c0d06caSMauro Carvalho Chehab 		}
13610c0d06caSMauro Carvalho Chehab 	} else {
13620c0d06caSMauro Carvalho Chehab 		errCode = cx231xx_set_power_mode(dev,
13630c0d06caSMauro Carvalho Chehab 				 POLARIS_AVMODE_ANALOGT_TV);
13640c0d06caSMauro Carvalho Chehab 		if (errCode < 0) {
1365336fea92SMauro Carvalho Chehab 			dev_err(dev->dev,
1366b7085c08SMauro Carvalho Chehab 				"%s: Failed to set Power - errCode [%d]!\n",
13670c0d06caSMauro Carvalho Chehab 				__func__, errCode);
13680c0d06caSMauro Carvalho Chehab 			return errCode;
13690c0d06caSMauro Carvalho Chehab 		}
13700c0d06caSMauro Carvalho Chehab 	}
13710c0d06caSMauro Carvalho Chehab 
13720c0d06caSMauro Carvalho Chehab 	/* reset the Tuner, if it is a Xceive tuner */
13730c0d06caSMauro Carvalho Chehab 	if ((dev->board.tuner_type == TUNER_XC5000) ||
13740c0d06caSMauro Carvalho Chehab 	    (dev->board.tuner_type == TUNER_XC2028))
13750c0d06caSMauro Carvalho Chehab 			cx231xx_gpio_set(dev, dev->board.tuner_gpio);
13760c0d06caSMauro Carvalho Chehab 
13770c0d06caSMauro Carvalho Chehab 	/* initialize Colibri block */
13780c0d06caSMauro Carvalho Chehab 	errCode = cx231xx_afe_init_super_block(dev, 0x23c);
13790c0d06caSMauro Carvalho Chehab 	if (errCode < 0) {
1380336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1381b7085c08SMauro Carvalho Chehab 			"%s: cx231xx_afe init super block - errCode [%d]!\n",
13820c0d06caSMauro Carvalho Chehab 			__func__, errCode);
13830c0d06caSMauro Carvalho Chehab 		return errCode;
13840c0d06caSMauro Carvalho Chehab 	}
13850c0d06caSMauro Carvalho Chehab 	errCode = cx231xx_afe_init_channels(dev);
13860c0d06caSMauro Carvalho Chehab 	if (errCode < 0) {
1387336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1388b7085c08SMauro Carvalho Chehab 			"%s: cx231xx_afe init channels - errCode [%d]!\n",
13890c0d06caSMauro Carvalho Chehab 			__func__, errCode);
13900c0d06caSMauro Carvalho Chehab 		return errCode;
13910c0d06caSMauro Carvalho Chehab 	}
13920c0d06caSMauro Carvalho Chehab 
13930c0d06caSMauro Carvalho Chehab 	/* Set DIF in By pass mode */
13940c0d06caSMauro Carvalho Chehab 	errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
13950c0d06caSMauro Carvalho Chehab 	if (errCode < 0) {
1396336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1397b7085c08SMauro Carvalho Chehab 			"%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
13980c0d06caSMauro Carvalho Chehab 			__func__, errCode);
13990c0d06caSMauro Carvalho Chehab 		return errCode;
14000c0d06caSMauro Carvalho Chehab 	}
14010c0d06caSMauro Carvalho Chehab 
14020c0d06caSMauro Carvalho Chehab 	/* I2S block related functions */
14030c0d06caSMauro Carvalho Chehab 	errCode = cx231xx_i2s_blk_initialize(dev);
14040c0d06caSMauro Carvalho Chehab 	if (errCode < 0) {
1405336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1406b7085c08SMauro Carvalho Chehab 			"%s: cx231xx_i2s block initialize - errCode [%d]!\n",
14070c0d06caSMauro Carvalho Chehab 			__func__, errCode);
14080c0d06caSMauro Carvalho Chehab 		return errCode;
14090c0d06caSMauro Carvalho Chehab 	}
14100c0d06caSMauro Carvalho Chehab 
14110c0d06caSMauro Carvalho Chehab 	/* init control pins */
14120c0d06caSMauro Carvalho Chehab 	errCode = cx231xx_init_ctrl_pin_status(dev);
14130c0d06caSMauro Carvalho Chehab 	if (errCode < 0) {
1414336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1415b7085c08SMauro Carvalho Chehab 			"%s: cx231xx_init ctrl pins - errCode [%d]!\n",
14160c0d06caSMauro Carvalho Chehab 			__func__, errCode);
14170c0d06caSMauro Carvalho Chehab 		return errCode;
14180c0d06caSMauro Carvalho Chehab 	}
14190c0d06caSMauro Carvalho Chehab 
14200c0d06caSMauro Carvalho Chehab 	/* set AGC mode to Analog */
14210c0d06caSMauro Carvalho Chehab 	switch (dev->model) {
14220c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_CNXT_CARRAERA:
14230c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_CNXT_RDE_250:
14240c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_CNXT_SHELBY:
14250c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_CNXT_RDU_250:
14260c0d06caSMauro Carvalho Chehab 	errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
14270c0d06caSMauro Carvalho Chehab 		break;
14280c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_CNXT_RDE_253S:
14290c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_CNXT_RDU_253S:
14300c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_HAUPPAUGE_EXETER:
1431dd2e7dd2SMatthias Schwarzott 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
14320c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
14330c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
14340c0d06caSMauro Carvalho Chehab 	case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
14350c0d06caSMauro Carvalho Chehab 	errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
14360c0d06caSMauro Carvalho Chehab 		break;
14370c0d06caSMauro Carvalho Chehab 	default:
14380c0d06caSMauro Carvalho Chehab 		break;
14390c0d06caSMauro Carvalho Chehab 	}
14400c0d06caSMauro Carvalho Chehab 	if (errCode < 0) {
1441336fea92SMauro Carvalho Chehab 		dev_err(dev->dev,
1442b7085c08SMauro Carvalho Chehab 			"%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
14430c0d06caSMauro Carvalho Chehab 			__func__, errCode);
14440c0d06caSMauro Carvalho Chehab 		return errCode;
14450c0d06caSMauro Carvalho Chehab 	}
14460c0d06caSMauro Carvalho Chehab 
14470c0d06caSMauro Carvalho Chehab 	/* set all alternate settings to zero initially */
14480c0d06caSMauro Carvalho Chehab 	cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
14490c0d06caSMauro Carvalho Chehab 	cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
14500c0d06caSMauro Carvalho Chehab 	cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
14510c0d06caSMauro Carvalho Chehab 	if (dev->board.has_dvb)
14520c0d06caSMauro Carvalho Chehab 		cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
14530c0d06caSMauro Carvalho Chehab 
1454660acd54SMatthias Schwarzott 	errCode = 0;
14550c0d06caSMauro Carvalho Chehab 	return errCode;
14560c0d06caSMauro Carvalho Chehab }
14570c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_dev_init);
14580c0d06caSMauro Carvalho Chehab 
cx231xx_dev_uninit(struct cx231xx * dev)14590c0d06caSMauro Carvalho Chehab void cx231xx_dev_uninit(struct cx231xx *dev)
14600c0d06caSMauro Carvalho Chehab {
14610c0d06caSMauro Carvalho Chehab 	/* Un Initialize I2C bus */
146205e0dfd0SPeter Rosin 	cx231xx_i2c_mux_unregister(dev);
14630c0d06caSMauro Carvalho Chehab 	cx231xx_i2c_unregister(&dev->i2c_bus[2]);
14640c0d06caSMauro Carvalho Chehab 	cx231xx_i2c_unregister(&dev->i2c_bus[1]);
14650c0d06caSMauro Carvalho Chehab 	cx231xx_i2c_unregister(&dev->i2c_bus[0]);
14660c0d06caSMauro Carvalho Chehab }
14670c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
14680c0d06caSMauro Carvalho Chehab 
14690c0d06caSMauro Carvalho Chehab /*****************************************************************
14700c0d06caSMauro Carvalho Chehab *              G P I O related functions                         *
14710c0d06caSMauro Carvalho Chehab ******************************************************************/
cx231xx_send_gpio_cmd(struct cx231xx * dev,u32 gpio_bit,u8 * gpio_val,u8 len,u8 request,u8 direction)14720c0d06caSMauro Carvalho Chehab int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val,
14730c0d06caSMauro Carvalho Chehab 			  u8 len, u8 request, u8 direction)
14740c0d06caSMauro Carvalho Chehab {
14750c0d06caSMauro Carvalho Chehab 	int status = 0;
14760c0d06caSMauro Carvalho Chehab 	struct VENDOR_REQUEST_IN ven_req;
14770c0d06caSMauro Carvalho Chehab 
14780c0d06caSMauro Carvalho Chehab 	/* Set wValue */
14790c0d06caSMauro Carvalho Chehab 	ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff);
14800c0d06caSMauro Carvalho Chehab 
14810c0d06caSMauro Carvalho Chehab 	/* set request */
14820c0d06caSMauro Carvalho Chehab 	if (!request) {
14830c0d06caSMauro Carvalho Chehab 		if (direction)
14847adc7998SMauro Carvalho Chehab 			ven_req.bRequest = VRT_GET_GPIO;	/* 0x9 gpio */
14850c0d06caSMauro Carvalho Chehab 		else
14867adc7998SMauro Carvalho Chehab 			ven_req.bRequest = VRT_SET_GPIO;	/* 0x8 gpio */
14870c0d06caSMauro Carvalho Chehab 	} else {
14880c0d06caSMauro Carvalho Chehab 		if (direction)
14897adc7998SMauro Carvalho Chehab 			ven_req.bRequest = VRT_GET_GPIE;	/* 0xb gpie */
14900c0d06caSMauro Carvalho Chehab 		else
14917adc7998SMauro Carvalho Chehab 			ven_req.bRequest = VRT_SET_GPIE;	/* 0xa gpie */
14920c0d06caSMauro Carvalho Chehab 	}
14930c0d06caSMauro Carvalho Chehab 
14940c0d06caSMauro Carvalho Chehab 	/* set index value */
14950c0d06caSMauro Carvalho Chehab 	ven_req.wIndex = (u16) (gpio_bit & 0xffff);
14960c0d06caSMauro Carvalho Chehab 
14970c0d06caSMauro Carvalho Chehab 	/* set wLength value */
14980c0d06caSMauro Carvalho Chehab 	ven_req.wLength = len;
14990c0d06caSMauro Carvalho Chehab 
15000c0d06caSMauro Carvalho Chehab 	/* set bData value */
15010c0d06caSMauro Carvalho Chehab 	ven_req.bData = 0;
15020c0d06caSMauro Carvalho Chehab 
15030c0d06caSMauro Carvalho Chehab 	/* set the buffer for read / write */
15040c0d06caSMauro Carvalho Chehab 	ven_req.pBuff = gpio_val;
15050c0d06caSMauro Carvalho Chehab 
15060c0d06caSMauro Carvalho Chehab 	/* set the direction */
15070c0d06caSMauro Carvalho Chehab 	if (direction) {
15080c0d06caSMauro Carvalho Chehab 		ven_req.direction = USB_DIR_IN;
15090c0d06caSMauro Carvalho Chehab 		memset(ven_req.pBuff, 0x00, ven_req.wLength);
15100c0d06caSMauro Carvalho Chehab 	} else
15110c0d06caSMauro Carvalho Chehab 		ven_req.direction = USB_DIR_OUT;
15120c0d06caSMauro Carvalho Chehab 
15130c0d06caSMauro Carvalho Chehab 
15140c0d06caSMauro Carvalho Chehab 	/* call common vendor command request */
15150c0d06caSMauro Carvalho Chehab 	status = cx231xx_send_vendor_cmd(dev, &ven_req);
15160c0d06caSMauro Carvalho Chehab 	if (status < 0) {
1517336fea92SMauro Carvalho Chehab 		dev_err(dev->dev, "%s: failed with status -%d\n",
1518ed0e3729SMauro Carvalho Chehab 			__func__, status);
15190c0d06caSMauro Carvalho Chehab 	}
15200c0d06caSMauro Carvalho Chehab 
15210c0d06caSMauro Carvalho Chehab 	return status;
15220c0d06caSMauro Carvalho Chehab }
15230c0d06caSMauro Carvalho Chehab EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);
15240c0d06caSMauro Carvalho Chehab 
15250c0d06caSMauro Carvalho Chehab /*****************************************************************
15260c0d06caSMauro Carvalho Chehab  *    C O N T R O L - Register R E A D / W R I T E functions     *
15270c0d06caSMauro Carvalho Chehab  *****************************************************************/
cx231xx_mode_register(struct cx231xx * dev,u16 address,u32 mode)15280c0d06caSMauro Carvalho Chehab int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
15290c0d06caSMauro Carvalho Chehab {
15300c0d06caSMauro Carvalho Chehab 	u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
15310c0d06caSMauro Carvalho Chehab 	u32 tmp = 0;
15320c0d06caSMauro Carvalho Chehab 	int status = 0;
15330c0d06caSMauro Carvalho Chehab 
15340c0d06caSMauro Carvalho Chehab 	status =
15350c0d06caSMauro Carvalho Chehab 	    cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
15360c0d06caSMauro Carvalho Chehab 	if (status < 0)
15370c0d06caSMauro Carvalho Chehab 		return status;
15380c0d06caSMauro Carvalho Chehab 
15393f9280a8SHans Verkuil 	tmp = le32_to_cpu(*((__le32 *) value));
15400c0d06caSMauro Carvalho Chehab 	tmp |= mode;
15410c0d06caSMauro Carvalho Chehab 
15420c0d06caSMauro Carvalho Chehab 	value[0] = (u8) tmp;
15430c0d06caSMauro Carvalho Chehab 	value[1] = (u8) (tmp >> 8);
15440c0d06caSMauro Carvalho Chehab 	value[2] = (u8) (tmp >> 16);
15450c0d06caSMauro Carvalho Chehab 	value[3] = (u8) (tmp >> 24);
15460c0d06caSMauro Carvalho Chehab 
15470c0d06caSMauro Carvalho Chehab 	status =
15480c0d06caSMauro Carvalho Chehab 	    cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
15490c0d06caSMauro Carvalho Chehab 
15500c0d06caSMauro Carvalho Chehab 	return status;
15510c0d06caSMauro Carvalho Chehab }
15520c0d06caSMauro Carvalho Chehab 
15530c0d06caSMauro Carvalho Chehab /*****************************************************************
15540c0d06caSMauro Carvalho Chehab  *            I 2 C Internal C O N T R O L   functions           *
15550c0d06caSMauro Carvalho Chehab  *****************************************************************/
cx231xx_read_i2c_master(struct cx231xx * dev,u8 dev_addr,u16 saddr,u8 saddr_len,u32 * data,u8 data_len,int master)15560c0d06caSMauro Carvalho Chehab int cx231xx_read_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
15570c0d06caSMauro Carvalho Chehab 			  u8 saddr_len, u32 *data, u8 data_len, int master)
15580c0d06caSMauro Carvalho Chehab {
15590c0d06caSMauro Carvalho Chehab 	int status = 0;
15600c0d06caSMauro Carvalho Chehab 	struct cx231xx_i2c_xfer_data req_data;
15610c0d06caSMauro Carvalho Chehab 	u8 value[64] = "0";
15620c0d06caSMauro Carvalho Chehab 
15630c0d06caSMauro Carvalho Chehab 	if (saddr_len == 0)
15640c0d06caSMauro Carvalho Chehab 		saddr = 0;
15650c0d06caSMauro Carvalho Chehab 	else if (saddr_len == 1)
15660c0d06caSMauro Carvalho Chehab 		saddr &= 0xff;
15670c0d06caSMauro Carvalho Chehab 
15680c0d06caSMauro Carvalho Chehab 	/* prepare xfer_data struct */
15690c0d06caSMauro Carvalho Chehab 	req_data.dev_addr = dev_addr >> 1;
15700c0d06caSMauro Carvalho Chehab 	req_data.direction = I2C_M_RD;
15710c0d06caSMauro Carvalho Chehab 	req_data.saddr_len = saddr_len;
15720c0d06caSMauro Carvalho Chehab 	req_data.saddr_dat = saddr;
15730c0d06caSMauro Carvalho Chehab 	req_data.buf_size = data_len;
15740c0d06caSMauro Carvalho Chehab 	req_data.p_buffer = (u8 *) value;
15750c0d06caSMauro Carvalho Chehab 
15760c0d06caSMauro Carvalho Chehab 	/* usb send command */
15770c0d06caSMauro Carvalho Chehab 	if (master == 0)
15780c0d06caSMauro Carvalho Chehab 		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
15790c0d06caSMauro Carvalho Chehab 					 &req_data);
15800c0d06caSMauro Carvalho Chehab 	else if (master == 1)
15810c0d06caSMauro Carvalho Chehab 		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
15820c0d06caSMauro Carvalho Chehab 					 &req_data);
15830c0d06caSMauro Carvalho Chehab 	else if (master == 2)
15840c0d06caSMauro Carvalho Chehab 		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
15850c0d06caSMauro Carvalho Chehab 					 &req_data);
15860c0d06caSMauro Carvalho Chehab 
15870c0d06caSMauro Carvalho Chehab 	if (status >= 0) {
15880c0d06caSMauro Carvalho Chehab 		/* Copy the data read back to main buffer */
15890c0d06caSMauro Carvalho Chehab 		if (data_len == 1)
15900c0d06caSMauro Carvalho Chehab 			*data = value[0];
15910c0d06caSMauro Carvalho Chehab 		else if (data_len == 4)
15920c0d06caSMauro Carvalho Chehab 			*data =
15930c0d06caSMauro Carvalho Chehab 			    value[0] | value[1] << 8 | value[2] << 16 | value[3]
15940c0d06caSMauro Carvalho Chehab 			    << 24;
15950c0d06caSMauro Carvalho Chehab 		else if (data_len > 4)
15960c0d06caSMauro Carvalho Chehab 			*data = value[saddr];
15970c0d06caSMauro Carvalho Chehab 	}
15980c0d06caSMauro Carvalho Chehab 
15990c0d06caSMauro Carvalho Chehab 	return status;
16000c0d06caSMauro Carvalho Chehab }
16010c0d06caSMauro Carvalho Chehab 
cx231xx_write_i2c_master(struct cx231xx * dev,u8 dev_addr,u16 saddr,u8 saddr_len,u32 data,u8 data_len,int master)16020c0d06caSMauro Carvalho Chehab int cx231xx_write_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
16030c0d06caSMauro Carvalho Chehab 			   u8 saddr_len, u32 data, u8 data_len, int master)
16040c0d06caSMauro Carvalho Chehab {
16050c0d06caSMauro Carvalho Chehab 	int status = 0;
16060c0d06caSMauro Carvalho Chehab 	u8 value[4] = { 0, 0, 0, 0 };
16070c0d06caSMauro Carvalho Chehab 	struct cx231xx_i2c_xfer_data req_data;
16080c0d06caSMauro Carvalho Chehab 
16090c0d06caSMauro Carvalho Chehab 	value[0] = (u8) data;
16100c0d06caSMauro Carvalho Chehab 	value[1] = (u8) (data >> 8);
16110c0d06caSMauro Carvalho Chehab 	value[2] = (u8) (data >> 16);
16120c0d06caSMauro Carvalho Chehab 	value[3] = (u8) (data >> 24);
16130c0d06caSMauro Carvalho Chehab 
16140c0d06caSMauro Carvalho Chehab 	if (saddr_len == 0)
16150c0d06caSMauro Carvalho Chehab 		saddr = 0;
16160c0d06caSMauro Carvalho Chehab 	else if (saddr_len == 1)
16170c0d06caSMauro Carvalho Chehab 		saddr &= 0xff;
16180c0d06caSMauro Carvalho Chehab 
16190c0d06caSMauro Carvalho Chehab 	/* prepare xfer_data struct */
16200c0d06caSMauro Carvalho Chehab 	req_data.dev_addr = dev_addr >> 1;
16210c0d06caSMauro Carvalho Chehab 	req_data.direction = 0;
16220c0d06caSMauro Carvalho Chehab 	req_data.saddr_len = saddr_len;
16230c0d06caSMauro Carvalho Chehab 	req_data.saddr_dat = saddr;
16240c0d06caSMauro Carvalho Chehab 	req_data.buf_size = data_len;
16250c0d06caSMauro Carvalho Chehab 	req_data.p_buffer = value;
16260c0d06caSMauro Carvalho Chehab 
16270c0d06caSMauro Carvalho Chehab 	/* usb send command */
16280c0d06caSMauro Carvalho Chehab 	if (master == 0)
16290c0d06caSMauro Carvalho Chehab 		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
16300c0d06caSMauro Carvalho Chehab 				 &req_data);
16310c0d06caSMauro Carvalho Chehab 	else if (master == 1)
16320c0d06caSMauro Carvalho Chehab 		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
16330c0d06caSMauro Carvalho Chehab 				 &req_data);
16340c0d06caSMauro Carvalho Chehab 	else if (master == 2)
16350c0d06caSMauro Carvalho Chehab 		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
16360c0d06caSMauro Carvalho Chehab 				 &req_data);
16370c0d06caSMauro Carvalho Chehab 
16380c0d06caSMauro Carvalho Chehab 	return status;
16390c0d06caSMauro Carvalho Chehab }
16400c0d06caSMauro Carvalho Chehab 
cx231xx_read_i2c_data(struct cx231xx * dev,u8 dev_addr,u16 saddr,u8 saddr_len,u32 * data,u8 data_len)16410c0d06caSMauro Carvalho Chehab int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
16420c0d06caSMauro Carvalho Chehab 			  u8 saddr_len, u32 *data, u8 data_len)
16430c0d06caSMauro Carvalho Chehab {
16440c0d06caSMauro Carvalho Chehab 	int status = 0;
16450c0d06caSMauro Carvalho Chehab 	struct cx231xx_i2c_xfer_data req_data;
16460c0d06caSMauro Carvalho Chehab 	u8 value[4] = { 0, 0, 0, 0 };
16470c0d06caSMauro Carvalho Chehab 
16480c0d06caSMauro Carvalho Chehab 	if (saddr_len == 0)
16490c0d06caSMauro Carvalho Chehab 		saddr = 0;
16500c0d06caSMauro Carvalho Chehab 	else if (saddr_len == 1)
16510c0d06caSMauro Carvalho Chehab 		saddr &= 0xff;
16520c0d06caSMauro Carvalho Chehab 
16530c0d06caSMauro Carvalho Chehab 	/* prepare xfer_data struct */
16540c0d06caSMauro Carvalho Chehab 	req_data.dev_addr = dev_addr >> 1;
16550c0d06caSMauro Carvalho Chehab 	req_data.direction = I2C_M_RD;
16560c0d06caSMauro Carvalho Chehab 	req_data.saddr_len = saddr_len;
16570c0d06caSMauro Carvalho Chehab 	req_data.saddr_dat = saddr;
16580c0d06caSMauro Carvalho Chehab 	req_data.buf_size = data_len;
16590c0d06caSMauro Carvalho Chehab 	req_data.p_buffer = (u8 *) value;
16600c0d06caSMauro Carvalho Chehab 
16610c0d06caSMauro Carvalho Chehab 	/* usb send command */
16620c0d06caSMauro Carvalho Chehab 	status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
16630c0d06caSMauro Carvalho Chehab 
16640c0d06caSMauro Carvalho Chehab 	if (status >= 0) {
16650c0d06caSMauro Carvalho Chehab 		/* Copy the data read back to main buffer */
16660c0d06caSMauro Carvalho Chehab 		if (data_len == 1)
16670c0d06caSMauro Carvalho Chehab 			*data = value[0];
16680c0d06caSMauro Carvalho Chehab 		else
16690c0d06caSMauro Carvalho Chehab 			*data =
16700c0d06caSMauro Carvalho Chehab 			    value[0] | value[1] << 8 | value[2] << 16 | value[3]
16710c0d06caSMauro Carvalho Chehab 			    << 24;
16720c0d06caSMauro Carvalho Chehab 	}
16730c0d06caSMauro Carvalho Chehab 
16740c0d06caSMauro Carvalho Chehab 	return status;
16750c0d06caSMauro Carvalho Chehab }
16760c0d06caSMauro Carvalho Chehab 
cx231xx_write_i2c_data(struct cx231xx * dev,u8 dev_addr,u16 saddr,u8 saddr_len,u32 data,u8 data_len)16770c0d06caSMauro Carvalho Chehab int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
16780c0d06caSMauro Carvalho Chehab 			   u8 saddr_len, u32 data, u8 data_len)
16790c0d06caSMauro Carvalho Chehab {
16800c0d06caSMauro Carvalho Chehab 	int status = 0;
16810c0d06caSMauro Carvalho Chehab 	u8 value[4] = { 0, 0, 0, 0 };
16820c0d06caSMauro Carvalho Chehab 	struct cx231xx_i2c_xfer_data req_data;
16830c0d06caSMauro Carvalho Chehab 
16840c0d06caSMauro Carvalho Chehab 	value[0] = (u8) data;
16850c0d06caSMauro Carvalho Chehab 	value[1] = (u8) (data >> 8);
16860c0d06caSMauro Carvalho Chehab 	value[2] = (u8) (data >> 16);
16870c0d06caSMauro Carvalho Chehab 	value[3] = (u8) (data >> 24);
16880c0d06caSMauro Carvalho Chehab 
16890c0d06caSMauro Carvalho Chehab 	if (saddr_len == 0)
16900c0d06caSMauro Carvalho Chehab 		saddr = 0;
16910c0d06caSMauro Carvalho Chehab 	else if (saddr_len == 1)
16920c0d06caSMauro Carvalho Chehab 		saddr &= 0xff;
16930c0d06caSMauro Carvalho Chehab 
16940c0d06caSMauro Carvalho Chehab 	/* prepare xfer_data struct */
16950c0d06caSMauro Carvalho Chehab 	req_data.dev_addr = dev_addr >> 1;
16960c0d06caSMauro Carvalho Chehab 	req_data.direction = 0;
16970c0d06caSMauro Carvalho Chehab 	req_data.saddr_len = saddr_len;
16980c0d06caSMauro Carvalho Chehab 	req_data.saddr_dat = saddr;
16990c0d06caSMauro Carvalho Chehab 	req_data.buf_size = data_len;
17000c0d06caSMauro Carvalho Chehab 	req_data.p_buffer = value;
17010c0d06caSMauro Carvalho Chehab 
17020c0d06caSMauro Carvalho Chehab 	/* usb send command */
17030c0d06caSMauro Carvalho Chehab 	status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
17040c0d06caSMauro Carvalho Chehab 
17050c0d06caSMauro Carvalho Chehab 	return status;
17060c0d06caSMauro Carvalho Chehab }
17070c0d06caSMauro Carvalho Chehab 
cx231xx_reg_mask_write(struct cx231xx * dev,u8 dev_addr,u8 size,u16 register_address,u8 bit_start,u8 bit_end,u32 value)17080c0d06caSMauro Carvalho Chehab int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
17090c0d06caSMauro Carvalho Chehab 			   u16 register_address, u8 bit_start, u8 bit_end,
17100c0d06caSMauro Carvalho Chehab 			   u32 value)
17110c0d06caSMauro Carvalho Chehab {
17120c0d06caSMauro Carvalho Chehab 	int status = 0;
17130c0d06caSMauro Carvalho Chehab 	u32 tmp;
17140c0d06caSMauro Carvalho Chehab 	u32 mask = 0;
17150c0d06caSMauro Carvalho Chehab 	int i;
17160c0d06caSMauro Carvalho Chehab 
17170c0d06caSMauro Carvalho Chehab 	if (bit_start > (size - 1) || bit_end > (size - 1))
17180c0d06caSMauro Carvalho Chehab 		return -1;
17190c0d06caSMauro Carvalho Chehab 
17200c0d06caSMauro Carvalho Chehab 	if (size == 8) {
17210c0d06caSMauro Carvalho Chehab 		status =
17220c0d06caSMauro Carvalho Chehab 		    cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
17230c0d06caSMauro Carvalho Chehab 					  &tmp, 1);
17240c0d06caSMauro Carvalho Chehab 	} else {
17250c0d06caSMauro Carvalho Chehab 		status =
17260c0d06caSMauro Carvalho Chehab 		    cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
17270c0d06caSMauro Carvalho Chehab 					  &tmp, 4);
17280c0d06caSMauro Carvalho Chehab 	}
17290c0d06caSMauro Carvalho Chehab 
17300c0d06caSMauro Carvalho Chehab 	if (status < 0)
17310c0d06caSMauro Carvalho Chehab 		return status;
17320c0d06caSMauro Carvalho Chehab 
17330c0d06caSMauro Carvalho Chehab 	mask = 1 << bit_end;
17340c0d06caSMauro Carvalho Chehab 	for (i = bit_end; i > bit_start && i > 0; i--)
17350c0d06caSMauro Carvalho Chehab 		mask = mask + (1 << (i - 1));
17360c0d06caSMauro Carvalho Chehab 
17370c0d06caSMauro Carvalho Chehab 	value <<= bit_start;
17380c0d06caSMauro Carvalho Chehab 
17390c0d06caSMauro Carvalho Chehab 	if (size == 8) {
17400c0d06caSMauro Carvalho Chehab 		tmp &= ~mask;
17410c0d06caSMauro Carvalho Chehab 		tmp |= value;
17420c0d06caSMauro Carvalho Chehab 		tmp &= 0xff;
17430c0d06caSMauro Carvalho Chehab 		status =
17440c0d06caSMauro Carvalho Chehab 		    cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
17450c0d06caSMauro Carvalho Chehab 					   tmp, 1);
17460c0d06caSMauro Carvalho Chehab 	} else {
17470c0d06caSMauro Carvalho Chehab 		tmp &= ~mask;
17480c0d06caSMauro Carvalho Chehab 		tmp |= value;
17490c0d06caSMauro Carvalho Chehab 		status =
17500c0d06caSMauro Carvalho Chehab 		    cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
17510c0d06caSMauro Carvalho Chehab 					   tmp, 4);
17520c0d06caSMauro Carvalho Chehab 	}
17530c0d06caSMauro Carvalho Chehab 
17540c0d06caSMauro Carvalho Chehab 	return status;
17550c0d06caSMauro Carvalho Chehab }
17560c0d06caSMauro Carvalho Chehab 
cx231xx_read_modify_write_i2c_dword(struct cx231xx * dev,u8 dev_addr,u16 saddr,u32 mask,u32 value)17570c0d06caSMauro Carvalho Chehab int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
17580c0d06caSMauro Carvalho Chehab 					u16 saddr, u32 mask, u32 value)
17590c0d06caSMauro Carvalho Chehab {
17600c0d06caSMauro Carvalho Chehab 	u32 temp;
17610c0d06caSMauro Carvalho Chehab 	int status = 0;
17620c0d06caSMauro Carvalho Chehab 
17630c0d06caSMauro Carvalho Chehab 	status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
17640c0d06caSMauro Carvalho Chehab 
17650c0d06caSMauro Carvalho Chehab 	if (status < 0)
17660c0d06caSMauro Carvalho Chehab 		return status;
17670c0d06caSMauro Carvalho Chehab 
17680c0d06caSMauro Carvalho Chehab 	temp &= ~mask;
17690c0d06caSMauro Carvalho Chehab 	temp |= value;
17700c0d06caSMauro Carvalho Chehab 
17710c0d06caSMauro Carvalho Chehab 	status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
17720c0d06caSMauro Carvalho Chehab 
17730c0d06caSMauro Carvalho Chehab 	return status;
17740c0d06caSMauro Carvalho Chehab }
17750c0d06caSMauro Carvalho Chehab 
cx231xx_set_field(u32 field_mask,u32 data)17760c0d06caSMauro Carvalho Chehab u32 cx231xx_set_field(u32 field_mask, u32 data)
17770c0d06caSMauro Carvalho Chehab {
17780c0d06caSMauro Carvalho Chehab 	u32 temp;
17790c0d06caSMauro Carvalho Chehab 
17800c0d06caSMauro Carvalho Chehab 	for (temp = field_mask; (temp & 1) == 0; temp >>= 1)
17810c0d06caSMauro Carvalho Chehab 		data <<= 1;
17820c0d06caSMauro Carvalho Chehab 
17830c0d06caSMauro Carvalho Chehab 	return data;
17840c0d06caSMauro Carvalho Chehab }
1785