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 = -1; 756 int ret = -ENOMEM; 757 u32 *buffer; 758 759 buffer = kzalloc(4096, GFP_KERNEL); 760 if (buffer == NULL) 761 return -ENOMEM; 762 memcpy(&buffer[0], firmware, 4096); 763 764 ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 5), 765 buffer, 4096, &actlen, 2000); 766 767 if (ret) 768 dev_err(dev->dev, 769 "bulk message failed: %d (%d/%d)", ret, 770 size, actlen); 771 else { 772 errCode = actlen != size ? -1 : 0; 773 } 774 kfree(buffer); 775 return errCode; 776 } 777 778 /***************************************************************** 779 * URB Streaming functions * 780 ******************************************************************/ 781 782 /* 783 * IRQ callback, called by URB callback 784 */ 785 static void cx231xx_isoc_irq_callback(struct urb *urb) 786 { 787 struct cx231xx_dmaqueue *dma_q = urb->context; 788 struct cx231xx_video_mode *vmode = 789 container_of(dma_q, struct cx231xx_video_mode, vidq); 790 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode); 791 int i; 792 793 switch (urb->status) { 794 case 0: /* success */ 795 case -ETIMEDOUT: /* NAK */ 796 break; 797 case -ECONNRESET: /* kill */ 798 case -ENOENT: 799 case -ESHUTDOWN: 800 return; 801 default: /* error */ 802 cx231xx_isocdbg("urb completition error %d.\n", urb->status); 803 break; 804 } 805 806 /* Copy data from URB */ 807 spin_lock(&dev->video_mode.slock); 808 dev->video_mode.isoc_ctl.isoc_copy(dev, urb); 809 spin_unlock(&dev->video_mode.slock); 810 811 /* Reset urb buffers */ 812 for (i = 0; i < urb->number_of_packets; i++) { 813 urb->iso_frame_desc[i].status = 0; 814 urb->iso_frame_desc[i].actual_length = 0; 815 } 816 817 urb->status = usb_submit_urb(urb, GFP_ATOMIC); 818 if (urb->status) { 819 cx231xx_isocdbg("urb resubmit failed (error=%i)\n", 820 urb->status); 821 } 822 } 823 /***************************************************************** 824 * URB Streaming functions * 825 ******************************************************************/ 826 827 /* 828 * IRQ callback, called by URB callback 829 */ 830 static void cx231xx_bulk_irq_callback(struct urb *urb) 831 { 832 struct cx231xx_dmaqueue *dma_q = urb->context; 833 struct cx231xx_video_mode *vmode = 834 container_of(dma_q, struct cx231xx_video_mode, vidq); 835 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode); 836 837 switch (urb->status) { 838 case 0: /* success */ 839 case -ETIMEDOUT: /* NAK */ 840 break; 841 case -ECONNRESET: /* kill */ 842 case -ENOENT: 843 case -ESHUTDOWN: 844 return; 845 default: /* error */ 846 cx231xx_isocdbg("urb completition error %d.\n", urb->status); 847 break; 848 } 849 850 /* Copy data from URB */ 851 spin_lock(&dev->video_mode.slock); 852 dev->video_mode.bulk_ctl.bulk_copy(dev, urb); 853 spin_unlock(&dev->video_mode.slock); 854 855 /* Reset urb buffers */ 856 urb->status = usb_submit_urb(urb, GFP_ATOMIC); 857 if (urb->status) { 858 cx231xx_isocdbg("urb resubmit failed (error=%i)\n", 859 urb->status); 860 } 861 } 862 /* 863 * Stop and Deallocate URBs 864 */ 865 void cx231xx_uninit_isoc(struct cx231xx *dev) 866 { 867 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq; 868 struct urb *urb; 869 int i; 870 871 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n"); 872 873 dev->video_mode.isoc_ctl.nfields = -1; 874 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) { 875 urb = dev->video_mode.isoc_ctl.urb[i]; 876 if (urb) { 877 if (!irqs_disabled()) 878 usb_kill_urb(urb); 879 else 880 usb_unlink_urb(urb); 881 882 if (dev->video_mode.isoc_ctl.transfer_buffer[i]) { 883 usb_free_coherent(dev->udev, 884 urb->transfer_buffer_length, 885 dev->video_mode.isoc_ctl. 886 transfer_buffer[i], 887 urb->transfer_dma); 888 } 889 usb_free_urb(urb); 890 dev->video_mode.isoc_ctl.urb[i] = NULL; 891 } 892 dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL; 893 } 894 895 kfree(dev->video_mode.isoc_ctl.urb); 896 kfree(dev->video_mode.isoc_ctl.transfer_buffer); 897 kfree(dma_q->p_left_data); 898 899 dev->video_mode.isoc_ctl.urb = NULL; 900 dev->video_mode.isoc_ctl.transfer_buffer = NULL; 901 dev->video_mode.isoc_ctl.num_bufs = 0; 902 dma_q->p_left_data = NULL; 903 904 if (dev->mode_tv == 0) 905 cx231xx_capture_start(dev, 0, Raw_Video); 906 else 907 cx231xx_capture_start(dev, 0, TS1_serial_mode); 908 909 910 } 911 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc); 912 913 /* 914 * Stop and Deallocate URBs 915 */ 916 void cx231xx_uninit_bulk(struct cx231xx *dev) 917 { 918 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq; 919 struct urb *urb; 920 int i; 921 922 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_bulk\n"); 923 924 dev->video_mode.bulk_ctl.nfields = -1; 925 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) { 926 urb = dev->video_mode.bulk_ctl.urb[i]; 927 if (urb) { 928 if (!irqs_disabled()) 929 usb_kill_urb(urb); 930 else 931 usb_unlink_urb(urb); 932 933 if (dev->video_mode.bulk_ctl.transfer_buffer[i]) { 934 usb_free_coherent(dev->udev, 935 urb->transfer_buffer_length, 936 dev->video_mode.bulk_ctl. 937 transfer_buffer[i], 938 urb->transfer_dma); 939 } 940 usb_free_urb(urb); 941 dev->video_mode.bulk_ctl.urb[i] = NULL; 942 } 943 dev->video_mode.bulk_ctl.transfer_buffer[i] = NULL; 944 } 945 946 kfree(dev->video_mode.bulk_ctl.urb); 947 kfree(dev->video_mode.bulk_ctl.transfer_buffer); 948 kfree(dma_q->p_left_data); 949 950 dev->video_mode.bulk_ctl.urb = NULL; 951 dev->video_mode.bulk_ctl.transfer_buffer = NULL; 952 dev->video_mode.bulk_ctl.num_bufs = 0; 953 dma_q->p_left_data = NULL; 954 955 if (dev->mode_tv == 0) 956 cx231xx_capture_start(dev, 0, Raw_Video); 957 else 958 cx231xx_capture_start(dev, 0, TS1_serial_mode); 959 960 961 } 962 EXPORT_SYMBOL_GPL(cx231xx_uninit_bulk); 963 964 /* 965 * Allocate URBs and start IRQ 966 */ 967 int cx231xx_init_isoc(struct cx231xx *dev, int max_packets, 968 int num_bufs, int max_pkt_size, 969 int (*isoc_copy) (struct cx231xx *dev, struct urb *urb)) 970 { 971 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq; 972 int i; 973 int sb_size, pipe; 974 struct urb *urb; 975 int j, k; 976 int rc; 977 978 /* De-allocates all pending stuff */ 979 cx231xx_uninit_isoc(dev); 980 981 dma_q->p_left_data = kzalloc(4096, GFP_KERNEL); 982 if (dma_q->p_left_data == NULL) 983 return -ENOMEM; 984 985 dev->video_mode.isoc_ctl.isoc_copy = isoc_copy; 986 dev->video_mode.isoc_ctl.num_bufs = num_bufs; 987 dma_q->pos = 0; 988 dma_q->is_partial_line = 0; 989 dma_q->last_sav = 0; 990 dma_q->current_field = -1; 991 dma_q->field1_done = 0; 992 dma_q->lines_per_field = dev->height / 2; 993 dma_q->bytes_left_in_line = dev->width << 1; 994 dma_q->lines_completed = 0; 995 dma_q->mpeg_buffer_done = 0; 996 dma_q->left_data_count = 0; 997 dma_q->mpeg_buffer_completed = 0; 998 dma_q->add_ps_package_head = CX231XX_NEED_ADD_PS_PACKAGE_HEAD; 999 dma_q->ps_head[0] = 0x00; 1000 dma_q->ps_head[1] = 0x00; 1001 dma_q->ps_head[2] = 0x01; 1002 dma_q->ps_head[3] = 0xBA; 1003 for (i = 0; i < 8; i++) 1004 dma_q->partial_buf[i] = 0; 1005 1006 dev->video_mode.isoc_ctl.urb = 1007 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); 1008 if (!dev->video_mode.isoc_ctl.urb) { 1009 dev_err(dev->dev, 1010 "cannot alloc memory for usb buffers\n"); 1011 return -ENOMEM; 1012 } 1013 1014 dev->video_mode.isoc_ctl.transfer_buffer = 1015 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); 1016 if (!dev->video_mode.isoc_ctl.transfer_buffer) { 1017 dev_err(dev->dev, 1018 "cannot allocate memory for usbtransfer\n"); 1019 kfree(dev->video_mode.isoc_ctl.urb); 1020 return -ENOMEM; 1021 } 1022 1023 dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size; 1024 dev->video_mode.isoc_ctl.buf = NULL; 1025 1026 sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size; 1027 1028 if (dev->mode_tv == 1) 1029 dev->video_mode.end_point_addr = 0x81; 1030 else 1031 dev->video_mode.end_point_addr = 0x84; 1032 1033 1034 /* allocate urbs and transfer buffers */ 1035 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) { 1036 urb = usb_alloc_urb(max_packets, GFP_KERNEL); 1037 if (!urb) { 1038 dev_err(dev->dev, 1039 "cannot alloc isoc_ctl.urb %i\n", i); 1040 cx231xx_uninit_isoc(dev); 1041 return -ENOMEM; 1042 } 1043 dev->video_mode.isoc_ctl.urb[i] = urb; 1044 1045 dev->video_mode.isoc_ctl.transfer_buffer[i] = 1046 usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL, 1047 &urb->transfer_dma); 1048 if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) { 1049 dev_err(dev->dev, 1050 "unable to allocate %i bytes for transfer buffer %i%s\n", 1051 sb_size, i, 1052 in_interrupt() ? " while in int" : ""); 1053 cx231xx_uninit_isoc(dev); 1054 return -ENOMEM; 1055 } 1056 memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size); 1057 1058 pipe = 1059 usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr); 1060 1061 usb_fill_int_urb(urb, dev->udev, pipe, 1062 dev->video_mode.isoc_ctl.transfer_buffer[i], 1063 sb_size, cx231xx_isoc_irq_callback, dma_q, 1); 1064 1065 urb->number_of_packets = max_packets; 1066 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; 1067 1068 k = 0; 1069 for (j = 0; j < max_packets; j++) { 1070 urb->iso_frame_desc[j].offset = k; 1071 urb->iso_frame_desc[j].length = 1072 dev->video_mode.isoc_ctl.max_pkt_size; 1073 k += dev->video_mode.isoc_ctl.max_pkt_size; 1074 } 1075 } 1076 1077 init_waitqueue_head(&dma_q->wq); 1078 1079 /* submit urbs and enables IRQ */ 1080 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) { 1081 rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i], 1082 GFP_ATOMIC); 1083 if (rc) { 1084 dev_err(dev->dev, 1085 "submit of urb %i failed (error=%i)\n", i, 1086 rc); 1087 cx231xx_uninit_isoc(dev); 1088 return rc; 1089 } 1090 } 1091 1092 if (dev->mode_tv == 0) 1093 cx231xx_capture_start(dev, 1, Raw_Video); 1094 else 1095 cx231xx_capture_start(dev, 1, TS1_serial_mode); 1096 1097 return 0; 1098 } 1099 EXPORT_SYMBOL_GPL(cx231xx_init_isoc); 1100 1101 /* 1102 * Allocate URBs and start IRQ 1103 */ 1104 int cx231xx_init_bulk(struct cx231xx *dev, int max_packets, 1105 int num_bufs, int max_pkt_size, 1106 int (*bulk_copy) (struct cx231xx *dev, struct urb *urb)) 1107 { 1108 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq; 1109 int i; 1110 int sb_size, pipe; 1111 struct urb *urb; 1112 int rc; 1113 1114 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input; 1115 1116 cx231xx_coredbg("Setting Video mux to %d\n", dev->video_input); 1117 1118 video_mux(dev, dev->video_input); 1119 1120 /* De-allocates all pending stuff */ 1121 cx231xx_uninit_bulk(dev); 1122 1123 dev->video_mode.bulk_ctl.bulk_copy = bulk_copy; 1124 dev->video_mode.bulk_ctl.num_bufs = num_bufs; 1125 dma_q->pos = 0; 1126 dma_q->is_partial_line = 0; 1127 dma_q->last_sav = 0; 1128 dma_q->current_field = -1; 1129 dma_q->field1_done = 0; 1130 dma_q->lines_per_field = dev->height / 2; 1131 dma_q->bytes_left_in_line = dev->width << 1; 1132 dma_q->lines_completed = 0; 1133 dma_q->mpeg_buffer_done = 0; 1134 dma_q->left_data_count = 0; 1135 dma_q->mpeg_buffer_completed = 0; 1136 dma_q->ps_head[0] = 0x00; 1137 dma_q->ps_head[1] = 0x00; 1138 dma_q->ps_head[2] = 0x01; 1139 dma_q->ps_head[3] = 0xBA; 1140 for (i = 0; i < 8; i++) 1141 dma_q->partial_buf[i] = 0; 1142 1143 dev->video_mode.bulk_ctl.urb = 1144 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); 1145 if (!dev->video_mode.bulk_ctl.urb) { 1146 dev_err(dev->dev, 1147 "cannot alloc memory for usb buffers\n"); 1148 return -ENOMEM; 1149 } 1150 1151 dev->video_mode.bulk_ctl.transfer_buffer = 1152 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); 1153 if (!dev->video_mode.bulk_ctl.transfer_buffer) { 1154 dev_err(dev->dev, 1155 "cannot allocate memory for usbtransfer\n"); 1156 kfree(dev->video_mode.bulk_ctl.urb); 1157 return -ENOMEM; 1158 } 1159 1160 dev->video_mode.bulk_ctl.max_pkt_size = max_pkt_size; 1161 dev->video_mode.bulk_ctl.buf = NULL; 1162 1163 sb_size = max_packets * dev->video_mode.bulk_ctl.max_pkt_size; 1164 1165 if (dev->mode_tv == 1) 1166 dev->video_mode.end_point_addr = 0x81; 1167 else 1168 dev->video_mode.end_point_addr = 0x84; 1169 1170 1171 /* allocate urbs and transfer buffers */ 1172 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) { 1173 urb = usb_alloc_urb(0, GFP_KERNEL); 1174 if (!urb) { 1175 dev_err(dev->dev, 1176 "cannot alloc bulk_ctl.urb %i\n", i); 1177 cx231xx_uninit_bulk(dev); 1178 return -ENOMEM; 1179 } 1180 dev->video_mode.bulk_ctl.urb[i] = urb; 1181 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 1182 1183 dev->video_mode.bulk_ctl.transfer_buffer[i] = 1184 usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL, 1185 &urb->transfer_dma); 1186 if (!dev->video_mode.bulk_ctl.transfer_buffer[i]) { 1187 dev_err(dev->dev, 1188 "unable to allocate %i bytes for transfer buffer %i%s\n", 1189 sb_size, i, 1190 in_interrupt() ? " while in int" : ""); 1191 cx231xx_uninit_bulk(dev); 1192 return -ENOMEM; 1193 } 1194 memset(dev->video_mode.bulk_ctl.transfer_buffer[i], 0, sb_size); 1195 1196 pipe = usb_rcvbulkpipe(dev->udev, 1197 dev->video_mode.end_point_addr); 1198 usb_fill_bulk_urb(urb, dev->udev, pipe, 1199 dev->video_mode.bulk_ctl.transfer_buffer[i], 1200 sb_size, cx231xx_bulk_irq_callback, dma_q); 1201 } 1202 1203 /* clear halt */ 1204 rc = usb_clear_halt(dev->udev, dev->video_mode.bulk_ctl.urb[0]->pipe); 1205 if (rc < 0) { 1206 dev_err(dev->dev, 1207 "failed to clear USB bulk endpoint stall/halt condition (error=%i)\n", 1208 rc); 1209 cx231xx_uninit_bulk(dev); 1210 return rc; 1211 } 1212 1213 init_waitqueue_head(&dma_q->wq); 1214 1215 /* submit urbs and enables IRQ */ 1216 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) { 1217 rc = usb_submit_urb(dev->video_mode.bulk_ctl.urb[i], 1218 GFP_ATOMIC); 1219 if (rc) { 1220 dev_err(dev->dev, 1221 "submit of urb %i failed (error=%i)\n", i, rc); 1222 cx231xx_uninit_bulk(dev); 1223 return rc; 1224 } 1225 } 1226 1227 if (dev->mode_tv == 0) 1228 cx231xx_capture_start(dev, 1, Raw_Video); 1229 else 1230 cx231xx_capture_start(dev, 1, TS1_serial_mode); 1231 1232 return 0; 1233 } 1234 EXPORT_SYMBOL_GPL(cx231xx_init_bulk); 1235 void cx231xx_stop_TS1(struct cx231xx *dev) 1236 { 1237 u8 val[4] = { 0, 0, 0, 0 }; 1238 1239 val[0] = 0x00; 1240 val[1] = 0x03; 1241 val[2] = 0x00; 1242 val[3] = 0x00; 1243 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 1244 TS_MODE_REG, val, 4); 1245 1246 val[0] = 0x00; 1247 val[1] = 0x70; 1248 val[2] = 0x04; 1249 val[3] = 0x00; 1250 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 1251 TS1_CFG_REG, val, 4); 1252 } 1253 /* EXPORT_SYMBOL_GPL(cx231xx_stop_TS1); */ 1254 void cx231xx_start_TS1(struct cx231xx *dev) 1255 { 1256 u8 val[4] = { 0, 0, 0, 0 }; 1257 1258 val[0] = 0x03; 1259 val[1] = 0x03; 1260 val[2] = 0x00; 1261 val[3] = 0x00; 1262 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 1263 TS_MODE_REG, val, 4); 1264 1265 val[0] = 0x04; 1266 val[1] = 0xA3; 1267 val[2] = 0x3B; 1268 val[3] = 0x00; 1269 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, 1270 TS1_CFG_REG, val, 4); 1271 } 1272 /* EXPORT_SYMBOL_GPL(cx231xx_start_TS1); */ 1273 /***************************************************************** 1274 * Device Init/UnInit functions * 1275 ******************************************************************/ 1276 int cx231xx_dev_init(struct cx231xx *dev) 1277 { 1278 int errCode = 0; 1279 1280 /* Initialize I2C bus */ 1281 1282 /* External Master 1 Bus */ 1283 dev->i2c_bus[0].nr = 0; 1284 dev->i2c_bus[0].dev = dev; 1285 dev->i2c_bus[0].i2c_period = I2C_SPEED_100K; /* 100 KHz */ 1286 dev->i2c_bus[0].i2c_nostop = 0; 1287 dev->i2c_bus[0].i2c_reserve = 0; 1288 1289 /* External Master 2 Bus */ 1290 dev->i2c_bus[1].nr = 1; 1291 dev->i2c_bus[1].dev = dev; 1292 dev->i2c_bus[1].i2c_period = I2C_SPEED_100K; /* 100 KHz */ 1293 dev->i2c_bus[1].i2c_nostop = 0; 1294 dev->i2c_bus[1].i2c_reserve = 0; 1295 1296 /* Internal Master 3 Bus */ 1297 dev->i2c_bus[2].nr = 2; 1298 dev->i2c_bus[2].dev = dev; 1299 dev->i2c_bus[2].i2c_period = I2C_SPEED_100K; /* 100kHz */ 1300 dev->i2c_bus[2].i2c_nostop = 0; 1301 dev->i2c_bus[2].i2c_reserve = 0; 1302 1303 /* register I2C buses */ 1304 cx231xx_i2c_register(&dev->i2c_bus[0]); 1305 cx231xx_i2c_register(&dev->i2c_bus[1]); 1306 cx231xx_i2c_register(&dev->i2c_bus[2]); 1307 1308 errCode = cx231xx_i2c_mux_create(dev); 1309 if (errCode < 0) 1310 return errCode; 1311 cx231xx_i2c_mux_register(dev, 0); 1312 cx231xx_i2c_mux_register(dev, 1); 1313 1314 /* scan the real bus segments in the order of physical port numbers */ 1315 cx231xx_do_i2c_scan(dev, I2C_0); 1316 cx231xx_do_i2c_scan(dev, I2C_1_MUX_1); 1317 cx231xx_do_i2c_scan(dev, I2C_2); 1318 cx231xx_do_i2c_scan(dev, I2C_1_MUX_3); 1319 1320 /* init hardware */ 1321 /* Note : with out calling set power mode function, 1322 afe can not be set up correctly */ 1323 if (dev->board.external_av) { 1324 errCode = cx231xx_set_power_mode(dev, 1325 POLARIS_AVMODE_ENXTERNAL_AV); 1326 if (errCode < 0) { 1327 dev_err(dev->dev, 1328 "%s: Failed to set Power - errCode [%d]!\n", 1329 __func__, errCode); 1330 return errCode; 1331 } 1332 } else { 1333 errCode = cx231xx_set_power_mode(dev, 1334 POLARIS_AVMODE_ANALOGT_TV); 1335 if (errCode < 0) { 1336 dev_err(dev->dev, 1337 "%s: Failed to set Power - errCode [%d]!\n", 1338 __func__, errCode); 1339 return errCode; 1340 } 1341 } 1342 1343 /* reset the Tuner, if it is a Xceive tuner */ 1344 if ((dev->board.tuner_type == TUNER_XC5000) || 1345 (dev->board.tuner_type == TUNER_XC2028)) 1346 cx231xx_gpio_set(dev, dev->board.tuner_gpio); 1347 1348 /* initialize Colibri block */ 1349 errCode = cx231xx_afe_init_super_block(dev, 0x23c); 1350 if (errCode < 0) { 1351 dev_err(dev->dev, 1352 "%s: cx231xx_afe init super block - errCode [%d]!\n", 1353 __func__, errCode); 1354 return errCode; 1355 } 1356 errCode = cx231xx_afe_init_channels(dev); 1357 if (errCode < 0) { 1358 dev_err(dev->dev, 1359 "%s: cx231xx_afe init channels - errCode [%d]!\n", 1360 __func__, errCode); 1361 return errCode; 1362 } 1363 1364 /* Set DIF in By pass mode */ 1365 errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND); 1366 if (errCode < 0) { 1367 dev_err(dev->dev, 1368 "%s: cx231xx_dif set to By pass mode - errCode [%d]!\n", 1369 __func__, errCode); 1370 return errCode; 1371 } 1372 1373 /* I2S block related functions */ 1374 errCode = cx231xx_i2s_blk_initialize(dev); 1375 if (errCode < 0) { 1376 dev_err(dev->dev, 1377 "%s: cx231xx_i2s block initialize - errCode [%d]!\n", 1378 __func__, errCode); 1379 return errCode; 1380 } 1381 1382 /* init control pins */ 1383 errCode = cx231xx_init_ctrl_pin_status(dev); 1384 if (errCode < 0) { 1385 dev_err(dev->dev, 1386 "%s: cx231xx_init ctrl pins - errCode [%d]!\n", 1387 __func__, errCode); 1388 return errCode; 1389 } 1390 1391 /* set AGC mode to Analog */ 1392 switch (dev->model) { 1393 case CX231XX_BOARD_CNXT_CARRAERA: 1394 case CX231XX_BOARD_CNXT_RDE_250: 1395 case CX231XX_BOARD_CNXT_SHELBY: 1396 case CX231XX_BOARD_CNXT_RDU_250: 1397 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1); 1398 break; 1399 case CX231XX_BOARD_CNXT_RDE_253S: 1400 case CX231XX_BOARD_CNXT_RDU_253S: 1401 case CX231XX_BOARD_HAUPPAUGE_EXETER: 1402 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx: 1403 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID: 1404 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL: 1405 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC: 1406 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0); 1407 break; 1408 default: 1409 break; 1410 } 1411 if (errCode < 0) { 1412 dev_err(dev->dev, 1413 "%s: cx231xx_AGC mode to Analog - errCode [%d]!\n", 1414 __func__, errCode); 1415 return errCode; 1416 } 1417 1418 /* set all alternate settings to zero initially */ 1419 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0); 1420 cx231xx_set_alt_setting(dev, INDEX_VANC, 0); 1421 cx231xx_set_alt_setting(dev, INDEX_HANC, 0); 1422 if (dev->board.has_dvb) 1423 cx231xx_set_alt_setting(dev, INDEX_TS1, 0); 1424 1425 errCode = 0; 1426 return errCode; 1427 } 1428 EXPORT_SYMBOL_GPL(cx231xx_dev_init); 1429 1430 void cx231xx_dev_uninit(struct cx231xx *dev) 1431 { 1432 /* Un Initialize I2C bus */ 1433 cx231xx_i2c_mux_unregister(dev); 1434 cx231xx_i2c_unregister(&dev->i2c_bus[2]); 1435 cx231xx_i2c_unregister(&dev->i2c_bus[1]); 1436 cx231xx_i2c_unregister(&dev->i2c_bus[0]); 1437 } 1438 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit); 1439 1440 /***************************************************************** 1441 * G P I O related functions * 1442 ******************************************************************/ 1443 int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val, 1444 u8 len, u8 request, u8 direction) 1445 { 1446 int status = 0; 1447 struct VENDOR_REQUEST_IN ven_req; 1448 1449 /* Set wValue */ 1450 ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff); 1451 1452 /* set request */ 1453 if (!request) { 1454 if (direction) 1455 ven_req.bRequest = VRT_GET_GPIO; /* 0x8 gpio */ 1456 else 1457 ven_req.bRequest = VRT_SET_GPIO; /* 0x9 gpio */ 1458 } else { 1459 if (direction) 1460 ven_req.bRequest = VRT_GET_GPIE; /* 0xa gpie */ 1461 else 1462 ven_req.bRequest = VRT_SET_GPIE; /* 0xb gpie */ 1463 } 1464 1465 /* set index value */ 1466 ven_req.wIndex = (u16) (gpio_bit & 0xffff); 1467 1468 /* set wLength value */ 1469 ven_req.wLength = len; 1470 1471 /* set bData value */ 1472 ven_req.bData = 0; 1473 1474 /* set the buffer for read / write */ 1475 ven_req.pBuff = gpio_val; 1476 1477 /* set the direction */ 1478 if (direction) { 1479 ven_req.direction = USB_DIR_IN; 1480 memset(ven_req.pBuff, 0x00, ven_req.wLength); 1481 } else 1482 ven_req.direction = USB_DIR_OUT; 1483 1484 1485 /* call common vendor command request */ 1486 status = cx231xx_send_vendor_cmd(dev, &ven_req); 1487 if (status < 0) { 1488 dev_err(dev->dev, "%s: failed with status -%d\n", 1489 __func__, status); 1490 } 1491 1492 return status; 1493 } 1494 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd); 1495 1496 /***************************************************************** 1497 * C O N T R O L - Register R E A D / W R I T E functions * 1498 *****************************************************************/ 1499 int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode) 1500 { 1501 u8 value[4] = { 0x0, 0x0, 0x0, 0x0 }; 1502 u32 tmp = 0; 1503 int status = 0; 1504 1505 status = 1506 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4); 1507 if (status < 0) 1508 return status; 1509 1510 tmp = le32_to_cpu(*((__le32 *) value)); 1511 tmp |= mode; 1512 1513 value[0] = (u8) tmp; 1514 value[1] = (u8) (tmp >> 8); 1515 value[2] = (u8) (tmp >> 16); 1516 value[3] = (u8) (tmp >> 24); 1517 1518 status = 1519 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4); 1520 1521 return status; 1522 } 1523 1524 /***************************************************************** 1525 * I 2 C Internal C O N T R O L functions * 1526 *****************************************************************/ 1527 int cx231xx_read_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr, 1528 u8 saddr_len, u32 *data, u8 data_len, int master) 1529 { 1530 int status = 0; 1531 struct cx231xx_i2c_xfer_data req_data; 1532 u8 value[64] = "0"; 1533 1534 if (saddr_len == 0) 1535 saddr = 0; 1536 else if (saddr_len == 1) 1537 saddr &= 0xff; 1538 1539 /* prepare xfer_data struct */ 1540 req_data.dev_addr = dev_addr >> 1; 1541 req_data.direction = I2C_M_RD; 1542 req_data.saddr_len = saddr_len; 1543 req_data.saddr_dat = saddr; 1544 req_data.buf_size = data_len; 1545 req_data.p_buffer = (u8 *) value; 1546 1547 /* usb send command */ 1548 if (master == 0) 1549 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], 1550 &req_data); 1551 else if (master == 1) 1552 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1], 1553 &req_data); 1554 else if (master == 2) 1555 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2], 1556 &req_data); 1557 1558 if (status >= 0) { 1559 /* Copy the data read back to main buffer */ 1560 if (data_len == 1) 1561 *data = value[0]; 1562 else if (data_len == 4) 1563 *data = 1564 value[0] | value[1] << 8 | value[2] << 16 | value[3] 1565 << 24; 1566 else if (data_len > 4) 1567 *data = value[saddr]; 1568 } 1569 1570 return status; 1571 } 1572 1573 int cx231xx_write_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr, 1574 u8 saddr_len, u32 data, u8 data_len, int master) 1575 { 1576 int status = 0; 1577 u8 value[4] = { 0, 0, 0, 0 }; 1578 struct cx231xx_i2c_xfer_data req_data; 1579 1580 value[0] = (u8) data; 1581 value[1] = (u8) (data >> 8); 1582 value[2] = (u8) (data >> 16); 1583 value[3] = (u8) (data >> 24); 1584 1585 if (saddr_len == 0) 1586 saddr = 0; 1587 else if (saddr_len == 1) 1588 saddr &= 0xff; 1589 1590 /* prepare xfer_data struct */ 1591 req_data.dev_addr = dev_addr >> 1; 1592 req_data.direction = 0; 1593 req_data.saddr_len = saddr_len; 1594 req_data.saddr_dat = saddr; 1595 req_data.buf_size = data_len; 1596 req_data.p_buffer = value; 1597 1598 /* usb send command */ 1599 if (master == 0) 1600 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], 1601 &req_data); 1602 else if (master == 1) 1603 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1], 1604 &req_data); 1605 else if (master == 2) 1606 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2], 1607 &req_data); 1608 1609 return status; 1610 } 1611 1612 int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr, 1613 u8 saddr_len, u32 *data, u8 data_len) 1614 { 1615 int status = 0; 1616 struct cx231xx_i2c_xfer_data req_data; 1617 u8 value[4] = { 0, 0, 0, 0 }; 1618 1619 if (saddr_len == 0) 1620 saddr = 0; 1621 else if (saddr_len == 1) 1622 saddr &= 0xff; 1623 1624 /* prepare xfer_data struct */ 1625 req_data.dev_addr = dev_addr >> 1; 1626 req_data.direction = I2C_M_RD; 1627 req_data.saddr_len = saddr_len; 1628 req_data.saddr_dat = saddr; 1629 req_data.buf_size = data_len; 1630 req_data.p_buffer = (u8 *) value; 1631 1632 /* usb send command */ 1633 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data); 1634 1635 if (status >= 0) { 1636 /* Copy the data read back to main buffer */ 1637 if (data_len == 1) 1638 *data = value[0]; 1639 else 1640 *data = 1641 value[0] | value[1] << 8 | value[2] << 16 | value[3] 1642 << 24; 1643 } 1644 1645 return status; 1646 } 1647 1648 int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr, 1649 u8 saddr_len, u32 data, u8 data_len) 1650 { 1651 int status = 0; 1652 u8 value[4] = { 0, 0, 0, 0 }; 1653 struct cx231xx_i2c_xfer_data req_data; 1654 1655 value[0] = (u8) data; 1656 value[1] = (u8) (data >> 8); 1657 value[2] = (u8) (data >> 16); 1658 value[3] = (u8) (data >> 24); 1659 1660 if (saddr_len == 0) 1661 saddr = 0; 1662 else if (saddr_len == 1) 1663 saddr &= 0xff; 1664 1665 /* prepare xfer_data struct */ 1666 req_data.dev_addr = dev_addr >> 1; 1667 req_data.direction = 0; 1668 req_data.saddr_len = saddr_len; 1669 req_data.saddr_dat = saddr; 1670 req_data.buf_size = data_len; 1671 req_data.p_buffer = value; 1672 1673 /* usb send command */ 1674 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data); 1675 1676 return status; 1677 } 1678 1679 int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size, 1680 u16 register_address, u8 bit_start, u8 bit_end, 1681 u32 value) 1682 { 1683 int status = 0; 1684 u32 tmp; 1685 u32 mask = 0; 1686 int i; 1687 1688 if (bit_start > (size - 1) || bit_end > (size - 1)) 1689 return -1; 1690 1691 if (size == 8) { 1692 status = 1693 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2, 1694 &tmp, 1); 1695 } else { 1696 status = 1697 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2, 1698 &tmp, 4); 1699 } 1700 1701 if (status < 0) 1702 return status; 1703 1704 mask = 1 << bit_end; 1705 for (i = bit_end; i > bit_start && i > 0; i--) 1706 mask = mask + (1 << (i - 1)); 1707 1708 value <<= bit_start; 1709 1710 if (size == 8) { 1711 tmp &= ~mask; 1712 tmp |= value; 1713 tmp &= 0xff; 1714 status = 1715 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2, 1716 tmp, 1); 1717 } else { 1718 tmp &= ~mask; 1719 tmp |= value; 1720 status = 1721 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2, 1722 tmp, 4); 1723 } 1724 1725 return status; 1726 } 1727 1728 int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr, 1729 u16 saddr, u32 mask, u32 value) 1730 { 1731 u32 temp; 1732 int status = 0; 1733 1734 status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4); 1735 1736 if (status < 0) 1737 return status; 1738 1739 temp &= ~mask; 1740 temp |= value; 1741 1742 status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4); 1743 1744 return status; 1745 } 1746 1747 u32 cx231xx_set_field(u32 field_mask, u32 data) 1748 { 1749 u32 temp; 1750 1751 for (temp = field_mask; (temp & 1) == 0; temp >>= 1) 1752 data <<= 1; 1753 1754 return data; 1755 } 1756