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