cx231xx-core.c 44.0 KB
Newer Older
1
/*
2 3
   cx231xx-core.c - driver for Conexant Cx23100/101/102
				USB video capture devices
4 5

   Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6
				Based on em28xx driver
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

23
#include "cx231xx.h"
24 25 26
#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
27
#include <linux/slab.h>
28 29 30
#include <linux/usb.h>
#include <linux/vmalloc.h>
#include <media/v4l2-common.h>
31
#include <media/tuner.h>
32 33 34 35 36 37

#include "cx231xx-reg.h"

/* #define ENABLE_DEBUG_ISOC_FRAMES */

static unsigned int core_debug;
38 39
module_param(core_debug, int, 0644);
MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
40 41 42 43 44 45 46

#define cx231xx_coredbg(fmt, arg...) do {\
	if (core_debug) \
		printk(KERN_INFO "%s %s :"fmt, \
			 dev->name, __func__ , ##arg); } while (0)

static unsigned int reg_debug;
47 48
module_param(reg_debug, int, 0644);
MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
49 50 51 52 53 54 55 56 57 58

static int alt = CX231XX_PINOUT;
module_param(alt, int, 0644);
MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");

#define cx231xx_isocdbg(fmt, arg...) do {\
	if (core_debug) \
		printk(KERN_INFO "%s %s :"fmt, \
			 dev->name, __func__ , ##arg); } while (0)

59 60 61
/*****************************************************************
*             Device control list functions     				 *
******************************************************************/
62

63
LIST_HEAD(cx231xx_devlist);
64 65 66 67 68 69 70 71 72
static DEFINE_MUTEX(cx231xx_devlist_mutex);

/*
 * cx231xx_realease_resources()
 * unregisters the v4l2,i2c and usb devices
 * called when the device gets disconected or at module unload
*/
void cx231xx_remove_from_devlist(struct cx231xx *dev)
{
73 74 75 76 77 78 79 80 81 82 83
	if (dev == NULL)
		return;
	if (dev->udev == NULL)
		return;

	if (atomic_read(&dev->devlist_count) > 0) {
		mutex_lock(&cx231xx_devlist_mutex);
		list_del(&dev->devlist);
		atomic_dec(&dev->devlist_count);
		mutex_unlock(&cx231xx_devlist_mutex);
	}
84 85 86 87 88 89
};

void cx231xx_add_into_devlist(struct cx231xx *dev)
{
	mutex_lock(&cx231xx_devlist_mutex);
	list_add_tail(&dev->devlist, &cx231xx_devlist);
90
	atomic_inc(&dev->devlist_count);
91 92 93 94 95 96 97 98 99 100 101
	mutex_unlock(&cx231xx_devlist_mutex);
};

static LIST_HEAD(cx231xx_extension_devlist);

int cx231xx_register_extension(struct cx231xx_ops *ops)
{
	struct cx231xx *dev = NULL;

	mutex_lock(&cx231xx_devlist_mutex);
	list_add_tail(&ops->next, &cx231xx_extension_devlist);
102 103 104
	list_for_each_entry(dev, &cx231xx_devlist, devlist)
		ops->init(dev);

105
	printk(KERN_INFO DRIVER_NAME ": %s initialized\n", ops->name);
106 107 108 109 110 111 112 113 114 115
	mutex_unlock(&cx231xx_devlist_mutex);
	return 0;
}
EXPORT_SYMBOL(cx231xx_register_extension);

void cx231xx_unregister_extension(struct cx231xx_ops *ops)
{
	struct cx231xx *dev = NULL;

	mutex_lock(&cx231xx_devlist_mutex);
116 117
	list_for_each_entry(dev, &cx231xx_devlist, devlist)
		ops->fini(dev);
118

119

120
	printk(KERN_INFO DRIVER_NAME ": %s removed\n", ops->name);
121 122 123
	list_del(&ops->next);
	mutex_unlock(&cx231xx_devlist_mutex);
}
124
EXPORT_SYMBOL(cx231xx_unregister_extension);
125 126 127 128 129

void cx231xx_init_extension(struct cx231xx *dev)
{
	struct cx231xx_ops *ops = NULL;

130
	mutex_lock(&cx231xx_devlist_mutex);
131 132 133 134 135 136
	if (!list_empty(&cx231xx_extension_devlist)) {
		list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
			if (ops->init)
				ops->init(dev);
		}
	}
137
	mutex_unlock(&cx231xx_devlist_mutex);
138 139 140 141 142 143
}

void cx231xx_close_extension(struct cx231xx *dev)
{
	struct cx231xx_ops *ops = NULL;

144
	mutex_lock(&cx231xx_devlist_mutex);
145 146 147 148 149 150
	if (!list_empty(&cx231xx_extension_devlist)) {
		list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
			if (ops->fini)
				ops->fini(dev);
		}
	}
151
	mutex_unlock(&cx231xx_devlist_mutex);
152 153
}

154 155 156
/****************************************************************
*               U S B related functions                         *
*****************************************************************/
157
int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
158
			     struct cx231xx_i2c_xfer_data *req_data)
159
{
160 161
	int status = 0;
	struct cx231xx *dev = i2c_bus->dev;
162
	struct VENDOR_REQUEST_IN ven_req;
163 164 165 166 167 168

	u8 saddr_len = 0;
	u8 _i2c_period = 0;
	u8 _i2c_nostop = 0;
	u8 _i2c_reserve = 0;

169 170 171
	if (dev->state & DEV_DISCONNECTED)
		return -ENODEV;

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	/* Get the I2C period, nostop and reserve parameters */
	_i2c_period = i2c_bus->i2c_period;
	_i2c_nostop = i2c_bus->i2c_nostop;
	_i2c_reserve = i2c_bus->i2c_reserve;

	saddr_len = req_data->saddr_len;

	/* Set wValue */
	if (saddr_len == 1)	/* need check saddr_len == 0  */
		ven_req.wValue =
		    req_data->
		    dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
		    _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
	else
		ven_req.wValue =
		    req_data->
		    dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
		    _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;

	/* set channel number */
192 193 194 195
	if (req_data->direction & I2C_M_RD) {
		/* channel number, for read,spec required channel_num +4 */
		ven_req.bRequest = i2c_bus->nr + 4;
	} else
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
		ven_req.bRequest = i2c_bus->nr;	/* channel number,  */

	/* set index value */
	switch (saddr_len) {
	case 0:
		ven_req.wIndex = 0;	/* need check */
		break;
	case 1:
		ven_req.wIndex = (req_data->saddr_dat & 0xff);
		break;
	case 2:
		ven_req.wIndex = req_data->saddr_dat;
		break;
	}

	/* set wLength value */
	ven_req.wLength = req_data->buf_size;

	/* set bData value */
	ven_req.bData = 0;

	/* set the direction */
	if (req_data->direction) {
		ven_req.direction = USB_DIR_IN;
		memset(req_data->p_buffer, 0x00, ven_req.wLength);
	} else
		ven_req.direction = USB_DIR_OUT;

	/* set the buffer for read / write */
	ven_req.pBuff = req_data->p_buffer;


	/* call common vendor command request */
	status = cx231xx_send_vendor_cmd(dev, &ven_req);
	if (status < 0) {
231
		pr_info
232
		    ("UsbInterface::sendCommand, failed with status -%d\n",
233 234 235 236
		     status);
	}

	return status;
237 238
}
EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/*
 * Sends/Receives URB control messages, assuring to use a kalloced buffer
 * for all operations (dev->urb_buf), to avoid using stacked buffers, as
 * they aren't safe for usage with USB, due to DMA restrictions.
 * Also implements the debug code for control URB's.
 */
