2 * Realtek RTL28xxU DVB USB driver
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>
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.
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.
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.
35 static int dvb_usb_rtl28xxu_debug;
36 module_param_named(debug, dvb_usb_rtl28xxu_debug, int, 0644);
37 MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
38 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
40 static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
47 buf = kmalloc(req->size, GFP_KERNEL);
53 if (req->index & CMD_WR_FLAG) {
55 memcpy(buf, req->data, req->size);
56 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
57 pipe = usb_sndctrlpipe(d->udev, 0);
60 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
61 pipe = usb_rcvctrlpipe(d->udev, 0);
64 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
65 req->index, buf, req->size, 1000);
69 deb_dump(0, requesttype, req->value, req->index, buf, req->size,
72 /* read request, copy returned data to return buf */
73 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
74 memcpy(req->data, buf, req->size);
83 deb_info("%s: failed=%d\n", __func__, ret);
87 static int rtl28xx_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
89 struct rtl28xxu_req req;
92 req.index = CMD_USB_WR;
93 else if (reg < 0x4000)
94 req.index = CMD_SYS_WR;
96 req.index = CMD_IR_WR;
102 return rtl28xxu_ctrl_msg(d, &req);
105 static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
107 struct rtl28xxu_req req;
110 req.index = CMD_USB_RD;
111 else if (reg < 0x4000)
112 req.index = CMD_SYS_RD;
114 req.index = CMD_IR_RD;
120 return rtl28xxu_ctrl_msg(d, &req);
123 static int rtl28xx_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
125 return rtl28xx_wr_regs(d, reg, &val, 1);
128 static int rtl28xx_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
130 return rtl2831_rd_regs(d, reg, val, 1);
134 static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
138 struct dvb_usb_device *d = i2c_get_adapdata(adap);
139 struct rtl28xxu_priv *priv = d->priv;
140 struct rtl28xxu_req req;
143 * It is not known which are real I2C bus xfer limits, but testing
144 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
145 * TODO: find out RTL2832U lens
149 * I2C adapter logic looks rather complicated due to fact it handles
150 * three different access methods. Those methods are;
151 * 1) integrated demod access
155 * Used method is selected in order 1, 2, 3. Method 3 can handle all
156 * requests but there is two reasons why not use it always;
157 * 1) It is most expensive, usually two USB messages are needed
158 * 2) At least RTL2831U does not support it
160 * Method 3 is needed in case of I2C write+read (typical register read)
161 * where write is more than one byte.
164 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
167 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
168 (msg[1].flags & I2C_M_RD)) {
169 if (msg[0].len > 24 || msg[1].len > 24) {
170 /* TODO: check msg[0].len max */
172 goto err_mutex_unlock;
173 } else if (msg[0].addr == 0x10) {
174 /* method 1 - integrated demod */
175 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
176 req.index = CMD_DEMOD_RD | priv->page;
177 req.size = msg[1].len;
178 req.data = &msg[1].buf[0];
179 ret = rtl28xxu_ctrl_msg(d, &req);
180 } else if (msg[0].len < 2) {
181 /* method 2 - old I2C */
182 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
183 req.index = CMD_I2C_RD;
184 req.size = msg[1].len;
185 req.data = &msg[1].buf[0];
186 ret = rtl28xxu_ctrl_msg(d, &req);
188 /* method 3 - new I2C */
189 req.value = (msg[0].addr << 1);
190 req.index = CMD_I2C_DA_WR;
191 req.size = msg[0].len;
192 req.data = msg[0].buf;
193 ret = rtl28xxu_ctrl_msg(d, &req);
195 goto err_mutex_unlock;
197 req.value = (msg[0].addr << 1);
198 req.index = CMD_I2C_DA_RD;
199 req.size = msg[1].len;
200 req.data = msg[1].buf;
201 ret = rtl28xxu_ctrl_msg(d, &req);
203 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
204 if (msg[0].len > 22) {
205 /* TODO: check msg[0].len max */
207 goto err_mutex_unlock;
208 } else if (msg[0].addr == 0x10) {
209 /* method 1 - integrated demod */
210 if (msg[0].buf[0] == 0x00) {
211 /* save demod page for later demod access */
212 priv->page = msg[0].buf[1];
215 req.value = (msg[0].buf[0] << 8) |
217 req.index = CMD_DEMOD_WR | priv->page;
218 req.size = msg[0].len-1;
219 req.data = &msg[0].buf[1];
220 ret = rtl28xxu_ctrl_msg(d, &req);
222 } else if (msg[0].len < 23) {
223 /* method 2 - old I2C */
224 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
225 req.index = CMD_I2C_WR;
226 req.size = msg[0].len-1;
227 req.data = &msg[0].buf[1];
228 ret = rtl28xxu_ctrl_msg(d, &req);
230 /* method 3 - new I2C */
231 req.value = (msg[0].addr << 1);
232 req.index = CMD_I2C_DA_WR;
233 req.size = msg[0].len;
234 req.data = msg[0].buf;
235 ret = rtl28xxu_ctrl_msg(d, &req);
242 mutex_unlock(&d->i2c_mutex);
244 return ret ? ret : num;
247 static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
252 static struct i2c_algorithm rtl28xxu_i2c_algo = {
253 .master_xfer = rtl28xxu_i2c_xfer,
254 .functionality = rtl28xxu_i2c_func,
257 static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
258 .i2c_addr = 0x10, /* 0x20 */
265 .agc_targ_val = 0x2d,
269 static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
270 .i2c_addr = 0x10, /* 0x20 */
277 .agc_targ_val = 0x2d,
280 static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
281 .i2c_addr = 0x10, /* 0x20 */
288 .agc_targ_val = 0x3e,
291 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
294 struct rtl28xxu_priv *priv = adap->dev->priv;
296 struct rtl2830_config *rtl2830_config;
297 /* open RTL2831U/RTL2830 I2C gate */
298 struct rtl28xxu_req req_gate = { 0x0120, 0x0011, 0x0001, "\x08" };
299 /* for MT2060 tuner probe */
300 struct rtl28xxu_req req_mt2060 = { 0x00c0, CMD_I2C_RD, 1, buf };
301 /* for QT1010 tuner probe */
302 struct rtl28xxu_req req_qt1010 = { 0x0fc4, CMD_I2C_RD, 1, buf };
304 deb_info("%s:\n", __func__);
308 * =========================================================
309 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
310 * GPIO2 | LED | 0 off | 1 on |
311 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
315 ret = rtl28xx_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
319 /* enable as output GPIO0, GPIO2, GPIO4 */
320 ret = rtl28xx_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
325 * Probe used tuner. We need to know used tuner before demod attach
326 * since there is some demod params needed to set according to tuner.
329 /* demod needs some time to wake up */
332 /* open demod I2C gate */
333 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
337 /* check QT1010 ID(?) register; reg=0f val=2c */
338 ret = rtl28xxu_ctrl_msg(adap->dev, &req_qt1010);
339 if (ret == 0 && buf[0] == 0x2c) {
340 priv->tuner = TUNER_RTL2830_QT1010;
341 rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
342 deb_info("%s: QT1010\n", __func__);
345 deb_info("%s: QT1010 probe failed=%d - %02x\n",
346 __func__, ret, buf[0]);
349 /* open demod I2C gate */
350 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
354 /* check MT2060 ID register; reg=00 val=63 */
355 ret = rtl28xxu_ctrl_msg(adap->dev, &req_mt2060);
356 if (ret == 0 && buf[0] == 0x63) {
357 priv->tuner = TUNER_RTL2830_MT2060;
358 rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
359 deb_info("%s: MT2060\n", __func__);
362 deb_info("%s: MT2060 probe failed=%d - %02x\n",
363 __func__, ret, buf[0]);
366 /* assume MXL5005S */
368 priv->tuner = TUNER_RTL2830_MXL5005S;
369 rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
370 deb_info("%s: MXL5005S\n", __func__);
374 /* attach demodulator */
375 adap->fe_adap[0].fe = dvb_attach(rtl2830_attach, rtl2830_config,
376 &adap->dev->i2c_adap);
377 if (adap->fe_adap[0].fe == NULL) {
384 deb_info("%s: failed=%d\n", __func__, ret);
388 static struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = {
389 .i2c_addr = 0x10, /* 0x20 */
392 .tuner = TUNER_RTL2832_FC0012
395 static struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = {
396 .i2c_addr = 0x10, /* 0x20 */
399 .tuner = TUNER_RTL2832_FC0013
402 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
408 deb_info("%s cmd=%d arg=%d\n", __func__, cmd, arg);
410 case FC_FE_CALLBACK_VHF_ENABLE:
411 /* set output values */
412 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
417 val &= 0xbf; /* set GPIO6 low */
419 val |= 0x40; /* set GPIO6 high */
422 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
433 err("%s: failed=%d\n", __func__, ret);
439 static int rtl2832u_fc0013_tuner_callback(struct dvb_usb_device *d,
446 static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
448 struct rtl28xxu_priv *priv = d->priv;
450 switch (priv->tuner) {
451 case TUNER_RTL2832_FC0012:
452 return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
454 case TUNER_RTL2832_FC0013:
455 return rtl2832u_fc0013_tuner_callback(d, cmd, arg);
463 static int rtl2832u_frontend_callback(void *adapter_priv, int component,
466 struct i2c_adapter *adap = adapter_priv;
467 struct dvb_usb_device *d = i2c_get_adapdata(adap);
470 case DVB_FRONTEND_COMPONENT_TUNER:
471 return rtl2832u_tuner_callback(d, cmd, arg);
482 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
485 struct rtl28xxu_priv *priv = adap->dev->priv;
486 struct rtl2832_config *rtl2832_config;
489 /* open RTL2832U/RTL2832 I2C gate */
490 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
491 /* close RTL2832U/RTL2832 I2C gate */
492 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
493 /* for FC0012 tuner probe */
494 struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
495 /* for FC0013 tuner probe */
496 struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
497 /* for MT2266 tuner probe */
498 struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
499 /* for FC2580 tuner probe */
500 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
501 /* for MT2063 tuner probe */
502 struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
503 /* for MAX3543 tuner probe */
504 struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
505 /* for TUA9001 tuner probe */
506 struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
507 /* for MXL5007T tuner probe */
508 struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
509 /* for E4000 tuner probe */
510 struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
511 /* for TDA18272 tuner probe */
512 struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
514 deb_info("%s:\n", __func__);
517 ret = rtl28xx_rd_reg(adap->dev, SYS_GPIO_DIR, &val);
523 ret = rtl28xx_wr_reg(adap->dev, SYS_GPIO_DIR, val);
528 /* enable as output GPIO3 and GPIO6*/
529 ret = rtl28xx_rd_reg(adap->dev, SYS_GPIO_OUT_EN, &val);
535 ret = rtl28xx_wr_reg(adap->dev, SYS_GPIO_OUT_EN, val);
542 * Probe used tuner. We need to know used tuner before demod attach
543 * since there is some demod params needed to set according to tuner.
546 /* open demod I2C gate */
547 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_open);
551 priv->tuner = TUNER_NONE;
553 /* check FC0012 ID register; reg=00 val=a1 */
554 ret = rtl28xxu_ctrl_msg(adap->dev, &req_fc0012);
555 if (ret == 0 && buf[0] == 0xa1) {
556 priv->tuner = TUNER_RTL2832_FC0012;
557 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
558 info("%s: FC0012 tuner found", __func__);
562 /* check FC0013 ID register; reg=00 val=a3 */
563 ret = rtl28xxu_ctrl_msg(adap->dev, &req_fc0013);
564 if (ret == 0 && buf[0] == 0xa3) {
565 priv->tuner = TUNER_RTL2832_FC0013;
566 rtl2832_config = &rtl28xxu_rtl2832_fc0013_config;
567 info("%s: FC0013 tuner found", __func__);
571 /* check MT2266 ID register; reg=00 val=85 */
572 ret = rtl28xxu_ctrl_msg(adap->dev, &req_mt2266);
573 if (ret == 0 && buf[0] == 0x85) {
574 priv->tuner = TUNER_RTL2832_MT2266;
575 /* TODO implement tuner */
576 info("%s: MT2266 tuner found", __func__);
580 /* check FC2580 ID register; reg=01 val=56 */
581 ret = rtl28xxu_ctrl_msg(adap->dev, &req_fc2580);
582 if (ret == 0 && buf[0] == 0x56) {
583 priv->tuner = TUNER_RTL2832_FC2580;
584 /* TODO implement tuner */
585 info("%s: FC2580 tuner found", __func__);
589 /* check MT2063 ID register; reg=00 val=9e || 9c */
590 ret = rtl28xxu_ctrl_msg(adap->dev, &req_mt2063);
591 if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
592 priv->tuner = TUNER_RTL2832_MT2063;
593 /* TODO implement tuner */
594 info("%s: MT2063 tuner found", __func__);
598 /* check MAX3543 ID register; reg=00 val=38 */
599 ret = rtl28xxu_ctrl_msg(adap->dev, &req_max3543);
600 if (ret == 0 && buf[0] == 0x38) {
601 priv->tuner = TUNER_RTL2832_MAX3543;
602 /* TODO implement tuner */
603 info("%s: MAX3534 tuner found", __func__);
607 /* check TUA9001 ID register; reg=7e val=2328 */
608 ret = rtl28xxu_ctrl_msg(adap->dev, &req_tua9001);
609 if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
610 priv->tuner = TUNER_RTL2832_TUA9001;
611 /* TODO implement tuner */
612 info("%s: TUA9001 tuner found", __func__);
616 /* check MXL5007R ID register; reg=d9 val=14 */
617 ret = rtl28xxu_ctrl_msg(adap->dev, &req_mxl5007t);
618 if (ret == 0 && buf[0] == 0x14) {
619 priv->tuner = TUNER_RTL2832_MXL5007T;
620 /* TODO implement tuner */
621 info("%s: MXL5007T tuner found", __func__);
625 /* check E4000 ID register; reg=02 val=40 */
626 ret = rtl28xxu_ctrl_msg(adap->dev, &req_e4000);
627 if (ret == 0 && buf[0] == 0x40) {
628 priv->tuner = TUNER_RTL2832_E4000;
629 /* TODO implement tuner */
630 info("%s: E4000 tuner found", __func__);
634 /* check TDA18272 ID register; reg=00 val=c760 */
635 ret = rtl28xxu_ctrl_msg(adap->dev, &req_tda18272);
636 if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
637 priv->tuner = TUNER_RTL2832_TDA18272;
638 /* TODO implement tuner */
639 info("%s: TDA18272 tuner found", __func__);
644 /* close demod I2C gate */
645 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
649 /* tuner not found */
650 deb_info("No compatible tuner found");
655 /* close demod I2C gate */
656 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
660 /* attach demodulator */
661 adap->fe_adap[0].fe = dvb_attach(rtl2832_attach, rtl2832_config,
662 &adap->dev->i2c_adap);
663 if (adap->fe_adap[0].fe == NULL) {
668 /* set fe callbacks */
669 adap->fe_adap[0].fe->callback = rtl2832u_frontend_callback;
674 deb_info("%s: failed=%d\n", __func__, ret);
678 static struct qt1010_config rtl28xxu_qt1010_config = {
679 .i2c_address = 0x62, /* 0xc4 */
682 static struct mt2060_config rtl28xxu_mt2060_config = {
683 .i2c_address = 0x60, /* 0xc0 */
687 static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
688 .i2c_address = 0x63, /* 0xc6 */
689 .if_freq = IF_FREQ_4570000HZ,
690 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
691 .agc_mode = MXL_SINGLE_AGC,
692 .tracking_filter = MXL_TF_C_H,
693 .rssi_enable = MXL_RSSI_ENABLE,
694 .cap_select = MXL_CAP_SEL_ENABLE,
695 .div_out = MXL_DIV_OUT_4,
696 .clock_out = MXL_CLOCK_OUT_DISABLE,
697 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
698 .top = MXL5005S_TOP_25P2,
699 .mod_mode = MXL_DIGITAL_MODE,
700 .if_mode = MXL_ZERO_IF,
701 .AgcMasterByte = 0x00,
704 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
707 struct rtl28xxu_priv *priv = adap->dev->priv;
708 struct i2c_adapter *rtl2830_tuner_i2c;
709 struct dvb_frontend *fe;
711 deb_info("%s:\n", __func__);
713 /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
714 rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe_adap[0].fe);
716 switch (priv->tuner) {
717 case TUNER_RTL2830_QT1010:
718 fe = dvb_attach(qt1010_attach, adap->fe_adap[0].fe,
719 rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
721 case TUNER_RTL2830_MT2060:
722 fe = dvb_attach(mt2060_attach, adap->fe_adap[0].fe,
723 rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
726 case TUNER_RTL2830_MXL5005S:
727 fe = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
728 rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
732 err("unknown tuner=%d", priv->tuner);
742 deb_info("%s: failed=%d\n", __func__, ret);
746 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
749 struct rtl28xxu_priv *priv = adap->dev->priv;
750 struct dvb_frontend *fe;
752 deb_info("%s:\n", __func__);
754 switch (priv->tuner) {
755 case TUNER_RTL2832_FC0012:
756 fe = dvb_attach(fc0012_attach, adap->fe_adap[0].fe,
757 &adap->dev->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
759 /* since fc0012 includs reading the signal strength delegate
760 * that to the tuner driver */
761 adap->fe_adap[0].fe->ops.read_signal_strength = adap->fe_adap[0].
762 fe->ops.tuner_ops.get_rf_strength;
765 case TUNER_RTL2832_FC0013:
766 fe = dvb_attach(fc0013_attach, adap->fe_adap[0].fe,
767 &adap->dev->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
769 /* fc0013 also supports signal strength reading */
770 adap->fe_adap[0].fe->ops.read_signal_strength = adap->fe_adap[0]
771 .fe->ops.tuner_ops.get_rf_strength;
775 err("unknown tuner=%d", priv->tuner);
785 deb_info("%s: failed=%d\n", __func__, ret);
789 static int rtl2831u_streaming_ctrl(struct dvb_usb_adapter *adap , int onoff)
794 deb_info("%s: onoff=%d\n", __func__, onoff);
796 ret = rtl28xx_rd_reg(adap->dev, SYS_GPIO_OUT_VAL, &gpio);
803 gpio |= 0x04; /* LED on */
805 buf[0] = 0x10; /* stall EPA */
806 buf[1] = 0x02; /* reset EPA */
807 gpio &= (~0x04); /* LED off */
810 ret = rtl28xx_wr_reg(adap->dev, SYS_GPIO_OUT_VAL, gpio);
814 ret = rtl28xx_wr_regs(adap->dev, USB_EPA_CTL, buf, 2);
820 deb_info("%s: failed=%d\n", __func__, ret);
824 static int rtl2832u_streaming_ctrl(struct dvb_usb_adapter *adap , int onoff)
829 deb_info("%s: onoff=%d\n", __func__, onoff);
836 buf[0] = 0x10; /* stall EPA */
837 buf[1] = 0x02; /* reset EPA */
840 ret = rtl28xx_wr_regs(adap->dev, USB_EPA_CTL, buf, 2);
846 deb_info("%s: failed=%d\n", __func__, ret);
850 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
855 deb_info("%s: onoff=%d\n", __func__, onoff);
858 ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
862 /* tuner power, read GPIOs */
863 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
867 deb_info("%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
870 gpio |= 0x01; /* GPIO0 = 1 */
871 gpio &= (~0x10); /* GPIO4 = 0 */
875 gpio &= (~0x01); /* GPIO0 = 0 */
876 gpio |= 0x10; /* GPIO4 = 1 */
877 sys0 = sys0 & (~0xc0);
880 deb_info("%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
883 ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
887 /* tuner power, write GPIOs */
888 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
894 deb_info("%s: failed=%d\n", __func__, ret);
898 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
903 deb_info("%s: onoff=%d\n", __func__, onoff);
906 /* set output values */
907 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
914 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
919 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL1, &val);
925 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL1, val);
931 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
938 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
943 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
949 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
953 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
959 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
965 /*enable ADC_Q and ADC_I */
966 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
972 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
979 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL1, &val);
985 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL1, val);
989 /* set output values */
990 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
996 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
1001 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
1007 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
1015 deb_info("%s: failed=%d\n", __func__, ret);
1020 static int rtl2831u_rc_query(struct dvb_usb_device *d)
1023 struct rtl28xxu_priv *priv = d->priv;
1026 struct rtl28xxu_reg_val rc_nec_tab[] = {
1043 /* init remote controller */
1044 if (!priv->rc_active) {
1045 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1046 ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
1051 priv->rc_active = true;
1054 ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
1058 if (buf[4] & 0x01) {
1059 if (buf[2] == (u8) ~buf[3]) {
1060 if (buf[0] == (u8) ~buf[1]) {
1061 /* NEC standard (16 bit) */
1062 rc_code = buf[0] << 8 | buf[2];
1064 /* NEC extended (24 bit) */
1065 rc_code = buf[0] << 16 |
1066 buf[1] << 8 | buf[2];
1069 /* NEC full (32 bit) */
1070 rc_code = buf[0] << 24 | buf[1] << 16 |
1071 buf[2] << 8 | buf[3];
1074 rc_keydown(d->rc_dev, rc_code, 0);
1076 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1080 /* repeated intentionally to avoid extra keypress */
1081 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1088 deb_info("%s: failed=%d\n", __func__, ret);
1092 static int rtl2832u_rc_query(struct dvb_usb_device *d)
1095 struct rtl28xxu_priv *priv = d->priv;
1098 struct rtl28xxu_reg_val rc_nec_tab[] = {
1099 { IR_RX_CTRL, 0x20 },
1100 { IR_RX_BUF_CTRL, 0x80 },
1103 { IR_MAX_DURATION0, 0xd0 },
1104 { IR_MAX_DURATION1, 0x07 },
1105 { IR_IDLE_LEN0, 0xc0 },
1106 { IR_IDLE_LEN1, 0x00 },
1107 { IR_GLITCH_LEN, 0x03 },
1108 { IR_RX_CLK, 0x09 },
1109 { IR_RX_CFG, 0x1c },
1110 { IR_MAX_H_TOL_LEN, 0x1e },
1111 { IR_MAX_L_TOL_LEN, 0x1e },
1112 { IR_RX_CTRL, 0x80 },
1115 /* init remote controller */
1116 if (!priv->rc_active) {
1117 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1118 ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
1123 priv->rc_active = true;
1126 ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
1133 ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
1138 ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
1140 /* TODO: pass raw IR to Kernel IR decoder */
1142 ret = rtl28xx_wr_reg(d, IR_RX_IF, 0x03);
1143 ret = rtl28xx_wr_reg(d, IR_RX_BUF_CTRL, 0x80);
1144 ret = rtl28xx_wr_reg(d, IR_RX_CTRL, 0x80);
1149 deb_info("%s: failed=%d\n", __func__, ret);
1153 enum rtl28xxu_usb_table_entry {
1162 static struct usb_device_id rtl28xxu_table[] = {
1164 [RTL2831U_0BDA_2831] = {
1165 USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U)},
1166 [RTL2831U_14AA_0160] = {
1167 USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT)},
1168 [RTL2831U_14AA_0161] = {
1169 USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2)},
1172 [RTL2832U_0CCD_00A9] = {
1173 USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1)},
1174 [RTL2832U_1F4D_B803] = {
1175 USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT)},
1176 [RTL2832U_0CCD_00B3] = {
1177 USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK)},
1178 {} /* terminating entry */
1181 MODULE_DEVICE_TABLE(usb, rtl28xxu_table);
1183 static struct dvb_usb_device_properties rtl28xxu_properties[] = {
1185 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1187 .usb_ctrl = DEVICE_SPECIFIC,
1190 .size_of_priv = sizeof(struct rtl28xxu_priv),
1198 .frontend_attach = rtl2831u_frontend_attach,
1199 .tuner_attach = rtl2831u_tuner_attach,
1200 .streaming_ctrl = rtl2831u_streaming_ctrl,
1207 .buffersize = 8*512,
1216 .power_ctrl = rtl2831u_power_ctrl,
1219 .protocol = RC_TYPE_NEC,
1220 .module_name = "rtl28xxu",
1221 .rc_query = rtl2831u_rc_query,
1223 .allowed_protos = RC_TYPE_NEC,
1224 .rc_codes = RC_MAP_EMPTY,
1227 .i2c_algo = &rtl28xxu_i2c_algo,
1229 .num_device_descs = 2,
1232 .name = "Realtek RTL2831U reference design",
1234 &rtl28xxu_table[RTL2831U_0BDA_2831],
1238 .name = "Freecom USB2.0 DVB-T",
1240 &rtl28xxu_table[RTL2831U_14AA_0160],
1241 &rtl28xxu_table[RTL2831U_14AA_0161],
1247 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1249 .usb_ctrl = DEVICE_SPECIFIC,
1252 .size_of_priv = sizeof(struct rtl28xxu_priv),
1260 .frontend_attach = rtl2832u_frontend_attach,
1261 .tuner_attach = rtl2832u_tuner_attach,
1262 .streaming_ctrl = rtl2832u_streaming_ctrl,
1269 .buffersize = 8*512,
1278 .power_ctrl = rtl2832u_power_ctrl,
1281 .protocol = RC_TYPE_NEC,
1282 .module_name = "rtl28xxu",
1283 .rc_query = rtl2832u_rc_query,
1285 .allowed_protos = RC_TYPE_NEC,
1286 .rc_codes = RC_MAP_EMPTY,
1289 .i2c_algo = &rtl28xxu_i2c_algo,
1291 .num_device_descs = 3,
1294 .name = "Terratec Cinergy T Stick Black",
1296 &rtl28xxu_table[RTL2832U_0CCD_00A9],
1300 .name = "G-Tek Electronics Group Lifeview LV5TDLX DVB-T",
1302 &rtl28xxu_table[RTL2832U_1F4D_B803],
1306 .name = "NOXON DAB/DAB+ USB dongle",
1308 &rtl28xxu_table[RTL2832U_0CCD_00B3],
1316 static int rtl28xxu_probe(struct usb_interface *intf,
1317 const struct usb_device_id *id)
1321 int properties_count = ARRAY_SIZE(rtl28xxu_properties);
1322 struct dvb_usb_device *d;
1323 struct usb_device *udev;
1326 deb_info("%s: interface=%d\n", __func__,
1327 intf->cur_altsetting->desc.bInterfaceNumber);
1329 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1332 /* Dynamic USB ID support. Replaces first device ID with current one .*/
1333 udev = interface_to_usbdev(intf);
1335 for (i = 0, found = false; i < ARRAY_SIZE(rtl28xxu_table) - 1; i++) {
1336 if (rtl28xxu_table[i].idVendor ==
1337 le16_to_cpu(udev->descriptor.idVendor) &&
1338 rtl28xxu_table[i].idProduct ==
1339 le16_to_cpu(udev->descriptor.idProduct)) {
1346 deb_info("%s: using dynamic ID %04x:%04x\n", __func__,
1347 le16_to_cpu(udev->descriptor.idVendor),
1348 le16_to_cpu(udev->descriptor.idProduct));
1349 rtl28xxu_properties[0].devices[0].warm_ids[0]->idVendor =
1350 le16_to_cpu(udev->descriptor.idVendor);
1351 rtl28xxu_properties[0].devices[0].warm_ids[0]->idProduct =
1352 le16_to_cpu(udev->descriptor.idProduct);
1355 for (i = 0; i < properties_count; i++) {
1356 ret = dvb_usb_device_init(intf, &rtl28xxu_properties[i],
1357 THIS_MODULE, &d, adapter_nr);
1358 if (ret == 0 || ret != -ENODEV)
1366 /* init USB endpoints */
1367 ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
1371 /* enable DMA and Full Packet Mode*/
1373 ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
1377 /* set EPA maximum packet size to 0x0200 */
1378 ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
1382 /* change EPA FIFO length */
1383 ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
1389 deb_info("%s: failed=%d\n", __func__, ret);
1393 static struct usb_driver rtl28xxu_driver = {
1394 .name = "dvb_usb_rtl28xxu",
1395 .probe = rtl28xxu_probe,
1396 .disconnect = dvb_usb_device_exit,
1397 .id_table = rtl28xxu_table,
1401 static int __init rtl28xxu_module_init(void)
1405 deb_info("%s:\n", __func__);
1407 ret = usb_register(&rtl28xxu_driver);
1409 err("usb_register failed=%d", ret);
1414 static void __exit rtl28xxu_module_exit(void)
1416 deb_info("%s:\n", __func__);
1418 /* deregister this driver from the USB subsystem */
1419 usb_deregister(&rtl28xxu_driver);
1422 module_init(rtl28xxu_module_init);
1423 module_exit(rtl28xxu_module_exit);
1425 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1426 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1427 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
1428 MODULE_LICENSE("GPL");