rtl28xxu.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. if (ret > 0)
  55. ret = 0;
  56. deb_dump(0, requesttype, req->value, req->index, buf, req->size,
  57. deb_xfer);
  58. /* read request, copy returned data to return buf */
  59. if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
  60. memcpy(req->data, buf, req->size);
  61. kfree(buf);
  62. if (ret)
  63. goto err;
  64. return ret;
  65. err:
  66. deb_info("%s: failed=%d\n", __func__, ret);
  67. return ret;
  68. }
  69. static int rtl2831_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  70. {
  71. struct rtl28xxu_req req;
  72. if (reg < 0x3000)
  73. req.index = CMD_USB_WR;
  74. else if (reg < 0x4000)
  75. req.index = CMD_SYS_WR;
  76. else
  77. req.index = CMD_IR_WR;
  78. req.value = reg;
  79. req.size = len;
  80. req.data = val;
  81. return rtl28xxu_ctrl_msg(d, &req);
  82. }
  83. static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  84. {
  85. struct rtl28xxu_req req;
  86. if (reg < 0x3000)
  87. req.index = CMD_USB_RD;
  88. else if (reg < 0x4000)
  89. req.index = CMD_SYS_RD;
  90. else
  91. req.index = CMD_IR_RD;
  92. req.value = reg;
  93. req.size = len;
  94. req.data = val;
  95. return rtl28xxu_ctrl_msg(d, &req);
  96. }
  97. static int rtl2831_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
  98. {
  99. return rtl2831_wr_regs(d, reg, &val, 1);
  100. }
  101. static int rtl2831_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
  102. {
  103. return rtl2831_rd_regs(d, reg, val, 1);
  104. }
  105. /* I2C */
  106. static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  107. int num)
  108. {
  109. int ret;
  110. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  111. struct rtl28xxu_priv *priv = d->priv;
  112. struct rtl28xxu_req req;
  113. /*
  114. * It is not known which are real I2C bus xfer limits, but testing
  115. * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
  116. * TODO: find out RTL2832U lens
  117. */
  118. /*
  119. * I2C adapter logic looks rather complicated due to fact it handles
  120. * three different access methods. Those methods are;
  121. * 1) integrated demod access
  122. * 2) old I2C access
  123. * 3) new I2C access
  124. *
  125. * Used method is selected in order 1, 2, 3. Method 3 can handle all
  126. * requests but there is two reasons why not use it always;
  127. * 1) It is most expensive, usually two USB messages are needed
  128. * 2) At least RTL2831U does not support it
  129. *
  130. * Method 3 is needed in case of I2C write+read (typical register read)
  131. * where write is more than one byte.
  132. */
  133. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  134. return -EAGAIN;
  135. if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
  136. (msg[1].flags & I2C_M_RD)) {
  137. if (msg[0].len > 24 || msg[1].len > 24) {
  138. /* TODO: check msg[0].len max */
  139. ret = -EOPNOTSUPP;
  140. goto err_mutex_unlock;
  141. } else if (msg[0].addr == 0x10) {
  142. /* method 1 - integrated demod */
  143. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  144. req.index = CMD_DEMOD_RD | priv->page;
  145. req.size = msg[1].len;
  146. req.data = &msg[1].buf[0];
  147. ret = rtl28xxu_ctrl_msg(d, &req);
  148. } else if (msg[0].len < 2) {
  149. /* method 2 - old I2C */
  150. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  151. req.index = CMD_I2C_RD;
  152. req.size = msg[1].len;
  153. req.data = &msg[1].buf[0];
  154. ret = rtl28xxu_ctrl_msg(d, &req);
  155. } else {
  156. /* method 3 - new I2C */
  157. req.value = (msg[0].addr << 1);
  158. req.index = CMD_I2C_DA_WR;
  159. req.size = msg[0].len;
  160. req.data = msg[0].buf;
  161. ret = rtl28xxu_ctrl_msg(d, &req);
  162. if (ret)
  163. goto err_mutex_unlock;
  164. req.value = (msg[0].addr << 1);
  165. req.index = CMD_I2C_DA_RD;
  166. req.size = msg[1].len;
  167. req.data = msg[1].buf;
  168. ret = rtl28xxu_ctrl_msg(d, &req);
  169. }
  170. } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
  171. if (msg[0].len > 22) {
  172. /* TODO: check msg[0].len max */
  173. ret = -EOPNOTSUPP;
  174. goto err_mutex_unlock;
  175. } else if (msg[0].addr == 0x10) {
  176. /* method 1 - integrated demod */
  177. if (msg[0].buf[0] == 0x00) {
  178. /* save demod page for later demod access */
  179. priv->page = msg[0].buf[1];
  180. ret = 0;
  181. } else {
  182. req.value = (msg[0].buf[0] << 8) |
  183. (msg[0].addr << 1);
  184. req.index = CMD_DEMOD_WR | priv->page;
  185. req.size = msg[0].len-1;
  186. req.data = &msg[0].buf[1];
  187. ret = rtl28xxu_ctrl_msg(d, &req);
  188. }
  189. } else if (msg[0].len < 23) {
  190. /* method 2 - old I2C */
  191. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  192. req.index = CMD_I2C_WR;
  193. req.size = msg[0].len-1;
  194. req.data = &msg[0].buf[1];
  195. ret = rtl28xxu_ctrl_msg(d, &req);
  196. } else {
  197. /* method 3 - new I2C */
  198. req.value = (msg[0].addr << 1);
  199. req.index = CMD_I2C_DA_WR;
  200. req.size = msg[0].len;
  201. req.data = msg[0].buf;
  202. ret = rtl28xxu_ctrl_msg(d, &req);
  203. }
  204. } else {
  205. ret = -EINVAL;
  206. }
  207. err_mutex_unlock:
  208. mutex_unlock(&d->i2c_mutex);
  209. return ret ? ret : num;
  210. }
  211. static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
  212. {
  213. return I2C_FUNC_I2C;
  214. }
  215. static struct i2c_algorithm rtl28xxu_i2c_algo = {
  216. .master_xfer = rtl28xxu_i2c_xfer,
  217. .functionality = rtl28xxu_i2c_func,
  218. };
  219. static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
  220. .i2c_addr = 0x10, /* 0x20 */
  221. .xtal = 28800000,
  222. .ts_mode = 0,
  223. .spec_inv = 1,
  224. .if_dvbt = 36150000,
  225. .vtop = 0x20,
  226. .krf = 0x04,
  227. .agc_targ_val = 0x2d,
  228. };
  229. static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
  230. .i2c_addr = 0x10, /* 0x20 */
  231. .xtal = 28800000,
  232. .ts_mode = 0,
  233. .spec_inv = 1,
  234. .if_dvbt = 36125000,
  235. .vtop = 0x20,
  236. .krf = 0x04,
  237. .agc_targ_val = 0x2d,
  238. };
  239. static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
  240. .i2c_addr = 0x10, /* 0x20 */
  241. .xtal = 28800000,
  242. .ts_mode = 0,
  243. .spec_inv = 0,
  244. .if_dvbt = 4570000,
  245. .vtop = 0x3f,
  246. .krf = 0x04,
  247. .agc_targ_val = 0x3e,
  248. };
  249. static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
  250. {
  251. int ret;
  252. struct rtl28xxu_priv *priv = adap->dev->priv;
  253. u8 buf[1];
  254. struct rtl2830_config *rtl2830_config;
  255. /* open RTL2831U/RTL2830 I2C gate */
  256. struct rtl28xxu_req req_gate = { 0x0120, 0x0011, 0x0001, "\x08" };
  257. /* for MT2060 tuner probe */
  258. struct rtl28xxu_req req_mt2060 = { 0x00c0, CMD_I2C_RD, 1, buf };
  259. /* for QT1010 tuner probe */
  260. struct rtl28xxu_req req_qt1010 = { 0x0fc4, CMD_I2C_RD, 1, buf };
  261. deb_info("%s:\n", __func__);
  262. /*
  263. * RTL2831U GPIOs
  264. * =========================================================
  265. * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
  266. * GPIO2 | LED | 0 off | 1 on |
  267. * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
  268. */
  269. /* GPIO direction */
  270. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
  271. if (ret)
  272. goto err;
  273. /* enable as output GPIO0, GPIO2, GPIO4 */
  274. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
  275. if (ret)
  276. goto err;
  277. /*
  278. * Probe used tuner. We need to know used tuner before demod attach
  279. * since there is some demod params needed to set according to tuner.
  280. */
  281. /* demod needs some time to wake up */
  282. msleep(20);
  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_adap[0].fe = dvb_attach(rtl2830_attach, rtl2830_config,
  322. &adap->dev->i2c_adap);
  323. if (adap->fe_adap[0].fe == 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;
  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_adap[0].fe);
  424. switch (priv->tuner) {
  425. case TUNER_RTL2830_QT1010:
  426. fe = dvb_attach(qt1010_attach, adap->fe_adap[0].fe,
  427. rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
  428. break;
  429. case TUNER_RTL2830_MT2060:
  430. fe = dvb_attach(mt2060_attach, adap->fe_adap[0].fe,
  431. rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
  432. 1220);
  433. break;
  434. case TUNER_RTL2830_MXL5005S:
  435. fe = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
  436. rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
  437. break;
  438. default:
  439. fe = NULL;
  440. err("unknown tuner=%d", priv->tuner);
  441. }
  442. if (fe == NULL) {
  443. ret = -ENODEV;
  444. goto err;
  445. }
  446. return 0;
  447. err:
  448. deb_info("%s: failed=%d\n", __func__, ret);
  449. return ret;
  450. }
  451. static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
  452. {
  453. int ret;
  454. struct rtl28xxu_priv *priv = adap->dev->priv;
  455. struct dvb_frontend *fe;
  456. deb_info("%s:\n", __func__);
  457. switch (priv->tuner) {
  458. case TUNER_RTL2832_FC2580:
  459. /* TODO: */
  460. fe = NULL;
  461. break;
  462. default:
  463. fe = NULL;
  464. err("unknown tuner=%d", priv->tuner);
  465. }
  466. if (fe == NULL) {
  467. ret = -ENODEV;
  468. goto err;
  469. }
  470. return 0;
  471. err:
  472. deb_info("%s: failed=%d\n", __func__, ret);
  473. return ret;
  474. }
  475. static int rtl28xxu_streaming_ctrl(struct dvb_usb_adapter *adap , int onoff)
  476. {
  477. int ret;
  478. u8 buf[2], gpio;
  479. deb_info("%s: onoff=%d\n", __func__, onoff);
  480. ret = rtl2831_rd_reg(adap->dev, SYS_GPIO_OUT_VAL, &gpio);
  481. if (ret)
  482. goto err;
  483. if (onoff) {
  484. buf[0] = 0x00;
  485. buf[1] = 0x00;
  486. gpio |= 0x04; /* LED on */
  487. } else {
  488. buf[0] = 0x10; /* stall EPA */
  489. buf[1] = 0x02; /* reset EPA */
  490. gpio &= (~0x04); /* LED off */
  491. }
  492. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_VAL, gpio);
  493. if (ret)
  494. goto err;
  495. ret = rtl2831_wr_regs(adap->dev, USB_EPA_CTL, buf, 2);
  496. if (ret)
  497. goto err;
  498. return ret;
  499. err:
  500. deb_info("%s: failed=%d\n", __func__, ret);
  501. return ret;
  502. }
  503. static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff)
  504. {
  505. int ret;
  506. u8 gpio, sys0;
  507. deb_info("%s: onoff=%d\n", __func__, onoff);
  508. /* demod adc */
  509. ret = rtl2831_rd_reg(d, SYS_SYS0, &sys0);
  510. if (ret)
  511. goto err;
  512. /* tuner power, read GPIOs */
  513. ret = rtl2831_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
  514. if (ret)
  515. goto err;
  516. deb_info("%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
  517. if (onoff) {
  518. gpio |= 0x01; /* GPIO0 = 1 */
  519. gpio &= (~0x10); /* GPIO4 = 0 */
  520. sys0 = sys0 & 0x0f;
  521. sys0 |= 0xe0;
  522. } else {
  523. gpio &= (~0x01); /* GPIO0 = 0 */
  524. gpio |= 0x10; /* GPIO4 = 1 */
  525. sys0 = sys0 & (~0xc0);
  526. }
  527. deb_info("%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
  528. /* demod adc */
  529. ret = rtl2831_wr_reg(d, SYS_SYS0, sys0);
  530. if (ret)
  531. goto err;
  532. /* tuner power, write GPIOs */
  533. ret = rtl2831_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
  534. if (ret)
  535. goto err;
  536. return ret;
  537. err:
  538. deb_info("%s: failed=%d\n", __func__, ret);
  539. return ret;
  540. }
  541. static int rtl2831u_rc_query(struct dvb_usb_device *d)
  542. {
  543. int ret, i;
  544. struct rtl28xxu_priv *priv = d->priv;
  545. u8 buf[5];
  546. u32 rc_code;
  547. struct rtl28xxu_reg_val rc_nec_tab[] = {
  548. { 0x3033, 0x80 },
  549. { 0x3020, 0x43 },
  550. { 0x3021, 0x16 },
  551. { 0x3022, 0x16 },
  552. { 0x3023, 0x5a },
  553. { 0x3024, 0x2d },
  554. { 0x3025, 0x16 },
  555. { 0x3026, 0x01 },
  556. { 0x3028, 0xb0 },
  557. { 0x3029, 0x04 },
  558. { 0x302c, 0x88 },
  559. { 0x302e, 0x13 },
  560. { 0x3030, 0xdf },
  561. { 0x3031, 0x05 },
  562. };
  563. /* init remote controller */
  564. if (!priv->rc_active) {
  565. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  566. ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
  567. rc_nec_tab[i].val);
  568. if (ret)
  569. goto err;
  570. }
  571. priv->rc_active = true;
  572. }
  573. ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
  574. if (ret)
  575. goto err;
  576. if (buf[4] & 0x01) {
  577. if (buf[2] == (u8) ~buf[3]) {
  578. if (buf[0] == (u8) ~buf[1]) {
  579. /* NEC standard (16 bit) */
  580. rc_code = buf[0] << 8 | buf[2];
  581. } else {
  582. /* NEC extended (24 bit) */
  583. rc_code = buf[0] << 16 |
  584. buf[1] << 8 | buf[2];
  585. }
  586. } else {
  587. /* NEC full (32 bit) */
  588. rc_code = buf[0] << 24 | buf[1] << 16 |
  589. buf[2] << 8 | buf[3];
  590. }
  591. rc_keydown(d->rc_dev, rc_code, 0);
  592. ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
  593. if (ret)
  594. goto err;
  595. /* repeated intentionally to avoid extra keypress */
  596. ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
  597. if (ret)
  598. goto err;
  599. }
  600. return ret;
  601. err:
  602. deb_info("%s: failed=%d\n", __func__, ret);
  603. return ret;
  604. }
  605. static int rtl2832u_rc_query(struct dvb_usb_device *d)
  606. {
  607. int ret, i;
  608. struct rtl28xxu_priv *priv = d->priv;
  609. u8 buf[128];
  610. int len;
  611. struct rtl28xxu_reg_val rc_nec_tab[] = {
  612. { IR_RX_CTRL, 0x20 },
  613. { IR_RX_BUF_CTRL, 0x80 },
  614. { IR_RX_IF, 0xff },
  615. { IR_RX_IE, 0xff },
  616. { IR_MAX_DURATION0, 0xd0 },
  617. { IR_MAX_DURATION1, 0x07 },
  618. { IR_IDLE_LEN0, 0xc0 },
  619. { IR_IDLE_LEN1, 0x00 },
  620. { IR_GLITCH_LEN, 0x03 },
  621. { IR_RX_CLK, 0x09 },
  622. { IR_RX_CFG, 0x1c },
  623. { IR_MAX_H_TOL_LEN, 0x1e },
  624. { IR_MAX_L_TOL_LEN, 0x1e },
  625. { IR_RX_CTRL, 0x80 },
  626. };
  627. /* init remote controller */
  628. if (!priv->rc_active) {
  629. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  630. ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
  631. rc_nec_tab[i].val);
  632. if (ret)
  633. goto err;
  634. }
  635. priv->rc_active = true;
  636. }
  637. ret = rtl2831_rd_reg(d, IR_RX_IF, &buf[0]);
  638. if (ret)
  639. goto err;
  640. if (buf[0] != 0x83)
  641. goto exit;
  642. ret = rtl2831_rd_reg(d, IR_RX_BC, &buf[0]);
  643. if (ret)
  644. goto err;
  645. len = buf[0];
  646. ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
  647. /* TODO: pass raw IR to Kernel IR decoder */
  648. ret = rtl2831_wr_reg(d, IR_RX_IF, 0x03);
  649. ret = rtl2831_wr_reg(d, IR_RX_BUF_CTRL, 0x80);
  650. ret = rtl2831_wr_reg(d, IR_RX_CTRL, 0x80);
  651. exit:
  652. return ret;
  653. err:
  654. deb_info("%s: failed=%d\n", __func__, ret);
  655. return ret;
  656. }
  657. enum rtl28xxu_usb_table_entry {
  658. RTL2831U_0BDA_2831,
  659. RTL2831U_14AA_0160,
  660. RTL2831U_14AA_0161,
  661. };
  662. static struct usb_device_id rtl28xxu_table[] = {
  663. /* RTL2831U */
  664. [RTL2831U_0BDA_2831] = {
  665. USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U)},
  666. [RTL2831U_14AA_0160] = {
  667. USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT)},
  668. [RTL2831U_14AA_0161] = {
  669. USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2)},
  670. /* RTL2832U */
  671. {} /* terminating entry */
  672. };
  673. MODULE_DEVICE_TABLE(usb, rtl28xxu_table);
  674. static struct dvb_usb_device_properties rtl28xxu_properties[] = {
  675. {
  676. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  677. .usb_ctrl = DEVICE_SPECIFIC,
  678. .no_reconnect = 1,
  679. .size_of_priv = sizeof(struct rtl28xxu_priv),
  680. .num_adapters = 1,
  681. .adapter = {
  682. {
  683. .num_frontends = 1,
  684. .fe = {
  685. {
  686. .frontend_attach = rtl2831u_frontend_attach,
  687. .tuner_attach = rtl2831u_tuner_attach,
  688. .streaming_ctrl = rtl28xxu_streaming_ctrl,
  689. .stream = {
  690. .type = USB_BULK,
  691. .count = 6,
  692. .endpoint = 0x81,
  693. .u = {
  694. .bulk = {
  695. .buffersize = 8*512,
  696. }
  697. }
  698. }
  699. }
  700. }
  701. }
  702. },
  703. .power_ctrl = rtl28xxu_power_ctrl,
  704. .rc.core = {
  705. .protocol = RC_TYPE_NEC,
  706. .module_name = "rtl28xxu",
  707. .rc_query = rtl2831u_rc_query,
  708. .rc_interval = 400,
  709. .allowed_protos = RC_TYPE_NEC,
  710. .rc_codes = RC_MAP_EMPTY,
  711. },
  712. .i2c_algo = &rtl28xxu_i2c_algo,
  713. .num_device_descs = 2,
  714. .devices = {
  715. {
  716. .name = "Realtek RTL2831U reference design",
  717. .warm_ids = {
  718. &rtl28xxu_table[RTL2831U_0BDA_2831],
  719. },
  720. },
  721. {
  722. .name = "Freecom USB2.0 DVB-T",
  723. .warm_ids = {
  724. &rtl28xxu_table[RTL2831U_14AA_0160],
  725. &rtl28xxu_table[RTL2831U_14AA_0161],
  726. },
  727. },
  728. }
  729. },
  730. {
  731. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  732. .usb_ctrl = DEVICE_SPECIFIC,
  733. .no_reconnect = 1,
  734. .size_of_priv = sizeof(struct rtl28xxu_priv),
  735. .num_adapters = 1,
  736. .adapter = {
  737. {
  738. .num_frontends = 1,
  739. .fe = {
  740. {
  741. .frontend_attach = rtl2832u_frontend_attach,
  742. .tuner_attach = rtl2832u_tuner_attach,
  743. .streaming_ctrl = rtl28xxu_streaming_ctrl,
  744. .stream = {
  745. .type = USB_BULK,
  746. .count = 6,
  747. .endpoint = 0x81,
  748. .u = {
  749. .bulk = {
  750. .buffersize = 8*512,
  751. }
  752. }
  753. }
  754. }
  755. }
  756. }
  757. },
  758. .power_ctrl = rtl28xxu_power_ctrl,
  759. .rc.core = {
  760. .protocol = RC_TYPE_NEC,
  761. .module_name = "rtl28xxu",
  762. .rc_query = rtl2832u_rc_query,
  763. .rc_interval = 400,
  764. .allowed_protos = RC_TYPE_NEC,
  765. .rc_codes = RC_MAP_EMPTY,
  766. },
  767. .i2c_algo = &rtl28xxu_i2c_algo,
  768. .num_device_descs = 0, /* disabled as no support for RTL2832 */
  769. .devices = {
  770. {
  771. .name = "Realtek RTL2832U reference design",
  772. },
  773. }
  774. },
  775. };
  776. static int rtl28xxu_probe(struct usb_interface *intf,
  777. const struct usb_device_id *id)
  778. {
  779. int ret, i;
  780. int properties_count = ARRAY_SIZE(rtl28xxu_properties);
  781. struct dvb_usb_device *d;
  782. struct usb_device *udev;
  783. bool found;
  784. deb_info("%s: interface=%d\n", __func__,
  785. intf->cur_altsetting->desc.bInterfaceNumber);
  786. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  787. return 0;
  788. /* Dynamic USB ID support. Replaces first device ID with current one .*/
  789. udev = interface_to_usbdev(intf);
  790. for (i = 0, found = false; i < ARRAY_SIZE(rtl28xxu_table) - 1; i++) {
  791. if (rtl28xxu_table[i].idVendor ==
  792. le16_to_cpu(udev->descriptor.idVendor) &&
  793. rtl28xxu_table[i].idProduct ==
  794. le16_to_cpu(udev->descriptor.idProduct)) {
  795. found = true;
  796. break;
  797. }
  798. }
  799. if (!found) {
  800. deb_info("%s: using dynamic ID %04x:%04x\n", __func__,
  801. le16_to_cpu(udev->descriptor.idVendor),
  802. le16_to_cpu(udev->descriptor.idProduct));
  803. rtl28xxu_properties[0].devices[0].warm_ids[0]->idVendor =
  804. le16_to_cpu(udev->descriptor.idVendor);
  805. rtl28xxu_properties[0].devices[0].warm_ids[0]->idProduct =
  806. le16_to_cpu(udev->descriptor.idProduct);
  807. }
  808. for (i = 0; i < properties_count; i++) {
  809. ret = dvb_usb_device_init(intf, &rtl28xxu_properties[i],
  810. THIS_MODULE, &d, adapter_nr);
  811. if (ret == 0 || ret != -ENODEV)
  812. break;
  813. }
  814. if (ret)
  815. goto err;
  816. /* init USB endpoints */
  817. ret = rtl2831_wr_reg(d, USB_SYSCTL_0, 0x09);
  818. if (ret)
  819. goto err;
  820. ret = rtl2831_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
  821. if (ret)
  822. goto err;
  823. ret = rtl2831_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
  824. if (ret)
  825. goto err;
  826. return ret;
  827. err:
  828. deb_info("%s: failed=%d\n", __func__, ret);
  829. return ret;
  830. }
  831. static struct usb_driver rtl28xxu_driver = {
  832. .name = "dvb_usb_rtl28xxu",
  833. .probe = rtl28xxu_probe,
  834. .disconnect = dvb_usb_device_exit,
  835. .id_table = rtl28xxu_table,
  836. };
  837. /* module stuff */
  838. static int __init rtl28xxu_module_init(void)
  839. {
  840. int ret;
  841. deb_info("%s:\n", __func__);
  842. ret = usb_register(&rtl28xxu_driver);
  843. if (ret)
  844. err("usb_register failed=%d", ret);
  845. return ret;
  846. }
  847. static void __exit rtl28xxu_module_exit(void)
  848. {
  849. deb_info("%s:\n", __func__);
  850. /* deregister this driver from the USB subsystem */
  851. usb_deregister(&rtl28xxu_driver);
  852. }
  853. module_init(rtl28xxu_module_init);
  854. module_exit(rtl28xxu_module_exit);
  855. MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
  856. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  857. MODULE_LICENSE("GPL");