]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - lib/libusb/libusb10.c
MFC r219100
[FreeBSD/stable/8.git] / lib / libusb / libusb10.c
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4  * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/fcntl.h>
29 #include <sys/ioctl.h>
30 #include <sys/queue.h>
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <poll.h>
35 #include <pthread.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #define libusb_device_handle libusb20_device
41
42 #include "libusb20.h"
43 #include "libusb20_desc.h"
44 #include "libusb20_int.h"
45 #include "libusb.h"
46 #include "libusb10.h"
47
48 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
49 struct libusb_context *usbi_default_context = NULL;
50
51 /* Prototypes */
52
53 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
54 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
55 static int libusb10_convert_error(uint8_t status);
56 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
57 static void libusb10_isoc_proxy(struct libusb20_transfer *);
58 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
59 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
60 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
61
62 /*  Library initialisation / deinitialisation */
63
64 void
65 libusb_set_debug(libusb_context *ctx, int level)
66 {
67         ctx = GET_CONTEXT(ctx);
68         if (ctx)
69                 ctx->debug = level;
70 }
71
72 static void
73 libusb_set_nonblocking(int f)
74 {
75         int flags;
76
77         /*
78          * We ignore any failures in this function, hence the
79          * non-blocking flag is not critical to the operation of
80          * libUSB. We use F_GETFL and F_SETFL to be compatible with
81          * Linux.
82          */
83
84         flags = fcntl(f, F_GETFL, NULL);
85         if (flags == -1)
86                 return;
87         flags |= O_NONBLOCK;
88         fcntl(f, F_SETFL, flags);
89 }
90
91 int
92 libusb_init(libusb_context **context)
93 {
94         struct libusb_context *ctx;
95         char *debug;
96         int ret;
97
98         ctx = malloc(sizeof(*ctx));
99         if (!ctx)
100                 return (LIBUSB_ERROR_INVALID_PARAM);
101
102         memset(ctx, 0, sizeof(*ctx));
103
104         debug = getenv("LIBUSB_DEBUG");
105         if (debug != NULL) {
106                 ctx->debug = atoi(debug);
107                 if (ctx->debug != 0)
108                         ctx->debug_fixed = 1;
109         }
110         TAILQ_INIT(&ctx->pollfds);
111         TAILQ_INIT(&ctx->tr_done);
112
113         pthread_mutex_init(&ctx->ctx_lock, NULL);
114         pthread_cond_init(&ctx->ctx_cond, NULL);
115
116         ctx->ctx_handler = NO_THREAD;
117
118         ret = pipe(ctx->ctrl_pipe);
119         if (ret < 0) {
120                 pthread_mutex_destroy(&ctx->ctx_lock);
121                 pthread_cond_destroy(&ctx->ctx_cond);
122                 free(ctx);
123                 return (LIBUSB_ERROR_OTHER);
124         }
125         /* set non-blocking mode on the control pipe to avoid deadlock */
126         libusb_set_nonblocking(ctx->ctrl_pipe[0]);
127         libusb_set_nonblocking(ctx->ctrl_pipe[1]);
128
129         libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
130
131         pthread_mutex_lock(&default_context_lock);
132         if (usbi_default_context == NULL) {
133                 usbi_default_context = ctx;
134         }
135         pthread_mutex_unlock(&default_context_lock);
136
137         if (context)
138                 *context = ctx;
139
140         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
141
142         return (0);
143 }
144
145 void
146 libusb_exit(libusb_context *ctx)
147 {
148         ctx = GET_CONTEXT(ctx);
149
150         if (ctx == NULL)
151                 return;
152
153         /* XXX cleanup devices */
154
155         libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
156         close(ctx->ctrl_pipe[0]);
157         close(ctx->ctrl_pipe[1]);
158         pthread_mutex_destroy(&ctx->ctx_lock);
159         pthread_cond_destroy(&ctx->ctx_cond);
160
161         pthread_mutex_lock(&default_context_lock);
162         if (ctx == usbi_default_context) {
163                 usbi_default_context = NULL;
164         }
165         pthread_mutex_unlock(&default_context_lock);
166
167         free(ctx);
168 }
169
170 /* Device handling and initialisation. */
171
172 ssize_t
173 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
174 {
175         struct libusb20_backend *usb_backend;
176         struct libusb20_device *pdev;
177         struct libusb_device *dev;
178         int i;
179
180         ctx = GET_CONTEXT(ctx);
181
182         if (ctx == NULL)
183                 return (LIBUSB_ERROR_INVALID_PARAM);
184
185         if (list == NULL)
186                 return (LIBUSB_ERROR_INVALID_PARAM);
187
188         usb_backend = libusb20_be_alloc_default();
189         if (usb_backend == NULL)
190                 return (LIBUSB_ERROR_NO_MEM);
191
192         /* figure out how many USB devices are present */
193         pdev = NULL;
194         i = 0;
195         while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
196                 i++;
197
198         /* allocate device pointer list */
199         *list = malloc((i + 1) * sizeof(void *));
200         if (*list == NULL) {
201                 libusb20_be_free(usb_backend);
202                 return (LIBUSB_ERROR_NO_MEM);
203         }
204         /* create libusb v1.0 compliant devices */
205         i = 0;
206         while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
207
208                 dev = malloc(sizeof(*dev));
209                 if (dev == NULL) {
210                         while (i != 0) {
211                                 libusb_unref_device((*list)[i - 1]);
212                                 i--;
213                         }
214                         free(*list);
215                         *list = NULL;
216                         libusb20_be_free(usb_backend);
217                         return (LIBUSB_ERROR_NO_MEM);
218                 }
219
220                 /* get device into libUSB v1.0 list */
221                 libusb20_be_dequeue_device(usb_backend, pdev);
222
223                 memset(dev, 0, sizeof(*dev));
224
225                 /* init transfer queues */
226                 TAILQ_INIT(&dev->tr_head);
227
228                 /* set context we belong to */
229                 dev->ctx = ctx;
230
231                 /* link together the two structures */
232                 dev->os_priv = pdev;
233                 pdev->privLuData = dev;
234
235                 (*list)[i] = libusb_ref_device(dev);
236                 i++;
237         }
238         (*list)[i] = NULL;
239
240         libusb20_be_free(usb_backend);
241         return (i);
242 }
243
244 void
245 libusb_free_device_list(libusb_device **list, int unref_devices)
246 {
247         int i;
248
249         if (list == NULL)
250                 return;                 /* be NULL safe */
251
252         if (unref_devices) {
253                 for (i = 0; list[i] != NULL; i++)
254                         libusb_unref_device(list[i]);
255         }
256         free(list);
257 }
258
259 uint8_t
260 libusb_get_bus_number(libusb_device *dev)
261 {
262         if (dev == NULL)
263                 return (0);             /* should not happen */
264         return (libusb20_dev_get_bus_number(dev->os_priv));
265 }
266
267 uint8_t
268 libusb_get_device_address(libusb_device *dev)
269 {
270         if (dev == NULL)
271                 return (0);             /* should not happen */
272         return (libusb20_dev_get_address(dev->os_priv));
273 }
274
275 int
276 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
277 {
278         struct libusb_config_descriptor *pdconf;
279         struct libusb_interface *pinf;
280         struct libusb_interface_descriptor *pdinf;
281         struct libusb_endpoint_descriptor *pdend;
282         int i;
283         int j;
284         int k;
285         int ret;
286
287         if (dev == NULL)
288                 return (LIBUSB_ERROR_NO_DEVICE);
289
290         ret = libusb_get_active_config_descriptor(dev, &pdconf);
291         if (ret < 0)
292                 return (ret);
293
294         ret = LIBUSB_ERROR_NOT_FOUND;
295         for (i = 0; i < pdconf->bNumInterfaces; i++) {
296                 pinf = &pdconf->interface[i];
297                 for (j = 0; j < pinf->num_altsetting; j++) {
298                         pdinf = &pinf->altsetting[j];
299                         for (k = 0; k < pdinf->bNumEndpoints; k++) {
300                                 pdend = &pdinf->endpoint[k];
301                                 if (pdend->bEndpointAddress == endpoint) {
302                                         ret = pdend->wMaxPacketSize;
303                                         goto out;
304                                 }
305                         }
306                 }
307         }
308
309 out:
310         libusb_free_config_descriptor(pdconf);
311         return (ret);
312 }
313
314 libusb_device *
315 libusb_ref_device(libusb_device *dev)
316 {
317         if (dev == NULL)
318                 return (NULL);          /* be NULL safe */
319
320         CTX_LOCK(dev->ctx);
321         dev->refcnt++;
322         CTX_UNLOCK(dev->ctx);
323
324         return (dev);
325 }
326
327 void
328 libusb_unref_device(libusb_device *dev)
329 {
330         if (dev == NULL)
331                 return;                 /* be NULL safe */
332
333         CTX_LOCK(dev->ctx);
334         dev->refcnt--;
335         CTX_UNLOCK(dev->ctx);
336
337         if (dev->refcnt == 0) {
338                 libusb20_dev_free(dev->os_priv);
339                 free(dev);
340         }
341 }
342
343 int
344 libusb_open(libusb_device *dev, libusb_device_handle **devh)
345 {
346         libusb_context *ctx = dev->ctx;
347         struct libusb20_device *pdev = dev->os_priv;
348         uint8_t dummy;
349         int err;
350
351         if (devh == NULL)
352                 return (LIBUSB_ERROR_INVALID_PARAM);
353
354         /* set default device handle value */
355         *devh = NULL;
356
357         dev = libusb_ref_device(dev);
358         if (dev == NULL)
359                 return (LIBUSB_ERROR_INVALID_PARAM);
360
361         err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
362         if (err) {
363                 libusb_unref_device(dev);
364                 return (LIBUSB_ERROR_NO_MEM);
365         }
366         libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
367             POLLOUT | POLLRDNORM | POLLWRNORM);
368
369         /* make sure our event loop detects the new device */
370         dummy = 0;
371         err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
372         if (err < (int)sizeof(dummy)) {
373                 /* ignore error, if any */
374                 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
375         }
376         *devh = pdev;
377
378         return (0);
379 }
380
381 libusb_device_handle *
382 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
383     uint16_t product_id)
384 {
385         struct libusb_device **devs;
386         struct libusb20_device *pdev;
387         struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
388         int i;
389         int j;
390
391         ctx = GET_CONTEXT(ctx);
392         if (ctx == NULL)
393                 return (NULL);          /* be NULL safe */
394
395         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
396
397         if ((i = libusb_get_device_list(ctx, &devs)) < 0)
398                 return (NULL);
399
400         for (j = 0; j < i; j++) {
401                 pdev = devs[j]->os_priv;
402                 pdesc = libusb20_dev_get_device_desc(pdev);
403                 /*
404                  * NOTE: The USB library will automatically swap the
405                  * fields in the device descriptor to be of host
406                  * endian type!
407                  */
408                 if (pdesc->idVendor == vendor_id &&
409                     pdesc->idProduct == product_id) {
410                         if (libusb_open(devs[j], &pdev) < 0)
411                                 pdev = NULL;
412                         break;
413                 }
414         }
415         if (j == i)
416                 pdev = NULL;
417
418         libusb_free_device_list(devs, 1);
419         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
420         return (pdev);
421 }
422
423 void
424 libusb_close(struct libusb20_device *pdev)
425 {
426         libusb_context *ctx;
427         struct libusb_device *dev;
428         uint8_t dummy;
429         int err;
430
431         if (pdev == NULL)
432                 return;                 /* be NULL safe */
433
434         dev = libusb_get_device(pdev);
435         ctx = dev->ctx;
436
437         libusb10_remove_pollfd(ctx, &dev->dev_poll);
438
439         libusb20_dev_close(pdev);
440
441         /* unref will free the "pdev" when the refcount reaches zero */
442         libusb_unref_device(dev);
443
444         /* make sure our event loop detects the closed device */
445         dummy = 0;
446         err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
447         if (err < (int)sizeof(dummy)) {
448                 /* ignore error, if any */
449                 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
450         }
451 }
452
453 libusb_device *
454 libusb_get_device(struct libusb20_device *pdev)
455 {
456         if (pdev == NULL)
457                 return (NULL);
458         return ((libusb_device *)pdev->privLuData);
459 }
460
461 int
462 libusb_get_configuration(struct libusb20_device *pdev, int *config)
463 {
464         struct libusb20_config *pconf;
465
466         if (pdev == NULL || config == NULL)
467                 return (LIBUSB_ERROR_INVALID_PARAM);
468
469         pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
470         if (pconf == NULL)
471                 return (LIBUSB_ERROR_NO_MEM);
472
473         *config = pconf->desc.bConfigurationValue;
474
475         free(pconf);
476
477         return (0);
478 }
479
480 int
481 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
482 {
483         struct libusb20_config *pconf;
484         struct libusb_device *dev;
485         int err;
486         uint8_t i;
487
488         dev = libusb_get_device(pdev);
489         if (dev == NULL)
490                 return (LIBUSB_ERROR_INVALID_PARAM);
491
492         if (configuration < 1) {
493                 /* unconfigure */
494                 i = 255;
495         } else {
496                 for (i = 0; i != 255; i++) {
497                         uint8_t found;
498
499                         pconf = libusb20_dev_alloc_config(pdev, i);
500                         if (pconf == NULL)
501                                 return (LIBUSB_ERROR_INVALID_PARAM);
502                         found = (pconf->desc.bConfigurationValue
503                             == configuration);
504                         free(pconf);
505
506                         if (found)
507                                 goto set_config;
508                 }
509                 return (LIBUSB_ERROR_INVALID_PARAM);
510         }
511
512 set_config:
513
514         libusb10_cancel_all_transfer(dev);
515
516         libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
517
518         err = libusb20_dev_set_config_index(pdev, i);
519
520         libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
521             POLLOUT | POLLRDNORM | POLLWRNORM);
522
523         return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
524 }
525
526 int
527 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
528 {
529         libusb_device *dev;
530         int err = 0;
531
532         dev = libusb_get_device(pdev);
533         if (dev == NULL)
534                 return (LIBUSB_ERROR_INVALID_PARAM);
535
536         if (interface_number < 0 || interface_number > 31)
537                 return (LIBUSB_ERROR_INVALID_PARAM);
538
539         CTX_LOCK(dev->ctx);
540         if (dev->claimed_interfaces & (1 << interface_number))
541                 err = LIBUSB_ERROR_BUSY;
542
543         if (!err)
544                 dev->claimed_interfaces |= (1 << interface_number);
545         CTX_UNLOCK(dev->ctx);
546         return (err);
547 }
548
549 int
550 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
551 {
552         libusb_device *dev;
553         int err = 0;
554
555         dev = libusb_get_device(pdev);
556         if (dev == NULL)
557                 return (LIBUSB_ERROR_INVALID_PARAM);
558
559         if (interface_number < 0 || interface_number > 31)
560                 return (LIBUSB_ERROR_INVALID_PARAM);
561
562         CTX_LOCK(dev->ctx);
563         if (!(dev->claimed_interfaces & (1 << interface_number)))
564                 err = LIBUSB_ERROR_NOT_FOUND;
565
566         if (!err)
567                 dev->claimed_interfaces &= ~(1 << interface_number);
568         CTX_UNLOCK(dev->ctx);
569         return (err);
570 }
571
572 int
573 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
574     int interface_number, int alternate_setting)
575 {
576         libusb_device *dev;
577         int err = 0;
578
579         dev = libusb_get_device(pdev);
580         if (dev == NULL)
581                 return (LIBUSB_ERROR_INVALID_PARAM);
582
583         if (interface_number < 0 || interface_number > 31)
584                 return (LIBUSB_ERROR_INVALID_PARAM);
585
586         CTX_LOCK(dev->ctx);
587         if (!(dev->claimed_interfaces & (1 << interface_number)))
588                 err = LIBUSB_ERROR_NOT_FOUND;
589         CTX_UNLOCK(dev->ctx);
590
591         if (err)
592                 return (err);
593
594         libusb10_cancel_all_transfer(dev);
595
596         libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
597
598         err = libusb20_dev_set_alt_index(pdev,
599             interface_number, alternate_setting);
600
601         libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
602             pdev, libusb20_dev_get_fd(pdev),
603             POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
604
605         return (err ? LIBUSB_ERROR_OTHER : 0);
606 }
607
608 static struct libusb20_transfer *
609 libusb10_get_transfer(struct libusb20_device *pdev,
610     uint8_t endpoint, uint8_t index)
611 {
612         index &= 1;                     /* double buffering */
613
614         index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
615
616         if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
617                 /* this is an IN endpoint */
618                 index |= 2;
619         }
620         return (libusb20_tr_get_pointer(pdev, index));
621 }
622
623 int
624 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
625 {
626         struct libusb20_transfer *xfer;
627         struct libusb_device *dev;
628         int err;
629
630         xfer = libusb10_get_transfer(pdev, endpoint, 0);
631         if (xfer == NULL)
632                 return (LIBUSB_ERROR_INVALID_PARAM);
633
634         dev = libusb_get_device(pdev);
635         if (dev == NULL)
636                 return (LIBUSB_ERROR_INVALID_PARAM);
637
638         CTX_LOCK(dev->ctx);
639         err = libusb20_tr_open(xfer, 0, 0, endpoint);
640         CTX_UNLOCK(dev->ctx);
641
642         if (err != 0 && err != LIBUSB20_ERROR_BUSY)
643                 return (LIBUSB_ERROR_OTHER);
644
645         libusb20_tr_clear_stall_sync(xfer);
646
647         /* check if we opened the transfer */
648         if (err == 0) {
649                 CTX_LOCK(dev->ctx);
650                 libusb20_tr_close(xfer);
651                 CTX_UNLOCK(dev->ctx);
652         }
653         return (0);                     /* success */
654 }
655
656 int
657 libusb_reset_device(struct libusb20_device *pdev)
658 {
659         libusb_device *dev;
660         int err;
661
662         dev = libusb_get_device(pdev);
663         if (dev == NULL)
664                 return (LIBUSB_ERROR_INVALID_PARAM);
665
666         libusb10_cancel_all_transfer(dev);
667
668         libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
669
670         err = libusb20_dev_reset(pdev);
671
672         libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
673             pdev, libusb20_dev_get_fd(pdev),
674             POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
675
676         return (err ? LIBUSB_ERROR_OTHER : 0);
677 }
678
679 int
680 libusb_check_connected(struct libusb20_device *pdev)
681 {
682         libusb_device *dev;
683         int err;
684
685         dev = libusb_get_device(pdev);
686         if (dev == NULL)
687                 return (LIBUSB_ERROR_INVALID_PARAM);
688
689         err = libusb20_dev_check_connected(pdev);
690
691         return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
692 }
693
694 int
695 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
696 {
697         if (pdev == NULL)
698                 return (LIBUSB_ERROR_INVALID_PARAM);
699
700         return (libusb20_dev_kernel_driver_active(
701             pdev, interface));
702 }
703
704 int
705 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
706     char *name, int namelen)
707 {
708         return (libusb_get_driver(pdev, interface, name, namelen));
709 }
710
711 int
712 libusb_get_driver(struct libusb20_device *pdev, int interface,
713     char *name, int namelen)
714 {
715         char *ptr;
716         int err;
717
718         if (pdev == NULL)
719                 return (LIBUSB_ERROR_INVALID_PARAM);
720         if (namelen < 1)
721                 return (LIBUSB_ERROR_INVALID_PARAM);
722
723         err = libusb20_dev_get_iface_desc(
724             pdev, interface, name, namelen);
725
726         if (err != 0)
727                 return (LIBUSB_ERROR_OTHER);
728
729         /* we only want the driver name */
730         ptr = strstr(name, ":");
731         if (ptr != NULL)
732                 *ptr = 0;
733
734         return (0);
735 }
736
737 int
738 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
739 {
740         return (libusb_detach_kernel_driver(pdev, interface));
741 }
742
743 int
744 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
745 {
746         int err;
747
748         if (pdev == NULL)
749                 return (LIBUSB_ERROR_INVALID_PARAM);
750
751         err = libusb20_dev_detach_kernel_driver(
752             pdev, interface);
753
754         return (err ? LIBUSB_ERROR_OTHER : 0);
755 }
756
757 int
758 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
759 {
760         if (pdev == NULL)
761                 return (LIBUSB_ERROR_INVALID_PARAM);
762         /* stub - currently not supported by libusb20 */
763         return (0);
764 }
765
766 /* Asynchronous device I/O */
767
768 struct libusb_transfer *
769 libusb_alloc_transfer(int iso_packets)
770 {
771         struct libusb_transfer *uxfer;
772         struct libusb_super_transfer *sxfer;
773         int len;
774
775         len = sizeof(struct libusb_transfer) +
776             sizeof(struct libusb_super_transfer) +
777             (iso_packets * sizeof(libusb_iso_packet_descriptor));
778
779         sxfer = malloc(len);
780         if (sxfer == NULL)
781                 return (NULL);
782
783         memset(sxfer, 0, len);
784
785         uxfer = (struct libusb_transfer *)(
786             ((uint8_t *)sxfer) + sizeof(*sxfer));
787
788         /* set default value */
789         uxfer->num_iso_packets = iso_packets;
790
791         return (uxfer);
792 }
793
794 void
795 libusb_free_transfer(struct libusb_transfer *uxfer)
796 {
797         struct libusb_super_transfer *sxfer;
798
799         if (uxfer == NULL)
800                 return;                 /* be NULL safe */
801
802         /* check if we should free the transfer buffer */
803         if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
804                 free(uxfer->buffer);
805
806         sxfer = (struct libusb_super_transfer *)(
807             (uint8_t *)uxfer - sizeof(*sxfer));
808
809         free(sxfer);
810 }
811
812 static uint32_t
813 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
814 {
815         uint32_t ret;
816
817         switch (xfer->type) {
818         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
819                 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE;        /* 60ms */
820                 break;
821         case LIBUSB_TRANSFER_TYPE_CONTROL:
822                 ret = 2;
823                 break;
824         default:
825                 ret = 1;
826                 break;
827         }
828         return (ret);
829 }
830
831 static int
832 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
833 {
834         int ret;
835         int usb_speed;
836
837         usb_speed = libusb20_dev_get_speed(pdev);
838
839         switch (xfer->type) {
840         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
841                 ret = 0;                /* kernel will auto-select */
842                 break;
843         case LIBUSB_TRANSFER_TYPE_CONTROL:
844                 ret = 1024;
845                 break;
846         default:
847                 switch (usb_speed) {
848                 case LIBUSB20_SPEED_LOW:
849                         ret = 256;
850                         break;
851                 case LIBUSB20_SPEED_FULL:
852                         ret = 4096;
853                         break;
854                 default:
855                         ret = 16384;
856                         break;
857                 }
858                 break;
859         }
860         return (ret);
861 }
862
863 static int
864 libusb10_convert_error(uint8_t status)
865 {
866         ;                               /* indent fix */
867
868         switch (status) {
869         case LIBUSB20_TRANSFER_START:
870         case LIBUSB20_TRANSFER_COMPLETED:
871                 return (LIBUSB_TRANSFER_COMPLETED);
872         case LIBUSB20_TRANSFER_OVERFLOW:
873                 return (LIBUSB_TRANSFER_OVERFLOW);
874         case LIBUSB20_TRANSFER_NO_DEVICE:
875                 return (LIBUSB_TRANSFER_NO_DEVICE);
876         case LIBUSB20_TRANSFER_STALL:
877                 return (LIBUSB_TRANSFER_STALL);
878         case LIBUSB20_TRANSFER_CANCELLED:
879                 return (LIBUSB_TRANSFER_CANCELLED);
880         case LIBUSB20_TRANSFER_TIMED_OUT:
881                 return (LIBUSB_TRANSFER_TIMED_OUT);
882         default:
883                 return (LIBUSB_TRANSFER_ERROR);
884         }
885 }
886
887 /* This function must be called locked */
888
889 static void
890 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
891     struct libusb_super_transfer *sxfer, int status)
892 {
893         struct libusb_transfer *uxfer;
894         struct libusb_device *dev;
895
896         uxfer = (struct libusb_transfer *)(
897             ((uint8_t *)sxfer) + sizeof(*sxfer));
898
899         if (pxfer != NULL)
900                 libusb20_tr_set_priv_sc1(pxfer, NULL);
901
902         /* set transfer status */
903         uxfer->status = status;
904
905         /* update super transfer state */
906         sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
907
908         dev = libusb_get_device(uxfer->dev_handle);
909
910         TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
911 }
912
913 /* This function must be called locked */
914
915 static void
916 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
917 {
918         struct libusb_super_transfer *sxfer;
919         struct libusb_transfer *uxfer;
920         uint32_t actlen;
921         uint16_t iso_packets;
922         uint16_t i;
923         uint8_t status;
924         uint8_t flags;
925
926         status = libusb20_tr_get_status(pxfer);
927         sxfer = libusb20_tr_get_priv_sc1(pxfer);
928         actlen = libusb20_tr_get_actual_length(pxfer);
929         iso_packets = libusb20_tr_get_max_frames(pxfer);
930
931         if (sxfer == NULL)
932                 return;                 /* cancelled - nothing to do */
933
934         uxfer = (struct libusb_transfer *)(
935             ((uint8_t *)sxfer) + sizeof(*sxfer));
936
937         if (iso_packets > uxfer->num_iso_packets)
938                 iso_packets = uxfer->num_iso_packets;
939
940         if (iso_packets == 0)
941                 return;                 /* nothing to do */
942
943         /* make sure that the number of ISOCHRONOUS packets is valid */
944         uxfer->num_iso_packets = iso_packets;
945
946         flags = uxfer->flags;
947
948         switch (status) {
949         case LIBUSB20_TRANSFER_COMPLETED:
950
951                 /* update actual length */
952                 uxfer->actual_length = actlen;
953                 for (i = 0; i != iso_packets; i++) {
954                         uxfer->iso_packet_desc[i].actual_length =
955                             libusb20_tr_get_length(pxfer, i);
956                 }
957                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
958                 break;
959
960         case LIBUSB20_TRANSFER_START:
961
962                 /* setup length(s) */
963                 actlen = 0;
964                 for (i = 0; i != iso_packets; i++) {
965                         libusb20_tr_setup_isoc(pxfer,
966                             &uxfer->buffer[actlen],
967                             uxfer->iso_packet_desc[i].length, i);
968                         actlen += uxfer->iso_packet_desc[i].length;
969                 }
970
971                 /* no remainder */
972                 sxfer->rem_len = 0;
973
974                 libusb20_tr_set_total_frames(pxfer, iso_packets);
975                 libusb20_tr_submit(pxfer);
976
977                 /* fork another USB transfer, if any */
978                 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
979                 break;
980
981         default:
982                 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
983                 break;
984         }
985 }
986
987 /* This function must be called locked */
988
989 static void
990 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
991 {
992         struct libusb_super_transfer *sxfer;
993         struct libusb_transfer *uxfer;
994         uint32_t max_bulk;
995         uint32_t actlen;
996         uint8_t status;
997         uint8_t flags;
998
999         status = libusb20_tr_get_status(pxfer);
1000         sxfer = libusb20_tr_get_priv_sc1(pxfer);
1001         max_bulk = libusb20_tr_get_max_total_length(pxfer);
1002         actlen = libusb20_tr_get_actual_length(pxfer);
1003
1004         if (sxfer == NULL)
1005                 return;                 /* cancelled - nothing to do */
1006
1007         uxfer = (struct libusb_transfer *)(
1008             ((uint8_t *)sxfer) + sizeof(*sxfer));
1009
1010         flags = uxfer->flags;
1011
1012         switch (status) {
1013         case LIBUSB20_TRANSFER_COMPLETED:
1014
1015                 uxfer->actual_length += actlen;
1016
1017                 /* check for short packet */
1018                 if (sxfer->last_len != actlen) {
1019                         if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1020                                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1021                         } else {
1022                                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1023                         }
1024                         break;
1025                 }
1026                 /* check for end of data */
1027                 if (sxfer->rem_len == 0) {
1028                         libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1029                         break;
1030                 }
1031                 /* FALLTHROUGH */
1032
1033         case LIBUSB20_TRANSFER_START:
1034                 if (max_bulk > sxfer->rem_len) {
1035                         max_bulk = sxfer->rem_len;
1036                 }
1037                 /* setup new BULK or INTERRUPT transaction */
1038                 libusb20_tr_setup_bulk(pxfer,
1039                     sxfer->curr_data, max_bulk, uxfer->timeout);
1040
1041                 /* update counters */
1042                 sxfer->last_len = max_bulk;
1043                 sxfer->curr_data += max_bulk;
1044                 sxfer->rem_len -= max_bulk;
1045
1046                 libusb20_tr_submit(pxfer);
1047
1048                 /* check if we can fork another USB transfer */
1049                 if (sxfer->rem_len == 0)
1050                         libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1051                 break;
1052
1053         default:
1054                 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1055                 break;
1056         }
1057 }
1058
1059 /* This function must be called locked */
1060
1061 static void
1062 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1063 {
1064         struct libusb_super_transfer *sxfer;
1065         struct libusb_transfer *uxfer;
1066         uint32_t max_bulk;
1067         uint32_t actlen;
1068         uint8_t status;
1069         uint8_t flags;
1070
1071         status = libusb20_tr_get_status(pxfer);
1072         sxfer = libusb20_tr_get_priv_sc1(pxfer);
1073         max_bulk = libusb20_tr_get_max_total_length(pxfer);
1074         actlen = libusb20_tr_get_actual_length(pxfer);
1075
1076         if (sxfer == NULL)
1077                 return;                 /* cancelled - nothing to do */
1078
1079         uxfer = (struct libusb_transfer *)(
1080             ((uint8_t *)sxfer) + sizeof(*sxfer));
1081
1082         flags = uxfer->flags;
1083
1084         switch (status) {
1085         case LIBUSB20_TRANSFER_COMPLETED:
1086
1087                 uxfer->actual_length += actlen;
1088
1089                 /* subtract length of SETUP packet, if any */
1090                 actlen -= libusb20_tr_get_length(pxfer, 0);
1091
1092                 /* check for short packet */
1093                 if (sxfer->last_len != actlen) {
1094                         if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1095                                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1096                         } else {
1097                                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1098                         }
1099                         break;
1100                 }
1101                 /* check for end of data */
1102                 if (sxfer->rem_len == 0) {
1103                         libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1104                         break;
1105                 }
1106                 /* FALLTHROUGH */
1107
1108         case LIBUSB20_TRANSFER_START:
1109                 if (max_bulk > sxfer->rem_len) {
1110                         max_bulk = sxfer->rem_len;
1111                 }
1112                 /* setup new CONTROL transaction */
1113                 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1114                         /* next fragment - don't send SETUP packet */
1115                         libusb20_tr_set_length(pxfer, 0, 0);
1116                 } else {
1117                         /* first fragment - send SETUP packet */
1118                         libusb20_tr_set_length(pxfer, 8, 0);
1119                         libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1120                 }
1121
1122                 if (max_bulk != 0) {
1123                         libusb20_tr_set_length(pxfer, max_bulk, 1);
1124                         libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1125                         libusb20_tr_set_total_frames(pxfer, 2);
1126                 } else {
1127                         libusb20_tr_set_total_frames(pxfer, 1);
1128                 }
1129
1130                 /* update counters */
1131                 sxfer->last_len = max_bulk;
1132                 sxfer->curr_data += max_bulk;
1133                 sxfer->rem_len -= max_bulk;
1134
1135                 libusb20_tr_submit(pxfer);
1136
1137                 /* check if we can fork another USB transfer */
1138                 if (sxfer->rem_len == 0)
1139                         libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1140                 break;
1141
1142         default:
1143                 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1144                 break;
1145         }
1146 }
1147
1148 /* The following function must be called locked */
1149
1150 static void
1151 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1152 {
1153         struct libusb20_transfer *pxfer0;
1154         struct libusb20_transfer *pxfer1;
1155         struct libusb_super_transfer *sxfer;
1156         struct libusb_transfer *uxfer;
1157         struct libusb_device *dev;
1158         int err;
1159         int buffsize;
1160         int maxframe;
1161         int temp;
1162         uint8_t dummy;
1163
1164         dev = libusb_get_device(pdev);
1165
1166         pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1167         pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1168
1169         if (pxfer0 == NULL || pxfer1 == NULL)
1170                 return;                 /* shouldn't happen */
1171
1172         temp = 0;
1173         if (libusb20_tr_pending(pxfer0))
1174                 temp |= 1;
1175         if (libusb20_tr_pending(pxfer1))
1176                 temp |= 2;
1177
1178         switch (temp) {
1179         case 3:
1180                 /* wait till one of the transfers complete */
1181                 return;
1182         case 2:
1183                 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1184                 if (sxfer == NULL)
1185                         return;         /* cancelling */
1186                 if (sxfer->rem_len)
1187                         return;         /* cannot queue another one */
1188                 /* swap transfers */
1189                 pxfer1 = pxfer0;
1190                 break;
1191         case 1:
1192                 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1193                 if (sxfer == NULL)
1194                         return;         /* cancelling */
1195                 if (sxfer->rem_len)
1196                         return;         /* cannot queue another one */
1197                 /* swap transfers */
1198                 pxfer0 = pxfer1;
1199                 break;
1200         default:
1201                 break;
1202         }
1203
1204         /* find next transfer on same endpoint */
1205         TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1206
1207                 uxfer = (struct libusb_transfer *)(
1208                     ((uint8_t *)sxfer) + sizeof(*sxfer));
1209
1210                 if (uxfer->endpoint == endpoint) {
1211                         TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1212                         sxfer->entry.tqe_prev = NULL;
1213                         goto found;
1214                 }
1215         }
1216         return;                         /* success */
1217
1218 found:
1219
1220         libusb20_tr_set_priv_sc0(pxfer0, pdev);
1221         libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1222
1223         /* reset super transfer state */
1224         sxfer->rem_len = uxfer->length;
1225         sxfer->curr_data = uxfer->buffer;
1226         uxfer->actual_length = 0;
1227
1228         switch (uxfer->type) {
1229         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1230                 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1231                 break;
1232         case LIBUSB_TRANSFER_TYPE_BULK:
1233         case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1234                 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1235                 break;
1236         case LIBUSB_TRANSFER_TYPE_CONTROL:
1237                 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1238                 if (sxfer->rem_len < 8)
1239                         goto failure;
1240
1241                 /* remove SETUP packet from data */
1242                 sxfer->rem_len -= 8;
1243                 sxfer->curr_data += 8;
1244                 break;
1245         default:
1246                 goto failure;
1247         }
1248
1249         buffsize = libusb10_get_buffsize(pdev, uxfer);
1250         maxframe = libusb10_get_maxframe(pdev, uxfer);
1251
1252         /* make sure the transfer is opened */
1253         err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1254         if (err && (err != LIBUSB20_ERROR_BUSY)) {
1255                 goto failure;
1256         }
1257         libusb20_tr_start(pxfer0);
1258         return;
1259
1260 failure:
1261         libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1262
1263         /* make sure our event loop spins the done handler */
1264         dummy = 0;
1265         write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1266 }
1267
1268 /* The following function must be called unlocked */
1269
1270 int
1271 libusb_submit_transfer(struct libusb_transfer *uxfer)
1272 {
1273         struct libusb20_transfer *pxfer0;
1274         struct libusb20_transfer *pxfer1;
1275         struct libusb_super_transfer *sxfer;
1276         struct libusb_device *dev;
1277         uint32_t endpoint;
1278         int err;
1279
1280         if (uxfer == NULL)
1281                 return (LIBUSB_ERROR_INVALID_PARAM);
1282
1283         if (uxfer->dev_handle == NULL)
1284                 return (LIBUSB_ERROR_INVALID_PARAM);
1285
1286         endpoint = uxfer->endpoint;
1287
1288         if (endpoint > 255)
1289                 return (LIBUSB_ERROR_INVALID_PARAM);
1290
1291         dev = libusb_get_device(uxfer->dev_handle);
1292
1293         DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1294
1295         sxfer = (struct libusb_super_transfer *)(
1296             (uint8_t *)uxfer - sizeof(*sxfer));
1297
1298         CTX_LOCK(dev->ctx);
1299
1300         pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1301         pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1302
1303         if (pxfer0 == NULL || pxfer1 == NULL) {
1304                 err = LIBUSB_ERROR_OTHER;
1305         } else if ((sxfer->entry.tqe_prev != NULL) ||
1306             (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1307             (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1308                 err = LIBUSB_ERROR_BUSY;
1309         } else {
1310
1311                 /* set pending state */
1312                 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1313
1314                 /* insert transfer into transfer head list */
1315                 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1316
1317                 /* start work transfers */
1318                 libusb10_submit_transfer_sub(
1319                     uxfer->dev_handle, endpoint);
1320
1321                 err = 0;                /* success */
1322         }
1323
1324         CTX_UNLOCK(dev->ctx);
1325
1326         DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1327
1328         return (err);
1329 }
1330
1331 /* Asynchronous transfer cancel */
1332
1333 int
1334 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1335 {
1336         struct libusb20_transfer *pxfer0;
1337         struct libusb20_transfer *pxfer1;
1338         struct libusb_super_transfer *sxfer;
1339         struct libusb_device *dev;
1340         uint32_t endpoint;
1341         int retval;
1342
1343         if (uxfer == NULL)
1344                 return (LIBUSB_ERROR_INVALID_PARAM);
1345
1346         /* check if not initialised */
1347         if (uxfer->dev_handle == NULL)
1348                 return (LIBUSB_ERROR_NOT_FOUND);
1349
1350         endpoint = uxfer->endpoint;
1351
1352         if (endpoint > 255)
1353                 return (LIBUSB_ERROR_INVALID_PARAM);
1354
1355         dev = libusb_get_device(uxfer->dev_handle);
1356
1357         DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1358
1359         sxfer = (struct libusb_super_transfer *)(
1360             (uint8_t *)uxfer - sizeof(*sxfer));
1361
1362         retval = 0;
1363
1364         CTX_LOCK(dev->ctx);
1365
1366         pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1367         pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1368
1369         if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1370                 /* only update the transfer status */
1371                 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1372                 retval = LIBUSB_ERROR_NOT_FOUND;
1373         } else if (sxfer->entry.tqe_prev != NULL) {
1374                 /* we are lucky - transfer is on a queue */
1375                 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1376                 sxfer->entry.tqe_prev = NULL;
1377                 libusb10_complete_transfer(NULL,
1378                     sxfer, LIBUSB_TRANSFER_CANCELLED);
1379         } else if (pxfer0 == NULL || pxfer1 == NULL) {
1380                 /* not started */
1381                 retval = LIBUSB_ERROR_NOT_FOUND;
1382         } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1383                 libusb10_complete_transfer(pxfer0,
1384                     sxfer, LIBUSB_TRANSFER_CANCELLED);
1385                 libusb20_tr_stop(pxfer0);
1386                 /* make sure the queue doesn't stall */
1387                 libusb10_submit_transfer_sub(
1388                     uxfer->dev_handle, endpoint);
1389         } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1390                 libusb10_complete_transfer(pxfer1,
1391                     sxfer, LIBUSB_TRANSFER_CANCELLED);
1392                 libusb20_tr_stop(pxfer1);
1393                 /* make sure the queue doesn't stall */
1394                 libusb10_submit_transfer_sub(
1395                     uxfer->dev_handle, endpoint);
1396         } else {
1397                 /* not started */
1398                 retval = LIBUSB_ERROR_NOT_FOUND;
1399         }
1400
1401         CTX_UNLOCK(dev->ctx);
1402
1403         DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1404
1405         return (retval);
1406 }
1407
1408 UNEXPORTED void
1409 libusb10_cancel_all_transfer(libusb_device *dev)
1410 {
1411         /* TODO */
1412 }
1413
1414 uint16_t
1415 libusb_cpu_to_le16(uint16_t x)
1416 {
1417         return (htole16(x));
1418 }
1419
1420 uint16_t
1421 libusb_le16_to_cpu(uint16_t x)
1422 {
1423         return (le16toh(x));
1424 }
1425
1426 const char *
1427 libusb_strerror(int code)
1428 {
1429         /* TODO */
1430         return ("Unknown error");
1431 }