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