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