static int __usb_control_msg(struct cx231xx *dev, unsigned int pipe,
	__u8 request, __u8 requesttype, __u16 value, __u16 index,
	void *data, __u16 size, int timeout)
{
	int rc, i;

	if (reg_debug) {
		printk(KERN_DEBUG "%s: (pipe 0x%08x): "
				"%s:  %02x %02x %02x %02x %02x %02x %02x %02x ",
				dev->name,
				pipe,
				(requesttype & USB_DIR_IN) ? "IN" : "OUT",
				requesttype,
				request,
				value & 0xff, value >> 8,
				index & 0xff, index >> 8,
				size & 0xff, size >> 8);
		if (!(requesttype & USB_DIR_IN)) {
			printk(KERN_CONT ">>>");
			for (i = 0; i < size; i++)
				printk(KERN_CONT " %02x",
				       ((unsigned char *)data)[i]);
		}
	}

	/* Do the real call to usb_control_msg */
	mutex_lock(&dev->ctrl_urb_lock);
	if (!(requesttype & USB_DIR_IN) && size)
		memcpy(dev->urb_buf, data, size);
	rc = usb_control_msg(dev->udev, pipe, request, requesttype, value,
			     index, dev->urb_buf, size, timeout);
	if ((requesttype & USB_DIR_IN) && size)
		memcpy(data, dev->urb_buf, size);
	mutex_unlock(&dev->ctrl_urb_lock);

	if (reg_debug) {
		if (unlikely(rc < 0)) {
			printk(KERN_CONT "FAILED!\n");
			return rc;
		}

		if ((requesttype & USB_DIR_IN)) {
			printk(KERN_CONT "<<<");
			for (i = 0; i < size; i++)
				printk(KERN_CONT " %02x",
				       ((unsigned char *)data)[i]);
		}
		printk(KERN_CONT "\n");
	}

	return rc;
}


300 301 302 303 304
/*
 * cx231xx_read_ctrl_reg()
 * reads data from the usb device specifying bRequest and wValue
 */
int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
305
			  char *buf, int len)
306
{
307
	u8 val = 0;
308 309 310 311 312 313 314 315 316
	int ret;
	int pipe = usb_rcvctrlpipe(dev->udev, 0);

	if (dev->state & DEV_DISCONNECTED)
		return -ENODEV;

	if (len > URB_MAX_CTRL_SIZE)
		return -EINVAL;

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	switch (len) {
	case 1:
		val = ENABLE_ONE_BYTE;
		break;
	case 2:
		val = ENABLE_TWE_BYTE;
		break;
	case 3:
		val = ENABLE_THREE_BYTE;
		break;
	case 4:
		val = ENABLE_FOUR_BYTE;
		break;
	default:
		val = 0xFF;	/* invalid option */
	}

	if (val == 0xFF)
		return -EINVAL;
336

337
	ret = __usb_control_msg(dev, pipe, req,
338
			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
339
			      val, reg, buf, len, HZ);
340 341 342
	return ret;
}

343 344
int cx231xx_send_vendor_cmd(struct cx231xx *dev,
				struct VENDOR_REQUEST_IN *ven_req)
345
{
346
	int ret;
347
	int pipe = 0;
348 349
	int unsend_size = 0;
	u8 *pdata;
350 351 352 353 354 355 356

	if (dev->state & DEV_DISCONNECTED)
		return -ENODEV;

	if ((ven_req->wLength > URB_MAX_CTRL_SIZE))
		return -EINVAL;

357 358 359 360
	if (ven_req->direction)
		pipe = usb_rcvctrlpipe(dev->udev, 0);
	else
		pipe = usb_sndctrlpipe(dev->udev, 0);
361

362 363 364 365
	/*
	 * If the cx23102 read more than 4 bytes with i2c bus,
	 * need chop to 4 byte per request
	 */
366 367 368 369 370 371 372 373 374
	if ((ven_req->wLength > 4) && ((ven_req->bRequest == 0x4) ||
					(ven_req->bRequest == 0x5) ||
					(ven_req->bRequest == 0x6))) {
		unsend_size = 0;
		pdata = ven_req->pBuff;


		unsend_size = ven_req->wLength;

375
		/* the first package */
376 377
		ven_req->wValue = ven_req->wValue & 0xFFFB;
		ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x2;
378 379
		ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
			ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
380 381 382 383
			ven_req->wValue, ven_req->wIndex, pdata,
			0x0004, HZ);
		unsend_size = unsend_size - 4;

384
		/* the middle package */
385 386 387
		ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x42;
		while (unsend_size - 4 > 0) {
			pdata = pdata + 4;
388
			ret = __usb_control_msg(dev, pipe,
389
				ven_req->bRequest,
390
				ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
391 392 393 394 395
				ven_req->wValue, ven_req->wIndex, pdata,
				0x0004, HZ);
			unsend_size = unsend_size - 4;
		}

396
		/* the last package */
397 398
		ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x40;
		pdata = pdata + 4;
399 400
		ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
			ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
401 402 403
			ven_req->wValue, ven_req->wIndex, pdata,
			unsend_size, HZ);
	} else {
404 405
		ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
				ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
406
				ven_req->wValue, ven_req->wIndex,
407
				ven_req->pBuff, ven_req->wLength, HZ);
408
	}
409 410 411 412 413 414 415 416 417

	return ret;
}

/*
 * cx231xx_write_ctrl_reg()
 * sends data to the usb device, specifying bRequest
 */
int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf,
418
			   int len)
419
{
420
	u8 val = 0;
421 422 423 424 425 426 427 428 429
	int ret;
	int pipe = usb_sndctrlpipe(dev->udev, 0);

	if (dev->state & DEV_DISCONNECTED)
		return -ENODEV;

	if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
		return -EINVAL;

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	switch (len) {
	case 1:
		val = ENABLE_ONE_BYTE;
		break;
	case 2:
		val = ENABLE_TWE_BYTE;
		break;
	case 3:
		val = ENABLE_THREE_BYTE;
		break;
	case 4:
		val = ENABLE_FOUR_BYTE;
		break;
	default:
		val = 0xFF;	/* invalid option */
	}

	if (val == 0xFF)
		return -EINVAL;
449 450 451 452 453

	if (reg_debug) {
		int byte;

		cx231xx_isocdbg("(pipe 0x%08x): "
454 455 456 457 458
			"OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
			pipe,
			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			req, 0, val, reg & 0xff,
			reg >> 8, len & 0xff, len >> 8);
459 460 461 462 463 464

		for (byte = 0; byte < len; byte++)
			cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
		cx231xx_isocdbg("\n");
	}

465
	ret = __usb_control_msg(dev, pipe, req,
466
			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
467
			      val, reg, buf, len, HZ);
468 469 470 471

	return ret;
}

