1 | /*
|
---|
2 | * Synchronous I/O functions for libusb
|
---|
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 |
|
---|
20 | #include <config.h>
|
---|
21 |
|
---|
22 | #include <errno.h>
|
---|
23 | #include <stdint.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <string.h>
|
---|
26 |
|
---|
27 | #include "libusbi.h"
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @defgroup libusb_syncio Synchronous device I/O
|
---|
31 | *
|
---|
32 | * This page documents libusb's synchronous (blocking) API for USB device I/O.
|
---|
33 | * This interface is easy to use but has some limitations. More advanced users
|
---|
34 | * may wish to consider using the \ref libusb_asyncio "asynchronous I/O API" instead.
|
---|
35 | */
|
---|
36 |
|
---|
37 | static void LIBUSB_CALL sync_transfer_cb(struct libusb_transfer *transfer)
|
---|
38 | {
|
---|
39 | int *completed = transfer->user_data;
|
---|
40 | *completed = 1;
|
---|
41 | usbi_dbg("actual_length=%d", transfer->actual_length);
|
---|
42 | /* caller interprets result and frees transfer */
|
---|
43 | }
|
---|
44 |
|
---|
45 | static void sync_transfer_wait_for_completion(struct libusb_transfer *transfer)
|
---|
46 | {
|
---|
47 | int r, *completed = transfer->user_data;
|
---|
48 | struct libusb_context *ctx = HANDLE_CTX(transfer->dev_handle);
|
---|
49 |
|
---|
50 | while (!*completed) {
|
---|
51 | r = libusb_handle_events_completed(ctx, completed);
|
---|
52 | if (r < 0) {
|
---|
53 | if (r == LIBUSB_ERROR_INTERRUPTED)
|
---|
54 | continue;
|
---|
55 | usbi_err(ctx, "libusb_handle_events failed: %s, cancelling transfer and retrying",
|
---|
56 | libusb_error_name(r));
|
---|
57 | libusb_cancel_transfer(transfer);
|
---|
58 | continue;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** \ingroup libusb_syncio
|
---|
64 | * Perform a USB control transfer.
|
---|
65 | *
|
---|
66 | * The direction of the transfer is inferred from the bmRequestType field of
|
---|
67 | * the setup packet.
|
---|
68 | *
|
---|
69 | * The wValue, wIndex and wLength fields values should be given in host-endian
|
---|
70 | * byte order.
|
---|
71 | *
|
---|
72 | * \param dev_handle a handle for the device to communicate with
|
---|
73 | * \param bmRequestType the request type field for the setup packet
|
---|
74 | * \param bRequest the request field for the setup packet
|
---|
75 | * \param wValue the value field for the setup packet
|
---|
76 | * \param wIndex the index field for the setup packet
|
---|
77 | * \param data a suitably-sized data buffer for either input or output
|
---|
78 | * (depending on direction bits within bmRequestType)
|
---|
79 | * \param wLength the length field for the setup packet. The data buffer should
|
---|
80 | * be at least this size.
|
---|
81 | * \param timeout timeout (in millseconds) that this function should wait
|
---|
82 | * before giving up due to no response being received. For an unlimited
|
---|
83 | * timeout, use value 0.
|
---|
84 | * \returns on success, the number of bytes actually transferred
|
---|
85 | * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
|
---|
86 | * \returns LIBUSB_ERROR_PIPE if the control request was not supported by the
|
---|
87 | * device
|
---|
88 | * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
---|
89 | * \returns LIBUSB_ERROR_BUSY if called from event handling context
|
---|
90 | * \returns LIBUSB_ERROR_INVALID_PARAM if the transfer size is larger than
|
---|
91 | * the operating system and/or hardware can support
|
---|
92 | * \returns another LIBUSB_ERROR code on other failures
|
---|
93 | */
|
---|
94 | int API_EXPORTED libusb_control_transfer(libusb_device_handle *dev_handle,
|
---|
95 | uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
|
---|
96 | unsigned char *data, uint16_t wLength, unsigned int timeout)
|
---|
97 | {
|
---|
98 | struct libusb_transfer *transfer;
|
---|
99 | unsigned char *buffer;
|
---|
100 | int completed = 0;
|
---|
101 | int r;
|
---|
102 |
|
---|
103 | if (usbi_handling_events(HANDLE_CTX(dev_handle)))
|
---|
104 | return LIBUSB_ERROR_BUSY;
|
---|
105 |
|
---|
106 | transfer = libusb_alloc_transfer(0);
|
---|
107 | if (!transfer)
|
---|
108 | return LIBUSB_ERROR_NO_MEM;
|
---|
109 |
|
---|
110 | #ifndef __OS2__
|
---|
111 | buffer = (unsigned char*) malloc(LIBUSB_CONTROL_SETUP_SIZE + wLength);
|
---|
112 | #else
|
---|
113 | buffer = (unsigned char*)_tmalloc(LIBUSB_CONTROL_SETUP_SIZE + wLength);
|
---|
114 | #endif
|
---|
115 | if (!buffer) {
|
---|
116 | libusb_free_transfer(transfer);
|
---|
117 | return LIBUSB_ERROR_NO_MEM;
|
---|
118 | }
|
---|
119 |
|
---|
120 | libusb_fill_control_setup(buffer, bmRequestType, bRequest, wValue, wIndex,
|
---|
121 | wLength);
|
---|
122 | if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
|
---|
123 | memcpy(buffer + LIBUSB_CONTROL_SETUP_SIZE, data, wLength);
|
---|
124 |
|
---|
125 | libusb_fill_control_transfer(transfer, dev_handle, buffer,
|
---|
126 | sync_transfer_cb, &completed, timeout);
|
---|
127 | transfer->flags = LIBUSB_TRANSFER_FREE_BUFFER;
|
---|
128 | r = libusb_submit_transfer(transfer);
|
---|
129 | if (r < 0) {
|
---|
130 | libusb_free_transfer(transfer);
|
---|
131 | return r;
|
---|
132 | }
|
---|
133 |
|
---|
134 | sync_transfer_wait_for_completion(transfer);
|
---|
135 |
|
---|
136 | if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
|
---|
137 | memcpy(data, libusb_control_transfer_get_data(transfer),
|
---|
138 | transfer->actual_length);
|
---|
139 |
|
---|
140 | switch (transfer->status) {
|
---|
141 | case LIBUSB_TRANSFER_COMPLETED:
|
---|
142 | r = transfer->actual_length;
|
---|
143 | break;
|
---|
144 | case LIBUSB_TRANSFER_TIMED_OUT:
|
---|
145 | r = LIBUSB_ERROR_TIMEOUT;
|
---|
146 | break;
|
---|
147 | case LIBUSB_TRANSFER_STALL:
|
---|
148 | r = LIBUSB_ERROR_PIPE;
|
---|
149 | break;
|
---|
150 | case LIBUSB_TRANSFER_NO_DEVICE:
|
---|
151 | r = LIBUSB_ERROR_NO_DEVICE;
|
---|
152 | break;
|
---|
153 | case LIBUSB_TRANSFER_OVERFLOW:
|
---|
154 | r = LIBUSB_ERROR_OVERFLOW;
|
---|
155 | break;
|
---|
156 | case LIBUSB_TRANSFER_ERROR:
|
---|
157 | case LIBUSB_TRANSFER_CANCELLED:
|
---|
158 | r = LIBUSB_ERROR_IO;
|
---|
159 | break;
|
---|
160 | default:
|
---|
161 | usbi_warn(HANDLE_CTX(dev_handle),
|
---|
162 | "unrecognised status code %d", transfer->status);
|
---|
163 | r = LIBUSB_ERROR_OTHER;
|
---|
164 | }
|
---|
165 |
|
---|
166 | libusb_free_transfer(transfer);
|
---|
167 | return r;
|
---|
168 | }
|
---|
169 |
|
---|
170 | static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
|
---|
171 | unsigned char endpoint, unsigned char *buffer, int length,
|
---|
172 | int *transferred, unsigned int timeout, unsigned char type)
|
---|
173 | {
|
---|
174 | struct libusb_transfer *transfer;
|
---|
175 | int completed = 0;
|
---|
176 | int r;
|
---|
177 |
|
---|
178 | if (usbi_handling_events(HANDLE_CTX(dev_handle)))
|
---|
179 | return LIBUSB_ERROR_BUSY;
|
---|
180 |
|
---|
181 | transfer = libusb_alloc_transfer(0);
|
---|
182 | if (!transfer)
|
---|
183 | return LIBUSB_ERROR_NO_MEM;
|
---|
184 |
|
---|
185 | libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
|
---|
186 | sync_transfer_cb, &completed, timeout);
|
---|
187 | transfer->type = type;
|
---|
188 |
|
---|
189 | r = libusb_submit_transfer(transfer);
|
---|
190 | if (r < 0) {
|
---|
191 | libusb_free_transfer(transfer);
|
---|
192 | return r;
|
---|
193 | }
|
---|
194 |
|
---|
195 | sync_transfer_wait_for_completion(transfer);
|
---|
196 |
|
---|
197 | if (transferred)
|
---|
198 | *transferred = transfer->actual_length;
|
---|
199 |
|
---|
200 | switch (transfer->status) {
|
---|
201 | case LIBUSB_TRANSFER_COMPLETED:
|
---|
202 | r = 0;
|
---|
203 | break;
|
---|
204 | case LIBUSB_TRANSFER_TIMED_OUT:
|
---|
205 | r = LIBUSB_ERROR_TIMEOUT;
|
---|
206 | break;
|
---|
207 | case LIBUSB_TRANSFER_STALL:
|
---|
208 | r = LIBUSB_ERROR_PIPE;
|
---|
209 | break;
|
---|
210 | case LIBUSB_TRANSFER_OVERFLOW:
|
---|
211 | r = LIBUSB_ERROR_OVERFLOW;
|
---|
212 | break;
|
---|
213 | case LIBUSB_TRANSFER_NO_DEVICE:
|
---|
214 | r = LIBUSB_ERROR_NO_DEVICE;
|
---|
215 | break;
|
---|
216 | case LIBUSB_TRANSFER_ERROR:
|
---|
217 | case LIBUSB_TRANSFER_CANCELLED:
|
---|
218 | r = LIBUSB_ERROR_IO;
|
---|
219 | break;
|
---|
220 | default:
|
---|
221 | usbi_warn(HANDLE_CTX(dev_handle),
|
---|
222 | "unrecognised status code %d", transfer->status);
|
---|
223 | r = LIBUSB_ERROR_OTHER;
|
---|
224 | }
|
---|
225 |
|
---|
226 | libusb_free_transfer(transfer);
|
---|
227 | return r;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** \ingroup libusb_syncio
|
---|
231 | * Perform a USB bulk transfer. The direction of the transfer is inferred from
|
---|
232 | * the direction bits of the endpoint address.
|
---|
233 | *
|
---|
234 | * For bulk reads, the <tt>length</tt> field indicates the maximum length of
|
---|
235 | * 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.
|
---|
238 | *
|
---|
239 | * You should also check the <tt>transferred</tt> parameter for bulk writes.
|
---|
240 | * Not all of the data may have been written.
|
---|
241 | *
|
---|
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.
|
---|
248 | *
|
---|
249 | * \param dev_handle a handle for the device to communicate with
|
---|
250 | * \param endpoint the address of a valid endpoint to communicate with
|
---|
251 | * \param data a suitably-sized data buffer for either input or output
|
---|
252 | * (depending on endpoint)
|
---|
253 | * \param length for bulk writes, the number of bytes from data to be sent. for
|
---|
254 | * bulk reads, the maximum number of bytes to receive into the data buffer.
|
---|
255 | * \param transferred output location for the number of bytes actually
|
---|
256 | * transferred. Since version 1.0.21 (\ref LIBUSB_API_VERSION >= 0x01000105),
|
---|
257 | * it is legal to pass a NULL pointer if you do not wish to receive this
|
---|
258 | * information.
|
---|
259 | * \param timeout timeout (in millseconds) that this function should wait
|
---|
260 | * before giving up due to no response being received. For an unlimited
|
---|
261 | * timeout, use value 0.
|
---|
262 | *
|
---|
263 | * \returns 0 on success (and populates <tt>transferred</tt>)
|
---|
264 | * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates
|
---|
265 | * <tt>transferred</tt>)
|
---|
266 | * \returns LIBUSB_ERROR_PIPE if the endpoint halted
|
---|
267 | * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
|
---|
268 | * \ref libusb_packetoverflow
|
---|
269 | * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
---|
270 | * \returns LIBUSB_ERROR_BUSY if called from event handling context
|
---|
271 | * \returns another LIBUSB_ERROR code on other failures
|
---|
272 | */
|
---|
273 | int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
|
---|
274 | unsigned char endpoint, unsigned char *data, int length, int *transferred,
|
---|
275 | unsigned int timeout)
|
---|
276 | {
|
---|
277 | return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
|
---|
278 | transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** \ingroup libusb_syncio
|
---|
282 | * Perform a USB interrupt transfer. The direction of the transfer is inferred
|
---|
283 | * from the direction bits of the endpoint address.
|
---|
284 | *
|
---|
285 | * For interrupt reads, the <tt>length</tt> field indicates the maximum length
|
---|
286 | * of data you are expecting to receive. If less data arrives than expected,
|
---|
287 | * this function will return that data, so be sure to check the
|
---|
288 | * <tt>transferred</tt> output parameter.
|
---|
289 | *
|
---|
290 | * You should also check the <tt>transferred</tt> parameter for interrupt
|
---|
291 | * writes. Not all of the data may have been written.
|
---|
292 | *
|
---|
293 | * Also check <tt>transferred</tt> when dealing with a timeout error code.
|
---|
294 | * libusb may have to split your transfer into a number of chunks to satisfy
|
---|
295 | * underlying O/S requirements, meaning that the timeout may expire after
|
---|
296 | * the first few chunks have completed. libusb is careful not to lose any data
|
---|
297 | * that may have been transferred; do not assume that timeout conditions
|
---|
298 | * indicate a complete lack of I/O.
|
---|
299 | *
|
---|
300 | * The default endpoint bInterval value is used as the polling interval.
|
---|
301 | *
|
---|
302 | * \param dev_handle a handle for the device to communicate with
|
---|
303 | * \param endpoint the address of a valid endpoint to communicate with
|
---|
304 | * \param data a suitably-sized data buffer for either input or output
|
---|
305 | * (depending on endpoint)
|
---|
306 | * \param length for bulk writes, the number of bytes from data to be sent. for
|
---|
307 | * bulk reads, the maximum number of bytes to receive into the data buffer.
|
---|
308 | * \param transferred output location for the number of bytes actually
|
---|
309 | * transferred. Since version 1.0.21 (\ref LIBUSB_API_VERSION >= 0x01000105),
|
---|
310 | * it is legal to pass a NULL pointer if you do not wish to receive this
|
---|
311 | * information.
|
---|
312 | * \param timeout timeout (in millseconds) that this function should wait
|
---|
313 | * before giving up due to no response being received. For an unlimited
|
---|
314 | * timeout, use value 0.
|
---|
315 | *
|
---|
316 | * \returns 0 on success (and populates <tt>transferred</tt>)
|
---|
317 | * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
|
---|
318 | * \returns LIBUSB_ERROR_PIPE if the endpoint halted
|
---|
319 | * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
|
---|
320 | * \ref libusb_packetoverflow
|
---|
321 | * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
---|
322 | * \returns LIBUSB_ERROR_BUSY if called from event handling context
|
---|
323 | * \returns another LIBUSB_ERROR code on other error
|
---|
324 | */
|
---|
325 | int API_EXPORTED libusb_interrupt_transfer(
|
---|
326 | struct libusb_device_handle *dev_handle, unsigned char endpoint,
|
---|
327 | unsigned char *data, int length, int *transferred, unsigned int timeout)
|
---|
328 | {
|
---|
329 | return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
|
---|
330 | transferred, timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
|
---|
331 | }
|
---|