rtl28xxu.c 32 KB

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