2 * Synchronous I/O functions for libusbx
3 * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
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.
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.
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
29 * @defgroup syncio Synchronous device I/O
31 * This page documents libusbx's synchronous (blocking) API for USB device I/O.
32 * This interface is easy to use but has some limitations. More advanced users
33 * may wish to consider using the \ref asyncio "asynchronous I/O API" instead.
36 static void LIBUSB_CALL ctrl_transfer_cb(struct libusb_transfer *transfer)
38 int *completed = transfer->user_data;
40 usbi_dbg("actual_length=%d", transfer->actual_length);
41 /* caller interprets result and frees transfer */
45 * Perform a USB control transfer.
47 * The direction of the transfer is inferred from the bmRequestType field of
50 * The wValue, wIndex and wLength fields values should be given in host-endian
53 * \param dev_handle a handle for the device to communicate with
54 * \param bmRequestType the request type field for the setup packet
55 * \param bRequest the request field for the setup packet
56 * \param wValue the value field for the setup packet
57 * \param wIndex the index field for the setup packet
58 * \param data a suitably-sized data buffer for either input or output
59 * (depending on direction bits within bmRequestType)
60 * \param wLength the length field for the setup packet. The data buffer should
61 * be at least this size.
62 * \param timeout timeout (in millseconds) that this function should wait
63 * before giving up due to no response being received. For an unlimited
64 * timeout, use value 0.
65 * \returns on success, the number of bytes actually transferred
66 * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
67 * \returns LIBUSB_ERROR_PIPE if the control request was not supported by the
69 * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
70 * \returns another LIBUSB_ERROR code on other failures
72 int API_EXPORTED libusb_control_transfer(libusb_device_handle *dev_handle,
73 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
74 unsigned char *data, uint16_t wLength, unsigned int timeout)
76 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
77 unsigned char *buffer;
82 return LIBUSB_ERROR_NO_MEM;
84 buffer = malloc(LIBUSB_CONTROL_SETUP_SIZE + wLength);
86 libusb_free_transfer(transfer);
87 return LIBUSB_ERROR_NO_MEM;
90 libusb_fill_control_setup(buffer, bmRequestType, bRequest, wValue, wIndex,
92 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
93 memcpy(buffer + LIBUSB_CONTROL_SETUP_SIZE, data, wLength);
95 libusb_fill_control_transfer(transfer, dev_handle, buffer,
96 ctrl_transfer_cb, &completed, timeout);
97 transfer->flags = LIBUSB_TRANSFER_FREE_BUFFER;
98 r = libusb_submit_transfer(transfer);
100 libusb_free_transfer(transfer);
105 r = libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed);
107 if (r == LIBUSB_ERROR_INTERRUPTED)
109 libusb_cancel_transfer(transfer);
111 if (libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed) < 0)
113 libusb_free_transfer(transfer);
118 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
119 memcpy(data, libusb_control_transfer_get_data(transfer),
120 transfer->actual_length);
122 switch (transfer->status) {
123 case LIBUSB_TRANSFER_COMPLETED:
124 r = transfer->actual_length;
126 case LIBUSB_TRANSFER_TIMED_OUT:
127 r = LIBUSB_ERROR_TIMEOUT;
129 case LIBUSB_TRANSFER_STALL:
130 r = LIBUSB_ERROR_PIPE;
132 case LIBUSB_TRANSFER_NO_DEVICE:
133 r = LIBUSB_ERROR_NO_DEVICE;
135 case LIBUSB_TRANSFER_OVERFLOW:
136 r = LIBUSB_ERROR_OVERFLOW;
139 usbi_warn(HANDLE_CTX(dev_handle),
140 "unrecognised status code %d", transfer->status);
141 r = LIBUSB_ERROR_OTHER;
144 libusb_free_transfer(transfer);
148 static void LIBUSB_CALL bulk_transfer_cb(struct libusb_transfer *transfer)
150 int *completed = transfer->user_data;
152 usbi_dbg("actual_length=%d", transfer->actual_length);
153 /* caller interprets results and frees transfer */
156 static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
157 unsigned char endpoint, unsigned char *buffer, int length,
158 int *transferred, unsigned int timeout, unsigned char type)
160 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
165 return LIBUSB_ERROR_NO_MEM;
167 libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
168 bulk_transfer_cb, &completed, timeout);
169 transfer->type = type;
171 r = libusb_submit_transfer(transfer);
173 libusb_free_transfer(transfer);
178 r = libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed);
180 if (r == LIBUSB_ERROR_INTERRUPTED)
182 libusb_cancel_transfer(transfer);
184 if (libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed) < 0)
186 libusb_free_transfer(transfer);
191 *transferred = transfer->actual_length;
192 switch (transfer->status) {
193 case LIBUSB_TRANSFER_COMPLETED:
196 case LIBUSB_TRANSFER_TIMED_OUT:
197 r = LIBUSB_ERROR_TIMEOUT;
199 case LIBUSB_TRANSFER_STALL:
200 r = LIBUSB_ERROR_PIPE;
202 case LIBUSB_TRANSFER_OVERFLOW:
203 r = LIBUSB_ERROR_OVERFLOW;
205 case LIBUSB_TRANSFER_NO_DEVICE:
206 r = LIBUSB_ERROR_NO_DEVICE;
209 usbi_warn(HANDLE_CTX(dev_handle),
210 "unrecognised status code %d", transfer->status);
211 r = LIBUSB_ERROR_OTHER;
214 libusb_free_transfer(transfer);
219 * Perform a USB bulk transfer. The direction of the transfer is inferred from
220 * the direction bits of the endpoint address.
222 * For bulk reads, the <tt>length</tt> field indicates the maximum length of
223 * data you are expecting to receive. If less data arrives than expected,
224 * this function will return that data, so be sure to check the
225 * <tt>transferred</tt> output parameter.
227 * You should also check the <tt>transferred</tt> parameter for bulk writes.
228 * Not all of the data may have been written.
230 * Also check <tt>transferred</tt> when dealing with a timeout error code.
231 * libusbx may have to split your transfer into a number of chunks to satisfy
232 * underlying O/S requirements, meaning that the timeout may expire after
233 * the first few chunks have completed. libusbx is careful not to lose any data
234 * that may have been transferred; do not assume that timeout conditions
235 * indicate a complete lack of I/O.
237 * \param dev_handle a handle for the device to communicate with
238 * \param endpoint the address of a valid endpoint to communicate with
239 * \param data a suitably-sized data buffer for either input or output
240 * (depending on endpoint)
241 * \param length for bulk writes, the number of bytes from data to be sent. for
242 * bulk reads, the maximum number of bytes to receive into the data buffer.
243 * \param transferred output location for the number of bytes actually
245 * \param timeout timeout (in millseconds) that this function should wait
246 * before giving up due to no response being received. For an unlimited
247 * timeout, use value 0.
249 * \returns 0 on success (and populates <tt>transferred</tt>)
250 * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates
251 * <tt>transferred</tt>)
252 * \returns LIBUSB_ERROR_PIPE if the endpoint halted
253 * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
254 * \ref packetoverflow
255 * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
256 * \returns another LIBUSB_ERROR code on other failures
258 int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
259 unsigned char endpoint, unsigned char *data, int length, int *transferred,
260 unsigned int timeout)
262 return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
263 transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
267 * Perform a USB interrupt transfer. The direction of the transfer is inferred
268 * from the direction bits of the endpoint address.
270 * For interrupt reads, the <tt>length</tt> field indicates the maximum length
271 * of data you are expecting to receive. If less data arrives than expected,
272 * this function will return that data, so be sure to check the
273 * <tt>transferred</tt> output parameter.
275 * You should also check the <tt>transferred</tt> parameter for interrupt
276 * writes. Not all of the data may have been written.
278 * Also check <tt>transferred</tt> when dealing with a timeout error code.
279 * libusbx may have to split your transfer into a number of chunks to satisfy
280 * underlying O/S requirements, meaning that the timeout may expire after
281 * the first few chunks have completed. libusbx is careful not to lose any data
282 * that may have been transferred; do not assume that timeout conditions
283 * indicate a complete lack of I/O.
285 * The default endpoint bInterval value is used as the polling interval.
287 * \param dev_handle a handle for the device to communicate with
288 * \param endpoint the address of a valid endpoint to communicate with
289 * \param data a suitably-sized data buffer for either input or output
290 * (depending on endpoint)
291 * \param length for bulk writes, the number of bytes from data to be sent. for
292 * bulk reads, the maximum number of bytes to receive into the data buffer.
293 * \param transferred output location for the number of bytes actually
295 * \param timeout timeout (in millseconds) that this function should wait
296 * before giving up due to no response being received. For an unlimited
297 * timeout, use value 0.
299 * \returns 0 on success (and populates <tt>transferred</tt>)
300 * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
301 * \returns LIBUSB_ERROR_PIPE if the endpoint halted
302 * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
303 * \ref packetoverflow
304 * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
305 * \returns another LIBUSB_ERROR code on other error
307 int API_EXPORTED libusb_interrupt_transfer(
308 struct libusb_device_handle *dev_handle, unsigned char endpoint,
309 unsigned char *data, int length, int *transferred, unsigned int timeout)
311 return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
312 transferred, timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);