dib0700_core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /* Linux driver for devices based on the DiBcom DiB0700 USB bridge
  2. *
  3. * This program is free software; you can redistribute it and/or modify it
  4. * under the terms of the GNU General Public License as published by the Free
  5. * Software Foundation, version 2.
  6. *
  7. * Copyright (C) 2005-6 DiBcom, SA
  8. */
  9. #include "dib0700.h"
  10. /* debug */
  11. int dvb_usb_dib0700_debug;
  12. module_param_named(debug,dvb_usb_dib0700_debug, int, 0644);
  13. MODULE_PARM_DESC(debug, "set debugging level (1=info,2=fw,4=fwdata,8=data (or-able))." DVB_USB_DEBUG_STATUS);
  14. static int nb_packet_buffer_size = 21;
  15. module_param(nb_packet_buffer_size, int, 0644);
  16. MODULE_PARM_DESC(nb_packet_buffer_size,
  17. "Set the dib0700 driver data buffer size. This parameter "
  18. "corresponds to the number of TS packets. The actual size of "
  19. "the data buffer corresponds to this parameter "
  20. "multiplied by 188 (default: 21)");
  21. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  22. int dib0700_get_version(struct dvb_usb_device *d, u32 *hwversion,
  23. u32 *romversion, u32 *ramversion, u32 *fwtype)
  24. {
  25. struct dib0700_state *st = d->priv;
  26. int ret;
  27. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  28. err("could not acquire lock");
  29. return -EINTR;
  30. }
  31. ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
  32. REQUEST_GET_VERSION,
  33. USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
  34. st->buf, 16, USB_CTRL_GET_TIMEOUT);
  35. if (hwversion != NULL)
  36. *hwversion = (st->buf[0] << 24) | (st->buf[1] << 16) |
  37. (st->buf[2] << 8) | st->buf[3];
  38. if (romversion != NULL)
  39. *romversion = (st->buf[4] << 24) | (st->buf[5] << 16) |
  40. (st->buf[6] << 8) | st->buf[7];
  41. if (ramversion != NULL)
  42. *ramversion = (st->buf[8] << 24) | (st->buf[9] << 16) |
  43. (st->buf[10] << 8) | st->buf[11];
  44. if (fwtype != NULL)
  45. *fwtype = (st->buf[12] << 24) | (st->buf[13] << 16) |
  46. (st->buf[14] << 8) | st->buf[15];
  47. mutex_unlock(&d->usb_mutex);
  48. return ret;
  49. }
  50. /* expecting rx buffer: request data[0] data[1] ... data[2] */
  51. static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
  52. {
  53. int status;
  54. deb_data(">>> ");
  55. debug_dump(tx, txlen, deb_data);
  56. status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0),
  57. tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen,
  58. USB_CTRL_GET_TIMEOUT);
  59. if (status != txlen)
  60. deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
  61. return status < 0 ? status : 0;
  62. }
  63. /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
  64. int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
  65. {
  66. u16 index, value;
  67. int status;
  68. if (txlen < 2) {
  69. err("tx buffer length is smaller than 2. Makes no sense.");
  70. return -EINVAL;
  71. }
  72. if (txlen > 4) {
  73. err("tx buffer length is larger than 4. Not supported.");
  74. return -EINVAL;
  75. }
  76. deb_data(">>> ");
  77. debug_dump(tx,txlen,deb_data);
  78. value = ((txlen - 2) << 8) | tx[1];
  79. index = 0;
  80. if (txlen > 2)
  81. index |= (tx[2] << 8);
  82. if (txlen > 3)
  83. index |= tx[3];
  84. status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0],
  85. USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen,
  86. USB_CTRL_GET_TIMEOUT);
  87. if (status < 0)
  88. deb_info("ep 0 read error (status = %d)\n",status);
  89. deb_data("<<< ");
  90. debug_dump(rx, rxlen, deb_data);
  91. return status; /* length in case of success */
  92. }
  93. int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
  94. {
  95. struct dib0700_state *st = d->priv;
  96. int ret;
  97. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  98. err("could not acquire lock");
  99. return -EINTR;
  100. }
  101. st->buf[0] = REQUEST_SET_GPIO;
  102. st->buf[1] = gpio;
  103. st->buf[2] = ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6);
  104. ret = dib0700_ctrl_wr(d, st->buf, 3);
  105. mutex_unlock(&d->usb_mutex);
  106. return ret;
  107. }
  108. static int dib0700_set_usb_xfer_len(struct dvb_usb_device *d, u16 nb_ts_packets)
  109. {
  110. struct dib0700_state *st = d->priv;
  111. int ret;
  112. if (st->fw_version >= 0x10201) {
  113. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  114. err("could not acquire lock");
  115. return -EINTR;
  116. }
  117. st->buf[0] = REQUEST_SET_USB_XFER_LEN;
  118. st->buf[1] = (nb_ts_packets >> 8) & 0xff;
  119. st->buf[2] = nb_ts_packets & 0xff;
  120. deb_info("set the USB xfer len to %i Ts packet\n", nb_ts_packets);
  121. ret = dib0700_ctrl_wr(d, st->buf, 3);
  122. mutex_unlock(&d->usb_mutex);
  123. } else {
  124. deb_info("this firmware does not allow to change the USB xfer len\n");
  125. ret = -EIO;
  126. }
  127. return ret;
  128. }
  129. /*
  130. * I2C master xfer function (supported in 1.20 firmware)
  131. */
  132. static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg,
  133. int num)
  134. {
  135. /* The new i2c firmware messages are more reliable and in particular
  136. properly support i2c read calls not preceded by a write */
  137. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  138. struct dib0700_state *st = d->priv;
  139. uint8_t bus_mode = 1; /* 0=eeprom bus, 1=frontend bus */
  140. uint8_t gen_mode = 0; /* 0=master i2c, 1=gpio i2c */
  141. uint8_t en_start = 0;
  142. uint8_t en_stop = 0;
  143. int result, i;
  144. /* Ensure nobody else hits the i2c bus while we're sending our
  145. sequence of messages, (such as the remote control thread) */
  146. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  147. return -EINTR;
  148. for (i = 0; i < num; i++) {
  149. if (i == 0) {
  150. /* First message in the transaction */
  151. en_start = 1;
  152. } else if (!(msg[i].flags & I2C_M_NOSTART)) {
  153. /* Device supports repeated-start */
  154. en_start = 1;
  155. } else {
  156. /* Not the first packet and device doesn't support
  157. repeated start */
  158. en_start = 0;
  159. }
  160. if (i == (num - 1)) {
  161. /* Last message in the transaction */
  162. en_stop = 1;
  163. }
  164. if (msg[i].flags & I2C_M_RD) {
  165. /* Read request */
  166. u16 index, value;
  167. uint8_t i2c_dest;
  168. i2c_dest = (msg[i].addr << 1);
  169. value = ((en_start << 7) | (en_stop << 6) |
  170. (msg[i].len & 0x3F)) << 8 | i2c_dest;
  171. /* I2C ctrl + FE bus; */
  172. index = ((gen_mode << 6) & 0xC0) |
  173. ((bus_mode << 4) & 0x30);
  174. result = usb_control_msg(d->udev,
  175. usb_rcvctrlpipe(d->udev, 0),
  176. REQUEST_NEW_I2C_READ,
  177. USB_TYPE_VENDOR | USB_DIR_IN,
  178. value, index, msg[i].buf,
  179. msg[i].len,
  180. USB_CTRL_GET_TIMEOUT);
  181. if (result < 0) {
  182. deb_info("i2c read error (status = %d)\n", result);
  183. break;
  184. }
  185. deb_data("<<< ");
  186. debug_dump(msg[i].buf, msg[i].len, deb_data);
  187. } else {
  188. /* Write request */
  189. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  190. err("could not acquire lock");
  191. mutex_unlock(&d->i2c_mutex);
  192. return -EINTR;
  193. }
  194. st->buf[0] = REQUEST_NEW_I2C_WRITE;
  195. st->buf[1] = msg[i].addr << 1;
  196. st->buf[2] = (en_start << 7) | (en_stop << 6) |
  197. (msg[i].len & 0x3F);
  198. /* I2C ctrl + FE bus; */
  199. st->buf[3] = ((gen_mode << 6) & 0xC0) |
  200. ((bus_mode << 4) & 0x30);
  201. /* The Actual i2c payload */
  202. memcpy(&st->buf[4], msg[i].buf, msg[i].len);
  203. deb_data(">>> ");
  204. debug_dump(st->buf, msg[i].len + 4, deb_data);
  205. result = usb_control_msg(d->udev,
  206. usb_sndctrlpipe(d->udev, 0),
  207. REQUEST_NEW_I2C_WRITE,
  208. USB_TYPE_VENDOR | USB_DIR_OUT,
  209. 0, 0, st->buf, msg[i].len + 4,
  210. USB_CTRL_GET_TIMEOUT);
  211. mutex_unlock(&d->usb_mutex);
  212. if (result < 0) {
  213. deb_info("i2c write error (status = %d)\n", result);
  214. break;
  215. }
  216. }
  217. }
  218. mutex_unlock(&d->i2c_mutex);
  219. return i;
  220. }
  221. /*
  222. * I2C master xfer function (pre-1.20 firmware)
  223. */
  224. static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
  225. struct i2c_msg *msg, int num)
  226. {
  227. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  228. struct dib0700_state *st = d->priv;
  229. int i,len;
  230. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  231. return -EINTR;
  232. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  233. err("could not acquire lock");
  234. mutex_unlock(&d->i2c_mutex);
  235. return -EINTR;
  236. }
  237. for (i = 0; i < num; i++) {
  238. /* fill in the address */
  239. st->buf[1] = msg[i].addr << 1;
  240. /* fill the buffer */
  241. memcpy(&st->buf[2], msg[i].buf, msg[i].len);
  242. /* write/read request */
  243. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  244. st->buf[0] = REQUEST_I2C_READ;
  245. st->buf[1] |= 1;
  246. /* special thing in the current firmware: when length is zero the read-failed */
  247. len = dib0700_ctrl_rd(d, st->buf, msg[i].len + 2,
  248. msg[i+1].buf, msg[i+1].len);
  249. if (len <= 0) {
  250. deb_info("I2C read failed on address 0x%02x\n",
  251. msg[i].addr);
  252. break;
  253. }
  254. msg[i+1].len = len;
  255. i++;
  256. } else {
  257. st->buf[0] = REQUEST_I2C_WRITE;
  258. if (dib0700_ctrl_wr(d, st->buf, msg[i].len + 2) < 0)
  259. break;
  260. }
  261. }
  262. mutex_unlock(&d->usb_mutex);
  263. mutex_unlock(&d->i2c_mutex);
  264. return i;
  265. }
  266. static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  267. int num)
  268. {
  269. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  270. struct dib0700_state *st = d->priv;
  271. if (st->fw_use_new_i2c_api == 1) {
  272. /* User running at least fw 1.20 */
  273. return dib0700_i2c_xfer_new(adap, msg, num);
  274. } else {
  275. /* Use legacy calls */
  276. return dib0700_i2c_xfer_legacy(adap, msg, num);
  277. }
  278. }
  279. static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
  280. {
  281. return I2C_FUNC_I2C;
  282. }
  283. struct i2c_algorithm dib0700_i2c_algo = {
  284. .master_xfer = dib0700_i2c_xfer,
  285. .functionality = dib0700_i2c_func,
  286. };
  287. int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
  288. struct dvb_usb_device_description **desc, int *cold)
  289. {
  290. s16 ret;
  291. u8 *b;
  292. b = kmalloc(16, GFP_KERNEL);
  293. if (!b)
  294. return -ENOMEM;
  295. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  296. REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
  297. deb_info("FW GET_VERSION length: %d\n",ret);
  298. *cold = ret <= 0;
  299. deb_info("cold: %d\n", *cold);
  300. kfree(b);
  301. return 0;
  302. }
  303. static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
  304. u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
  305. u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
  306. {
  307. struct dib0700_state *st = d->priv;
  308. int ret;
  309. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  310. err("could not acquire lock");
  311. return -EINTR;
  312. }
  313. st->buf[0] = REQUEST_SET_CLOCK;
  314. st->buf[1] = (en_pll << 7) | (pll_src << 6) |
  315. (pll_range << 5) | (clock_gpio3 << 4);
  316. st->buf[2] = (pll_prediv >> 8) & 0xff; /* MSB */
  317. st->buf[3] = pll_prediv & 0xff; /* LSB */
  318. st->buf[4] = (pll_loopdiv >> 8) & 0xff; /* MSB */
  319. st->buf[5] = pll_loopdiv & 0xff; /* LSB */
  320. st->buf[6] = (free_div >> 8) & 0xff; /* MSB */
  321. st->buf[7] = free_div & 0xff; /* LSB */
  322. st->buf[8] = (dsuScaler >> 8) & 0xff; /* MSB */
  323. st->buf[9] = dsuScaler & 0xff; /* LSB */
  324. ret = dib0700_ctrl_wr(d, st->buf, 10);
  325. mutex_unlock(&d->usb_mutex);
  326. return ret;
  327. }
  328. int dib0700_set_i2c_speed(struct dvb_usb_device *d, u16 scl_kHz)
  329. {
  330. struct dib0700_state *st = d->priv;
  331. u16 divider;
  332. int ret;
  333. if (scl_kHz == 0)
  334. return -EINVAL;
  335. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  336. err("could not acquire lock");
  337. return -EINTR;
  338. }
  339. st->buf[0] = REQUEST_SET_I2C_PARAM;
  340. divider = (u16) (30000 / scl_kHz);
  341. st->buf[1] = 0;
  342. st->buf[2] = (u8) (divider >> 8);
  343. st->buf[3] = (u8) (divider & 0xff);
  344. divider = (u16) (72000 / scl_kHz);
  345. st->buf[4] = (u8) (divider >> 8);
  346. st->buf[5] = (u8) (divider & 0xff);
  347. divider = (u16) (72000 / scl_kHz); /* clock: 72MHz */
  348. st->buf[6] = (u8) (divider >> 8);
  349. st->buf[7] = (u8) (divider & 0xff);
  350. deb_info("setting I2C speed: %04x %04x %04x (%d kHz).",
  351. (st->buf[2] << 8) | (st->buf[3]), (st->buf[4] << 8) |
  352. st->buf[5], (st->buf[6] << 8) | st->buf[7], scl_kHz);
  353. ret = dib0700_ctrl_wr(d, st->buf, 8);
  354. mutex_unlock(&d->usb_mutex);
  355. return ret;
  356. }
  357. int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
  358. {
  359. switch (clk_MHz) {
  360. case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
  361. default: return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. static int dib0700_jumpram(struct usb_device *udev, u32 address)
  366. {
  367. int ret = 0, actlen;
  368. u8 *buf;
  369. buf = kmalloc(8, GFP_KERNEL);
  370. if (!buf)
  371. return -ENOMEM;
  372. buf[0] = REQUEST_JUMPRAM;
  373. buf[1] = 0;
  374. buf[2] = 0;
  375. buf[3] = 0;
  376. buf[4] = (address >> 24) & 0xff;
  377. buf[5] = (address >> 16) & 0xff;
  378. buf[6] = (address >> 8) & 0xff;
  379. buf[7] = address & 0xff;
  380. if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
  381. deb_fw("jumpram to 0x%x failed\n",address);
  382. goto out;
  383. }
  384. if (actlen != 8) {
  385. deb_fw("jumpram to 0x%x failed\n",address);
  386. ret = -EIO;
  387. goto out;
  388. }
  389. out:
  390. kfree(buf);
  391. return ret;
  392. }
  393. int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
  394. {
  395. struct hexline hx;
  396. int pos = 0, ret, act_len, i, adap_num;
  397. u8 *buf;
  398. u32 fw_version;
  399. buf = kmalloc(260, GFP_KERNEL);
  400. if (!buf)
  401. return -ENOMEM;
  402. while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
  403. deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",
  404. hx.addr, hx.len, hx.chk);
  405. buf[0] = hx.len;
  406. buf[1] = (hx.addr >> 8) & 0xff;
  407. buf[2] = hx.addr & 0xff;
  408. buf[3] = hx.type;
  409. memcpy(&buf[4],hx.data,hx.len);
  410. buf[4+hx.len] = hx.chk;
  411. ret = usb_bulk_msg(udev,
  412. usb_sndbulkpipe(udev, 0x01),
  413. buf,
  414. hx.len + 5,
  415. &act_len,
  416. 1000);
  417. if (ret < 0) {
  418. err("firmware download failed at %d with %d",pos,ret);
  419. goto out;
  420. }
  421. }
  422. if (ret == 0) {
  423. /* start the firmware */
  424. if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
  425. info("firmware started successfully.");
  426. msleep(500);
  427. }
  428. } else
  429. ret = -EIO;
  430. /* the number of ts packet has to be at least 1 */
  431. if (nb_packet_buffer_size < 1)
  432. nb_packet_buffer_size = 1;
  433. /* get the fimware version */
  434. usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  435. REQUEST_GET_VERSION,
  436. USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
  437. buf, 16, USB_CTRL_GET_TIMEOUT);
  438. fw_version = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
  439. /* set the buffer size - DVB-USB is allocating URB buffers
  440. * only after the firwmare download was successful */
  441. for (i = 0; i < dib0700_device_count; i++) {
  442. for (adap_num = 0; adap_num < dib0700_devices[i].num_adapters;
  443. adap_num++) {
  444. if (fw_version >= 0x10201) {
  445. dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 188*nb_packet_buffer_size;
  446. } else {
  447. /* for fw version older than 1.20.1,
  448. * the buffersize has to be n times 512 */
  449. dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = ((188*nb_packet_buffer_size+188/2)/512)*512;
  450. if (dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize < 512)
  451. dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 512;
  452. }
  453. }
  454. }
  455. out:
  456. kfree(buf);
  457. return ret;
  458. }
  459. int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  460. {
  461. struct dib0700_state *st = adap->dev->priv;
  462. int ret;
  463. if ((onoff != 0) && (st->fw_version >= 0x10201)) {
  464. /* for firmware later than 1.20.1,
  465. * the USB xfer length can be set */
  466. ret = dib0700_set_usb_xfer_len(adap->dev,
  467. st->nb_packet_buffer_size);
  468. if (ret < 0) {
  469. deb_info("can not set the USB xfer len\n");
  470. return ret;
  471. }
  472. }
  473. mutex_lock(&adap->dev->usb_mutex);
  474. st->buf[0] = REQUEST_ENABLE_VIDEO;
  475. /* this bit gives a kind of command,
  476. * rather than enabling something or not */
  477. st->buf[1] = (onoff << 4) | 0x00;
  478. if (st->disable_streaming_master_mode == 1)
  479. st->buf[2] = 0x00;
  480. else
  481. st->buf[2] = 0x01 << 4; /* Master mode */
  482. st->buf[3] = 0x00;
  483. deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
  484. st->channel_state &= ~0x3;
  485. if ((adap->fe_adap[0].stream.props.endpoint != 2)
  486. && (adap->fe_adap[0].stream.props.endpoint != 3)) {
  487. deb_info("the endpoint number (%i) is not correct, use the adapter id instead", adap->fe_adap[0].stream.props.endpoint);
  488. if (onoff)
  489. st->channel_state |= 1 << (adap->id);
  490. else
  491. st->channel_state |= 1 << ~(adap->id);
  492. } else {
  493. if (onoff)
  494. st->channel_state |= 1 << (adap->fe_adap[0].stream.props.endpoint-2);
  495. else
  496. st->channel_state |= 1 << (3-adap->fe_adap[0].stream.props.endpoint);
  497. }
  498. st->buf[2] |= st->channel_state;
  499. deb_info("data for streaming: %x %x\n", st->buf[1], st->buf[2]);
  500. ret = dib0700_ctrl_wr(adap->dev, st->buf, 4);
  501. mutex_unlock(&adap->dev->usb_mutex);
  502. return ret;
  503. }
  504. int dib0700_change_protocol(struct rc_dev *rc, u64 *rc_type)
  505. {
  506. struct dvb_usb_device *d = rc->priv;
  507. struct dib0700_state *st = d->priv;
  508. int new_proto, ret;
  509. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  510. err("could not acquire lock");
  511. return -EINTR;
  512. }
  513. st->buf[0] = REQUEST_SET_RC;
  514. st->buf[1] = 0;
  515. st->buf[2] = 0;
  516. /* Set the IR mode */
  517. if (*rc_type & RC_BIT_RC5) {
  518. new_proto = 1;
  519. *rc_type = RC_BIT_RC5;
  520. } else if (*rc_type & RC_BIT_NEC) {
  521. new_proto = 0;
  522. *rc_type = RC_BIT_NEC;
  523. } else if (*rc_type & RC_BIT_RC6_MCE) {
  524. if (st->fw_version < 0x10200) {
  525. ret = -EINVAL;
  526. goto out;
  527. }
  528. new_proto = 2;
  529. *rc_type = RC_BIT_RC6_MCE;
  530. } else {
  531. ret = -EINVAL;
  532. goto out;
  533. }
  534. st->buf[1] = new_proto;
  535. ret = dib0700_ctrl_wr(d, st->buf, 3);
  536. if (ret < 0) {
  537. err("ir protocol setup failed");
  538. goto out;
  539. }
  540. d->props.rc.core.protocol = *rc_type;
  541. out:
  542. mutex_unlock(&d->usb_mutex);
  543. return ret;
  544. }
  545. /* This is the structure of the RC response packet starting in firmware 1.20 */
  546. struct dib0700_rc_response {
  547. u8 report_id;
  548. u8 data_state;
  549. union {
  550. struct {
  551. u8 system;
  552. u8 not_system;
  553. u8 data;
  554. u8 not_data;
  555. } nec;
  556. struct {
  557. u8 not_used;
  558. u8 system;
  559. u8 data;
  560. u8 not_data;
  561. } rc5;
  562. };
  563. };
  564. #define RC_MSG_SIZE_V1_20 6
  565. static void dib0700_rc_urb_completion(struct urb *purb)
  566. {
  567. struct dvb_usb_device *d = purb->context;
  568. struct dib0700_rc_response *poll_reply;
  569. enum rc_type protocol;
  570. u32 keycode;
  571. u8 toggle;
  572. deb_info("%s()\n", __func__);
  573. if (d->rc_dev == NULL) {
  574. /* This will occur if disable_rc_polling=1 */
  575. kfree(purb->transfer_buffer);
  576. usb_free_urb(purb);
  577. return;
  578. }
  579. poll_reply = purb->transfer_buffer;
  580. if (purb->status < 0) {
  581. deb_info("discontinuing polling\n");
  582. kfree(purb->transfer_buffer);
  583. usb_free_urb(purb);
  584. return;
  585. }
  586. if (purb->actual_length != RC_MSG_SIZE_V1_20) {
  587. deb_info("malformed rc msg size=%d\n", purb->actual_length);
  588. goto resubmit;
  589. }
  590. deb_data("IR ID = %02X state = %02X System = %02X %02X Cmd = %02X %02X (len %d)\n",
  591. poll_reply->report_id, poll_reply->data_state,
  592. poll_reply->nec.system, poll_reply->nec.not_system,
  593. poll_reply->nec.data, poll_reply->nec.not_data,
  594. purb->actual_length);
  595. switch (d->props.rc.core.protocol) {
  596. case RC_BIT_NEC:
  597. protocol = RC_TYPE_NEC;
  598. toggle = 0;
  599. /* NEC protocol sends repeat code as 0 0 0 FF */
  600. if (poll_reply->nec.system == 0x00 &&
  601. poll_reply->nec.not_system == 0x00 &&
  602. poll_reply->nec.data == 0x00 &&
  603. poll_reply->nec.not_data == 0xff) {
  604. poll_reply->data_state = 2;
  605. rc_repeat(d->rc_dev);
  606. goto resubmit;
  607. }
  608. if ((poll_reply->nec.data ^ poll_reply->nec.not_data) != 0xff) {
  609. deb_data("NEC32 protocol\n");
  610. keycode = RC_SCANCODE_NEC32(poll_reply->nec.system << 24 |
  611. poll_reply->nec.not_system << 16 |
  612. poll_reply->nec.data << 8 |
  613. poll_reply->nec.not_data);
  614. } else if ((poll_reply->nec.system ^ poll_reply->nec.not_system) != 0xff) {
  615. deb_data("NEC extended protocol\n");
  616. keycode = RC_SCANCODE_NECX(poll_reply->nec.system << 8 |
  617. poll_reply->nec.not_system,
  618. poll_reply->nec.data);
  619. } else {
  620. deb_data("NEC normal protocol\n");
  621. keycode = RC_SCANCODE_NEC(poll_reply->nec.system,
  622. poll_reply->nec.data);
  623. }
  624. break;
  625. default:
  626. deb_data("RC5 protocol\n");
  627. protocol = RC_TYPE_RC5;
  628. toggle = poll_reply->report_id;
  629. keycode = RC_SCANCODE_RC5(poll_reply->rc5.system, poll_reply->rc5.data);
  630. if ((poll_reply->rc5.data ^ poll_reply->rc5.not_data) != 0xff) {
  631. /* Key failed integrity check */
  632. err("key failed integrity check: %02x %02x %02x %02x",
  633. poll_reply->rc5.not_used, poll_reply->rc5.system,
  634. poll_reply->rc5.data, poll_reply->rc5.not_data);
  635. goto resubmit;
  636. }
  637. break;
  638. }
  639. rc_keydown(d->rc_dev, protocol, keycode, toggle);
  640. resubmit:
  641. /* Clean the buffer before we requeue */
  642. memset(purb->transfer_buffer, 0, RC_MSG_SIZE_V1_20);
  643. /* Requeue URB */
  644. usb_submit_urb(purb, GFP_ATOMIC);
  645. }
  646. int dib0700_rc_setup(struct dvb_usb_device *d, struct usb_interface *intf)
  647. {
  648. struct dib0700_state *st = d->priv;
  649. struct urb *purb;
  650. const struct usb_endpoint_descriptor *e;
  651. int ret, rc_ep = 1;
  652. unsigned int pipe = 0;
  653. /* Poll-based. Don't initialize bulk mode */
  654. if (st->fw_version < 0x10200 || !intf)
  655. return 0;
  656. /* Starting in firmware 1.20, the RC info is provided on a bulk pipe */
  657. if (intf->altsetting[0].desc.bNumEndpoints < rc_ep + 1)
  658. return -ENODEV;
  659. purb = usb_alloc_urb(0, GFP_KERNEL);
  660. if (purb == NULL) {
  661. err("rc usb alloc urb failed");
  662. return -ENOMEM;
  663. }
  664. purb->transfer_buffer = kzalloc(RC_MSG_SIZE_V1_20, GFP_KERNEL);
  665. if (purb->transfer_buffer == NULL) {
  666. err("rc kzalloc failed");
  667. usb_free_urb(purb);
  668. return -ENOMEM;
  669. }
  670. purb->status = -EINPROGRESS;
  671. /*
  672. * Some devices like the Hauppauge NovaTD model 52009 use an interrupt
  673. * endpoint, while others use a bulk one.
  674. */
  675. e = &intf->altsetting[0].endpoint[rc_ep].desc;
  676. if (usb_endpoint_dir_in(e)) {
  677. if (usb_endpoint_xfer_bulk(e)) {
  678. pipe = usb_rcvbulkpipe(d->udev, rc_ep);
  679. usb_fill_bulk_urb(purb, d->udev, pipe,
  680. purb->transfer_buffer,
  681. RC_MSG_SIZE_V1_20,
  682. dib0700_rc_urb_completion, d);
  683. } else if (usb_endpoint_xfer_int(e)) {
  684. pipe = usb_rcvintpipe(d->udev, rc_ep);
  685. usb_fill_int_urb(purb, d->udev, pipe,
  686. purb->transfer_buffer,
  687. RC_MSG_SIZE_V1_20,
  688. dib0700_rc_urb_completion, d, 1);
  689. }
  690. }
  691. if (!pipe) {
  692. err("There's no endpoint for remote controller");
  693. kfree(purb->transfer_buffer);
  694. usb_free_urb(purb);
  695. return 0;
  696. }
  697. ret = usb_submit_urb(purb, GFP_ATOMIC);
  698. if (ret) {
  699. err("rc submit urb failed");
  700. kfree(purb->transfer_buffer);
  701. usb_free_urb(purb);
  702. }
  703. return ret;
  704. }
  705. static int dib0700_probe(struct usb_interface *intf,
  706. const struct usb_device_id *id)
  707. {
  708. int i;
  709. struct dvb_usb_device *dev;
  710. for (i = 0; i < dib0700_device_count; i++)
  711. if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
  712. &dev, adapter_nr) == 0) {
  713. struct dib0700_state *st = dev->priv;
  714. u32 hwversion, romversion, fw_version, fwtype;
  715. dib0700_get_version(dev, &hwversion, &romversion,
  716. &fw_version, &fwtype);
  717. deb_info("Firmware version: %x, %d, 0x%x, %d\n",
  718. hwversion, romversion, fw_version, fwtype);
  719. st->fw_version = fw_version;
  720. st->nb_packet_buffer_size = (u32)nb_packet_buffer_size;
  721. /* Disable polling mode on newer firmwares */
  722. if (st->fw_version >= 0x10200)
  723. dev->props.rc.core.bulk_mode = true;
  724. else
  725. dev->props.rc.core.bulk_mode = false;
  726. dib0700_rc_setup(dev, intf);
  727. return 0;
  728. }
  729. return -ENODEV;
  730. }
  731. static struct usb_driver dib0700_driver = {
  732. .name = "dvb_usb_dib0700",
  733. .probe = dib0700_probe,
  734. .disconnect = dvb_usb_device_exit,
  735. .id_table = dib0700_usb_id_table,
  736. };
  737. module_usb_driver(dib0700_driver);
  738. MODULE_FIRMWARE("dvb-usb-dib0700-1.20.fw");
  739. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  740. MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
  741. MODULE_VERSION("1.0");
  742. MODULE_LICENSE("GPL");