rtl28xxu.c 32 KB

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