Remove timers and signalfd
[platform/upstream/libusb.git] / libusb / io.c
1 /*
2  * I/O functions for libusb
3  * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
4  * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <config.h>
22
23 #include <errno.h>
24 #include <signal.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <sys/select.h>
29 #include <sys/time.h>
30 #include <time.h>
31 #include <unistd.h>
32
33 #include "libusbi.h"
34
35 /* this is a list of in-flight rb_handles, sorted by timeout expiration.
36  * URBs to timeout the soonest are placed at the beginning of the list, URBs
37  * that will time out later are placed after, and urbs with infinite timeout
38  * are always placed at the very end. */
39 static struct list_head flying_urbs;
40
41 void usbi_io_init()
42 {
43         list_init(&flying_urbs);
44 }
45
46 static int calculate_timeout(struct libusb_urb_handle *urbh,
47         unsigned int timeout)
48 {
49         int r;
50         struct timespec current_time;
51
52         if (!timeout)
53                 return 0;
54
55         r = clock_gettime(CLOCK_MONOTONIC, &current_time);
56         if (r < 0) {
57                 usbi_err("failed to read monotonic clock, errno=%d", errno);
58                 return r;
59         }
60
61         current_time.tv_sec += timeout / 1000;
62         current_time.tv_nsec += (timeout % 1000) * 1000000;
63
64         if (current_time.tv_nsec > 1000000000) {
65                 current_time.tv_nsec -= 1000000000;
66                 current_time.tv_sec++;
67         }
68
69         TIMESPEC_TO_TIMEVAL(&urbh->timeout, &current_time);
70         return 0;
71 }
72
73 static void add_to_flying_list(struct libusb_urb_handle *urbh)
74 {
75         struct libusb_urb_handle *cur;
76         struct timeval *timeout = &urbh->timeout;
77
78         /* if we have no other flying urbs, start the list with this one */
79         if (list_empty(&flying_urbs)) {
80                 list_add(&urbh->list, &flying_urbs);
81                 return;
82         }
83
84         /* if we have infinite timeout, append to end of list */
85         if (!timerisset(timeout)) {
86                 list_add_tail(&urbh->list, &flying_urbs);
87                 return;
88         }
89
90         /* otherwise, find appropriate place in list */
91         list_for_each_entry(cur, &flying_urbs, list) {
92                 /* find first timeout that occurs after the urbh in question */
93                 struct timeval *cur_tv = &cur->timeout;
94
95                 if (!timerisset(cur_tv) || (cur_tv->tv_sec > timeout->tv_sec) ||
96                                 (cur_tv->tv_sec == timeout->tv_sec &&
97                                         cur_tv->tv_usec > timeout->tv_usec)) {
98                         list_add_tail(&urbh->list, &cur->list);
99                         return;
100                 }
101         }
102
103         /* otherwise we need to be inserted at the end */
104         list_add_tail(&urbh->list, &flying_urbs);
105 }
106
107 static int submit_urb(struct libusb_dev_handle *devh,
108         struct libusb_urb_handle *urbh)
109 {
110         int r;
111         struct usb_urb *urb = &urbh->urb;
112         int to_be_transferred = urbh->transfer_len - urbh->transferred;
113
114         urb->type = urbh->urb_type;
115         urb->endpoint = urbh->endpoint;
116         urb->buffer = urbh->buffer + urbh->transferred;
117         urb->buffer_length = MIN(to_be_transferred, MAX_URB_BUFFER_LENGTH);
118
119         /* FIXME: for requests that we have to split into multiple URBs, we should
120          * submit all the URBs instantly: submit, submit, submit, reap, reap, reap
121          * rather than: submit, reap, submit, reap, submit, reap
122          * this will improve performance and fix bugs concerning behaviour when
123          * the user submits two similar multiple-urb requests */
124         usbi_dbg("transferring %d from %d bytes", urb->buffer_length,
125                 to_be_transferred);
126
127         r = ioctl(devh->fd, IOCTL_USB_SUBMITURB, &urbh->urb);
128         if (r < 0) {
129                 usbi_err("submiturb failed error %d errno=%d", r, errno);
130                 return r;
131         }
132
133         add_to_flying_list(urbh);
134         return 0;
135 }
136
137 API_EXPORTED struct libusb_urb_handle *libusb_async_control_transfer(
138         struct libusb_dev_handle *devh, struct libusb_control_transfer *transfer,
139         libusb_ctrl_cb_fn callback, void *user_data, unsigned int timeout)
140 {
141         struct libusb_urb_handle *urbh = malloc(sizeof(*urbh));
142         struct libusb_ctrl_setup *setup;
143         unsigned char *urbdata;
144         int urbdata_length = sizeof(struct libusb_ctrl_setup) + transfer->length;
145         int r;
146
147         if (!urbh)
148                 return NULL;
149         memset(urbh, 0, sizeof(*urbh));
150         urbh->devh = devh;
151         urbh->callback = callback;
152         urbh->user_data = user_data;
153         r = calculate_timeout(urbh, timeout);
154         if (r < 0) {
155                 free(urbh);
156                 return NULL;
157         }
158
159         urbdata = malloc(urbdata_length);
160         if (!urbdata) {
161                 free(urbh);
162                 return NULL;
163         }
164
165         usbi_dbg("RQT=%02x RQ=%02x VAL=%04x IDX=%04x length=%d",
166                 transfer->requesttype, transfer->request, transfer->value,
167                 transfer->index, transfer->length);
168
169         setup = (struct libusb_ctrl_setup *) urbdata;
170         setup->bRequestType = transfer->requesttype;
171         setup->bRequest = transfer->request;
172         setup->wValue = cpu_to_le16(transfer->value);
173         setup->wIndex = cpu_to_le16(transfer->index);
174         setup->wLength = cpu_to_le16(transfer->length);
175
176         if ((transfer->requesttype & 0x80) == LIBUSB_ENDPOINT_OUT)
177                 memcpy(urbdata + sizeof(struct libusb_ctrl_setup), transfer->data,
178                 transfer->length);
179
180         urbh->urb_type = USB_URB_TYPE_CONTROL;
181         urbh->buffer = urbdata;
182         urbh->transfer_len = urbdata_length;
183
184         r = submit_urb(devh, urbh);
185         if (r < 0) {
186                 free(urbh);
187                 free(urbdata);
188                 return NULL;
189         }
190
191         return urbh;
192 }
193
194 static struct libusb_urb_handle *submit_bulk_transfer(
195         struct libusb_dev_handle *devh, struct libusb_bulk_transfer *transfer,
196         libusb_bulk_cb_fn callback, void *user_data, unsigned int timeout,
197         unsigned char urbtype)
198 {
199         struct libusb_urb_handle *urbh = malloc(sizeof(*urbh));
200         int r;
201
202         usbi_dbg("length %d timeout %d", transfer->length, timeout);
203
204         if (!urbh)
205                 return NULL;
206         memset(urbh, 0, sizeof(*urbh));
207         r = calculate_timeout(urbh, timeout);
208         if (r < 0) {
209                 free(urbh);
210                 return NULL;
211         }
212         urbh->devh = devh;
213         urbh->callback = callback;
214         urbh->user_data = user_data;
215         urbh->flags |= LIBUSB_URBH_DATA_BELONGS_TO_USER;
216         urbh->endpoint = transfer->endpoint;
217         urbh->urb_type = urbtype;
218         urbh->buffer = transfer->data;
219         urbh->transfer_len = transfer->length;
220
221         r = submit_urb(devh, urbh);
222         if (r < 0) {
223                 free(urbh);
224                 return NULL;
225         }
226
227         return urbh;
228 }
229
230 API_EXPORTED struct libusb_urb_handle *libusb_async_bulk_transfer(
231         struct libusb_dev_handle *devh, struct libusb_bulk_transfer *transfer,
232         libusb_bulk_cb_fn callback, void *user_data, unsigned int timeout)
233 {
234         return submit_bulk_transfer(devh, transfer, callback, user_data, timeout,
235                 USB_URB_TYPE_BULK);
236 }
237
238 API_EXPORTED struct libusb_urb_handle *libusb_async_interrupt_transfer(
239         struct libusb_dev_handle *devh, struct libusb_bulk_transfer *transfer,
240         libusb_bulk_cb_fn callback, void *user_data, unsigned int timeout)
241 {
242         return submit_bulk_transfer(devh, transfer, callback, user_data, timeout,
243                 USB_URB_TYPE_INTERRUPT);
244 }
245
246 API_EXPORTED int libusb_urb_handle_cancel(struct libusb_dev_handle *devh,
247         struct libusb_urb_handle *urbh)
248 {
249         int r;
250         usbi_dbg("");
251         r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb);
252         if (r < 0)
253                 usbi_err("cancel urb failed error %d", r);
254         return r;
255 }
256
257 API_EXPORTED int libusb_urb_handle_cancel_sync(struct libusb_dev_handle *devh,
258         struct libusb_urb_handle *urbh)
259 {
260         int r;
261         usbi_dbg("");
262         r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb);
263         if (r < 0) {
264                 usbi_err("cancel urb failed error %d", r);
265                 return r;
266         }
267
268         urbh->flags |= LIBUSB_URBH_SYNC_CANCELLED;
269         while (urbh->flags & LIBUSB_URBH_SYNC_CANCELLED) {
270                 r = libusb_poll();
271                 if (r < 0)
272                         return r;
273         }
274
275         return 0;
276 }
277
278 int handle_transfer_completion(struct libusb_dev_handle *devh,
279         struct libusb_urb_handle *urbh, enum libusb_urb_cb_status status)
280 {
281         struct usb_urb *urb = &urbh->urb;
282
283         if (status == FP_URB_SILENT_COMPLETION)
284                 return 0;
285
286         if (urb->type == USB_URB_TYPE_CONTROL) {
287                 libusb_ctrl_cb_fn callback = urbh->callback;
288                 if (callback)
289                         callback(devh, urbh, status, urb->buffer,
290                                 urb->buffer + sizeof(struct libusb_ctrl_setup), urbh->transferred,
291                                 urbh->user_data);
292         } else if (urb->type == USB_URB_TYPE_BULK ||
293                         urb->type == USB_URB_TYPE_INTERRUPT) {
294                 libusb_bulk_cb_fn callback = urbh->callback;
295                 if (callback)
296                         callback(devh, urbh, status, urbh->endpoint, urbh->transfer_len,
297                                 urbh->buffer, urbh->transferred, urbh->user_data);
298         }
299         return 0;
300 }
301
302 static int handle_transfer_cancellation(struct libusb_dev_handle *devh,
303         struct libusb_urb_handle *urbh)
304 {
305         /* if the URB is being cancelled synchronously, raise cancellation
306          * completion event by unsetting flag, and ensure that user callback does
307          * not get called.
308          */
309         if (urbh->flags & LIBUSB_URBH_SYNC_CANCELLED) {
310                 urbh->flags &= ~LIBUSB_URBH_SYNC_CANCELLED;
311                 usbi_dbg("detected sync. cancel");
312                 return handle_transfer_completion(devh, urbh, FP_URB_SILENT_COMPLETION);
313         }
314
315         /* if the URB was cancelled due to timeout, report timeout to the user */
316         if (urbh->flags & LIBUSB_URBH_TIMED_OUT) {
317                 usbi_dbg("detected timeout cancellation");
318                 return handle_transfer_completion(devh, urbh, FP_URB_TIMEOUT);
319         }
320
321         /* otherwise its a normal async cancel */
322         return handle_transfer_completion(devh, urbh, FP_URB_CANCELLED);
323 }
324
325 static int reap_for_devh(struct libusb_dev_handle *devh)
326 {
327         int r;
328         struct usb_urb *urb;
329         struct libusb_urb_handle *urbh;
330         int trf_requested;
331
332         r = ioctl(devh->fd, IOCTL_USB_REAPURBNDELAY, &urb);
333         if (r == -1 && errno == EAGAIN)
334                 return r;
335         if (r < 0) {
336                 usbi_err("reap failed error %d errno=%d", r, errno);
337                 return r;
338         }
339
340         urbh = container_of(urb, struct libusb_urb_handle, urb);
341
342         usbi_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status,
343                 urb->actual_length);
344         list_del(&urbh->list);
345
346         if (urb->status == -2)
347                 return handle_transfer_cancellation(devh, urbh);
348         /* FIXME: research what other status codes may exist */
349         if (urb->status != 0)
350                 usbi_warn("unrecognised urb status %d", urb->status);
351
352         /* determine how much data was asked for */
353         trf_requested = MIN(urbh->transfer_len - urbh->transferred,
354                 MAX_URB_BUFFER_LENGTH);
355
356         urbh->transferred += urb->actual_length;        
357
358         /* if we were provided less data than requested, then our transfer is
359          * done */
360         if (urb->actual_length < trf_requested) {
361                 usbi_dbg("less data than requested (%d/%d) --> all done",
362                         urb->actual_length, trf_requested);
363                 return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED);
364         }
365
366         /* if we've transferred all data, we're done */
367         if (urbh->transferred == urbh->transfer_len) {
368                 usbi_dbg("transfer complete --> all done");
369                 return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED);
370         }
371
372         /* otherwise, we have more data to transfer */
373         usbi_dbg("more data to transfer...");
374         memset(urb, 0, sizeof(*urb));
375         return submit_urb(devh, urbh);
376 }
377
378 static void handle_timeout(struct libusb_urb_handle *urbh)
379 {
380         /* handling timeouts is tricky, as we may race with the kernel: we may
381          * detect a timeout racing with the condition that the urb has actually
382          * completed. we asynchronously cancel the URB and report timeout
383          * to the user when the URB cancellation completes (or not at all if the
384          * URB actually gets delivered as per this race) */
385         int r;
386
387
388         urbh->flags |= LIBUSB_URBH_TIMED_OUT;
389         r = libusb_urb_handle_cancel(urbh->devh, urbh);
390         if (r < 0)
391                 usbi_warn("async cancel failed %d errno=%d", r, errno);
392 }
393
394 static int handle_timeouts(void)
395 {
396         struct timespec systime_ts;
397         struct timeval systime;
398         struct libusb_urb_handle *urbh;
399         int r;
400
401         if (list_empty(&flying_urbs))
402                 return 0;
403
404         /* get current time */
405         r = clock_gettime(CLOCK_MONOTONIC, &systime_ts);
406         if (r < 0)
407                 return r;
408
409         TIMESPEC_TO_TIMEVAL(&systime, &systime_ts);
410
411         /* iterate through flying urbs list, finding all urbs that have expired
412          * timeouts */
413         list_for_each_entry(urbh, &flying_urbs, list) {
414                 struct timeval *cur_tv = &urbh->timeout;
415
416                 /* if we've reached urbs of infinite timeout, we're all done */
417                 if (!timerisset(cur_tv))
418                         return 0;
419
420                 /* ignore timeouts we've already handled */
421                 if (urbh->flags & LIBUSB_URBH_TIMED_OUT)
422                         continue;
423
424                 /* if urb has non-expired timeout, nothing more to do */
425                 if ((cur_tv->tv_sec > systime.tv_sec) ||
426                                 (cur_tv->tv_sec == systime.tv_sec &&
427                                         cur_tv->tv_usec > systime.tv_usec))
428                         return 0;
429         
430                 /* otherwise, we've got an expired timeout to handle */
431                 handle_timeout(urbh);
432         }
433
434         return 0;
435 }
436
437 static int poll_io(struct timeval *tv)
438 {
439         struct libusb_dev_handle *devh;
440         int r;
441         int maxfd = 0;
442         fd_set writefds;
443         struct timeval select_timeout;
444         struct timeval timeout;
445
446         r = libusb_get_next_timeout(&timeout);
447         if (r) {
448                 /* timeout already expired? */
449                 if (!timerisset(&timeout))
450                         return handle_timeouts();
451
452                 /* choose the smallest of next URB timeout or user specified timeout */
453                 if (timercmp(&timeout, tv, <))
454                         select_timeout = timeout;
455                 else
456                         select_timeout = *tv;
457         } else {
458                 select_timeout = *tv;
459         }
460
461         FD_ZERO(&writefds);
462         list_for_each_entry(devh, &open_devs, list) {
463                 int fd = devh->fd;
464                 FD_SET(fd, &writefds);
465                 if (fd > maxfd)
466                         maxfd = fd;
467         }
468
469         usbi_dbg("select() with timeout in %d.%06ds", select_timeout.tv_sec,
470                 select_timeout.tv_usec);
471         r = select(maxfd + 1, NULL, &writefds, NULL, &select_timeout);
472         usbi_dbg("select() returned %d with %d.%06ds remaining", r, select_timeout.tv_sec,
473                 select_timeout.tv_usec);
474         if (r == 0) {
475                 *tv = select_timeout;
476                 return handle_timeouts();
477         } else if (r == -1 && errno == EINTR) {
478                 return 0;
479         } else if (r < 0) {
480                 usbi_err("select failed %d err=%d\n", r, errno);
481                 return r;
482         }
483
484         list_for_each_entry(devh, &open_devs, list) {
485                 if (!FD_ISSET(devh->fd, &writefds))
486                         continue;
487                 r = reap_for_devh(devh);
488                 if (r == -1 && errno == EAGAIN)
489                         continue;
490                 if (r < 0)
491                         return r;
492         }
493
494         /* FIXME check return value? */
495         return handle_timeouts();
496 }
497
498 API_EXPORTED int libusb_poll_timeout(struct timeval *tv)
499 {
500         return poll_io(tv);
501 }
502
503 API_EXPORTED int libusb_poll(void)
504 {
505         struct timeval tv;
506         tv.tv_sec = 2;
507         tv.tv_usec = 0;
508         return poll_io(&tv);
509 }
510
511 API_EXPORTED int libusb_get_next_timeout(struct timeval *tv)
512 {
513         struct libusb_urb_handle *urbh;
514         struct timespec cur_ts;
515         struct timeval cur_tv;
516         struct timeval *next_timeout;
517         int r;
518         int found = 0;
519
520         if (list_empty(&flying_urbs)) {
521                 usbi_dbg("no URBs, no timeout!");
522                 return 0;
523         }
524
525         /* find next urb which hasn't already been processed as timed out */
526         list_for_each_entry(urbh, &flying_urbs, list) {
527                 if (!(urbh->flags & LIBUSB_URBH_TIMED_OUT)) {
528                         found = 1;
529                         break;
530                 }
531         }
532
533         if (!found) {
534                 usbi_dbg("all URBs have already been processed for timeouts");
535                 return 0;
536         }
537
538         next_timeout = &urbh->timeout;
539
540         /* no timeout for next urb */
541         if (!timerisset(next_timeout)) {
542                 usbi_dbg("no URBs with timeouts, no timeout!");
543                 return 0;
544         }
545
546         r = clock_gettime(CLOCK_MONOTONIC, &cur_ts);
547         if (r < 0) {
548                 usbi_err("failed to read monotonic clock, errno=%d", errno);
549                 return r;
550         }
551         TIMESPEC_TO_TIMEVAL(&cur_tv, &cur_ts);
552
553         if (timercmp(&cur_tv, next_timeout, >=)) {
554                 usbi_dbg("first timeout already expired");
555                 timerclear(tv);
556         } else {
557                 timersub(next_timeout, &cur_tv, tv);
558                 usbi_dbg("next timeout in %d.%06ds", tv->tv_sec, tv->tv_usec);
559         }
560
561         return 1;
562 }
563
564 struct sync_ctrl_handle {
565         enum libusb_urb_cb_status status;
566         unsigned char *data;
567         int actual_length;
568 };
569
570 static void ctrl_transfer_cb(struct libusb_dev_handle *devh,
571         struct libusb_urb_handle *urbh, enum libusb_urb_cb_status status,
572         struct libusb_ctrl_setup *setup, unsigned char *data, int actual_length,
573         void *user_data)
574 {
575         struct sync_ctrl_handle *ctrlh = (struct sync_ctrl_handle *) user_data;
576         usbi_dbg("actual_length=%d", actual_length);
577
578         if (status == FP_URB_COMPLETED) {
579                 /* copy results into user-defined buffer */
580                 if (setup->bRequestType & LIBUSB_ENDPOINT_IN)
581                         memcpy(ctrlh->data, data, actual_length);
582         }
583
584         ctrlh->status = status;
585         ctrlh->actual_length = actual_length;
586         /* caller frees urbh */
587 }
588
589 API_EXPORTED int libusb_control_transfer(struct libusb_dev_handle *devh,
590         struct libusb_control_transfer *transfer, unsigned int timeout)
591 {
592         struct libusb_urb_handle *urbh;
593         struct sync_ctrl_handle ctrlh;
594
595         memset(&ctrlh, 0, sizeof(ctrlh));
596         ctrlh.data = transfer->data;
597
598         urbh = libusb_async_control_transfer(devh, transfer, ctrl_transfer_cb,
599                 &ctrlh, timeout);
600         if (!urbh)
601                 return -1;
602
603         while (!ctrlh.status) {
604                 int r = libusb_poll();
605                 if (r < 0) {
606                         libusb_urb_handle_cancel_sync(devh, urbh);
607                         libusb_urb_handle_free(urbh);
608                         return r;
609                 }
610         }
611
612         libusb_urb_handle_free(urbh);
613         switch (ctrlh.status) {
614         case FP_URB_COMPLETED:
615                 return ctrlh.actual_length;
616         case FP_URB_TIMEOUT:
617                 return -ETIMEDOUT;
618         default:
619                 usbi_warn("unrecognised status code %d", ctrlh.status);
620                 return -1;
621         }
622 }
623
624 struct sync_bulk_handle {
625         enum libusb_urb_cb_status status;
626         int actual_length;
627 };
628
629 static void bulk_transfer_cb(struct libusb_dev_handle *devh,
630         struct libusb_urb_handle *urbh, enum libusb_urb_cb_status status,
631         unsigned char endpoint, int rqlength, unsigned char *data,
632         int actual_length, void *user_data)
633 {
634         struct sync_bulk_handle *bulkh = (struct sync_bulk_handle *) user_data;
635         usbi_dbg("");
636         bulkh->status = status;
637         bulkh->actual_length = actual_length;
638         /* caller frees urbh */
639 }
640
641 static int do_sync_bulk_transfer(struct libusb_dev_handle *devh,
642         struct libusb_bulk_transfer *transfer, int *transferred,
643         unsigned int timeout, unsigned char urbtype)
644 {
645         struct libusb_urb_handle *urbh;
646         struct sync_bulk_handle bulkh;
647
648         memset(&bulkh, 0, sizeof(bulkh));
649
650         urbh = submit_bulk_transfer(devh, transfer, bulk_transfer_cb, &bulkh,
651                 timeout, urbtype);
652         if (!urbh)
653                 return -1;
654
655         while (!bulkh.status) {
656                 int r = libusb_poll();
657                 if (r < 0) {
658                         libusb_urb_handle_cancel_sync(devh, urbh);
659                         libusb_urb_handle_free(urbh);
660                         return r;
661                 }
662         }
663
664         *transferred = bulkh.actual_length;
665         libusb_urb_handle_free(urbh);
666
667         switch (bulkh.status) {
668         case FP_URB_COMPLETED:
669                 return 0;
670         case FP_URB_TIMEOUT:
671                 return -ETIMEDOUT;
672         default:
673                 usbi_warn("unrecognised status code %d", bulkh.status);
674                 return -1;
675         }
676 }
677
678 API_EXPORTED int libusb_interrupt_transfer(struct libusb_dev_handle *devh,
679         struct libusb_bulk_transfer *transfer, int *transferred,
680         unsigned int timeout)
681 {
682         return do_sync_bulk_transfer(devh, transfer, transferred, timeout,
683                 USB_URB_TYPE_INTERRUPT);
684 }
685
686 API_EXPORTED int libusb_bulk_transfer(struct libusb_dev_handle *devh,
687         struct libusb_bulk_transfer *transfer, int *transferred,
688         unsigned int timeout)
689 {
690         return do_sync_bulk_transfer(devh, transfer, transferred, timeout,
691                 USB_URB_TYPE_BULK);
692 }
693
694 API_EXPORTED void libusb_urb_handle_free(struct libusb_urb_handle *urbh)
695 {
696         if (!urbh)
697                 return;
698
699         if (!(urbh->flags & LIBUSB_URBH_DATA_BELONGS_TO_USER))
700                 free(urbh->urb.buffer);
701         free(urbh);
702 }
703