cx231xx-core.c 30.1 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 23 24 25 26 27 28 29 30 31 32 33 34 35

   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.
 */

#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/vmalloc.h>
#include <media/v4l2-common.h>

#include "cx231xx.h"
#include "cx231xx-reg.h"

/* #define ENABLE_DEBUG_ISOC_FRAMES */

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

#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;
45 46
module_param(reg_debug, int, 0644);
MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

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

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)

62 63 64
/*****************************************************************
*             Device control list functions     				 *
******************************************************************/
65 66 67 68 69

static LIST_HEAD(cx231xx_devlist);
static DEFINE_MUTEX(cx231xx_devlist_mutex);

struct cx231xx *cx231xx_get_device(int minor,
70
				   enum v4l2_buf_type *fh_type, int *has_radio)
71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
	struct cx231xx *h, *dev = NULL;

	*fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	*has_radio = 0;

	mutex_lock(&cx231xx_devlist_mutex);
	list_for_each_entry(h, &cx231xx_devlist, devlist) {
		if (h->vdev->minor == minor)
			dev = h;
		if (h->vbi_dev->minor == minor) {
			dev = h;
			*fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
		}
85
		if (h->radio_dev && h->radio_dev->minor == minor) {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
			dev = h;
			*has_radio = 1;
		}
	}
	mutex_unlock(&cx231xx_devlist_mutex);

	return dev;
}

/*
 * 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)
{
	mutex_lock(&cx231xx_devlist_mutex);
	list_del(&dev->devlist);
	mutex_unlock(&cx231xx_devlist_mutex);
};

void cx231xx_add_into_devlist(struct cx231xx *dev)
{
	mutex_lock(&cx231xx_devlist_mutex);
	list_add_tail(&dev->devlist, &cx231xx_devlist);
	mutex_unlock(&cx231xx_devlist_mutex);
};

static LIST_HEAD(cx231xx_extension_devlist);
static DEFINE_MUTEX(cx231xx_extension_devlist_lock);

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

	mutex_lock(&cx231xx_devlist_mutex);
	mutex_lock(&cx231xx_extension_devlist_lock);
	list_add_tail(&ops->next, &cx231xx_extension_devlist);
	list_for_each_entry(dev, &cx231xx_devlist, devlist) {
		if (dev)
			ops->init(dev);
	}
	cx231xx_info("Cx231xx: Initialized (%s) extension\n", ops->name);
	mutex_unlock(&cx231xx_extension_devlist_lock);
	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);
	list_for_each_entry(dev, &cx231xx_devlist, devlist) {
		if (dev)
			ops->fini(dev);
	}

	mutex_lock(&cx231xx_extension_devlist_lock);
	cx231xx_info("Cx231xx: Removed (%s) extension\n", ops->name);
	list_del(&ops->next);
	mutex_unlock(&cx231xx_extension_devlist_lock);
	mutex_unlock(&cx231xx_devlist_mutex);
}
151
EXPORT_SYMBOL(cx231xx_unregister_extension);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

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

	mutex_lock(&cx231xx_extension_devlist_lock);
	if (!list_empty(&cx231xx_extension_devlist)) {
		list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
			if (ops->init)
				ops->init(dev);
		}
	}
	mutex_unlock(&cx231xx_extension_devlist_lock);
}

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

	mutex_lock(&cx231xx_extension_devlist_lock);
	if (!list_empty(&cx231xx_extension_devlist)) {
		list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
			if (ops->fini)
				ops->fini(dev);
		}
	}
	mutex_unlock(&cx231xx_extension_devlist_lock);
}

181 182 183
/****************************************************************
*               U S B related functions                         *
*****************************************************************/
184
int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
185
			     struct cx231xx_i2c_xfer_data *req_data)
186
{
187 188
	int status = 0;
	struct cx231xx *dev = i2c_bus->dev;
189
	struct VENDOR_REQUEST_IN ven_req;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

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

	/* 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 */
216 217 218 219
	if (req_data->direction & I2C_M_RD) {
		/* channel number, for read,spec required channel_num +4 */
		ven_req.bRequest = i2c_bus->nr + 4;
	} else
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		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) {
		cx231xx_info
256
		    ("UsbInterface::sendCommand, failed with status -%d\n",
257 258 259 260
		     status);
	}

	return status;
261 262
}
EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
263

