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