dib0700_core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. deb_info("could not acquire lock");
  29. return 0;
  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. deb_info("could not acquire lock");
  99. return 0;
  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. deb_info("could not acquire lock");
  115. return 0;
  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 -EAGAIN;
  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. deb_info("could not acquire lock");
  191. return 0;
  192. }
  193. st->buf[0] = REQUEST_NEW_I2C_WRITE;
  194. st->buf[1] = msg[i].addr << 1;
  195. st->buf[2] = (en_start << 7) | (en_stop << 6) |
  196. (msg[i].len & 0x3F);
  197. /* I2C ctrl + FE bus; */
  198. st->buf[3] = ((gen_mode << 6) & 0xC0) |
  199. ((bus_mode << 4) & 0x30);
  200. /* The Actual i2c payload */
  201. memcpy(&st->buf[4], msg[i].buf, msg[i].len);
  202. deb_data(">>> ");
  203. debug_dump(st->buf, msg[i].len + 4, deb_data);
  204. result = usb_control_msg(d->udev,
  205. usb_sndctrlpipe(d->udev, 0),
  206. REQUEST_NEW_I2C_WRITE,
  207. USB_TYPE_VENDOR | USB_DIR_OUT,
  208. 0, 0, st->buf, msg[i].len + 4,
  209. USB_CTRL_GET_TIMEOUT);
  210. mutex_unlock(&d->usb_mutex);
  211. if (result < 0) {
  212. deb_info("i2c write error (status = %d)\n", result);
  213. break;
  214. }
  215. }
  216. }
  217. mutex_unlock(&d->i2c_mutex);
  218. return i;
  219. }
  220. /*
  221. * I2C master xfer function (pre-1.20 firmware)
  222. */
  223. static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
  224. struct i2c_msg *msg, int num)
  225. {
  226. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  227. struct dib0700_state *st = d->priv;
  228. int i,len;
  229. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  230. return -EAGAIN;
  231. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  232. deb_info("could not acquire lock");
  233. return 0;
  234. }
  235. for (i = 0; i < num; i++) {
  236. /* fill in the address */
  237. st->buf[1] = msg[i].addr << 1;
  238. /* fill the buffer */
  239. memcpy(&st->buf[2], msg[i].buf, msg[i].len);
  240. /* write/read request */
  241. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  242. st->buf[0] = REQUEST_I2C_READ;
  243. st->buf[1] |= 1;
  244. /* special thing in the current firmware: when length is zero the read-failed */
  245. len = dib0700_ctrl_rd(d, st->buf, msg[i].len + 2,
  246. msg[i+1].buf, msg[i+1].len);
  247. if (len <= 0) {
  248. deb_info("I2C read failed on address 0x%02x\n",
  249. msg[i].addr);
  250. break;
  251. }
  252. msg[i+1].len = len;
  253. i++;
  254. } else {
  255. st->buf[0] = REQUEST_I2C_WRITE;
  256. if (dib0700_ctrl_wr(d, st->buf, msg[i].len + 2) < 0)
  257. break;
  258. }
  259. }
  260. mutex_unlock(&d->usb_mutex);
  261. mutex_unlock(&d->i2c_mutex);
  262. return i;
  263. }
  264. static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  265. int num)
  266. {
  267. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  268. struct dib0700_state *st = d->priv;
  269. if (st->fw_use_new_i2c_api == 1) {
  270. /* User running at least fw 1.20 */
  271. return dib0700_i2c_xfer_new(adap, msg, num);
  272. } else {
  273. /* Use legacy calls */
  274. return dib0700_i2c_xfer_legacy(adap, msg, num);
  275. }
  276. }
  277. static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
  278. {
  279. return I2C_FUNC_I2C;
  280. }
  281. struct i2c_algorithm dib0700_i2c_algo = {
  282. .master_xfer = dib0700_i2c_xfer,
  283. .functionality = dib0700_i2c_func,
  284. };
  285. int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
  286. struct dvb_usb_device_description **desc, int *cold)
  287. {
  288. s16 ret;
  289. u8 *b;
  290. b = kmalloc(16, GFP_KERNEL);
  291. if (!b)
  292. return -ENOMEM;
  293. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  294. REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
  295. deb_info("FW GET_VERSION length: %d\n",ret);
  296. *cold = ret <= 0;
  297. deb_info("cold: %d\n", *cold);
  298. kfree(b);
  299. return 0;
  300. }
  301. static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
  302. u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
  303. u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
  304. {
  305. struct dib0700_state *st = d->priv;
  306. int ret;
  307. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  308. deb_info("could not acquire lock");
  309. return 0;
  310. }
  311. st->buf[0] = REQUEST_SET_CLOCK;
  312. st->buf[1] = (en_pll << 7) | (pll_src << 6) |
  313. (pll_range << 5) | (clock_gpio3 << 4);
  314. st->buf[2] = (pll_prediv >> 8) & 0xff; /* MSB */
  315. st->buf[3] = pll_prediv & 0xff; /* LSB */
  316. st->buf[4] = (pll_loopdiv >> 8) & 0xff; /* MSB */
  317. st->buf[5] = pll_loopdiv & 0xff; /* LSB */
  318. st->buf[6] = (free_div >> 8) & 0xff; /* MSB */
  319. st->buf[7] = free_div & 0xff; /* LSB */
  320. st->buf[8] = (dsuScaler >> 8) & 0xff; /* MSB */
  321. st->buf[9] = dsuScaler & 0xff; /* LSB */
  322. ret = dib0700_ctrl_wr(d, st->buf, 10);
  323. mutex_unlock(&d->usb_mutex);
  324. return ret;
  325. }
  326. int dib0700_set_i2c_speed(struct dvb_usb_device *d, u16 scl_kHz)
  327. {
  328. struct dib0700_state *st = d->priv;
  329. u16 divider;
  330. int ret;
  331. if (scl_kHz == 0)
  332. return -EINVAL;
  333. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  334. deb_info("could not acquire lock");
  335. return 0;
  336. }
  337. st->buf[0] = REQUEST_SET_I2C_PARAM;
  338. divider = (u16) (30000 / scl_kHz);
  339. st->buf[1] = 0;
  340. st->buf[2] = (u8) (divider >> 8);
  341. st->buf[3] = (u8) (divider & 0xff);
  342. divider = (u16) (72000 / scl_kHz);
  343. st->buf[4] = (u8) (divider >> 8);
  344. st->buf[5] = (u8) (divider & 0xff);
  345. divider = (u16) (72000 / scl_kHz); /* clock: 72MHz */
  346. st->buf[6] = (u8) (divider >> 8);
  347. st->buf[7] = (u8) (divider & 0xff);
  348. deb_info("setting I2C speed: %04x %04x %04x (%d kHz).",
  349. (st->buf[2] << 8) | (st->buf[3]), (st->buf[4] << 8) |
  350. st->buf[5], (st->buf[6] << 8) | st->buf[7], scl_kHz);
  351. ret = dib0700_ctrl_wr(d, st->buf, 8);
  352. mutex_unlock(&d->usb_mutex);
  353. return ret;
  354. }
  355. int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
  356. {
  357. switch (clk_MHz) {
  358. case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
  359. default: return -EINVAL;
  360. }
  361. return 0;
  362. }
  363. static int dib0700_jumpram(struct usb_device *udev, u32 address)
  364. {
  365. int ret = 0, actlen;
  366. u8 *buf;
  367. buf = kmalloc(8, GFP_KERNEL);
  368. if (!buf)
  369. return -ENOMEM;
  370. buf[0] = REQUEST_JUMPRAM;
  371. buf[1] = 0;
  372. buf[2] = 0;
  373. buf[3] = 0;
  374. buf[4] = (address >> 24) & 0xff;
  375. buf[5] = (address >> 16) & 0xff;
  376. buf[6] = (address >> 8) & 0xff;
  377. buf[7] = address & 0xff;
  378. if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
  379. deb_fw("jumpram to 0x%x failed\n",address);
  380. goto out;
  381. }
  382. if (actlen != 8) {
  383. deb_fw("jumpram to 0x%x failed\n",address);
  384. ret = -EIO;
  385. goto out;
  386. }
  387. out:
  388. kfree(buf);
  389. return ret;
  390. }
  391. int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
  392. {
  393. struct hexline hx;
  394. int pos = 0, ret, act_len, i, adap_num;
  395. u8 *buf;
  396. u32 fw_version;
  397. buf = kmalloc(260, GFP_KERNEL);
  398. if (!buf)
  399. return -ENOMEM;
  400. while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
  401. deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",
  402. hx.addr, hx.len, hx.chk);
  403. buf[0] = hx.len;
  404. buf[1] = (hx.addr >> 8) & 0xff;
  405. buf[2] = hx.addr & 0xff;
  406. buf[3] = hx.type;
  407. memcpy(&buf[4],hx.data,hx.len);
  408. buf[4+hx.len] = hx.chk;
  409. ret = usb_bulk_msg(udev,
  410. usb_sndbulkpipe(udev, 0x01),
  411. buf,
  412. hx.len + 5,
  413. &act_len,
  414. 1000);
  415. if (ret < 0) {
  416. err("firmware download failed at %d with %d",pos,ret);
  417. goto out;
  418. }
  419. }
  420. if (ret == 0) {
  421. /* start the firmware */
  422. if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
  423. info("firmware started successfully.");
  424. msleep(500);
  425. }
  426. } else
  427. ret = -EIO;
  428. /* the number of ts packet has to be at least 1 */
  429. if (nb_packet_buffer_size < 1)
  430. nb_packet_buffer_size = 1;
  431. /* get the fimware version */
  432. usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  433. REQUEST_GET_VERSION,
  434. USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
  435. buf, 16, USB_CTRL_GET_TIMEOUT);
  436. fw_version = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
  437. /* set the buffer size - DVB-USB is allocating URB buffers
  438. * only after the firwmare download was successful */
  439. for (i = 0; i < dib0700_device_count; i++) {
  440. for (adap_num = 0; adap_num < dib0700_devices[i].num_adapters;
  441. adap_num++) {
  442. if (fw_version >= 0x10201) {
  443. dib0700_devices[i].adapter[adap_num].stream.u.bulk.buffersize = 188*nb_packet_buffer_size;
  444. } else {
  445. /* for fw version older than 1.20.1,
  446. * the buffersize has to be n times 512 */
  447. dib0700_devices[i].adapter[adap_num].stream.u.bulk.buffersize = ((188*nb_packet_buffer_size+188/2)/512)*512;
  448. if (dib0700_devices[i].adapter[adap_num].stream.u.bulk.buffersize < 512)
  449. dib0700_devices[i].adapter[adap_num].stream.u.bulk.buffersize = 512;
  450. }
  451. }
  452. }
  453. out:
  454. kfree(buf);
  455. return ret;
  456. }
  457. int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  458. {
  459. struct dib0700_state *st = adap->dev->priv;
  460. int ret;
  461. if ((onoff != 0) && (st->fw_version >= 0x10201)) {
  462. /* for firmware later than 1.20.1,
  463. * the USB xfer length can be set */
  464. ret = dib0700_set_usb_xfer_len(adap->dev,
  465. st->nb_packet_buffer_size);
  466. if (ret < 0) {
  467. deb_info("can not set the USB xfer len\n");
  468. return ret;
  469. }
  470. }
  471. if (mutex_lock_interruptible(&adap->dev->usb_mutex) < 0) {
  472. deb_info("could not acquire lock");
  473. return 0;
  474. }
  475. st->buf[0] = REQUEST_ENABLE_VIDEO;
  476. /* this bit gives a kind of command,
  477. * rather than enabling something or not */
  478. st->buf[1] = (onoff << 4) | 0x00;
  479. if (st->disable_streaming_master_mode == 1)
  480. st->buf[2] = 0x00;
  481. else
  482. st->buf[2] = 0x01 << 4; /* Master mode */
  483. st->buf[3] = 0x00;
  484. deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
  485. st->channel_state &= ~0x3;
  486. if ((adap->stream.props.endpoint != 2)
  487. && (adap->stream.props.endpoint != 3)) {
  488. deb_info("the endpoint number (%i) is not correct, use the adapter id instead", adap->stream.props.endpoint);
  489. if (onoff)
  490. st->channel_state |= 1 << (adap->id);
  491. else
  492. st->channel_state |= 1 << ~(adap->id);
  493. } else {
  494. if (onoff)
  495. st->channel_state |= 1 << (adap->stream.props.endpoint-2);
  496. else
  497. st->channel_state |= 1 << (3-adap->stream.props.endpoint);
  498. }
  499. st->buf[2] |= st->channel_state;
  500. deb_info("data for streaming: %x %x\n", st->buf[1], st->buf[2]);
  501. ret = dib0700_ctrl_wr(adap->dev, st->buf, 4);
  502. mutex_unlock(&adap->dev->usb_mutex);
  503. return ret;
  504. }
  505. int dib0700_change_protocol(struct rc_dev *rc, u64 rc_type)
  506. {
  507. struct dvb_usb_device *d = rc->priv;
  508. struct dib0700_state *st = d->priv;
  509. int new_proto, ret;
  510. if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
  511. deb_info("could not acquire lock");
  512. return 0;
  513. }
  514. st->buf[0] = REQUEST_SET_RC;
  515. st->buf[1] = 0;
  516. st->buf[2] = 0;
  517. /* Set the IR mode */
  518. if (rc_type == RC_TYPE_RC5)
  519. new_proto = 1;
  520. else if (rc_type == RC_TYPE_NEC)
  521. new_proto = 0;
  522. else if (rc_type == RC_TYPE_RC6) {
  523. if (st->fw_version < 0x10200) {
  524. ret = -EINVAL;
  525. goto out;
  526. }
  527. new_proto = 2;
  528. } else {
  529. ret = -EINVAL;
  530. goto out;
  531. }
  532. st->buf[1] = new_proto;
  533. ret = dib0700_ctrl_wr(d, st->buf, 3);
  534. if (ret < 0) {
  535. err("ir protocol setup failed");
  536. goto out;
  537. }
  538. d->props.rc.core.protocol = rc_type;
  539. out:
  540. mutex_unlock(&d->usb_mutex);
  541. return ret;
  542. }
  543. /* Number of keypresses to ignore before start repeating */
  544. #define RC_REPEAT_DELAY_V1_20 10
  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. u16 system16;
  551. struct {
  552. u8 not_system;
  553. u8 system;
  554. };
  555. };
  556. u8 data;
  557. u8 not_data;
  558. };
  559. #define RC_MSG_SIZE_V1_20 6
  560. static void dib0700_rc_urb_completion(struct urb *purb)
  561. {
  562. struct dvb_usb_device *d = purb->context;
  563. struct dib0700_rc_response *poll_reply;
  564. u32 uninitialized_var(keycode);
  565. u8 toggle;
  566. deb_info("%s()\n", __func__);
  567. if (d == NULL)
  568. return;
  569. if (d->rc_dev == NULL) {
  570. /* This will occur if disable_rc_polling=1 */
  571. usb_free_urb(purb);
  572. return;
  573. }
  574. poll_reply = purb->transfer_buffer;
  575. if (purb->status < 0) {
  576. deb_info("discontinuing polling\n");
  577. usb_free_urb(purb);
  578. return;
  579. }
  580. if (purb->actual_length != RC_MSG_SIZE_V1_20) {
  581. deb_info("malformed rc msg size=%d\n", purb->actual_length);
  582. goto resubmit;
  583. }
  584. deb_data("IR ID = %02X state = %02X System = %02X %02X Cmd = %02X %02X (len %d)\n",
  585. poll_reply->report_id, poll_reply->data_state,
  586. poll_reply->system, poll_reply->not_system,
  587. poll_reply->data, poll_reply->not_data,
  588. purb->actual_length);
  589. switch (d->props.rc.core.protocol) {
  590. case RC_TYPE_NEC:
  591. toggle = 0;
  592. /* NEC protocol sends repeat code as 0 0 0 FF */
  593. if ((poll_reply->system == 0x00) && (poll_reply->data == 0x00)
  594. && (poll_reply->not_data == 0xff)) {
  595. poll_reply->data_state = 2;
  596. break;
  597. }
  598. if ((poll_reply->system ^ poll_reply->not_system) != 0xff) {
  599. deb_data("NEC extended protocol\n");
  600. /* NEC extended code - 24 bits */
  601. keycode = be16_to_cpu(poll_reply->system16) << 8 | poll_reply->data;
  602. } else {
  603. deb_data("NEC normal protocol\n");
  604. /* normal NEC code - 16 bits */
  605. keycode = poll_reply->system << 8 | poll_reply->data;
  606. }
  607. break;
  608. default:
  609. deb_data("RC5 protocol\n");
  610. /* RC5 Protocol */
  611. toggle = poll_reply->report_id;
  612. keycode = poll_reply->system << 8 | poll_reply->data;
  613. break;
  614. }
  615. if ((poll_reply->data + poll_reply->not_data) != 0xff) {
  616. /* Key failed integrity check */
  617. err("key failed integrity check: %04x %02x %02x",
  618. poll_reply->system,
  619. poll_reply->data, poll_reply->not_data);
  620. goto resubmit;
  621. }
  622. rc_keydown(d->rc_dev, keycode, toggle);
  623. resubmit:
  624. /* Clean the buffer before we requeue */
  625. memset(purb->transfer_buffer, 0, RC_MSG_SIZE_V1_20);
  626. /* Requeue URB */
  627. usb_submit_urb(purb, GFP_ATOMIC);
  628. }
  629. int dib0700_rc_setup(struct dvb_usb_device *d)
  630. {
  631. struct dib0700_state *st = d->priv;
  632. struct urb *purb;
  633. int ret;
  634. /* Poll-based. Don't initialize bulk mode */
  635. if (st->fw_version < 0x10200)
  636. return 0;
  637. /* Starting in firmware 1.20, the RC info is provided on a bulk pipe */
  638. purb = usb_alloc_urb(0, GFP_KERNEL);
  639. if (purb == NULL) {
  640. err("rc usb alloc urb failed\n");
  641. return -ENOMEM;
  642. }
  643. purb->transfer_buffer = kzalloc(RC_MSG_SIZE_V1_20, GFP_KERNEL);
  644. if (purb->transfer_buffer == NULL) {
  645. err("rc kzalloc failed\n");
  646. usb_free_urb(purb);
  647. return -ENOMEM;
  648. }
  649. purb->status = -EINPROGRESS;
  650. usb_fill_bulk_urb(purb, d->udev, usb_rcvbulkpipe(d->udev, 1),
  651. purb->transfer_buffer, RC_MSG_SIZE_V1_20,
  652. dib0700_rc_urb_completion, d);
  653. ret = usb_submit_urb(purb, GFP_ATOMIC);
  654. if (ret)
  655. err("rc submit urb failed\n");
  656. return ret;
  657. }
  658. static int dib0700_probe(struct usb_interface *intf,
  659. const struct usb_device_id *id)
  660. {
  661. int i;
  662. struct dvb_usb_device *dev;
  663. for (i = 0; i < dib0700_device_count; i++)
  664. if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
  665. &dev, adapter_nr) == 0) {
  666. struct dib0700_state *st = dev->priv;
  667. u32 hwversion, romversion, fw_version, fwtype;
  668. dib0700_get_version(dev, &hwversion, &romversion,
  669. &fw_version, &fwtype);
  670. deb_info("Firmware version: %x, %d, 0x%x, %d\n",
  671. hwversion, romversion, fw_version, fwtype);
  672. st->fw_version = fw_version;
  673. st->nb_packet_buffer_size = (u32)nb_packet_buffer_size;
  674. /* Disable polling mode on newer firmwares */
  675. if (st->fw_version >= 0x10200)
  676. dev->props.rc.core.bulk_mode = true;
  677. else
  678. dev->props.rc.core.bulk_mode = false;
  679. dib0700_rc_setup(dev);
  680. return 0;
  681. }
  682. return -ENODEV;
  683. }
  684. static struct usb_driver dib0700_driver = {
  685. .name = "dvb_usb_dib0700",
  686. .probe = dib0700_probe,
  687. .disconnect = dvb_usb_device_exit,
  688. .id_table = dib0700_usb_id_table,
  689. };
  690. /* module stuff */
  691. static int __init dib0700_module_init(void)
  692. {
  693. int result;
  694. info("loaded with support for %d different device-types", dib0700_device_count);
  695. if ((result = usb_register(&dib0700_driver))) {
  696. err("usb_register failed. Error number %d",result);
  697. return result;
  698. }
  699. return 0;
  700. }
  701. static void __exit dib0700_module_exit(void)
  702. {
  703. /* deregister this driver from the USB subsystem */
  704. usb_deregister(&dib0700_driver);
  705. }
  706. module_init (dib0700_module_init);
  707. module_exit (dib0700_module_exit);
  708. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  709. MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
  710. MODULE_VERSION("1.0");
  711. MODULE_LICENSE("GPL");