sync.c 11 KB

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