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