rtl28xxu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. #include "rtl28xxu.h"
  22. #include "rtl2830.h"
  23. #include "qt1010.h"
  24. #include "mt2060.h"
  25. #include "mxl5005s.h"
  26. /* debug */
  27. static int dvb_usb_rtl28xxu_debug;
  28. module_param_named(debug, dvb_usb_rtl28xxu_debug, int, 0644);
  29. MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
  30. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  31. static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
  32. {
  33. int ret;
  34. unsigned int pipe;
  35. u8 requesttype;
  36. u8 *buf;
  37. buf = kmalloc(req->size, GFP_KERNEL);
  38. if (!buf) {
  39. ret = -ENOMEM;
  40. goto err;
  41. }
  42. if (req->index & CMD_WR_FLAG) {
  43. /* write */
  44. memcpy(buf, req->data, req->size);
  45. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  46. pipe = usb_sndctrlpipe(d->udev, 0);
  47. } else {
  48. /* read */
  49. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  50. pipe = usb_rcvctrlpipe(d->udev, 0);
  51. }
  52. ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
  53. req->index, buf, req->size, 1000);
  54. deb_dump(0, requesttype, req->value, req->index, buf, req->size,
  55. deb_xfer);
  56. if (ret < 0)
  57. goto err_dealloc;
  58. else
  59. ret = 0;
  60. /* read request, copy returned data to return buf */
  61. if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
  62. memcpy(req->data, buf, req->size);
  63. kfree(buf);
  64. return ret;
  65. err_dealloc:
  66. kfree(buf);
  67. err:
  68. deb_info("%s: failed=%d\n", __func__, ret);
  69. return ret;
  70. }
  71. static int rtl2831_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  72. {
  73. struct rtl28xxu_req req;
  74. if (reg < 0x3000)
  75. req.index = CMD_USB_WR;
  76. else if (reg < 0x4000)
  77. req.index = CMD_SYS_WR;
  78. else
  79. req.index = CMD_IR_WR;
  80. req.value = reg;
  81. req.size = len;
  82. req.data = val;
  83. return rtl28xxu_ctrl_msg(d, &req);
  84. }
  85. static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  86. {
  87. struct rtl28xxu_req req;
  88. if (reg < 0x3000)
  89. req.index = CMD_USB_RD;
  90. else if (reg < 0x4000)
  91. req.index = CMD_SYS_RD;
  92. else
  93. req.index = CMD_IR_RD;
  94. req.value = reg;
  95. req.size = len;
  96. req.data = val;
  97. return rtl28xxu_ctrl_msg(d, &req);
  98. }
  99. static int rtl2831_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
  100. {
  101. return rtl2831_wr_regs(d, reg, &val, 1);
  102. }
  103. static int rtl2831_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
  104. {
  105. return rtl2831_rd_regs(d, reg, val, 1);
  106. }
  107. /* I2C */
  108. static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  109. int num)
  110. {
  111. int ret;
  112. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  113. struct rtl28xxu_req req;
  114. /*
  115. * It is not known which are real I2C bus xfer limits, but testing
  116. * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
  117. */
  118. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  119. return -EAGAIN;
  120. if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
  121. (msg[1].flags & I2C_M_RD)) {
  122. if (msg[0].len > 2 || msg[1].len > 24) {
  123. ret = -EOPNOTSUPP;
  124. goto err_unlock;
  125. }
  126. if (msg[0].addr == 0x10) {
  127. /* integrated demod */
  128. req.value = (msg[0].buf[1] << 8) | (msg[0].addr << 1);
  129. req.index = CMD_DEMOD_RD | msg[0].buf[0];
  130. req.size = msg[1].len;
  131. req.data = &msg[1].buf[0];
  132. ret = rtl28xxu_ctrl_msg(d, &req);
  133. } else {
  134. /* real I2C */
  135. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  136. req.index = CMD_I2C_RD;
  137. req.size = msg[1].len;
  138. req.data = &msg[1].buf[0];
  139. ret = rtl28xxu_ctrl_msg(d, &req);
  140. }
  141. } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
  142. if (msg[0].len > 22) {
  143. ret = -EOPNOTSUPP;
  144. goto err_unlock;
  145. }
  146. if (msg[0].addr == 0x10) {
  147. /* integrated demod */
  148. req.value = (msg[0].buf[1] << 8) | (msg[0].addr << 1);
  149. req.index = CMD_DEMOD_WR | msg[0].buf[0];
  150. req.size = msg[0].len-2;
  151. req.data = &msg[0].buf[2];
  152. ret = rtl28xxu_ctrl_msg(d, &req);
  153. } else {
  154. /* real I2C */
  155. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  156. req.index = CMD_I2C_WR;
  157. req.size = msg[0].len-1;
  158. req.data = &msg[0].buf[1];
  159. ret = rtl28xxu_ctrl_msg(d, &req);
  160. }
  161. } else {
  162. ret = -EINVAL;
  163. }
  164. err_unlock:
  165. mutex_unlock(&d->i2c_mutex);
  166. return ret ? ret : num;
  167. }
  168. static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
  169. {
  170. return I2C_FUNC_I2C;
  171. }
  172. static struct i2c_algorithm rtl28xxu_i2c_algo = {
  173. .master_xfer = rtl28xxu_i2c_xfer,
  174. .functionality = rtl28xxu_i2c_func,
  175. };
  176. static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
  177. .i2c_addr = 0x10, /* 0x20 */
  178. .xtal = 28800000,
  179. .ts_mode = 0,
  180. .spec_inv = 1,
  181. .if_dvbt = 36150000,
  182. .vtop = 0x20,
  183. .krf = 0x04,
  184. .agc_targ_val = 0x2d,
  185. };
  186. static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
  187. .i2c_addr = 0x10, /* 0x20 */
  188. .xtal = 28800000,
  189. .ts_mode = 0,
  190. .spec_inv = 1,
  191. .if_dvbt = 36125000,
  192. .vtop = 0x20,
  193. .krf = 0x04,
  194. .agc_targ_val = 0x2d,
  195. };
  196. static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
  197. .i2c_addr = 0x10, /* 0x20 */
  198. .xtal = 28800000,
  199. .ts_mode = 0,
  200. .spec_inv = 0,
  201. .if_dvbt = 4570000,
  202. .vtop = 0x3f,
  203. .krf = 0x04,
  204. .agc_targ_val = 0x3e,
  205. };
  206. static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
  207. {
  208. int ret;
  209. struct rtl28xxu_priv *priv = adap->dev->priv;
  210. u8 buf[1];
  211. struct rtl2830_config *rtl2830_config;
  212. /* open RTL2831U/RTL2830 I2C gate */
  213. struct rtl28xxu_req req_gate = {0x0120, 0x0011, 0x0001, "\x08"};
  214. /* for MT2060 tuner probe */
  215. struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
  216. /* for QT1010 tuner probe */
  217. struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
  218. deb_info("%s:\n", __func__);
  219. /*
  220. * RTL2831U GPIOs
  221. * =========================================================
  222. * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
  223. * GPIO2 | LED | 0 off | 1 on |
  224. * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
  225. */
  226. /* GPIO direction */
  227. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
  228. if (ret)
  229. goto err;
  230. /* enable as output GPIO0, GPIO2, GPIO4 */
  231. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
  232. if (ret)
  233. goto err;
  234. /*
  235. * Probe used tuner. We need to know used tuner before demod attach
  236. * since there is some demod params needed to set according to tuner.
  237. */
  238. /* open demod I2C gate */
  239. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
  240. if (ret)
  241. goto err;
  242. /* check QT1010 ID(?) register; reg=0f val=2c */
  243. ret = rtl28xxu_ctrl_msg(adap->dev, &req_qt1010);
  244. if (ret == 0 && buf[0] == 0x2c) {
  245. priv->tuner = TUNER_RTL2830_QT1010;
  246. rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
  247. deb_info("%s: QT1010\n", __func__);
  248. goto found;
  249. } else {
  250. deb_info("%s: QT1010 probe failed=%d - %02x\n",
  251. __func__, ret, buf[0]);
  252. }
  253. /* open demod I2C gate */
  254. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
  255. if (ret)
  256. goto err;
  257. /* check MT2060 ID register; reg=00 val=63 */
  258. ret = rtl28xxu_ctrl_msg(adap->dev, &req_mt2060);
  259. if (ret == 0 && buf[0] == 0x63) {
  260. priv->tuner = TUNER_RTL2830_MT2060;
  261. rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
  262. deb_info("%s: MT2060\n", __func__);
  263. goto found;
  264. } else {
  265. deb_info("%s: MT2060 probe failed=%d - %02x\n",
  266. __func__, ret, buf[0]);
  267. }
  268. /* assume MXL5005S */
  269. ret = 0;
  270. priv->tuner = TUNER_RTL2830_MXL5005S;
  271. rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
  272. deb_info("%s: MXL5005S\n", __func__);
  273. goto found;
  274. found:
  275. /* attach demodulator */
  276. adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config,
  277. &adap->dev->i2c_adap);
  278. if (adap->fe[0] == NULL) {
  279. ret = -ENODEV;
  280. goto err;
  281. }
  282. return ret;
  283. err:
  284. deb_info("%s: failed=%d\n", __func__, ret);
  285. return ret;
  286. }
  287. static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
  288. {
  289. int ret;
  290. struct rtl28xxu_priv *priv = adap->dev->priv;
  291. u8 buf[1];
  292. /* open RTL2832U/RTL2832 I2C gate */
  293. struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
  294. /* close RTL2832U/RTL2832 I2C gate */
  295. struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
  296. /* for FC2580 tuner probe */
  297. struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
  298. deb_info("%s:\n", __func__);
  299. /* GPIO direction */
  300. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
  301. if (ret)
  302. goto err;
  303. /* enable as output GPIO0, GPIO2, GPIO4 */
  304. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
  305. if (ret)
  306. goto err;
  307. ret = rtl2831_wr_reg(adap->dev, SYS_DEMOD_CTL, 0xe8);
  308. if (ret)
  309. goto err;
  310. /*
  311. * Probe used tuner. We need to know used tuner before demod attach
  312. * since there is some demod params needed to set according to tuner.
  313. */
  314. /* open demod I2C gate */
  315. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_open);
  316. if (ret)
  317. goto err;
  318. /* check FC2580 ID register; reg=01 val=56 */
  319. ret = rtl28xxu_ctrl_msg(adap->dev, &req_fc2580);
  320. if (ret == 0 && buf[0] == 0x56) {
  321. priv->tuner = TUNER_RTL2832_FC2580;
  322. deb_info("%s: FC2580\n", __func__);
  323. goto found;
  324. } else {
  325. deb_info("%s: FC2580 probe failed=%d - %02x\n",
  326. __func__, ret, buf[0]);
  327. }
  328. /* close demod I2C gate */
  329. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
  330. if (ret)
  331. goto err;
  332. /* tuner not found */
  333. ret = -ENODEV;
  334. goto err;
  335. found:
  336. /* close demod I2C gate */
  337. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
  338. if (ret)
  339. goto err;
  340. /* attach demodulator */
  341. /* TODO: */
  342. return ret;
  343. err:
  344. deb_info("%s: failed=%d\n", __func__, ret);
  345. return ret;
  346. }
  347. static struct qt1010_config rtl28xxu_qt1010_config = {
  348. .i2c_address = 0x62, /* 0xc4 */
  349. };
  350. static struct mt2060_config rtl28xxu_mt2060_config = {
  351. .i2c_address = 0x60, /* 0xc0 */
  352. .clock_out = 0,
  353. };
  354. static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
  355. .i2c_address = 0x63, /* 0xc6 */
  356. .if_freq = IF_FREQ_4570000HZ,
  357. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  358. .agc_mode = MXL_SINGLE_AGC,
  359. .tracking_filter = MXL_TF_C_H,
  360. .rssi_enable = MXL_RSSI_ENABLE,
  361. .cap_select = MXL_CAP_SEL_ENABLE,
  362. .div_out = MXL_DIV_OUT_4,
  363. .clock_out = MXL_CLOCK_OUT_DISABLE,
  364. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  365. .top = MXL5005S_TOP_25P2,
  366. .mod_mode = MXL_DIGITAL_MODE,
  367. .if_mode = MXL_ZERO_IF,
  368. .AgcMasterByte = 0x00,
  369. };
  370. static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
  371. {
  372. int ret;
  373. struct rtl28xxu_priv *priv = adap->dev->priv;
  374. struct i2c_adapter *rtl2830_tuner_i2c;
  375. struct dvb_frontend *fe = NULL;
  376. deb_info("%s:\n", __func__);
  377. /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
  378. rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
  379. switch (priv->tuner) {
  380. case TUNER_RTL2830_QT1010:
  381. fe = dvb_attach(qt1010_attach, adap->fe[0], rtl2830_tuner_i2c,
  382. &rtl28xxu_qt1010_config);
  383. break;
  384. case TUNER_RTL2830_MT2060:
  385. fe = dvb_attach(mt2060_attach, adap->fe[0], rtl2830_tuner_i2c,
  386. &rtl28xxu_mt2060_config, 1220);
  387. break;
  388. case TUNER_RTL2830_MXL5005S:
  389. fe = dvb_attach(mxl5005s_attach, adap->fe[0], rtl2830_tuner_i2c,
  390. &rtl28xxu_mxl5005s_config);
  391. break;
  392. default:
  393. err("unknown tuner=%d", priv->tuner);
  394. }
  395. if (fe == NULL) {
  396. ret = -ENODEV;
  397. goto err;
  398. }
  399. return 0;
  400. err:
  401. deb_info("%s: failed=%d\n", __func__, ret);
  402. return ret;
  403. }
  404. static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
  405. {
  406. int ret;
  407. struct rtl28xxu_priv *priv = adap->dev->priv;
  408. struct dvb_frontend *fe = NULL;
  409. deb_info("%s:\n", __func__);
  410. switch (priv->tuner) {
  411. case TUNER_RTL2832_FC2580:
  412. /* TODO: */
  413. break;
  414. default:
  415. err("unknown tuner=%d", priv->tuner);
  416. }
  417. if (fe == NULL) {
  418. ret = -ENODEV;
  419. goto err;
  420. }
  421. return 0;
  422. err:
  423. deb_info("%s: failed=%d\n", __func__, ret);
  424. return ret;
  425. }
  426. static int rtl28xxu_streaming_ctrl(struct dvb_usb_adapter *adap , int onoff)
  427. {
  428. int ret;
  429. u8 buf[2], gpio;
  430. deb_info("%s: onoff=%d\n", __func__, onoff);
  431. ret = rtl2831_rd_reg(adap->dev, SYS_GPIO_OUT_VAL, &gpio);
  432. if (ret)
  433. goto err;
  434. if (onoff) {
  435. buf[0] = 0x00;
  436. buf[1] = 0x00;
  437. gpio |= 0x04; /* LED on */
  438. } else {
  439. buf[0] = 0x10; /* stall EPA */
  440. buf[1] = 0x02; /* reset EPA */
  441. gpio &= (~0x04); /* LED off */
  442. }
  443. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_VAL, gpio);
  444. if (ret)
  445. goto err;
  446. ret = rtl2831_wr_regs(adap->dev, USB_EPA_CTL, buf, 2);
  447. if (ret)
  448. goto err;
  449. return ret;
  450. err:
  451. deb_info("%s: failed=%d\n", __func__, ret);
  452. return ret;
  453. }
  454. static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff)
  455. {
  456. int ret;
  457. u8 gpio, sys0;
  458. deb_info("%s: onoff=%d\n", __func__, onoff);
  459. /* demod adc */
  460. ret = rtl2831_rd_reg(d, SYS_SYS0, &sys0);
  461. if (ret)
  462. goto err;
  463. /* tuner power, read GPIOs */
  464. ret = rtl2831_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
  465. if (ret)
  466. goto err;
  467. deb_info("%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
  468. if (onoff) {
  469. gpio |= 0x01; /* GPIO0 = 1 */
  470. gpio &= (~0x10); /* GPIO4 = 0 */
  471. sys0 = sys0 & 0x0f;
  472. sys0 |= 0xe0;
  473. } else {
  474. /*
  475. * FIXME: Use .fe_ioctl_override() to prevent demod
  476. * IOCTLs in case of device is powered off. Or change
  477. * RTL2830 demod not perform requestesd IOCTL & IO when sleep.
  478. */
  479. gpio &= (~0x01); /* GPIO0 = 0 */
  480. gpio |= 0x10; /* GPIO4 = 1 */
  481. sys0 = sys0 & (~0xc0);
  482. }
  483. deb_info("%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
  484. /* demod adc */
  485. ret = rtl2831_wr_reg(d, SYS_SYS0, sys0);
  486. if (ret)
  487. goto err;
  488. /* tuner power, write GPIOs */
  489. ret = rtl2831_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
  490. if (ret)
  491. goto err;
  492. return ret;
  493. err:
  494. deb_info("%s: failed=%d\n", __func__, ret);
  495. return ret;
  496. }
  497. static int rtl2831u_rc_query(struct dvb_usb_device *d)
  498. {
  499. int ret, i;
  500. struct rtl28xxu_priv *priv = d->priv;
  501. u8 buf[5];
  502. u32 rc_code;
  503. struct rtl28xxu_reg_val rc_nec_tab[] = {
  504. { 0x3033, 0x80 },
  505. { 0x3020, 0x43 },
  506. { 0x3021, 0x16 },
  507. { 0x3022, 0x16 },
  508. { 0x3023, 0x5a },
  509. { 0x3024, 0x2d },
  510. { 0x3025, 0x16 },
  511. { 0x3026, 0x01 },
  512. { 0x3028, 0xb0 },
  513. { 0x3029, 0x04 },
  514. { 0x302c, 0x88 },
  515. { 0x302e, 0x13 },
  516. { 0x3030, 0xdf },
  517. { 0x3031, 0x05 },
  518. };
  519. /* init remote controller */
  520. if (!priv->rc_active) {
  521. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  522. ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
  523. rc_nec_tab[i].val);
  524. if (ret)
  525. goto err;
  526. }
  527. priv->rc_active = true;
  528. }
  529. ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
  530. if (ret)
  531. goto err;
  532. if (buf[4] & 0x01) {
  533. if (buf[2] == (u8) ~buf[3]) {
  534. if (buf[0] == (u8) ~buf[1]) {
  535. /* NEC standard (16 bit) */
  536. rc_code = buf[0] << 8 | buf[2];
  537. } else {
  538. /* NEC extended (24 bit) */
  539. rc_code = buf[0] << 16 |
  540. buf[1] << 8 | buf[2];
  541. }
  542. } else {
  543. /* NEC full (32 bit) */
  544. rc_code = buf[0] << 24 | buf[1] << 16 |
  545. buf[2] << 8 | buf[3];
  546. }
  547. rc_keydown(d->rc_dev, rc_code, 0);
  548. ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
  549. if (ret)
  550. goto err;
  551. /* repeated intentionally to avoid extra keypress */
  552. ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
  553. if (ret)
  554. goto err;
  555. }
  556. return ret;
  557. err:
  558. deb_info("%s: failed=%d\n", __func__, ret);
  559. return ret;
  560. }
  561. static int rtl2832u_rc_query(struct dvb_usb_device *d)
  562. {
  563. int ret, i;
  564. struct rtl28xxu_priv *priv = d->priv;
  565. u8 buf[128];
  566. int len;
  567. struct rtl28xxu_reg_val rc_nec_tab[] = {
  568. {IR_RX_CTRL, 0x20},
  569. {IR_RX_BUF_CTRL, 0x80},
  570. {IR_RX_IF, 0xff},
  571. {IR_RX_IE, 0xff},
  572. {IR_MAX_DURATION0, 0xd0},
  573. {IR_MAX_DURATION1, 0x07},
  574. {IR_IDLE_LEN0, 0xc0},
  575. {IR_IDLE_LEN1, 0x00},
  576. {IR_GLITCH_LEN, 0x03},
  577. {IR_RX_CLK, 0x09},
  578. {IR_RX_CFG, 0x1c},
  579. {IR_MAX_H_TOL_LEN, 0x1e},
  580. {IR_MAX_L_TOL_LEN, 0x1e},
  581. {IR_RX_CTRL, 0x80},
  582. };
  583. /* init remote controller */
  584. if (!priv->rc_active) {
  585. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  586. ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
  587. rc_nec_tab[i].val);
  588. if (ret)
  589. goto err;
  590. }
  591. priv->rc_active = true;
  592. }
  593. ret = rtl2831_rd_reg(d, IR_RX_IF, &buf[0]);
  594. if (ret)
  595. goto err;
  596. if (buf[0] != 0x83)
  597. goto exit;
  598. ret = rtl2831_rd_reg(d, IR_RX_BC, &buf[0]);
  599. if (ret)
  600. goto err;
  601. len = buf[0];
  602. ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
  603. /* TODO: pass raw IR to Kernel IR decoder */
  604. ret = rtl2831_wr_reg(d, IR_RX_IF, 0x03);
  605. ret = rtl2831_wr_reg(d, IR_RX_BUF_CTRL, 0x80);
  606. ret = rtl2831_wr_reg(d, IR_RX_CTRL, 0x80);
  607. exit:
  608. return ret;
  609. err:
  610. deb_info("%s: failed=%d\n", __func__, ret);
  611. return ret;
  612. }
  613. /* DVB USB Driver stuff */
  614. #define USB_VID_REALTEK 0x0bda
  615. #define USB_VID_DEXATEK 0x1D19
  616. #define USB_PID_RTL2831U 0x2831
  617. #define USB_PID_RTL2832U 0x2832
  618. #define USB_PID_FREECOM 0x0160
  619. #define USB_PID_DEXATEK_1101 0x1101
  620. #define RTL2831U_0BDA_2831 0
  621. #define RTL2831U_14AA_0160 1
  622. #define RTL2832U_1ST_ID (RTL2831U_14AA_0160 + 1)
  623. #define RTL2832U_0BDA_2832 (0 + RTL2832U_1ST_ID)
  624. #define RTL2832U_1D19_1101 (1 + RTL2832U_1ST_ID)
  625. static struct usb_device_id rtl28xxu_table[] = {
  626. [RTL2831U_0BDA_2831] = {
  627. USB_DEVICE(USB_VID_REALTEK, USB_PID_RTL2831U)},
  628. [RTL2831U_14AA_0160] = {
  629. USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM)},
  630. [RTL2832U_0BDA_2832] = {
  631. USB_DEVICE(USB_VID_REALTEK, USB_PID_RTL2832U)},
  632. [RTL2832U_1D19_1101] = {
  633. USB_DEVICE(USB_VID_DEXATEK, USB_PID_DEXATEK_1101)},
  634. {} /* terminating entry */
  635. };
  636. MODULE_DEVICE_TABLE(usb, rtl28xxu_table);
  637. static struct dvb_usb_device_properties rtl28xxu_properties[] = {
  638. {
  639. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  640. .usb_ctrl = DEVICE_SPECIFIC,
  641. .no_reconnect = 1,
  642. .size_of_priv = sizeof(struct rtl28xxu_priv),
  643. .num_adapters = 1,
  644. .adapter = {
  645. {
  646. .frontend_attach = rtl2831u_frontend_attach,
  647. .tuner_attach = rtl2831u_tuner_attach,
  648. .streaming_ctrl = rtl28xxu_streaming_ctrl,
  649. .stream = {
  650. .type = USB_BULK,
  651. .count = 6,
  652. .endpoint = 0x81,
  653. .u = {
  654. .bulk = {
  655. .buffersize = 4096,
  656. }
  657. }
  658. },
  659. }
  660. },
  661. .power_ctrl = rtl28xxu_power_ctrl,
  662. .rc.core = {
  663. .protocol = RC_TYPE_NEC,
  664. .module_name = "rtl28xxu",
  665. .rc_query = rtl2831u_rc_query,
  666. .rc_interval = 400,
  667. .allowed_protos = RC_TYPE_NEC,
  668. .rc_codes = RC_MAP_EMPTY,
  669. },
  670. .i2c_algo = &rtl28xxu_i2c_algo,
  671. .num_device_descs = 2,
  672. .devices = {
  673. {
  674. .name = "Realtek RTL2831U reference design",
  675. .cold_ids = {NULL},
  676. .warm_ids = {
  677. &rtl28xxu_table[RTL2831U_0BDA_2831], NULL},
  678. },
  679. {
  680. .name = "Freecom USB2.0 DVB-T",
  681. .cold_ids = {NULL},
  682. .warm_ids = {
  683. &rtl28xxu_table[RTL2831U_14AA_0160], NULL},
  684. },
  685. }
  686. },
  687. {
  688. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  689. .usb_ctrl = DEVICE_SPECIFIC,
  690. .no_reconnect = 1,
  691. .size_of_priv = sizeof(struct rtl28xxu_priv),
  692. .num_adapters = 1,
  693. .adapter = {
  694. {
  695. .frontend_attach = rtl2832u_frontend_attach,
  696. .tuner_attach = rtl2832u_tuner_attach,
  697. .streaming_ctrl = rtl28xxu_streaming_ctrl,
  698. .stream = {
  699. .type = USB_BULK,
  700. .count = 6,
  701. .endpoint = 0x81,
  702. .u = {
  703. .bulk = {
  704. .buffersize = 4096,
  705. }
  706. }
  707. },
  708. }
  709. },
  710. .power_ctrl = rtl28xxu_power_ctrl,
  711. .rc.core = {
  712. .protocol = RC_TYPE_NEC,
  713. .module_name = "rtl28xxu",
  714. .rc_query = rtl2832u_rc_query,
  715. .rc_interval = 400,
  716. .allowed_protos = RC_TYPE_NEC,
  717. .rc_codes = RC_MAP_EMPTY,
  718. },
  719. .i2c_algo = &rtl28xxu_i2c_algo,
  720. .num_device_descs = 2,
  721. .devices = {
  722. {
  723. .name = "Realtek RTL2832U reference design",
  724. .cold_ids = {NULL},
  725. .warm_ids = {
  726. &rtl28xxu_table[RTL2832U_0BDA_2832], NULL},
  727. },
  728. {
  729. .name = "Dexatek dongle",
  730. .cold_ids = {NULL},
  731. .warm_ids = {
  732. &rtl28xxu_table[RTL2832U_1D19_1101], NULL},
  733. },
  734. }
  735. },
  736. };
  737. static int rtl28xxu_probe(struct usb_interface *intf,
  738. const struct usb_device_id *id)
  739. {
  740. int ret, i;
  741. int properties_count = ARRAY_SIZE(rtl28xxu_properties);
  742. struct dvb_usb_device *d = NULL;
  743. deb_info("%s: interface=%d\n", __func__,
  744. intf->cur_altsetting->desc.bInterfaceNumber);
  745. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  746. return 0;
  747. for (i = 0; i < properties_count; i++) {
  748. ret = dvb_usb_device_init(intf, &rtl28xxu_properties[i],
  749. THIS_MODULE, &d, adapter_nr);
  750. if (ret == 0 || ret != -ENODEV)
  751. break;
  752. }
  753. if (ret)
  754. goto err;
  755. /* init USB endpoints */
  756. ret = rtl2831_wr_reg(d, USB_SYSCTL_0, 0x09);
  757. if (ret)
  758. goto err;
  759. ret = rtl2831_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
  760. if (ret)
  761. goto err;
  762. ret = rtl2831_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
  763. if (ret)
  764. goto err;
  765. return ret;
  766. err:
  767. deb_info("%s: failed=%d\n", __func__, ret);
  768. return ret;
  769. }
  770. static struct usb_driver rtl28xxu_driver = {
  771. .name = "dvb_usb_rtl28xxu",
  772. .probe = rtl28xxu_probe,
  773. .disconnect = dvb_usb_device_exit,
  774. .id_table = rtl28xxu_table,
  775. };
  776. /* module stuff */
  777. static int __init rtl28xxu_module_init(void)
  778. {
  779. int ret;
  780. deb_info("%s:\n", __func__);
  781. ret = usb_register(&rtl28xxu_driver);
  782. if (ret)
  783. err("usb_register failed=%d", ret);
  784. return ret;
  785. }
  786. static void __exit rtl28xxu_module_exit(void)
  787. {
  788. deb_info("%s:\n", __func__);
  789. /* deregister this driver from the USB subsystem */
  790. usb_deregister(&rtl28xxu_driver);
  791. }
  792. module_init(rtl28xxu_module_init);
  793. module_exit(rtl28xxu_module_exit);
  794. MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
  795. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  796. MODULE_LICENSE("GPL");