264 265 266 267 268
/*
 * 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,
269
			  char *buf, int len)
270
{
271
	u8 val = 0;
272 273 274 275 276 277 278 279 280
	int ret;
	int pipe = usb_rcvctrlpipe(dev->udev, 0);

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

	if (len > URB_MAX_CTRL_SIZE)
		return -EINVAL;

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	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;
300 301 302

	if (reg_debug) {
		cx231xx_isocdbg("(pipe 0x%08x): "
303 304 305 306 307
				"IN:  %02x %02x %02x %02x %02x %02x %02x %02x ",
				pipe,
				USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
				req, 0, val,
				reg & 0xff, reg >> 8, len & 0xff, len >> 8);
308 309
	}

310
	mutex_lock(&dev->ctrl_urb_lock);
311 312 313 314 315 316 317 318 319 320 321 322
	ret = usb_control_msg(dev->udev, pipe, req,
			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			      val, reg, dev->urb_buf, len, HZ);
	if (ret < 0) {
		cx231xx_isocdbg(" failed!\n");
		/* mutex_unlock(&dev->ctrl_urb_lock); */
		return ret;
	}

	if (len)
		memcpy(buf, dev->urb_buf, len);

323
	mutex_unlock(&dev->ctrl_urb_lock);
324 325 326 327 328 329 330 331 332 333 334 335 336

	if (reg_debug) {
		int byte;

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

	return ret;
}

337 338
int cx231xx_send_vendor_cmd(struct cx231xx *dev,
				struct VENDOR_REQUEST_IN *ven_req)
339
{
340
	int ret;
341 342 343 344 345 346 347 348
	int pipe = 0;

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

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

349 350 351 352
	if (ven_req->direction)
		pipe = usb_rcvctrlpipe(dev->udev, 0);
	else
		pipe = usb_sndctrlpipe(dev->udev, 0);
353 354 355 356 357

	if (reg_debug) {
		int byte;

		cx231xx_isocdbg("(pipe 0x%08x): "
358 359 360 361 362 363
				"OUT: %02x %02x %02x %04x %04x %04x >>>",
				pipe,
				ven_req->
				direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
				ven_req->bRequest, 0, ven_req->wValue,
				ven_req->wIndex, ven_req->wLength);
364 365

		for (byte = 0; byte < ven_req->wLength; byte++)
366 367
			cx231xx_isocdbg(" %02x",
					(unsigned char)ven_req->pBuff[byte]);
368 369 370
		cx231xx_isocdbg("\n");
	}

371
	mutex_lock(&dev->ctrl_urb_lock);
372
	ret = usb_control_msg(dev->udev, pipe, ven_req->bRequest,
373 374 375 376
			      ven_req->
			      direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			      ven_req->wValue, ven_req->wIndex, ven_req->pBuff,
			      ven_req->wLength, HZ);
377
	mutex_unlock(&dev->ctrl_urb_lock);
378 379 380 381 382 383 384 385 386

	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,
387
			   int len)
388
{
389
	u8 val = 0;
390 391 392 393 394 395 396 397 398
	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;

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	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;
418 419 420 421 422

	if (reg_debug) {
		int byte;

		cx231xx_isocdbg("(pipe 0x%08x): "
423 424 425 426 427
			"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);
428 429 430 431 432 433

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

434
	mutex_lock(&dev->ctrl_urb_lock);
435 436 437 438
	memcpy(dev->urb_buf, buf, len);
	ret = usb_control_msg(dev->udev, pipe, req,
			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			      val, reg, dev->urb_buf, len, HZ);
439
	mutex_unlock(&dev->ctrl_urb_lock);
440 441 442 443

	return ret;
}

444 445 446
/****************************************************************
*           USB Alternate Setting functions                     *
*****************************************************************/
447 448 449 450 451

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;
452
	u32 usb_interface_index = 0;
453 454 455 456 457 458 459 460

	/* 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;

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	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;
	}

	/* Get the correct video interface Index */
	usb_interface_index =
	    dev->current_pcb_config.hs_config_info[0].interface_info.
	    video_index + 1;
479 480 481 482

	if (dev->video_mode.alt != prev_alt) {
		cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
				min_pkt_size, dev->video_mode.alt);
483 484
		dev->video_mode.max_pkt_size =
		    dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
485
		cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
486 487 488
				dev->video_mode.alt,
				dev->video_mode.max_pkt_size);
		cx231xx_info
489
		    (" setting alt %d with wMaxPktSize=%u , Interface = %d\n",
490 491 492 493 494
		     dev->video_mode.alt, dev->video_mode.max_pkt_size,
		     usb_interface_index);
		errCode =
		    usb_set_interface(dev->udev, usb_interface_index,
				      dev->video_mode.alt);
495
		if (errCode < 0) {
496
			cx231xx_errdev
497
			    ("cannot change alt number to %d (error=%i)\n",
498
			     dev->video_mode.alt, errCode);
499 500 501 502 503 504 505 506
			return errCode;
		}
	}
	return 0;
}

