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