friio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* DVB USB compliant Linux driver for the Friio USB2.0 ISDB-T receiver.
  2. *
  3. * Copyright (C) 2009 Akihiro Tsukada <tskd2@yahoo.co.jp>
  4. *
  5. * This module is based off the the gl861 and vp702x modules.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation, version 2.
  10. *
  11. * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
  12. */
  13. #include "friio.h"
  14. /* debug */
  15. int dvb_usb_friio_debug;
  16. module_param_named(debug, dvb_usb_friio_debug, int, 0644);
  17. MODULE_PARM_DESC(debug,
  18. "set debugging level (1=info,2=xfer,4=rc,8=fe (or-able))."
  19. DVB_USB_DEBUG_STATUS);
  20. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  21. /*
  22. * Indirect I2C access to the PLL via FE.
  23. * whole I2C protocol data to the PLL is sent via the FE's I2C register.
  24. * This is done by a control msg to the FE with the I2C data accompanied, and
  25. * a specific USB request number is assigned for that purpose.
  26. *
  27. * this func sends wbuf[1..] to the I2C register wbuf[0] at addr (= at FE).
  28. * TODO: refoctored, smarter i2c functions.
  29. */
  30. static int gl861_i2c_ctrlmsg_data(struct dvb_usb_device *d, u8 addr,
  31. u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
  32. {
  33. u16 index = wbuf[0]; /* must be JDVBT90502_2ND_I2C_REG(=0xFE) */
  34. u16 value = addr << (8 + 1);
  35. int wo = (rbuf == NULL || rlen == 0); /* write only */
  36. u8 req, type;
  37. deb_xfer("write to PLL:0x%02x via FE reg:0x%02x, len:%d\n",
  38. wbuf[1], wbuf[0], wlen - 1);
  39. if (wo && wlen >= 2) {
  40. req = GL861_REQ_I2C_DATA_CTRL_WRITE;
  41. type = GL861_WRITE;
  42. udelay(20);
  43. return usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
  44. req, type, value, index,
  45. &wbuf[1], wlen - 1, 2000);
  46. }
  47. deb_xfer("not supported ctrl-msg, aborting.");
  48. return -EINVAL;
  49. }
  50. /* normal I2C access (without extra data arguments).
  51. * write to the register wbuf[0] at I2C address addr with the value wbuf[1],
  52. * or read from the register wbuf[0].
  53. * register address can be 16bit (wbuf[2]<<8 | wbuf[0]) if wlen==3
  54. */
  55. static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
  56. u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
  57. {
  58. u16 index;
  59. u16 value = addr << (8 + 1);
  60. int wo = (rbuf == NULL || rlen == 0); /* write-only */
  61. u8 req, type;
  62. unsigned int pipe;
  63. /* special case for the indirect I2C access to the PLL via FE, */
  64. if (addr == friio_fe_config.demod_address &&
  65. wbuf[0] == JDVBT90502_2ND_I2C_REG)
  66. return gl861_i2c_ctrlmsg_data(d, addr, wbuf, wlen, rbuf, rlen);
  67. if (wo) {
  68. req = GL861_REQ_I2C_WRITE;
  69. type = GL861_WRITE;
  70. pipe = usb_sndctrlpipe(d->udev, 0);
  71. } else { /* rw */
  72. req = GL861_REQ_I2C_READ;
  73. type = GL861_READ;
  74. pipe = usb_rcvctrlpipe(d->udev, 0);
  75. }
  76. switch (wlen) {
  77. case 1:
  78. index = wbuf[0];
  79. break;
  80. case 2:
  81. index = wbuf[0];
  82. value = value + wbuf[1];
  83. break;
  84. case 3:
  85. /* special case for 16bit register-address */
  86. index = (wbuf[2] << 8) | wbuf[0];
  87. value = value + wbuf[1];
  88. break;
  89. default:
  90. deb_xfer("wlen = %x, aborting.", wlen);
  91. return -EINVAL;
  92. }
  93. msleep(1);
  94. return usb_control_msg(d->udev, pipe, req, type,
  95. value, index, rbuf, rlen, 2000);
  96. }
  97. /* I2C */
  98. static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  99. int num)
  100. {
  101. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  102. int i;
  103. if (num > 2)
  104. return -EINVAL;
  105. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  106. return -EAGAIN;
  107. for (i = 0; i < num; i++) {
  108. /* write/read request */
  109. if (i + 1 < num && (msg[i + 1].flags & I2C_M_RD)) {
  110. if (gl861_i2c_msg(d, msg[i].addr,
  111. msg[i].buf, msg[i].len,
  112. msg[i + 1].buf, msg[i + 1].len) < 0)
  113. break;
  114. i++;
  115. } else
  116. if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
  117. msg[i].len, NULL, 0) < 0)
  118. break;
  119. }
  120. mutex_unlock(&d->i2c_mutex);
  121. return i;
  122. }
  123. static u32 gl861_i2c_func(struct i2c_adapter *adapter)
  124. {
  125. return I2C_FUNC_I2C;
  126. }
  127. static int friio_ext_ctl(struct dvb_usb_adapter *adap,
  128. u32 sat_color, int lnb_on)
  129. {
  130. int i;
  131. int ret;
  132. struct i2c_msg msg;
  133. u8 *buf;
  134. u32 mask;
  135. u8 lnb = (lnb_on) ? FRIIO_CTL_LNB : 0;
  136. buf = kmalloc(2, GFP_KERNEL);
  137. if (!buf)
  138. return -ENOMEM;
  139. msg.addr = 0x00;
  140. msg.flags = 0;
  141. msg.len = 2;
  142. msg.buf = buf;
  143. buf[0] = 0x00;
  144. /* send 2bit header (&B10) */
  145. buf[1] = lnb | FRIIO_CTL_LED | FRIIO_CTL_STROBE;
  146. ret = gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
  147. buf[1] |= FRIIO_CTL_CLK;
  148. ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
  149. buf[1] = lnb | FRIIO_CTL_STROBE;
  150. ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
  151. buf[1] |= FRIIO_CTL_CLK;
  152. ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
  153. /* send 32bit(satur, R, G, B) data in serial */
  154. mask = 1 << 31;
  155. for (i = 0; i < 32; i++) {
  156. buf[1] = lnb | FRIIO_CTL_STROBE;
  157. if (sat_color & mask)
  158. buf[1] |= FRIIO_CTL_LED;
  159. ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
  160. buf[1] |= FRIIO_CTL_CLK;
  161. ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
  162. mask >>= 1;
  163. }
  164. /* set the strobe off */
  165. buf[1] = lnb;
  166. ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
  167. buf[1] |= FRIIO_CTL_CLK;
  168. ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
  169. kfree(buf);
  170. return (ret == 70);
  171. }
  172. static int friio_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff);
  173. /* TODO: move these init cmds to the FE's init routine? */
  174. static u8 streaming_init_cmds[][2] = {
  175. {0x33, 0x08},
  176. {0x37, 0x40},
  177. {0x3A, 0x1F},
  178. {0x3B, 0xFF},
  179. {0x3C, 0x1F},
  180. {0x3D, 0xFF},
  181. {0x38, 0x00},
  182. {0x35, 0x00},
  183. {0x39, 0x00},
  184. {0x36, 0x00},
  185. };
  186. static int cmdlen = sizeof(streaming_init_cmds) / 2;
  187. /*
  188. * Command sequence in this init function is a replay
  189. * of the captured USB commands from the Windows proprietary driver.
  190. */
  191. static int friio_initialize(struct dvb_usb_device *d)
  192. {
  193. int ret;
  194. int i;
  195. int retry = 0;
  196. u8 *rbuf, *wbuf;
  197. deb_info("%s called.\n", __func__);
  198. wbuf = kmalloc(3, GFP_KERNEL);
  199. if (!wbuf)
  200. return -ENOMEM;
  201. rbuf = kmalloc(2, GFP_KERNEL);
  202. if (!rbuf) {
  203. kfree(wbuf);
  204. return -ENOMEM;
  205. }
  206. /* use gl861_i2c_msg instead of gl861_i2c_xfer(), */
  207. /* because the i2c device is not set up yet. */
  208. wbuf[0] = 0x11;
  209. wbuf[1] = 0x02;
  210. ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
  211. if (ret < 0)
  212. goto error;
  213. msleep(2);
  214. wbuf[0] = 0x11;
  215. wbuf[1] = 0x00;
  216. ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
  217. if (ret < 0)
  218. goto error;
  219. msleep(1);
  220. /* following msgs should be in the FE's init code? */
  221. /* cmd sequence to identify the device type? (friio black/white) */
  222. wbuf[0] = 0x03;
  223. wbuf[1] = 0x80;
  224. /* can't use gl861_i2c_cmd, as the register-addr is 16bit(0x0100) */
  225. ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
  226. GL861_REQ_I2C_DATA_CTRL_WRITE, GL861_WRITE,
  227. 0x1200, 0x0100, wbuf, 2, 2000);
  228. if (ret < 0)
  229. goto error;
  230. msleep(2);
  231. wbuf[0] = 0x00;
  232. wbuf[2] = 0x01; /* reg.0x0100 */
  233. wbuf[1] = 0x00;
  234. ret = gl861_i2c_msg(d, 0x12 >> 1, wbuf, 3, rbuf, 2);
  235. /* my Friio White returns 0xffff. */
  236. if (ret < 0 || rbuf[0] != 0xff || rbuf[1] != 0xff)
  237. goto error;
  238. msleep(2);
  239. wbuf[0] = 0x03;
  240. wbuf[1] = 0x80;
  241. ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
  242. GL861_REQ_I2C_DATA_CTRL_WRITE, GL861_WRITE,
  243. 0x9000, 0x0100, wbuf, 2, 2000);
  244. if (ret < 0)
  245. goto error;
  246. msleep(2);
  247. wbuf[0] = 0x00;
  248. wbuf[2] = 0x01; /* reg.0x0100 */
  249. wbuf[1] = 0x00;
  250. ret = gl861_i2c_msg(d, 0x90 >> 1, wbuf, 3, rbuf, 2);
  251. /* my Friio White returns 0xffff again. */
  252. if (ret < 0 || rbuf[0] != 0xff || rbuf[1] != 0xff)
  253. goto error;
  254. msleep(1);
  255. restart:
  256. /* ============ start DEMOD init cmds ================== */
  257. /* read PLL status to clear the POR bit */
  258. wbuf[0] = JDVBT90502_2ND_I2C_REG;
  259. wbuf[1] = (FRIIO_PLL_ADDR << 1) + 1; /* +1 for reading */
  260. ret = gl861_i2c_msg(d, FRIIO_DEMOD_ADDR, wbuf, 2, NULL, 0);
  261. if (ret < 0)
  262. goto error;
  263. msleep(5);
  264. /* note: DEMODULATOR has 16bit register-address. */
  265. wbuf[0] = 0x00;
  266. wbuf[2] = 0x01; /* reg addr: 0x0100 */
  267. wbuf[1] = 0x00; /* val: not used */
  268. ret = gl861_i2c_msg(d, FRIIO_DEMOD_ADDR, wbuf, 3, rbuf, 1);
  269. if (ret < 0)
  270. goto error;
  271. /*
  272. msleep(1);
  273. wbuf[0] = 0x80;
  274. wbuf[1] = 0x00;
  275. ret = gl861_i2c_msg(d, FRIIO_DEMOD_ADDR, wbuf, 2, rbuf, 1);
  276. if (ret < 0)
  277. goto error;
  278. */
  279. if (rbuf[0] & 0x80) { /* still in PowerOnReset state? */
  280. if (++retry > 3) {
  281. deb_info("failed to get the correct FE demod status:0x%02x\n",
  282. rbuf[0]);
  283. goto error;
  284. }
  285. msleep(100);
  286. goto restart;
  287. }
  288. /* TODO: check return value in rbuf */
  289. /* =========== end DEMOD init cmds ===================== */
  290. msleep(1);
  291. wbuf[0] = 0x30;
  292. wbuf[1] = 0x04;
  293. ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
  294. if (ret < 0)
  295. goto error;
  296. msleep(2);
  297. /* following 2 cmds unnecessary? */
  298. wbuf[0] = 0x00;
  299. wbuf[1] = 0x01;
  300. ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
  301. if (ret < 0)
  302. goto error;
  303. wbuf[0] = 0x06;
  304. wbuf[1] = 0x0F;
  305. ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
  306. if (ret < 0)
  307. goto error;
  308. /* some streaming ctl cmds (maybe) */
  309. msleep(10);
  310. for (i = 0; i < cmdlen; i++) {
  311. ret = gl861_i2c_msg(d, 0x00, streaming_init_cmds[i], 2,
  312. NULL, 0);
  313. if (ret < 0)
  314. goto error;
  315. msleep(1);
  316. }
  317. msleep(20);
  318. /* change the LED color etc. */
  319. ret = friio_streaming_ctrl(&d->adapter[0], 0);
  320. if (ret < 0)
  321. goto error;
  322. return 0;
  323. error:
  324. kfree(wbuf);
  325. kfree(rbuf);
  326. deb_info("%s:ret == %d\n", __func__, ret);
  327. return -EIO;
  328. }
  329. /* Callbacks for DVB USB */
  330. static int friio_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  331. {
  332. int ret;
  333. deb_info("%s called.(%d)\n", __func__, onoff);
  334. /* set the LED color and saturation (and LNB on) */
  335. if (onoff)
  336. ret = friio_ext_ctl(adap, 0x6400ff64, 1);
  337. else
  338. ret = friio_ext_ctl(adap, 0x96ff00ff, 1);
  339. if (ret != 1) {
  340. deb_info("%s failed to send cmdx. ret==%d\n", __func__, ret);
  341. return -EREMOTEIO;
  342. }
  343. return 0;
  344. }
  345. static int friio_frontend_attach(struct dvb_usb_adapter *adap)
  346. {
  347. if (friio_initialize(adap->dev) < 0)
  348. return -EIO;
  349. adap->fe_adap[0].fe = jdvbt90502_attach(adap->dev);
  350. if (adap->fe_adap[0].fe == NULL)
  351. return -EIO;
  352. return 0;
  353. }
  354. /* DVB USB Driver stuff */
  355. static struct dvb_usb_device_properties friio_properties;
  356. static int friio_probe(struct usb_interface *intf,
  357. const struct usb_device_id *id)
  358. {
  359. struct dvb_usb_device *d;
  360. struct usb_host_interface *alt;
  361. int ret;
  362. if (intf->num_altsetting < GL861_ALTSETTING_COUNT)
  363. return -ENODEV;
  364. alt = usb_altnum_to_altsetting(intf, FRIIO_BULK_ALTSETTING);
  365. if (alt == NULL) {
  366. deb_rc("not alt found!\n");
  367. return -ENODEV;
  368. }
  369. ret = usb_set_interface(interface_to_usbdev(intf),
  370. alt->desc.bInterfaceNumber,
  371. alt->desc.bAlternateSetting);
  372. if (ret != 0) {
  373. deb_rc("failed to set alt-setting!\n");
  374. return ret;
  375. }
  376. ret = dvb_usb_device_init(intf, &friio_properties,
  377. THIS_MODULE, &d, adapter_nr);
  378. if (ret == 0)
  379. friio_streaming_ctrl(&d->adapter[0], 1);
  380. return ret;
  381. }
  382. struct jdvbt90502_config friio_fe_config = {
  383. .demod_address = FRIIO_DEMOD_ADDR,
  384. .pll_address = FRIIO_PLL_ADDR,
  385. };
  386. static struct i2c_algorithm gl861_i2c_algo = {
  387. .master_xfer = gl861_i2c_xfer,
  388. .functionality = gl861_i2c_func,
  389. };
  390. static struct usb_device_id friio_table[] = {
  391. { USB_DEVICE(USB_VID_774, USB_PID_FRIIO_WHITE) },
  392. { } /* Terminating entry */
  393. };
  394. MODULE_DEVICE_TABLE(usb, friio_table);
  395. static struct dvb_usb_device_properties friio_properties = {
  396. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  397. .usb_ctrl = DEVICE_SPECIFIC,
  398. .size_of_priv = 0,
  399. .num_adapters = 1,
  400. .adapter = {
  401. /* caps:0 => no pid filter, 188B TS packet */
  402. /* GL861 has a HW pid filter, but no info available. */
  403. {
  404. .num_frontends = 1,
  405. .fe = {{
  406. .caps = 0,
  407. .frontend_attach = friio_frontend_attach,
  408. .streaming_ctrl = friio_streaming_ctrl,
  409. .stream = {
  410. .type = USB_BULK,
  411. /* count <= MAX_NO_URBS_FOR_DATA_STREAM(10) */
  412. .count = 8,
  413. .endpoint = 0x01,
  414. .u = {
  415. /* GL861 has 6KB buf inside */
  416. .bulk = {
  417. .buffersize = 16384,
  418. }
  419. }
  420. },
  421. }},
  422. }
  423. },
  424. .i2c_algo = &gl861_i2c_algo,
  425. .num_device_descs = 1,
  426. .devices = {
  427. {
  428. .name = "774 Friio ISDB-T USB2.0",
  429. .cold_ids = { NULL },
  430. .warm_ids = { &friio_table[0], NULL },
  431. },
  432. }
  433. };
  434. static struct usb_driver friio_driver = {
  435. .name = "dvb_usb_friio",
  436. .probe = friio_probe,
  437. .disconnect = dvb_usb_device_exit,
  438. .id_table = friio_table,
  439. };
  440. module_usb_driver(friio_driver);
  441. MODULE_AUTHOR("Akihiro Tsukada <tskd2@yahoo.co.jp>");
  442. MODULE_DESCRIPTION("Driver for Friio ISDB-T USB2.0 Receiver");
  443. MODULE_VERSION("0.2");
  444. MODULE_LICENSE("GPL");