usb4bsd: Pull up libusb and usbcontrol to FreeBSD current's.
[dragonfly.git] / lib / libusb / libusb10.c
... / ...
CommitLineData
1/* $FreeBSD: src/lib/libusb/libusb10.c,v 1.25 2012/06/12 07:28:25 hselasky Exp $ */
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
48static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
49struct libusb_context *usbi_default_context = NULL;
50
51/* Prototypes */
52
53static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
54static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
55static int libusb10_convert_error(uint8_t status);
56static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
57static void libusb10_isoc_proxy(struct libusb20_transfer *);
58static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
59static void libusb10_ctrl_proxy(struct libusb20_transfer *);
60static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
61
62/* Library initialisation / deinitialisation */
63
64void
65libusb_set_debug(libusb_context *ctx, int level)
66{
67 ctx = GET_CONTEXT(ctx);
68 if (ctx)
69 ctx->debug = level;
70}
71
72static void
73libusb_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
91int
92libusb_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
166void
167libusb_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
193ssize_t
194libusb_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
264void
265libusb_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
279uint8_t
280libusb_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
287uint8_t
288libusb_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
295enum libusb_speed
296libusb_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
316int
317libusb_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
350out:
351 libusb_free_config_descriptor(pdconf);
352 return (ret);
353}
354
355int
356libusb_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
379libusb_device *
380libusb_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
392void
393libusb_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
408int
409libusb_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
446libusb_device_handle *
447libusb_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
488void
489libusb_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
518libusb_device *
519libusb_get_device(struct libusb20_device *pdev)
520{
521 if (pdev == NULL)
522 return (NULL);
523 return ((libusb_device *)pdev->privLuData);
524}
525
526int
527libusb_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
545int
546libusb_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
577set_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
591int
592libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
593{
594 libusb_device *dev;
595 int err = 0;
596
597 dev = libusb_get_device(pdev);
598 if (dev == NULL)
599 return (LIBUSB_ERROR_INVALID_PARAM);
600
601 if (interface_number < 0 || interface_number > 31)
602 return (LIBUSB_ERROR_INVALID_PARAM);
603
604 CTX_LOCK(dev->ctx);
605 if (dev->claimed_interfaces & (1 << interface_number))
606 err = LIBUSB_ERROR_BUSY;
607
608 if (!err)
609 dev->claimed_interfaces |= (1 << interface_number);
610 CTX_UNLOCK(dev->ctx);
611 return (err);
612}
613
614int
615libusb_release_interface(struct libusb20_device *pdev, int interface_number)
616{
617 libusb_device *dev;
618 int err = 0;
619
620 dev = libusb_get_device(pdev);
621 if (dev == NULL)
622 return (LIBUSB_ERROR_INVALID_PARAM);
623
624 if (interface_number < 0 || interface_number > 31)
625 return (LIBUSB_ERROR_INVALID_PARAM);
626
627 CTX_LOCK(dev->ctx);
628 if (!(dev->claimed_interfaces & (1 << interface_number)))
629 err = LIBUSB_ERROR_NOT_FOUND;
630
631 if (!err)
632 dev->claimed_interfaces &= ~(1 << interface_number);
633 CTX_UNLOCK(dev->ctx);
634 return (err);
635}
636
637int
638libusb_set_interface_alt_setting(struct libusb20_device *pdev,
639 int interface_number, int alternate_setting)
640{
641 libusb_device *dev;
642 int err = 0;
643
644 dev = libusb_get_device(pdev);
645 if (dev == NULL)
646 return (LIBUSB_ERROR_INVALID_PARAM);
647
648 if (interface_number < 0 || interface_number > 31)
649 return (LIBUSB_ERROR_INVALID_PARAM);
650
651 CTX_LOCK(dev->ctx);
652 if (!(dev->claimed_interfaces & (1 << interface_number)))
653 err = LIBUSB_ERROR_NOT_FOUND;
654 CTX_UNLOCK(dev->ctx);
655
656 if (err)
657 return (err);
658
659 libusb10_cancel_all_transfer(dev);
660
661 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
662
663 err = libusb20_dev_set_alt_index(pdev,
664 interface_number, alternate_setting);
665
666 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
667 pdev, libusb20_dev_get_fd(pdev),
668 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
669
670 return (err ? LIBUSB_ERROR_OTHER : 0);
671}
672
673static struct libusb20_transfer *
674libusb10_get_transfer(struct libusb20_device *pdev,
675 uint8_t endpoint, uint8_t xfer_index)
676{
677 xfer_index &= 1; /* double buffering */
678
679 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
680
681 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
682 /* this is an IN endpoint */
683 xfer_index |= 2;
684 }
685 return (libusb20_tr_get_pointer(pdev, xfer_index));
686}
687
688int
689libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
690{
691 struct libusb20_transfer *xfer;
692 struct libusb_device *dev;
693 int err;
694
695 xfer = libusb10_get_transfer(pdev, endpoint, 0);
696 if (xfer == NULL)
697 return (LIBUSB_ERROR_INVALID_PARAM);
698
699 dev = libusb_get_device(pdev);
700 if (dev == NULL)
701 return (LIBUSB_ERROR_INVALID_PARAM);
702
703 CTX_LOCK(dev->ctx);
704 err = libusb20_tr_open(xfer, 0, 1, endpoint);
705 CTX_UNLOCK(dev->ctx);
706
707 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
708 return (LIBUSB_ERROR_OTHER);
709
710 libusb20_tr_clear_stall_sync(xfer);
711
712 /* check if we opened the transfer */
713 if (err == 0) {
714 CTX_LOCK(dev->ctx);
715 libusb20_tr_close(xfer);
716 CTX_UNLOCK(dev->ctx);
717 }
718 return (0); /* success */
719}
720
721int
722libusb_reset_device(struct libusb20_device *pdev)
723{
724 libusb_device *dev;
725 int err;
726
727 dev = libusb_get_device(pdev);
728 if (dev == NULL)
729 return (LIBUSB_ERROR_INVALID_PARAM);
730
731 libusb10_cancel_all_transfer(dev);
732
733 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
734
735 err = libusb20_dev_reset(pdev);
736
737 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
738 pdev, libusb20_dev_get_fd(pdev),
739 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
740
741 return (err ? LIBUSB_ERROR_OTHER : 0);
742}
743
744int
745libusb_check_connected(struct libusb20_device *pdev)
746{
747 libusb_device *dev;
748 int err;
749
750 dev = libusb_get_device(pdev);
751 if (dev == NULL)
752 return (LIBUSB_ERROR_INVALID_PARAM);
753
754 err = libusb20_dev_check_connected(pdev);
755
756 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
757}
758
759int
760libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
761{
762 if (pdev == NULL)
763 return (LIBUSB_ERROR_INVALID_PARAM);
764
765 if (libusb20_dev_kernel_driver_active(pdev, interface))
766 return (0); /* no kernel driver is active */
767 else
768 return (1); /* kernel driver is active */
769}
770
771int
772libusb_get_driver_np(struct libusb20_device *pdev, int interface,
773 char *name, int namelen)
774{
775 return (libusb_get_driver(pdev, interface, name, namelen));
776}
777
778int
779libusb_get_driver(struct libusb20_device *pdev, int interface,
780 char *name, int namelen)
781{
782 char *ptr;
783 int err;
784
785 if (pdev == NULL)
786 return (LIBUSB_ERROR_INVALID_PARAM);
787 if (namelen < 1)
788 return (LIBUSB_ERROR_INVALID_PARAM);
789 if (namelen > 255)
790 namelen = 255;
791
792 err = libusb20_dev_get_iface_desc(
793 pdev, interface, name, namelen);
794
795 if (err != 0)
796 return (LIBUSB_ERROR_OTHER);
797
798 /* we only want the driver name */
799 ptr = strstr(name, ":");
800 if (ptr != NULL)
801 *ptr = 0;
802
803 return (0);
804}
805
806int
807libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
808{
809 return (libusb_detach_kernel_driver(pdev, interface));
810}
811
812int
813libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
814{
815 int err;
816
817 if (pdev == NULL)
818 return (LIBUSB_ERROR_INVALID_PARAM);
819
820 err = libusb20_dev_detach_kernel_driver(
821 pdev, interface);
822
823 return (err ? LIBUSB_ERROR_OTHER : 0);
824}
825
826int
827libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
828{
829 if (pdev == NULL)
830 return (LIBUSB_ERROR_INVALID_PARAM);
831 /* stub - currently not supported by libusb20 */
832 return (0);
833}
834
835/* Asynchronous device I/O */
836
837struct libusb_transfer *
838libusb_alloc_transfer(int iso_packets)
839{
840 struct libusb_transfer *uxfer;
841 struct libusb_super_transfer *sxfer;
842 int len;
843
844 len = sizeof(struct libusb_transfer) +
845 sizeof(struct libusb_super_transfer) +
846 (iso_packets * sizeof(libusb_iso_packet_descriptor));
847
848 sxfer = malloc(len);
849 if (sxfer == NULL)
850 return (NULL);
851
852 memset(sxfer, 0, len);
853
854 uxfer = (struct libusb_transfer *)(
855 ((uint8_t *)sxfer) + sizeof(*sxfer));
856
857 /* set default value */
858 uxfer->num_iso_packets = iso_packets;
859
860 return (uxfer);
861}
862
863void
864libusb_free_transfer(struct libusb_transfer *uxfer)
865{
866 struct libusb_super_transfer *sxfer;
867
868 if (uxfer == NULL)
869 return; /* be NULL safe */
870
871 /* check if we should free the transfer buffer */
872 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
873 free(uxfer->buffer);
874
875 sxfer = (struct libusb_super_transfer *)(
876 (uint8_t *)uxfer - sizeof(*sxfer));
877
878 free(sxfer);
879}
880
881static uint32_t
882libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
883{
884 uint32_t ret;
885
886 switch (xfer->type) {
887 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
888 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
889 break;
890 case LIBUSB_TRANSFER_TYPE_CONTROL:
891 ret = 2;
892 break;
893 default:
894 ret = 1;
895 break;
896 }
897 return (ret);
898}
899
900static int
901libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
902{
903 int ret;
904 int usb_speed;
905
906 usb_speed = libusb20_dev_get_speed(pdev);
907
908 switch (xfer->type) {
909 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
910 ret = 0; /* kernel will auto-select */
911 break;
912 case LIBUSB_TRANSFER_TYPE_CONTROL:
913 ret = 1024;
914 break;
915 default:
916 switch (usb_speed) {
917 case LIBUSB20_SPEED_LOW:
918 ret = 256;
919 break;
920 case LIBUSB20_SPEED_FULL:
921 ret = 4096;
922 break;
923 default:
924 ret = 16384;
925 break;
926 }
927 break;
928 }
929 return (ret);
930}
931
932static int
933libusb10_convert_error(uint8_t status)
934{
935 ; /* indent fix */
936
937 switch (status) {
938 case LIBUSB20_TRANSFER_START:
939 case LIBUSB20_TRANSFER_COMPLETED:
940 return (LIBUSB_TRANSFER_COMPLETED);
941 case LIBUSB20_TRANSFER_OVERFLOW:
942 return (LIBUSB_TRANSFER_OVERFLOW);
943 case LIBUSB20_TRANSFER_NO_DEVICE:
944 return (LIBUSB_TRANSFER_NO_DEVICE);
945 case LIBUSB20_TRANSFER_STALL:
946 return (LIBUSB_TRANSFER_STALL);
947 case LIBUSB20_TRANSFER_CANCELLED:
948 return (LIBUSB_TRANSFER_CANCELLED);
949 case LIBUSB20_TRANSFER_TIMED_OUT:
950 return (LIBUSB_TRANSFER_TIMED_OUT);
951 default:
952 return (LIBUSB_TRANSFER_ERROR);
953 }
954}
955
956/* This function must be called locked */
957
958static void
959libusb10_complete_transfer(struct libusb20_transfer *pxfer,
960 struct libusb_super_transfer *sxfer, int status)
961{
962 struct libusb_transfer *uxfer;
963 struct libusb_device *dev;
964
965 uxfer = (struct libusb_transfer *)(
966 ((uint8_t *)sxfer) + sizeof(*sxfer));
967
968 if (pxfer != NULL)
969 libusb20_tr_set_priv_sc1(pxfer, NULL);
970
971 /* set transfer status */
972 uxfer->status = status;
973
974 /* update super transfer state */
975 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
976
977 dev = libusb_get_device(uxfer->dev_handle);
978
979 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
980}
981
982/* This function must be called locked */
983
984static void
985libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
986{
987 struct libusb_super_transfer *sxfer;
988 struct libusb_transfer *uxfer;
989 uint32_t actlen;
990 uint16_t iso_packets;
991 uint16_t i;
992 uint8_t status;
993 uint8_t flags;
994
995 status = libusb20_tr_get_status(pxfer);
996 sxfer = libusb20_tr_get_priv_sc1(pxfer);
997 actlen = libusb20_tr_get_actual_length(pxfer);
998 iso_packets = libusb20_tr_get_max_frames(pxfer);
999
1000 if (sxfer == NULL)
1001 return; /* cancelled - nothing to do */
1002
1003 uxfer = (struct libusb_transfer *)(
1004 ((uint8_t *)sxfer) + sizeof(*sxfer));
1005
1006 if (iso_packets > uxfer->num_iso_packets)
1007 iso_packets = uxfer->num_iso_packets;
1008
1009 if (iso_packets == 0)
1010 return; /* nothing to do */
1011
1012 /* make sure that the number of ISOCHRONOUS packets is valid */
1013 uxfer->num_iso_packets = iso_packets;
1014
1015 flags = uxfer->flags;
1016
1017 switch (status) {
1018 case LIBUSB20_TRANSFER_COMPLETED:
1019
1020 /* update actual length */
1021 uxfer->actual_length = actlen;
1022 for (i = 0; i != iso_packets; i++) {
1023 uxfer->iso_packet_desc[i].actual_length =
1024 libusb20_tr_get_length(pxfer, i);
1025 }
1026 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1027 break;
1028
1029 case LIBUSB20_TRANSFER_START:
1030
1031 /* setup length(s) */
1032 actlen = 0;
1033 for (i = 0; i != iso_packets; i++) {
1034 libusb20_tr_setup_isoc(pxfer,
1035 &uxfer->buffer[actlen],
1036 uxfer->iso_packet_desc[i].length, i);
1037 actlen += uxfer->iso_packet_desc[i].length;
1038 }
1039
1040 /* no remainder */
1041 sxfer->rem_len = 0;
1042
1043 libusb20_tr_set_total_frames(pxfer, iso_packets);
1044 libusb20_tr_submit(pxfer);
1045
1046 /* fork another USB transfer, if any */
1047 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1048 break;
1049
1050 default:
1051 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1052 break;
1053 }
1054}
1055
1056/* This function must be called locked */
1057
1058static void
1059libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1060{
1061 struct libusb_super_transfer *sxfer;
1062 struct libusb_transfer *uxfer;
1063 uint32_t max_bulk;
1064 uint32_t actlen;
1065 uint8_t status;
1066 uint8_t flags;
1067
1068 status = libusb20_tr_get_status(pxfer);
1069 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1070 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1071 actlen = libusb20_tr_get_actual_length(pxfer);
1072
1073 if (sxfer == NULL)
1074 return; /* cancelled - nothing to do */
1075
1076 uxfer = (struct libusb_transfer *)(
1077 ((uint8_t *)sxfer) + sizeof(*sxfer));
1078
1079 flags = uxfer->flags;
1080
1081 switch (status) {
1082 case LIBUSB20_TRANSFER_COMPLETED:
1083
1084 uxfer->actual_length += actlen;
1085
1086 /* check for short packet */
1087 if (sxfer->last_len != actlen) {
1088 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1089 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1090 } else {
1091 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1092 }
1093 break;
1094 }
1095 /* check for end of data */
1096 if (sxfer->rem_len == 0) {
1097 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1098 break;
1099 }
1100 /* FALLTHROUGH */
1101
1102 case LIBUSB20_TRANSFER_START:
1103 if (max_bulk > sxfer->rem_len) {
1104 max_bulk = sxfer->rem_len;
1105 }
1106 /* setup new BULK or INTERRUPT transaction */
1107 libusb20_tr_setup_bulk(pxfer,
1108 sxfer->curr_data, max_bulk, uxfer->timeout);
1109
1110 /* update counters */
1111 sxfer->last_len = max_bulk;
1112 sxfer->curr_data += max_bulk;
1113 sxfer->rem_len -= max_bulk;
1114
1115 libusb20_tr_submit(pxfer);
1116
1117 /* check if we can fork another USB transfer */
1118 if (sxfer->rem_len == 0)
1119 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1120 break;
1121
1122 default:
1123 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1124 break;
1125 }
1126}
1127
1128/* This function must be called locked */
1129
1130static void
1131libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1132{
1133 struct libusb_super_transfer *sxfer;
1134 struct libusb_transfer *uxfer;
1135 uint32_t max_bulk;
1136 uint32_t actlen;
1137 uint8_t status;
1138 uint8_t flags;
1139
1140 status = libusb20_tr_get_status(pxfer);
1141 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1142 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1143 actlen = libusb20_tr_get_actual_length(pxfer);
1144
1145 if (sxfer == NULL)
1146 return; /* cancelled - nothing to do */
1147
1148 uxfer = (struct libusb_transfer *)(
1149 ((uint8_t *)sxfer) + sizeof(*sxfer));
1150
1151 flags = uxfer->flags;
1152
1153 switch (status) {
1154 case LIBUSB20_TRANSFER_COMPLETED:
1155
1156 uxfer->actual_length += actlen;
1157
1158 /* subtract length of SETUP packet, if any */
1159 actlen -= libusb20_tr_get_length(pxfer, 0);
1160
1161 /* check for short packet */
1162 if (sxfer->last_len != actlen) {
1163 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1164 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1165 } else {
1166 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1167 }
1168 break;
1169 }
1170 /* check for end of data */
1171 if (sxfer->rem_len == 0) {
1172 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1173 break;
1174 }
1175 /* FALLTHROUGH */
1176
1177 case LIBUSB20_TRANSFER_START:
1178 if (max_bulk > sxfer->rem_len) {
1179 max_bulk = sxfer->rem_len;
1180 }
1181 /* setup new CONTROL transaction */
1182 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1183 /* next fragment - don't send SETUP packet */
1184 libusb20_tr_set_length(pxfer, 0, 0);
1185 } else {
1186 /* first fragment - send SETUP packet */
1187 libusb20_tr_set_length(pxfer, 8, 0);
1188 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1189 }
1190
1191 if (max_bulk != 0) {
1192 libusb20_tr_set_length(pxfer, max_bulk, 1);
1193 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1194 libusb20_tr_set_total_frames(pxfer, 2);
1195 } else {
1196 libusb20_tr_set_total_frames(pxfer, 1);
1197 }
1198
1199 /* update counters */
1200 sxfer->last_len = max_bulk;
1201 sxfer->curr_data += max_bulk;
1202 sxfer->rem_len -= max_bulk;
1203
1204 libusb20_tr_submit(pxfer);
1205
1206 /* check if we can fork another USB transfer */
1207 if (sxfer->rem_len == 0)
1208 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1209 break;
1210
1211 default:
1212 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1213 break;
1214 }
1215}
1216
1217/* The following function must be called locked */
1218
1219static void
1220libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1221{
1222 struct libusb20_transfer *pxfer0;
1223 struct libusb20_transfer *pxfer1;
1224 struct libusb_super_transfer *sxfer;
1225 struct libusb_transfer *uxfer;
1226 struct libusb_device *dev;
1227 int err;
1228 int buffsize;
1229 int maxframe;
1230 int temp;
1231 uint8_t dummy;
1232
1233 dev = libusb_get_device(pdev);
1234
1235 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1236 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1237
1238 if (pxfer0 == NULL || pxfer1 == NULL)
1239 return; /* shouldn't happen */
1240
1241 temp = 0;
1242 if (libusb20_tr_pending(pxfer0))
1243 temp |= 1;
1244 if (libusb20_tr_pending(pxfer1))
1245 temp |= 2;
1246
1247 switch (temp) {
1248 case 3:
1249 /* wait till one of the transfers complete */
1250 return;
1251 case 2:
1252 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1253 if (sxfer == NULL)
1254 return; /* cancelling */
1255 if (sxfer->rem_len)
1256 return; /* cannot queue another one */
1257 /* swap transfers */
1258 pxfer1 = pxfer0;
1259 break;
1260 case 1:
1261 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1262 if (sxfer == NULL)
1263 return; /* cancelling */
1264 if (sxfer->rem_len)
1265 return; /* cannot queue another one */
1266 /* swap transfers */
1267 pxfer0 = pxfer1;
1268 break;
1269 default:
1270 break;
1271 }
1272
1273 /* find next transfer on same endpoint */
1274 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1275
1276 uxfer = (struct libusb_transfer *)(
1277 ((uint8_t *)sxfer) + sizeof(*sxfer));
1278
1279 if (uxfer->endpoint == endpoint) {
1280 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1281 sxfer->entry.tqe_prev = NULL;
1282 goto found;
1283 }
1284 }
1285 return; /* success */
1286
1287found:
1288
1289 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1290 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1291
1292 /* reset super transfer state */
1293 sxfer->rem_len = uxfer->length;
1294 sxfer->curr_data = uxfer->buffer;
1295 uxfer->actual_length = 0;
1296
1297 switch (uxfer->type) {
1298 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1299 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1300 break;
1301 case LIBUSB_TRANSFER_TYPE_BULK:
1302 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1303 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1304 break;
1305 case LIBUSB_TRANSFER_TYPE_CONTROL:
1306 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1307 if (sxfer->rem_len < 8)
1308 goto failure;
1309
1310 /* remove SETUP packet from data */
1311 sxfer->rem_len -= 8;
1312 sxfer->curr_data += 8;
1313 break;
1314 default:
1315 goto failure;
1316 }
1317
1318 buffsize = libusb10_get_buffsize(pdev, uxfer);
1319 maxframe = libusb10_get_maxframe(pdev, uxfer);
1320
1321 /* make sure the transfer is opened */
1322 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1323 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1324 goto failure;
1325 }
1326 libusb20_tr_start(pxfer0);
1327 return;
1328
1329failure:
1330 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1331
1332 /* make sure our event loop spins the done handler */
1333 dummy = 0;
1334 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1335}
1336
1337/* The following function must be called unlocked */
1338
1339int
1340libusb_submit_transfer(struct libusb_transfer *uxfer)
1341{
1342 struct libusb20_transfer *pxfer0;
1343 struct libusb20_transfer *pxfer1;
1344 struct libusb_super_transfer *sxfer;
1345 struct libusb_device *dev;
1346 uint8_t endpoint;
1347 int err;
1348
1349 if (uxfer == NULL)
1350 return (LIBUSB_ERROR_INVALID_PARAM);
1351
1352 if (uxfer->dev_handle == NULL)
1353 return (LIBUSB_ERROR_INVALID_PARAM);
1354
1355 endpoint = uxfer->endpoint;
1356
1357 dev = libusb_get_device(uxfer->dev_handle);
1358
1359 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1360
1361 sxfer = (struct libusb_super_transfer *)(
1362 (uint8_t *)uxfer - sizeof(*sxfer));
1363
1364 CTX_LOCK(dev->ctx);
1365
1366 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1367 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1368
1369 if (pxfer0 == NULL || pxfer1 == NULL) {
1370 err = LIBUSB_ERROR_OTHER;
1371 } else if ((sxfer->entry.tqe_prev != NULL) ||
1372 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1373 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1374 err = LIBUSB_ERROR_BUSY;
1375 } else {
1376
1377 /* set pending state */
1378 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1379
1380 /* insert transfer into transfer head list */
1381 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1382
1383 /* start work transfers */
1384 libusb10_submit_transfer_sub(
1385 uxfer->dev_handle, endpoint);
1386
1387 err = 0; /* success */
1388 }
1389
1390 CTX_UNLOCK(dev->ctx);
1391
1392 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1393
1394 return (err);
1395}
1396
1397/* Asynchronous transfer cancel */
1398
1399int
1400libusb_cancel_transfer(struct libusb_transfer *uxfer)
1401{
1402 struct libusb20_transfer *pxfer0;
1403 struct libusb20_transfer *pxfer1;
1404 struct libusb_super_transfer *sxfer;
1405 struct libusb_device *dev;
1406 uint8_t endpoint;
1407 int retval;
1408
1409 if (uxfer == NULL)
1410 return (LIBUSB_ERROR_INVALID_PARAM);
1411
1412 /* check if not initialised */
1413 if (uxfer->dev_handle == NULL)
1414 return (LIBUSB_ERROR_NOT_FOUND);
1415
1416 endpoint = uxfer->endpoint;
1417
1418 dev = libusb_get_device(uxfer->dev_handle);
1419
1420 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1421
1422 sxfer = (struct libusb_super_transfer *)(
1423 (uint8_t *)uxfer - sizeof(*sxfer));
1424
1425 retval = 0;
1426
1427 CTX_LOCK(dev->ctx);
1428
1429 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1430 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1431
1432 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1433 /* only update the transfer status */
1434 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1435 retval = LIBUSB_ERROR_NOT_FOUND;
1436 } else if (sxfer->entry.tqe_prev != NULL) {
1437 /* we are lucky - transfer is on a queue */
1438 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1439 sxfer->entry.tqe_prev = NULL;
1440 libusb10_complete_transfer(NULL,
1441 sxfer, LIBUSB_TRANSFER_CANCELLED);
1442 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1443 /* not started */
1444 retval = LIBUSB_ERROR_NOT_FOUND;
1445 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1446 libusb10_complete_transfer(pxfer0,
1447 sxfer, LIBUSB_TRANSFER_CANCELLED);
1448 libusb20_tr_stop(pxfer0);
1449 /* make sure the queue doesn't stall */
1450 libusb10_submit_transfer_sub(
1451 uxfer->dev_handle, endpoint);
1452 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1453 libusb10_complete_transfer(pxfer1,
1454 sxfer, LIBUSB_TRANSFER_CANCELLED);
1455 libusb20_tr_stop(pxfer1);
1456 /* make sure the queue doesn't stall */
1457 libusb10_submit_transfer_sub(
1458 uxfer->dev_handle, endpoint);
1459 } else {
1460 /* not started */
1461 retval = LIBUSB_ERROR_NOT_FOUND;
1462 }
1463
1464 CTX_UNLOCK(dev->ctx);
1465
1466 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1467
1468 return (retval);
1469}
1470
1471UNEXPORTED void
1472libusb10_cancel_all_transfer(libusb_device *dev)
1473{
1474 /* TODO */
1475}
1476
1477uint16_t
1478libusb_cpu_to_le16(uint16_t x)
1479{
1480 return (htole16(x));
1481}
1482
1483uint16_t
1484libusb_le16_to_cpu(uint16_t x)
1485{
1486 return (le16toh(x));
1487}
1488
1489const char *
1490libusb_strerror(int code)
1491{
1492 switch (code) {
1493 case LIBUSB_SUCCESS:
1494 return ("Success");
1495 case LIBUSB_ERROR_IO:
1496 return ("I/O error");
1497 case LIBUSB_ERROR_INVALID_PARAM:
1498 return ("Invalid parameter");
1499 case LIBUSB_ERROR_ACCESS:
1500 return ("Permissions error");
1501 case LIBUSB_ERROR_NO_DEVICE:
1502 return ("No device");
1503 case LIBUSB_ERROR_NOT_FOUND:
1504 return ("Not found");
1505 case LIBUSB_ERROR_BUSY:
1506 return ("Device busy");
1507 case LIBUSB_ERROR_TIMEOUT:
1508 return ("Timeout");
1509 case LIBUSB_ERROR_OVERFLOW:
1510 return ("Overflow");
1511 case LIBUSB_ERROR_PIPE:
1512 return ("Pipe error");
1513 case LIBUSB_ERROR_INTERRUPTED:
1514 return ("Interrupted");
1515 case LIBUSB_ERROR_NO_MEM:
1516 return ("Out of memory");
1517 case LIBUSB_ERROR_NOT_SUPPORTED:
1518 return ("Not supported");
1519 case LIBUSB_ERROR_OTHER:
1520 return ("Other error");
1521 default:
1522 return ("Unknown error");
1523 }
1524}
1525
1526const char *
1527libusb_error_name(int code)
1528{
1529 switch (code) {
1530 case LIBUSB_SUCCESS:
1531 return ("LIBUSB_SUCCESS");
1532 case LIBUSB_ERROR_IO:
1533 return ("LIBUSB_ERROR_IO");
1534 case LIBUSB_ERROR_INVALID_PARAM:
1535 return ("LIBUSB_ERROR_INVALID_PARAM");
1536 case LIBUSB_ERROR_ACCESS:
1537 return ("LIBUSB_ERROR_ACCESS");
1538 case LIBUSB_ERROR_NO_DEVICE:
1539 return ("LIBUSB_ERROR_NO_DEVICE");
1540 case LIBUSB_ERROR_NOT_FOUND:
1541 return ("LIBUSB_ERROR_NOT_FOUND");
1542 case LIBUSB_ERROR_BUSY:
1543 return ("LIBUSB_ERROR_BUSY");
1544 case LIBUSB_ERROR_TIMEOUT:
1545 return ("LIBUSB_ERROR_TIMEOUT");
1546 case LIBUSB_ERROR_OVERFLOW:
1547 return ("LIBUSB_ERROR_OVERFLOW");
1548 case LIBUSB_ERROR_PIPE:
1549 return ("LIBUSB_ERROR_PIPE");
1550 case LIBUSB_ERROR_INTERRUPTED:
1551 return ("LIBUSB_ERROR_INTERRUPTED");
1552 case LIBUSB_ERROR_NO_MEM:
1553 return ("LIBUSB_ERROR_NO_MEM");
1554 case LIBUSB_ERROR_NOT_SUPPORTED:
1555 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1556 case LIBUSB_ERROR_OTHER:
1557 return ("LIBUSB_ERROR_OTHER");
1558 default:
1559 return ("LIBUSB_ERROR_UNKNOWN");
1560 }
1561}