472 473 474
/****************************************************************
*           USB Alternate Setting functions                     *
*****************************************************************/
475 476 477 478 479

int cx231xx_set_video_alternate(struct cx231xx *dev)
{
	int errCode, prev_alt = dev->video_mode.alt;
	unsigned int min_pkt_size = dev->width * 2 + 4;
480
	u32 usb_interface_index = 0;
481 482 483 484 485 486 487 488

	/* When image size is bigger than a certain value,
	   the frame size should be increased, otherwise, only
	   green screen will be received.
	 */
	if (dev->width * 2 * dev->height > 720 * 240 * 2)
		min_pkt_size *= 2;

489 490 491 492 493 494 495 496 497 498 499 500 501 502
	if (dev->width > 360) {
		/* resolutions: 720,704,640 */
		dev->video_mode.alt = 3;
	} else if (dev->width > 180) {
		/* resolutions: 360,352,320,240 */
		dev->video_mode.alt = 2;
	} else if (dev->width > 0) {
		/* resolutions: 180,176,160,128,88 */
		dev->video_mode.alt = 1;
	} else {
		/* Change to alt0 BULK to release USB bandwidth */
		dev->video_mode.alt = 0;
	}

503 504 505
	if (dev->USE_ISO == 0)
		dev->video_mode.alt = 0;

506
	cx231xx_coredbg("dev->video_mode.alt= %d\n", dev->video_mode.alt);
507

508 509 510 511
	/* Get the correct video interface Index */
	usb_interface_index =
	    dev->current_pcb_config.hs_config_info[0].interface_info.
	    video_index + 1;
512 513 514 515

	if (dev->video_mode.alt != prev_alt) {
		cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
				min_pkt_size, dev->video_mode.alt);
516 517 518 519

		if (dev->video_mode.alt_max_pkt_size != NULL)
			dev->video_mode.max_pkt_size =
			dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
520
		cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
521 522 523 524 525
				dev->video_mode.alt,
				dev->video_mode.max_pkt_size);
		errCode =
		    usb_set_interface(dev->udev, usb_interface_index,
				      dev->video_mode.alt);
526
		if (errCode < 0) {
527
			pr_err
528
			    ("cannot change alt number to %d (error=%i)\n",
529
			     dev->video_mode.alt, errCode);
530 531 532 533 534 535 536 537
			return errCode;
		}
	}
	return 0;
}

int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
{
538 539 540 541 542 543 544 545 546
	int status = 0;
	u32 usb_interface_index = 0;
	u32 max_pkt_size = 0;

	switch (index) {
	case INDEX_TS1:
		usb_interface_index =
		    dev->current_pcb_config.hs_config_info[0].interface_info.
		    ts1_index + 1;
547
		dev->ts1_mode.alt = alt;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
		if (dev->ts1_mode.alt_max_pkt_size != NULL)
			max_pkt_size = dev->ts1_mode.max_pkt_size =
			    dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt];
		break;
	case INDEX_TS2:
		usb_interface_index =
		    dev->current_pcb_config.hs_config_info[0].interface_info.
		    ts2_index + 1;
		break;
	case INDEX_AUDIO:
		usb_interface_index =
		    dev->current_pcb_config.hs_config_info[0].interface_info.
		    audio_index + 1;
		dev->adev.alt = alt;
		if (dev->adev.alt_max_pkt_size != NULL)
			max_pkt_size = dev->adev.max_pkt_size =
			    dev->adev.alt_max_pkt_size[dev->adev.alt];
		break;
	case INDEX_VIDEO:
		usb_interface_index =
		    dev->current_pcb_config.hs_config_info[0].interface_info.
		    video_index + 1;
		dev->video_mode.alt = alt;
		if (dev->video_mode.alt_max_pkt_size != NULL)
			max_pkt_size = dev->video_mode.max_pkt_size =
			    dev->video_mode.alt_max_pkt_size[dev->video_mode.
							     alt];
		break;
	case INDEX_VANC:
577 578
		if (dev->board.no_alt_vanc)
			return 0;
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
		usb_interface_index =
		    dev->current_pcb_config.hs_config_info[0].interface_info.
		    vanc_index + 1;
		dev->vbi_mode.alt = alt;
		if (dev->vbi_mode.alt_max_pkt_size != NULL)
			max_pkt_size = dev->vbi_mode.max_pkt_size =
			    dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt];
		break;
	case INDEX_HANC:
		usb_interface_index =
		    dev->current_pcb_config.hs_config_info[0].interface_info.
		    hanc_index + 1;
		dev->sliced_cc_mode.alt = alt;
		if (dev->sliced_cc_mode.alt_max_pkt_size != NULL)
			max_pkt_size = dev->sliced_cc_mode.max_pkt_size =
			    dev->sliced_cc_mode.alt_max_pkt_size[dev->
								 sliced_cc_mode.
								 alt];
		break;
	default:
		break;
	}

	if (alt > 0 && max_pkt_size == 0) {
603
		pr_err
604 605
		("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
		usb_interface_index, alt);
606 607
		/*To workaround error number=-71 on EP0 for videograbber,
		 need add following codes.*/
608
		if (dev->board.no_alt_vanc)
609
			return -1;
610 611
	}

612 613 614
	cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u,"
			"Interface = %d\n", alt, max_pkt_size,
			usb_interface_index);
615 616 617

	if (usb_interface_index > 0) {
		status = usb_set_interface(dev->udev, usb_interface_index, alt);
618
		if (status < 0) {
619
			pr_err
620 621
			("can't change interface %d alt no. to %d (err=%i)\n",
			usb_interface_index, alt, status);
622 623
			return status;
		}
624
	}
625

626
	return status;
627 628 629 630 631 632 633 634 635 636 637 638
}
EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting);

int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio)
{
	int rc = 0;

	if (!gpio)
		return rc;

	/* Send GPIO reset sequences specified at board entry */
	while (gpio->sleep >= 0) {
639 640 641
		rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
		if (rc < 0)
			return rc;
642 643 644 645 646 647 648 649 650

		if (gpio->sleep > 0)
			msleep(gpio->sleep);

		gpio++;
	}
	return rc;
}

651 652 653 654 655 656 657 658 659
int cx231xx_demod_reset(struct cx231xx *dev)
{

	u8 status = 0;
	u8 value[4] = { 0, 0, 0, 0 };

	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
				 value, 4);

660 661 662 663 664
	cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
			value[0], value[1], value[2], value[3]);

	cx231xx_coredbg("Enter cx231xx_demod_reset()\n");

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
		value[1] = (u8) 0x3;
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
			msleep(10);

		value[1] = (u8) 0x0;
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
			msleep(10);

		value[1] = (u8) 0x3;
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
			msleep(10);



	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
				 value, 4);
684 685 686

	cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
			value[0], value[1], value[2], value[3]);
