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