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 */
44 /* FIXME: does this support partial transfers? */
46 * Perform a USB control transfer. The direction of the transfer is inferred
47 * from the bmRequestType field of the setup packet.
49 * \param dev_handle a handle for the device to communicate with
50 * \param bmRequestType the request type field for the setup packet
51 * \param bRequest the request field for the setup packet
52 * \param wValue the value field for the setup packet
53 * \param wIndex the index field for the setup packet
54 * \param data a suitably-sized data buffer for either input or output
55 * (depending on direction bits within bmRequestType)
56 * \param wLength the length field for the setup packet. The data buffer should
57 * be at least this size.
58 * \param timeout timeout (in millseconds) that this function should wait
59 * before giving up due to no response being received. For no timeout, use
61 * \returns 0 on success
62 * \returns -ETIMEDOUT if the transfer timed out
63 * \returns other negative code on error
65 API_EXPORTED int libusb_control_transfer(libusb_device_handle *dev_handle,
66 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
67 unsigned char *data, uint16_t wLength, unsigned int timeout)
69 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
70 unsigned char *buffer;
77 buffer = malloc(LIBUSB_CONTROL_SETUP_SIZE + wLength);
79 libusb_free_transfer(transfer);
83 libusb_fill_control_setup(buffer, bmRequestType, bRequest, wValue, wIndex,
85 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
86 memcpy(buffer + LIBUSB_CONTROL_SETUP_SIZE, data, wLength);
88 libusb_fill_control_transfer(transfer, dev_handle, buffer,
89 ctrl_transfer_cb, &completed, timeout);
90 r = libusb_submit_transfer(transfer);
92 libusb_free_transfer(transfer);
97 r = libusb_handle_events();
99 libusb_cancel_transfer(transfer);
101 if (libusb_handle_events() < 0)
103 libusb_free_transfer(transfer);
108 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
109 memcpy(data, libusb_control_transfer_get_data(transfer),
110 transfer->actual_length);
112 switch (transfer->status) {
113 case LIBUSB_TRANSFER_COMPLETED:
114 r = transfer->actual_length;
116 case LIBUSB_TRANSFER_TIMED_OUT:
120 usbi_warn("unrecognised status code %d", transfer->status);
124 libusb_free_transfer(transfer);
128 static void bulk_transfer_cb(struct libusb_transfer *transfer)
130 int *completed = transfer->user_data;
132 usbi_dbg("actual_length=%d", transfer->actual_length);
133 /* caller interprets results and frees transfer */
136 static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
137 unsigned char endpoint, unsigned char *buffer, int length,
138 int *transferred, unsigned int timeout, unsigned char endpoint_type)
140 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
147 libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
148 bulk_transfer_cb, &completed, timeout);
149 transfer->endpoint_type = endpoint_type;
151 r = libusb_submit_transfer(transfer);
153 libusb_free_transfer(transfer);
158 r = libusb_handle_events();
160 libusb_cancel_transfer(transfer);
162 if (libusb_handle_events() < 0)
164 libusb_free_transfer(transfer);
169 *transferred = transfer->actual_length;
170 switch (transfer->status) {
171 case LIBUSB_TRANSFER_COMPLETED:
174 case LIBUSB_TRANSFER_TIMED_OUT:
178 usbi_warn("unrecognised status code %d", transfer->status);
182 libusb_free_transfer(transfer);
187 * Perform a USB bulk transfer. The direction of the transfer is inferred from
188 * the direction bits of the endpoint address.
190 * For bulk reads, the <tt>length</tt> field indicates the maximum length of
191 * data you are expecting to receive. If less data arrives than expected,
192 * this function will return that data, so be sure to check the
193 * <tt>transferred</tt> output parameter.
195 * You should also check the <tt>transferred</tt> parameter for bulk writes.
196 * Not all of the data may have been written.
198 * Also check <tt>transferred</tt> when dealing with a timeout error code.
199 * libusb may have to split your transfer into a number of chunks to satisfy
200 * underlying O/S requirements, meaning that the timeout may expire after
201 * the first few chunks have completed. libusb is careful not to lose any data
202 * that may have been transferred; do not assume that timeout conditions
203 * indicate a complete lack of I/O.
205 * \param dev_handle a handle for the device to communicate with
206 * \param endpoint the address of a valid endpoint to communicate with
207 * \param data a suitably-sized data buffer for either input or output
208 * (depending on endpoint)
209 * \param length for bulk writes, the number of bytes from data to be sent. for
210 * bulk reads, the maximum number of bytes to receive into the data buffer.
211 * \param transferred output location for the number of bytes actually
213 * \param timeout timeout (in millseconds) that this function should wait
214 * before giving up due to no response being received. For no timeout, use
217 * \returns 0 on success (and populates <tt>transferred</tt>)
218 * \returns -ETIMEDOUT if the transfer timed out (and populates
219 * <tt>transferred</tt>)
220 * \returns other negative code on error
222 API_EXPORTED int libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
223 unsigned char endpoint, unsigned char *data, int length, int *transferred,
224 unsigned int timeout)
226 return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
227 transferred, timeout, LIBUSB_ENDPOINT_TYPE_BULK);
231 * Perform a USB interrupt transfer. The direction of the transfer is inferred
232 * from the direction bits of the endpoint address.
234 * For interrupt reads, the <tt>length</tt> field indicates the maximum length
235 * of data you are expecting to receive. If less data arrives than expected,
236 * this function will return that data, so be sure to check the
237 * <tt>transferred</tt> output parameter.
239 * You should also check the <tt>transferred</tt> parameter for interrupt
240 * writes. Not all of the data may have been written.
242 * Also check <tt>transferred</tt> when dealing with a timeout error code.
243 * libusb may have to split your transfer into a number of chunks to satisfy
244 * underlying O/S requirements, meaning that the timeout may expire after
245 * the first few chunks have completed. libusb is careful not to lose any data
246 * that may have been transferred; do not assume that timeout conditions
247 * indicate a complete lack of I/O.
249 * The default endpoint bInterval value is used as the polling interval.
251 * \param dev_handle a handle for the device to communicate with
252 * \param endpoint the address of a valid endpoint to communicate with
253 * \param data a suitably-sized data buffer for either input or output
254 * (depending on endpoint)
255 * \param length for bulk writes, the number of bytes from data to be sent. for
256 * bulk reads, the maximum number of bytes to receive into the data buffer.
257 * \param transferred output location for the number of bytes actually
259 * \param timeout timeout (in millseconds) that this function should wait
260 * before giving up due to no response being received. For no timeout, use
263 * \returns 0 on success (and populates <tt>transferred</tt>)
264 * \returns -ETIMEDOUT if the transfer timed out
265 * \returns other negative code on error
267 API_EXPORTED int libusb_interrupt_transfer(
268 struct libusb_device_handle *dev_handle, unsigned char endpoint,
269 unsigned char *data, int length, int *transferred, unsigned int timeout)
271 return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
272 transferred, timeout, LIBUSB_ENDPOINT_TYPE_INTERRUPT);