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