687 688 689 690 691 692 693 694 695 696

	return status;
}
EXPORT_SYMBOL_GPL(cx231xx_demod_reset);
int is_fw_load(struct cx231xx *dev)
{
	return cx231xx_check_fw(dev);
}
EXPORT_SYMBOL_GPL(is_fw_load);

697 698
int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
{
699 700
	int errCode = 0;

701 702 703 704
	if (dev->mode == set_mode)
		return 0;

	if (set_mode == CX231XX_SUSPEND) {
705
		/* Set the chip in power saving mode */
706 707 708 709 710 711 712 713 714
		dev->mode = set_mode;
	}

	/* Resource is locked */
	if (dev->mode != CX231XX_SUSPEND)
		return -EINVAL;

	dev->mode = set_mode;

715 716 717 718 719 720 721 722 723 724 725
	if (dev->mode == CX231XX_DIGITAL_MODE)/* Set Digital power mode */ {
	/* set AGC mode to Digital */
		switch (dev->model) {
		case CX231XX_BOARD_CNXT_CARRAERA:
		case CX231XX_BOARD_CNXT_RDE_250:
		case CX231XX_BOARD_CNXT_SHELBY:
		case CX231XX_BOARD_CNXT_RDU_250:
		errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
			break;
		case CX231XX_BOARD_CNXT_RDE_253S:
		case CX231XX_BOARD_CNXT_RDU_253S:
726 727
			errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
			break;
728
		case CX231XX_BOARD_HAUPPAUGE_EXETER:
729
		case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
730 731
			errCode = cx231xx_set_power_mode(dev,
						POLARIS_AVMODE_DIGITAL);
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
			break;
		default:
			break;
		}
	} else/* Set Analog Power mode */ {
	/* set AGC mode to Analog */
		switch (dev->model) {
		case CX231XX_BOARD_CNXT_CARRAERA:
		case CX231XX_BOARD_CNXT_RDE_250:
		case CX231XX_BOARD_CNXT_SHELBY:
		case CX231XX_BOARD_CNXT_RDU_250:
		errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
			break;
		case CX231XX_BOARD_CNXT_RDE_253S:
		case CX231XX_BOARD_CNXT_RDU_253S:
747
		case CX231XX_BOARD_HAUPPAUGE_EXETER:
748
		case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
749
		case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
750 751
		case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
		case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
752 753 754 755 756 757
		errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
			break;
		default:
			break;
		}
	}
758

759
	return errCode ? -EINVAL : 0;
760 761 762
}
EXPORT_SYMBOL_GPL(cx231xx_set_mode);

763 764 765 766 767 768
int cx231xx_ep5_bulkout(struct cx231xx *dev, u8 *firmware, u16 size)
{
	int errCode = 0;
	int actlen, ret = -ENOMEM;
	u32 *buffer;

769
	buffer = kzalloc(4096, GFP_KERNEL);
770
	if (buffer == NULL) {
771
		pr_info("out of mem\n");
772 773 774 775 776
		return -ENOMEM;
	}
	memcpy(&buffer[0], firmware, 4096);

	ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 5),
777
			buffer, 4096, &actlen, 2000);
778 779

	if (ret)
780
		pr_info("bulk message failed: %d (%d/%d)", ret,
781
				size, actlen);
782 783 784
	else {
		errCode = actlen != size ? -1 : 0;
	}
785 786
	kfree(buffer);
	return errCode;
787 788
}

789 790 791
/*****************************************************************
*                URB Streaming functions                         *
******************************************************************/
792 793 794 795

/*
 * IRQ callback, called by URB callback
 */
796
static void cx231xx_isoc_irq_callback(struct urb *urb)
797
{
798 799 800 801
	struct cx231xx_dmaqueue *dma_q = urb->context;
	struct cx231xx_video_mode *vmode =
	    container_of(dma_q, struct cx231xx_video_mode, vidq);
	struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
802
	int i;
803

804 805 806 807 808 809 810 811 812 813 814
	switch (urb->status) {
	case 0:		/* success */
	case -ETIMEDOUT:	/* NAK */
		break;
	case -ECONNRESET:	/* kill */
	case -ENOENT:
	case -ESHUTDOWN:
		return;
	default:		/* error */
		cx231xx_isocdbg("urb completition error %d.\n", urb->status);
		break;
815 816 817 818
	}

	/* Copy data from URB */
	spin_lock(&dev->video_mode.slock);
819
	dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
820 821 822 823 824 825 826 827 828 829 830
	spin_unlock(&dev->video_mode.slock);

	/* Reset urb buffers */
	for (i = 0; i < urb->number_of_packets; i++) {
		urb->iso_frame_desc[i].status = 0;
		urb->iso_frame_desc[i].actual_length = 0;
	}

	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
	if (urb->status) {
		cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
831
				urb->status);
832 833
	}
}
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
/*****************************************************************
*                URB Streaming functions                         *
******************************************************************/

/*
 * IRQ callback, called by URB callback
 */
static void cx231xx_bulk_irq_callback(struct urb *urb)
{
	struct cx231xx_dmaqueue *dma_q = urb->context;
	struct cx231xx_video_mode *vmode =
	    container_of(dma_q, struct cx231xx_video_mode, vidq);
	struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);

	switch (urb->status) {
	case 0:		/* success */
	case -ETIMEDOUT:	/* NAK */
		break;
	case -ECONNRESET:	/* kill */
	case -ENOENT:
	case -ESHUTDOWN:
		return;
	default:		/* error */
		cx231xx_isocdbg("urb completition error %d.\n", urb->status);
		break;
	}

	/* Copy data from URB */
	spin_lock(&dev->video_mode.slock);
863
	dev->video_mode.bulk_ctl.bulk_copy(dev, urb);
864
	spin_unlock(&dev->video_mode.slock);
865

866 867 868 869 870 871 872
	/* Reset urb buffers */
	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
	if (urb->status) {
		cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
				urb->status);
	}
}
873 874 875 876 877
/*
 * Stop and Deallocate URBs
 */
void cx231xx_uninit_isoc(struct cx231xx *dev)
{
878
	struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
879 880 881 882 883 884 885 886 887
	struct urb *urb;
	int i;

	cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");

	dev->video_mode.isoc_ctl.nfields = -1;
	for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
		urb = dev->video_mode.isoc_ctl.urb[i];
		if (urb) {
888 889 890 891
			if (!irqs_disabled())
				usb_kill_urb(urb);
			else
				usb_unlink_urb(urb);
892 893

			if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
894 895 896 897 898
				usb_free_coherent(dev->udev,
						  urb->transfer_buffer_length,
						  dev->video_mode.isoc_ctl.
						  transfer_buffer[i],
						  urb->transfer_dma);
899 900 901 902 903 904 905 906 907
			}
			usb_free_urb(urb);
			dev->video_mode.isoc_ctl.urb[i] = NULL;
		}
		dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL;
	}

	kfree(dev->video_mode.isoc_ctl.urb);
	kfree(dev->video_mode.isoc_ctl.transfer_buffer);
