ch341a_spi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2011 asbokid <ballymunboy@gmail.com>
  5. * Copyright (C) 2014 Pluto Yang <yangyj.ee@gmail.com>
  6. * Copyright (C) 2015-2016 Stefan Tauner
  7. * Copyright (C) 2015 Urja Rannikko <urjaman@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <string.h>
  24. #include <libusb.h>
  25. #include "flash.h"
  26. #include "programmer.h"
  27. /* LIBUSB_CALL ensures the right calling conventions on libusb callbacks.
  28. * However, the macro is not defined everywhere. m(
  29. */
  30. #ifndef LIBUSB_CALL
  31. #define LIBUSB_CALL
  32. #endif
  33. #define USB_TIMEOUT 1000 /* 1000 ms is plenty and we have no backup strategy anyway. */
  34. #define WRITE_EP 0x02
  35. #define READ_EP 0x82
  36. #define CH341_PACKET_LENGTH 0x20
  37. #define CH341_MAX_PACKETS 256
  38. #define CH341_MAX_PACKET_LEN (CH341_PACKET_LENGTH * CH341_MAX_PACKETS)
  39. #define CH341A_CMD_SET_OUTPUT 0xA1
  40. #define CH341A_CMD_IO_ADDR 0xA2
  41. #define CH341A_CMD_PRINT_OUT 0xA3
  42. #define CH341A_CMD_SPI_STREAM 0xA8
  43. #define CH341A_CMD_SIO_STREAM 0xA9
  44. #define CH341A_CMD_I2C_STREAM 0xAA
  45. #define CH341A_CMD_UIO_STREAM 0xAB
  46. #define CH341A_CMD_I2C_STM_START 0x74
  47. #define CH341A_CMD_I2C_STM_STOP 0x75
  48. #define CH341A_CMD_I2C_STM_OUT 0x80
  49. #define CH341A_CMD_I2C_STM_IN 0xC0
  50. #define CH341A_CMD_I2C_STM_MAX ( min( 0x3F, CH341_PACKET_LENGTH ) )
  51. #define CH341A_CMD_I2C_STM_SET 0x60 // bit 2: SPI with two data pairs D5,D4=out, D7,D6=in
  52. #define CH341A_CMD_I2C_STM_US 0x40
  53. #define CH341A_CMD_I2C_STM_MS 0x50
  54. #define CH341A_CMD_I2C_STM_DLY 0x0F
  55. #define CH341A_CMD_I2C_STM_END 0x00
  56. #define CH341A_CMD_UIO_STM_IN 0x00
  57. #define CH341A_CMD_UIO_STM_DIR 0x40
  58. #define CH341A_CMD_UIO_STM_OUT 0x80
  59. #define CH341A_CMD_UIO_STM_US 0xC0
  60. #define CH341A_CMD_UIO_STM_END 0x20
  61. #define CH341A_STM_I2C_20K 0x00
  62. #define CH341A_STM_I2C_100K 0x01
  63. #define CH341A_STM_I2C_400K 0x02
  64. #define CH341A_STM_I2C_750K 0x03
  65. #define CH341A_STM_SPI_DBL 0x04
  66. /* Number of parallel IN transfers. 32 seems to produce the most stable throughput on Windows. */
  67. #define USB_IN_TRANSFERS 32
  68. /* We need to use many queued IN transfers for any resemblance of performance (especially on Windows)
  69. * because USB spec says that transfers end on non-full packets and the device sends the 31 reply
  70. * data bytes to each 32-byte packet with command + 31 bytes of data... */
  71. static struct libusb_transfer *transfer_out = NULL;
  72. static struct libusb_transfer *transfer_ins[USB_IN_TRANSFERS] = {0};
  73. /* Accumulate delays to be plucked between CS deassertion and CS assertions. */
  74. static unsigned int stored_delay_us = 0;
  75. static struct libusb_device_handle *handle = NULL;
  76. const struct dev_entry devs_ch341a_spi[] = {
  77. {0x1A86, 0x5512, OK, "Winchiphead (WCH)", "CH341A"},
  78. {0},
  79. };
  80. enum trans_state {TRANS_ACTIVE = -2, TRANS_ERR = -1, TRANS_IDLE = 0};
  81. static void print_hex(const void *buf, size_t len)
  82. {
  83. size_t i;
  84. for (i = 0; i < len; i++) {
  85. msg_pspew(" %02x", ((uint8_t *)buf)[i]);
  86. if (i % CH341_PACKET_LENGTH == CH341_PACKET_LENGTH - 1)
  87. msg_pspew("\n");
  88. }
  89. }
  90. static void cb_common(const char *func, struct libusb_transfer *transfer)
  91. {
  92. int *transfer_cnt = (int*)transfer->user_data;
  93. if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
  94. /* Silently ACK and exit. */
  95. *transfer_cnt = TRANS_IDLE;
  96. return;
  97. }
  98. if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
  99. msg_perr("\n%s: error: %s\n", func, libusb_error_name(transfer->status));
  100. *transfer_cnt = TRANS_ERR;
  101. } else {
  102. *transfer_cnt = transfer->actual_length;
  103. }
  104. }
  105. /* callback for bulk out async transfer */
  106. static void LIBUSB_CALL cb_out(struct libusb_transfer *transfer)
  107. {
  108. cb_common(__func__, transfer);
  109. }
  110. /* callback for bulk in async transfer */
  111. static void LIBUSB_CALL cb_in(struct libusb_transfer *transfer)
  112. {
  113. cb_common(__func__, transfer);
  114. }
  115. static int32_t usb_transfer(const char *func, unsigned int writecnt, unsigned int readcnt, const uint8_t *writearr, uint8_t *readarr)
  116. {
  117. if (handle == NULL)
  118. return -1;
  119. int state_out = TRANS_IDLE;
  120. transfer_out->buffer = (uint8_t*)writearr;
  121. transfer_out->length = writecnt;
  122. transfer_out->user_data = &state_out;
  123. /* Schedule write first */
  124. if (writecnt > 0) {
  125. state_out = TRANS_ACTIVE;
  126. int ret = libusb_submit_transfer(transfer_out);
  127. if (ret) {
  128. msg_perr("%s: failed to submit OUT transfer: %s\n", func, libusb_error_name(ret));
  129. state_out = TRANS_ERR;
  130. goto err;
  131. }
  132. }
  133. /* Handle all asynchronous packets as long as we have stuff to write or read. The write(s) simply need
  134. * to complete but we need to scheduling reads as long as we are not done. */
  135. unsigned int free_idx = 0; /* The IN transfer we expect to be free next. */
  136. unsigned int in_idx = 0; /* The IN transfer we expect to be completed next. */
  137. unsigned int in_done = 0;
  138. unsigned int in_active = 0;
  139. unsigned int out_done = 0;
  140. uint8_t *in_buf = readarr;
  141. int state_in[USB_IN_TRANSFERS] = {0};
  142. do {
  143. /* Schedule new reads as long as there are free transfers and unscheduled bytes to read. */
  144. while ((in_done + in_active) < readcnt && state_in[free_idx] == TRANS_IDLE) {
  145. unsigned int cur_todo = min(CH341_PACKET_LENGTH - 1, readcnt - in_done - in_active);
  146. transfer_ins[free_idx]->length = cur_todo;
  147. transfer_ins[free_idx]->buffer = in_buf;
  148. transfer_ins[free_idx]->user_data = &state_in[free_idx];
  149. int ret = libusb_submit_transfer(transfer_ins[free_idx]);
  150. if (ret) {
  151. state_in[free_idx] = TRANS_ERR;
  152. msg_perr("%s: failed to submit IN transfer: %s\n",
  153. func, libusb_error_name(ret));
  154. goto err;
  155. }
  156. in_buf += cur_todo;
  157. in_active += cur_todo;
  158. state_in[free_idx] = TRANS_ACTIVE;
  159. free_idx = (free_idx + 1) % USB_IN_TRANSFERS; /* Increment (and wrap around). */
  160. }
  161. /* Actually get some work done. */
  162. libusb_handle_events_timeout(NULL, &(struct timeval){1, 0});
  163. /* Check for the write */
  164. if (out_done < writecnt) {
  165. if (state_out == TRANS_ERR) {
  166. goto err;
  167. } else if (state_out > 0) {
  168. out_done += state_out;
  169. state_out = TRANS_IDLE;
  170. }
  171. }
  172. /* Check for completed transfers. */
  173. while (state_in[in_idx] != TRANS_IDLE && state_in[in_idx] != TRANS_ACTIVE) {
  174. if (state_in[in_idx] == TRANS_ERR) {
  175. goto err;
  176. }
  177. /* If a transfer is done, record the number of bytes read and reuse it later. */
  178. in_done += state_in[in_idx];
  179. in_active -= state_in[in_idx];
  180. state_in[in_idx] = TRANS_IDLE;
  181. in_idx = (in_idx + 1) % USB_IN_TRANSFERS; /* Increment (and wrap around). */
  182. }
  183. } while ((out_done < writecnt) || (in_done < readcnt));
  184. if (out_done > 0) {
  185. msg_pspew("Wrote %d bytes:\n", out_done);
  186. print_hex(writearr, out_done);
  187. msg_pspew("\n\n");
  188. }
  189. if (in_done > 0) {
  190. msg_pspew("Read %d bytes:\n", in_done);
  191. print_hex(readarr, in_done);
  192. msg_pspew("\n\n");
  193. }
  194. return 0;
  195. err:
  196. /* Clean up on errors. */
  197. msg_perr("%s: Failed to %s %d bytes\n", func, (state_out == TRANS_ERR) ? "write" : "read",
  198. (state_out == TRANS_ERR) ? writecnt : readcnt);
  199. /* First, we must cancel any ongoing requests and wait for them to be canceled. */
  200. if ((writecnt > 0) && (state_out == TRANS_ACTIVE)) {
  201. if (libusb_cancel_transfer(transfer_out) != 0)
  202. state_out = TRANS_ERR;
  203. }
  204. if (readcnt > 0) {
  205. unsigned int i;
  206. for (i = 0; i < USB_IN_TRANSFERS; i++) {
  207. if (state_in[i] == TRANS_ACTIVE)
  208. if (libusb_cancel_transfer(transfer_ins[i]) != 0)
  209. state_in[i] = TRANS_ERR;
  210. }
  211. }
  212. /* Wait for cancellations to complete. */
  213. while (1) {
  214. bool finished = true;
  215. if ((writecnt > 0) && (state_out == TRANS_ACTIVE))
  216. finished = false;
  217. if (readcnt > 0) {
  218. unsigned int i;
  219. for (i = 0; i < USB_IN_TRANSFERS; i++) {
  220. if (state_in[i] == TRANS_ACTIVE)
  221. finished = false;
  222. }
  223. }
  224. if (finished)
  225. break;
  226. libusb_handle_events_timeout(NULL, &(struct timeval){1, 0});
  227. }
  228. return -1;
  229. }
  230. /* Set the I2C bus speed (speed(b1b0): 0 = 20kHz; 1 = 100kHz, 2 = 400kHz, 3 = 750kHz).
  231. * Set the SPI bus data width (speed(b2): 0 = Single, 1 = Double). */
  232. static int32_t config_stream(uint32_t speed)
  233. {
  234. if (handle == NULL)
  235. return -1;
  236. uint8_t buf[] = {
  237. CH341A_CMD_I2C_STREAM,
  238. CH341A_CMD_I2C_STM_SET | (speed & 0x7),
  239. CH341A_CMD_I2C_STM_END
  240. };
  241. int32_t ret = usb_transfer(__func__, sizeof(buf), 0, buf, NULL);
  242. if (ret < 0) {
  243. msg_perr("Could not configure stream interface.\n");
  244. }
  245. return ret;
  246. }
  247. /* ch341 requires LSB first, swap the bit order before send and after receive */
  248. static uint8_t swap_byte(uint8_t x)
  249. {
  250. x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
  251. x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
  252. x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
  253. return x;
  254. }
  255. /* The assumed map between UIO command bits, pins on CH341A chip and pins on SPI chip:
  256. * UIO CH341A SPI CH341A SPI name
  257. * 0 D0/15 CS/1 (CS0)
  258. * 1 D1/16 unused (CS1)
  259. * 2 D2/17 unused (CS2)
  260. * 3 D3/18 SCK/6 (DCK)
  261. * 4 D4/19 unused (DOUT2)
  262. * 5 D5/20 SI/5 (DOUT)
  263. * - The UIO stream commands seem to only have 6 bits of output, and D6/D7 are the SPI inputs,
  264. * mapped as follows:
  265. * D6/21 unused (DIN2)
  266. * D7/22 SO/2 (DIN)
  267. */
  268. static int32_t enable_pins(bool enable)
  269. {
  270. uint8_t buf[] = {
  271. CH341A_CMD_UIO_STREAM,
  272. CH341A_CMD_UIO_STM_OUT | 0x37, // CS high (all of them), SCK=0, DOUT*=1
  273. CH341A_CMD_UIO_STM_DIR | (enable ? 0x3F : 0x00), // Interface output enable / disable
  274. CH341A_CMD_UIO_STM_END,
  275. };
  276. int32_t ret = usb_transfer(__func__, sizeof(buf), 0, buf, NULL);
  277. if (ret < 0) {
  278. msg_perr("Could not %sable output pins.\n", enable ? "en" : "dis");
  279. }
  280. return ret;
  281. }
  282. /* De-assert and assert CS in one operation. */
  283. static void pluck_cs(uint8_t *ptr)
  284. {
  285. /* This was measured to give a minumum deassertion time of 2.25 us,
  286. * >20x more than needed for most SPI chips (100ns). */
  287. int delay_cnt = 2;
  288. if (stored_delay_us) {
  289. delay_cnt = (stored_delay_us * 4) / 3;
  290. stored_delay_us = 0;
  291. }
  292. *ptr++ = CH341A_CMD_UIO_STREAM;
  293. *ptr++ = CH341A_CMD_UIO_STM_OUT | 0x37; /* deasserted */
  294. int i;
  295. for (i = 0; i < delay_cnt; i++)
  296. *ptr++ = CH341A_CMD_UIO_STM_OUT | 0x37; /* "delay" */
  297. *ptr++ = CH341A_CMD_UIO_STM_OUT | 0x36; /* asserted */
  298. *ptr++ = CH341A_CMD_UIO_STM_END;
  299. }
  300. void ch341a_spi_delay(unsigned int usecs)
  301. {
  302. /* There is space for 28 bytes instructions of 750 ns each in the CS packet (32 - 4 for the actual CS
  303. * instructions), thus max 21 us, but we avoid getting too near to this boundary and use
  304. * internal_delay() for durations over 20 us. */
  305. if ((usecs + stored_delay_us) > 20) {
  306. unsigned int inc = 20 - stored_delay_us;
  307. internal_delay(usecs - inc);
  308. usecs = inc;
  309. }
  310. stored_delay_us += usecs;
  311. }
  312. static int ch341a_spi_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
  313. {
  314. if (handle == NULL)
  315. return -1;
  316. /* How many packets ... */
  317. const size_t packets = (writecnt + readcnt + CH341_PACKET_LENGTH - 2) / (CH341_PACKET_LENGTH - 1);
  318. /* We pluck CS/timeout handling into the first packet thus we need to allocate one extra package. */
  319. uint8_t wbuf[packets+1][CH341_PACKET_LENGTH];
  320. uint8_t rbuf[writecnt + readcnt];
  321. /* Initialize the write buffer to zero to prevent writing random stack contents to device. */
  322. memset(wbuf[0], 0, CH341_PACKET_LENGTH);
  323. uint8_t *ptr = wbuf[0];
  324. /* CS usage is optimized by doing both transitions in one packet.
  325. * Final transition to deselected state is in the pin disable. */
  326. pluck_cs(ptr);
  327. unsigned int write_left = writecnt;
  328. unsigned int read_left = readcnt;
  329. unsigned int p;
  330. for (p = 0; p < packets; p++) {
  331. unsigned int write_now = min(CH341_PACKET_LENGTH - 1, write_left);
  332. unsigned int read_now = min ((CH341_PACKET_LENGTH - 1) - write_now, read_left);
  333. ptr = wbuf[p+1];
  334. *ptr++ = CH341A_CMD_SPI_STREAM;
  335. unsigned int i;
  336. for (i = 0; i < write_now; ++i)
  337. *ptr++ = swap_byte(*writearr++);
  338. if (read_now) {
  339. memset(ptr, 0xFF, read_now);
  340. read_left -= read_now;
  341. }
  342. write_left -= write_now;
  343. }
  344. int32_t ret = usb_transfer(__func__, CH341_PACKET_LENGTH + packets + writecnt + readcnt,
  345. writecnt + readcnt, wbuf[0], rbuf);
  346. if (ret < 0)
  347. return -1;
  348. unsigned int i;
  349. for (i = 0; i < readcnt; i++) {
  350. *readarr++ = swap_byte(rbuf[writecnt + i]);
  351. }
  352. return 0;
  353. }
  354. static const struct spi_master spi_master_ch341a_spi = {
  355. .type = SPI_CONTROLLER_CH341A_SPI,
  356. /* flashrom's current maximum is 256 B. CH341A was tested on Linux and Windows to accept atleast
  357. * 128 kB. Basically there should be no hard limit because transfers are broken up into USB packets
  358. * sent to the device and most of their payload streamed via SPI. */
  359. .max_data_read = 4 * 1024,
  360. .max_data_write = 4 * 1024,
  361. .command = ch341a_spi_spi_send_command,
  362. .multicommand = default_spi_send_multicommand,
  363. .read = default_spi_read,
  364. .write_256 = default_spi_write_256,
  365. .write_aai = default_spi_write_aai,
  366. };
  367. static int ch341a_spi_shutdown(void *data)
  368. {
  369. if (handle == NULL)
  370. return -1;
  371. enable_pins(false);
  372. libusb_free_transfer(transfer_out);
  373. transfer_out = NULL;
  374. int i;
  375. for (i = 0; i < USB_IN_TRANSFERS; i++) {
  376. libusb_free_transfer(transfer_ins[i]);
  377. transfer_ins[i] = NULL;
  378. }
  379. libusb_release_interface(handle, 0);
  380. libusb_close(handle);
  381. libusb_exit(NULL);
  382. handle = NULL;
  383. return 0;
  384. }
  385. int ch341a_spi_init(void)
  386. {
  387. if (handle != NULL) {
  388. msg_cerr("%s: handle already set! Please report a bug at flashrom@flashrom.org\n", __func__);
  389. return -1;
  390. }
  391. int32_t ret = libusb_init(NULL);
  392. if (ret < 0) {
  393. msg_perr("Couldnt initialize libusb!\n");
  394. return -1;
  395. }
  396. libusb_set_debug(NULL, 3); // Enable information, warning and error messages (only).
  397. uint16_t vid = devs_ch341a_spi[0].vendor_id;
  398. uint16_t pid = devs_ch341a_spi[0].device_id;
  399. handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
  400. if (handle == NULL) {
  401. msg_perr("Couldn't open device %04x:%04x.\n", vid, pid);
  402. return -1;
  403. }
  404. /* libusb_detach_kernel_driver() and friends basically only work on Linux. We simply try to detach on Linux
  405. * without a lot of passion here. If that works fine else we will fail on claiming the interface anyway. */
  406. #if IS_LINUX
  407. ret = libusb_detach_kernel_driver(handle, 0);
  408. if (ret == LIBUSB_ERROR_NOT_SUPPORTED) {
  409. msg_pwarn("Detaching kernel drivers is not supported. Further accesses may fail.\n");
  410. } else if (ret != 0 && ret != LIBUSB_ERROR_NOT_FOUND) {
  411. msg_pwarn("Failed to detach kernel driver: '%s'. Further accesses will probably fail.\n",
  412. libusb_error_name(ret));
  413. }
  414. #endif
  415. ret = libusb_claim_interface(handle, 0);
  416. if (ret != 0) {
  417. msg_perr("Failed to claim interface 0: '%s'\n", libusb_error_name(ret));
  418. goto close_handle;
  419. }
  420. struct libusb_device *dev;
  421. if (!(dev = libusb_get_device(handle))) {
  422. msg_perr("Failed to get device from device handle.\n");
  423. goto close_handle;
  424. }
  425. struct libusb_device_descriptor desc;
  426. ret = libusb_get_device_descriptor(dev, &desc);
  427. if (ret < 0) {
  428. msg_perr("Failed to get device descriptor: '%s'\n", libusb_error_name(ret));
  429. goto release_interface;
  430. }
  431. msg_pdbg("Device revision is %d.%01d.%01d\n",
  432. (desc.bcdDevice >> 8) & 0x00FF,
  433. (desc.bcdDevice >> 4) & 0x000F,
  434. (desc.bcdDevice >> 0) & 0x000F);
  435. /* Allocate and pre-fill transfer structures. */
  436. transfer_out = libusb_alloc_transfer(0);
  437. if (!transfer_out) {
  438. msg_perr("Failed to alloc libusb OUT transfer\n");
  439. goto release_interface;
  440. }
  441. int i;
  442. for (i = 0; i < USB_IN_TRANSFERS; i++) {
  443. transfer_ins[i] = libusb_alloc_transfer(0);
  444. if (transfer_ins[i] == NULL) {
  445. msg_perr("Failed to alloc libusb IN transfer %d\n", i);
  446. goto dealloc_transfers;
  447. }
  448. }
  449. /* We use these helpers but dont fill the actual buffer yet. */
  450. libusb_fill_bulk_transfer(transfer_out, handle, WRITE_EP, NULL, 0, cb_out, NULL, USB_TIMEOUT);
  451. for (i = 0; i < USB_IN_TRANSFERS; i++)
  452. libusb_fill_bulk_transfer(transfer_ins[i], handle, READ_EP, NULL, 0, cb_in, NULL, USB_TIMEOUT);
  453. if ((config_stream(CH341A_STM_I2C_100K) < 0) || (enable_pins(true) < 0))
  454. goto dealloc_transfers;
  455. register_shutdown(ch341a_spi_shutdown, NULL);
  456. register_spi_master(&spi_master_ch341a_spi);
  457. return 0;
  458. dealloc_transfers:
  459. for (i = 0; i < USB_IN_TRANSFERS; i++) {
  460. if (transfer_ins[i] == NULL)
  461. break;
  462. libusb_free_transfer(transfer_ins[i]);
  463. transfer_ins[i] = NULL;
  464. }
  465. libusb_free_transfer(transfer_out);
  466. transfer_out = NULL;
  467. release_interface:
  468. libusb_release_interface(handle, 0);
  469. close_handle:
  470. libusb_close(handle);
  471. handle = NULL;
  472. return -1;
  473. }