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