908
	kfree(dma_q->p_left_data);
909 910 911 912

	dev->video_mode.isoc_ctl.urb = NULL;
	dev->video_mode.isoc_ctl.transfer_buffer = NULL;
	dev->video_mode.isoc_ctl.num_bufs = 0;
913 914 915 916 917 918 919
	dma_q->p_left_data = NULL;

	if (dev->mode_tv == 0)
		cx231xx_capture_start(dev, 0, Raw_Video);
	else
		cx231xx_capture_start(dev, 0, TS1_serial_mode);

920 921 922 923

}
EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
/*
 * Stop and Deallocate URBs
 */
void cx231xx_uninit_bulk(struct cx231xx *dev)
{
	struct urb *urb;
	int i;

	cx231xx_isocdbg("cx231xx: called cx231xx_uninit_bulk\n");

	dev->video_mode.bulk_ctl.nfields = -1;
	for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
		urb = dev->video_mode.bulk_ctl.urb[i];
		if (urb) {
			if (!irqs_disabled())
				usb_kill_urb(urb);
			else
				usb_unlink_urb(urb);

			if (dev->video_mode.bulk_ctl.transfer_buffer[i]) {
				usb_free_coherent(dev->udev,
						urb->transfer_buffer_length,
						dev->video_mode.isoc_ctl.
						transfer_buffer[i],
						urb->transfer_dma);
			}
			usb_free_urb(urb);
			dev->video_mode.bulk_ctl.urb[i] = NULL;
		}
		dev->video_mode.bulk_ctl.transfer_buffer[i] = NULL;
	}

	kfree(dev->video_mode.bulk_ctl.urb);
	kfree(dev->video_mode.bulk_ctl.transfer_buffer);

	dev->video_mode.bulk_ctl.urb = NULL;
	dev->video_mode.bulk_ctl.transfer_buffer = NULL;
	dev->video_mode.bulk_ctl.num_bufs = 0;

	if (dev->mode_tv == 0)
		cx231xx_capture_start(dev, 0, Raw_Video);
	else
		cx231xx_capture_start(dev, 0, TS1_serial_mode);


}
EXPORT_SYMBOL_GPL(cx231xx_uninit_bulk);

972 973 974 975
/*
 * Allocate URBs and start IRQ
 */
int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
976
		      int num_bufs, int max_pkt_size,
977
		      int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
978 979 980 981 982 983 984 985 986 987 988
{
	struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
	int i;
	int sb_size, pipe;
	struct urb *urb;
	int j, k;
	int rc;

	/* De-allocates all pending stuff */
	cx231xx_uninit_isoc(dev);

989 990
	dma_q->p_left_data = kzalloc(4096, GFP_KERNEL);
	if (dma_q->p_left_data == NULL) {
991
		pr_info("out of mem\n");
992 993 994 995 996
		return -ENOMEM;
	}



997 998
	dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
	dev->video_mode.isoc_ctl.num_bufs = num_bufs;
999 1000 1001 1002 1003 1004 1005 1006
	dma_q->pos = 0;
	dma_q->is_partial_line = 0;
	dma_q->last_sav = 0;
	dma_q->current_field = -1;
	dma_q->field1_done = 0;
	dma_q->lines_per_field = dev->height / 2;
	dma_q->bytes_left_in_line = dev->width << 1;
	dma_q->lines_completed = 0;
1007 1008 1009 1010 1011 1012 1013 1014
	dma_q->mpeg_buffer_done = 0;
	dma_q->left_data_count = 0;
	dma_q->mpeg_buffer_completed = 0;
	dma_q->add_ps_package_head = CX231XX_NEED_ADD_PS_PACKAGE_HEAD;
	dma_q->ps_head[0] = 0x00;
	dma_q->ps_head[1] = 0x00;
	dma_q->ps_head[2] = 0x01;
	dma_q->ps_head[3] = 0xBA;
1015 1016 1017 1018 1019
	for (i = 0; i < 8; i++)
		dma_q->partial_buf[i] = 0;

	dev->video_mode.isoc_ctl.urb =
	    kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1020
	if (!dev->video_mode.isoc_ctl.urb) {
1021
		pr_err("cannot alloc memory for usb buffers\n");
1022 1023 1024
		return -ENOMEM;
	}

1025 1026
	dev->video_mode.isoc_ctl.transfer_buffer =
	    kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1027
	if (!dev->video_mode.isoc_ctl.transfer_buffer) {
1028
		pr_err("cannot allocate memory for usbtransfer\n");
1029 1030 1031 1032 1033 1034 1035 1036 1037
		kfree(dev->video_mode.isoc_ctl.urb);
		return -ENOMEM;
	}

	dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size;
	dev->video_mode.isoc_ctl.buf = NULL;

	sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size;

1038 1039 1040 1041 1042 1043
	if (dev->mode_tv == 1)
		dev->video_mode.end_point_addr = 0x81;
	else
		dev->video_mode.end_point_addr = 0x84;


1044 1045 1046 1047
	/* allocate urbs and transfer buffers */
	for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
		urb = usb_alloc_urb(max_packets, GFP_KERNEL);
		if (!urb) {
1048
			pr_err("cannot alloc isoc_ctl.urb %i\n", i);
1049 1050 1051 1052 1053
			cx231xx_uninit_isoc(dev);
			return -ENOMEM;
		}
		dev->video_mode.isoc_ctl.urb[i] = urb;

1054
		dev->video_mode.isoc_ctl.transfer_buffer[i] =
1055 1056
		    usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
				       &urb->transfer_dma);
1057
		if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
1058
			pr_err("unable to allocate %i bytes for transfer"
1059 1060
				    " buffer %i%s\n",
				    sb_size, i,
1061
				    in_interrupt() ? " while in int" : "");
1062 1063 1064 1065 1066
			cx231xx_uninit_isoc(dev);
			return -ENOMEM;
		}
		memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);

1067 1068
		pipe =
		    usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
1069 1070

		usb_fill_int_urb(urb, dev->udev, pipe,
1071
				 dev->video_mode.isoc_ctl.transfer_buffer[i],
1072
				 sb_size, cx231xx_isoc_irq_callback, dma_q, 1);
1073 1074

		urb->number_of_packets = max_packets;
1075
		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1076 1077 1078 1079 1080

		k = 0;
		for (j = 0; j < max_packets; j++) {
			urb->iso_frame_desc[j].offset = k;
			urb->iso_frame_desc[j].length =
1081
			    dev->video_mode.isoc_ctl.max_pkt_size;
1082 1083 1084 1085 1086 1087 1088 1089
			k += dev->video_mode.isoc_ctl.max_pkt_size;
		}
	}

	init_waitqueue_head(&dma_q->wq);

	/* submit urbs and enables IRQ */
	for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
1090 1091
		rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
				    GFP_ATOMIC);
1092
		if (rc) {
1093
			pr_err("submit of urb %i failed (error=%i)\n", i,
1094
				    rc);
1095 1096 1097 1098 1099
			cx231xx_uninit_isoc(dev);
			return rc;
		}
	}

