rtl28xxu.c 32 KB

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