2 * Synchronous I/O functions for libusb
3 * Copyright (C) 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 libusb'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 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 no timeout, use
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 API_EXPORTED int 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(HANDLE_CTX(dev_handle));
107 libusb_cancel_transfer(transfer);
109 if (libusb_handle_events(HANDLE_CTX(dev_handle)) < 0)
111 libusb_free_transfer(transfer);
116 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
117 memcpy(data, libusb_control_transfer_get_data(transfer),
118 transfer->actual_length);
120 switch (transfer->status) {
121 case LIBUSB_TRANSFER_COMPLETED:
122 r = transfer->actual_length;
124 case LIBUSB_TRANSFER_TIMED_OUT:
125 r = LIBUSB_ERROR_TIMEOUT;
127 case LIBUSB_TRANSFER_STALL:
128 r = LIBUSB_ERROR_PIPE;
130 case LIBUSB_TRANSFER_NO_DEVICE:
131 r = LIBUSB_ERROR_NO_DEVICE;
134 usbi_warn(HANDLE_CTX(dev_handle),
135 "unrecognised status code %d", transfer->status);
136 r = LIBUSB_ERROR_OTHER;
139 libusb_free_transfer(transfer);
143 static void bulk_transfer_cb(struct libusb_transfer *transfer)
145 int *completed = transfer->user_data;
147 usbi_dbg("actual_length=%d", transfer->actual_length);
148 /* caller interprets results and frees transfer */
151 static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
152 unsigned char endpoint, unsigned char *buffer, int length,
153 int *transferred, unsigned int timeout, unsigned char type)
155 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
160 return LIBUSB_ERROR_NO_MEM;
162 libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
163 bulk_transfer_cb, &completed, timeout);
164 transfer->type = type;
166 r = libusb_submit_transfer(transfer);
168 libusb_free_transfer(transfer);
173 r = libusb_handle_events(HANDLE_CTX(dev_handle));
175 libusb_cancel_transfer(transfer);
177 if (libusb_handle_events(HANDLE_CTX(dev_handle)) < 0)
179 libusb_free_transfer(transfer);
184 *transferred = transfer->actual_length;
185 switch (transfer->status) {
186 case LIBUSB_TRANSFER_COMPLETED:
189 case LIBUSB_TRANSFER_TIMED_OUT:
190 r = LIBUSB_ERROR_TIMEOUT;
192 case LIBUSB_TRANSFER_STALL:
193 r = LIBUSB_ERROR_PIPE;
195 case LIBUSB_TRANSFER_OVERFLOW:
196 r = LIBUSB_ERROR_OVERFLOW;
198 case LIBUSB_TRANSFER_NO_DEVICE:
199 r = LIBUSB_ERROR_NO_DEVICE;
202 usbi_warn(HANDLE_CTX(dev_handle),
203 "unrecognised status code %d", transfer->status);
204 r = LIBUSB_ERROR_OTHER;
207 libusb_free_transfer(transfer);
212 * Perform a USB bulk transfer. The direction of the transfer is inferred from
213 * the direction bits of the endpoint address.
215 * For bulk reads, the <tt>length</tt> field indicates the maximum length of
216 * data you are expecting to receive. If less data arrives than expected,
217 * this function will return that data, so be sure to check the
218 * <tt>transferred</tt> output parameter.
220 * You should also check the <tt>transferred</tt> parameter for bulk writes.
221 * Not all of the data may have been written.
223 * Also check <tt>transferred</tt> when dealing with a timeout error code.
224 * libusb may have to split your transfer into a number of chunks to satisfy
225 * underlying O/S requirements, meaning that the timeout may expire after
226 * the first few chunks have completed. libusb is careful not to lose any data
227 * that may have been transferred; do not assume that timeout conditions
228 * indicate a complete lack of I/O.
230 * \param dev_handle a handle for the device to communicate with
231 * \param endpoint the address of a valid endpoint to communicate with
232 * \param data a suitably-sized data buffer for either input or output
233 * (depending on endpoint)
234 * \param length for bulk writes, the number of bytes from data to be sent. for
235 * bulk reads, the maximum number of bytes to receive into the data buffer.
236 * \param transferred output location for the number of bytes actually
238 * \param timeout timeout (in millseconds) that this function should wait
239 * before giving up due to no response being received. For no timeout, use
242 * \returns 0 on success (and populates <tt>transferred</tt>)
243 * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates
244 * <tt>transferred</tt>)
245 * \returns LIBUSB_ERROR_PIPE if the endpoint halted
246 * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
247 * \ref packetoverflow
248 * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
249 * \returns another LIBUSB_ERROR code on other failures
251 API_EXPORTED int libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
252 unsigned char endpoint, unsigned char *data, int length, int *transferred,
253 unsigned int timeout)
255 return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
256 transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
260 * Perform a USB interrupt transfer. The direction of the transfer is inferred
261 * from the direction bits of the endpoint address.
263 * For interrupt reads, the <tt>length</tt> field indicates the maximum length
264 * of data you are expecting to receive. If less data arrives than expected,
265 * this function will return that data, so be sure to check the
266 * <tt>transferred</tt> output parameter.
268 * You should also check the <tt>transferred</tt> parameter for interrupt
269 * writes. Not all of the data may have been written.
271 * Also check <tt>transferred</tt> when dealing with a timeout error code.
272 * libusb may have to split your transfer into a number of chunks to satisfy
273 * underlying O/S requirements, meaning that the timeout may expire after
274 * the first few chunks have completed. libusb is careful not to lose any data
275 * that may have been transferred; do not assume that timeout conditions
276 * indicate a complete lack of I/O.
278 * The default endpoint bInterval value is used as the polling interval.
280 * \param dev_handle a handle for the device to communicate with
281 * \param endpoint the address of a valid endpoint to communicate with
282 * \param data a suitably-sized data buffer for either input or output
283 * (depending on endpoint)
284 * \param length for bulk writes, the number of bytes from data to be sent. for
285 * bulk reads, the maximum number of bytes to receive into the data buffer.
286 * \param transferred output location for the number of bytes actually
288 * \param timeout timeout (in millseconds) that this function should wait
289 * before giving up due to no response being received. For no timeout, use
292 * \returns 0 on success (and populates <tt>transferred</tt>)
293 * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
294 * \returns LIBUSB_ERROR_PIPE if the endpoint halted
295 * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
296 * \ref packetoverflow
297 * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
298 * \returns another LIBUSB_ERROR code on other error
300 API_EXPORTED int libusb_interrupt_transfer(
301 struct libusb_device_handle *dev_handle, unsigned char endpoint,
302 unsigned char *data, int length, int *transferred, unsigned int timeout)
304 return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
305 transferred, timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);