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