1100 1101 1102 1103
	if (dev->mode_tv == 0)
		cx231xx_capture_start(dev, 1, Raw_Video);
	else
		cx231xx_capture_start(dev, 1, TS1_serial_mode);
1104 1105 1106 1107 1108

	return 0;
}
EXPORT_SYMBOL_GPL(cx231xx_init_isoc);

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
/*
 * Allocate URBs and start IRQ
 */
int 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))
{
	struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
	int i;
	int sb_size, pipe;
	struct urb *urb;
	int rc;

	dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;

1124 1125
	cx231xx_coredbg("Setting Video mux to %d\n", dev->video_input);

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
	video_mux(dev, dev->video_input);

	/* De-allocates all pending stuff */
	cx231xx_uninit_bulk(dev);

	dev->video_mode.bulk_ctl.bulk_copy = bulk_copy;
	dev->video_mode.bulk_ctl.num_bufs = num_bufs;
	dma_q->pos = 0;
	dma_q->is_partial_line = 0;
	dma_q->last_sav = 0;
	dma_q->current_field = -1;
	dma_q->field1_done = 0;
	dma_q->lines_per_field = dev->height / 2;
	dma_q->bytes_left_in_line = dev->width << 1;
	dma_q->lines_completed = 0;
	dma_q->mpeg_buffer_done = 0;
	dma_q->left_data_count = 0;
	dma_q->mpeg_buffer_completed = 0;
	dma_q->ps_head[0] = 0x00;
	dma_q->ps_head[1] = 0x00;
	dma_q->ps_head[2] = 0x01;
	dma_q->ps_head[3] = 0xBA;
	for (i = 0; i < 8; i++)
		dma_q->partial_buf[i] = 0;

	dev->video_mode.bulk_ctl.urb =
	    kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
	if (!dev->video_mode.bulk_ctl.urb) {
1154
		pr_err("cannot alloc memory for usb buffers\n");
1155 1156 1157 1158 1159 1160
		return -ENOMEM;
	}

	dev->video_mode.bulk_ctl.transfer_buffer =
	    kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
	if (!dev->video_mode.bulk_ctl.transfer_buffer) {
1161
		pr_err("cannot allocate memory for usbtransfer\n");
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
		kfree(dev->video_mode.bulk_ctl.urb);
		return -ENOMEM;
	}

	dev->video_mode.bulk_ctl.max_pkt_size = max_pkt_size;
	dev->video_mode.bulk_ctl.buf = NULL;

	sb_size = max_packets * dev->video_mode.bulk_ctl.max_pkt_size;

	if (dev->mode_tv == 1)
		dev->video_mode.end_point_addr = 0x81;
	else
		dev->video_mode.end_point_addr = 0x84;


	/* allocate urbs and transfer buffers */
	for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
		urb = usb_alloc_urb(0, GFP_KERNEL);
		if (!urb) {
1181
			pr_err("cannot alloc bulk_ctl.urb %i\n", i);
1182 1183 1184 1185
			cx231xx_uninit_bulk(dev);
			return -ENOMEM;
		}
		dev->video_mode.bulk_ctl.urb[i] = urb;
1186
		urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1187 1188 1189 1190 1191

		dev->video_mode.bulk_ctl.transfer_buffer[i] =
		    usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
				     &urb->transfer_dma);
		if (!dev->video_mode.bulk_ctl.transfer_buffer[i]) {
1192
			pr_err("unable to allocate %i bytes for transfer"
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
				    " buffer %i%s\n",
				    sb_size, i,
				    in_interrupt() ? " while in int" : "");
			cx231xx_uninit_bulk(dev);
			return -ENOMEM;
		}
		memset(dev->video_mode.bulk_ctl.transfer_buffer[i], 0, sb_size);

		pipe = usb_rcvbulkpipe(dev->udev,
				 dev->video_mode.end_point_addr);
		usb_fill_bulk_urb(urb, dev->udev, pipe,
				  dev->video_mode.bulk_ctl.transfer_buffer[i],
				  sb_size, cx231xx_bulk_irq_callback, dma_q);
	}

	init_waitqueue_head(&dma_q->wq);

	/* submit urbs and enables IRQ */
	for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
		rc = usb_submit_urb(dev->video_mode.bulk_ctl.urb[i],
				    GFP_ATOMIC);
		if (rc) {
1215
			pr_err("submit of urb %i failed (error=%i)\n", i,
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
				    rc);
			cx231xx_uninit_bulk(dev);
			return rc;
		}
	}

	if (dev->mode_tv == 0)
		cx231xx_capture_start(dev, 1, Raw_Video);
	else
		cx231xx_capture_start(dev, 1, TS1_serial_mode);

	return 0;
}
EXPORT_SYMBOL_GPL(cx231xx_init_bulk);
void cx231xx_stop_TS1(struct cx231xx *dev)
{
	u8 val[4] = { 0, 0, 0, 0 };

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
	val[0] = 0x00;
	val[1] = 0x03;
	val[2] = 0x00;
	val[3] = 0x00;
	cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
			TS_MODE_REG, val, 4);

	val[0] = 0x00;
	val[1] = 0x70;
	val[2] = 0x04;
	val[3] = 0x00;
	cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
			TS1_CFG_REG, val, 4);
1247 1248 1249 1250 1251 1252
}
/* EXPORT_SYMBOL_GPL(cx231xx_stop_TS1); */
void cx231xx_start_TS1(struct cx231xx *dev)
{
	u8 val[4] = { 0, 0, 0, 0 };

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	val[0] = 0x03;
	val[1] = 0x03;
	val[2] = 0x00;
	val[3] = 0x00;
	cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
			TS_MODE_REG, val, 4);

	val[0] = 0x04;
	val[1] = 0xA3;
	val[2] = 0x3B;
	val[3] = 0x00;
	cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
			TS1_CFG_REG, val, 4);
