Core: Handle TRANSFER_ERROR/TRANSFER_CANCELLED for sync transfers
[platform/upstream/libusb.git] / libusb / sync.c
1 /*
2  * Synchronous I/O functions for libusbx
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 #include <errno.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "libusbi.h"
27
28 /**
29  * @defgroup syncio Synchronous device I/O
30  *
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.
34  */
35
36 static void LIBUSB_CALL ctrl_transfer_cb(struct libusb_transfer *transfer)
37 {
38         int *completed = transfer->user_data;
39         *completed = 1;
40         usbi_dbg("actual_length=%d", transfer->actual_length);
41         /* caller interprets result and frees transfer */
42 }
43
44 /** \ingroup syncio
45  * Perform a USB control transfer.
46  *
47  * The direction of the transfer is inferred from the bmRequestType field of
48  * the setup packet.
49  *
50  * The wValue, wIndex and wLength fields values should be given in host-endian
51  * byte order.
52  *
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
68  * device
69  * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
70  * \returns another LIBUSB_ERROR code on other failures
71  */
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)
75 {
76         struct libusb_transfer *transfer = libusb_alloc_transfer(0);
77         unsigned char *buffer;
78         int completed = 0;
79         int r;
80
81         if (!transfer)
82                 return LIBUSB_ERROR_NO_MEM;
83
84         buffer = malloc(LIBUSB_CONTROL_SETUP_SIZE + wLength);
85         if (!buffer) {
86                 libusb_free_transfer(transfer);
87                 return LIBUSB_ERROR_NO_MEM;
88         }
89
90         libusb_fill_control_setup(buffer, bmRequestType, bRequest, wValue, wIndex,
91                 wLength);
92         if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
93                 memcpy(buffer + LIBUSB_CONTROL_SETUP_SIZE, data, wLength);
94
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);
99         if (r < 0) {
100                 libusb_free_transfer(transfer);
101                 return r;
102         }
103
104         while (!completed) {
105                 r = libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed);
106                 if (r < 0) {
107                         if (r == LIBUSB_ERROR_INTERRUPTED)
108                                 continue;
109                         libusb_cancel_transfer(transfer);
110                         while (!completed)
111                                 if (libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed) < 0)
112                                         break;
113                         libusb_free_transfer(transfer);
114                         return r;
115                 }
116         }
117
118         if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
119                 memcpy(data, libusb_control_transfer_get_data(transfer),
120                         transfer->actual_length);
121
122         switch (transfer->status) {
123         case LIBUSB_TRANSFER_COMPLETED:
124                 r = transfer->actual_length;
125                 break;
126         case LIBUSB_TRANSFER_TIMED_OUT:
127                 r = LIBUSB_ERROR_TIMEOUT;
128                 break;
129         case LIBUSB_TRANSFER_STALL:
130                 r = LIBUSB_ERROR_PIPE;
131                 break;
132         case LIBUSB_TRANSFER_NO_DEVICE:
133                 r = LIBUSB_ERROR_NO_DEVICE;
134                 break;
135         case LIBUSB_TRANSFER_OVERFLOW:
136                 r = LIBUSB_ERROR_OVERFLOW;
137                 break;
138         case LIBUSB_TRANSFER_ERROR:
139         case LIBUSB_TRANSFER_CANCELLED:
140                 r = LIBUSB_ERROR_IO;
141                 break;
142         default:
143                 usbi_warn(HANDLE_CTX(dev_handle),
144                         "unrecognised status code %d", transfer->status);
145                 r = LIBUSB_ERROR_OTHER;
146         }
147
148         libusb_free_transfer(transfer);
149         return r;
150 }
151
152 static void LIBUSB_CALL bulk_transfer_cb(struct libusb_transfer *transfer)
153 {
154         int *completed = transfer->user_data;
155         *completed = 1;
156         usbi_dbg("actual_length=%d", transfer->actual_length);
157         /* caller interprets results and frees transfer */
158 }
159
160 static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
161         unsigned char endpoint, unsigned char *buffer, int length,
162         int *transferred, unsigned int timeout, unsigned char type)
163 {
164         struct libusb_transfer *transfer = libusb_alloc_transfer(0);
165         int completed = 0;
166         int r;
167
168         if (!transfer)
169                 return LIBUSB_ERROR_NO_MEM;
170
171         libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
172                 bulk_transfer_cb, &completed, timeout);
173         transfer->type = type;
174
175         r = libusb_submit_transfer(transfer);
176         if (r < 0) {
177                 libusb_free_transfer(transfer);
178                 return r;
179         }
180
181         while (!completed) {
182                 r = libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed);
183                 if (r < 0) {
184                         if (r == LIBUSB_ERROR_INTERRUPTED)
185                                 continue;
186                         libusb_cancel_transfer(transfer);
187                         while (!completed)
188                                 if (libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed) < 0)
189                                         break;
190                         libusb_free_transfer(transfer);
191                         return r;
192                 }
193         }
194
195         *transferred = transfer->actual_length;
196         switch (transfer->status) {
197         case LIBUSB_TRANSFER_COMPLETED:
198                 r = 0;
199                 break;
200         case LIBUSB_TRANSFER_TIMED_OUT:
201                 r = LIBUSB_ERROR_TIMEOUT;
202                 break;
203         case LIBUSB_TRANSFER_STALL:
204                 r = LIBUSB_ERROR_PIPE;
205                 break;
206         case LIBUSB_TRANSFER_OVERFLOW:
207                 r = LIBUSB_ERROR_OVERFLOW;
208                 break;
209         case LIBUSB_TRANSFER_NO_DEVICE:
210                 r = LIBUSB_ERROR_NO_DEVICE;
211                 break;
212         case LIBUSB_TRANSFER_ERROR:
213         case LIBUSB_TRANSFER_CANCELLED:
214                 r = LIBUSB_ERROR_IO;
215                 break;
216         default:
217                 usbi_warn(HANDLE_CTX(dev_handle),
218                         "unrecognised status code %d", transfer->status);
219                 r = LIBUSB_ERROR_OTHER;
220         }
221
222         libusb_free_transfer(transfer);
223         return r;
224 }
225
226 /** \ingroup syncio
227  * Perform a USB bulk transfer. The direction of the transfer is inferred from
228  * the direction bits of the endpoint address.
229  *
230  * For bulk reads, the <tt>length</tt> field indicates the maximum length of
231  * data you are expecting to receive. If less data arrives than expected,
232  * this function will return that data, so be sure to check the
233  * <tt>transferred</tt> output parameter.
234  *
235  * You should also check the <tt>transferred</tt> parameter for bulk writes.
236  * Not all of the data may have been written.
237  *
238  * Also check <tt>transferred</tt> when dealing with a timeout error code.
239  * libusbx may have to split your transfer into a number of chunks to satisfy
240  * underlying O/S requirements, meaning that the timeout may expire after
241  * the first few chunks have completed. libusbx is careful not to lose any data
242  * that may have been transferred; do not assume that timeout conditions
243  * indicate a complete lack of I/O.
244  *
245  * \param dev_handle a handle for the device to communicate with
246  * \param endpoint the address of a valid endpoint to communicate with
247  * \param data a suitably-sized data buffer for either input or output
248  * (depending on endpoint)
249  * \param length for bulk writes, the number of bytes from data to be sent. for
250  * bulk reads, the maximum number of bytes to receive into the data buffer.
251  * \param transferred output location for the number of bytes actually
252  * transferred.
253  * \param timeout timeout (in millseconds) that this function should wait
254  * before giving up due to no response being received. For an unlimited
255  * timeout, use value 0.
256  *
257  * \returns 0 on success (and populates <tt>transferred</tt>)
258  * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates
259  * <tt>transferred</tt>)
260  * \returns LIBUSB_ERROR_PIPE if the endpoint halted
261  * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
262  * \ref packetoverflow
263  * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
264  * \returns another LIBUSB_ERROR code on other failures
265  */
266 int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
267         unsigned char endpoint, unsigned char *data, int length, int *transferred,
268         unsigned int timeout)
269 {
270         return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
271                 transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
272 }
273
274 /** \ingroup syncio
275  * Perform a USB interrupt transfer. The direction of the transfer is inferred
276  * from the direction bits of the endpoint address.
277  *
278  * For interrupt reads, the <tt>length</tt> field indicates the maximum length
279  * of data you are expecting to receive. If less data arrives than expected,
280  * this function will return that data, so be sure to check the
281  * <tt>transferred</tt> output parameter.
282  *
283  * You should also check the <tt>transferred</tt> parameter for interrupt
284  * writes. Not all of the data may have been written.
285  *
286  * Also check <tt>transferred</tt> when dealing with a timeout error code.
287  * libusbx may have to split your transfer into a number of chunks to satisfy
288  * underlying O/S requirements, meaning that the timeout may expire after
289  * the first few chunks have completed. libusbx is careful not to lose any data
290  * that may have been transferred; do not assume that timeout conditions
291  * indicate a complete lack of I/O.
292  *
293  * The default endpoint bInterval value is used as the polling interval.
294  *
295  * \param dev_handle a handle for the device to communicate with
296  * \param endpoint the address of a valid endpoint to communicate with
297  * \param data a suitably-sized data buffer for either input or output
298  * (depending on endpoint)
299  * \param length for bulk writes, the number of bytes from data to be sent. for
300  * bulk reads, the maximum number of bytes to receive into the data buffer.
301  * \param transferred output location for the number of bytes actually
302  * transferred.
303  * \param timeout timeout (in millseconds) that this function should wait
304  * before giving up due to no response being received. For an unlimited
305  * timeout, use value 0.
306  *
307  * \returns 0 on success (and populates <tt>transferred</tt>)
308  * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
309  * \returns LIBUSB_ERROR_PIPE if the endpoint halted
310  * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
311  * \ref packetoverflow
312  * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
313  * \returns another LIBUSB_ERROR code on other error
314  */
315 int API_EXPORTED libusb_interrupt_transfer(
316         struct libusb_device_handle *dev_handle, unsigned char endpoint,
317         unsigned char *data, int length, int *transferred, unsigned int timeout)
318 {
319         return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
320                 transferred, timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
321 }