rtl28xxu.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. /*
  2. * Realtek RTL28xxU DVB USB driver
  3. *
  4. * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
  5. * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
  6. * Copyright (C) 2012 Thomas Mair <thomas.mair86@googlemail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include "rtl28xxu.h"
  23. #include "rtl2830.h"
  24. #include "rtl2832.h"
  25. #include "qt1010.h"
  26. #include "mt2060.h"
  27. #include "mxl5005s.h"
  28. #include "fc0012.h"
  29. #include "fc0013.h"
  30. #include "e4000.h"
  31. #include "fc2580.h"
  32. #include "tua9001.h"
  33. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  34. static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
  35. {
  36. int ret;
  37. unsigned int pipe;
  38. u8 requesttype;
  39. u8 *buf;
  40. buf = kmalloc(req->size, GFP_KERNEL);
  41. if (!buf) {
  42. ret = -ENOMEM;
  43. goto err;
  44. }
  45. if (req->index & CMD_WR_FLAG) {
  46. /* write */
  47. memcpy(buf, req->data, req->size);
  48. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  49. pipe = usb_sndctrlpipe(d->udev, 0);
  50. } else {
  51. /* read */
  52. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  53. pipe = usb_rcvctrlpipe(d->udev, 0);
  54. }
  55. ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
  56. req->index, buf, req->size, 1000);
  57. dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value,
  58. req->index, buf, req->size);
  59. if (ret > 0)
  60. ret = 0;
  61. /* read request, copy returned data to return buf */
  62. if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
  63. memcpy(req->data, buf, req->size);
  64. kfree(buf);
  65. if (ret)
  66. goto err;
  67. return ret;
  68. err:
  69. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  70. return ret;
  71. }
  72. static int rtl28xx_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  73. {
  74. struct rtl28xxu_req req;
  75. if (reg < 0x3000)
  76. req.index = CMD_USB_WR;
  77. else if (reg < 0x4000)
  78. req.index = CMD_SYS_WR;
  79. else
  80. req.index = CMD_IR_WR;
  81. req.value = reg;
  82. req.size = len;
  83. req.data = val;
  84. return rtl28xxu_ctrl_msg(d, &req);
  85. }
  86. static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  87. {
  88. struct rtl28xxu_req req;
  89. if (reg < 0x3000)
  90. req.index = CMD_USB_RD;
  91. else if (reg < 0x4000)
  92. req.index = CMD_SYS_RD;
  93. else
  94. req.index = CMD_IR_RD;
  95. req.value = reg;
  96. req.size = len;
  97. req.data = val;
  98. return rtl28xxu_ctrl_msg(d, &req);
  99. }
  100. static int rtl28xx_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
  101. {
  102. return rtl28xx_wr_regs(d, reg, &val, 1);
  103. }
  104. static int rtl28xx_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
  105. {
  106. return rtl2831_rd_regs(d, reg, val, 1);
  107. }
  108. /* I2C */
  109. static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  110. int num)
  111. {
  112. int ret;
  113. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  114. struct rtl28xxu_priv *priv = d->priv;
  115. struct rtl28xxu_req req;
  116. /*
  117. * It is not known which are real I2C bus xfer limits, but testing
  118. * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
  119. * TODO: find out RTL2832U lens
  120. */
  121. /*
  122. * I2C adapter logic looks rather complicated due to fact it handles
  123. * three different access methods. Those methods are;
  124. * 1) integrated demod access
  125. * 2) old I2C access
  126. * 3) new I2C access
  127. *
  128. * Used method is selected in order 1, 2, 3. Method 3 can handle all
  129. * requests but there is two reasons why not use it always;
  130. * 1) It is most expensive, usually two USB messages are needed
  131. * 2) At least RTL2831U does not support it
  132. *
  133. * Method 3 is needed in case of I2C write+read (typical register read)
  134. * where write is more than one byte.
  135. */
  136. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  137. return -EAGAIN;
  138. if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
  139. (msg[1].flags & I2C_M_RD)) {
  140. if (msg[0].len > 24 || msg[1].len > 24) {
  141. /* TODO: check msg[0].len max */
  142. ret = -EOPNOTSUPP;
  143. goto err_mutex_unlock;
  144. } else if (msg[0].addr == 0x10) {
  145. /* method 1 - integrated demod */
  146. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  147. req.index = CMD_DEMOD_RD | priv->page;
  148. req.size = msg[1].len;
  149. req.data = &msg[1].buf[0];
  150. ret = rtl28xxu_ctrl_msg(d, &req);
  151. } else if (msg[0].len < 2) {
  152. /* method 2 - old I2C */
  153. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  154. req.index = CMD_I2C_RD;
  155. req.size = msg[1].len;
  156. req.data = &msg[1].buf[0];
  157. ret = rtl28xxu_ctrl_msg(d, &req);
  158. } else {
  159. /* method 3 - new I2C */
  160. req.value = (msg[0].addr << 1);
  161. req.index = CMD_I2C_DA_WR;
  162. req.size = msg[0].len;
  163. req.data = msg[0].buf;
  164. ret = rtl28xxu_ctrl_msg(d, &req);
  165. if (ret)
  166. goto err_mutex_unlock;
  167. req.value = (msg[0].addr << 1);
  168. req.index = CMD_I2C_DA_RD;
  169. req.size = msg[1].len;
  170. req.data = msg[1].buf;
  171. ret = rtl28xxu_ctrl_msg(d, &req);
  172. }
  173. } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
  174. if (msg[0].len > 22) {
  175. /* TODO: check msg[0].len max */
  176. ret = -EOPNOTSUPP;
  177. goto err_mutex_unlock;
  178. } else if (msg[0].addr == 0x10) {
  179. /* method 1 - integrated demod */
  180. if (msg[0].buf[0] == 0x00) {
  181. /* save demod page for later demod access */
  182. priv->page = msg[0].buf[1];
  183. ret = 0;
  184. } else {
  185. req.value = (msg[0].buf[0] << 8) |
  186. (msg[0].addr << 1);
  187. req.index = CMD_DEMOD_WR | priv->page;
  188. req.size = msg[0].len-1;
  189. req.data = &msg[0].buf[1];
  190. ret = rtl28xxu_ctrl_msg(d, &req);
  191. }
  192. } else if (msg[0].len < 23) {
  193. /* method 2 - old I2C */
  194. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  195. req.index = CMD_I2C_WR;
  196. req.size = msg[0].len-1;
  197. req.data = &msg[0].buf[1];
  198. ret = rtl28xxu_ctrl_msg(d, &req);
  199. } else {
  200. /* method 3 - new I2C */
  201. req.value = (msg[0].addr << 1);
  202. req.index = CMD_I2C_DA_WR;
  203. req.size = msg[0].len;
  204. req.data = msg[0].buf;
  205. ret = rtl28xxu_ctrl_msg(d, &req);
  206. }
  207. } else {
  208. ret = -EINVAL;
  209. }
  210. err_mutex_unlock:
  211. mutex_unlock(&d->i2c_mutex);
  212. return ret ? ret : num;
  213. }
  214. static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
  215. {
  216. return I2C_FUNC_I2C;
  217. }
  218. static struct i2c_algorithm rtl28xxu_i2c_algo = {
  219. .master_xfer = rtl28xxu_i2c_xfer,
  220. .functionality = rtl28xxu_i2c_func,
  221. };
  222. static int rtl2832u_read_config(struct dvb_usb_device *d)
  223. {
  224. struct rtl28xxu_priv *priv = d_to_priv(d);
  225. int ret;
  226. u8 buf[2], val;
  227. /* open RTL2832U/RTL2832 I2C gate */
  228. struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
  229. /* close RTL2832U/RTL2832 I2C gate */
  230. struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
  231. /* tuner probes */
  232. struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
  233. struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
  234. struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
  235. struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
  236. struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
  237. struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
  238. struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
  239. struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
  240. struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
  241. struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
  242. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  243. ret = rtl28xx_rd_reg(d, SYS_GPIO_DIR, &val);
  244. if (ret)
  245. goto err;
  246. val &= 0xbf;
  247. ret = rtl28xx_wr_reg(d, SYS_GPIO_DIR, val);
  248. if (ret)
  249. goto err;
  250. /* enable as output GPIO3 and GPIO6 */
  251. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_EN, &val);
  252. if (ret)
  253. goto err;
  254. val |= 0x48;
  255. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_EN, val);
  256. if (ret)
  257. goto err;
  258. /*
  259. * Probe used tuner. We need to know used tuner before demod attach
  260. * since there is some demod params needed to set according to tuner.
  261. */
  262. /* open demod I2C gate */
  263. ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
  264. if (ret)
  265. goto err;
  266. priv->tuner_name = "NONE";
  267. /* check FC0012 ID register; reg=00 val=a1 */
  268. ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
  269. if (ret == 0 && buf[0] == 0xa1) {
  270. priv->tuner = TUNER_RTL2832_FC0012;
  271. priv->tuner_name = "FC0012";
  272. goto found;
  273. }
  274. /* check FC0013 ID register; reg=00 val=a3 */
  275. ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
  276. if (ret == 0 && buf[0] == 0xa3) {
  277. priv->tuner = TUNER_RTL2832_FC0013;
  278. priv->tuner_name = "FC0013";
  279. goto found;
  280. }
  281. /* check MT2266 ID register; reg=00 val=85 */
  282. ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
  283. if (ret == 0 && buf[0] == 0x85) {
  284. priv->tuner = TUNER_RTL2832_MT2266;
  285. priv->tuner_name = "MT2266";
  286. goto found;
  287. }
  288. /* check FC2580 ID register; reg=01 val=56 */
  289. ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
  290. if (ret == 0 && buf[0] == 0x56) {
  291. priv->tuner = TUNER_RTL2832_FC2580;
  292. priv->tuner_name = "FC2580";
  293. goto found;
  294. }
  295. /* check MT2063 ID register; reg=00 val=9e || 9c */
  296. ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
  297. if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
  298. priv->tuner = TUNER_RTL2832_MT2063;
  299. priv->tuner_name = "MT2063";
  300. goto found;
  301. }
  302. /* check MAX3543 ID register; reg=00 val=38 */
  303. ret = rtl28xxu_ctrl_msg(d, &req_max3543);
  304. if (ret == 0 && buf[0] == 0x38) {
  305. priv->tuner = TUNER_RTL2832_MAX3543;
  306. priv->tuner_name = "MAX3543";
  307. goto found;
  308. }
  309. /* check TUA9001 ID register; reg=7e val=2328 */
  310. ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
  311. if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
  312. priv->tuner = TUNER_RTL2832_TUA9001;
  313. priv->tuner_name = "TUA9001";
  314. goto found;
  315. }
  316. /* check MXL5007R ID register; reg=d9 val=14 */
  317. ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
  318. if (ret == 0 && buf[0] == 0x14) {
  319. priv->tuner = TUNER_RTL2832_MXL5007T;
  320. priv->tuner_name = "MXL5007T";
  321. goto found;
  322. }
  323. /* check E4000 ID register; reg=02 val=40 */
  324. ret = rtl28xxu_ctrl_msg(d, &req_e4000);
  325. if (ret == 0 && buf[0] == 0x40) {
  326. priv->tuner = TUNER_RTL2832_E4000;
  327. priv->tuner_name = "E4000";
  328. goto found;
  329. }
  330. /* check TDA18272 ID register; reg=00 val=c760 */
  331. ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
  332. if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
  333. priv->tuner = TUNER_RTL2832_TDA18272;
  334. priv->tuner_name = "TDA18272";
  335. goto found;
  336. }
  337. found:
  338. dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
  339. /* close demod I2C gate */
  340. ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
  341. if (ret < 0)
  342. goto err;
  343. return 0;
  344. err:
  345. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  346. return ret;
  347. }
  348. static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
  349. .i2c_addr = 0x10, /* 0x20 */
  350. .xtal = 28800000,
  351. .ts_mode = 0,
  352. .spec_inv = 1,
  353. .vtop = 0x20,
  354. .krf = 0x04,
  355. .agc_targ_val = 0x2d,
  356. };
  357. static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
  358. .i2c_addr = 0x10, /* 0x20 */
  359. .xtal = 28800000,
  360. .ts_mode = 0,
  361. .spec_inv = 1,
  362. .vtop = 0x20,
  363. .krf = 0x04,
  364. .agc_targ_val = 0x2d,
  365. };
  366. static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
  367. .i2c_addr = 0x10, /* 0x20 */
  368. .xtal = 28800000,
  369. .ts_mode = 0,
  370. .spec_inv = 0,
  371. .vtop = 0x3f,
  372. .krf = 0x04,
  373. .agc_targ_val = 0x3e,
  374. };
  375. static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
  376. {
  377. int ret;
  378. struct dvb_usb_device *d = adap_to_d(adap);
  379. struct rtl28xxu_priv *priv = d_to_priv(d);
  380. u8 buf[1];
  381. struct rtl2830_config *rtl2830_config;
  382. /* open RTL2831U/RTL2830 I2C gate */
  383. struct rtl28xxu_req req_gate = { 0x0120, 0x0011, 0x0001, "\x08" };
  384. /* for MT2060 tuner probe */
  385. struct rtl28xxu_req req_mt2060 = { 0x00c0, CMD_I2C_RD, 1, buf };
  386. /* for QT1010 tuner probe */
  387. struct rtl28xxu_req req_qt1010 = { 0x0fc4, CMD_I2C_RD, 1, buf };
  388. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  389. /*
  390. * RTL2831U GPIOs
  391. * =========================================================
  392. * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
  393. * GPIO2 | LED | 0 off | 1 on |
  394. * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
  395. */
  396. /* GPIO direction */
  397. ret = rtl28xx_wr_reg(d, SYS_GPIO_DIR, 0x0a);
  398. if (ret)
  399. goto err;
  400. /* enable as output GPIO0, GPIO2, GPIO4 */
  401. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_EN, 0x15);
  402. if (ret)
  403. goto err;
  404. /*
  405. * Probe used tuner. We need to know used tuner before demod attach
  406. * since there is some demod params needed to set according to tuner.
  407. */
  408. /* demod needs some time to wake up */
  409. msleep(20);
  410. /* open demod I2C gate */
  411. ret = rtl28xxu_ctrl_msg(d, &req_gate);
  412. if (ret)
  413. goto err;
  414. /* check QT1010 ID(?) register; reg=0f val=2c */
  415. ret = rtl28xxu_ctrl_msg(d, &req_qt1010);
  416. if (ret == 0 && buf[0] == 0x2c) {
  417. priv->tuner = TUNER_RTL2830_QT1010;
  418. rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
  419. dev_dbg(&d->udev->dev, "%s: QT1010\n", __func__);
  420. goto found;
  421. } else {
  422. dev_dbg(&d->udev->dev, "%s: QT1010 probe failed=%d - %02x\n",
  423. __func__, ret, buf[0]);
  424. }
  425. /* open demod I2C gate */
  426. ret = rtl28xxu_ctrl_msg(d, &req_gate);
  427. if (ret)
  428. goto err;
  429. /* check MT2060 ID register; reg=00 val=63 */
  430. ret = rtl28xxu_ctrl_msg(d, &req_mt2060);
  431. if (ret == 0 && buf[0] == 0x63) {
  432. priv->tuner = TUNER_RTL2830_MT2060;
  433. rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
  434. dev_dbg(&d->udev->dev, "%s: MT2060\n", __func__);
  435. goto found;
  436. } else {
  437. dev_dbg(&d->udev->dev, "%s: MT2060 probe failed=%d - %02x\n",
  438. __func__, ret, buf[0]);
  439. }
  440. /* assume MXL5005S */
  441. ret = 0;
  442. priv->tuner = TUNER_RTL2830_MXL5005S;
  443. rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
  444. dev_dbg(&d->udev->dev, "%s: MXL5005S\n", __func__);
  445. goto found;
  446. found:
  447. /* attach demodulator */
  448. adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config,
  449. &d->i2c_adap);
  450. if (adap->fe[0] == NULL) {
  451. ret = -ENODEV;
  452. goto err;
  453. }
  454. return ret;
  455. err:
  456. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  457. return ret;
  458. }
  459. static struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = {
  460. .i2c_addr = 0x10, /* 0x20 */
  461. .xtal = 28800000,
  462. .if_dvbt = 0,
  463. .tuner = TUNER_RTL2832_FC0012
  464. };
  465. static struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = {
  466. .i2c_addr = 0x10, /* 0x20 */
  467. .xtal = 28800000,
  468. .if_dvbt = 0,
  469. .tuner = TUNER_RTL2832_FC0013
  470. };
  471. static struct rtl2832_config rtl28xxu_rtl2832_tua9001_config = {
  472. .i2c_addr = 0x10, /* 0x20 */
  473. .xtal = 28800000,
  474. .tuner = TUNER_RTL2832_TUA9001,
  475. };
  476. static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
  477. int cmd, int arg)
  478. {
  479. int ret;
  480. u8 val;
  481. dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
  482. switch (cmd) {
  483. case FC_FE_CALLBACK_VHF_ENABLE:
  484. /* set output values */
  485. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
  486. if (ret)
  487. goto err;
  488. if (arg)
  489. val &= 0xbf; /* set GPIO6 low */
  490. else
  491. val |= 0x40; /* set GPIO6 high */
  492. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
  493. if (ret)
  494. goto err;
  495. break;
  496. default:
  497. ret = -EINVAL;
  498. goto err;
  499. }
  500. return 0;
  501. err:
  502. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  503. return ret;
  504. }
  505. static int rtl2832u_fc0013_tuner_callback(struct dvb_usb_device *d,
  506. int cmd, int arg)
  507. {
  508. /* TODO implement*/
  509. return 0;
  510. }
  511. static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
  512. int cmd, int arg)
  513. {
  514. int ret;
  515. u8 val;
  516. dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
  517. /*
  518. * CEN always enabled by hardware wiring
  519. * RESETN GPIO4
  520. * RXEN GPIO1
  521. */
  522. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
  523. if (ret < 0)
  524. goto err;
  525. switch (cmd) {
  526. case TUA9001_CMD_RESETN:
  527. if (arg)
  528. val |= (1 << 4);
  529. else
  530. val &= ~(1 << 4);
  531. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
  532. if (ret < 0)
  533. goto err;
  534. break;
  535. case TUA9001_CMD_RXEN:
  536. if (arg)
  537. val |= (1 << 1);
  538. else
  539. val &= ~(1 << 1);
  540. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
  541. if (ret < 0)
  542. goto err;
  543. break;
  544. }
  545. return 0;
  546. err:
  547. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  548. return ret;
  549. }
  550. static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
  551. {
  552. struct rtl28xxu_priv *priv = d->priv;
  553. switch (priv->tuner) {
  554. case TUNER_RTL2832_FC0012:
  555. return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
  556. case TUNER_RTL2832_FC0013:
  557. return rtl2832u_fc0013_tuner_callback(d, cmd, arg);
  558. case TUNER_RTL2832_TUA9001:
  559. return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
  560. default:
  561. break;
  562. }
  563. return -ENODEV;
  564. }
  565. static int rtl2832u_frontend_callback(void *adapter_priv, int component,
  566. int cmd, int arg)
  567. {
  568. struct i2c_adapter *adap = adapter_priv;
  569. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  570. switch (component) {
  571. case DVB_FRONTEND_COMPONENT_TUNER:
  572. return rtl2832u_tuner_callback(d, cmd, arg);
  573. default:
  574. break;
  575. }
  576. return -EINVAL;
  577. }
  578. static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
  579. {
  580. int ret;
  581. struct dvb_usb_device *d = adap_to_d(adap);
  582. struct rtl28xxu_priv *priv = d_to_priv(d);
  583. struct rtl2832_config *rtl2832_config;
  584. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  585. switch (priv->tuner) {
  586. case TUNER_RTL2832_FC0012:
  587. rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
  588. break;
  589. case TUNER_RTL2832_FC0013:
  590. rtl2832_config = &rtl28xxu_rtl2832_fc0013_config;
  591. break;
  592. case TUNER_RTL2832_FC2580:
  593. /* FIXME: do not abuse fc0012 settings */
  594. rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
  595. break;
  596. case TUNER_RTL2832_TUA9001:
  597. rtl2832_config = &rtl28xxu_rtl2832_tua9001_config;
  598. break;
  599. case TUNER_RTL2832_E4000:
  600. /* FIXME: do not abuse fc0012 settings */
  601. rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
  602. break;
  603. default:
  604. dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
  605. KBUILD_MODNAME, priv->tuner_name);
  606. ret = -ENODEV;
  607. goto err;
  608. }
  609. /* attach demodulator */
  610. adap->fe[0] = dvb_attach(rtl2832_attach, rtl2832_config, &d->i2c_adap);
  611. if (!adap->fe[0]) {
  612. ret = -ENODEV;
  613. goto err;
  614. }
  615. /* set fe callback */
  616. adap->fe[0]->callback = rtl2832u_frontend_callback;
  617. return 0;
  618. err:
  619. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  620. return ret;
  621. }
  622. static struct qt1010_config rtl28xxu_qt1010_config = {
  623. .i2c_address = 0x62, /* 0xc4 */
  624. };
  625. static struct mt2060_config rtl28xxu_mt2060_config = {
  626. .i2c_address = 0x60, /* 0xc0 */
  627. .clock_out = 0,
  628. };
  629. static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
  630. .i2c_address = 0x63, /* 0xc6 */
  631. .if_freq = IF_FREQ_4570000HZ,
  632. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  633. .agc_mode = MXL_SINGLE_AGC,
  634. .tracking_filter = MXL_TF_C_H,
  635. .rssi_enable = MXL_RSSI_ENABLE,
  636. .cap_select = MXL_CAP_SEL_ENABLE,
  637. .div_out = MXL_DIV_OUT_4,
  638. .clock_out = MXL_CLOCK_OUT_DISABLE,
  639. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  640. .top = MXL5005S_TOP_25P2,
  641. .mod_mode = MXL_DIGITAL_MODE,
  642. .if_mode = MXL_ZERO_IF,
  643. .AgcMasterByte = 0x00,
  644. };
  645. static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
  646. {
  647. int ret;
  648. struct dvb_usb_device *d = adap_to_d(adap);
  649. struct rtl28xxu_priv *priv = d_to_priv(d);
  650. struct i2c_adapter *rtl2830_tuner_i2c;
  651. struct dvb_frontend *fe;
  652. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  653. /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
  654. rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
  655. switch (priv->tuner) {
  656. case TUNER_RTL2830_QT1010:
  657. fe = dvb_attach(qt1010_attach, adap->fe[0],
  658. rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
  659. break;
  660. case TUNER_RTL2830_MT2060:
  661. fe = dvb_attach(mt2060_attach, adap->fe[0],
  662. rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
  663. 1220);
  664. break;
  665. case TUNER_RTL2830_MXL5005S:
  666. fe = dvb_attach(mxl5005s_attach, adap->fe[0],
  667. rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
  668. break;
  669. default:
  670. fe = NULL;
  671. dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
  672. priv->tuner);
  673. }
  674. if (fe == NULL) {
  675. ret = -ENODEV;
  676. goto err;
  677. }
  678. return 0;
  679. err:
  680. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  681. return ret;
  682. }
  683. static const struct e4000_config rtl2832u_e4000_config = {
  684. .i2c_addr = 0x64,
  685. .clock = 28800000,
  686. };
  687. static const struct fc2580_config rtl2832u_fc2580_config = {
  688. .i2c_addr = 0x56,
  689. .clock = 16384000,
  690. };
  691. static struct tua9001_config rtl2832u_tua9001_config = {
  692. .i2c_addr = 0x60,
  693. };
  694. static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
  695. {
  696. int ret;
  697. struct dvb_usb_device *d = adap_to_d(adap);
  698. struct rtl28xxu_priv *priv = d_to_priv(d);
  699. struct dvb_frontend *fe;
  700. u8 val;
  701. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  702. switch (priv->tuner) {
  703. case TUNER_RTL2832_FC0012:
  704. fe = dvb_attach(fc0012_attach, adap->fe[0],
  705. &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
  706. /* since fc0012 includs reading the signal strength delegate
  707. * that to the tuner driver */
  708. adap->fe[0]->ops.read_signal_strength =
  709. adap->fe[0]->ops.tuner_ops.get_rf_strength;
  710. return 0;
  711. break;
  712. case TUNER_RTL2832_FC0013:
  713. fe = dvb_attach(fc0013_attach, adap->fe[0],
  714. &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
  715. /* fc0013 also supports signal strength reading */
  716. adap->fe[0]->ops.read_signal_strength =
  717. adap->fe[0]->ops.tuner_ops.get_rf_strength;
  718. return 0;
  719. case TUNER_RTL2832_E4000:
  720. fe = dvb_attach(e4000_attach, adap->fe[0], &d->i2c_adap,
  721. &rtl2832u_e4000_config);
  722. break;
  723. case TUNER_RTL2832_FC2580:
  724. fe = dvb_attach(fc2580_attach, adap->fe[0], &d->i2c_adap,
  725. &rtl2832u_fc2580_config);
  726. break;
  727. case TUNER_RTL2832_TUA9001:
  728. /* enable GPIO1 and GPIO4 as output */
  729. ret = rtl28xx_rd_reg(d, SYS_GPIO_DIR, &val);
  730. if (ret < 0)
  731. goto err;
  732. val &= ~(1 << 1);
  733. val &= ~(1 << 4);
  734. ret = rtl28xx_wr_reg(d, SYS_GPIO_DIR, val);
  735. if (ret < 0)
  736. goto err;
  737. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_EN, &val);
  738. if (ret < 0)
  739. goto err;
  740. val |= (1 << 1);
  741. val |= (1 << 4);
  742. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_EN, val);
  743. if (ret < 0)
  744. goto err;
  745. fe = dvb_attach(tua9001_attach, adap->fe[0], &d->i2c_adap,
  746. &rtl2832u_tua9001_config);
  747. break;
  748. default:
  749. fe = NULL;
  750. dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
  751. priv->tuner);
  752. }
  753. if (fe == NULL) {
  754. ret = -ENODEV;
  755. goto err;
  756. }
  757. return 0;
  758. err:
  759. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  760. return ret;
  761. }
  762. static int rtl28xxu_init(struct dvb_usb_device *d)
  763. {
  764. int ret;
  765. u8 val;
  766. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  767. /* init USB endpoints */
  768. ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
  769. if (ret)
  770. goto err;
  771. /* enable DMA and Full Packet Mode*/
  772. val |= 0x09;
  773. ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
  774. if (ret)
  775. goto err;
  776. /* set EPA maximum packet size to 0x0200 */
  777. ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
  778. if (ret)
  779. goto err;
  780. /* change EPA FIFO length */
  781. ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
  782. if (ret)
  783. goto err;
  784. return ret;
  785. err:
  786. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  787. return ret;
  788. }
  789. static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
  790. {
  791. int ret;
  792. u8 gpio, sys0, epa_ctl[2];
  793. dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
  794. /* demod adc */
  795. ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
  796. if (ret)
  797. goto err;
  798. /* tuner power, read GPIOs */
  799. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
  800. if (ret)
  801. goto err;
  802. dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
  803. sys0, gpio);
  804. if (onoff) {
  805. gpio |= 0x01; /* GPIO0 = 1 */
  806. gpio &= (~0x10); /* GPIO4 = 0 */
  807. gpio |= 0x04; /* GPIO2 = 1, LED on */
  808. sys0 = sys0 & 0x0f;
  809. sys0 |= 0xe0;
  810. epa_ctl[0] = 0x00; /* clear stall */
  811. epa_ctl[1] = 0x00; /* clear reset */
  812. } else {
  813. gpio &= (~0x01); /* GPIO0 = 0 */
  814. gpio |= 0x10; /* GPIO4 = 1 */
  815. gpio &= (~0x04); /* GPIO2 = 1, LED off */
  816. sys0 = sys0 & (~0xc0);
  817. epa_ctl[0] = 0x10; /* set stall */
  818. epa_ctl[1] = 0x02; /* set reset */
  819. }
  820. dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
  821. sys0, gpio);
  822. /* demod adc */
  823. ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
  824. if (ret)
  825. goto err;
  826. /* tuner power, write GPIOs */
  827. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
  828. if (ret)
  829. goto err;
  830. /* streaming EP: stall & reset */
  831. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
  832. if (ret)
  833. goto err;
  834. if (onoff)
  835. usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
  836. return ret;
  837. err:
  838. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  839. return ret;
  840. }
  841. static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
  842. {
  843. int ret;
  844. u8 val;
  845. dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
  846. if (onoff) {
  847. /* set output values */
  848. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
  849. if (ret)
  850. goto err;
  851. val |= 0x08;
  852. val &= 0xef;
  853. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
  854. if (ret)
  855. goto err;
  856. /* demod_ctl_1 */
  857. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL1, &val);
  858. if (ret)
  859. goto err;
  860. val &= 0xef;
  861. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL1, val);
  862. if (ret)
  863. goto err;
  864. /* demod control */
  865. /* PLL enable */
  866. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
  867. if (ret)
  868. goto err;
  869. /* bit 7 to 1 */
  870. val |= 0x80;
  871. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
  872. if (ret)
  873. goto err;
  874. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
  875. if (ret)
  876. goto err;
  877. val |= 0x20;
  878. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
  879. if (ret)
  880. goto err;
  881. mdelay(5);
  882. /*enable ADC_Q and ADC_I */
  883. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
  884. if (ret)
  885. goto err;
  886. val |= 0x48;
  887. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
  888. if (ret)
  889. goto err;
  890. /* streaming EP: clear stall & reset */
  891. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
  892. if (ret)
  893. goto err;
  894. ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
  895. if (ret)
  896. goto err;
  897. } else {
  898. /* demod_ctl_1 */
  899. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL1, &val);
  900. if (ret)
  901. goto err;
  902. val |= 0x0c;
  903. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL1, val);
  904. if (ret)
  905. goto err;
  906. /* set output values */
  907. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
  908. if (ret)
  909. goto err;
  910. val |= 0x10;
  911. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
  912. if (ret)
  913. goto err;
  914. /* demod control */
  915. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
  916. if (ret)
  917. goto err;
  918. val &= 0x37;
  919. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
  920. if (ret)
  921. goto err;
  922. /* streaming EP: set stall & reset */
  923. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
  924. if (ret)
  925. goto err;
  926. }
  927. return ret;
  928. err:
  929. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  930. return ret;
  931. }
  932. static int rtl2831u_rc_query(struct dvb_usb_device *d)
  933. {
  934. int ret, i;
  935. struct rtl28xxu_priv *priv = d->priv;
  936. u8 buf[5];
  937. u32 rc_code;
  938. struct rtl28xxu_reg_val rc_nec_tab[] = {
  939. { 0x3033, 0x80 },
  940. { 0x3020, 0x43 },
  941. { 0x3021, 0x16 },
  942. { 0x3022, 0x16 },
  943. { 0x3023, 0x5a },
  944. { 0x3024, 0x2d },
  945. { 0x3025, 0x16 },
  946. { 0x3026, 0x01 },
  947. { 0x3028, 0xb0 },
  948. { 0x3029, 0x04 },
  949. { 0x302c, 0x88 },
  950. { 0x302e, 0x13 },
  951. { 0x3030, 0xdf },
  952. { 0x3031, 0x05 },
  953. };
  954. /* init remote controller */
  955. if (!priv->rc_active) {
  956. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  957. ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
  958. rc_nec_tab[i].val);
  959. if (ret)
  960. goto err;
  961. }
  962. priv->rc_active = true;
  963. }
  964. ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
  965. if (ret)
  966. goto err;
  967. if (buf[4] & 0x01) {
  968. if (buf[2] == (u8) ~buf[3]) {
  969. if (buf[0] == (u8) ~buf[1]) {
  970. /* NEC standard (16 bit) */
  971. rc_code = buf[0] << 8 | buf[2];
  972. } else {
  973. /* NEC extended (24 bit) */
  974. rc_code = buf[0] << 16 |
  975. buf[1] << 8 | buf[2];
  976. }
  977. } else {
  978. /* NEC full (32 bit) */
  979. rc_code = buf[0] << 24 | buf[1] << 16 |
  980. buf[2] << 8 | buf[3];
  981. }
  982. rc_keydown(d->rc_dev, rc_code, 0);
  983. ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
  984. if (ret)
  985. goto err;
  986. /* repeated intentionally to avoid extra keypress */
  987. ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
  988. if (ret)
  989. goto err;
  990. }
  991. return ret;
  992. err:
  993. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  994. return ret;
  995. }
  996. static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
  997. struct dvb_usb_rc *rc)
  998. {
  999. rc->map_name = RC_MAP_EMPTY;
  1000. rc->allowed_protos = RC_TYPE_NEC;
  1001. rc->query = rtl2831u_rc_query;
  1002. rc->interval = 400;
  1003. return 0;
  1004. }
  1005. static int rtl2832u_rc_query(struct dvb_usb_device *d)
  1006. {
  1007. int ret, i;
  1008. struct rtl28xxu_priv *priv = d->priv;
  1009. u8 buf[128];
  1010. int len;
  1011. struct rtl28xxu_reg_val rc_nec_tab[] = {
  1012. { IR_RX_CTRL, 0x20 },
  1013. { IR_RX_BUF_CTRL, 0x80 },
  1014. { IR_RX_IF, 0xff },
  1015. { IR_RX_IE, 0xff },
  1016. { IR_MAX_DURATION0, 0xd0 },
  1017. { IR_MAX_DURATION1, 0x07 },
  1018. { IR_IDLE_LEN0, 0xc0 },
  1019. { IR_IDLE_LEN1, 0x00 },
  1020. { IR_GLITCH_LEN, 0x03 },
  1021. { IR_RX_CLK, 0x09 },
  1022. { IR_RX_CFG, 0x1c },
  1023. { IR_MAX_H_TOL_LEN, 0x1e },
  1024. { IR_MAX_L_TOL_LEN, 0x1e },
  1025. { IR_RX_CTRL, 0x80 },
  1026. };
  1027. /* init remote controller */
  1028. if (!priv->rc_active) {
  1029. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  1030. ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
  1031. rc_nec_tab[i].val);
  1032. if (ret)
  1033. goto err;
  1034. }
  1035. priv->rc_active = true;
  1036. }
  1037. ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
  1038. if (ret)
  1039. goto err;
  1040. if (buf[0] != 0x83)
  1041. goto exit;
  1042. ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
  1043. if (ret)
  1044. goto err;
  1045. len = buf[0];
  1046. ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
  1047. /* TODO: pass raw IR to Kernel IR decoder */
  1048. ret = rtl28xx_wr_reg(d, IR_RX_IF, 0x03);
  1049. ret = rtl28xx_wr_reg(d, IR_RX_BUF_CTRL, 0x80);
  1050. ret = rtl28xx_wr_reg(d, IR_RX_CTRL, 0x80);
  1051. exit:
  1052. return ret;
  1053. err:
  1054. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  1055. return ret;
  1056. }
  1057. static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
  1058. struct dvb_usb_rc *rc)
  1059. {
  1060. rc->map_name = RC_MAP_EMPTY;
  1061. rc->allowed_protos = RC_TYPE_NEC;
  1062. rc->query = rtl2832u_rc_query;
  1063. rc->interval = 400;
  1064. return 0;
  1065. }
  1066. static const struct dvb_usb_device_properties rtl2831u_props = {
  1067. .driver_name = KBUILD_MODNAME,
  1068. .owner = THIS_MODULE,
  1069. .adapter_nr = adapter_nr,
  1070. .size_of_priv = sizeof(struct rtl28xxu_priv),
  1071. .power_ctrl = rtl2831u_power_ctrl,
  1072. .i2c_algo = &rtl28xxu_i2c_algo,
  1073. .frontend_attach = rtl2831u_frontend_attach,
  1074. .tuner_attach = rtl2831u_tuner_attach,
  1075. .init = rtl28xxu_init,
  1076. .get_rc_config = rtl2831u_get_rc_config,
  1077. .num_adapters = 1,
  1078. .adapter = {
  1079. {
  1080. .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
  1081. },
  1082. },
  1083. };
  1084. static const struct dvb_usb_device_properties rtl2832u_props = {
  1085. .driver_name = KBUILD_MODNAME,
  1086. .owner = THIS_MODULE,
  1087. .adapter_nr = adapter_nr,
  1088. .size_of_priv = sizeof(struct rtl28xxu_priv),
  1089. .power_ctrl = rtl2832u_power_ctrl,
  1090. .i2c_algo = &rtl28xxu_i2c_algo,
  1091. .read_config = rtl2832u_read_config,
  1092. .frontend_attach = rtl2832u_frontend_attach,
  1093. .tuner_attach = rtl2832u_tuner_attach,
  1094. .init = rtl28xxu_init,
  1095. .get_rc_config = rtl2832u_get_rc_config,
  1096. .num_adapters = 1,
  1097. .adapter = {
  1098. {
  1099. .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
  1100. },
  1101. },
  1102. };
  1103. static const struct usb_device_id rtl28xxu_id_table[] = {
  1104. { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
  1105. &rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
  1106. { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
  1107. &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
  1108. { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
  1109. &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
  1110. { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
  1111. &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
  1112. { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
  1113. &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
  1114. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
  1115. &rtl2832u_props, "Terratec Cinergy T Stick Black", NULL) },
  1116. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
  1117. &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
  1118. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
  1119. &rtl2832u_props, "NOXON DAB/DAB+ USB dongle", NULL) },
  1120. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
  1121. &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
  1122. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
  1123. &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
  1124. { }
  1125. };
  1126. MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
  1127. static struct usb_driver rtl28xxu_usb_driver = {
  1128. .name = KBUILD_MODNAME,
  1129. .id_table = rtl28xxu_id_table,
  1130. .probe = dvb_usbv2_probe,
  1131. .disconnect = dvb_usbv2_disconnect,
  1132. .suspend = dvb_usbv2_suspend,
  1133. .resume = dvb_usbv2_resume,
  1134. .reset_resume = dvb_usbv2_reset_resume,
  1135. .no_dynamic_id = 1,
  1136. .soft_unbind = 1,
  1137. };
  1138. module_usb_driver(rtl28xxu_usb_driver);
  1139. MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
  1140. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1141. MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
  1142. MODULE_LICENSE("GPL");