1 /* Linux driver for devices based on the DiBcom DiB0700 USB bridge 2 * 3 * This program is free software; you can redistribute it and/or modify it 4 * under the terms of the GNU General Public License as published by the Free 5 * Software Foundation, version 2. 6 * 7 * Copyright (C) 2005-6 DiBcom, SA 8 */ 9 #include "dib0700.h" 10 11 /* debug */ 12 int dvb_usb_dib0700_debug; 13 module_param_named(debug,dvb_usb_dib0700_debug, int, 0644); 14 MODULE_PARM_DESC(debug, "set debugging level (1=info,2=fw,4=fwdata,8=data (or-able))." DVB_USB_DEBUG_STATUS); 15 16 static int nb_packet_buffer_size = 21; 17 module_param(nb_packet_buffer_size, int, 0644); 18 MODULE_PARM_DESC(nb_packet_buffer_size, 19 "Set the dib0700 driver data buffer size. This parameter " 20 "corresponds to the number of TS packets. The actual size of " 21 "the data buffer corresponds to this parameter " 22 "multiplied by 188 (default: 21)"); 23 24 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 25 26 27 int dib0700_get_version(struct dvb_usb_device *d, u32 *hwversion, 28 u32 *romversion, u32 *ramversion, u32 *fwtype) 29 { 30 struct dib0700_state *st = d->priv; 31 int ret; 32 33 if (mutex_lock_interruptible(&d->usb_mutex) < 0) { 34 err("could not acquire lock"); 35 return -EINTR; 36 } 37 38 ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), 39 REQUEST_GET_VERSION, 40 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, 41 st->buf, 16, USB_CTRL_GET_TIMEOUT); 42 if (hwversion != NULL) 43 *hwversion = (st->buf[0] << 24) | (st->buf[1] << 16) | 44 (st->buf[2] << 8) | st->buf[3]; 45 if (romversion != NULL) 46 *romversion = (st->buf[4] << 24) | (st->buf[5] << 16) | 47 (st->buf[6] << 8) | st->buf[7]; 48 if (ramversion != NULL) 49 *ramversion = (st->buf[8] << 24) | (st->buf[9] << 16) | 50 (st->buf[10] << 8) | st->buf[11]; 51 if (fwtype != NULL) 52 *fwtype = (st->buf[12] << 24) | (st->buf[13] << 16) | 53 (st->buf[14] << 8) | st->buf[15]; 54 mutex_unlock(&d->usb_mutex); 55 return ret; 56 } 57 58 /* expecting rx buffer: request data[0] data[1] ... data[2] */ 59 static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen) 60 { 61 int status; 62 63 deb_data(">>> "); 64 debug_dump(tx, txlen, deb_data); 65 66 status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0), 67 tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen, 68 USB_CTRL_GET_TIMEOUT); 69 70 if (status != txlen) 71 deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen); 72 73 return status < 0 ? status : 0; 74 } 75 76 /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */ 77 int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen) 78 { 79 u16 index, value; 80 int status; 81 82 if (txlen < 2) { 83 err("tx buffer length is smaller than 2. Makes no sense."); 84 return -EINVAL; 85 } 86 if (txlen > 4) { 87 err("tx buffer length is larger than 4. Not supported."); 88 return -EINVAL; 89 } 90 91 deb_data(">>> "); 92 debug_dump(tx,txlen,deb_data); 93 94 value = ((txlen - 2) << 8) | tx[1]; 95 index = 0; 96 if (txlen > 2) 97 index |= (tx[2] << 8); 98 if (txlen > 3) 99 index |= tx[3]; 100 101 status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0], 102 USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen, 103 USB_CTRL_GET_TIMEOUT); 104 105 if (status < 0) 106 deb_info("ep 0 read error (status = %d)\n",status); 107 108 deb_data("<<< "); 109 debug_dump(rx, rxlen, deb_data); 110 111 return status; /* length in case of success */ 112 } 113 114 int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val) 115 { 116 struct dib0700_state *st = d->priv; 117 int ret; 118 119 if (mutex_lock_interruptible(&d->usb_mutex) < 0) { 120 err("could not acquire lock"); 121 return -EINTR; 122 } 123 124 st->buf[0] = REQUEST_SET_GPIO; 125 st->buf[1] = gpio; 126 st->buf[2] = ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6); 127 128 ret = dib0700_ctrl_wr(d, st->buf, 3); 129 130 mutex_unlock(&d->usb_mutex); 131 return ret; 132 } 133 134 static int dib0700_set_usb_xfer_len(struct dvb_usb_device *d, u16 nb_ts_packets) 135 { 136 struct dib0700_state *st = d->priv; 137 int ret; 138 139 if (st->fw_version >= 0x10201) { 140 if (mutex_lock_interruptible(&d->usb_mutex) < 0) { 141 err("could not acquire lock"); 142 return -EINTR; 143 } 144 145 st->buf[0] = REQUEST_SET_USB_XFER_LEN; 146 st->buf[1] = (nb_ts_packets >> 8) & 0xff; 147 st->buf[2] = nb_ts_packets & 0xff; 148 149 deb_info("set the USB xfer len to %i Ts packet\n", nb_ts_packets); 150 151 ret = dib0700_ctrl_wr(d, st->buf, 3); 152 mutex_unlock(&d->usb_mutex); 153 } else { 154 deb_info("this firmware does not allow to change the USB xfer len\n"); 155 ret = -EIO; 156 } 157 158 return ret; 159 } 160 161 /* 162 * I2C master xfer function (supported in 1.20 firmware) 163 */ 164 static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg, 165 int num) 166 { 167 /* The new i2c firmware messages are more reliable and in particular 168 properly support i2c read calls not preceded by a write */ 169 170 struct dvb_usb_device *d = i2c_get_adapdata(adap); 171 struct dib0700_state *st = d->priv; 172 uint8_t bus_mode = 1; /* 0=eeprom bus, 1=frontend bus */ 173 uint8_t gen_mode = 0; /* 0=master i2c, 1=gpio i2c */ 174 uint8_t en_start = 0; 175 uint8_t en_stop = 0; 176 int result, i; 177 178 /* Ensure nobody else hits the i2c bus while we're sending our 179 sequence of messages, (such as the remote control thread) */ 180 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 181 return -EINTR; 182 183 for (i = 0; i < num; i++) { 184 if (i == 0) { 185 /* First message in the transaction */ 186 en_start = 1; 187 } else if (!(msg[i].flags & I2C_M_NOSTART)) { 188 /* Device supports repeated-start */ 189 en_start = 1; 190 } else { 191 /* Not the first packet and device doesn't support 192 repeated start */ 193 en_start = 0; 194 } 195 if (i == (num - 1)) { 196 /* Last message in the transaction */ 197 en_stop = 1; 198 } 199 200 if (msg[i].flags & I2C_M_RD) { 201 /* Read request */ 202 u16 index, value; 203 uint8_t i2c_dest; 204 205 i2c_dest = (msg[i].addr << 1); 206 value = ((en_start << 7) | (en_stop << 6) | 207 (msg[i].len & 0x3F)) << 8 | i2c_dest; 208 /* I2C ctrl + FE bus; */ 209 index = ((gen_mode << 6) & 0xC0) | 210 ((bus_mode << 4) & 0x30); 211 212 result = usb_control_msg(d->udev, 213 usb_rcvctrlpipe(d->udev, 0), 214 REQUEST_NEW_I2C_READ, 215 USB_TYPE_VENDOR | USB_DIR_IN, 216 value, index, msg[i].buf, 217 msg[i].len, 218 USB_CTRL_GET_TIMEOUT); 219 if (result < 0) { 220 deb_info("i2c read error (status = %d)\n", result); 221 break; 222 } 223 224 deb_data("<<< "); 225 debug_dump(msg[i].buf, msg[i].len, deb_data); 226 227 } else { 228 /* Write request */ 229 if (mutex_lock_interruptible(&d->usb_mutex) < 0) { 230 err("could not acquire lock"); 231 mutex_unlock(&d->i2c_mutex); 232 return -EINTR; 233 } 234 st->buf[0] = REQUEST_NEW_I2C_WRITE; 235 st->buf[1] = msg[i].addr << 1; 236 st->buf[2] = (en_start << 7) | (en_stop << 6) | 237 (msg[i].len & 0x3F); 238 /* I2C ctrl + FE bus; */ 239 st->buf[3] = ((gen_mode << 6) & 0xC0) | 240 ((bus_mode << 4) & 0x30); 241 /* The Actual i2c payload */ 242 memcpy(&st->buf[4], msg[i].buf, msg[i].len); 243 244 deb_data(">>> "); 245 debug_dump(st->buf, msg[i].len + 4, deb_data); 246 247 result = usb_control_msg(d->udev, 248 usb_sndctrlpipe(d->udev, 0), 249 REQUEST_NEW_I2C_WRITE, 250 USB_TYPE_VENDOR | USB_DIR_OUT, 251 0, 0, st->buf, msg[i].len + 4, 252 USB_CTRL_GET_TIMEOUT); 253 mutex_unlock(&d->usb_mutex); 254 if (result < 0) { 255 deb_info("i2c write error (status = %d)\n", result); 256 break; 257 } 258 } 259 } 260 mutex_unlock(&d->i2c_mutex); 261 return i; 262 } 263 264 /* 265 * I2C master xfer function (pre-1.20 firmware) 266 */ 267 static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap, 268 struct i2c_msg *msg, int num) 269 { 270 struct dvb_usb_device *d = i2c_get_adapdata(adap); 271 struct dib0700_state *st = d->priv; 272 int i,len; 273 274 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 275 return -EINTR; 276 if (mutex_lock_interruptible(&d->usb_mutex) < 0) { 277 err("could not acquire lock"); 278 mutex_unlock(&d->i2c_mutex); 279 return -EINTR; 280 } 281 282 for (i = 0; i < num; i++) { 283 /* fill in the address */ 284 st->buf[1] = msg[i].addr << 1; 285 /* fill the buffer */ 286 memcpy(&st->buf[2], msg[i].buf, msg[i].len); 287 288 /* write/read request */ 289 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { 290 st->buf[0] = REQUEST_I2C_READ; 291 st->buf[1] |= 1; 292 293 /* special thing in the current firmware: when length is zero the read-failed */ 294 len = dib0700_ctrl_rd(d, st->buf, msg[i].len + 2, 295 msg[i+1].buf, msg[i+1].len); 296 if (len <= 0) { 297 deb_info("I2C read failed on address 0x%02x\n", 298 msg[i].addr); 299 break; 300 } 301 302 msg[i+1].len = len; 303 304 i++; 305 } else { 306 st->buf[0] = REQUEST_I2C_WRITE; 307 if (dib0700_ctrl_wr(d, st->buf, msg[i].len + 2) < 0) 308 break; 309 } 310 } 311 mutex_unlock(&d->usb_mutex); 312 mutex_unlock(&d->i2c_mutex); 313 314 return i; 315 } 316 317 static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, 318 int num) 319 { 320 struct dvb_usb_device *d = i2c_get_adapdata(adap); 321 struct dib0700_state *st = d->priv; 322 323 if (st->fw_use_new_i2c_api == 1) { 324 /* User running at least fw 1.20 */ 325 return dib0700_i2c_xfer_new(adap, msg, num); 326 } else { 327 /* Use legacy calls */ 328 return dib0700_i2c_xfer_legacy(adap, msg, num); 329 } 330 } 331 332 static u32 dib0700_i2c_func(struct i2c_adapter *adapter) 333 { 334 return I2C_FUNC_I2C; 335 } 336 337 struct i2c_algorithm dib0700_i2c_algo = { 338 .master_xfer = dib0700_i2c_xfer, 339 .functionality = dib0700_i2c_func, 340 }; 341 342 int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props, 343 struct dvb_usb_device_description **desc, int *cold) 344 { 345 s16 ret; 346 u8 *b; 347 348 b = kmalloc(16, GFP_KERNEL); 349 if (!b) 350 return -ENOMEM; 351 352 353 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 354 REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT); 355 356 deb_info("FW GET_VERSION length: %d\n",ret); 357 358 *cold = ret <= 0; 359 deb_info("cold: %d\n", *cold); 360 361 kfree(b); 362 return 0; 363 } 364 365 static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll, 366 u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv, 367 u16 pll_loopdiv, u16 free_div, u16 dsuScaler) 368 { 369 struct dib0700_state *st = d->priv; 370 int ret; 371 372 if (mutex_lock_interruptible(&d->usb_mutex) < 0) { 373 err("could not acquire lock"); 374 return -EINTR; 375 } 376 377 st->buf[0] = REQUEST_SET_CLOCK; 378 st->buf[1] = (en_pll << 7) | (pll_src << 6) | 379 (pll_range << 5) | (clock_gpio3 << 4); 380 st->buf[2] = (pll_prediv >> 8) & 0xff; /* MSB */ 381 st->buf[3] = pll_prediv & 0xff; /* LSB */ 382 st->buf[4] = (pll_loopdiv >> 8) & 0xff; /* MSB */ 383 st->buf[5] = pll_loopdiv & 0xff; /* LSB */ 384 st->buf[6] = (free_div >> 8) & 0xff; /* MSB */ 385 st->buf[7] = free_div & 0xff; /* LSB */ 386 st->buf[8] = (dsuScaler >> 8) & 0xff; /* MSB */ 387 st->buf[9] = dsuScaler & 0xff; /* LSB */ 388 389 ret = dib0700_ctrl_wr(d, st->buf, 10); 390 mutex_unlock(&d->usb_mutex); 391 392 return ret; 393 } 394 395 int dib0700_set_i2c_speed(struct dvb_usb_device *d, u16 scl_kHz) 396 { 397 struct dib0700_state *st = d->priv; 398 u16 divider; 399 int ret; 400 401 if (scl_kHz == 0) 402 return -EINVAL; 403 404 if (mutex_lock_interruptible(&d->usb_mutex) < 0) { 405 err("could not acquire lock"); 406 return -EINTR; 407 } 408 409 st->buf[0] = REQUEST_SET_I2C_PARAM; 410 divider = (u16) (30000 / scl_kHz); 411 st->buf[1] = 0; 412 st->buf[2] = (u8) (divider >> 8); 413 st->buf[3] = (u8) (divider & 0xff); 414 divider = (u16) (72000 / scl_kHz); 415 st->buf[4] = (u8) (divider >> 8); 416 st->buf[5] = (u8) (divider & 0xff); 417 divider = (u16) (72000 / scl_kHz); /* clock: 72MHz */ 418 st->buf[6] = (u8) (divider >> 8); 419 st->buf[7] = (u8) (divider & 0xff); 420 421 deb_info("setting I2C speed: %04x %04x %04x (%d kHz).", 422 (st->buf[2] << 8) | (st->buf[3]), (st->buf[4] << 8) | 423 st->buf[5], (st->buf[6] << 8) | st->buf[7], scl_kHz); 424 425 ret = dib0700_ctrl_wr(d, st->buf, 8); 426 mutex_unlock(&d->usb_mutex); 427 428 return ret; 429 } 430 431 432 int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3) 433 { 434 switch (clk_MHz) { 435 case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break; 436 default: return -EINVAL; 437 } 438 return 0; 439 } 440 441 static int dib0700_jumpram(struct usb_device *udev, u32 address) 442 { 443 int ret = 0, actlen; 444 u8 *buf; 445 446 buf = kmalloc(8, GFP_KERNEL); 447 if (!buf) 448 return -ENOMEM; 449 buf[0] = REQUEST_JUMPRAM; 450 buf[1] = 0; 451 buf[2] = 0; 452 buf[3] = 0; 453 buf[4] = (address >> 24) & 0xff; 454 buf[5] = (address >> 16) & 0xff; 455 buf[6] = (address >> 8) & 0xff; 456 buf[7] = address & 0xff; 457 458 if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) { 459 deb_fw("jumpram to 0x%x failed\n",address); 460 goto out; 461 } 462 if (actlen != 8) { 463 deb_fw("jumpram to 0x%x failed\n",address); 464 ret = -EIO; 465 goto out; 466 } 467 out: 468 kfree(buf); 469 return ret; 470 } 471 472 int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw) 473 { 474 struct hexline hx; 475 int pos = 0, ret, act_len, i, adap_num; 476 u8 *buf; 477 u32 fw_version; 478 479 buf = kmalloc(260, GFP_KERNEL); 480 if (!buf) 481 return -ENOMEM; 482 483 while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) { 484 deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n", 485 hx.addr, hx.len, hx.chk); 486 487 buf[0] = hx.len; 488 buf[1] = (hx.addr >> 8) & 0xff; 489 buf[2] = hx.addr & 0xff; 490 buf[3] = hx.type; 491 memcpy(&buf[4],hx.data,hx.len); 492 buf[4+hx.len] = hx.chk; 493 494 ret = usb_bulk_msg(udev, 495 usb_sndbulkpipe(udev, 0x01), 496 buf, 497 hx.len + 5, 498 &act_len, 499 1000); 500 501 if (ret < 0) { 502 err("firmware download failed at %d with %d",pos,ret); 503 goto out; 504 } 505 } 506 507 if (ret == 0) { 508 /* start the firmware */ 509 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) { 510 info("firmware started successfully."); 511 msleep(500); 512 } 513 } else 514 ret = -EIO; 515 516 /* the number of ts packet has to be at least 1 */ 517 if (nb_packet_buffer_size < 1) 518 nb_packet_buffer_size = 1; 519 520 /* get the fimware version */ 521 usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 522 REQUEST_GET_VERSION, 523 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, 524 buf, 16, USB_CTRL_GET_TIMEOUT); 525 fw_version = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11]; 526 527 /* set the buffer size - DVB-USB is allocating URB buffers 528 * only after the firwmare download was successful */ 529 for (i = 0; i < dib0700_device_count; i++) { 530 for (adap_num = 0; adap_num < dib0700_devices[i].num_adapters; 531 adap_num++) { 532 if (fw_version >= 0x10201) { 533 dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 188*nb_packet_buffer_size; 534 } else { 535 /* for fw version older than 1.20.1, 536 * the buffersize has to be n times 512 */ 537 dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = ((188*nb_packet_buffer_size+188/2)/512)*512; 538 if (dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize < 512) 539 dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 512; 540 } 541 } 542 } 543 out: 544 kfree(buf); 545 return ret; 546 } 547 548 int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff) 549 { 550 struct dib0700_state *st = adap->dev->priv; 551 int ret; 552 553 if ((onoff != 0) && (st->fw_version >= 0x10201)) { 554 /* for firmware later than 1.20.1, 555 * the USB xfer length can be set */ 556 ret = dib0700_set_usb_xfer_len(adap->dev, 557 st->nb_packet_buffer_size); 558 if (ret < 0) { 559 deb_info("can not set the USB xfer len\n"); 560 return ret; 561 } 562 } 563 564 mutex_lock(&adap->dev->usb_mutex); 565 566 st->buf[0] = REQUEST_ENABLE_VIDEO; 567 /* this bit gives a kind of command, 568 * rather than enabling something or not */ 569 st->buf[1] = (onoff << 4) | 0x00; 570 571 if (st->disable_streaming_master_mode == 1) 572 st->buf[2] = 0x00; 573 else 574 st->buf[2] = 0x01 << 4; /* Master mode */ 575 576 st->buf[3] = 0x00; 577 578 deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id); 579 580 st->channel_state &= ~0x3; 581 if ((adap->fe_adap[0].stream.props.endpoint != 2) 582 && (adap->fe_adap[0].stream.props.endpoint != 3)) { 583 deb_info("the endpoint number (%i) is not correct, use the adapter id instead", adap->fe_adap[0].stream.props.endpoint); 584 if (onoff) 585 st->channel_state |= 1 << (adap->id); 586 else 587 st->channel_state |= 1 << ~(adap->id); 588 } else { 589 if (onoff) 590 st->channel_state |= 1 << (adap->fe_adap[0].stream.props.endpoint-2); 591 else 592 st->channel_state |= 1 << (3-adap->fe_adap[0].stream.props.endpoint); 593 } 594 595 st->buf[2] |= st->channel_state; 596 597 deb_info("data for streaming: %x %x\n", st->buf[1], st->buf[2]); 598 599 ret = dib0700_ctrl_wr(adap->dev, st->buf, 4); 600 mutex_unlock(&adap->dev->usb_mutex); 601 602 return ret; 603 } 604 605 int dib0700_change_protocol(struct rc_dev *rc, u64 *rc_type) 606 { 607 struct dvb_usb_device *d = rc->priv; 608 struct dib0700_state *st = d->priv; 609 int new_proto, ret; 610 611 if (mutex_lock_interruptible(&d->usb_mutex) < 0) { 612 err("could not acquire lock"); 613 return -EINTR; 614 } 615 616 st->buf[0] = REQUEST_SET_RC; 617 st->buf[1] = 0; 618 st->buf[2] = 0; 619 620 /* Set the IR mode */ 621 if (*rc_type & RC_BIT_RC5) { 622 new_proto = 1; 623 *rc_type = RC_BIT_RC5; 624 } else if (*rc_type & RC_BIT_NEC) { 625 new_proto = 0; 626 *rc_type = RC_BIT_NEC; 627 } else if (*rc_type & RC_BIT_RC6_MCE) { 628 if (st->fw_version < 0x10200) { 629 ret = -EINVAL; 630 goto out; 631 } 632 new_proto = 2; 633 *rc_type = RC_BIT_RC6_MCE; 634 } else { 635 ret = -EINVAL; 636 goto out; 637 } 638 639 st->buf[1] = new_proto; 640 641 ret = dib0700_ctrl_wr(d, st->buf, 3); 642 if (ret < 0) { 643 err("ir protocol setup failed"); 644 goto out; 645 } 646 647 d->props.rc.core.protocol = *rc_type; 648 649 out: 650 mutex_unlock(&d->usb_mutex); 651 return ret; 652 } 653 654 /* Number of keypresses to ignore before start repeating */ 655 #define RC_REPEAT_DELAY_V1_20 10 656 657 /* This is the structure of the RC response packet starting in firmware 1.20 */ 658 struct dib0700_rc_response { 659 u8 report_id; 660 u8 data_state; 661 union { 662 u16 system16; 663 struct { 664 u8 not_system; 665 u8 system; 666 }; 667 }; 668 u8 data; 669 u8 not_data; 670 }; 671 #define RC_MSG_SIZE_V1_20 6 672 673 static void dib0700_rc_urb_completion(struct urb *purb) 674 { 675 struct dvb_usb_device *d = purb->context; 676 struct dib0700_rc_response *poll_reply; 677 u32 uninitialized_var(keycode); 678 u8 toggle; 679 680 deb_info("%s()\n", __func__); 681 if (d->rc_dev == NULL) { 682 /* This will occur if disable_rc_polling=1 */ 683 kfree(purb->transfer_buffer); 684 usb_free_urb(purb); 685 return; 686 } 687 688 poll_reply = purb->transfer_buffer; 689 690 if (purb->status < 0) { 691 deb_info("discontinuing polling\n"); 692 kfree(purb->transfer_buffer); 693 usb_free_urb(purb); 694 return; 695 } 696 697 if (purb->actual_length != RC_MSG_SIZE_V1_20) { 698 deb_info("malformed rc msg size=%d\n", purb->actual_length); 699 goto resubmit; 700 } 701 702 deb_data("IR ID = %02X state = %02X System = %02X %02X Cmd = %02X %02X (len %d)\n", 703 poll_reply->report_id, poll_reply->data_state, 704 poll_reply->system, poll_reply->not_system, 705 poll_reply->data, poll_reply->not_data, 706 purb->actual_length); 707 708 switch (d->props.rc.core.protocol) { 709 case RC_BIT_NEC: 710 toggle = 0; 711 712 /* NEC protocol sends repeat code as 0 0 0 FF */ 713 if ((poll_reply->system == 0x00) && (poll_reply->data == 0x00) 714 && (poll_reply->not_data == 0xff)) { 715 poll_reply->data_state = 2; 716 break; 717 } 718 719 if ((poll_reply->system ^ poll_reply->not_system) != 0xff) { 720 deb_data("NEC extended protocol\n"); 721 /* NEC extended code - 24 bits */ 722 keycode = be16_to_cpu(poll_reply->system16) << 8 | poll_reply->data; 723 } else { 724 deb_data("NEC normal protocol\n"); 725 /* normal NEC code - 16 bits */ 726 keycode = poll_reply->system << 8 | poll_reply->data; 727 } 728 729 break; 730 default: 731 deb_data("RC5 protocol\n"); 732 /* RC5 Protocol */ 733 toggle = poll_reply->report_id; 734 keycode = poll_reply->system << 8 | poll_reply->data; 735 736 break; 737 } 738 739 if ((poll_reply->data + poll_reply->not_data) != 0xff) { 740 /* Key failed integrity check */ 741 err("key failed integrity check: %04x %02x %02x", 742 poll_reply->system, 743 poll_reply->data, poll_reply->not_data); 744 goto resubmit; 745 } 746 747 rc_keydown(d->rc_dev, keycode, toggle); 748 749 resubmit: 750 /* Clean the buffer before we requeue */ 751 memset(purb->transfer_buffer, 0, RC_MSG_SIZE_V1_20); 752 753 /* Requeue URB */ 754 usb_submit_urb(purb, GFP_ATOMIC); 755 } 756 757 int dib0700_rc_setup(struct dvb_usb_device *d, struct usb_interface *intf) 758 { 759 struct dib0700_state *st = d->priv; 760 struct urb *purb; 761 const struct usb_endpoint_descriptor *e; 762 int ret, rc_ep = 1; 763 unsigned int pipe = 0; 764 765 /* Poll-based. Don't initialize bulk mode */ 766 if (st->fw_version < 0x10200 || !intf) 767 return 0; 768 769 /* Starting in firmware 1.20, the RC info is provided on a bulk pipe */ 770 771 purb = usb_alloc_urb(0, GFP_KERNEL); 772 if (purb == NULL) { 773 err("rc usb alloc urb failed"); 774 return -ENOMEM; 775 } 776 777 purb->transfer_buffer = kzalloc(RC_MSG_SIZE_V1_20, GFP_KERNEL); 778 if (purb->transfer_buffer == NULL) { 779 err("rc kzalloc failed"); 780 usb_free_urb(purb); 781 return -ENOMEM; 782 } 783 784 purb->status = -EINPROGRESS; 785 786 /* 787 * Some devices like the Hauppauge NovaTD model 52009 use an interrupt 788 * endpoint, while others use a bulk one. 789 */ 790 e = &intf->altsetting[0].endpoint[rc_ep].desc; 791 if (usb_endpoint_dir_in(e)) { 792 if (usb_endpoint_xfer_bulk(e)) { 793 pipe = usb_rcvbulkpipe(d->udev, rc_ep); 794 usb_fill_bulk_urb(purb, d->udev, pipe, 795 purb->transfer_buffer, 796 RC_MSG_SIZE_V1_20, 797 dib0700_rc_urb_completion, d); 798 799 } else if (usb_endpoint_xfer_int(e)) { 800 pipe = usb_rcvintpipe(d->udev, rc_ep); 801 usb_fill_int_urb(purb, d->udev, pipe, 802 purb->transfer_buffer, 803 RC_MSG_SIZE_V1_20, 804 dib0700_rc_urb_completion, d, 1); 805 } 806 } 807 808 if (!pipe) { 809 err("There's no endpoint for remote controller"); 810 kfree(purb->transfer_buffer); 811 usb_free_urb(purb); 812 return 0; 813 } 814 815 ret = usb_submit_urb(purb, GFP_ATOMIC); 816 if (ret) { 817 err("rc submit urb failed"); 818 kfree(purb->transfer_buffer); 819 usb_free_urb(purb); 820 } 821 822 return ret; 823 } 824 825 static int dib0700_probe(struct usb_interface *intf, 826 const struct usb_device_id *id) 827 { 828 int i; 829 struct dvb_usb_device *dev; 830 831 for (i = 0; i < dib0700_device_count; i++) 832 if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE, 833 &dev, adapter_nr) == 0) { 834 struct dib0700_state *st = dev->priv; 835 u32 hwversion, romversion, fw_version, fwtype; 836 837 dib0700_get_version(dev, &hwversion, &romversion, 838 &fw_version, &fwtype); 839 840 deb_info("Firmware version: %x, %d, 0x%x, %d\n", 841 hwversion, romversion, fw_version, fwtype); 842 843 st->fw_version = fw_version; 844 st->nb_packet_buffer_size = (u32)nb_packet_buffer_size; 845 846 /* Disable polling mode on newer firmwares */ 847 if (st->fw_version >= 0x10200) 848 dev->props.rc.core.bulk_mode = true; 849 else 850 dev->props.rc.core.bulk_mode = false; 851 852 dib0700_rc_setup(dev, intf); 853 854 return 0; 855 } 856 857 return -ENODEV; 858 } 859 860 static struct usb_driver dib0700_driver = { 861 .name = "dvb_usb_dib0700", 862 .probe = dib0700_probe, 863 .disconnect = dvb_usb_device_exit, 864 .id_table = dib0700_usb_id_table, 865 }; 866 867 module_usb_driver(dib0700_driver); 868 869 MODULE_FIRMWARE("dvb-usb-dib0700-1.20.fw"); 870 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>"); 871 MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge"); 872 MODULE_VERSION("1.0"); 873 MODULE_LICENSE("GPL"); 874