int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
{
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	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;
		dev->video_mode.alt = alt;
		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:
		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) {
		cx231xx_errdev
571 572
		("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
		usb_interface_index, alt);
573 574 575 576 577 578 579 580 581
		return -1;
	}

	cx231xx_info
	    (" setting alternate %d with wMaxPacketSize=%u , Interface = %d\n",
	     alt, max_pkt_size, usb_interface_index);

	if (usb_interface_index > 0) {
		status = usb_set_interface(dev->udev, usb_interface_index, alt);
582
		if (status < 0) {
583
			cx231xx_errdev
584 585
			("can't change interface %d alt no. to %d (err=%i)\n",
			usb_interface_index, alt, status);
586 587
			return status;
		}
588
	}
589

590
	return status;
591 592 593 594 595 596 597 598 599 600 601 602
}
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) {
603 604 605
		rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
		if (rc < 0)
			return rc;
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

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

		gpio++;
	}
	return rc;
}

int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
{
	if (dev->mode == set_mode)
		return 0;

	if (set_mode == CX231XX_SUSPEND) {
621
		/* Set the chip in power saving mode */
622 623 624 625 626 627 628 629 630
		dev->mode = set_mode;
	}

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

	dev->mode = set_mode;

631 632 633 634 635
	if (dev->mode == CX231XX_DIGITAL_MODE)
		;/* Set Digital power mode */
	else
		;/* Set Analog Power mode */

636
	return 0;
637 638 639
}
EXPORT_SYMBOL_GPL(cx231xx_set_mode);

640 641 642
/*****************************************************************
*                URB Streaming functions                         *
******************************************************************/
643 644 645 646 647 648

/*
 * IRQ callback, called by URB callback
 */
static void cx231xx_irq_callback(struct urb *urb)
{
649 650 651 652
	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);
653 654
	int rc, i;

655 656 657 658 659 660 661 662 663 664 665
	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;
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
	}

	/* Copy data from URB */
	spin_lock(&dev->video_mode.slock);
	rc = dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
	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 = 0;

	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
	if (urb->status) {
		cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
683
				urb->status);
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	}
}

/*
 * Stop and Deallocate URBs
 */
void cx231xx_uninit_isoc(struct cx231xx *dev)
{
	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) {
701 702 703 704
			if (!irqs_disabled())
				usb_kill_urb(urb);
			else
				usb_unlink_urb(urb);
705 706 707

			if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
				usb_buffer_free(dev->udev,
708 709 710 711
						urb->transfer_buffer_length,
						dev->video_mode.isoc_ctl.
						transfer_buffer[i],
						urb->transfer_dma);
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
			}
			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);

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

	cx231xx_capture_start(dev, 0, Raw_Video);
}
EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);

/*
 * Allocate URBs and start IRQ
 */
int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
734
		      int num_bufs, int max_pkt_size,
735
		      int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
736 737 738 739 740 741 742 743 744 745
{
	struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
	int i;
	int sb_size, pipe;
	struct urb *urb;
	int j, k;
	int rc;

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

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

748 749
	cx231xx_info("Setting Video mux to %d\n", dev->video_input);
	video_mux(dev, dev->video_input);
750 751 752 753 754 755

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

	dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
	dev->video_mode.isoc_ctl.num_bufs = num_bufs;
756 757 758 759 760 761 762 763 764 765 766 767 768
	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;
	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);
769 770 771 772 773
	if (!dev->video_mode.isoc_ctl.urb) {
		cx231xx_errdev("cannot alloc memory for usb buffers\n");
		return -ENOMEM;
	}

