sync.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Synchronous I/O functions for libusb
  3. * Copyright (C) 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 libusb'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 ctrl_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. /** \ingroup syncio
  40. * Perform a USB control transfer.
  41. *
  42. * The direction of the transfer is inferred from the bmRequestType field of
  43. * the setup packet.
  44. *
  45. * The wValue, wIndex and wLength fields values should be given in host-endian
  46. * byte order.
  47. *
  48. * \param dev_handle a handle for the device to communicate with
  49. * \param bmRequestType the request type field for the setup packet
  50. * \param bRequest the request field for the setup packet
  51. * \param wValue the value field for the setup packet
  52. * \param wIndex the index field for the setup packet
  53. * \param data a suitably-sized data buffer for either input or output
  54. * (depending on direction bits within bmRequestType)
  55. * \param wLength the length field for the setup packet. The data buffer should
  56. * be at least this size.
  57. * \param timeout timeout (in millseconds) that this function should wait
  58. * before giving up due to no response being received. For an unlimited
  59. * timeout, use value 0.
  60. * \returns on success, the number of bytes actually transferred
  61. * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
  62. * \returns LIBUSB_ERROR_PIPE if the control request was not supported by the
  63. * device
  64. * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
  65. * \returns another LIBUSB_ERROR code on other failures
  66. */
  67. int API_EXPORTED libusb_control_transfer(libusb_device_handle *dev_handle,
  68. uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
  69. unsigned char *data, uint16_t wLength, unsigned int timeout)
  70. {
  71. struct libusb_transfer *transfer = libusb_alloc_transfer(0);
  72. unsigned char *buffer;
  73. int completed = 0;
  74. int r;
  75. if (!transfer)
  76. return LIBUSB_ERROR_NO_MEM;
  77. buffer = malloc(LIBUSB_CONTROL_SETUP_SIZE + wLength);
  78. if (!buffer) {
  79. libusb_free_transfer(transfer);
  80. return LIBUSB_ERROR_NO_MEM;
  81. }
  82. libusb_fill_control_setup(buffer, bmRequestType, bRequest, wValue, wIndex,
  83. wLength);
  84. if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
  85. memcpy(buffer + LIBUSB_CONTROL_SETUP_SIZE, data, wLength);
  86. libusb_fill_control_transfer(transfer, dev_handle, buffer,
  87. ctrl_transfer_cb, &completed, timeout);
  88. transfer->flags = LIBUSB_TRANSFER_FREE_BUFFER;
  89. r = libusb_submit_transfer(transfer);
  90. if (r < 0) {
  91. libusb_free_transfer(transfer);
  92. return r;
  93. }
  94. while (!completed) {
  95. r = libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed);
  96. if (r < 0) {
  97. if (r == LIBUSB_ERROR_INTERRUPTED)
  98. continue;
  99. libusb_cancel_transfer(transfer);
  100. while (!completed)
  101. if (libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed) < 0)
  102. break;
  103. libusb_free_transfer(transfer);
  104. return r;
  105. }
  106. }
  107. if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
  108. memcpy(data, libusb_control_transfer_get_data(transfer),
  109. transfer->actual_length);
  110. switch (transfer->status) {
  111. case LIBUSB_TRANSFER_COMPLETED:
  112. r = transfer->actual_length;
  113. break;
  114. case LIBUSB_TRANSFER_TIMED_OUT:
  115. r = LIBUSB_ERROR_TIMEOUT;
  116. break;
  117. case LIBUSB_TRANSFER_STALL:
  118. r = LIBUSB_ERROR_PIPE;
  119. break;
  120. case LIBUSB_TRANSFER_NO_DEVICE:
  121. r = LIBUSB_ERROR_NO_DEVICE;
  122. break;
  123. case LIBUSB_TRANSFER_OVERFLOW:
  124. r = LIBUSB_ERROR_OVERFLOW;
  125. break;
  126. case LIBUSB_TRANSFER_ERROR:
  127. case LIBUSB_TRANSFER_CANCELLED:
  128. r = LIBUSB_ERROR_IO;
  129. break;
  130. default:
  131. usbi_warn(HANDLE_CTX(dev_handle),
  132. "unrecognised status code %d", transfer->status);
  133. r = LIBUSB_ERROR_OTHER;
  134. }
  135. libusb_free_transfer(transfer);
  136. return r;
  137. }
  138. static void LIBUSB_CALL bulk_transfer_cb(struct libusb_transfer *transfer)
  139. {
  140. int *completed = transfer->user_data;
  141. *completed = 1;
  142. usbi_dbg("actual_length=%d", transfer->actual_length);
  143. /* caller interprets results and frees transfer */
  144. }
  145. static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
  146. unsigned char endpoint, unsigned char *buffer, int length,
  147. int *transferred, unsigned int timeout, unsigned char type)
  148. {
  149. struct libusb_transfer *transfer = libusb_alloc_transfer(0);
  150. int completed = 0;
  151. int r;
  152. if (!transfer)
  153. return LIBUSB_ERROR_NO_MEM;
  154. libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
  155. bulk_transfer_cb, &completed, timeout);
  156. transfer->type = type;
  157. r = libusb_submit_transfer(transfer);
  158. if (r < 0) {
  159. libusb_free_transfer(transfer);
  160. return r;
  161. }
  162. while (!completed) {
  163. r = libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed);
  164. if (r < 0) {
  165. if (r == LIBUSB_ERROR_INTERRUPTED)
  166. continue;
  167. libusb_cancel_transfer(transfer);
  168. while (!completed)
  169. if (libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed) < 0)
  170. break;
  171. libusb_free_transfer(transfer);
  172. return r;
  173. }
  174. }
  175. *transferred = transfer->actual_length;
  176. switch (transfer->status) {
  177. case LIBUSB_TRANSFER_COMPLETED:
  178. r = 0;
  179. break;
  180. case LIBUSB_TRANSFER_TIMED_OUT:
  181. r = LIBUSB_ERROR_TIMEOUT;
  182. break;
  183. case LIBUSB_TRANSFER_STALL:
  184. r = LIBUSB_ERROR_PIPE;
  185. break;
  186. case LIBUSB_TRANSFER_OVERFLOW:
  187. r = LIBUSB_ERROR_OVERFLOW;
  188. break;
  189. case LIBUSB_TRANSFER_NO_DEVICE:
  190. r = LIBUSB_ERROR_NO_DEVICE;
  191. break;
  192. case LIBUSB_TRANSFER_ERROR:
  193. case LIBUSB_TRANSFER_CANCELLED:
  194. r = LIBUSB_ERROR_IO;
  195. break;
  196. default:
  197. usbi_warn(HANDLE_CTX(dev_handle),
  198. "unrecognised status code %d", transfer->status);
  199. r = LIBUSB_ERROR_OTHER;
  200. }
  201. libusb_free_transfer(transfer);
  202. return r;
  203. }
  204. /** \ingroup syncio
  205. * Perform a USB bulk transfer. The direction of the transfer is inferred from
  206. * the direction bits of the endpoint address.
  207. *
  208. * For bulk reads, the <tt>length</tt> field indicates the maximum length of
  209. * data you are expecting to receive. If less data arrives than expected,
  210. * this function will return that data, so be sure to check the
  211. * <tt>transferred</tt> output parameter.
  212. *
  213. * You should also check the <tt>transferred</tt> parameter for bulk writes.
  214. * Not all of the data may have been written.
  215. *
  216. * Also check <tt>transferred</tt> when dealing with a timeout error code.
  217. * libusb may have to split your transfer into a number of chunks to satisfy
  218. * underlying O/S requirements, meaning that the timeout may expire after
  219. * the first few chunks have completed. libusb is careful not to lose any data
  220. * that may have been transferred; do not assume that timeout conditions
  221. * indicate a complete lack of I/O.
  222. *
  223. * \param dev_handle a handle for the device to communicate with
  224. * \param endpoint the address of a valid endpoint to communicate with
  225. * \param data a suitably-sized data buffer for either input or output
  226. * (depending on endpoint)
  227. * \param length for bulk writes, the number of bytes from data to be sent. for
  228. * bulk reads, the maximum number of bytes to receive into the data buffer.
  229. * \param transferred output location for the number of bytes actually
  230. * transferred.
  231. * \param timeout timeout (in millseconds) that this function should wait
  232. * before giving up due to no response being received. For an unlimited
  233. * timeout, use value 0.
  234. *
  235. * \returns 0 on success (and populates <tt>transferred</tt>)
  236. * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates
  237. * <tt>transferred</tt>)
  238. * \returns LIBUSB_ERROR_PIPE if the endpoint halted
  239. * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
  240. * \ref packetoverflow
  241. * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
  242. * \returns another LIBUSB_ERROR code on other failures
  243. */
  244. int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
  245. unsigned char endpoint, unsigned char *data, int length, int *transferred,
  246. unsigned int timeout)
  247. {
  248. return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
  249. transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
  250. }
  251. /** \ingroup syncio
  252. * Perform a USB interrupt transfer. The direction of the transfer is inferred
  253. * from the direction bits of the endpoint address.
  254. *
  255. * For interrupt reads, the <tt>length</tt> field indicates the maximum length
  256. * of data you are expecting to receive. If less data arrives than expected,
  257. * this function will return that data, so be sure to check the
  258. * <tt>transferred</tt> output parameter.
  259. *
  260. * You should also check the <tt>transferred</tt> parameter for interrupt
  261. * writes. Not all of the data may have been written.
  262. *
  263. * Also check <tt>transferred</tt> when dealing with a timeout error code.
  264. * libusb may have to split your transfer into a number of chunks to satisfy
  265. * underlying O/S requirements, meaning that the timeout may expire after
  266. * the first few chunks have completed. libusb is careful not to lose any data
  267. * that may have been transferred; do not assume that timeout conditions
  268. * indicate a complete lack of I/O.
  269. *
  270. * The default endpoint bInterval value is used as the polling interval.
  271. *
  272. * \param dev_handle a handle for the device to communicate with
  273. * \param endpoint the address of a valid endpoint to communicate with
  274. * \param data a suitably-sized data buffer for either input or output
  275. * (depending on endpoint)
  276. * \param length for bulk writes, the number of bytes from data to be sent. for
  277. * bulk reads, the maximum number of bytes to receive into the data buffer.
  278. * \param transferred output location for the number of bytes actually
  279. * transferred.
  280. * \param timeout timeout (in millseconds) that this function should wait
  281. * before giving up due to no response being received. For an unlimited
  282. * timeout, use value 0.
  283. *
  284. * \returns 0 on success (and populates <tt>transferred</tt>)
  285. * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
  286. * \returns LIBUSB_ERROR_PIPE if the endpoint halted
  287. * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
  288. * \ref packetoverflow
  289. * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
  290. * \returns another LIBUSB_ERROR code on other error
  291. */
  292. int API_EXPORTED libusb_interrupt_transfer(
  293. struct libusb_device_handle *dev_handle, unsigned char endpoint,
  294. unsigned char *data, int length, int *transferred, unsigned int timeout)
  295. {
  296. return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
  297. transferred, timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
  298. }