1266 1267
}
/* EXPORT_SYMBOL_GPL(cx231xx_start_TS1); */
1268 1269 1270
/*****************************************************************
*             Device Init/UnInit functions                       *
******************************************************************/
1271 1272
int cx231xx_dev_init(struct cx231xx *dev)
{
1273
	int errCode = 0;
1274

1275
	/* Initialize I2C bus */
1276 1277 1278 1279

	/* External Master 1 Bus */
	dev->i2c_bus[0].nr = 0;
	dev->i2c_bus[0].dev = dev;
1280
	dev->i2c_bus[0].i2c_period = I2C_SPEED_100K;	/* 100 KHz */
1281 1282
	dev->i2c_bus[0].i2c_nostop = 0;
	dev->i2c_bus[0].i2c_reserve = 0;
1283 1284 1285 1286

	/* External Master 2 Bus */
	dev->i2c_bus[1].nr = 1;
	dev->i2c_bus[1].dev = dev;
1287
	dev->i2c_bus[1].i2c_period = I2C_SPEED_100K;	/* 100 KHz */
1288 1289
	dev->i2c_bus[1].i2c_nostop = 0;
	dev->i2c_bus[1].i2c_reserve = 0;
1290 1291 1292 1293

	/* Internal Master 3 Bus */
	dev->i2c_bus[2].nr = 2;
	dev->i2c_bus[2].dev = dev;
1294
	dev->i2c_bus[2].i2c_period = I2C_SPEED_100K;	/* 100kHz */
1295 1296
	dev->i2c_bus[2].i2c_nostop = 0;
	dev->i2c_bus[2].i2c_reserve = 0;
1297

1298
	/* register I2C buses */
1299 1300 1301 1302
	cx231xx_i2c_register(&dev->i2c_bus[0]);
	cx231xx_i2c_register(&dev->i2c_bus[1]);
	cx231xx_i2c_register(&dev->i2c_bus[2]);

1303 1304 1305
	cx231xx_i2c_mux_register(dev, 0);
	cx231xx_i2c_mux_register(dev, 1);

1306 1307 1308 1309 1310 1311
	/* scan the real bus segments in the order of physical port numbers */
	cx231xx_do_i2c_scan(dev, I2C_0);
	cx231xx_do_i2c_scan(dev, I2C_1_MUX_1);
	cx231xx_do_i2c_scan(dev, I2C_2);
	cx231xx_do_i2c_scan(dev, I2C_1_MUX_3);

1312
	/* init hardware */
1313
	/* Note : with out calling set power mode function,
1314
	afe can not be set up correctly */
1315
	if (dev->board.external_av) {
1316 1317 1318
		errCode = cx231xx_set_power_mode(dev,
				 POLARIS_AVMODE_ENXTERNAL_AV);
		if (errCode < 0) {
1319
			pr_err
1320 1321 1322 1323 1324 1325 1326 1327
			("%s: Failed to set Power - errCode [%d]!\n",
			__func__, errCode);
			return errCode;
		}
	} else {
		errCode = cx231xx_set_power_mode(dev,
				 POLARIS_AVMODE_ANALOGT_TV);
		if (errCode < 0) {
1328
			pr_err
1329 1330 1331 1332
			("%s: Failed to set Power - errCode [%d]!\n",
			__func__, errCode);
			return errCode;
		}
1333 1334
	}

1335 1336 1337
	/* reset the Tuner, if it is a Xceive tuner */
	if ((dev->board.tuner_type == TUNER_XC5000) ||
	    (dev->board.tuner_type == TUNER_XC2028))
1338 1339
			cx231xx_gpio_set(dev, dev->board.tuner_gpio);

1340
	/* initialize Colibri block */
1341
	errCode = cx231xx_afe_init_super_block(dev, 0x23c);
1342
	if (errCode < 0) {
1343
		pr_err
1344
		    ("%s: cx231xx_afe init super block - errCode [%d]!\n",
1345
		     __func__, errCode);
1346 1347
		return errCode;
	}
1348
	errCode = cx231xx_afe_init_channels(dev);
1349
	if (errCode < 0) {
1350
		pr_err
1351
		    ("%s: cx231xx_afe init channels - errCode [%d]!\n",
1352
		     __func__, errCode);
1353 1354 1355
		return errCode;
	}

1356 1357 1358
	/* Set DIF in By pass mode */
	errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
	if (errCode < 0) {
1359
		pr_err
1360 1361
		    ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
		     __func__, errCode);
1362 1363 1364
		return errCode;
	}

1365 1366
	/* I2S block related functions */
	errCode = cx231xx_i2s_blk_initialize(dev);
1367
	if (errCode < 0) {
1368
		pr_err
1369
		    ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
1370
		     __func__, errCode);
1371 1372 1373
		return errCode;
	}

1374 1375 1376
	/* init control pins */
	errCode = cx231xx_init_ctrl_pin_status(dev);
	if (errCode < 0) {
1377
		pr_err("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
1378
			       __func__, errCode);
1379 1380 1381
		return errCode;
	}

1382
	/* set AGC mode to Analog */
1383 1384 1385 1386 1387
	switch (dev->model) {
	case CX231XX_BOARD_CNXT_CARRAERA:
	case CX231XX_BOARD_CNXT_RDE_250:
	case CX231XX_BOARD_CNXT_SHELBY:
	case CX231XX_BOARD_CNXT_RDU_250:
1388
	errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
1389 1390 1391
		break;
	case CX231XX_BOARD_CNXT_RDE_253S:
	case CX231XX_BOARD_CNXT_RDU_253S:
1392
	case CX231XX_BOARD_HAUPPAUGE_EXETER:
1393
	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
1394
	case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
1395 1396
	case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
	case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
1397 1398 1399 1400 1401
	errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
		break;
	default:
		break;
	}
1402
	if (errCode < 0) {
1403
		pr_err
1404 1405
		    ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
		     __func__, errCode);
1406 1407 1408
		return errCode;
	}

1409 1410 1411 1412 1413 1414
	/* set all alternate settings to zero initially */
	cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
	cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
	cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
	if (dev->board.has_dvb)
		cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
1415

1416
	errCode = 0;
1417 1418 1419 1420 1421 1422
	return errCode;
}
EXPORT_SYMBOL_GPL(cx231xx_dev_init);

void cx231xx_dev_uninit(struct cx231xx *dev)
{
1423
	/* Un Initialize I2C bus */
1424 1425
	cx231xx_i2c_mux_unregister(dev, 1);
	cx231xx_i2c_mux_unregister(dev, 0);
1426 1427 1428 1429
	cx231xx_i2c_unregister(&dev->i2c_bus[2]);
	cx231xx_i2c_unregister(&dev->i2c_bus[1]);
	cx231xx_i2c_unregister(&dev->i2c_bus[0]);
}
1430
EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
1431

1432 1433 1434
/*****************************************************************
*              G P I O related functions                         *
******************************************************************/
1435
int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val,
1436
			  u8 len, u8 request, u8 direction)
1437
{
1438
	int status = 0;
1439
	struct VENDOR_REQUEST_IN ven_req;
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455

	/* Set wValue */
	ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff);

	/* set request */
	if (!request) {
		if (direction)
			ven_req.bRequest = VRT_GET_GPIO;	/* 0x8 gpio */
		else
			ven_req.bRequest = VRT_SET_GPIO;	/* 0x9 gpio */
	} else {
		if (direction)
			ven_req.bRequest = VRT_GET_GPIE;	/* 0xa gpie */
		else
			ven_req.bRequest = VRT_SET_GPIE;	/* 0xb gpie */
	}
1456

1457 1458
	/* set index value */
	ven_req.wIndex = (u16) (gpio_bit & 0xffff);
1459

1460 1461
	/* set wLength value */
	ven_req.wLength = len;
1462

1463 1464
	/* set bData value */
	ven_req.bData = 0;
1465

1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
	/* set the buffer for read / write */
	ven_req.pBuff = gpio_val;

	/* set the direction */
	if (direction) {
		ven_req.direction = USB_DIR_IN;
		memset(ven_req.pBuff, 0x00, ven_req.wLength);
	} else
		ven_req.direction = USB_DIR_OUT;


	/* call common vendor command request */
	status = cx231xx_send_vendor_cmd(dev, &ven_req);
	if (status < 0) {
1480
		pr_info
1481
		    ("UsbInterface::sendCommand, failed with status -%d\n",
1482 1483
		     status);
	}
