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