1 /* 2 cx231xx-core.c - driver for Conexant Cx23100/101/102 3 USB video capture devices 4 5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com> 6 Based on em28xx driver 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 */ 22 23 #include "cx231xx.h" 24 #include <linux/init.h> 25 #include <linux/list.h> 26 #include <linux/module.h> 27 #include <linux/slab.h> 28 #include <linux/vmalloc.h> 29 #include <media/v4l2-common.h> 30 #include <media/tuner.h> 31 32 #include "cx231xx-reg.h" 33 34 /* #define ENABLE_DEBUG_ISOC_FRAMES */ 35 36 static unsigned int core_debug; 37 module_param(core_debug, int, 0644); 38 MODULE_PARM_DESC(core_debug, "enable debug messages [core]"); 39 40 #define cx231xx_coredbg(fmt, arg...) do {\ 41 if (core_debug) \ 42 printk(KERN_INFO "%s %s :"fmt, \ 43 dev->name, __func__ , ##arg); } while (0) 44 45 static unsigned int reg_debug; 46 module_param(reg_debug, int, 0644); 47 MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]"); 48 49 static int alt = CX231XX_PINOUT; 50 module_param(alt, int, 0644); 51 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint"); 52 53 #define cx231xx_isocdbg(fmt, arg...) do {\ 54 if (core_debug) \ 55 printk(KERN_INFO "%s %s :"fmt, \ 56 dev->name, __func__ , ##arg); } while (0) 57 58 /***************************************************************** 59 * Device control list functions * 60 ******************************************************************/ 61 62 LIST_HEAD(cx231xx_devlist); 63 static DEFINE_MUTEX(cx231xx_devlist_mutex); 64 65 /* 66 * cx231xx_realease_resources() 67 * unregisters the v4l2,i2c and usb devices 68 * called when the device gets disconected or at module unload 69 */ 70 void cx231xx_remove_from_devlist(struct cx231xx *dev) 71 { 72 if (dev == NULL) 73 return; 74 if (dev->udev == NULL) 75 return; 76 77 if (atomic_read(&dev->devlist_count) > 0) { 78 mutex_lock(&cx231xx_devlist_mutex); 79 list_del(&dev->devlist); 80 atomic_dec(&dev->devlist_count); 81 mutex_unlock(&cx231xx_devlist_mutex); 82 } 83 }; 84 85 void cx231xx_add_into_devlist(struct cx231xx *dev) 86 { 87 mutex_lock(&cx231xx_devlist_mutex); 88 list_add_tail(&dev->devlist, &cx231xx_devlist); 89 atomic_inc(&dev->devlist_count); 90 mutex_unlock(&cx231xx_devlist_mutex); 91 }; 92 93 static LIST_HEAD(cx231xx_extension_devlist); 94 95 int cx231xx_register_extension(struct cx231xx_ops *ops) 96 { 97 struct cx231xx *dev = NULL; 98 99 mutex_lock(&cx231xx_devlist_mutex); 100 list_add_tail(&ops->next, &cx231xx_extension_devlist); 101 list_for_each_entry(dev, &cx231xx_devlist, devlist) { 102 ops->init(dev); 103 dev_info(dev->dev, "%s initialized\n", ops->name); 104 } 105 mutex_unlock(&cx231xx_devlist_mutex); 106 return 0; 107 } 108 EXPORT_SYMBOL(cx231xx_register_extension); 109 110 void cx231xx_unregister_extension(struct cx231xx_ops *ops) 111 { 112 struct cx231xx *dev = NULL; 113 114 mutex_lock(&cx231xx_devlist_mutex); 115 list_for_each_entry(dev, &cx231xx_devlist, devlist) { 116 ops->fini(dev); 117 dev_info(dev->dev, "%s removed\n", ops->name); 118 } 119 120 list_del(&ops->next); 121 mutex_unlock(&cx231xx_devlist_mutex); 122 } 123 EXPORT_SYMBOL(cx231xx_unregister_extension); 124 125 void cx231xx_init_extension(struct cx231xx *dev) 126 { 127 struct cx231xx_ops *ops = NULL; 128 129 mutex_lock(&cx231xx_devlist_mutex); 130 if (!list_empty(&cx231xx_extension_devlist)) { 131 list_for_each_entry(ops, &cx231xx_extension_devlist, next) { 132 if (ops->init) 133 ops->init(dev); 134 } 135 } 136 mutex_unlock(&cx231xx_devlist_mutex); 137 } 138 139 void cx231xx_close_extension(struct cx231xx *dev) 140 { 141 struct cx231xx_ops *ops = NULL; 142 143 mutex_lock(&cx231xx_devlist_mutex); 144 if (!list_empty(&cx231xx_extension_devlist)) { 145 list_for_each_entry(ops, &cx231xx_extension_devlist, next) { 146 if (ops->fini) 147 ops->fini(dev); 148 } 149 } 150 mutex_unlock(&cx231xx_devlist_mutex); 151 } 152 153 /**************************************************************** 154 * U S B related functions * 155 *****************************************************************/ 156 int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus, 157 struct cx231xx_i2c_xfer_data *req_data) 158 { 159 int status = 0; 160 struct cx231xx *dev = i2c_bus->dev; 161 struct VENDOR_REQUEST_IN ven_req; 162 163 u8 saddr_len = 0; 164 u8 _i2c_period = 0; 165 u8 _i2c_nostop = 0; 166 u8 _i2c_reserve = 0; 167 168 if (dev->state & DEV_DISCONNECTED) 169 return -ENODEV; 170 171 /* Get the I2C period, nostop and reserve parameters */ 172 _i2c_period = i2c_bus->i2c_period; 173 _i2c_nostop = i2c_bus->i2c_nostop; 174 _i2c_reserve = i2c_bus->i2c_reserve; 175 176 saddr_len = req_data->saddr_len; 177 178 /* Set wValue */ 179 ven_req.wValue = (req_data->dev_addr << 9 | _i2c_period << 4 | 180 saddr_len << 2 | _i2c_nostop << 1 | I2C_SYNC | 181 _i2c_reserve << 6); 182 183 /* set channel number */ 184 if (req_data->direction & I2C_M_RD) { 185 /* channel number, for read,spec required channel_num +4 */ 186 ven_req.bRequest = i2c_bus->nr + 4; 187 } else 188 ven_req.bRequest = i2c_bus->nr; /* channel number, */ 189 190 /* set index value */ 191 switch (saddr_len) { 192 case 0: 193 ven_req.wIndex = 0; /* need check */ 194 break; 195 case 1: 196 ven_req.wIndex = (req_data->saddr_dat & 0xff); 197 break; 198 case 2: 199 ven_req.wIndex = req_data->saddr_dat; 200 break; 201 } 202 203 /* set wLength value */ 204 ven_req.wLength = req_data->buf_size; 205 206 /* set bData value */ 207 ven_req.bData = 0; 208 209 /* set the direction */ 210 if (req_data->direction) { 211 ven_req.direction = USB_DIR_IN; 212 memset(req_data->p_buffer, 0x00, ven_req.wLength); 213 } else 214 ven_req.direction = USB_DIR_OUT; 215 216 /* set the buffer for read / write */ 217 ven_req.pBuff = req_data->p_buffer; 218 219 220 /* call common vendor command request */ 221 status = cx231xx_send_vendor_cmd(dev, &ven_req); 222 if (status < 0 && !dev->i2c_scan_running) { 223 dev_err(dev->dev, "%s: failed with status -%d\n", 224 __func__, status); 225 } 226 227 return status; 228 } 229 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command); 230 231 /* 232 * Sends/Receives URB control messages, assuring to use a kalloced buffer 233 * for all operations (dev->urb_buf), to avoid using stacked buffers, as 234 * they aren't safe for usage with USB, due to DMA restrictions. 235 * Also implements the debug code for control URB's. 236 */ 237 static int __usb_control_msg(struct cx231xx *dev, unsigned int pipe, 238 __u8 request, __u8 requesttype, __u16 value, __u16 index, 239 void *data, __u16 size, int timeout) 240 { 241 int rc, i; 242 243 if (reg_debug) { 244 printk(KERN_DEBUG "%s: (pipe 0x%08x): " 245 "%s: %02x %02x %02x %02x %02x %02x %02x %02x ", 246 dev->name, 247 pipe, 248 (requesttype & USB_DIR_IN) ? "IN" : "OUT", 249 requesttype, 250 request, 251 value & 0xff, value >> 8, 252 index & 0xff, index >> 8, 253 size & 0xff, size >> 8); 254 if (!(requesttype & USB_DIR_IN)) { 255 printk(KERN_CONT ">>>"); 256 for (i = 0; i < size; i++) 257 printk(KERN_CONT " %02x", 258 ((unsigned char *)data)[i]); 259 } 260 } 261 262 /* Do the real call to usb_control_msg */ 263 mutex_lock(&dev->ctrl_urb_lock); 264 if (!(requesttype & USB_DIR_IN) && size) 265 memcpy(dev->urb_buf, data, size); 266 rc = usb_control_msg(dev->udev, pipe, request, requesttype, value, 267 index, dev->urb_buf, size, timeout); 268 if ((requesttype & USB_DIR_IN) && size) 269 memcpy(data, dev->urb_buf, size); 270 mutex_unlock(&dev->ctrl_urb_lock); 271 272 if (reg_debug) { 273 if (unlikely(rc < 0)) { 274 printk(KERN_CONT "FAILED!\n"); 275 return rc; 276 } 277 278 if ((requesttype & USB_DIR_IN)) { 279 printk(KERN_CONT "<<<"); 280 for (i = 0; i < size; i++) 281 printk(KERN_CONT " %02x", 282 ((unsigned char *)data)[i]); 283 } 284 printk(KERN_CONT "\n"); 285 } 286 287 return rc; 288 } 289 290 291 /* 292 * cx231xx_read_ctrl_reg() 293 * reads data from the usb device specifying bRequest and wValue 294 */ 295 int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, 296 char *buf, int len) 297 { 298 u8 val = 0; 299 int ret; 300 int pipe = usb_rcvctrlpipe(dev->udev, 0); 301 302 if (dev->state & DEV_DISCONNECTED) 303 return -ENODEV; 304 305 if (len > URB_MAX_CTRL_SIZE) 306 return -EINVAL; 307 308 switch (len) { 309 case 1: 310 val = ENABLE_ONE_BYTE; 311 break; 312 case 2: 313 val = ENABLE_TWE_BYTE; 314 break; 315 case 3: 316 val = ENABLE_THREE_BYTE; 317 break; 318 case 4: 319 val = ENABLE_FOUR_BYTE; 320 break; 321 default: 322 val = 0xFF; /* invalid option */ 323 } 324 325 if (val == 0xFF) 326 return -EINVAL; 327 328 ret = __usb_control_msg(dev, pipe, req, 329 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 330 val, reg, buf, len, HZ); 331 return ret; 332 } 333 334 int cx231xx_send_vendor_cmd(struct cx231xx *dev, 335 struct VENDOR_REQUEST_IN *ven_req) 336 { 337 int ret; 338 int pipe = 0; 339 int unsend_size = 0; 340 u8 *pdata; 341 342 if (dev->state & DEV_DISCONNECTED) 343 return -ENODEV; 344 345 if ((ven_req->wLength > URB_MAX_CTRL_SIZE)) 346 return -EINVAL; 347 348 if (ven_req->direction) 349 pipe = usb_rcvctrlpipe(dev->udev, 0); 350 else 351 pipe = usb_sndctrlpipe(dev->udev, 0); 352 353 /* 354 * If the cx23102 read more than 4 bytes with i2c bus, 355 * need chop to 4 byte per request 356 */ 357 if ((ven_req->wLength > 4) && ((ven_req->bRequest == 0x4) || 358 (ven_req->bRequest == 0x5) || 359 (ven_req->bRequest == 0x6))) { 360 unsend_size = 0; 361 pdata = ven_req->pBuff; 362 363 364 unsend_size = ven_req->wLength; 365 366 /* the first package */ 367 ven_req->wValue = ven_req->wValue & 0xFFFB; 368 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x2; 369 ret = __usb_control_msg(dev, pipe, ven_req->bRequest, 370 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 371 ven_req->wValue, ven_req->wIndex, pdata, 372 0x0004, HZ); 373 unsend_size = unsend_size - 4; 374 375 /* the middle package */ 376 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x42; 377 while (unsend_size - 4 > 0) { 378 pdata = pdata + 4; 379 ret = __usb_control_msg(dev, pipe, 380 ven_req->bRequest, 381 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 382 ven_req->wValue, ven_req->wIndex, pdata, 383 0x0004, HZ); 384 unsend_size = unsend_size - 4; 385 } 386 387 /* the last package */ 388 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x40; 389 pdata = pdata + 4; 390 ret = __usb_control_msg(dev, pipe, ven_req->bRequest, 391 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 392 ven_req->wValue, ven_req->wIndex, pdata, 393 unsend_size, HZ); 394 } else { 395 ret = __usb_control_msg(dev, pipe, ven_req->bRequest, 396 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 397 ven_req->wValue, ven_req->wIndex, 398 ven_req->pBuff, ven_req->wLength, HZ); 399 } 400 401 return ret; 402 } 403 404 /* 405 * cx231xx_write_ctrl_reg() 406 * sends data to the usb device, specifying bRequest 407 */ 408 int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf, 409 int len) 410 { 411 u8 val = 0; 412 int ret; 413 int pipe = usb_sndctrlpipe(dev->udev, 0); 414 415 if (dev->state & DEV_DISCONNECTED) 416 return -ENODEV; 417 418 if ((len < 1) || (len > URB_MAX_CTRL_SIZE)) 419 return -EINVAL; 420 421 switch (len) { 422 case 1: 423 val = ENABLE_ONE_BYTE; 424 break; 425 case 2: 426 val = ENABLE_TWE_BYTE; 427 break; 428 case 3: 429 val = ENABLE_THREE_BYTE; 430 break; 431 case 4: 432 val = ENABLE_FOUR_BYTE; 433 break; 434 default: 435 val = 0xFF; /* invalid option */ 436 } 437 438 if (val == 0xFF) 439 return -EINVAL; 440 441 if (reg_debug) { 442 int byte; 443 444 cx231xx_isocdbg("(pipe 0x%08x): " 445 "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>", 446 pipe, 447 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 448 req, 0, val, reg & 0xff, 449 reg >> 8, len & 0xff, len >> 8); 450 451 for (byte = 0; byte < len; byte++) 452 cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]); 453 cx231xx_isocdbg("\n"); 454 } 455 456 ret = __usb_control_msg(dev, pipe, req, 457 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 458 val, reg, buf, len, HZ); 459 460 return ret; 461 } 462 463 /**************************************************************** 464 * USB Alternate Setting functions * 465 *****************************************************************/ 466 467 int cx231xx_set_video_alternate(struct cx231xx *dev) 468 { 469 int errCode, prev_alt = dev->video_mode.alt; 470 unsigned int min_pkt_size = dev->width * 2 + 4; 471 u32 usb_interface_index = 0; 472 473 /* When image size is bigger than a certain value, 474 the frame size should be increased, otherwise, only 475 green screen will be received. 476 */ 477 if (dev->width * 2 * dev->height > 720 * 240 * 2) 478 min_pkt_size *= 2; 479 480 if (dev->width > 360) { 481 /* resolutions: 720,704,640 */ 482 dev->video_mode.alt = 3; 483 } else if (dev->width > 180) { 484 /* resolutions: 360,352,320,240 */ 485 dev->video_mode.alt = 2; 486 } else if (dev->width > 0) { 487 /* resolutions: 180,176,160,128,88 */ 488 dev->video_mode.alt = 1; 489 } else { 490 /* Change to alt0 BULK to release USB bandwidth */ 491 dev->video_mode.alt = 0; 492 } 493 494 if (dev->USE_ISO == 0) 495 dev->video_mode.alt = 0; 496 497 cx231xx_coredbg("dev->video_mode.alt= %d\n", dev->video_mode.alt); 498 499 /* Get the correct video interface Index */ 500 usb_interface_index = 501 dev->current_pcb_config.hs_config_info[0].interface_info. 502 video_index + 1; 503 504 if (dev->video_mode.alt != prev_alt) { 505 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n", 506 min_pkt_size, dev->video_mode.alt); 507 508 if (dev->video_mode.alt_max_pkt_size != NULL) 509 dev->video_mode.max_pkt_size = 510 dev->video_mode.alt_max_pkt_size[dev->video_mode.alt]; 511 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n", 512 dev->video_mode.alt, 513 dev->video_mode.max_pkt_size); 514 errCode = 515 usb_set_interface(dev->udev, usb_interface_index, 516 dev->video_mode.alt); 517 if (errCode < 0) { 518 dev_err(dev->dev, 519 "cannot change alt number to %d (error=%i)\n", 520 dev->video_mode.alt, errCode); 521 return errCode; 522 } 523 } 524 return 0; 525 } 526 527 int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt) 528 { 529 int status = 0; 530 u32 usb_interface_index = 0; 531 u32 max_pkt_size = 0; 532 533 switch (index) { 534 case INDEX_TS1: 535 usb_interface_index = 536 dev->current_pcb_config.hs_config_info[0].interface_info. 537 ts1_index + 1; 538 dev->ts1_mode.alt = alt; 539 if (dev->ts1_mode.alt_max_pkt_size != NULL) 540 max_pkt_size = dev->ts1_mode.max_pkt_size = 541 dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt]; 542 break; 543 case INDEX_TS2: 544 usb_interface_index = 545 dev->current_pcb_config.hs_config_info[0].interface_info. 546 ts2_index + 1; 547 break; 548 case INDEX_AUDIO: 549 usb_interface_index = 550 dev->current_pcb_config.hs_config_info[0].interface_info. 551 audio_index + 1; 552 dev->adev.alt = alt; 553 if (dev->adev.alt_max_pkt_size != NULL) 554 max_pkt_size = dev->adev.max_pkt_size = 555 dev->adev.alt_max_pkt_size[dev->adev.alt]; 556 break; 557 case INDEX_VIDEO: 558 usb_interface_index = 559 dev->current_pcb_config.hs_config_info[0].interface_info. 560 video_index + 1; 561 dev->video_mode.alt = alt; 562 if (dev->video_mode.alt_max_pkt_size != NULL) 563 max_pkt_size = dev->video_mode.max_pkt_size = 564 dev->video_mode.alt_max_pkt_size[dev->video_mode. 565 alt]; 566 break; 567 case INDEX_VANC: 568 if (dev->board.no_alt_vanc) 569 return 0; 570 usb_interface_index = 571 dev->current_pcb_config.hs_config_info[0].interface_info. 572 vanc_index + 1; 573 dev->vbi_mode.alt = alt; 574 if (dev->vbi_mode.alt_max_pkt_size != NULL) 575 max_pkt_size = dev->vbi_mode.max_pkt_size = 576 dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt]; 577 break; 578 case INDEX_HANC: 579 usb_interface_index = 580 dev->current_pcb_config.hs_config_info[0].interface_info. 581 hanc_index + 1; 582 dev->sliced_cc_mode.alt = alt; 583 if (dev->sliced_cc_mode.alt_max_pkt_size != NULL) 584 max_pkt_size = dev->sliced_cc_mode.max_pkt_size = 585 dev->sliced_cc_mode.alt_max_pkt_size[dev-> 586 sliced_cc_mode. 587 alt]; 588 break; 589 default: 590 break; 591 } 592 593 if (alt > 0 && max_pkt_size == 0) { 594 dev_err(dev->dev, 595 "can't change interface %d alt no. to %d: Max. Pkt size = 0\n", 596 usb_interface_index, alt); 597 /*To workaround error number=-71 on EP0 for videograbber, 598 need add following codes.*/ 599 if (dev->board.no_alt_vanc) 600 return -1; 601 } 602 603 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u," 604 "Interface = %d\n", alt, max_pkt_size, 605 usb_interface_index); 606 607 if (usb_interface_index > 0) { 608 status = usb_set_interface(dev->udev, usb_interface_index, alt); 609 if (status < 0) { 610 dev_err(dev->dev, 611 "can't change interface %d alt no. to %d (err=%i)\n", 612 usb_interface_index, alt, status); 613 return status; 614 } 615 } 616 617 return status; 618 } 619 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting); 620 621 int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio) 622 { 623 int rc = 0; 624 625 if (!gpio) 626 return rc; 627 628 /* Send GPIO reset sequences specified at board entry */ 629 while (gpio->sleep >= 0) { 630 rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val); 631 if (rc < 0) 632 return rc; 633 634 if (gpio->sleep > 0) 635 msleep(gpio->sleep); 636 637 gpio++; 638 } 639 return rc; 640 } 641 642 int cx231xx_demod_reset(struct cx231xx *dev) 643 { 644 645 u8 status = 0; 646 u8 value[4] = { 0, 0, 0, 0 }; 647 648 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN, 649 value, 4); 650 651 cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN, 652 value[0], value[1], value[2], value[3]); 653 654 cx231xx_coredbg("Enter cx231xx_demod_reset()\n"); 655 656 value[1] = (u8) 0x3; 657 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 658 PWR_CTL_EN, value, 4); 659 msleep(10); 660 661 value[1] = (u8) 0x0; 662 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 663 PWR_CTL_EN, value, 4); 664 msleep(10); 665 666 value[1] = (u8) 0x3; 667 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 668 PWR_CTL_EN, value, 4); 669 msleep(10); 670 671 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN, 672 value, 4); 673 674 cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN, 675 value[0], value[1], value[2], value[3]); 676 677 return status; 678 } 679 EXPORT_SYMBOL_GPL(cx231xx_demod_reset); 680 int is_fw_load(struct cx231xx *dev) 681 { 682 return cx231xx_check_fw(dev); 683 } 684 EXPORT_SYMBOL_GPL(is_fw_load); 685 686 int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode) 687 { 688 int errCode = 0; 689 690 if (dev->mode == set_mode) 691 return 0; 692 693 if (set_mode == CX231XX_SUSPEND) { 694 /* Set the chip in power saving mode */ 695 dev->mode = set_mode; 696 } 697 698 /* Resource is locked */ 699 if (dev->mode != CX231XX_SUSPEND) 700 return -EINVAL; 701 702 dev->mode = set_mode; 703 704 if (dev->mode == CX231XX_DIGITAL_MODE)/* Set Digital power mode */ { 705 /* set AGC mode to Digital */ 706 switch (dev->model) { 707 case CX231XX_BOARD_CNXT_CARRAERA: 708 case CX231XX_BOARD_CNXT_RDE_250: 709 case CX231XX_BOARD_CNXT_SHELBY: 710 case CX231XX_BOARD_CNXT_RDU_250: 711 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0); 712 break; 713 case CX231XX_BOARD_CNXT_RDE_253S: 714 case CX231XX_BOARD_CNXT_RDU_253S: 715 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1); 716 break; 717 case CX231XX_BOARD_HAUPPAUGE_EXETER: 718 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx: 719 errCode = cx231xx_set_power_mode(dev, 720 POLARIS_AVMODE_DIGITAL); 721 break; 722 default: 723 break; 724 } 725 } else/* Set Analog Power mode */ { 726 /* set AGC mode to Analog */ 727 switch (dev->model) { 728 case CX231XX_BOARD_CNXT_CARRAERA: 729 case CX231XX_BOARD_CNXT_RDE_250: 730 case CX231XX_BOARD_CNXT_SHELBY: 731 case CX231XX_BOARD_CNXT_RDU_250: 732 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1); 733 break; 734 case CX231XX_BOARD_CNXT_RDE_253S: 735 case CX231XX_BOARD_CNXT_RDU_253S: 736 case CX231XX_BOARD_HAUPPAUGE_EXETER: 737 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx: 738 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID: 739 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL: 740 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC: 741 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0); 742 break; 743 default: 744 break; 745 } 746 } 747 748 return errCode ? -EINVAL : 0; 749 } 750 EXPORT_SYMBOL_GPL(cx231xx_set_mode); 751 752 int cx231xx_ep5_bulkout(struct cx231xx *dev, u8 *firmware, u16 size) 753 { 754 int errCode = 0; 755 int actlen, ret = -ENOMEM; 756 u32 *buffer; 757 758 buffer = kzalloc(4096, GFP_KERNEL); 759 if (buffer == NULL) 760 return -ENOMEM; 761 memcpy(&buffer[0], firmware, 4096); 762 763 ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 5), 764 buffer, 4096, &actlen, 2000); 765 766 if (ret) 767 dev_err(dev->dev, 768 "bulk message failed: %d (%d/%d)", ret, 769 size, actlen); 770 else { 771 errCode = actlen != size ? -1 : 0; 772 } 773 kfree(buffer); 774 return errCode; 775 } 776 777 /***************************************************************** 778 * URB Streaming functions * 779 ******************************************************************/ 780 781 /* 782 * IRQ callback, called by URB callback 783 */ 784 static void cx231xx_isoc_irq_callback(struct urb *urb) 785 { 786 struct cx231xx_dmaqueue *dma_q = urb->context; 787 struct cx231xx_video_mode *vmode = 788 container_of(dma_q, struct cx231xx_video_mode, vidq); 789 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode); 790 int i; 791 792 switch (urb->status) { 793 case 0: /* success */ 794 case -ETIMEDOUT: /* NAK */ 795 break; 796 case -ECONNRESET: /* kill */ 797 case -ENOENT: 798 case -ESHUTDOWN: 799 return; 800 default: /* error */ 801 cx231xx_isocdbg("urb completition error %d.\n", urb->status); 802 break; 803 } 804 805 /* Copy data from URB */ 806 spin_lock(&dev->video_mode.slock); 807 dev->video_mode.isoc_ctl.isoc_copy(dev, urb); 808 spin_unlock(&dev->video_mode.slock); 809 810 /* Reset urb buffers */ 811 for (i = 0; i < urb->number_of_packets; i++) { 812 urb->iso_frame_desc[i].status = 0; 813 urb->iso_frame_desc[i].actual_length = 0; 814 } 815 816 urb->status = usb_submit_urb(urb, GFP_ATOMIC); 817 if (urb->status) { 818 cx231xx_isocdbg("urb resubmit failed (error=%i)\n", 819 urb->status); 820 } 821 } 822 /***************************************************************** 823 * URB Streaming functions * 824 ******************************************************************/ 825 826 /* 827 * IRQ callback, called by URB callback 828 */ 829 static void cx231xx_bulk_irq_callback(struct urb *urb) 830 { 831 struct cx231xx_dmaqueue *dma_q = urb->context; 832 struct cx231xx_video_mode *vmode = 833 container_of(dma_q, struct cx231xx_video_mode, vidq); 834 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode); 835 836 switch (urb->status) { 837 case 0: /* success */ 838 case -ETIMEDOUT: /* NAK */ 839 break; 840 case -ECONNRESET: /* kill */ 841 case -ENOENT: 842 case -ESHUTDOWN: 843 return; 844 default: /* error */ 845 cx231xx_isocdbg("urb completition error %d.\n", urb->status); 846 break; 847 } 848 849 /* Copy data from URB */ 850 spin_lock(&dev->video_mode.slock); 851 dev->video_mode.bulk_ctl.bulk_copy(dev, urb); 852 spin_unlock(&dev->video_mode.slock); 853 854 /* Reset urb buffers */ 855 urb->status = usb_submit_urb(urb, GFP_ATOMIC); 856 if (urb->status) { 857 cx231xx_isocdbg("urb resubmit failed (error=%i)\n", 858 urb->status); 859 } 860 } 861 /* 862 * Stop and Deallocate URBs 863 */ 864 void cx231xx_uninit_isoc(struct cx231xx *dev) 865 { 866 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq; 867 struct urb *urb; 868 int i; 869 870 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n"); 871 872 dev->video_mode.isoc_ctl.nfields = -1; 873 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) { 874 urb = dev->video_mode.isoc_ctl.urb[i]; 875 if (urb) { 876 if (!irqs_disabled()) 877 usb_kill_urb(urb); 878 else 879 usb_unlink_urb(urb); 880 881 if (dev->video_mode.isoc_ctl.transfer_buffer[i]) { 882 usb_free_coherent(dev->udev, 883 urb->transfer_buffer_length, 884 dev->video_mode.isoc_ctl. 885 transfer_buffer[i], 886 urb->transfer_dma); 887 } 888 usb_free_urb(urb); 889 dev->video_mode.isoc_ctl.urb[i] = NULL; 890 } 891 dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL; 892 } 893 894 kfree(dev->video_mode.isoc_ctl.urb); 895 kfree(dev->video_mode.isoc_ctl.transfer_buffer); 896 kfree(dma_q->p_left_data); 897 898 dev->video_mode.isoc_ctl.urb = NULL; 899 dev->video_mode.isoc_ctl.transfer_buffer = NULL; 900 dev->video_mode.isoc_ctl.num_bufs = 0; 901 dma_q->p_left_data = NULL; 902 903 if (dev->mode_tv == 0) 904 cx231xx_capture_start(dev, 0, Raw_Video); 905 else 906 cx231xx_capture_start(dev, 0, TS1_serial_mode); 907 908 909 } 910 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc); 911 912 /* 913 * Stop and Deallocate URBs 914 */ 915 void cx231xx_uninit_bulk(struct cx231xx *dev) 916 { 917 struct urb *urb; 918 int i; 919 920 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_bulk\n"); 921 922 dev->video_mode.bulk_ctl.nfields = -1; 923 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) { 924 urb = dev->video_mode.bulk_ctl.urb[i]; 925 if (urb) { 926 if (!irqs_disabled()) 927 usb_kill_urb(urb); 928 else 929 usb_unlink_urb(urb); 930 931 if (dev->video_mode.bulk_ctl.transfer_buffer[i]) { 932 usb_free_coherent(dev->udev, 933 urb->transfer_buffer_length, 934 dev->video_mode.isoc_ctl. 935 transfer_buffer[i], 936 urb->transfer_dma); 937 } 938 usb_free_urb(urb); 939 dev->video_mode.bulk_ctl.urb[i] = NULL; 940 } 941 dev->video_mode.bulk_ctl.transfer_buffer[i] = NULL; 942 } 943 944 kfree(dev->video_mode.bulk_ctl.urb); 945 kfree(dev->video_mode.bulk_ctl.transfer_buffer); 946 947 dev->video_mode.bulk_ctl.urb = NULL; 948 dev->video_mode.bulk_ctl.transfer_buffer = NULL; 949 dev->video_mode.bulk_ctl.num_bufs = 0; 950 951 if (dev->mode_tv == 0) 952 cx231xx_capture_start(dev, 0, Raw_Video); 953 else 954 cx231xx_capture_start(dev, 0, TS1_serial_mode); 955 956 957 } 958 EXPORT_SYMBOL_GPL(cx231xx_uninit_bulk); 959 960 /* 961 * Allocate URBs and start IRQ 962 */ 963 int cx231xx_init_isoc(struct cx231xx *dev, int max_packets, 964 int num_bufs, int max_pkt_size, 965 int (*isoc_copy) (struct cx231xx *dev, struct urb *urb)) 966 { 967 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq; 968 int i; 969 int sb_size, pipe; 970 struct urb *urb; 971 int j, k; 972 int rc; 973 974 /* De-allocates all pending stuff */ 975 cx231xx_uninit_isoc(dev); 976 977 dma_q->p_left_data = kzalloc(4096, GFP_KERNEL); 978 if (dma_q->p_left_data == NULL) 979 return -ENOMEM; 980 981 dev->video_mode.isoc_ctl.isoc_copy = isoc_copy; 982 dev->video_mode.isoc_ctl.num_bufs = num_bufs; 983 dma_q->pos = 0; 984 dma_q->is_partial_line = 0; 985 dma_q->last_sav = 0; 986 dma_q->current_field = -1; 987 dma_q->field1_done = 0; 988 dma_q->lines_per_field = dev->height / 2; 989 dma_q->bytes_left_in_line = dev->width << 1; 990 dma_q->lines_completed = 0; 991 dma_q->mpeg_buffer_done = 0; 992 dma_q->left_data_count = 0; 993 dma_q->mpeg_buffer_completed = 0; 994 dma_q->add_ps_package_head = CX231XX_NEED_ADD_PS_PACKAGE_HEAD; 995 dma_q->ps_head[0] = 0x00; 996 dma_q->ps_head[1] = 0x00; 997 dma_q->ps_head[2] = 0x01; 998 dma_q->ps_head[3] = 0xBA; 999 for (i = 0; i < 8; i++) 1000 dma_q->partial_buf[i] = 0; 1001 1002 dev->video_mode.isoc_ctl.urb = 1003 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); 1004 if (!dev->video_mode.isoc_ctl.urb) { 1005 dev_err(dev->dev, 1006 "cannot alloc memory for usb buffers\n"); 1007 return -ENOMEM; 1008 } 1009 1010 dev->video_mode.isoc_ctl.transfer_buffer = 1011 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); 1012 if (!dev->video_mode.isoc_ctl.transfer_buffer) { 1013 dev_err(dev->dev, 1014 "cannot allocate memory for usbtransfer\n"); 1015 kfree(dev->video_mode.isoc_ctl.urb); 1016 return -ENOMEM; 1017 } 1018 1019 dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size; 1020 dev->video_mode.isoc_ctl.buf = NULL; 1021 1022 sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size; 1023 1024 if (dev->mode_tv == 1) 1025 dev->video_mode.end_point_addr = 0x81; 1026 else 1027 dev->video_mode.end_point_addr = 0x84; 1028 1029 1030 /* allocate urbs and transfer buffers */ 1031 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) { 1032 urb = usb_alloc_urb(max_packets, GFP_KERNEL); 1033 if (!urb) { 1034 dev_err(dev->dev, 1035 "cannot alloc isoc_ctl.urb %i\n", i); 1036 cx231xx_uninit_isoc(dev); 1037 return -ENOMEM; 1038 } 1039 dev->video_mode.isoc_ctl.urb[i] = urb; 1040 1041 dev->video_mode.isoc_ctl.transfer_buffer[i] = 1042 usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL, 1043 &urb->transfer_dma); 1044 if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) { 1045 dev_err(dev->dev, 1046 "unable to allocate %i bytes for transfer buffer %i%s\n", 1047 sb_size, i, 1048 in_interrupt() ? " while in int" : ""); 1049 cx231xx_uninit_isoc(dev); 1050 return -ENOMEM; 1051 } 1052 memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size); 1053 1054 pipe = 1055 usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr); 1056 1057 usb_fill_int_urb(urb, dev->udev, pipe, 1058 dev->video_mode.isoc_ctl.transfer_buffer[i], 1059 sb_size, cx231xx_isoc_irq_callback, dma_q, 1); 1060 1061 urb->number_of_packets = max_packets; 1062 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; 1063 1064 k = 0; 1065 for (j = 0; j < max_packets; j++) { 1066 urb->iso_frame_desc[j].offset = k; 1067 urb->iso_frame_desc[j].length = 1068 dev->video_mode.isoc_ctl.max_pkt_size; 1069 k += dev->video_mode.isoc_ctl.max_pkt_size; 1070 } 1071 } 1072 1073 init_waitqueue_head(&dma_q->wq); 1074 1075 /* submit urbs and enables IRQ */ 1076 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) { 1077 rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i], 1078 GFP_ATOMIC); 1079 if (rc) { 1080 dev_err(dev->dev, 1081 "submit of urb %i failed (error=%i)\n", i, 1082 rc); 1083 cx231xx_uninit_isoc(dev); 1084 return rc; 1085 } 1086 } 1087 1088 if (dev->mode_tv == 0) 1089 cx231xx_capture_start(dev, 1, Raw_Video); 1090 else 1091 cx231xx_capture_start(dev, 1, TS1_serial_mode); 1092 1093 return 0; 1094 } 1095 EXPORT_SYMBOL_GPL(cx231xx_init_isoc); 1096 1097 /* 1098 * Allocate URBs and start IRQ 1099 */ 1100 int cx231xx_init_bulk(struct cx231xx *dev, int max_packets, 1101 int num_bufs, int max_pkt_size, 1102 int (*bulk_copy) (struct cx231xx *dev, struct urb *urb)) 1103 { 1104 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq; 1105 int i; 1106 int sb_size, pipe; 1107 struct urb *urb; 1108 int rc; 1109 1110 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input; 1111 1112 cx231xx_coredbg("Setting Video mux to %d\n", dev->video_input); 1113 1114 video_mux(dev, dev->video_input); 1115 1116 /* De-allocates all pending stuff */ 1117 cx231xx_uninit_bulk(dev); 1118 1119 dev->video_mode.bulk_ctl.bulk_copy = bulk_copy; 1120 dev->video_mode.bulk_ctl.num_bufs = num_bufs; 1121 dma_q->pos = 0; 1122 dma_q->is_partial_line = 0; 1123 dma_q->last_sav = 0; 1124 dma_q->current_field = -1; 1125 dma_q->field1_done = 0; 1126 dma_q->lines_per_field = dev->height / 2; 1127 dma_q->bytes_left_in_line = dev->width << 1; 1128 dma_q->lines_completed = 0; 1129 dma_q->mpeg_buffer_done = 0; 1130 dma_q->left_data_count = 0; 1131 dma_q->mpeg_buffer_completed = 0; 1132 dma_q->ps_head[0] = 0x00; 1133 dma_q->ps_head[1] = 0x00; 1134 dma_q->ps_head[2] = 0x01; 1135 dma_q->ps_head[3] = 0xBA; 1136 for (i = 0; i < 8; i++) 1137 dma_q->partial_buf[i] = 0; 1138 1139 dev->video_mode.bulk_ctl.urb = 1140 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); 1141 if (!dev->video_mode.bulk_ctl.urb) { 1142 dev_err(dev->dev, 1143 "cannot alloc memory for usb buffers\n"); 1144 return -ENOMEM; 1145 } 1146 1147 dev->video_mode.bulk_ctl.transfer_buffer = 1148 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); 1149 if (!dev->video_mode.bulk_ctl.transfer_buffer) { 1150 dev_err(dev->dev, 1151 "cannot allocate memory for usbtransfer\n"); 1152 kfree(dev->video_mode.bulk_ctl.urb); 1153 return -ENOMEM; 1154 } 1155 1156 dev->video_mode.bulk_ctl.max_pkt_size = max_pkt_size; 1157 dev->video_mode.bulk_ctl.buf = NULL; 1158 1159 sb_size = max_packets * dev->video_mode.bulk_ctl.max_pkt_size; 1160 1161 if (dev->mode_tv == 1) 1162 dev->video_mode.end_point_addr = 0x81; 1163 else 1164 dev->video_mode.end_point_addr = 0x84; 1165 1166 1167 /* allocate urbs and transfer buffers */ 1168 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) { 1169 urb = usb_alloc_urb(0, GFP_KERNEL); 1170 if (!urb) { 1171 dev_err(dev->dev, 1172 "cannot alloc bulk_ctl.urb %i\n", i); 1173 cx231xx_uninit_bulk(dev); 1174 return -ENOMEM; 1175 } 1176 dev->video_mode.bulk_ctl.urb[i] = urb; 1177 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 1178 1179 dev->video_mode.bulk_ctl.transfer_buffer[i] = 1180 usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL, 1181 &urb->transfer_dma); 1182 if (!dev->video_mode.bulk_ctl.transfer_buffer[i]) { 1183 dev_err(dev->dev, 1184 "unable to allocate %i bytes for transfer buffer %i%s\n", 1185 sb_size, i, 1186 in_interrupt() ? " while in int" : ""); 1187 cx231xx_uninit_bulk(dev); 1188 return -ENOMEM; 1189 } 1190 memset(dev->video_mode.bulk_ctl.transfer_buffer[i], 0, sb_size); 1191 1192 pipe = usb_rcvbulkpipe(dev->udev, 1193 dev->video_mode.end_point_addr); 1194 usb_fill_bulk_urb(urb, dev->udev, pipe, 1195 dev->video_mode.bulk_ctl.transfer_buffer[i], 1196 sb_size, cx231xx_bulk_irq_callback, dma_q); 1197 } 1198 1199 init_waitqueue_head(&dma_q->wq); 1200 1201 /* submit urbs and enables IRQ */ 1202 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) { 1203 rc = usb_submit_urb(dev->video_mode.bulk_ctl.urb[i], 1204 GFP_ATOMIC); 1205 if (rc) { 1206 dev_err(dev->dev, 1207 "submit of urb %i failed (error=%i)\n", i, rc); 1208 cx231xx_uninit_bulk(dev); 1209 return rc; 1210 } 1211 } 1212 1213 if (dev->mode_tv == 0) 1214 cx231xx_capture_start(dev, 1, Raw_Video); 1215 else 1216 cx231xx_capture_start(dev, 1, TS1_serial_mode); 1217 1218 return 0; 1219 } 1220 EXPORT_SYMBOL_GPL(cx231xx_init_bulk); 1221 void cx231xx_stop_TS1(struct cx231xx *dev) 1222 { 1223 u8 val[4] = { 0, 0, 0, 0 }; 1224 1225 val[0] = 0x00; 1226 val[1] = 0x03; 1227 val[2] = 0x00; 1228 val[3] = 0x00; 1229 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 1230 TS_MODE_REG, val, 4); 1231 1232 val[0] = 0x00; 1233 val[1] = 0x70; 1234 val[2] = 0x04; 1235 val[3] = 0x00; 1236 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 1237 TS1_CFG_REG, val, 4); 1238 } 1239 /* EXPORT_SYMBOL_GPL(cx231xx_stop_TS1); */ 1240 void cx231xx_start_TS1(struct cx231xx *dev) 1241 { 1242 u8 val[4] = { 0, 0, 0, 0 }; 1243 1244 val[0] = 0x03; 1245 val[1] = 0x03; 1246 val[2] = 0x00; 1247 val[3] = 0x00; 1248 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 1249 TS_MODE_REG, val, 4); 1250 1251 val[0] = 0x04; 1252 val[1] = 0xA3; 1253 val[2] = 0x3B; 1254 val[3] = 0x00; 1255 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 1256 TS1_CFG_REG, val, 4); 1257 } 1258 /* EXPORT_SYMBOL_GPL(cx231xx_start_TS1); */ 1259 /***************************************************************** 1260 * Device Init/UnInit functions * 1261 ******************************************************************/ 1262 int cx231xx_dev_init(struct cx231xx *dev) 1263 { 1264 int errCode = 0; 1265 1266 /* Initialize I2C bus */ 1267 1268 /* External Master 1 Bus */ 1269 dev->i2c_bus[0].nr = 0; 1270 dev->i2c_bus[0].dev = dev; 1271 dev->i2c_bus[0].i2c_period = I2C_SPEED_100K; /* 100 KHz */ 1272 dev->i2c_bus[0].i2c_nostop = 0; 1273 dev->i2c_bus[0].i2c_reserve = 0; 1274 1275 /* External Master 2 Bus */ 1276 dev->i2c_bus[1].nr = 1; 1277 dev->i2c_bus[1].dev = dev; 1278 dev->i2c_bus[1].i2c_period = I2C_SPEED_100K; /* 100 KHz */ 1279 dev->i2c_bus[1].i2c_nostop = 0; 1280 dev->i2c_bus[1].i2c_reserve = 0; 1281 1282 /* Internal Master 3 Bus */ 1283 dev->i2c_bus[2].nr = 2; 1284 dev->i2c_bus[2].dev = dev; 1285 dev->i2c_bus[2].i2c_period = I2C_SPEED_100K; /* 100kHz */ 1286 dev->i2c_bus[2].i2c_nostop = 0; 1287 dev->i2c_bus[2].i2c_reserve = 0; 1288 1289 /* register I2C buses */ 1290 cx231xx_i2c_register(&dev->i2c_bus[0]); 1291 cx231xx_i2c_register(&dev->i2c_bus[1]); 1292 cx231xx_i2c_register(&dev->i2c_bus[2]); 1293 1294 cx231xx_i2c_mux_register(dev, 0); 1295 cx231xx_i2c_mux_register(dev, 1); 1296 1297 /* scan the real bus segments in the order of physical port numbers */ 1298 cx231xx_do_i2c_scan(dev, I2C_0); 1299 cx231xx_do_i2c_scan(dev, I2C_1_MUX_1); 1300 cx231xx_do_i2c_scan(dev, I2C_2); 1301 cx231xx_do_i2c_scan(dev, I2C_1_MUX_3); 1302 1303 /* init hardware */ 1304 /* Note : with out calling set power mode function, 1305 afe can not be set up correctly */ 1306 if (dev->board.external_av) { 1307 errCode = cx231xx_set_power_mode(dev, 1308 POLARIS_AVMODE_ENXTERNAL_AV); 1309 if (errCode < 0) { 1310 dev_err(dev->dev, 1311 "%s: Failed to set Power - errCode [%d]!\n", 1312 __func__, errCode); 1313 return errCode; 1314 } 1315 } else { 1316 errCode = cx231xx_set_power_mode(dev, 1317 POLARIS_AVMODE_ANALOGT_TV); 1318 if (errCode < 0) { 1319 dev_err(dev->dev, 1320 "%s: Failed to set Power - errCode [%d]!\n", 1321 __func__, errCode); 1322 return errCode; 1323 } 1324 } 1325 1326 /* reset the Tuner, if it is a Xceive tuner */ 1327 if ((dev->board.tuner_type == TUNER_XC5000) || 1328 (dev->board.tuner_type == TUNER_XC2028)) 1329 cx231xx_gpio_set(dev, dev->board.tuner_gpio); 1330 1331 /* initialize Colibri block */ 1332 errCode = cx231xx_afe_init_super_block(dev, 0x23c); 1333 if (errCode < 0) { 1334 dev_err(dev->dev, 1335 "%s: cx231xx_afe init super block - errCode [%d]!\n", 1336 __func__, errCode); 1337 return errCode; 1338 } 1339 errCode = cx231xx_afe_init_channels(dev); 1340 if (errCode < 0) { 1341 dev_err(dev->dev, 1342 "%s: cx231xx_afe init channels - errCode [%d]!\n", 1343 __func__, errCode); 1344 return errCode; 1345 } 1346 1347 /* Set DIF in By pass mode */ 1348 errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND); 1349 if (errCode < 0) { 1350 dev_err(dev->dev, 1351 "%s: cx231xx_dif set to By pass mode - errCode [%d]!\n", 1352 __func__, errCode); 1353 return errCode; 1354 } 1355 1356 /* I2S block related functions */ 1357 errCode = cx231xx_i2s_blk_initialize(dev); 1358 if (errCode < 0) { 1359 dev_err(dev->dev, 1360 "%s: cx231xx_i2s block initialize - errCode [%d]!\n", 1361 __func__, errCode); 1362 return errCode; 1363 } 1364 1365 /* init control pins */ 1366 errCode = cx231xx_init_ctrl_pin_status(dev); 1367 if (errCode < 0) { 1368 dev_err(dev->dev, 1369 "%s: cx231xx_init ctrl pins - errCode [%d]!\n", 1370 __func__, errCode); 1371 return errCode; 1372 } 1373 1374 /* set AGC mode to Analog */ 1375 switch (dev->model) { 1376 case CX231XX_BOARD_CNXT_CARRAERA: 1377 case CX231XX_BOARD_CNXT_RDE_250: 1378 case CX231XX_BOARD_CNXT_SHELBY: 1379 case CX231XX_BOARD_CNXT_RDU_250: 1380 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1); 1381 break; 1382 case CX231XX_BOARD_CNXT_RDE_253S: 1383 case CX231XX_BOARD_CNXT_RDU_253S: 1384 case CX231XX_BOARD_HAUPPAUGE_EXETER: 1385 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx: 1386 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID: 1387 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL: 1388 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC: 1389 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0); 1390 break; 1391 default: 1392 break; 1393 } 1394 if (errCode < 0) { 1395 dev_err(dev->dev, 1396 "%s: cx231xx_AGC mode to Analog - errCode [%d]!\n", 1397 __func__, errCode); 1398 return errCode; 1399 } 1400 1401 /* set all alternate settings to zero initially */ 1402 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0); 1403 cx231xx_set_alt_setting(dev, INDEX_VANC, 0); 1404 cx231xx_set_alt_setting(dev, INDEX_HANC, 0); 1405 if (dev->board.has_dvb) 1406 cx231xx_set_alt_setting(dev, INDEX_TS1, 0); 1407 1408 errCode = 0; 1409 return errCode; 1410 } 1411 EXPORT_SYMBOL_GPL(cx231xx_dev_init); 1412 1413 void cx231xx_dev_uninit(struct cx231xx *dev) 1414 { 1415 /* Un Initialize I2C bus */ 1416 cx231xx_i2c_mux_unregister(dev, 1); 1417 cx231xx_i2c_mux_unregister(dev, 0); 1418 cx231xx_i2c_unregister(&dev->i2c_bus[2]); 1419 cx231xx_i2c_unregister(&dev->i2c_bus[1]); 1420 cx231xx_i2c_unregister(&dev->i2c_bus[0]); 1421 } 1422 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit); 1423 1424 /***************************************************************** 1425 * G P I O related functions * 1426 ******************************************************************/ 1427 int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val, 1428 u8 len, u8 request, u8 direction) 1429 { 1430 int status = 0; 1431 struct VENDOR_REQUEST_IN ven_req; 1432 1433 /* Set wValue */ 1434 ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff); 1435 1436 /* set request */ 1437 if (!request) { 1438 if (direction) 1439 ven_req.bRequest = VRT_GET_GPIO; /* 0x8 gpio */ 1440 else 1441 ven_req.bRequest = VRT_SET_GPIO; /* 0x9 gpio */ 1442 } else { 1443 if (direction) 1444 ven_req.bRequest = VRT_GET_GPIE; /* 0xa gpie */ 1445 else 1446 ven_req.bRequest = VRT_SET_GPIE; /* 0xb gpie */ 1447 } 1448 1449 /* set index value */ 1450 ven_req.wIndex = (u16) (gpio_bit & 0xffff); 1451 1452 /* set wLength value */ 1453 ven_req.wLength = len; 1454 1455 /* set bData value */ 1456 ven_req.bData = 0; 1457 1458 /* set the buffer for read / write */ 1459 ven_req.pBuff = gpio_val; 1460 1461 /* set the direction */ 1462 if (direction) { 1463 ven_req.direction = USB_DIR_IN; 1464 memset(ven_req.pBuff, 0x00, ven_req.wLength); 1465 } else 1466 ven_req.direction = USB_DIR_OUT; 1467 1468 1469 /* call common vendor command request */ 1470 status = cx231xx_send_vendor_cmd(dev, &ven_req); 1471 if (status < 0) { 1472 dev_err(dev->dev, "%s: failed with status -%d\n", 1473 __func__, status); 1474 } 1475 1476 return status; 1477 } 1478 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd); 1479 1480 /***************************************************************** 1481 * C O N T R O L - Register R E A D / W R I T E functions * 1482 *****************************************************************/ 1483 int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode) 1484 { 1485 u8 value[4] = { 0x0, 0x0, 0x0, 0x0 }; 1486 u32 tmp = 0; 1487 int status = 0; 1488 1489 status = 1490 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4); 1491 if (status < 0) 1492 return status; 1493 1494 tmp = le32_to_cpu(*((__le32 *) value)); 1495 tmp |= mode; 1496 1497 value[0] = (u8) tmp; 1498 value[1] = (u8) (tmp >> 8); 1499 value[2] = (u8) (tmp >> 16); 1500 value[3] = (u8) (tmp >> 24); 1501 1502 status = 1503 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4); 1504 1505 return status; 1506 } 1507 1508 /***************************************************************** 1509 * I 2 C Internal C O N T R O L functions * 1510 *****************************************************************/ 1511 int cx231xx_read_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr, 1512 u8 saddr_len, u32 *data, u8 data_len, int master) 1513 { 1514 int status = 0; 1515 struct cx231xx_i2c_xfer_data req_data; 1516 u8 value[64] = "0"; 1517 1518 if (saddr_len == 0) 1519 saddr = 0; 1520 else if (saddr_len == 1) 1521 saddr &= 0xff; 1522 1523 /* prepare xfer_data struct */ 1524 req_data.dev_addr = dev_addr >> 1; 1525 req_data.direction = I2C_M_RD; 1526 req_data.saddr_len = saddr_len; 1527 req_data.saddr_dat = saddr; 1528 req_data.buf_size = data_len; 1529 req_data.p_buffer = (u8 *) value; 1530 1531 /* usb send command */ 1532 if (master == 0) 1533 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], 1534 &req_data); 1535 else if (master == 1) 1536 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1], 1537 &req_data); 1538 else if (master == 2) 1539 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2], 1540 &req_data); 1541 1542 if (status >= 0) { 1543 /* Copy the data read back to main buffer */ 1544 if (data_len == 1) 1545 *data = value[0]; 1546 else if (data_len == 4) 1547 *data = 1548 value[0] | value[1] << 8 | value[2] << 16 | value[3] 1549 << 24; 1550 else if (data_len > 4) 1551 *data = value[saddr]; 1552 } 1553 1554 return status; 1555 } 1556 1557 int cx231xx_write_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr, 1558 u8 saddr_len, u32 data, u8 data_len, int master) 1559 { 1560 int status = 0; 1561 u8 value[4] = { 0, 0, 0, 0 }; 1562 struct cx231xx_i2c_xfer_data req_data; 1563 1564 value[0] = (u8) data; 1565 value[1] = (u8) (data >> 8); 1566 value[2] = (u8) (data >> 16); 1567 value[3] = (u8) (data >> 24); 1568 1569 if (saddr_len == 0) 1570 saddr = 0; 1571 else if (saddr_len == 1) 1572 saddr &= 0xff; 1573 1574 /* prepare xfer_data struct */ 1575 req_data.dev_addr = dev_addr >> 1; 1576 req_data.direction = 0; 1577 req_data.saddr_len = saddr_len; 1578 req_data.saddr_dat = saddr; 1579 req_data.buf_size = data_len; 1580 req_data.p_buffer = value; 1581 1582 /* usb send command */ 1583 if (master == 0) 1584 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], 1585 &req_data); 1586 else if (master == 1) 1587 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1], 1588 &req_data); 1589 else if (master == 2) 1590 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2], 1591 &req_data); 1592 1593 return status; 1594 } 1595 1596 int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr, 1597 u8 saddr_len, u32 *data, u8 data_len) 1598 { 1599 int status = 0; 1600 struct cx231xx_i2c_xfer_data req_data; 1601 u8 value[4] = { 0, 0, 0, 0 }; 1602 1603 if (saddr_len == 0) 1604 saddr = 0; 1605 else if (saddr_len == 1) 1606 saddr &= 0xff; 1607 1608 /* prepare xfer_data struct */ 1609 req_data.dev_addr = dev_addr >> 1; 1610 req_data.direction = I2C_M_RD; 1611 req_data.saddr_len = saddr_len; 1612 req_data.saddr_dat = saddr; 1613 req_data.buf_size = data_len; 1614 req_data.p_buffer = (u8 *) value; 1615 1616 /* usb send command */ 1617 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data); 1618 1619 if (status >= 0) { 1620 /* Copy the data read back to main buffer */ 1621 if (data_len == 1) 1622 *data = value[0]; 1623 else 1624 *data = 1625 value[0] | value[1] << 8 | value[2] << 16 | value[3] 1626 << 24; 1627 } 1628 1629 return status; 1630 } 1631 1632 int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr, 1633 u8 saddr_len, u32 data, u8 data_len) 1634 { 1635 int status = 0; 1636 u8 value[4] = { 0, 0, 0, 0 }; 1637 struct cx231xx_i2c_xfer_data req_data; 1638 1639 value[0] = (u8) data; 1640 value[1] = (u8) (data >> 8); 1641 value[2] = (u8) (data >> 16); 1642 value[3] = (u8) (data >> 24); 1643 1644 if (saddr_len == 0) 1645 saddr = 0; 1646 else if (saddr_len == 1) 1647 saddr &= 0xff; 1648 1649 /* prepare xfer_data struct */ 1650 req_data.dev_addr = dev_addr >> 1; 1651 req_data.direction = 0; 1652 req_data.saddr_len = saddr_len; 1653 req_data.saddr_dat = saddr; 1654 req_data.buf_size = data_len; 1655 req_data.p_buffer = value; 1656 1657 /* usb send command */ 1658 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data); 1659 1660 return status; 1661 } 1662 1663 int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size, 1664 u16 register_address, u8 bit_start, u8 bit_end, 1665 u32 value) 1666 { 1667 int status = 0; 1668 u32 tmp; 1669 u32 mask = 0; 1670 int i; 1671 1672 if (bit_start > (size - 1) || bit_end > (size - 1)) 1673 return -1; 1674 1675 if (size == 8) { 1676 status = 1677 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2, 1678 &tmp, 1); 1679 } else { 1680 status = 1681 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2, 1682 &tmp, 4); 1683 } 1684 1685 if (status < 0) 1686 return status; 1687 1688 mask = 1 << bit_end; 1689 for (i = bit_end; i > bit_start && i > 0; i--) 1690 mask = mask + (1 << (i - 1)); 1691 1692 value <<= bit_start; 1693 1694 if (size == 8) { 1695 tmp &= ~mask; 1696 tmp |= value; 1697 tmp &= 0xff; 1698 status = 1699 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2, 1700 tmp, 1); 1701 } else { 1702 tmp &= ~mask; 1703 tmp |= value; 1704 status = 1705 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2, 1706 tmp, 4); 1707 } 1708 1709 return status; 1710 } 1711 1712 int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr, 1713 u16 saddr, u32 mask, u32 value) 1714 { 1715 u32 temp; 1716 int status = 0; 1717 1718 status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4); 1719 1720 if (status < 0) 1721 return status; 1722 1723 temp &= ~mask; 1724 temp |= value; 1725 1726 status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4); 1727 1728 return status; 1729 } 1730 1731 u32 cx231xx_set_field(u32 field_mask, u32 data) 1732 { 1733 u32 temp; 1734 1735 for (temp = field_mask; (temp & 1) == 0; temp >>= 1) 1736 data <<= 1; 1737 1738 return data; 1739 } 1740