1484

1485
	return status;
1486 1487 1488
}
EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);

1489 1490 1491
/*****************************************************************
 *    C O N T R O L - Register R E A D / W R I T E functions     *
 *****************************************************************/
1492 1493
int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
{
1494 1495 1496
	u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
	u32 tmp = 0;
	int status = 0;
1497

1498 1499 1500 1501
	status =
	    cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
	if (status < 0)
		return status;
1502

1503
	tmp = le32_to_cpu(*((__le32 *) value));
1504
	tmp |= mode;
1505

1506 1507 1508 1509
	value[0] = (u8) tmp;
	value[1] = (u8) (tmp >> 8);
	value[2] = (u8) (tmp >> 16);
	value[3] = (u8) (tmp >> 24);
1510

1511 1512
	status =
	    cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
1513

1514
	return status;
1515 1516
}

1517 1518 1519
/*****************************************************************
 *            I 2 C Internal C O N T R O L   functions           *
 *****************************************************************/
1520 1521 1522 1523 1524 1525 1526 1527 1528
int cx231xx_read_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
			  u8 saddr_len, u32 *data, u8 data_len, int master)
{
	int status = 0;
	struct cx231xx_i2c_xfer_data req_data;
	u8 value[64] = "0";

	if (saddr_len == 0)
		saddr = 0;
1529
	else if (saddr_len == 1)
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
		saddr &= 0xff;

	/* prepare xfer_data struct */
	req_data.dev_addr = dev_addr >> 1;
	req_data.direction = I2C_M_RD;
	req_data.saddr_len = saddr_len;
	req_data.saddr_dat = saddr;
	req_data.buf_size = data_len;
	req_data.p_buffer = (u8 *) value;

	/* usb send command */
	if (master == 0)
		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
					 &req_data);
	else if (master == 1)
		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
					 &req_data);
	else if (master == 2)
		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
					 &req_data);

	if (status >= 0) {
		/* Copy the data read back to main buffer */
		if (data_len == 1)
			*data = value[0];
		else if (data_len == 4)
			*data =
			    value[0] | value[1] << 8 | value[2] << 16 | value[3]
			    << 24;
		else if (data_len > 4)
			*data = value[saddr];
	}

	return status;
}

int cx231xx_write_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
			   u8 saddr_len, u32 data, u8 data_len, int master)
{
	int status = 0;
	u8 value[4] = { 0, 0, 0, 0 };
	struct cx231xx_i2c_xfer_data req_data;

	value[0] = (u8) data;
	value[1] = (u8) (data >> 8);
	value[2] = (u8) (data >> 16);
	value[3] = (u8) (data >> 24);

	if (saddr_len == 0)
		saddr = 0;
1580
	else if (saddr_len == 1)
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
		saddr &= 0xff;

	/* prepare xfer_data struct */
	req_data.dev_addr = dev_addr >> 1;
	req_data.direction = 0;
	req_data.saddr_len = saddr_len;
	req_data.saddr_dat = saddr;
	req_data.buf_size = data_len;
	req_data.p_buffer = value;

	/* usb send command */
	if (master == 0)
		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
				 &req_data);
	else if (master == 1)
		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
				 &req_data);
	else if (master == 2)
		status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
				 &req_data);

	return status;
}

1605
int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1606
			  u8 saddr_len, u32 *data, u8 data_len)
1607
{
1608 1609 1610 1611 1612 1613
	int status = 0;
	struct cx231xx_i2c_xfer_data req_data;
	u8 value[4] = { 0, 0, 0, 0 };

	if (saddr_len == 0)
		saddr = 0;
1614
	else if (saddr_len == 1)
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
		saddr &= 0xff;

	/* prepare xfer_data struct */
	req_data.dev_addr = dev_addr >> 1;
	req_data.direction = I2C_M_RD;
	req_data.saddr_len = saddr_len;
	req_data.saddr_dat = saddr;
	req_data.buf_size = data_len;
	req_data.p_buffer = (u8 *) value;

	/* usb send command */
	status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);

	if (status >= 0) {
		/* Copy the data read back to main buffer */
		if (data_len == 1)
			*data = value[0];
		else
			*data =
			    value[0] | value[1] << 8 | value[2] << 16 | value[3]
			    << 24;
	}

	return status;
1639 1640 1641
}

int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1642
			   u8 saddr_len, u32 data, u8 data_len)
1643
{
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
	int status = 0;
	u8 value[4] = { 0, 0, 0, 0 };
	struct cx231xx_i2c_xfer_data req_data;

	value[0] = (u8) data;
	value[1] = (u8) (data >> 8);
	value[2] = (u8) (data >> 16);
	value[3] = (u8) (data >> 24);

	if (saddr_len == 0)
		saddr = 0;
1655
	else if (saddr_len == 1)
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
		saddr &= 0xff;

	/* prepare xfer_data struct */
	req_data.dev_addr = dev_addr >> 1;
	req_data.direction = 0;
	req_data.saddr_len = saddr_len;
	req_data.saddr_dat = saddr;
	req_data.buf_size = data_len;
	req_data.p_buffer = value;

	/* usb send command */
	status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);

	return status;
1670 1671
}

1672 1673 1674
int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
			   u16 register_address, u8 bit_start, u8 bit_end,
			   u32 value)
1675
{
1676 1677 1678 1679 1680
	int status = 0;
	u32 tmp;
	u32 mask = 0;
	int i;

1681
	if (bit_start > (size - 1) || bit_end > (size - 1))
1682
		return -1;
1683

1684 1685 1686 1687 1688 1689 1690 1691 1692
	if (size == 8) {
		status =
		    cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
					  &tmp, 1);
	} else {
		status =
		    cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
					  &tmp, 4);
	}
1693

1694
	if (status < 0)
1695 1696 1697
		return status;

	mask = 1 << bit_end;
1698
	for (i = bit_end; i > bit_start && i > 0; i--)
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
		mask = mask + (1 << (i - 1));

	value <<= bit_start;

	if (size == 8) {
		tmp &= ~mask;
		tmp |= value;
		tmp &= 0xff;
		status =
		    cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
					   tmp, 1);
	} else {
		tmp &= ~mask;
		tmp |= value;
		status =
		    cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
					   tmp, 4);
	}

	return status;
}
1720 1721

int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
1722
					u16 saddr, u32 mask, u32 value)
1723
{
1724 1725
	u32 temp;
	int status = 0;
1726

1727
	status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
1728

1729 1730
	if (status < 0)
		return status;
1731

1732 1733
	temp &= ~mask;
	temp |= value;
1734

1735
	status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
1736

1737
	return status;
1738 1739 1740 1741
}

u32 cx231xx_set_field(u32 field_mask, u32 data)
{
1742
	u32 temp;
1743

1744
	for (temp = field_mask; (temp & 1) == 0; temp >>= 1)
1745
		data <<= 1;
1746

1747
	return data;
1748
}