rtl28xxu.c 33 KB

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