774 775
	dev->video_mode.isoc_ctl.transfer_buffer =
	    kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
	if (!dev->video_mode.isoc_ctl.transfer_buffer) {
		cx231xx_errdev("cannot allocate memory for usbtransfer\n");
		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;

	/* 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) {
			cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i);
			cx231xx_uninit_isoc(dev);
			return -ENOMEM;
		}
		dev->video_mode.isoc_ctl.urb[i] = urb;

797 798 799
		dev->video_mode.isoc_ctl.transfer_buffer[i] =
		    usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL,
				     &urb->transfer_dma);
800 801
		if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
			cx231xx_err("unable to allocate %i bytes for transfer"
802 803
				    " buffer %i%s\n",
				    sb_size, i,
804
				    in_interrupt() ? " while in int" : "");
805 806 807 808 809
			cx231xx_uninit_isoc(dev);
			return -ENOMEM;
		}
		memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);

810 811
		pipe =
		    usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
812 813

		usb_fill_int_urb(urb, dev->udev, pipe,
814 815
				 dev->video_mode.isoc_ctl.transfer_buffer[i],
				 sb_size, cx231xx_irq_callback, dma_q, 1);
816 817 818 819 820 821 822 823

		urb->number_of_packets = max_packets;
		urb->transfer_flags = URB_ISO_ASAP;

		k = 0;
		for (j = 0; j < max_packets; j++) {
			urb->iso_frame_desc[j].offset = k;
			urb->iso_frame_desc[j].length =
824
			    dev->video_mode.isoc_ctl.max_pkt_size;
825 826 827 828 829 830 831 832
			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++) {
833 834
		rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
				    GFP_ATOMIC);
835 836
		if (rc) {
			cx231xx_err("submit of urb %i failed (error=%i)\n", i,
837
				    rc);
838 839 840 841 842
			cx231xx_uninit_isoc(dev);
			return rc;
		}
	}

843
	cx231xx_capture_start(dev, 1, Raw_Video);
844 845 846 847 848

	return 0;
}
EXPORT_SYMBOL_GPL(cx231xx_init_isoc);

849 850 851
/*****************************************************************
*             Device Init/UnInit functions                       *
******************************************************************/
852 853
int cx231xx_dev_init(struct cx231xx *dev)
{
854
	int errCode = 0;
855

856
	/* Initialize I2C bus */
857 858 859 860

	/* External Master 1 Bus */
	dev->i2c_bus[0].nr = 0;
	dev->i2c_bus[0].dev = dev;
861 862 863
	dev->i2c_bus[0].i2c_period = I2C_SPEED_1M;	/* 1MHz */
	dev->i2c_bus[0].i2c_nostop = 0;
	dev->i2c_bus[0].i2c_reserve = 0;
864 865 866 867

	/* External Master 2 Bus */
	dev->i2c_bus[1].nr = 1;
	dev->i2c_bus[1].dev = dev;
868 869 870
	dev->i2c_bus[1].i2c_period = I2C_SPEED_1M;	/* 1MHz */
	dev->i2c_bus[1].i2c_nostop = 0;
	dev->i2c_bus[1].i2c_reserve = 0;
871 872 873 874

	/* Internal Master 3 Bus */
	dev->i2c_bus[2].nr = 2;
	dev->i2c_bus[2].dev = dev;
875 876 877
	dev->i2c_bus[2].i2c_period = I2C_SPEED_400K;	/* 400kHz */
	dev->i2c_bus[2].i2c_nostop = 0;
	dev->i2c_bus[2].i2c_reserve = 0;
878

879
	/* register I2C buses */
880 881 882 883
	cx231xx_i2c_register(&dev->i2c_bus[0]);
	cx231xx_i2c_register(&dev->i2c_bus[1]);
	cx231xx_i2c_register(&dev->i2c_bus[2]);

884
	/* init hardware */
885
	/* Note : with out calling set power mode function,
886
	afe can not be set up correctly */
887 888 889
	errCode = cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
	if (errCode < 0) {
		cx231xx_errdev
890
		    ("%s: Failed to set Power - errCode [%d]!\n",
891
		     __func__, errCode);
892 893 894
		return errCode;
	}

895
	/* initialize Colibri block */
896
	errCode = cx231xx_afe_init_super_block(dev, 0x23c);
897
	if (errCode < 0) {
898
		cx231xx_errdev
899
		    ("%s: cx231xx_afe init super block - errCode [%d]!\n",
900
		     __func__, errCode);
901 902
		return errCode;
	}
903
	errCode = cx231xx_afe_init_channels(dev);
904 905
	if (errCode < 0) {
		cx231xx_errdev
906
		    ("%s: cx231xx_afe init channels - errCode [%d]!\n",
907
		     __func__, errCode);
908 909 910
		return errCode;
	}

911 912 913 914 915 916
	/* Set DIF in By pass mode */
	errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
	if (errCode < 0) {
		cx231xx_errdev
		    ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
		     __func__, errCode);
917 918 919
		return errCode;
	}

920 921
	/* I2S block related functions */
	errCode = cx231xx_i2s_blk_initialize(dev);
922 923
	if (errCode < 0) {
		cx231xx_errdev
924
		    ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
925
		     __func__, errCode);
926 927 928
		return errCode;
	}

929 930 931
	/* init control pins */
	errCode = cx231xx_init_ctrl_pin_status(dev);
	if (errCode < 0) {
932
		cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
933
			       __func__, errCode);
