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 * Copyright (C) 2012 Thomas Mair <thomas.mair86@googlemail.com> 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 along 19 * with this program; if not, write to the Free Software Foundation, Inc., 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 */ 22 23 #include "rtl28xxu.h" 24 25 #include "rtl2830.h" 26 #include "rtl2832.h" 27 #include "rtl2832_sdr.h" 28 29 #include "qt1010.h" 30 #include "mt2060.h" 31 #include "mxl5005s.h" 32 #include "fc0012.h" 33 #include "fc0013.h" 34 #include "e4000.h" 35 #include "fc2580.h" 36 #include "tua9001.h" 37 #include "r820t.h" 38 39 static int rtl28xxu_disable_rc; 40 module_param_named(disable_rc, rtl28xxu_disable_rc, int, 0644); 41 MODULE_PARM_DESC(disable_rc, "disable RTL2832U remote controller"); 42 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 43 44 static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req) 45 { 46 int ret; 47 unsigned int pipe; 48 u8 requesttype; 49 u8 *buf; 50 51 buf = kmalloc(req->size, GFP_KERNEL); 52 if (!buf) { 53 ret = -ENOMEM; 54 goto err; 55 } 56 57 if (req->index & CMD_WR_FLAG) { 58 /* write */ 59 memcpy(buf, req->data, req->size); 60 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT); 61 pipe = usb_sndctrlpipe(d->udev, 0); 62 } else { 63 /* read */ 64 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN); 65 pipe = usb_rcvctrlpipe(d->udev, 0); 66 } 67 68 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value, 69 req->index, buf, req->size, 1000); 70 71 dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value, 72 req->index, buf, req->size); 73 74 if (ret > 0) 75 ret = 0; 76 77 /* read request, copy returned data to return buf */ 78 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN)) 79 memcpy(req->data, buf, req->size); 80 81 kfree(buf); 82 83 if (ret) 84 goto err; 85 86 return ret; 87 err: 88 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 89 return ret; 90 } 91 92 static int rtl28xx_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len) 93 { 94 struct rtl28xxu_req req; 95 96 if (reg < 0x3000) 97 req.index = CMD_USB_WR; 98 else if (reg < 0x4000) 99 req.index = CMD_SYS_WR; 100 else 101 req.index = CMD_IR_WR; 102 103 req.value = reg; 104 req.size = len; 105 req.data = val; 106 107 return rtl28xxu_ctrl_msg(d, &req); 108 } 109 110 static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len) 111 { 112 struct rtl28xxu_req req; 113 114 if (reg < 0x3000) 115 req.index = CMD_USB_RD; 116 else if (reg < 0x4000) 117 req.index = CMD_SYS_RD; 118 else 119 req.index = CMD_IR_RD; 120 121 req.value = reg; 122 req.size = len; 123 req.data = val; 124 125 return rtl28xxu_ctrl_msg(d, &req); 126 } 127 128 static int rtl28xx_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val) 129 { 130 return rtl28xx_wr_regs(d, reg, &val, 1); 131 } 132 133 static int rtl28xx_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val) 134 { 135 return rtl2831_rd_regs(d, reg, val, 1); 136 } 137 138 static int rtl28xx_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val, 139 u8 mask) 140 { 141 int ret; 142 u8 tmp; 143 144 /* no need for read if whole reg is written */ 145 if (mask != 0xff) { 146 ret = rtl28xx_rd_reg(d, reg, &tmp); 147 if (ret) 148 return ret; 149 150 val &= mask; 151 tmp &= ~mask; 152 val |= tmp; 153 } 154 155 return rtl28xx_wr_reg(d, reg, val); 156 } 157 158 /* I2C */ 159 static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 160 int num) 161 { 162 int ret; 163 struct dvb_usb_device *d = i2c_get_adapdata(adap); 164 struct rtl28xxu_priv *priv = d->priv; 165 struct rtl28xxu_req req; 166 167 /* 168 * It is not known which are real I2C bus xfer limits, but testing 169 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes. 170 * TODO: find out RTL2832U lens 171 */ 172 173 /* 174 * I2C adapter logic looks rather complicated due to fact it handles 175 * three different access methods. Those methods are; 176 * 1) integrated demod access 177 * 2) old I2C access 178 * 3) new I2C access 179 * 180 * Used method is selected in order 1, 2, 3. Method 3 can handle all 181 * requests but there is two reasons why not use it always; 182 * 1) It is most expensive, usually two USB messages are needed 183 * 2) At least RTL2831U does not support it 184 * 185 * Method 3 is needed in case of I2C write+read (typical register read) 186 * where write is more than one byte. 187 */ 188 189 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 190 return -EAGAIN; 191 192 if (num == 2 && !(msg[0].flags & I2C_M_RD) && 193 (msg[1].flags & I2C_M_RD)) { 194 if (msg[0].len > 24 || msg[1].len > 24) { 195 /* TODO: check msg[0].len max */ 196 ret = -EOPNOTSUPP; 197 goto err_mutex_unlock; 198 } else if (msg[0].addr == 0x10) { 199 /* method 1 - integrated demod */ 200 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1); 201 req.index = CMD_DEMOD_RD | priv->page; 202 req.size = msg[1].len; 203 req.data = &msg[1].buf[0]; 204 ret = rtl28xxu_ctrl_msg(d, &req); 205 } else if (msg[0].len < 2) { 206 /* method 2 - old I2C */ 207 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1); 208 req.index = CMD_I2C_RD; 209 req.size = msg[1].len; 210 req.data = &msg[1].buf[0]; 211 ret = rtl28xxu_ctrl_msg(d, &req); 212 } else { 213 /* method 3 - new I2C */ 214 req.value = (msg[0].addr << 1); 215 req.index = CMD_I2C_DA_WR; 216 req.size = msg[0].len; 217 req.data = msg[0].buf; 218 ret = rtl28xxu_ctrl_msg(d, &req); 219 if (ret) 220 goto err_mutex_unlock; 221 222 req.value = (msg[0].addr << 1); 223 req.index = CMD_I2C_DA_RD; 224 req.size = msg[1].len; 225 req.data = msg[1].buf; 226 ret = rtl28xxu_ctrl_msg(d, &req); 227 } 228 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) { 229 if (msg[0].len > 22) { 230 /* TODO: check msg[0].len max */ 231 ret = -EOPNOTSUPP; 232 goto err_mutex_unlock; 233 } else if (msg[0].addr == 0x10) { 234 /* method 1 - integrated demod */ 235 if (msg[0].buf[0] == 0x00) { 236 /* save demod page for later demod access */ 237 priv->page = msg[0].buf[1]; 238 ret = 0; 239 } else { 240 req.value = (msg[0].buf[0] << 8) | 241 (msg[0].addr << 1); 242 req.index = CMD_DEMOD_WR | priv->page; 243 req.size = msg[0].len-1; 244 req.data = &msg[0].buf[1]; 245 ret = rtl28xxu_ctrl_msg(d, &req); 246 } 247 } else if (msg[0].len < 23) { 248 /* method 2 - old I2C */ 249 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1); 250 req.index = CMD_I2C_WR; 251 req.size = msg[0].len-1; 252 req.data = &msg[0].buf[1]; 253 ret = rtl28xxu_ctrl_msg(d, &req); 254 } else { 255 /* method 3 - new I2C */ 256 req.value = (msg[0].addr << 1); 257 req.index = CMD_I2C_DA_WR; 258 req.size = msg[0].len; 259 req.data = msg[0].buf; 260 ret = rtl28xxu_ctrl_msg(d, &req); 261 } 262 } else { 263 ret = -EINVAL; 264 } 265 266 err_mutex_unlock: 267 mutex_unlock(&d->i2c_mutex); 268 269 return ret ? ret : num; 270 } 271 272 static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter) 273 { 274 return I2C_FUNC_I2C; 275 } 276 277 static struct i2c_algorithm rtl28xxu_i2c_algo = { 278 .master_xfer = rtl28xxu_i2c_xfer, 279 .functionality = rtl28xxu_i2c_func, 280 }; 281 282 static int rtl2831u_read_config(struct dvb_usb_device *d) 283 { 284 struct rtl28xxu_priv *priv = d_to_priv(d); 285 int ret; 286 u8 buf[1]; 287 /* open RTL2831U/RTL2830 I2C gate */ 288 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"}; 289 /* tuner probes */ 290 struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf}; 291 struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf}; 292 293 dev_dbg(&d->udev->dev, "%s:\n", __func__); 294 295 /* 296 * RTL2831U GPIOs 297 * ========================================================= 298 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?) 299 * GPIO2 | LED | 0 off | 1 on | 300 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060 301 */ 302 303 /* GPIO direction */ 304 ret = rtl28xx_wr_reg(d, SYS_GPIO_DIR, 0x0a); 305 if (ret) 306 goto err; 307 308 /* enable as output GPIO0, GPIO2, GPIO4 */ 309 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_EN, 0x15); 310 if (ret) 311 goto err; 312 313 /* 314 * Probe used tuner. We need to know used tuner before demod attach 315 * since there is some demod params needed to set according to tuner. 316 */ 317 318 /* demod needs some time to wake up */ 319 msleep(20); 320 321 priv->tuner_name = "NONE"; 322 323 /* open demod I2C gate */ 324 ret = rtl28xxu_ctrl_msg(d, &req_gate_open); 325 if (ret) 326 goto err; 327 328 /* check QT1010 ID(?) register; reg=0f val=2c */ 329 ret = rtl28xxu_ctrl_msg(d, &req_qt1010); 330 if (ret == 0 && buf[0] == 0x2c) { 331 priv->tuner = TUNER_RTL2830_QT1010; 332 priv->tuner_name = "QT1010"; 333 goto found; 334 } 335 336 /* open demod I2C gate */ 337 ret = rtl28xxu_ctrl_msg(d, &req_gate_open); 338 if (ret) 339 goto err; 340 341 /* check MT2060 ID register; reg=00 val=63 */ 342 ret = rtl28xxu_ctrl_msg(d, &req_mt2060); 343 if (ret == 0 && buf[0] == 0x63) { 344 priv->tuner = TUNER_RTL2830_MT2060; 345 priv->tuner_name = "MT2060"; 346 goto found; 347 } 348 349 /* assume MXL5005S */ 350 priv->tuner = TUNER_RTL2830_MXL5005S; 351 priv->tuner_name = "MXL5005S"; 352 goto found; 353 354 found: 355 dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name); 356 357 return 0; 358 err: 359 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 360 return ret; 361 } 362 363 static int rtl2832u_read_config(struct dvb_usb_device *d) 364 { 365 struct rtl28xxu_priv *priv = d_to_priv(d); 366 int ret; 367 u8 buf[2]; 368 /* open RTL2832U/RTL2832 I2C gate */ 369 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"}; 370 /* close RTL2832U/RTL2832 I2C gate */ 371 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"}; 372 /* tuner probes */ 373 struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf}; 374 struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf}; 375 struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf}; 376 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf}; 377 struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf}; 378 struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf}; 379 struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf}; 380 struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf}; 381 struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf}; 382 struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf}; 383 struct rtl28xxu_req req_r820t = {0x0034, CMD_I2C_RD, 1, buf}; 384 struct rtl28xxu_req req_r828d = {0x0074, CMD_I2C_RD, 1, buf}; 385 386 dev_dbg(&d->udev->dev, "%s:\n", __func__); 387 388 /* enable GPIO3 and GPIO6 as output */ 389 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40); 390 if (ret) 391 goto err; 392 393 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48); 394 if (ret) 395 goto err; 396 397 /* 398 * Probe used tuner. We need to know used tuner before demod attach 399 * since there is some demod params needed to set according to tuner. 400 */ 401 402 /* open demod I2C gate */ 403 ret = rtl28xxu_ctrl_msg(d, &req_gate_open); 404 if (ret) 405 goto err; 406 407 priv->tuner_name = "NONE"; 408 409 /* check FC0012 ID register; reg=00 val=a1 */ 410 ret = rtl28xxu_ctrl_msg(d, &req_fc0012); 411 if (ret == 0 && buf[0] == 0xa1) { 412 priv->tuner = TUNER_RTL2832_FC0012; 413 priv->tuner_name = "FC0012"; 414 goto found; 415 } 416 417 /* check FC0013 ID register; reg=00 val=a3 */ 418 ret = rtl28xxu_ctrl_msg(d, &req_fc0013); 419 if (ret == 0 && buf[0] == 0xa3) { 420 priv->tuner = TUNER_RTL2832_FC0013; 421 priv->tuner_name = "FC0013"; 422 goto found; 423 } 424 425 /* check MT2266 ID register; reg=00 val=85 */ 426 ret = rtl28xxu_ctrl_msg(d, &req_mt2266); 427 if (ret == 0 && buf[0] == 0x85) { 428 priv->tuner = TUNER_RTL2832_MT2266; 429 priv->tuner_name = "MT2266"; 430 goto found; 431 } 432 433 /* check FC2580 ID register; reg=01 val=56 */ 434 ret = rtl28xxu_ctrl_msg(d, &req_fc2580); 435 if (ret == 0 && buf[0] == 0x56) { 436 priv->tuner = TUNER_RTL2832_FC2580; 437 priv->tuner_name = "FC2580"; 438 goto found; 439 } 440 441 /* check MT2063 ID register; reg=00 val=9e || 9c */ 442 ret = rtl28xxu_ctrl_msg(d, &req_mt2063); 443 if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) { 444 priv->tuner = TUNER_RTL2832_MT2063; 445 priv->tuner_name = "MT2063"; 446 goto found; 447 } 448 449 /* check MAX3543 ID register; reg=00 val=38 */ 450 ret = rtl28xxu_ctrl_msg(d, &req_max3543); 451 if (ret == 0 && buf[0] == 0x38) { 452 priv->tuner = TUNER_RTL2832_MAX3543; 453 priv->tuner_name = "MAX3543"; 454 goto found; 455 } 456 457 /* check TUA9001 ID register; reg=7e val=2328 */ 458 ret = rtl28xxu_ctrl_msg(d, &req_tua9001); 459 if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) { 460 priv->tuner = TUNER_RTL2832_TUA9001; 461 priv->tuner_name = "TUA9001"; 462 goto found; 463 } 464 465 /* check MXL5007R ID register; reg=d9 val=14 */ 466 ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t); 467 if (ret == 0 && buf[0] == 0x14) { 468 priv->tuner = TUNER_RTL2832_MXL5007T; 469 priv->tuner_name = "MXL5007T"; 470 goto found; 471 } 472 473 /* check E4000 ID register; reg=02 val=40 */ 474 ret = rtl28xxu_ctrl_msg(d, &req_e4000); 475 if (ret == 0 && buf[0] == 0x40) { 476 priv->tuner = TUNER_RTL2832_E4000; 477 priv->tuner_name = "E4000"; 478 goto found; 479 } 480 481 /* check TDA18272 ID register; reg=00 val=c760 */ 482 ret = rtl28xxu_ctrl_msg(d, &req_tda18272); 483 if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) { 484 priv->tuner = TUNER_RTL2832_TDA18272; 485 priv->tuner_name = "TDA18272"; 486 goto found; 487 } 488 489 /* check R820T ID register; reg=00 val=69 */ 490 ret = rtl28xxu_ctrl_msg(d, &req_r820t); 491 if (ret == 0 && buf[0] == 0x69) { 492 priv->tuner = TUNER_RTL2832_R820T; 493 priv->tuner_name = "R820T"; 494 goto found; 495 } 496 497 /* check R828D ID register; reg=00 val=69 */ 498 ret = rtl28xxu_ctrl_msg(d, &req_r828d); 499 if (ret == 0 && buf[0] == 0x69) { 500 priv->tuner = TUNER_RTL2832_R828D; 501 priv->tuner_name = "R828D"; 502 goto found; 503 } 504 505 506 found: 507 dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name); 508 509 /* close demod I2C gate */ 510 ret = rtl28xxu_ctrl_msg(d, &req_gate_close); 511 if (ret < 0) 512 goto err; 513 514 return 0; 515 err: 516 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 517 return ret; 518 } 519 520 static const struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = { 521 .i2c_addr = 0x10, /* 0x20 */ 522 .xtal = 28800000, 523 .ts_mode = 0, 524 .spec_inv = 1, 525 .vtop = 0x20, 526 .krf = 0x04, 527 .agc_targ_val = 0x2d, 528 529 }; 530 531 static const struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = { 532 .i2c_addr = 0x10, /* 0x20 */ 533 .xtal = 28800000, 534 .ts_mode = 0, 535 .spec_inv = 1, 536 .vtop = 0x20, 537 .krf = 0x04, 538 .agc_targ_val = 0x2d, 539 }; 540 541 static const struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = { 542 .i2c_addr = 0x10, /* 0x20 */ 543 .xtal = 28800000, 544 .ts_mode = 0, 545 .spec_inv = 0, 546 .vtop = 0x3f, 547 .krf = 0x04, 548 .agc_targ_val = 0x3e, 549 }; 550 551 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap) 552 { 553 struct dvb_usb_device *d = adap_to_d(adap); 554 struct rtl28xxu_priv *priv = d_to_priv(d); 555 const struct rtl2830_config *rtl2830_config; 556 int ret; 557 558 dev_dbg(&d->udev->dev, "%s:\n", __func__); 559 560 switch (priv->tuner) { 561 case TUNER_RTL2830_QT1010: 562 rtl2830_config = &rtl28xxu_rtl2830_qt1010_config; 563 break; 564 case TUNER_RTL2830_MT2060: 565 rtl2830_config = &rtl28xxu_rtl2830_mt2060_config; 566 break; 567 case TUNER_RTL2830_MXL5005S: 568 rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config; 569 break; 570 default: 571 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n", 572 KBUILD_MODNAME, priv->tuner_name); 573 ret = -ENODEV; 574 goto err; 575 } 576 577 /* attach demodulator */ 578 adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config, &d->i2c_adap); 579 if (!adap->fe[0]) { 580 ret = -ENODEV; 581 goto err; 582 } 583 584 return 0; 585 err: 586 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 587 return ret; 588 } 589 590 static const struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = { 591 .i2c_addr = 0x10, /* 0x20 */ 592 .xtal = 28800000, 593 .tuner = TUNER_RTL2832_FC0012 594 }; 595 596 static const struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = { 597 .i2c_addr = 0x10, /* 0x20 */ 598 .xtal = 28800000, 599 .tuner = TUNER_RTL2832_FC0013 600 }; 601 602 static const struct rtl2832_config rtl28xxu_rtl2832_tua9001_config = { 603 .i2c_addr = 0x10, /* 0x20 */ 604 .xtal = 28800000, 605 .tuner = TUNER_RTL2832_TUA9001, 606 }; 607 608 static const struct rtl2832_config rtl28xxu_rtl2832_e4000_config = { 609 .i2c_addr = 0x10, /* 0x20 */ 610 .xtal = 28800000, 611 .tuner = TUNER_RTL2832_E4000, 612 }; 613 614 static const struct rtl2832_config rtl28xxu_rtl2832_r820t_config = { 615 .i2c_addr = 0x10, 616 .xtal = 28800000, 617 .tuner = TUNER_RTL2832_R820T, 618 }; 619 620 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d, 621 int cmd, int arg) 622 { 623 int ret; 624 u8 val; 625 626 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg); 627 628 switch (cmd) { 629 case FC_FE_CALLBACK_VHF_ENABLE: 630 /* set output values */ 631 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val); 632 if (ret) 633 goto err; 634 635 if (arg) 636 val &= 0xbf; /* set GPIO6 low */ 637 else 638 val |= 0x40; /* set GPIO6 high */ 639 640 641 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val); 642 if (ret) 643 goto err; 644 break; 645 default: 646 ret = -EINVAL; 647 goto err; 648 } 649 return 0; 650 err: 651 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 652 return ret; 653 } 654 655 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d, 656 int cmd, int arg) 657 { 658 int ret; 659 u8 val; 660 661 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg); 662 663 /* 664 * CEN always enabled by hardware wiring 665 * RESETN GPIO4 666 * RXEN GPIO1 667 */ 668 669 switch (cmd) { 670 case TUA9001_CMD_RESETN: 671 if (arg) 672 val = (1 << 4); 673 else 674 val = (0 << 4); 675 676 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10); 677 if (ret) 678 goto err; 679 break; 680 case TUA9001_CMD_RXEN: 681 if (arg) 682 val = (1 << 1); 683 else 684 val = (0 << 1); 685 686 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02); 687 if (ret) 688 goto err; 689 break; 690 } 691 692 return 0; 693 err: 694 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 695 return ret; 696 } 697 698 static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg) 699 { 700 struct rtl28xxu_priv *priv = d->priv; 701 702 switch (priv->tuner) { 703 case TUNER_RTL2832_FC0012: 704 return rtl2832u_fc0012_tuner_callback(d, cmd, arg); 705 case TUNER_RTL2832_TUA9001: 706 return rtl2832u_tua9001_tuner_callback(d, cmd, arg); 707 default: 708 break; 709 } 710 711 return 0; 712 } 713 714 static int rtl2832u_frontend_callback(void *adapter_priv, int component, 715 int cmd, int arg) 716 { 717 struct i2c_adapter *adap = adapter_priv; 718 struct dvb_usb_device *d = i2c_get_adapdata(adap); 719 720 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n", 721 __func__, component, cmd, arg); 722 723 switch (component) { 724 case DVB_FRONTEND_COMPONENT_TUNER: 725 return rtl2832u_tuner_callback(d, cmd, arg); 726 default: 727 break; 728 } 729 730 return 0; 731 } 732 733 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap) 734 { 735 int ret; 736 struct dvb_usb_device *d = adap_to_d(adap); 737 struct rtl28xxu_priv *priv = d_to_priv(d); 738 const struct rtl2832_config *rtl2832_config; 739 740 dev_dbg(&d->udev->dev, "%s:\n", __func__); 741 742 switch (priv->tuner) { 743 case TUNER_RTL2832_FC0012: 744 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config; 745 break; 746 case TUNER_RTL2832_FC0013: 747 rtl2832_config = &rtl28xxu_rtl2832_fc0013_config; 748 break; 749 case TUNER_RTL2832_FC2580: 750 /* FIXME: do not abuse fc0012 settings */ 751 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config; 752 break; 753 case TUNER_RTL2832_TUA9001: 754 rtl2832_config = &rtl28xxu_rtl2832_tua9001_config; 755 break; 756 case TUNER_RTL2832_E4000: 757 rtl2832_config = &rtl28xxu_rtl2832_e4000_config; 758 break; 759 case TUNER_RTL2832_R820T: 760 case TUNER_RTL2832_R828D: 761 rtl2832_config = &rtl28xxu_rtl2832_r820t_config; 762 break; 763 default: 764 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n", 765 KBUILD_MODNAME, priv->tuner_name); 766 ret = -ENODEV; 767 goto err; 768 } 769 770 /* attach demodulator */ 771 adap->fe[0] = dvb_attach(rtl2832_attach, rtl2832_config, &d->i2c_adap); 772 if (!adap->fe[0]) { 773 ret = -ENODEV; 774 goto err; 775 } 776 777 /* RTL2832 I2C repeater */ 778 priv->demod_i2c_adapter = rtl2832_get_i2c_adapter(adap->fe[0]); 779 780 /* set fe callback */ 781 adap->fe[0]->callback = rtl2832u_frontend_callback; 782 783 return 0; 784 err: 785 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 786 return ret; 787 } 788 789 static struct qt1010_config rtl28xxu_qt1010_config = { 790 .i2c_address = 0x62, /* 0xc4 */ 791 }; 792 793 static struct mt2060_config rtl28xxu_mt2060_config = { 794 .i2c_address = 0x60, /* 0xc0 */ 795 .clock_out = 0, 796 }; 797 798 static struct mxl5005s_config rtl28xxu_mxl5005s_config = { 799 .i2c_address = 0x63, /* 0xc6 */ 800 .if_freq = IF_FREQ_4570000HZ, 801 .xtal_freq = CRYSTAL_FREQ_16000000HZ, 802 .agc_mode = MXL_SINGLE_AGC, 803 .tracking_filter = MXL_TF_C_H, 804 .rssi_enable = MXL_RSSI_ENABLE, 805 .cap_select = MXL_CAP_SEL_ENABLE, 806 .div_out = MXL_DIV_OUT_4, 807 .clock_out = MXL_CLOCK_OUT_DISABLE, 808 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM, 809 .top = MXL5005S_TOP_25P2, 810 .mod_mode = MXL_DIGITAL_MODE, 811 .if_mode = MXL_ZERO_IF, 812 .AgcMasterByte = 0x00, 813 }; 814 815 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap) 816 { 817 int ret; 818 struct dvb_usb_device *d = adap_to_d(adap); 819 struct rtl28xxu_priv *priv = d_to_priv(d); 820 struct i2c_adapter *rtl2830_tuner_i2c; 821 struct dvb_frontend *fe; 822 823 dev_dbg(&d->udev->dev, "%s:\n", __func__); 824 825 /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */ 826 rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]); 827 828 switch (priv->tuner) { 829 case TUNER_RTL2830_QT1010: 830 fe = dvb_attach(qt1010_attach, adap->fe[0], 831 rtl2830_tuner_i2c, &rtl28xxu_qt1010_config); 832 break; 833 case TUNER_RTL2830_MT2060: 834 fe = dvb_attach(mt2060_attach, adap->fe[0], 835 rtl2830_tuner_i2c, &rtl28xxu_mt2060_config, 836 1220); 837 break; 838 case TUNER_RTL2830_MXL5005S: 839 fe = dvb_attach(mxl5005s_attach, adap->fe[0], 840 rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config); 841 break; 842 default: 843 fe = NULL; 844 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME, 845 priv->tuner); 846 } 847 848 if (fe == NULL) { 849 ret = -ENODEV; 850 goto err; 851 } 852 853 return 0; 854 err: 855 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 856 return ret; 857 } 858 859 static const struct fc2580_config rtl2832u_fc2580_config = { 860 .i2c_addr = 0x56, 861 .clock = 16384000, 862 }; 863 864 static struct tua9001_config rtl2832u_tua9001_config = { 865 .i2c_addr = 0x60, 866 }; 867 868 static const struct fc0012_config rtl2832u_fc0012_config = { 869 .i2c_address = 0x63, /* 0xc6 >> 1 */ 870 .xtal_freq = FC_XTAL_28_8_MHZ, 871 }; 872 873 static const struct r820t_config rtl2832u_r820t_config = { 874 .i2c_addr = 0x1a, 875 .xtal = 28800000, 876 .max_i2c_msg_len = 2, 877 .rafael_chip = CHIP_R820T, 878 }; 879 880 static const struct r820t_config rtl2832u_r828d_config = { 881 .i2c_addr = 0x3a, 882 .xtal = 16000000, 883 .max_i2c_msg_len = 2, 884 .rafael_chip = CHIP_R828D, 885 }; 886 887 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap) 888 { 889 int ret; 890 struct dvb_usb_device *d = adap_to_d(adap); 891 struct rtl28xxu_priv *priv = d_to_priv(d); 892 struct dvb_frontend *fe = NULL; 893 struct i2c_board_info info; 894 struct i2c_client *client; 895 896 dev_dbg(&d->udev->dev, "%s:\n", __func__); 897 898 memset(&info, 0, sizeof(struct i2c_board_info)); 899 900 switch (priv->tuner) { 901 case TUNER_RTL2832_FC0012: 902 fe = dvb_attach(fc0012_attach, adap->fe[0], 903 &d->i2c_adap, &rtl2832u_fc0012_config); 904 905 /* since fc0012 includs reading the signal strength delegate 906 * that to the tuner driver */ 907 adap->fe[0]->ops.read_signal_strength = 908 adap->fe[0]->ops.tuner_ops.get_rf_strength; 909 910 /* attach SDR */ 911 dvb_attach(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap, 912 &rtl28xxu_rtl2832_fc0012_config, NULL); 913 break; 914 case TUNER_RTL2832_FC0013: 915 fe = dvb_attach(fc0013_attach, adap->fe[0], 916 &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ); 917 918 /* fc0013 also supports signal strength reading */ 919 adap->fe[0]->ops.read_signal_strength = 920 adap->fe[0]->ops.tuner_ops.get_rf_strength; 921 922 /* attach SDR */ 923 dvb_attach(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap, 924 &rtl28xxu_rtl2832_fc0013_config, NULL); 925 break; 926 case TUNER_RTL2832_E4000: { 927 struct v4l2_subdev *sd; 928 struct i2c_adapter *i2c_adap_internal = 929 rtl2832_get_private_i2c_adapter(adap->fe[0]); 930 struct e4000_config e4000_config = { 931 .fe = adap->fe[0], 932 .clock = 28800000, 933 }; 934 935 strlcpy(info.type, "e4000", I2C_NAME_SIZE); 936 info.addr = 0x64; 937 info.platform_data = &e4000_config; 938 939 request_module(info.type); 940 client = i2c_new_device(priv->demod_i2c_adapter, &info); 941 if (client == NULL || client->dev.driver == NULL) 942 break; 943 944 if (!try_module_get(client->dev.driver->owner)) { 945 i2c_unregister_device(client); 946 break; 947 } 948 949 priv->client = client; 950 sd = i2c_get_clientdata(client); 951 i2c_set_adapdata(i2c_adap_internal, d); 952 953 /* attach SDR */ 954 dvb_attach(rtl2832_sdr_attach, adap->fe[0], 955 i2c_adap_internal, 956 &rtl28xxu_rtl2832_e4000_config, sd); 957 } 958 break; 959 case TUNER_RTL2832_FC2580: 960 fe = dvb_attach(fc2580_attach, adap->fe[0], &d->i2c_adap, 961 &rtl2832u_fc2580_config); 962 break; 963 case TUNER_RTL2832_TUA9001: 964 /* enable GPIO1 and GPIO4 as output */ 965 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12); 966 if (ret) 967 goto err; 968 969 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12); 970 if (ret) 971 goto err; 972 973 fe = dvb_attach(tua9001_attach, adap->fe[0], &d->i2c_adap, 974 &rtl2832u_tua9001_config); 975 break; 976 case TUNER_RTL2832_R820T: 977 fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap, 978 &rtl2832u_r820t_config); 979 980 /* Use tuner to get the signal strength */ 981 adap->fe[0]->ops.read_signal_strength = 982 adap->fe[0]->ops.tuner_ops.get_rf_strength; 983 984 /* attach SDR */ 985 dvb_attach(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap, 986 &rtl28xxu_rtl2832_r820t_config, NULL); 987 break; 988 case TUNER_RTL2832_R828D: 989 /* power off mn88472 demod on GPIO0 */ 990 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x01); 991 if (ret) 992 goto err; 993 994 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x01); 995 if (ret) 996 goto err; 997 998 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x01, 0x01); 999 if (ret) 1000 goto err; 1001 1002 fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap, 1003 &rtl2832u_r828d_config); 1004 1005 /* Use tuner to get the signal strength */ 1006 adap->fe[0]->ops.read_signal_strength = 1007 adap->fe[0]->ops.tuner_ops.get_rf_strength; 1008 break; 1009 default: 1010 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME, 1011 priv->tuner); 1012 } 1013 1014 if (fe == NULL && priv->client == NULL) { 1015 ret = -ENODEV; 1016 goto err; 1017 } 1018 1019 return 0; 1020 err: 1021 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1022 return ret; 1023 } 1024 1025 static int rtl28xxu_init(struct dvb_usb_device *d) 1026 { 1027 int ret; 1028 u8 val; 1029 1030 dev_dbg(&d->udev->dev, "%s:\n", __func__); 1031 1032 /* init USB endpoints */ 1033 ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val); 1034 if (ret) 1035 goto err; 1036 1037 /* enable DMA and Full Packet Mode*/ 1038 val |= 0x09; 1039 ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val); 1040 if (ret) 1041 goto err; 1042 1043 /* set EPA maximum packet size to 0x0200 */ 1044 ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4); 1045 if (ret) 1046 goto err; 1047 1048 /* change EPA FIFO length */ 1049 ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4); 1050 if (ret) 1051 goto err; 1052 1053 return ret; 1054 err: 1055 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1056 return ret; 1057 } 1058 1059 static void rtl28xxu_exit(struct dvb_usb_device *d) 1060 { 1061 struct rtl28xxu_priv *priv = d->priv; 1062 struct i2c_client *client = priv->client; 1063 1064 dev_dbg(&d->udev->dev, "%s:\n", __func__); 1065 1066 /* remove I2C tuner */ 1067 if (client) { 1068 module_put(client->dev.driver->owner); 1069 i2c_unregister_device(client); 1070 } 1071 1072 return; 1073 } 1074 1075 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff) 1076 { 1077 int ret; 1078 u8 gpio, sys0, epa_ctl[2]; 1079 1080 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff); 1081 1082 /* demod adc */ 1083 ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0); 1084 if (ret) 1085 goto err; 1086 1087 /* tuner power, read GPIOs */ 1088 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio); 1089 if (ret) 1090 goto err; 1091 1092 dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, 1093 sys0, gpio); 1094 1095 if (onoff) { 1096 gpio |= 0x01; /* GPIO0 = 1 */ 1097 gpio &= (~0x10); /* GPIO4 = 0 */ 1098 gpio |= 0x04; /* GPIO2 = 1, LED on */ 1099 sys0 = sys0 & 0x0f; 1100 sys0 |= 0xe0; 1101 epa_ctl[0] = 0x00; /* clear stall */ 1102 epa_ctl[1] = 0x00; /* clear reset */ 1103 } else { 1104 gpio &= (~0x01); /* GPIO0 = 0 */ 1105 gpio |= 0x10; /* GPIO4 = 1 */ 1106 gpio &= (~0x04); /* GPIO2 = 1, LED off */ 1107 sys0 = sys0 & (~0xc0); 1108 epa_ctl[0] = 0x10; /* set stall */ 1109 epa_ctl[1] = 0x02; /* set reset */ 1110 } 1111 1112 dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, 1113 sys0, gpio); 1114 1115 /* demod adc */ 1116 ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0); 1117 if (ret) 1118 goto err; 1119 1120 /* tuner power, write GPIOs */ 1121 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio); 1122 if (ret) 1123 goto err; 1124 1125 /* streaming EP: stall & reset */ 1126 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2); 1127 if (ret) 1128 goto err; 1129 1130 if (onoff) 1131 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81)); 1132 1133 return ret; 1134 err: 1135 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1136 return ret; 1137 } 1138 1139 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff) 1140 { 1141 int ret; 1142 1143 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff); 1144 1145 if (onoff) { 1146 /* GPIO3=1, GPIO4=0 */ 1147 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18); 1148 if (ret) 1149 goto err; 1150 1151 /* suspend? */ 1152 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10); 1153 if (ret) 1154 goto err; 1155 1156 /* enable PLL */ 1157 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80); 1158 if (ret) 1159 goto err; 1160 1161 /* disable reset */ 1162 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20); 1163 if (ret) 1164 goto err; 1165 1166 mdelay(5); 1167 1168 /* enable ADC */ 1169 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x48, 0x48); 1170 if (ret) 1171 goto err; 1172 1173 /* streaming EP: clear stall & reset */ 1174 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2); 1175 if (ret) 1176 goto err; 1177 1178 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81)); 1179 if (ret) 1180 goto err; 1181 } else { 1182 /* GPIO4=1 */ 1183 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10); 1184 if (ret) 1185 goto err; 1186 1187 /* disable ADC */ 1188 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x48); 1189 if (ret) 1190 goto err; 1191 1192 /* disable PLL */ 1193 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80); 1194 if (ret) 1195 goto err; 1196 1197 /* streaming EP: set stall & reset */ 1198 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2); 1199 if (ret) 1200 goto err; 1201 } 1202 1203 return ret; 1204 err: 1205 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1206 return ret; 1207 } 1208 1209 #if IS_ENABLED(CONFIG_RC_CORE) 1210 static int rtl2831u_rc_query(struct dvb_usb_device *d) 1211 { 1212 int ret, i; 1213 struct rtl28xxu_priv *priv = d->priv; 1214 u8 buf[5]; 1215 u32 rc_code; 1216 struct rtl28xxu_reg_val rc_nec_tab[] = { 1217 { 0x3033, 0x80 }, 1218 { 0x3020, 0x43 }, 1219 { 0x3021, 0x16 }, 1220 { 0x3022, 0x16 }, 1221 { 0x3023, 0x5a }, 1222 { 0x3024, 0x2d }, 1223 { 0x3025, 0x16 }, 1224 { 0x3026, 0x01 }, 1225 { 0x3028, 0xb0 }, 1226 { 0x3029, 0x04 }, 1227 { 0x302c, 0x88 }, 1228 { 0x302e, 0x13 }, 1229 { 0x3030, 0xdf }, 1230 { 0x3031, 0x05 }, 1231 }; 1232 1233 /* init remote controller */ 1234 if (!priv->rc_active) { 1235 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) { 1236 ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg, 1237 rc_nec_tab[i].val); 1238 if (ret) 1239 goto err; 1240 } 1241 priv->rc_active = true; 1242 } 1243 1244 ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5); 1245 if (ret) 1246 goto err; 1247 1248 if (buf[4] & 0x01) { 1249 if (buf[2] == (u8) ~buf[3]) { 1250 if (buf[0] == (u8) ~buf[1]) { 1251 /* NEC standard (16 bit) */ 1252 rc_code = buf[0] << 8 | buf[2]; 1253 } else { 1254 /* NEC extended (24 bit) */ 1255 rc_code = buf[0] << 16 | 1256 buf[1] << 8 | buf[2]; 1257 } 1258 } else { 1259 /* NEC full (32 bit) */ 1260 rc_code = buf[0] << 24 | buf[1] << 16 | 1261 buf[2] << 8 | buf[3]; 1262 } 1263 1264 rc_keydown(d->rc_dev, rc_code, 0); 1265 1266 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1); 1267 if (ret) 1268 goto err; 1269 1270 /* repeated intentionally to avoid extra keypress */ 1271 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1); 1272 if (ret) 1273 goto err; 1274 } 1275 1276 return ret; 1277 err: 1278 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1279 return ret; 1280 } 1281 1282 static int rtl2831u_get_rc_config(struct dvb_usb_device *d, 1283 struct dvb_usb_rc *rc) 1284 { 1285 rc->map_name = RC_MAP_EMPTY; 1286 rc->allowed_protos = RC_BIT_NEC; 1287 rc->query = rtl2831u_rc_query; 1288 rc->interval = 400; 1289 1290 return 0; 1291 } 1292 1293 static int rtl2832u_rc_query(struct dvb_usb_device *d) 1294 { 1295 int ret, i, len; 1296 struct rtl28xxu_priv *priv = d->priv; 1297 struct ir_raw_event ev; 1298 u8 buf[128]; 1299 static const struct rtl28xxu_reg_val_mask refresh_tab[] = { 1300 {IR_RX_IF, 0x03, 0xff}, 1301 {IR_RX_BUF_CTRL, 0x80, 0xff}, 1302 {IR_RX_CTRL, 0x80, 0xff}, 1303 }; 1304 1305 /* init remote controller */ 1306 if (!priv->rc_active) { 1307 static const struct rtl28xxu_reg_val_mask init_tab[] = { 1308 {SYS_DEMOD_CTL1, 0x00, 0x04}, 1309 {SYS_DEMOD_CTL1, 0x00, 0x08}, 1310 {USB_CTRL, 0x20, 0x20}, 1311 {SYS_GPIO_DIR, 0x00, 0x08}, 1312 {SYS_GPIO_OUT_EN, 0x08, 0x08}, 1313 {SYS_GPIO_OUT_VAL, 0x08, 0x08}, 1314 {IR_MAX_DURATION0, 0xd0, 0xff}, 1315 {IR_MAX_DURATION1, 0x07, 0xff}, 1316 {IR_IDLE_LEN0, 0xc0, 0xff}, 1317 {IR_IDLE_LEN1, 0x00, 0xff}, 1318 {IR_GLITCH_LEN, 0x03, 0xff}, 1319 {IR_RX_CLK, 0x09, 0xff}, 1320 {IR_RX_CFG, 0x1c, 0xff}, 1321 {IR_MAX_H_TOL_LEN, 0x1e, 0xff}, 1322 {IR_MAX_L_TOL_LEN, 0x1e, 0xff}, 1323 {IR_RX_CTRL, 0x80, 0xff}, 1324 }; 1325 1326 for (i = 0; i < ARRAY_SIZE(init_tab); i++) { 1327 ret = rtl28xx_wr_reg_mask(d, init_tab[i].reg, 1328 init_tab[i].val, init_tab[i].mask); 1329 if (ret) 1330 goto err; 1331 } 1332 1333 priv->rc_active = true; 1334 } 1335 1336 ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]); 1337 if (ret) 1338 goto err; 1339 1340 if (buf[0] != 0x83) 1341 goto exit; 1342 1343 ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]); 1344 if (ret) 1345 goto err; 1346 1347 len = buf[0]; 1348 1349 /* read raw code from hw */ 1350 ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len); 1351 if (ret) 1352 goto err; 1353 1354 /* let hw receive new code */ 1355 for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) { 1356 ret = rtl28xx_wr_reg_mask(d, refresh_tab[i].reg, 1357 refresh_tab[i].val, refresh_tab[i].mask); 1358 if (ret) 1359 goto err; 1360 } 1361 1362 /* pass data to Kernel IR decoder */ 1363 init_ir_raw_event(&ev); 1364 1365 for (i = 0; i < len; i++) { 1366 ev.pulse = buf[i] >> 7; 1367 ev.duration = 50800 * (buf[i] & 0x7f); 1368 ir_raw_event_store_with_filter(d->rc_dev, &ev); 1369 } 1370 1371 /* 'flush' ir_raw_event_store_with_filter() */ 1372 ir_raw_event_set_idle(d->rc_dev, true); 1373 ir_raw_event_handle(d->rc_dev); 1374 exit: 1375 return ret; 1376 err: 1377 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1378 return ret; 1379 } 1380 1381 static int rtl2832u_get_rc_config(struct dvb_usb_device *d, 1382 struct dvb_usb_rc *rc) 1383 { 1384 /* disable IR interrupts in order to avoid SDR sample loss */ 1385 if (rtl28xxu_disable_rc) 1386 return rtl28xx_wr_reg(d, IR_RX_IE, 0x00); 1387 1388 /* load empty to enable rc */ 1389 if (!rc->map_name) 1390 rc->map_name = RC_MAP_EMPTY; 1391 rc->allowed_protos = RC_BIT_ALL; 1392 rc->driver_type = RC_DRIVER_IR_RAW; 1393 rc->query = rtl2832u_rc_query; 1394 rc->interval = 400; 1395 1396 return 0; 1397 } 1398 #else 1399 #define rtl2831u_get_rc_config NULL 1400 #define rtl2832u_get_rc_config NULL 1401 #endif 1402 1403 static const struct dvb_usb_device_properties rtl2831u_props = { 1404 .driver_name = KBUILD_MODNAME, 1405 .owner = THIS_MODULE, 1406 .adapter_nr = adapter_nr, 1407 .size_of_priv = sizeof(struct rtl28xxu_priv), 1408 1409 .power_ctrl = rtl2831u_power_ctrl, 1410 .i2c_algo = &rtl28xxu_i2c_algo, 1411 .read_config = rtl2831u_read_config, 1412 .frontend_attach = rtl2831u_frontend_attach, 1413 .tuner_attach = rtl2831u_tuner_attach, 1414 .init = rtl28xxu_init, 1415 .get_rc_config = rtl2831u_get_rc_config, 1416 1417 .num_adapters = 1, 1418 .adapter = { 1419 { 1420 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512), 1421 }, 1422 }, 1423 }; 1424 1425 static const struct dvb_usb_device_properties rtl2832u_props = { 1426 .driver_name = KBUILD_MODNAME, 1427 .owner = THIS_MODULE, 1428 .adapter_nr = adapter_nr, 1429 .size_of_priv = sizeof(struct rtl28xxu_priv), 1430 1431 .power_ctrl = rtl2832u_power_ctrl, 1432 .i2c_algo = &rtl28xxu_i2c_algo, 1433 .read_config = rtl2832u_read_config, 1434 .frontend_attach = rtl2832u_frontend_attach, 1435 .tuner_attach = rtl2832u_tuner_attach, 1436 .init = rtl28xxu_init, 1437 .exit = rtl28xxu_exit, 1438 .get_rc_config = rtl2832u_get_rc_config, 1439 1440 .num_adapters = 1, 1441 .adapter = { 1442 { 1443 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512), 1444 }, 1445 }, 1446 }; 1447 1448 static const struct usb_device_id rtl28xxu_id_table[] = { 1449 /* RTL2831U devices: */ 1450 { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U, 1451 &rtl2831u_props, "Realtek RTL2831U reference design", NULL) }, 1452 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT, 1453 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) }, 1454 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2, 1455 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) }, 1456 1457 /* RTL2832U devices: */ 1458 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832, 1459 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) }, 1460 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838, 1461 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) }, 1462 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1, 1463 &rtl2832u_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) }, 1464 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT, 1465 &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) }, 1466 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK, 1467 &rtl2832u_props, "TerraTec NOXON DAB Stick", NULL) }, 1468 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2, 1469 &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) }, 1470 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3, 1471 &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) }, 1472 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0, 1473 &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) }, 1474 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101, 1475 &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) }, 1476 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680, 1477 &rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) }, 1478 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID, 1479 &rtl2832u_props, "Leadtek Winfast DTV Dongle Mini D", NULL) }, 1480 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3, 1481 &rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) }, 1482 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102, 1483 &rtl2832u_props, "Dexatek DK mini DVB-T Dongle", NULL) }, 1484 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7, 1485 &rtl2832u_props, "TerraTec Cinergy T Stick+", NULL) }, 1486 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8, 1487 &rtl2832u_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) }, 1488 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393, 1489 &rtl2832u_props, "GIGABYTE U7300", NULL) }, 1490 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104, 1491 &rtl2832u_props, "MSI DIGIVOX Micro HD", NULL) }, 1492 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620, 1493 &rtl2832u_props, "Compro VideoMate U620F", NULL) }, 1494 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394, 1495 &rtl2832u_props, "MaxMedia HU394-T", NULL) }, 1496 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03, 1497 &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) }, 1498 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A, 1499 &rtl2832u_props, "Crypto ReDi PC 50 A", NULL) }, 1500 { DVB_USB_DEVICE(USB_VID_KYE, 0x707f, 1501 &rtl2832u_props, "Genius TVGo DVB-T03", NULL) }, 1502 1503 /* RTL2832P devices: */ 1504 { DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131, 1505 &rtl2832u_props, "Astrometa DVB-T2", NULL) }, 1506 { } 1507 }; 1508 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table); 1509 1510 static struct usb_driver rtl28xxu_usb_driver = { 1511 .name = KBUILD_MODNAME, 1512 .id_table = rtl28xxu_id_table, 1513 .probe = dvb_usbv2_probe, 1514 .disconnect = dvb_usbv2_disconnect, 1515 .suspend = dvb_usbv2_suspend, 1516 .resume = dvb_usbv2_resume, 1517 .reset_resume = dvb_usbv2_reset_resume, 1518 .no_dynamic_id = 1, 1519 .soft_unbind = 1, 1520 }; 1521 1522 module_usb_driver(rtl28xxu_usb_driver); 1523 1524 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver"); 1525 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1526 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>"); 1527 MODULE_LICENSE("GPL"); 1528