dib0700_core.c 20 KB

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