1/* 2 * Realtek RTL28xxU DVB USB driver 3 * 4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi> 5 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 */ 21 22#include "rtl28xxu.h" 23 24#include "rtl2830.h" 25 26#include "qt1010.h" 27#include "mt2060.h" 28#include "mxl5005s.h" 29 30/* debug */ 31static int dvb_usb_rtl28xxu_debug; 32module_param_named(debug, dvb_usb_rtl28xxu_debug, int, 0644); 33MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS); 34DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 35 36static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req) 37{ 38 int ret; 39 unsigned int pipe; 40 u8 requesttype; 41 u8 *buf; 42 43 buf = kmalloc(req->size, GFP_KERNEL); 44 if (!buf) { 45 ret = -ENOMEM; 46 goto err; 47 } 48 49 if (req->index & CMD_WR_FLAG) { 50 /* write */ 51 memcpy(buf, req->data, req->size); 52 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT); 53 pipe = usb_sndctrlpipe(d->udev, 0); 54 } else { 55 /* read */ 56 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN); 57 pipe = usb_rcvctrlpipe(d->udev, 0); 58 } 59 60 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value, 61 req->index, buf, req->size, 1000); 62 if (ret > 0) 63 ret = 0; 64 65 deb_dump(0, requesttype, req->value, req->index, buf, req->size, 66 deb_xfer); 67 68 /* read request, copy returned data to return buf */ 69 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN)) 70 memcpy(req->data, buf, req->size); 71 72 kfree(buf); 73 74 if (ret) 75 goto err; 76 77 return ret; 78err: 79 deb_info("%s: failed=%d\n", __func__, ret); 80 return ret; 81} 82 83static int rtl2831_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len) 84{ 85 struct rtl28xxu_req req; 86 87 if (reg < 0x3000) 88 req.index = CMD_USB_WR; 89 else if (reg < 0x4000) 90 req.index = CMD_SYS_WR; 91 else 92 req.index = CMD_IR_WR; 93 94 req.value = reg; 95 req.size = len; 96 req.data = val; 97 98 return rtl28xxu_ctrl_msg(d, &req); 99} 100 101static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len) 102{ 103 struct rtl28xxu_req req; 104 105 if (reg < 0x3000) 106 req.index = CMD_USB_RD; 107 else if (reg < 0x4000) 108 req.index = CMD_SYS_RD; 109 else 110 req.index = CMD_IR_RD; 111 112 req.value = reg; 113 req.size = len; 114 req.data = val; 115 116 return rtl28xxu_ctrl_msg(d, &req); 117} 118 119static int rtl2831_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val) 120{ 121 return rtl2831_wr_regs(d, reg, &val, 1); 122} 123 124static int rtl2831_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val) 125{ 126 return rtl2831_rd_regs(d, reg, val, 1); 127} 128 129/* I2C */ 130static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 131 int num) 132{ 133 int ret; 134 struct dvb_usb_device *d = i2c_get_adapdata(adap); 135 struct rtl28xxu_priv *priv = d->priv; 136 struct rtl28xxu_req req; 137 138 /* 139 * It is not known which are real I2C bus xfer limits, but testing 140 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes. 141 * TODO: find out RTL2832U lens 142 */ 143 144 /* 145 * I2C adapter logic looks rather complicated due to fact it handles 146 * three different access methods. Those methods are; 147 * 1) integrated demod access 148 * 2) old I2C access 149 * 3) new I2C access 150 * 151 * Used method is selected in order 1, 2, 3. Method 3 can handle all 152 * requests but there is two reasons why not use it always; 153 * 1) It is most expensive, usually two USB messages are needed 154 * 2) At least RTL2831U does not support it 155 * 156 * Method 3 is needed in case of I2C write+read (typical register read) 157 * where write is more than one byte. 158 */ 159 160 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 161 return -EAGAIN; 162 163 if (num == 2 && !(msg[0].flags & I2C_M_RD) && 164 (msg[1].flags & I2C_M_RD)) { 165 if (msg[0].len > 24 || msg[1].len > 24) { 166 /* TODO: check msg[0].len max */ 167 ret = -EOPNOTSUPP; 168 goto err_mutex_unlock; 169 } else if (msg[0].addr == 0x10) { 170 /* method 1 - integrated demod */ 171 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1); 172 req.index = CMD_DEMOD_RD | priv->page; 173 req.size = msg[1].len; 174 req.data = &msg[1].buf[0]; 175 ret = rtl28xxu_ctrl_msg(d, &req); 176 } else if (msg[0].len < 2) { 177 /* method 2 - old I2C */ 178 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1); 179 req.index = CMD_I2C_RD; 180 req.size = msg[1].len; 181 req.data = &msg[1].buf[0]; 182 ret = rtl28xxu_ctrl_msg(d, &req); 183 } else { 184 /* method 3 - new I2C */ 185 req.value = (msg[0].addr << 1); 186 req.index = CMD_I2C_DA_WR; 187 req.size = msg[0].len; 188 req.data = msg[0].buf; 189 ret = rtl28xxu_ctrl_msg(d, &req); 190 if (ret) 191 goto err_mutex_unlock; 192 193 req.value = (msg[0].addr << 1); 194 req.index = CMD_I2C_DA_RD; 195 req.size = msg[1].len; 196 req.data = msg[1].buf; 197 ret = rtl28xxu_ctrl_msg(d, &req); 198 } 199 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) { 200 if (msg[0].len > 22) { 201 /* TODO: check msg[0].len max */ 202 ret = -EOPNOTSUPP; 203 goto err_mutex_unlock; 204 } else if (msg[0].addr == 0x10) { 205 /* method 1 - integrated demod */ 206 if (msg[0].buf[0] == 0x00) { 207 /* save demod page for later demod access */ 208 priv->page = msg[0].buf[1]; 209 ret = 0; 210 } else { 211 req.value = (msg[0].buf[0] << 8) | 212 (msg[0].addr << 1); 213 req.index = CMD_DEMOD_WR | priv->page; 214 req.size = msg[0].len-1; 215 req.data = &msg[0].buf[1]; 216 ret = rtl28xxu_ctrl_msg(d, &req); 217 } 218 } else if (msg[0].len < 23) { 219 /* method 2 - old I2C */ 220 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1); 221 req.index = CMD_I2C_WR; 222 req.size = msg[0].len-1; 223 req.data = &msg[0].buf[1]; 224 ret = rtl28xxu_ctrl_msg(d, &req); 225 } else { 226 /* method 3 - new I2C */ 227 req.value = (msg[0].addr << 1); 228 req.index = CMD_I2C_DA_WR; 229 req.size = msg[0].len; 230 req.data = msg[0].buf; 231 ret = rtl28xxu_ctrl_msg(d, &req); 232 } 233 } else { 234 ret = -EINVAL; 235 } 236 237err_mutex_unlock: 238 mutex_unlock(&d->i2c_mutex); 239 240 return ret ? ret : num; 241} 242 243static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter) 244{ 245 return I2C_FUNC_I2C; 246} 247 248static struct i2c_algorithm rtl28xxu_i2c_algo = { 249 .master_xfer = rtl28xxu_i2c_xfer, 250 .functionality = rtl28xxu_i2c_func, 251}; 252 253static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = { 254 .i2c_addr = 0x10, /* 0x20 */ 255 .xtal = 28800000, 256 .ts_mode = 0, 257 .spec_inv = 1, 258 .if_dvbt = 36150000, 259 .vtop = 0x20, 260 .krf = 0x04, 261 .agc_targ_val = 0x2d, 262 263}; 264 265static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = { 266 .i2c_addr = 0x10, /* 0x20 */ 267 .xtal = 28800000, 268 .ts_mode = 0, 269 .spec_inv = 1, 270 .if_dvbt = 36125000, 271 .vtop = 0x20, 272 .krf = 0x04, 273 .agc_targ_val = 0x2d, 274}; 275 276static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = { 277 .i2c_addr = 0x10, /* 0x20 */ 278 .xtal = 28800000, 279 .ts_mode = 0, 280 .spec_inv = 0, 281 .if_dvbt = 4570000, 282 .vtop = 0x3f, 283 .krf = 0x04, 284 .agc_targ_val = 0x3e, 285}; 286 287static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap) 288{ 289 int ret; 290 struct rtl28xxu_priv *priv = adap->dev->priv; 291 u8 buf[1]; 292 struct rtl2830_config *rtl2830_config; 293 /* open RTL2831U/RTL2830 I2C gate */ 294 struct rtl28xxu_req req_gate = { 0x0120, 0x0011, 0x0001, "\x08" }; 295 /* for MT2060 tuner probe */ 296 struct rtl28xxu_req req_mt2060 = { 0x00c0, CMD_I2C_RD, 1, buf }; 297 /* for QT1010 tuner probe */ 298 struct rtl28xxu_req req_qt1010 = { 0x0fc4, CMD_I2C_RD, 1, buf }; 299 300 deb_info("%s:\n", __func__); 301 302 /* 303 * RTL2831U GPIOs 304 * ========================================================= 305 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?) 306 * GPIO2 | LED | 0 off | 1 on | 307 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060 308 */ 309 310 /* GPIO direction */ 311 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a); 312 if (ret) 313 goto err; 314 315 /* enable as output GPIO0, GPIO2, GPIO4 */ 316 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15); 317 if (ret) 318 goto err; 319 320 /* 321 * Probe used tuner. We need to know used tuner before demod attach 322 * since there is some demod params needed to set according to tuner. 323 */ 324 325 /* open demod I2C gate */ 326 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate); 327 if (ret) 328 goto err; 329 330 /* check QT1010 ID(?) register; reg=0f val=2c */ 331 ret = rtl28xxu_ctrl_msg(adap->dev, &req_qt1010); 332 if (ret == 0 && buf[0] == 0x2c) { 333 priv->tuner = TUNER_RTL2830_QT1010; 334 rtl2830_config = &rtl28xxu_rtl2830_qt1010_config; 335 deb_info("%s: QT1010\n", __func__); 336 goto found; 337 } else { 338 deb_info("%s: QT1010 probe failed=%d - %02x\n", 339 __func__, ret, buf[0]); 340 } 341 342 /* open demod I2C gate */ 343 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate); 344 if (ret) 345 goto err; 346 347 /* check MT2060 ID register; reg=00 val=63 */ 348 ret = rtl28xxu_ctrl_msg(adap->dev, &req_mt2060); 349 if (ret == 0 && buf[0] == 0x63) { 350 priv->tuner = TUNER_RTL2830_MT2060; 351 rtl2830_config = &rtl28xxu_rtl2830_mt2060_config; 352 deb_info("%s: MT2060\n", __func__); 353 goto found; 354 } else { 355 deb_info("%s: MT2060 probe failed=%d - %02x\n", 356 __func__, ret, buf[0]); 357 } 358 359 /* assume MXL5005S */ 360 ret = 0; 361 priv->tuner = TUNER_RTL2830_MXL5005S; 362 rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config; 363 deb_info("%s: MXL5005S\n", __func__); 364 goto found; 365 366found: 367 /* attach demodulator */ 368 adap->fe_adap[0].fe = dvb_attach(rtl2830_attach, rtl2830_config, 369 &adap->dev->i2c_adap); 370 if (adap->fe_adap[0].fe == NULL) { 371 ret = -ENODEV; 372 goto err; 373 } 374 375 return ret; 376err: 377 deb_info("%s: failed=%d\n", __func__, ret); 378 return ret; 379} 380 381static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap) 382{ 383 int ret; 384 struct rtl28xxu_priv *priv = adap->dev->priv; 385 u8 buf[1]; 386 /* open RTL2832U/RTL2832 I2C gate */ 387 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"}; 388 /* close RTL2832U/RTL2832 I2C gate */ 389 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"}; 390 /* for FC2580 tuner probe */ 391 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf}; 392 393 deb_info("%s:\n", __func__); 394 395 /* GPIO direction */ 396 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a); 397 if (ret) 398 goto err; 399 400 /* enable as output GPIO0, GPIO2, GPIO4 */ 401 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15); 402 if (ret) 403 goto err; 404 405 ret = rtl2831_wr_reg(adap->dev, SYS_DEMOD_CTL, 0xe8); 406 if (ret) 407 goto err; 408 409 /* 410 * Probe used tuner. We need to know used tuner before demod attach 411 * since there is some demod params needed to set according to tuner. 412 */ 413 414 /* open demod I2C gate */ 415 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_open); 416 if (ret) 417 goto err; 418 419 /* check FC2580 ID register; reg=01 val=56 */ 420 ret = rtl28xxu_ctrl_msg(adap->dev, &req_fc2580); 421 if (ret == 0 && buf[0] == 0x56) { 422 priv->tuner = TUNER_RTL2832_FC2580; 423 deb_info("%s: FC2580\n", __func__); 424 goto found; 425 } else { 426 deb_info("%s: FC2580 probe failed=%d - %02x\n", 427 __func__, ret, buf[0]); 428 } 429 430 /* close demod I2C gate */ 431 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close); 432 if (ret) 433 goto err; 434 435 /* tuner not found */ 436 ret = -ENODEV; 437 goto err; 438 439found: 440 /* close demod I2C gate */ 441 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close); 442 if (ret) 443 goto err; 444 445 /* attach demodulator */ 446 /* TODO: */ 447 448 return ret; 449err: 450 deb_info("%s: failed=%d\n", __func__, ret); 451 return ret; 452} 453 454static struct qt1010_config rtl28xxu_qt1010_config = { 455 .i2c_address = 0x62, /* 0xc4 */ 456}; 457 458static struct mt2060_config rtl28xxu_mt2060_config = { 459 .i2c_address = 0x60, /* 0xc0 */ 460 .clock_out = 0, 461}; 462 463static struct mxl5005s_config rtl28xxu_mxl5005s_config = { 464 .i2c_address = 0x63, /* 0xc6 */ 465 .if_freq = IF_FREQ_4570000HZ, 466 .xtal_freq = CRYSTAL_FREQ_16000000HZ, 467 .agc_mode = MXL_SINGLE_AGC, 468 .tracking_filter = MXL_TF_C_H, 469 .rssi_enable = MXL_RSSI_ENABLE, 470 .cap_select = MXL_CAP_SEL_ENABLE, 471 .div_out = MXL_DIV_OUT_4, 472 .clock_out = MXL_CLOCK_OUT_DISABLE, 473 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM, 474 .top = MXL5005S_TOP_25P2, 475 .mod_mode = MXL_DIGITAL_MODE, 476 .if_mode = MXL_ZERO_IF, 477 .AgcMasterByte = 0x00, 478}; 479 480static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap) 481{ 482 int ret; 483 struct rtl28xxu_priv *priv = adap->dev->priv; 484 struct i2c_adapter *rtl2830_tuner_i2c; 485 struct dvb_frontend *fe; 486 487 deb_info("%s:\n", __func__); 488 489 /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */ 490 rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe_adap[0].fe); 491 492 switch (priv->tuner) { 493 case TUNER_RTL2830_QT1010: 494 fe = dvb_attach(qt1010_attach, adap->fe_adap[0].fe, 495 rtl2830_tuner_i2c, &rtl28xxu_qt1010_config); 496 break; 497 case TUNER_RTL2830_MT2060: 498 fe = dvb_attach(mt2060_attach, adap->fe_adap[0].fe, 499 rtl2830_tuner_i2c, &rtl28xxu_mt2060_config, 500 1220); 501 break; 502 case TUNER_RTL2830_MXL5005S: 503 fe = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe, 504 rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config); 505 break; 506 default: 507 fe = NULL; 508 err("unknown tuner=%d", priv->tuner); 509 } 510 511 if (fe == NULL) { 512 ret = -ENODEV; 513 goto err; 514 } 515 516 return 0; 517err: 518 deb_info("%s: failed=%d\n", __func__, ret); 519 return ret; 520} 521 522static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap) 523{ 524 int ret; 525 struct rtl28xxu_priv *priv = adap->dev->priv; 526 struct dvb_frontend *fe; 527 528 deb_info("%s:\n", __func__); 529 530 switch (priv->tuner) { 531 case TUNER_RTL2832_FC2580: 532 /* TODO: */ 533 fe = NULL; 534 break; 535 default: 536 fe = NULL; 537 err("unknown tuner=%d", priv->tuner); 538 } 539 540 if (fe == NULL) { 541 ret = -ENODEV; 542 goto err; 543 } 544 545 return 0; 546err: 547 deb_info("%s: failed=%d\n", __func__, ret); 548 return ret; 549} 550 551static int rtl28xxu_streaming_ctrl(struct dvb_usb_adapter *adap , int onoff) 552{ 553 int ret; 554 u8 buf[2], gpio; 555 556 deb_info("%s: onoff=%d\n", __func__, onoff); 557 558 ret = rtl2831_rd_reg(adap->dev, SYS_GPIO_OUT_VAL, &gpio); 559 if (ret) 560 goto err; 561 562 if (onoff) { 563 buf[0] = 0x00; 564 buf[1] = 0x00; 565 gpio |= 0x04; /* LED on */ 566 } else { 567 buf[0] = 0x10; /* stall EPA */ 568 buf[1] = 0x02; /* reset EPA */ 569 gpio &= (~0x04); /* LED off */ 570 } 571 572 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_VAL, gpio); 573 if (ret) 574 goto err; 575 576 ret = rtl2831_wr_regs(adap->dev, USB_EPA_CTL, buf, 2); 577 if (ret) 578 goto err; 579 580 return ret; 581err: 582 deb_info("%s: failed=%d\n", __func__, ret); 583 return ret; 584} 585 586static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff) 587{ 588 int ret; 589 u8 gpio, sys0; 590 591 deb_info("%s: onoff=%d\n", __func__, onoff); 592 593 /* demod adc */ 594 ret = rtl2831_rd_reg(d, SYS_SYS0, &sys0); 595 if (ret) 596 goto err; 597 598 /* tuner power, read GPIOs */ 599 ret = rtl2831_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio); 600 if (ret) 601 goto err; 602 603 deb_info("%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio); 604 605 if (onoff) { 606 gpio |= 0x01; /* GPIO0 = 1 */ 607 gpio &= (~0x10); /* GPIO4 = 0 */ 608 sys0 = sys0 & 0x0f; 609 sys0 |= 0xe0; 610 } else { 611 gpio &= (~0x01); /* GPIO0 = 0 */ 612 gpio |= 0x10; /* GPIO4 = 1 */ 613 sys0 = sys0 & (~0xc0); 614 } 615 616 deb_info("%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio); 617 618 /* demod adc */ 619 ret = rtl2831_wr_reg(d, SYS_SYS0, sys0); 620 if (ret) 621 goto err; 622 623 /* tuner power, write GPIOs */ 624 ret = rtl2831_wr_reg(d, SYS_GPIO_OUT_VAL, gpio); 625 if (ret) 626 goto err; 627 628 return ret; 629err: 630 deb_info("%s: failed=%d\n", __func__, ret); 631 return ret; 632} 633 634static int rtl2831u_rc_query(struct dvb_usb_device *d) 635{ 636 int ret, i; 637 struct rtl28xxu_priv *priv = d->priv; 638 u8 buf[5]; 639 u32 rc_code; 640 struct rtl28xxu_reg_val rc_nec_tab[] = { 641 { 0x3033, 0x80 }, 642 { 0x3020, 0x43 }, 643 { 0x3021, 0x16 }, 644 { 0x3022, 0x16 }, 645 { 0x3023, 0x5a }, 646 { 0x3024, 0x2d }, 647 { 0x3025, 0x16 }, 648 { 0x3026, 0x01 }, 649 { 0x3028, 0xb0 }, 650 { 0x3029, 0x04 }, 651 { 0x302c, 0x88 }, 652 { 0x302e, 0x13 }, 653 { 0x3030, 0xdf }, 654 { 0x3031, 0x05 }, 655 }; 656 657 /* init remote controller */ 658 if (!priv->rc_active) { 659 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) { 660 ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg, 661 rc_nec_tab[i].val); 662 if (ret) 663 goto err; 664 } 665 priv->rc_active = true; 666 } 667 668 ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5); 669 if (ret) 670 goto err; 671 672 if (buf[4] & 0x01) { 673 if (buf[2] == (u8) ~buf[3]) { 674 if (buf[0] == (u8) ~buf[1]) { 675 /* NEC standard (16 bit) */ 676 rc_code = buf[0] << 8 | buf[2]; 677 } else { 678 /* NEC extended (24 bit) */ 679 rc_code = buf[0] << 16 | 680 buf[1] << 8 | buf[2]; 681 } 682 } else { 683 /* NEC full (32 bit) */ 684 rc_code = buf[0] << 24 | buf[1] << 16 | 685 buf[2] << 8 | buf[3]; 686 } 687 688 rc_keydown(d->rc_dev, rc_code, 0); 689 690 ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1); 691 if (ret) 692 goto err; 693 694 /* repeated intentionally to avoid extra keypress */ 695 ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1); 696 if (ret) 697 goto err; 698 } 699 700 return ret; 701err: 702 deb_info("%s: failed=%d\n", __func__, ret); 703 return ret; 704} 705 706static int rtl2832u_rc_query(struct dvb_usb_device *d) 707{ 708 int ret, i; 709 struct rtl28xxu_priv *priv = d->priv; 710 u8 buf[128]; 711 int len; 712 struct rtl28xxu_reg_val rc_nec_tab[] = { 713 { IR_RX_CTRL, 0x20 }, 714 { IR_RX_BUF_CTRL, 0x80 }, 715 { IR_RX_IF, 0xff }, 716 { IR_RX_IE, 0xff }, 717 { IR_MAX_DURATION0, 0xd0 }, 718 { IR_MAX_DURATION1, 0x07 }, 719 { IR_IDLE_LEN0, 0xc0 }, 720 { IR_IDLE_LEN1, 0x00 }, 721 { IR_GLITCH_LEN, 0x03 }, 722 { IR_RX_CLK, 0x09 }, 723 { IR_RX_CFG, 0x1c }, 724 { IR_MAX_H_TOL_LEN, 0x1e }, 725 { IR_MAX_L_TOL_LEN, 0x1e }, 726 { IR_RX_CTRL, 0x80 }, 727 }; 728 729 /* init remote controller */ 730 if (!priv->rc_active) { 731 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) { 732 ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg, 733 rc_nec_tab[i].val); 734 if (ret) 735 goto err; 736 } 737 priv->rc_active = true; 738 } 739 740 ret = rtl2831_rd_reg(d, IR_RX_IF, &buf[0]); 741 if (ret) 742 goto err; 743 744 if (buf[0] != 0x83) 745 goto exit; 746 747 ret = rtl2831_rd_reg(d, IR_RX_BC, &buf[0]); 748 if (ret) 749 goto err; 750 751 len = buf[0]; 752 ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len); 753 754 /* TODO: pass raw IR to Kernel IR decoder */ 755 756 ret = rtl2831_wr_reg(d, IR_RX_IF, 0x03); 757 ret = rtl2831_wr_reg(d, IR_RX_BUF_CTRL, 0x80); 758 ret = rtl2831_wr_reg(d, IR_RX_CTRL, 0x80); 759 760exit: 761 return ret; 762err: 763 deb_info("%s: failed=%d\n", __func__, ret); 764 return ret; 765} 766 767enum rtl28xxu_usb_table_entry { 768 RTL2831U_0BDA_2831, 769 RTL2831U_14AA_0160, 770 RTL2831U_14AA_0161, 771}; 772 773static struct usb_device_id rtl28xxu_table[] = { 774 /* RTL2831U */ 775 [RTL2831U_0BDA_2831] = { 776 USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U)}, 777 [RTL2831U_14AA_0160] = { 778 USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT)}, 779 [RTL2831U_14AA_0161] = { 780 USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2)}, 781 782 /* RTL2832U */ 783 {} /* terminating entry */ 784}; 785 786MODULE_DEVICE_TABLE(usb, rtl28xxu_table); 787 788static struct dvb_usb_device_properties rtl28xxu_properties[] = { 789 { 790 .caps = DVB_USB_IS_AN_I2C_ADAPTER, 791 792 .usb_ctrl = DEVICE_SPECIFIC, 793 .no_reconnect = 1, 794 795 .size_of_priv = sizeof(struct rtl28xxu_priv), 796 797 .num_adapters = 1, 798 .adapter = { 799 { 800 .num_frontends = 1, 801 .fe = { 802 { 803 .frontend_attach = rtl2831u_frontend_attach, 804 .tuner_attach = rtl2831u_tuner_attach, 805 .streaming_ctrl = rtl28xxu_streaming_ctrl, 806 .stream = { 807 .type = USB_BULK, 808 .count = 6, 809 .endpoint = 0x81, 810 .u = { 811 .bulk = { 812 .buffersize = 8*512, 813 } 814 } 815 } 816 } 817 } 818 } 819 }, 820 821 .power_ctrl = rtl28xxu_power_ctrl, 822 823 .rc.core = { 824 .protocol = RC_TYPE_NEC, 825 .module_name = "rtl28xxu", 826 .rc_query = rtl2831u_rc_query, 827 .rc_interval = 400, 828 .allowed_protos = RC_TYPE_NEC, 829 .rc_codes = RC_MAP_EMPTY, 830 }, 831 832 .i2c_algo = &rtl28xxu_i2c_algo, 833 834 .num_device_descs = 2, 835 .devices = { 836 { 837 .name = "Realtek RTL2831U reference design", 838 .warm_ids = { 839 &rtl28xxu_table[RTL2831U_0BDA_2831], 840 }, 841 }, 842 { 843 .name = "Freecom USB2.0 DVB-T", 844 .warm_ids = { 845 &rtl28xxu_table[RTL2831U_14AA_0160], 846 &rtl28xxu_table[RTL2831U_14AA_0161], 847 }, 848 }, 849 } 850 }, 851 { 852 .caps = DVB_USB_IS_AN_I2C_ADAPTER, 853 854 .usb_ctrl = DEVICE_SPECIFIC, 855 .no_reconnect = 1, 856 857 .size_of_priv = sizeof(struct rtl28xxu_priv), 858 859 .num_adapters = 1, 860 .adapter = { 861 { 862 .num_frontends = 1, 863 .fe = { 864 { 865 .frontend_attach = rtl2832u_frontend_attach, 866 .tuner_attach = rtl2832u_tuner_attach, 867 .streaming_ctrl = rtl28xxu_streaming_ctrl, 868 .stream = { 869 .type = USB_BULK, 870 .count = 6, 871 .endpoint = 0x81, 872 .u = { 873 .bulk = { 874 .buffersize = 8*512, 875 } 876 } 877 } 878 } 879 } 880 } 881 }, 882 883 .power_ctrl = rtl28xxu_power_ctrl, 884 885 .rc.core = { 886 .protocol = RC_TYPE_NEC, 887 .module_name = "rtl28xxu", 888 .rc_query = rtl2832u_rc_query, 889 .rc_interval = 400, 890 .allowed_protos = RC_TYPE_NEC, 891 .rc_codes = RC_MAP_EMPTY, 892 }, 893 894 .i2c_algo = &rtl28xxu_i2c_algo, 895 896 .num_device_descs = 0, /* disabled as no support for RTL2832 */ 897 .devices = { 898 { 899 .name = "Realtek RTL2832U reference design", 900 }, 901 } 902 }, 903 904}; 905 906static int rtl28xxu_probe(struct usb_interface *intf, 907 const struct usb_device_id *id) 908{ 909 int ret, i; 910 int properties_count = ARRAY_SIZE(rtl28xxu_properties); 911 struct dvb_usb_device *d; 912 913 deb_info("%s: interface=%d\n", __func__, 914 intf->cur_altsetting->desc.bInterfaceNumber); 915 916 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) 917 return 0; 918 919 for (i = 0; i < properties_count; i++) { 920 ret = dvb_usb_device_init(intf, &rtl28xxu_properties[i], 921 THIS_MODULE, &d, adapter_nr); 922 if (ret == 0 || ret != -ENODEV) 923 break; 924 } 925 926 if (ret) 927 goto err; 928 929 /* init USB endpoints */ 930 ret = rtl2831_wr_reg(d, USB_SYSCTL_0, 0x09); 931 if (ret) 932 goto err; 933 934 ret = rtl2831_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4); 935 if (ret) 936 goto err; 937 938 ret = rtl2831_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4); 939 if (ret) 940 goto err; 941 942 return ret; 943err: 944 deb_info("%s: failed=%d\n", __func__, ret); 945 return ret; 946} 947 948static struct usb_driver rtl28xxu_driver = { 949 .name = "dvb_usb_rtl28xxu", 950 .probe = rtl28xxu_probe, 951 .disconnect = dvb_usb_device_exit, 952 .id_table = rtl28xxu_table, 953}; 954 955/* module stuff */ 956static int __init rtl28xxu_module_init(void) 957{ 958 int ret; 959 960 deb_info("%s:\n", __func__); 961 962 ret = usb_register(&rtl28xxu_driver); 963 if (ret) 964 err("usb_register failed=%d", ret); 965 966 return ret; 967} 968 969static void __exit rtl28xxu_module_exit(void) 970{ 971 deb_info("%s:\n", __func__); 972 973 /* deregister this driver from the USB subsystem */ 974 usb_deregister(&rtl28xxu_driver); 975} 976 977module_init(rtl28xxu_module_init); 978module_exit(rtl28xxu_module_exit); 979 980MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver"); 981MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 982MODULE_LICENSE("GPL"); 983