sync.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
  2. /*
  3. * Synchronous I/O functions for libusb
  4. * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
  5. * Copyright © 2019 Nathan Hjelm <hjelmn@cs.unm.edu>
  6. * Copyright © 2019 Google LLC. All rights reserved.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libusbi.h"
  23. #include <string.h>
  24. /**
  25. * @defgroup libusb_syncio Synchronous device I/O
  26. *
  27. * This page documents libusb's synchronous (blocking) API for USB device I/O.
  28. * This interface is easy to use but has some limitations. More advanced users
  29. * may wish to consider using the \ref libusb_asyncio "asynchronous I/O API" instead.
  30. */
  31. static void LIBUSB_CALL sync_transfer_cb(struct libusb_transfer *transfer)
  32. {
  33. int *completed = transfer->user_data;
  34. *completed = 1;
  35. usbi_dbg(TRANSFER_CTX(transfer), "actual_length=%d", transfer->actual_length);
  36. /* caller interprets result and frees transfer */
  37. }
  38. static void sync_transfer_wait_for_completion(struct libusb_transfer *transfer)
  39. {
  40. int r, *completed = transfer->user_data;
  41. struct libusb_context *ctx = HANDLE_CTX(transfer->dev_handle);
  42. while (!*completed) {
  43. r = libusb_handle_events_completed(ctx, completed);
  44. if (r < 0) {
  45. if (r == LIBUSB_ERROR_INTERRUPTED)
  46. continue;
  47. usbi_err(ctx, "libusb_handle_events failed: %s, cancelling transfer and retrying",
  48. libusb_error_name(r));
  49. libusb_cancel_transfer(transfer);
  50. continue;
  51. }
  52. if (NULL == transfer->dev_handle) {
  53. /* transfer completion after libusb_close() */
  54. transfer->status = LIBUSB_TRANSFER_NO_DEVICE;
  55. *completed = 1;
  56. }
  57. }
  58. }
  59. /** \ingroup libusb_syncio
  60. * Perform a USB control transfer.
  61. *
  62. * The direction of the transfer is inferred from the bmRequestType field of
  63. * the setup packet.
  64. *
  65. * The wValue, wIndex and wLength fields values should be given in host-endian
  66. * byte order.
  67. *
  68. * \param dev_handle a handle for the device to communicate with
  69. * \param bmRequestType the request type field for the setup packet
  70. * \param bRequest the request field for the setup packet
  71. * \param wValue the value field for the setup packet
  72. * \param wIndex the index field for the setup packet
  73. * \param data a suitably-sized data buffer for either input or output
  74. * (depending on direction bits within bmRequestType)
  75. * \param wLength the length field for the setup packet. The data buffer should
  76. * be at least this size.
  77. * \param timeout timeout (in milliseconds) that this function should wait
  78. * before giving up due to no response being received. For an unlimited
  79. * timeout, use value 0.
  80. * \returns on success, the number of bytes actually transferred
  81. * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
  82. * \returns LIBUSB_ERROR_PIPE if the control request was not supported by the
  83. * device
  84. * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
  85. * \returns LIBUSB_ERROR_BUSY if called from event handling context
  86. * \returns LIBUSB_ERROR_INVALID_PARAM if the transfer size is larger than
  87. * the operating system and/or hardware can support (see \ref asynclimits)
  88. * \returns another LIBUSB_ERROR code on other failures
  89. */
  90. int API_EXPORTED libusb_control_transfer(libusb_device_handle *dev_handle,
  91. uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
  92. unsigned char *data, uint16_t wLength, unsigned int timeout)
  93. {
  94. struct libusb_transfer *transfer;
  95. unsigned char *buffer;
  96. int completed = 0;
  97. int r;
  98. if (usbi_handling_events(HANDLE_CTX(dev_handle)))
  99. return LIBUSB_ERROR_BUSY;
  100. transfer = libusb_alloc_transfer(0);
  101. if (!transfer)
  102. return LIBUSB_ERROR_NO_MEM;
  103. buffer = malloc(LIBUSB_CONTROL_SETUP_SIZE + wLength);
  104. if (!buffer) {
  105. libusb_free_transfer(transfer);
  106. return LIBUSB_ERROR_NO_MEM;
  107. }
  108. libusb_fill_control_setup(buffer, bmRequestType, bRequest, wValue, wIndex,
  109. wLength);
  110. if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
  111. memcpy(buffer + LIBUSB_CONTROL_SETUP_SIZE, data, wLength);
  112. libusb_fill_control_transfer(transfer, dev_handle, buffer,
  113. sync_transfer_cb, &completed, timeout);
  114. transfer->flags = LIBUSB_TRANSFER_FREE_BUFFER;
  115. r = libusb_submit_transfer(transfer);
  116. if (r < 0) {
  117. libusb_free_transfer(transfer);
  118. return r;
  119. }
  120. sync_transfer_wait_for_completion(transfer);
  121. if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
  122. memcpy(data, libusb_control_transfer_get_data(transfer),
  123. transfer->actual_length);
  124. switch (transfer->status) {
  125. case LIBUSB_TRANSFER_COMPLETED:
  126. r = transfer->actual_length;
  127. break;
  128. case LIBUSB_TRANSFER_TIMED_OUT:
  129. r = LIBUSB_ERROR_TIMEOUT;
  130. break;
  131. case LIBUSB_TRANSFER_STALL:
  132. r = LIBUSB_ERROR_PIPE;
  133. break;
  134. case LIBUSB_TRANSFER_NO_DEVICE:
  135. r = LIBUSB_ERROR_NO_DEVICE;
  136. break;
  137. case LIBUSB_TRANSFER_OVERFLOW:
  138. r = LIBUSB_ERROR_OVERFLOW;
  139. break;
  140. case LIBUSB_TRANSFER_ERROR:
  141. case LIBUSB_TRANSFER_CANCELLED:
  142. r = LIBUSB_ERROR_IO;
  143. break;
  144. default:
  145. usbi_warn(HANDLE_CTX(dev_handle),
  146. "unrecognised status code %d", transfer->status);
  147. r = LIBUSB_ERROR_OTHER;
  148. }
  149. libusb_free_transfer(transfer);
  150. return r;
  151. }
  152. static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
  153. unsigned char endpoint, unsigned char *buffer, int length,
  154. int *transferred, unsigned int timeout, unsigned char type)
  155. {
  156. struct libusb_transfer *transfer;
  157. int completed = 0;
  158. int r;
  159. if (usbi_handling_events(HANDLE_CTX(dev_handle)))
  160. return LIBUSB_ERROR_BUSY;
  161. transfer = libusb_alloc_transfer(0);
  162. if (!transfer)
  163. return LIBUSB_ERROR_NO_MEM;
  164. libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
  165. sync_transfer_cb, &completed, timeout);
  166. transfer->type = type;
  167. r = libusb_submit_transfer(transfer);
  168. if (r < 0) {
  169. libusb_free_transfer(transfer);
  170. return r;
  171. }
  172. sync_transfer_wait_for_completion(transfer);
  173. if (transferred)
  174. *transferred = transfer->actual_length;
  175. switch (transfer->status) {
  176. case LIBUSB_TRANSFER_COMPLETED:
  177. r = 0;
  178. break;
  179. case LIBUSB_TRANSFER_TIMED_OUT:
  180. r = LIBUSB_ERROR_TIMEOUT;
  181. break;
  182. case LIBUSB_TRANSFER_STALL:
  183. r = LIBUSB_ERROR_PIPE;
  184. break;
  185. case LIBUSB_TRANSFER_OVERFLOW:
  186. r = LIBUSB_ERROR_OVERFLOW;
  187. break;
  188. case LIBUSB_TRANSFER_NO_DEVICE:
  189. r = LIBUSB_ERROR_NO_DEVICE;
  190. break;
  191. case LIBUSB_TRANSFER_ERROR:
  192. case LIBUSB_TRANSFER_CANCELLED:
  193. r = LIBUSB_ERROR_IO;
  194. break;
  195. default:
  196. usbi_warn(HANDLE_CTX(dev_handle),
  197. "unrecognised status code %d", transfer->status);
  198. r = LIBUSB_ERROR_OTHER;
  199. }
  200. libusb_free_transfer(transfer);
  201. return r;
  202. }
  203. /** \ingroup libusb_syncio
  204. * Perform a USB bulk transfer. The direction of the transfer is inferred from
  205. * the direction bits of the endpoint address.
  206. *
  207. * For bulk reads, the <tt>length</tt> field indicates the maximum length of
  208. * data you are expecting to receive. If less data arrives than expected,
  209. * this function will return that data, so be sure to check the
  210. * <tt>transferred</tt> output parameter.
  211. *
  212. * You should also check the <tt>transferred</tt> parameter for bulk writes.
  213. * Not all of the data may have been written.
  214. *
  215. * Also check <tt>transferred</tt> when dealing with a timeout error code.
  216. * libusb may have to split your transfer into a number of chunks to satisfy
  217. * underlying O/S requirements, meaning that the timeout may expire after
  218. * the first few chunks have completed. libusb is careful not to lose any data
  219. * that may have been transferred; do not assume that timeout conditions
  220. * indicate a complete lack of I/O. See \ref asynctimeout for more details.
  221. *
  222. * \param dev_handle a handle for the device to communicate with
  223. * \param endpoint the address of a valid endpoint to communicate with
  224. * \param data a suitably-sized data buffer for either input or output
  225. * (depending on endpoint)
  226. * \param length for bulk writes, the number of bytes from data to be sent. for
  227. * bulk reads, the maximum number of bytes to receive into the data buffer.
  228. * \param transferred output location for the number of bytes actually
  229. * transferred. Since version 1.0.21 (\ref LIBUSB_API_VERSION >= 0x01000105),
  230. * it is legal to pass a NULL pointer if you do not wish to receive this
  231. * information.
  232. * \param timeout timeout (in milliseconds) that this function should wait
  233. * before giving up due to no response being received. For an unlimited
  234. * timeout, use value 0.
  235. *
  236. * \returns 0 on success (and populates <tt>transferred</tt>)
  237. * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates
  238. * <tt>transferred</tt>)
  239. * \returns LIBUSB_ERROR_PIPE if the endpoint halted
  240. * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
  241. * \ref libusb_packetoverflow
  242. * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
  243. * \returns LIBUSB_ERROR_BUSY if called from event handling context
  244. * \returns LIBUSB_ERROR_INVALID_PARAM if the transfer size is larger than
  245. * the operating system and/or hardware can support (see \ref asynclimits)
  246. * \returns another LIBUSB_ERROR code on other failures
  247. */
  248. int API_EXPORTED libusb_bulk_transfer(libusb_device_handle *dev_handle,
  249. unsigned char endpoint, unsigned char *data, int length,
  250. int *transferred, unsigned int timeout)
  251. {
  252. return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
  253. transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
  254. }
  255. /** \ingroup libusb_syncio
  256. * Perform a USB interrupt transfer. The direction of the transfer is inferred
  257. * from the direction bits of the endpoint address.
  258. *
  259. * For interrupt reads, the <tt>length</tt> field indicates the maximum length
  260. * of data you are expecting to receive. If less data arrives than expected,
  261. * this function will return that data, so be sure to check the
  262. * <tt>transferred</tt> output parameter.
  263. *
  264. * You should also check the <tt>transferred</tt> parameter for interrupt
  265. * writes. Not all of the data may have been written.
  266. *
  267. * Also check <tt>transferred</tt> when dealing with a timeout error code.
  268. * libusb may have to split your transfer into a number of chunks to satisfy
  269. * underlying O/S requirements, meaning that the timeout may expire after
  270. * the first few chunks have completed. libusb is careful not to lose any data
  271. * that may have been transferred; do not assume that timeout conditions
  272. * indicate a complete lack of I/O. See \ref asynctimeout for more details.
  273. *
  274. * The default endpoint bInterval value is used as the polling interval.
  275. *
  276. * \param dev_handle a handle for the device to communicate with
  277. * \param endpoint the address of a valid endpoint to communicate with
  278. * \param data a suitably-sized data buffer for either input or output
  279. * (depending on endpoint)
  280. * \param length for bulk writes, the number of bytes from data to be sent. for
  281. * bulk reads, the maximum number of bytes to receive into the data buffer.
  282. * \param transferred output location for the number of bytes actually
  283. * transferred. Since version 1.0.21 (\ref LIBUSB_API_VERSION >= 0x01000105),
  284. * it is legal to pass a NULL pointer if you do not wish to receive this
  285. * information.
  286. * \param timeout timeout (in milliseconds) that this function should wait
  287. * before giving up due to no response being received. For an unlimited
  288. * timeout, use value 0.
  289. *
  290. * \returns 0 on success (and populates <tt>transferred</tt>)
  291. * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
  292. * \returns LIBUSB_ERROR_PIPE if the endpoint halted
  293. * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
  294. * \ref libusb_packetoverflow
  295. * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
  296. * \returns LIBUSB_ERROR_BUSY if called from event handling context
  297. * \returns LIBUSB_ERROR_INVALID_PARAM if the transfer size is larger than
  298. * the operating system and/or hardware can support (see \ref asynclimits)
  299. * \returns another LIBUSB_ERROR code on other error
  300. */
  301. int API_EXPORTED libusb_interrupt_transfer(libusb_device_handle *dev_handle,
  302. unsigned char endpoint, unsigned char *data, int length,
  303. int *transferred, unsigned int timeout)
  304. {
  305. return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
  306. transferred, timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
  307. }