rtl28xxu.c 23 KB

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