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