1/* 2 * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III 3 * flexcop-usb.c - covers the USB part 4 * see flexcop.c for copyright information 5 */ 6#define FC_LOG_PREFIX "flexcop_usb" 7#include "flexcop-usb.h" 8#include "flexcop-common.h" 9 10/* Version information */ 11#define DRIVER_VERSION "0.1" 12#define DRIVER_NAME "Technisat/B2C2 FlexCop II/IIb/III Digital TV USB Driver" 13#define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@desy.de>" 14 15/* debug */ 16#ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG 17#define dprintk(level,args...) \ 18 do { if ((debug & level)) printk(args); } while (0) 19 20#define debug_dump(b, l, method) do {\ 21 int i; \ 22 for (i = 0; i < l; i++) \ 23 method("%02x ", b[i]); \ 24 method("\n"); \ 25} while (0) 26 27#define DEBSTATUS "" 28#else 29#define dprintk(level, args...) 30#define debug_dump(b, l, method) 31#define DEBSTATUS " (debugging is not enabled)" 32#endif 33 34static int debug; 35module_param(debug, int, 0644); 36MODULE_PARM_DESC(debug, "set debugging level (1=info,ts=2," 37 "ctrl=4,i2c=8,v8mem=16 (or-able))." DEBSTATUS); 38#undef DEBSTATUS 39 40#define deb_info(args...) dprintk(0x01, args) 41#define deb_ts(args...) dprintk(0x02, args) 42#define deb_ctrl(args...) dprintk(0x04, args) 43#define deb_i2c(args...) dprintk(0x08, args) 44#define deb_v8(args...) dprintk(0x10, args) 45 46/* JLP 111700: we will include the 1 bit gap between the upper and lower 3 bits 47 * in the IBI address, to make the V8 code simpler. 48 * PCI ADDRESS FORMAT: 0x71C -> 0000 0111 0001 1100 (the six bits used) 49 * in general: 0000 0HHH 000L LL00 50 * IBI ADDRESS FORMAT: RHHH BLLL 51 * 52 * where R is the read(1)/write(0) bit, B is the busy bit 53 * and HHH and LLL are the two sets of three bits from the PCI address. 54 */ 55#define B2C2_FLEX_PCIOFFSET_TO_INTERNALADDR(usPCI) (u8) \ 56 (((usPCI >> 2) & 0x07) + ((usPCI >> 4) & 0x70)) 57#define B2C2_FLEX_INTERNALADDR_TO_PCIOFFSET(ucAddr) (u16) \ 58 (((ucAddr & 0x07) << 2) + ((ucAddr & 0x70) << 4)) 59 60/* 61 * DKT 020228 62 * - forget about this VENDOR_BUFFER_SIZE, read and write register 63 * deal with DWORD or 4 bytes, that should be should from now on 64 * - from now on, we don't support anything older than firm 1.00 65 * I eliminated the write register as a 2 trip of writing hi word and lo word 66 * and force this to write only 4 bytes at a time. 67 * NOTE: this should work with all the firmware from 1.00 and newer 68 */ 69static int flexcop_usb_readwrite_dw(struct flexcop_device *fc, u16 wRegOffsPCI, u32 *val, u8 read) 70{ 71 struct flexcop_usb *fc_usb = fc->bus_specific; 72 u8 request = read ? B2C2_USB_READ_REG : B2C2_USB_WRITE_REG; 73 u8 request_type = (read ? USB_DIR_IN : USB_DIR_OUT) | USB_TYPE_VENDOR; 74 u8 wAddress = B2C2_FLEX_PCIOFFSET_TO_INTERNALADDR(wRegOffsPCI) | 75 (read ? 0x80 : 0); 76 77 int len = usb_control_msg(fc_usb->udev, 78 read ? B2C2_USB_CTRL_PIPE_IN : B2C2_USB_CTRL_PIPE_OUT, 79 request, 80 request_type, /* 0xc0 read or 0x40 write */ 81 wAddress, 82 0, 83 val, 84 sizeof(u32), 85 B2C2_WAIT_FOR_OPERATION_RDW * HZ); 86 87 if (len != sizeof(u32)) { 88 err("error while %s dword from %d (%d).", read ? "reading" : 89 "writing", wAddress, wRegOffsPCI); 90 return -EIO; 91 } 92 return 0; 93} 94/* 95 * DKT 010817 - add support for V8 memory read/write and flash update 96 */ 97static int flexcop_usb_v8_memory_req(struct flexcop_usb *fc_usb, 98 flexcop_usb_request_t req, u8 page, u16 wAddress, 99 u8 *pbBuffer, u32 buflen) 100{ 101 u8 request_type = USB_TYPE_VENDOR; 102 u16 wIndex; 103 int nWaitTime, pipe, len; 104 wIndex = page << 8; 105 106 switch (req) { 107 case B2C2_USB_READ_V8_MEM: 108 nWaitTime = B2C2_WAIT_FOR_OPERATION_V8READ; 109 request_type |= USB_DIR_IN; 110 pipe = B2C2_USB_CTRL_PIPE_IN; 111 break; 112 case B2C2_USB_WRITE_V8_MEM: 113 wIndex |= pbBuffer[0]; 114 request_type |= USB_DIR_OUT; 115 nWaitTime = B2C2_WAIT_FOR_OPERATION_V8WRITE; 116 pipe = B2C2_USB_CTRL_PIPE_OUT; 117 break; 118 case B2C2_USB_FLASH_BLOCK: 119 request_type |= USB_DIR_OUT; 120 nWaitTime = B2C2_WAIT_FOR_OPERATION_V8FLASH; 121 pipe = B2C2_USB_CTRL_PIPE_OUT; 122 break; 123 default: 124 deb_info("unsupported request for v8_mem_req %x.\n", req); 125 return -EINVAL; 126 } 127 deb_v8("v8mem: %02x %02x %04x %04x, len: %d\n", request_type, req, 128 wAddress, wIndex, buflen); 129 130 len = usb_control_msg(fc_usb->udev, pipe, 131 req, 132 request_type, 133 wAddress, 134 wIndex, 135 pbBuffer, 136 buflen, 137 nWaitTime * HZ); 138 139 debug_dump(pbBuffer, len, deb_v8); 140 return len == buflen ? 0 : -EIO; 141} 142 143#define bytes_left_to_read_on_page(paddr,buflen) \ 144 ((V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK)) > buflen \ 145 ? buflen : (V8_MEMORY_PAGE_SIZE - (paddr & V8_MEMORY_PAGE_MASK))) 146 147static int flexcop_usb_memory_req(struct flexcop_usb *fc_usb, 148 flexcop_usb_request_t req, flexcop_usb_mem_page_t page_start, 149 u32 addr, int extended, u8 *buf, u32 len) 150{ 151 int i,ret = 0; 152 u16 wMax; 153 u32 pagechunk = 0; 154 155 switch(req) { 156 case B2C2_USB_READ_V8_MEM: 157 wMax = USB_MEM_READ_MAX; 158 break; 159 case B2C2_USB_WRITE_V8_MEM: 160 wMax = USB_MEM_WRITE_MAX; 161 break; 162 case B2C2_USB_FLASH_BLOCK: 163 wMax = USB_FLASH_MAX; 164 break; 165 default: 166 return -EINVAL; 167 break; 168 } 169 for (i = 0; i < len;) { 170 pagechunk = 171 wMax < bytes_left_to_read_on_page(addr, len) ? 172 wMax : 173 bytes_left_to_read_on_page(addr, len); 174 deb_info("%x\n", 175 (addr & V8_MEMORY_PAGE_MASK) | 176 (V8_MEMORY_EXTENDED*extended)); 177 178 ret = flexcop_usb_v8_memory_req(fc_usb, req, 179 page_start + (addr / V8_MEMORY_PAGE_SIZE), 180 (addr & V8_MEMORY_PAGE_MASK) | 181 (V8_MEMORY_EXTENDED*extended), 182 &buf[i], pagechunk); 183 184 if (ret < 0) 185 return ret; 186 addr += pagechunk; 187 len -= pagechunk; 188 } 189 return 0; 190} 191 192static int flexcop_usb_get_mac_addr(struct flexcop_device *fc, int extended) 193{ 194 return flexcop_usb_memory_req(fc->bus_specific, B2C2_USB_READ_V8_MEM, 195 V8_MEMORY_PAGE_FLASH, 0x1f010, 1, 196 fc->dvb_adapter.proposed_mac, 6); 197} 198 199 200/* usb i2c stuff */ 201static int flexcop_usb_i2c_req(struct flexcop_i2c_adapter *i2c, 202 flexcop_usb_request_t req, flexcop_usb_i2c_function_t func, 203 u8 chipaddr, u8 addr, u8 *buf, u8 buflen) 204{ 205 struct flexcop_usb *fc_usb = i2c->fc->bus_specific; 206 u16 wValue, wIndex; 207 int nWaitTime,pipe,len; 208 u8 request_type = USB_TYPE_VENDOR; 209 210 switch (func) { 211 case USB_FUNC_I2C_WRITE: 212 case USB_FUNC_I2C_MULTIWRITE: 213 case USB_FUNC_I2C_REPEATWRITE: 214 /* DKT 020208 - add this to support special case of DiSEqC */ 215 case USB_FUNC_I2C_CHECKWRITE: 216 pipe = B2C2_USB_CTRL_PIPE_OUT; 217 nWaitTime = 2; 218 request_type |= USB_DIR_OUT; 219 break; 220 case USB_FUNC_I2C_READ: 221 case USB_FUNC_I2C_REPEATREAD: 222 pipe = B2C2_USB_CTRL_PIPE_IN; 223 nWaitTime = 2; 224 request_type |= USB_DIR_IN; 225 break; 226 default: 227 deb_info("unsupported function for i2c_req %x\n", func); 228 return -EINVAL; 229 } 230 wValue = (func << 8) | (i2c->port << 4); 231 wIndex = (chipaddr << 8 ) | addr; 232 233 deb_i2c("i2c %2d: %02x %02x %02x %02x %02x %02x\n", 234 func, request_type, req, 235 wValue & 0xff, wValue >> 8, 236 wIndex & 0xff, wIndex >> 8); 237 238 len = usb_control_msg(fc_usb->udev,pipe, 239 req, 240 request_type, 241 wValue, 242 wIndex, 243 buf, 244 buflen, 245 nWaitTime * HZ); 246 return len == buflen ? 0 : -EREMOTEIO; 247} 248 249/* actual bus specific access functions, 250 make sure prototype are/will be equal to pci */ 251static flexcop_ibi_value flexcop_usb_read_ibi_reg(struct flexcop_device *fc, 252 flexcop_ibi_register reg) 253{ 254 flexcop_ibi_value val; 255 val.raw = 0; 256 flexcop_usb_readwrite_dw(fc, reg, &val.raw, 1); 257 return val; 258} 259 260static int flexcop_usb_write_ibi_reg(struct flexcop_device *fc, 261 flexcop_ibi_register reg, flexcop_ibi_value val) 262{ 263 return flexcop_usb_readwrite_dw(fc, reg, &val.raw, 0); 264} 265 266static int flexcop_usb_i2c_request(struct flexcop_i2c_adapter *i2c, 267 flexcop_access_op_t op, u8 chipaddr, u8 addr, u8 *buf, u16 len) 268{ 269 if (op == FC_READ) 270 return flexcop_usb_i2c_req(i2c, B2C2_USB_I2C_REQUEST, 271 USB_FUNC_I2C_READ, chipaddr, addr, buf, len); 272 else 273 return flexcop_usb_i2c_req(i2c, B2C2_USB_I2C_REQUEST, 274 USB_FUNC_I2C_WRITE, chipaddr, addr, buf, len); 275} 276 277static void flexcop_usb_process_frame(struct flexcop_usb *fc_usb, 278 u8 *buffer, int buffer_length) 279{ 280 u8 *b; 281 int l; 282 283 deb_ts("tmp_buffer_length=%d, buffer_length=%d\n", 284 fc_usb->tmp_buffer_length, buffer_length); 285 286 if (fc_usb->tmp_buffer_length > 0) { 287 memcpy(fc_usb->tmp_buffer+fc_usb->tmp_buffer_length, buffer, 288 buffer_length); 289 fc_usb->tmp_buffer_length += buffer_length; 290 b = fc_usb->tmp_buffer; 291 l = fc_usb->tmp_buffer_length; 292 } else { 293 b=buffer; 294 l=buffer_length; 295 } 296 297 while (l >= 190) { 298 if (*b == 0xff) { 299 switch (*(b+1) & 0x03) { 300 case 0x01: /* media packet */ 301 if (*(b+2) == 0x47) 302 flexcop_pass_dmx_packets( 303 fc_usb->fc_dev, b+2, 1); 304 else 305 deb_ts( 306 "not ts packet %02x %02x %02x %02x \n", 307 *(b+2), *(b+3), 308 *(b+4), *(b+5)); 309 b += 190; 310 l -= 190; 311 break; 312 default: 313 deb_ts("wrong packet type\n"); 314 l = 0; 315 break; 316 } 317 } else { 318 deb_ts("wrong header\n"); 319 l = 0; 320 } 321 } 322 323 if (l>0) 324 memcpy(fc_usb->tmp_buffer, b, l); 325 fc_usb->tmp_buffer_length = l; 326} 327 328static void flexcop_usb_urb_complete(struct urb *urb) 329{ 330 struct flexcop_usb *fc_usb = urb->context; 331 int i; 332 333 if (urb->actual_length > 0) 334 deb_ts("urb completed, bufsize: %d actlen; %d\n", 335 urb->transfer_buffer_length, urb->actual_length); 336 337 for (i = 0; i < urb->number_of_packets; i++) { 338 if (urb->iso_frame_desc[i].status < 0) { 339 err("iso frame descriptor %d has an error: %d\n", i, 340 urb->iso_frame_desc[i].status); 341 } else 342 if (urb->iso_frame_desc[i].actual_length > 0) { 343 deb_ts("passed %d bytes to the demux\n", 344 urb->iso_frame_desc[i].actual_length); 345 346 flexcop_usb_process_frame(fc_usb, 347 urb->transfer_buffer + 348 urb->iso_frame_desc[i].offset, 349 urb->iso_frame_desc[i].actual_length); 350 } 351 urb->iso_frame_desc[i].status = 0; 352 urb->iso_frame_desc[i].actual_length = 0; 353 } 354 usb_submit_urb(urb,GFP_ATOMIC); 355} 356 357static int flexcop_usb_stream_control(struct flexcop_device *fc, int onoff) 358{ 359 /* submit/kill iso packets */ 360 return 0; 361} 362 363static void flexcop_usb_transfer_exit(struct flexcop_usb *fc_usb) 364{ 365 int i; 366 for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++) 367 if (fc_usb->iso_urb[i] != NULL) { 368 deb_ts("unlinking/killing urb no. %d\n",i); 369 usb_kill_urb(fc_usb->iso_urb[i]); 370 usb_free_urb(fc_usb->iso_urb[i]); 371 } 372 373 if (fc_usb->iso_buffer != NULL) 374 pci_free_consistent(NULL, 375 fc_usb->buffer_size, fc_usb->iso_buffer, 376 fc_usb->dma_addr); 377} 378 379static int flexcop_usb_transfer_init(struct flexcop_usb *fc_usb) 380{ 381 u16 frame_size = le16_to_cpu( 382 fc_usb->uintf->cur_altsetting->endpoint[0].desc.wMaxPacketSize); 383 int bufsize = B2C2_USB_NUM_ISO_URB * B2C2_USB_FRAMES_PER_ISO * 384 frame_size, i, j, ret; 385 int buffer_offset = 0; 386 387 deb_ts("creating %d iso-urbs with %d frames " 388 "each of %d bytes size = %d.\n", B2C2_USB_NUM_ISO_URB, 389 B2C2_USB_FRAMES_PER_ISO, frame_size, bufsize); 390 391 fc_usb->iso_buffer = pci_alloc_consistent(NULL, 392 bufsize, &fc_usb->dma_addr); 393 if (fc_usb->iso_buffer == NULL) 394 return -ENOMEM; 395 396 memset(fc_usb->iso_buffer, 0, bufsize); 397 fc_usb->buffer_size = bufsize; 398 399 /* creating iso urbs */ 400 for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++) { 401 fc_usb->iso_urb[i] = usb_alloc_urb(B2C2_USB_FRAMES_PER_ISO, 402 GFP_ATOMIC); 403 if (fc_usb->iso_urb[i] == NULL) { 404 ret = -ENOMEM; 405 goto urb_error; 406 } 407 } 408 409 /* initialising and submitting iso urbs */ 410 for (i = 0; i < B2C2_USB_NUM_ISO_URB; i++) { 411 int frame_offset = 0; 412 struct urb *urb = fc_usb->iso_urb[i]; 413 deb_ts("initializing and submitting urb no. %d " 414 "(buf_offset: %d).\n", i, buffer_offset); 415 416 urb->dev = fc_usb->udev; 417 urb->context = fc_usb; 418 urb->complete = flexcop_usb_urb_complete; 419 urb->pipe = B2C2_USB_DATA_PIPE; 420 urb->transfer_flags = URB_ISO_ASAP; 421 urb->interval = 1; 422 urb->number_of_packets = B2C2_USB_FRAMES_PER_ISO; 423 urb->transfer_buffer_length = frame_size * B2C2_USB_FRAMES_PER_ISO; 424 urb->transfer_buffer = fc_usb->iso_buffer + buffer_offset; 425 426 buffer_offset += frame_size * B2C2_USB_FRAMES_PER_ISO; 427 for (j = 0; j < B2C2_USB_FRAMES_PER_ISO; j++) { 428 deb_ts("urb no: %d, frame: %d, frame_offset: %d\n", 429 i, j, frame_offset); 430 urb->iso_frame_desc[j].offset = frame_offset; 431 urb->iso_frame_desc[j].length = frame_size; 432 frame_offset += frame_size; 433 } 434 435 if ((ret = usb_submit_urb(fc_usb->iso_urb[i],GFP_ATOMIC))) { 436 err("submitting urb %d failed with %d.", i, ret); 437 goto urb_error; 438 } 439 deb_ts("submitted urb no. %d.\n",i); 440 } 441 442 /* SRAM */ 443 flexcop_sram_set_dest(fc_usb->fc_dev, FC_SRAM_DEST_MEDIA | 444 FC_SRAM_DEST_NET | FC_SRAM_DEST_CAO | FC_SRAM_DEST_CAI, 445 FC_SRAM_DEST_TARGET_WAN_USB); 446 flexcop_wan_set_speed(fc_usb->fc_dev, FC_WAN_SPEED_8MBITS); 447 flexcop_sram_ctrl(fc_usb->fc_dev, 1, 1, 1); 448 return 0; 449 450urb_error: 451 flexcop_usb_transfer_exit(fc_usb); 452 return ret; 453} 454 455static int flexcop_usb_init(struct flexcop_usb *fc_usb) 456{ 457 /* use the alternate setting with the larges buffer */ 458 usb_set_interface(fc_usb->udev,0,1); 459 switch (fc_usb->udev->speed) { 460 case USB_SPEED_LOW: 461 err("cannot handle USB speed because it is too slow."); 462 return -ENODEV; 463 break; 464 case USB_SPEED_FULL: 465 info("running at FULL speed."); 466 break; 467 case USB_SPEED_HIGH: 468 info("running at HIGH speed."); 469 break; 470 case USB_SPEED_UNKNOWN: /* fall through */ 471 default: 472 err("cannot handle USB speed because it is unknown."); 473 return -ENODEV; 474 } 475 usb_set_intfdata(fc_usb->uintf, fc_usb); 476 return 0; 477} 478 479static void flexcop_usb_exit(struct flexcop_usb *fc_usb) 480{ 481 usb_set_intfdata(fc_usb->uintf, NULL); 482} 483 484static int flexcop_usb_probe(struct usb_interface *intf, 485 const struct usb_device_id *id) 486{ 487 struct usb_device *udev = interface_to_usbdev(intf); 488 struct flexcop_usb *fc_usb = NULL; 489 struct flexcop_device *fc = NULL; 490 int ret; 491 492 if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_usb))) == NULL) { 493 err("out of memory\n"); 494 return -ENOMEM; 495 } 496 497 /* general flexcop init */ 498 fc_usb = fc->bus_specific; 499 fc_usb->fc_dev = fc; 500 501 fc->read_ibi_reg = flexcop_usb_read_ibi_reg; 502 fc->write_ibi_reg = flexcop_usb_write_ibi_reg; 503 fc->i2c_request = flexcop_usb_i2c_request; 504 fc->get_mac_addr = flexcop_usb_get_mac_addr; 505 506 fc->stream_control = flexcop_usb_stream_control; 507 508 fc->pid_filtering = 1; 509 fc->bus_type = FC_USB; 510 511 fc->dev = &udev->dev; 512 fc->owner = THIS_MODULE; 513 514 /* bus specific part */ 515 fc_usb->udev = udev; 516 fc_usb->uintf = intf; 517 if ((ret = flexcop_usb_init(fc_usb)) != 0) 518 goto err_kfree; 519 520 /* init flexcop */ 521 if ((ret = flexcop_device_initialize(fc)) != 0) 522 goto err_usb_exit; 523 524 /* xfer init */ 525 if ((ret = flexcop_usb_transfer_init(fc_usb)) != 0) 526 goto err_fc_exit; 527 528 info("%s successfully initialized and connected.", DRIVER_NAME); 529 return 0; 530 531err_fc_exit: 532 flexcop_device_exit(fc); 533err_usb_exit: 534 flexcop_usb_exit(fc_usb); 535err_kfree: 536 flexcop_device_kfree(fc); 537 return ret; 538} 539 540static void flexcop_usb_disconnect(struct usb_interface *intf) 541{ 542 struct flexcop_usb *fc_usb = usb_get_intfdata(intf); 543 flexcop_usb_transfer_exit(fc_usb); 544 flexcop_device_exit(fc_usb->fc_dev); 545 flexcop_usb_exit(fc_usb); 546 flexcop_device_kfree(fc_usb->fc_dev); 547 info("%s successfully deinitialized and disconnected.", DRIVER_NAME); 548} 549 550static struct usb_device_id flexcop_usb_table [] = { 551 { USB_DEVICE(0x0af7, 0x0101) }, 552 { } 553}; 554MODULE_DEVICE_TABLE (usb, flexcop_usb_table); 555 556/* usb specific object needed to register this driver with the usb subsystem */ 557static struct usb_driver flexcop_usb_driver = { 558 .name = "b2c2_flexcop_usb", 559 .probe = flexcop_usb_probe, 560 .disconnect = flexcop_usb_disconnect, 561 .id_table = flexcop_usb_table, 562}; 563 564/* module stuff */ 565static int __init flexcop_usb_module_init(void) 566{ 567 int result; 568 if ((result = usb_register(&flexcop_usb_driver))) { 569 err("usb_register failed. (%d)", result); 570 return result; 571 } 572 return 0; 573} 574 575static void __exit flexcop_usb_module_exit(void) 576{ 577 /* deregister this driver from the USB subsystem */ 578 usb_deregister(&flexcop_usb_driver); 579} 580 581module_init(flexcop_usb_module_init); 582module_exit(flexcop_usb_module_exit); 583 584MODULE_AUTHOR(DRIVER_AUTHOR); 585MODULE_DESCRIPTION(DRIVER_NAME); 586MODULE_LICENSE("GPL"); 587