934 935 936
		return errCode;
	}

937 938 939 940 941 942
	/* set AGC mode to Analog */
	errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
	if (errCode < 0) {
		cx231xx_errdev
		    ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
		     __func__, errCode);
943 944 945
		return errCode;
	}

946 947 948 949 950 951
	/* 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);
952

953 954
	/* set the I2C master port to 3 on channel 1 */
	errCode = cx231xx_enable_i2c_for_tuner(dev, I2C_3);
955 956 957 958 959 960 961

	return errCode;
}
EXPORT_SYMBOL_GPL(cx231xx_dev_init);

void cx231xx_dev_uninit(struct cx231xx *dev)
{
962
	/* Un Initialize I2C bus */
963 964 965 966
	cx231xx_i2c_unregister(&dev->i2c_bus[2]);
	cx231xx_i2c_unregister(&dev->i2c_bus[1]);
	cx231xx_i2c_unregister(&dev->i2c_bus[0]);
}
967
EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
968

969 970 971
/*****************************************************************
*              G P I O related functions                         *
******************************************************************/
972 973
int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 * gpio_val,
			  u8 len, u8 request, u8 direction)
974
{
975
	int status = 0;
976
	struct VENDOR_REQUEST_IN ven_req;
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992

	/* 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 */
	}
993

994 995
	/* set index value */
	ven_req.wIndex = (u16) (gpio_bit & 0xffff);
996

997 998
	/* set wLength value */
	ven_req.wLength = len;
999

1000 1001
	/* set bData value */
	ven_req.bData = 0;
1002

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	/* 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) {
		cx231xx_info
1018
		    ("UsbInterface::sendCommand, failed with status -%d\n",
1019 1020
		     status);
	}
1021

1022
	return status;
1023 1024 1025
}
EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);

1026 1027 1028
/*****************************************************************
 *    C O N T R O L - Register R E A D / W R I T E functions     *
 *****************************************************************/
1029 1030
int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
{
1031 1032 1033
	u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
	u32 tmp = 0;
	int status = 0;
1034

1035 1036 1037 1038
	status =
	    cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
	if (status < 0)
		return status;
1039

1040 1041
	tmp = *((u32 *) value);
	tmp |= mode;
1042

1043 1044 1045 1046
	value[0] = (u8) tmp;
	value[1] = (u8) (tmp >> 8);
	value[2] = (u8) (tmp >> 16);
	value[3] = (u8) (tmp >> 24);
1047

1048 1049
	status =
	    cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
1050

1051
	return status;
1052 1053
}

1054 1055 1056
/*****************************************************************
 *            I 2 C Internal C O N T R O L   functions           *
 *****************************************************************/
1057
int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1058
			  u8 saddr_len, u32 *data, u8 data_len)
1059
{
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	int status = 0;
	struct cx231xx_i2c_xfer_data req_data;
	u8 value[4] = { 0, 0, 0, 0 };

	if (saddr_len == 0)
		saddr = 0;
	else if (saddr_len == 0)
		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;
1091 1092 1093
}

int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1094
			   u8 saddr_len, u32 data, u8 data_len)
1095
{
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	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;
	else if (saddr_len == 0)
		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;
1122 1123
}

1124 1125 1126
int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
			   u16 register_address, u8 bit_start, u8 bit_end,
			   u32 value)
1127
{
1128 1129 1130 1131 1132
	int status = 0;
	u32 tmp;
	u32 mask = 0;
	int i;

1133
	if (bit_start > (size - 1) || bit_end > (size - 1))
1134
		return -1;
1135

1136 1137 1138 1139 1140 1141 1142 1143 1144
	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);
	}
1145

1146
	if (status < 0)
1147 1148 1149
		return status;

	mask = 1 << bit_end;
1150
	for (i = bit_end; i > bit_start && i > 0; i--)
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
		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;
}
1172 1173

int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
1174
					u16 saddr, u32 mask, u32 value)
1175
{
1176 1177
	u32 temp;
	int status = 0;
1178

1179
	status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
1180

1181 1182
	if (status < 0)
		return status;
1183

1184 1185
	temp &= ~mask;
	temp |= value;
1186

1187
	status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
1188

1189
	return status;
1190 1191 1192 1193
}

u32 cx231xx_set_field(u32 field_mask, u32 data)
{
1194
	u32 temp;
1195

1196
	for (temp = field_mask; (temp & 1) == 0; temp >>= 1)
1197
		data <<= 1;
1198

1199
	return data;
1200
}