1 /* $FreeBSD: stable/9/lib/libusb/libusb20.c 348896 2019-06-11 08:54:21Z hselasky $ */
2 /*-
3 * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/queue.h>
28
29 #include <ctype.h>
30 #include <poll.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "libusb20.h"
36 #include "libusb20_desc.h"
37 #include "libusb20_int.h"
38
39 static int
dummy_int(void)40 dummy_int(void)
41 {
42 return (LIBUSB20_ERROR_NOT_SUPPORTED);
43 }
44
45 static void
dummy_void(void)46 dummy_void(void)
47 {
48 return;
49 }
50
51 static void
dummy_callback(struct libusb20_transfer * xfer)52 dummy_callback(struct libusb20_transfer *xfer)
53 {
54 ; /* style fix */
55 switch (libusb20_tr_get_status(xfer)) {
56 case LIBUSB20_TRANSFER_START:
57 libusb20_tr_submit(xfer);
58 break;
59 default:
60 /* complete or error */
61 break;
62 }
63 return;
64 }
65
66 #define dummy_get_config_desc_full (void *)dummy_int
67 #define dummy_get_config_index (void *)dummy_int
68 #define dummy_set_config_index (void *)dummy_int
69 #define dummy_set_alt_index (void *)dummy_int
70 #define dummy_reset_device (void *)dummy_int
71 #define dummy_check_connected (void *)dummy_int
72 #define dummy_set_power_mode (void *)dummy_int
73 #define dummy_get_power_mode (void *)dummy_int
74 #define dummy_get_port_path (void *)dummy_int
75 #define dummy_get_power_usage (void *)dummy_int
76 #define dummy_kernel_driver_active (void *)dummy_int
77 #define dummy_detach_kernel_driver (void *)dummy_int
78 #define dummy_do_request_sync (void *)dummy_int
79 #define dummy_tr_open (void *)dummy_int
80 #define dummy_tr_close (void *)dummy_int
81 #define dummy_tr_clear_stall_sync (void *)dummy_int
82 #define dummy_process (void *)dummy_int
83 #define dummy_dev_info (void *)dummy_int
84 #define dummy_dev_get_iface_driver (void *)dummy_int
85
86 #define dummy_tr_submit (void *)dummy_void
87 #define dummy_tr_cancel_async (void *)dummy_void
88
89 static const struct libusb20_device_methods libusb20_dummy_methods = {
90 LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
91 };
92
93 void
libusb20_tr_callback_wrapper(struct libusb20_transfer * xfer)94 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
95 {
96 ; /* style fix */
97
98 repeat:
99
100 if (!xfer->is_pending) {
101 xfer->status = LIBUSB20_TRANSFER_START;
102 } else {
103 xfer->is_pending = 0;
104 }
105
106 xfer->callback(xfer);
107
108 if (xfer->is_restart) {
109 xfer->is_restart = 0;
110 goto repeat;
111 }
112 if (xfer->is_draining &&
113 (!xfer->is_pending)) {
114 xfer->is_draining = 0;
115 xfer->status = LIBUSB20_TRANSFER_DRAINED;
116 xfer->callback(xfer);
117 }
118 return;
119 }
120
121 int
libusb20_tr_close(struct libusb20_transfer * xfer)122 libusb20_tr_close(struct libusb20_transfer *xfer)
123 {
124 int error;
125
126 if (!xfer->is_opened) {
127 return (LIBUSB20_ERROR_OTHER);
128 }
129 error = xfer->pdev->methods->tr_close(xfer);
130
131 if (xfer->pLength) {
132 free(xfer->pLength);
133 }
134 if (xfer->ppBuffer) {
135 free(xfer->ppBuffer);
136 }
137 /* reset variable fields in case the transfer is opened again */
138 xfer->priv_sc0 = 0;
139 xfer->priv_sc1 = 0;
140 xfer->is_opened = 0;
141 xfer->is_pending = 0;
142 xfer->is_cancel = 0;
143 xfer->is_draining = 0;
144 xfer->is_restart = 0;
145 xfer->status = 0;
146 xfer->flags = 0;
147 xfer->nFrames = 0;
148 xfer->aFrames = 0;
149 xfer->timeout = 0;
150 xfer->maxFrames = 0;
151 xfer->maxTotalLength = 0;
152 xfer->maxPacketLen = 0;
153 return (error);
154 }
155
156 int
libusb20_tr_open(struct libusb20_transfer * xfer,uint32_t MaxBufSize,uint32_t MaxFrameCount,uint8_t ep_no)157 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
158 uint32_t MaxFrameCount, uint8_t ep_no)
159 {
160 uint32_t size;
161 uint8_t pre_scale;
162 int error;
163
164 if (xfer->is_opened)
165 return (LIBUSB20_ERROR_BUSY);
166 if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
167 MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
168 /*
169 * The kernel can setup 8 times more frames when
170 * pre-scaling ISOCHRONOUS transfers. Make sure the
171 * length and pointer buffers are big enough:
172 */
173 MaxFrameCount *= 8;
174 pre_scale = 1;
175 } else {
176 pre_scale = 0;
177 }
178 if (MaxFrameCount == 0)
179 return (LIBUSB20_ERROR_INVALID_PARAM);
180
181 xfer->maxFrames = MaxFrameCount;
182
183 size = MaxFrameCount * sizeof(xfer->pLength[0]);
184 xfer->pLength = malloc(size);
185 if (xfer->pLength == NULL) {
186 return (LIBUSB20_ERROR_NO_MEM);
187 }
188 memset(xfer->pLength, 0, size);
189
190 size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
191 xfer->ppBuffer = malloc(size);
192 if (xfer->ppBuffer == NULL) {
193 free(xfer->pLength);
194 return (LIBUSB20_ERROR_NO_MEM);
195 }
196 memset(xfer->ppBuffer, 0, size);
197
198 if (pre_scale) {
199 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
200 MaxFrameCount / 8, ep_no, 1);
201 } else {
202 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
203 MaxFrameCount, ep_no, 0);
204 }
205
206 if (error) {
207 free(xfer->ppBuffer);
208 free(xfer->pLength);
209 } else {
210 xfer->is_opened = 1;
211 }
212 return (error);
213 }
214
215 struct libusb20_transfer *
libusb20_tr_get_pointer(struct libusb20_device * pdev,uint16_t trIndex)216 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
217 {
218 if (trIndex >= pdev->nTransfer) {
219 return (NULL);
220 }
221 return (pdev->pTransfer + trIndex);
222 }
223
224 uint32_t
libusb20_tr_get_actual_frames(struct libusb20_transfer * xfer)225 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
226 {
227 return (xfer->aFrames);
228 }
229
230 uint16_t
libusb20_tr_get_time_complete(struct libusb20_transfer * xfer)231 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
232 {
233 return (xfer->timeComplete);
234 }
235
236 uint32_t
libusb20_tr_get_actual_length(struct libusb20_transfer * xfer)237 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
238 {
239 uint32_t x;
240 uint32_t actlen = 0;
241
242 for (x = 0; x != xfer->aFrames; x++) {
243 actlen += xfer->pLength[x];
244 }
245 return (actlen);
246 }
247
248 uint32_t
libusb20_tr_get_max_frames(struct libusb20_transfer * xfer)249 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
250 {
251 return (xfer->maxFrames);
252 }
253
254 uint32_t
libusb20_tr_get_max_packet_length(struct libusb20_transfer * xfer)255 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
256 {
257 /*
258 * Special Case NOTE: If the packet multiplier is non-zero for
259 * High Speed USB, the value returned is equal to
260 * "wMaxPacketSize * multiplier" !
261 */
262 return (xfer->maxPacketLen);
263 }
264
265 uint32_t
libusb20_tr_get_max_total_length(struct libusb20_transfer * xfer)266 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
267 {
268 return (xfer->maxTotalLength);
269 }
270
271 uint8_t
libusb20_tr_get_status(struct libusb20_transfer * xfer)272 libusb20_tr_get_status(struct libusb20_transfer *xfer)
273 {
274 return (xfer->status);
275 }
276
277 uint8_t
libusb20_tr_pending(struct libusb20_transfer * xfer)278 libusb20_tr_pending(struct libusb20_transfer *xfer)
279 {
280 return (xfer->is_pending);
281 }
282
283 void *
libusb20_tr_get_priv_sc0(struct libusb20_transfer * xfer)284 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
285 {
286 return (xfer->priv_sc0);
287 }
288
289 void *
libusb20_tr_get_priv_sc1(struct libusb20_transfer * xfer)290 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
291 {
292 return (xfer->priv_sc1);
293 }
294
295 void
libusb20_tr_stop(struct libusb20_transfer * xfer)296 libusb20_tr_stop(struct libusb20_transfer *xfer)
297 {
298 if (!xfer->is_opened) {
299 /* transfer is not opened */
300 return;
301 }
302 if (!xfer->is_pending) {
303 /* transfer not pending */
304 return;
305 }
306 if (xfer->is_cancel) {
307 /* already cancelling */
308 return;
309 }
310 xfer->is_cancel = 1; /* we are cancelling */
311
312 xfer->pdev->methods->tr_cancel_async(xfer);
313 return;
314 }
315
316 void
libusb20_tr_drain(struct libusb20_transfer * xfer)317 libusb20_tr_drain(struct libusb20_transfer *xfer)
318 {
319 if (!xfer->is_opened) {
320 /* transfer is not opened */
321 return;
322 }
323 /* make sure that we are cancelling */
324 libusb20_tr_stop(xfer);
325
326 if (xfer->is_pending) {
327 xfer->is_draining = 1;
328 }
329 return;
330 }
331
332 void
libusb20_tr_clear_stall_sync(struct libusb20_transfer * xfer)333 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
334 {
335 xfer->pdev->methods->tr_clear_stall_sync(xfer);
336 return;
337 }
338
339 void
libusb20_tr_set_buffer(struct libusb20_transfer * xfer,void * buffer,uint16_t frIndex)340 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
341 {
342 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(buffer);
343 return;
344 }
345
346 void
libusb20_tr_set_callback(struct libusb20_transfer * xfer,libusb20_tr_callback_t * cb)347 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
348 {
349 xfer->callback = cb;
350 return;
351 }
352
353 void
libusb20_tr_set_flags(struct libusb20_transfer * xfer,uint8_t flags)354 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
355 {
356 xfer->flags = flags;
357 return;
358 }
359
360 uint32_t
libusb20_tr_get_length(struct libusb20_transfer * xfer,uint16_t frIndex)361 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
362 {
363 return (xfer->pLength[frIndex]);
364 }
365
366 void
libusb20_tr_set_length(struct libusb20_transfer * xfer,uint32_t length,uint16_t frIndex)367 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
368 {
369 xfer->pLength[frIndex] = length;
370 return;
371 }
372
373 void
libusb20_tr_set_priv_sc0(struct libusb20_transfer * xfer,void * sc0)374 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
375 {
376 xfer->priv_sc0 = sc0;
377 return;
378 }
379
380 void
libusb20_tr_set_priv_sc1(struct libusb20_transfer * xfer,void * sc1)381 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
382 {
383 xfer->priv_sc1 = sc1;
384 return;
385 }
386
387 void
libusb20_tr_set_timeout(struct libusb20_transfer * xfer,uint32_t timeout)388 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
389 {
390 xfer->timeout = timeout;
391 return;
392 }
393
394 void
libusb20_tr_set_total_frames(struct libusb20_transfer * xfer,uint32_t nFrames)395 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
396 {
397 if (nFrames > xfer->maxFrames) {
398 /* should not happen */
399 nFrames = xfer->maxFrames;
400 }
401 xfer->nFrames = nFrames;
402 return;
403 }
404
405 void
libusb20_tr_setup_bulk(struct libusb20_transfer * xfer,void * pBuf,uint32_t length,uint32_t timeout)406 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
407 {
408 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
409 xfer->pLength[0] = length;
410 xfer->timeout = timeout;
411 xfer->nFrames = 1;
412 return;
413 }
414
415 void
libusb20_tr_setup_control(struct libusb20_transfer * xfer,void * psetup,void * pBuf,uint32_t timeout)416 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
417 {
418 uint16_t len;
419
420 xfer->ppBuffer[0] = libusb20_pass_ptr(psetup);
421 xfer->pLength[0] = 8; /* fixed */
422 xfer->timeout = timeout;
423
424 len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
425
426 if (len != 0) {
427 xfer->nFrames = 2;
428 xfer->ppBuffer[1] = libusb20_pass_ptr(pBuf);
429 xfer->pLength[1] = len;
430 } else {
431 xfer->nFrames = 1;
432 }
433 return;
434 }
435
436 void
libusb20_tr_setup_intr(struct libusb20_transfer * xfer,void * pBuf,uint32_t length,uint32_t timeout)437 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
438 {
439 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
440 xfer->pLength[0] = length;
441 xfer->timeout = timeout;
442 xfer->nFrames = 1;
443 return;
444 }
445
446 void
libusb20_tr_setup_isoc(struct libusb20_transfer * xfer,void * pBuf,uint32_t length,uint16_t frIndex)447 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
448 {
449 if (frIndex >= xfer->maxFrames) {
450 /* should not happen */
451 return;
452 }
453 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(pBuf);
454 xfer->pLength[frIndex] = length;
455 return;
456 }
457
458 uint8_t
libusb20_tr_bulk_intr_sync(struct libusb20_transfer * xfer,void * pbuf,uint32_t length,uint32_t * pactlen,uint32_t timeout)459 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
460 void *pbuf, uint32_t length, uint32_t *pactlen,
461 uint32_t timeout)
462 {
463 struct libusb20_device *pdev = xfer->pdev;
464 uint32_t transfer_max;
465 uint32_t transfer_act;
466 uint8_t retval;
467
468 /* set some sensible default value */
469 if (pactlen != NULL)
470 *pactlen = 0;
471
472 /* check for error condition */
473 if (libusb20_tr_pending(xfer))
474 return (LIBUSB20_ERROR_OTHER);
475
476 do {
477 /* compute maximum transfer length */
478 transfer_max =
479 libusb20_tr_get_max_total_length(xfer);
480
481 if (transfer_max > length)
482 transfer_max = length;
483
484 /* setup bulk or interrupt transfer */
485 libusb20_tr_setup_bulk(xfer, pbuf,
486 transfer_max, timeout);
487
488 /* start the transfer */
489 libusb20_tr_start(xfer);
490
491 /* wait for transfer completion */
492 while (libusb20_dev_process(pdev) == 0) {
493
494 if (libusb20_tr_pending(xfer) == 0)
495 break;
496
497 libusb20_dev_wait_process(pdev, -1);
498 }
499
500 transfer_act = libusb20_tr_get_actual_length(xfer);
501
502 /* update actual length, if any */
503 if (pactlen != NULL)
504 pactlen[0] += transfer_act;
505
506 /* check transfer status */
507 retval = libusb20_tr_get_status(xfer);
508 if (retval)
509 break;
510
511 /* check for short transfer */
512 if (transfer_act != transfer_max)
513 break;
514
515 /* update buffer pointer and length */
516 pbuf = ((uint8_t *)pbuf) + transfer_max;
517 length = length - transfer_max;
518
519 } while (length != 0);
520
521 return (retval);
522 }
523
524 void
libusb20_tr_submit(struct libusb20_transfer * xfer)525 libusb20_tr_submit(struct libusb20_transfer *xfer)
526 {
527 if (!xfer->is_opened) {
528 /* transfer is not opened */
529 return;
530 }
531 if (xfer->is_pending) {
532 /* should not happen */
533 return;
534 }
535 xfer->is_pending = 1; /* we are pending */
536 xfer->is_cancel = 0; /* not cancelling */
537 xfer->is_restart = 0; /* not restarting */
538
539 xfer->pdev->methods->tr_submit(xfer);
540 return;
541 }
542
543 void
libusb20_tr_start(struct libusb20_transfer * xfer)544 libusb20_tr_start(struct libusb20_transfer *xfer)
545 {
546 if (!xfer->is_opened) {
547 /* transfer is not opened */
548 return;
549 }
550 if (xfer->is_pending) {
551 if (xfer->is_cancel) {
552 /* cancelling - restart */
553 xfer->is_restart = 1;
554 }
555 /* transfer not pending */
556 return;
557 }
558 /* get into the callback */
559 libusb20_tr_callback_wrapper(xfer);
560 return;
561 }
562
563 /* USB device operations */
564
565 int
libusb20_dev_close(struct libusb20_device * pdev)566 libusb20_dev_close(struct libusb20_device *pdev)
567 {
568 struct libusb20_transfer *xfer;
569 uint16_t x;
570 int error = 0;
571
572 if (!pdev->is_opened) {
573 return (LIBUSB20_ERROR_OTHER);
574 }
575 for (x = 0; x != pdev->nTransfer; x++) {
576 xfer = pdev->pTransfer + x;
577
578 if (!xfer->is_opened) {
579 /* transfer is not opened */
580 continue;
581 }
582
583 libusb20_tr_drain(xfer);
584
585 libusb20_tr_close(xfer);
586 }
587
588 if (pdev->pTransfer != NULL) {
589 free(pdev->pTransfer);
590 pdev->pTransfer = NULL;
591 }
592 error = pdev->beMethods->close_device(pdev);
593
594 pdev->methods = &libusb20_dummy_methods;
595
596 pdev->is_opened = 0;
597
598 /*
599 * The following variable is only used by the libusb v0.1
600 * compat layer:
601 */
602 pdev->claimed_interface = 0;
603
604 /*
605 * The following variable is only used by the libusb v1.0
606 * compat layer:
607 */
608 pdev->auto_detach = 0;
609
610 return (error);
611 }
612
613 int
libusb20_dev_detach_kernel_driver(struct libusb20_device * pdev,uint8_t ifaceIndex)614 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
615 {
616 int error;
617
618 error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
619 return (error);
620 }
621
622 struct LIBUSB20_DEVICE_DESC_DECODED *
libusb20_dev_get_device_desc(struct libusb20_device * pdev)623 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
624 {
625 return (&(pdev->ddesc));
626 }
627
628 int
libusb20_dev_get_fd(struct libusb20_device * pdev)629 libusb20_dev_get_fd(struct libusb20_device *pdev)
630 {
631 return (pdev->file);
632 }
633
634 int
libusb20_dev_kernel_driver_active(struct libusb20_device * pdev,uint8_t ifaceIndex)635 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
636 {
637 int error;
638
639 error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
640 return (error);
641 }
642
643 int
libusb20_dev_open(struct libusb20_device * pdev,uint16_t nTransferMax)644 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
645 {
646 struct libusb20_transfer *xfer;
647 uint32_t size;
648 uint16_t x;
649 int error;
650
651 if (pdev->is_opened) {
652 return (LIBUSB20_ERROR_BUSY);
653 }
654 if (nTransferMax >= 256) {
655 return (LIBUSB20_ERROR_INVALID_PARAM);
656 } else if (nTransferMax != 0) {
657 size = sizeof(pdev->pTransfer[0]) * nTransferMax;
658 pdev->pTransfer = malloc(size);
659 if (pdev->pTransfer == NULL) {
660 return (LIBUSB20_ERROR_NO_MEM);
661 }
662 memset(pdev->pTransfer, 0, size);
663 }
664 /* initialise all transfers */
665 for (x = 0; x != nTransferMax; x++) {
666
667 xfer = pdev->pTransfer + x;
668
669 xfer->pdev = pdev;
670 xfer->trIndex = x;
671 xfer->callback = &dummy_callback;
672 }
673
674 /* set "nTransfer" early */
675 pdev->nTransfer = nTransferMax;
676
677 error = pdev->beMethods->open_device(pdev, nTransferMax);
678
679 if (error) {
680 if (pdev->pTransfer != NULL) {
681 free(pdev->pTransfer);
682 pdev->pTransfer = NULL;
683 }
684 pdev->file = -1;
685 pdev->file_ctrl = -1;
686 pdev->nTransfer = 0;
687 } else {
688 pdev->is_opened = 1;
689 }
690 return (error);
691 }
692
693 int
libusb20_dev_reset(struct libusb20_device * pdev)694 libusb20_dev_reset(struct libusb20_device *pdev)
695 {
696 int error;
697
698 error = pdev->methods->reset_device(pdev);
699 return (error);
700 }
701
702 int
libusb20_dev_check_connected(struct libusb20_device * pdev)703 libusb20_dev_check_connected(struct libusb20_device *pdev)
704 {
705 int error;
706
707 error = pdev->methods->check_connected(pdev);
708 return (error);
709 }
710
711 int
libusb20_dev_set_power_mode(struct libusb20_device * pdev,uint8_t power_mode)712 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
713 {
714 int error;
715
716 error = pdev->methods->set_power_mode(pdev, power_mode);
717 return (error);
718 }
719
720 uint8_t
libusb20_dev_get_power_mode(struct libusb20_device * pdev)721 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
722 {
723 int error;
724 uint8_t power_mode;
725
726 error = pdev->methods->get_power_mode(pdev, &power_mode);
727 if (error)
728 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
729 return (power_mode);
730 }
731
732 int
libusb20_dev_get_port_path(struct libusb20_device * pdev,uint8_t * buf,uint8_t bufsize)733 libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
734 {
735 return (pdev->methods->get_port_path(pdev, buf, bufsize));
736 }
737
738 uint16_t
libusb20_dev_get_power_usage(struct libusb20_device * pdev)739 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
740 {
741 int error;
742 uint16_t power_usage;
743
744 error = pdev->methods->get_power_usage(pdev, &power_usage);
745 if (error)
746 power_usage = 0;
747 return (power_usage);
748 }
749
750 int
libusb20_dev_set_alt_index(struct libusb20_device * pdev,uint8_t ifaceIndex,uint8_t altIndex)751 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
752 {
753 int error;
754
755 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
756 return (error);
757 }
758
759 int
libusb20_dev_set_config_index(struct libusb20_device * pdev,uint8_t configIndex)760 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
761 {
762 int error;
763
764 error = pdev->methods->set_config_index(pdev, configIndex);
765 return (error);
766 }
767
768 int
libusb20_dev_request_sync(struct libusb20_device * pdev,struct LIBUSB20_CONTROL_SETUP_DECODED * setup,void * data,uint16_t * pactlen,uint32_t timeout,uint8_t flags)769 libusb20_dev_request_sync(struct libusb20_device *pdev,
770 struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
771 uint16_t *pactlen, uint32_t timeout, uint8_t flags)
772 {
773 int error;
774
775 error = pdev->methods->do_request_sync(pdev,
776 setup, data, pactlen, timeout, flags);
777 return (error);
778 }
779
780 int
libusb20_dev_req_string_sync(struct libusb20_device * pdev,uint8_t str_index,uint16_t langid,void * ptr,uint16_t len)781 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
782 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
783 {
784 struct LIBUSB20_CONTROL_SETUP_DECODED req;
785 int error;
786 int flags;
787
788 /* make sure memory is initialised */
789 memset(ptr, 0, len);
790
791 if (len < 4) {
792 /* invalid length */
793 return (LIBUSB20_ERROR_INVALID_PARAM);
794 }
795 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
796
797 /*
798 * We need to read the USB string in two steps else some USB
799 * devices will complain.
800 */
801 req.bmRequestType =
802 LIBUSB20_REQUEST_TYPE_STANDARD |
803 LIBUSB20_RECIPIENT_DEVICE |
804 LIBUSB20_ENDPOINT_IN;
805 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
806 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
807 req.wIndex = langid;
808 req.wLength = 4; /* bytes */
809
810 error = libusb20_dev_request_sync(pdev, &req,
811 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
812 if (error) {
813 /* try to request full string */
814 req.wLength = 255;
815 flags = 0;
816 } else {
817 /* extract length and request full string */
818 req.wLength = *(uint8_t *)ptr;
819 flags = LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK;
820 }
821 if (req.wLength > len) {
822 /* partial string read */
823 req.wLength = len;
824 }
825 error = libusb20_dev_request_sync(pdev, &req, ptr, NULL, 1000, flags);
826 if (error)
827 return (error);
828
829 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING)
830 return (LIBUSB20_ERROR_OTHER);
831 return (0); /* success */
832 }
833
834 int
libusb20_dev_req_string_simple_sync(struct libusb20_device * pdev,uint8_t str_index,void * ptr,uint16_t len)835 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
836 uint8_t str_index, void *ptr, uint16_t len)
837 {
838 char *buf;
839 int error;
840 uint16_t langid;
841 uint16_t n;
842 uint16_t i;
843 uint16_t c;
844 uint8_t temp[255];
845 uint8_t swap;
846
847 /* the following code derives from the FreeBSD USB kernel */
848
849 if ((len < 1) || (ptr == NULL)) {
850 /* too short buffer */
851 return (LIBUSB20_ERROR_INVALID_PARAM);
852 }
853 error = libusb20_dev_req_string_sync(pdev,
854 0, 0, temp, sizeof(temp));
855 if (error < 0) {
856 *(uint8_t *)ptr = 0; /* zero terminate */
857 return (error);
858 }
859 langid = temp[2] | (temp[3] << 8);
860
861 error = libusb20_dev_req_string_sync(pdev, str_index,
862 langid, temp, sizeof(temp));
863 if (error < 0) {
864 *(uint8_t *)ptr = 0; /* zero terminate */
865 return (error);
866 }
867 if (temp[0] < 2) {
868 /* string length is too short */
869 *(uint8_t *)ptr = 0; /* zero terminate */
870 return (LIBUSB20_ERROR_OTHER);
871 }
872 /* reserve one byte for terminating zero */
873 len--;
874
875 /* find maximum length */
876 n = (temp[0] / 2) - 1;
877 if (n > len) {
878 n = len;
879 }
880 /* reset swap state */
881 swap = 3;
882
883 /* setup output buffer pointer */
884 buf = ptr;
885
886 /* convert and filter */
887 for (i = 0; (i != n); i++) {
888 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
889
890 /* convert from Unicode, handle buggy strings */
891 if (((c & 0xff00) == 0) && (swap & 1)) {
892 /* Little Endian, default */
893 *buf = c;
894 swap = 1;
895 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
896 /* Big Endian */
897 *buf = c >> 8;
898 swap = 2;
899 } else {
900 /* skip invalid character */
901 continue;
902 }
903 /*
904 * Filter by default - we don't allow greater and less than
905 * signs because they might confuse the dmesg printouts!
906 */
907 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
908 /* skip invalid character */
909 continue;
910 }
911 buf++;
912 }
913 *buf = 0; /* zero terminate string */
914
915 return (0);
916 }
917
918 struct libusb20_config *
libusb20_dev_alloc_config(struct libusb20_device * pdev,uint8_t configIndex)919 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
920 {
921 struct libusb20_config *retval = NULL;
922 uint8_t *ptr;
923 uint16_t len;
924 uint8_t do_close;
925 int error;
926
927 /*
928 * Catch invalid configuration descriptor reads early on to
929 * avoid issues with devices that don't check for a valid USB
930 * configuration read request.
931 */
932 if (configIndex >= pdev->ddesc.bNumConfigurations)
933 return (NULL);
934
935 if (!pdev->is_opened) {
936 error = libusb20_dev_open(pdev, 0);
937 if (error) {
938 return (NULL);
939 }
940 do_close = 1;
941 } else {
942 do_close = 0;
943 }
944 error = pdev->methods->get_config_desc_full(pdev,
945 &ptr, &len, configIndex);
946
947 if (error) {
948 goto done;
949 }
950 /* parse new config descriptor */
951 retval = libusb20_parse_config_desc(ptr);
952
953 /* free config descriptor */
954 free(ptr);
955
956 done:
957 if (do_close) {
958 error = libusb20_dev_close(pdev);
959 }
960 return (retval);
961 }
962
963 struct libusb20_device *
libusb20_dev_alloc(void)964 libusb20_dev_alloc(void)
965 {
966 struct libusb20_device *pdev;
967
968 pdev = malloc(sizeof(*pdev));
969 if (pdev == NULL) {
970 return (NULL);
971 }
972 memset(pdev, 0, sizeof(*pdev));
973
974 pdev->file = -1;
975 pdev->file_ctrl = -1;
976 pdev->methods = &libusb20_dummy_methods;
977 return (pdev);
978 }
979
980 uint8_t
libusb20_dev_get_config_index(struct libusb20_device * pdev)981 libusb20_dev_get_config_index(struct libusb20_device *pdev)
982 {
983 int error;
984 uint8_t cfg_index;
985 uint8_t do_close;
986
987 if (!pdev->is_opened) {
988 error = libusb20_dev_open(pdev, 0);
989 if (error == 0) {
990 do_close = 1;
991 } else {
992 do_close = 0;
993 }
994 } else {
995 do_close = 0;
996 }
997
998 error = pdev->methods->get_config_index(pdev, &cfg_index);
999 if (error)
1000 cfg_index = 0xFF; /* current config index */
1001 if (do_close) {
1002 if (libusb20_dev_close(pdev)) {
1003 /* ignore */
1004 }
1005 }
1006 return (cfg_index);
1007 }
1008
1009 uint8_t
libusb20_dev_get_mode(struct libusb20_device * pdev)1010 libusb20_dev_get_mode(struct libusb20_device *pdev)
1011 {
1012 return (pdev->usb_mode);
1013 }
1014
1015 uint8_t
libusb20_dev_get_speed(struct libusb20_device * pdev)1016 libusb20_dev_get_speed(struct libusb20_device *pdev)
1017 {
1018 return (pdev->usb_speed);
1019 }
1020
1021 /* if this function returns an error, the device is gone */
1022 int
libusb20_dev_process(struct libusb20_device * pdev)1023 libusb20_dev_process(struct libusb20_device *pdev)
1024 {
1025 int error;
1026
1027 error = pdev->methods->process(pdev);
1028 return (error);
1029 }
1030
1031 void
libusb20_dev_wait_process(struct libusb20_device * pdev,int timeout)1032 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1033 {
1034 struct pollfd pfd[1];
1035
1036 if (!pdev->is_opened) {
1037 return;
1038 }
1039 pfd[0].fd = pdev->file;
1040 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1041 pfd[0].revents = 0;
1042
1043 if (poll(pfd, 1, timeout)) {
1044 /* ignore any error */
1045 }
1046 return;
1047 }
1048
1049 void
libusb20_dev_free(struct libusb20_device * pdev)1050 libusb20_dev_free(struct libusb20_device *pdev)
1051 {
1052 if (pdev == NULL) {
1053 /* be NULL safe */
1054 return;
1055 }
1056 if (pdev->is_opened) {
1057 if (libusb20_dev_close(pdev)) {
1058 /* ignore any errors */
1059 }
1060 }
1061 free(pdev);
1062 return;
1063 }
1064
1065 int
libusb20_dev_get_info(struct libusb20_device * pdev,struct usb_device_info * pinfo)1066 libusb20_dev_get_info(struct libusb20_device *pdev,
1067 struct usb_device_info *pinfo)
1068 {
1069 if (pinfo == NULL)
1070 return (LIBUSB20_ERROR_INVALID_PARAM);
1071
1072 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1073 }
1074
1075 const char *
libusb20_dev_get_backend_name(struct libusb20_device * pdev)1076 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1077 {
1078 return (pdev->beMethods->get_backend_name());
1079 }
1080
1081 const char *
libusb20_dev_get_desc(struct libusb20_device * pdev)1082 libusb20_dev_get_desc(struct libusb20_device *pdev)
1083 {
1084 return (pdev->usb_desc);
1085 }
1086
1087 void
libusb20_dev_set_debug(struct libusb20_device * pdev,int debug)1088 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1089 {
1090 pdev->debug = debug;
1091 return;
1092 }
1093
1094 int
libusb20_dev_get_debug(struct libusb20_device * pdev)1095 libusb20_dev_get_debug(struct libusb20_device *pdev)
1096 {
1097 return (pdev->debug);
1098 }
1099
1100 uint8_t
libusb20_dev_get_address(struct libusb20_device * pdev)1101 libusb20_dev_get_address(struct libusb20_device *pdev)
1102 {
1103 return (pdev->device_address);
1104 }
1105
1106 uint8_t
libusb20_dev_get_parent_address(struct libusb20_device * pdev)1107 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1108 {
1109 return (pdev->parent_address);
1110 }
1111
1112 uint8_t
libusb20_dev_get_parent_port(struct libusb20_device * pdev)1113 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1114 {
1115 return (pdev->parent_port);
1116 }
1117
1118 uint8_t
libusb20_dev_get_bus_number(struct libusb20_device * pdev)1119 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1120 {
1121 return (pdev->bus_number);
1122 }
1123
1124 int
libusb20_dev_get_iface_desc(struct libusb20_device * pdev,uint8_t iface_index,char * buf,uint8_t len)1125 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1126 uint8_t iface_index, char *buf, uint8_t len)
1127 {
1128 if ((buf == NULL) || (len == 0))
1129 return (LIBUSB20_ERROR_INVALID_PARAM);
1130
1131 buf[0] = 0; /* set default string value */
1132
1133 return (pdev->beMethods->dev_get_iface_desc(
1134 pdev, iface_index, buf, len));
1135 }
1136
1137 /* USB backend operations */
1138
1139 int
libusb20_be_get_dev_quirk(struct libusb20_backend * pbe,uint16_t quirk_index,struct libusb20_quirk * pq)1140 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1141 uint16_t quirk_index, struct libusb20_quirk *pq)
1142 {
1143 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1144 }
1145
1146 int
libusb20_be_get_quirk_name(struct libusb20_backend * pbe,uint16_t quirk_index,struct libusb20_quirk * pq)1147 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1148 uint16_t quirk_index, struct libusb20_quirk *pq)
1149 {
1150 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1151 }
1152
1153 int
libusb20_be_add_dev_quirk(struct libusb20_backend * pbe,struct libusb20_quirk * pq)1154 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1155 struct libusb20_quirk *pq)
1156 {
1157 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1158 }
1159
1160 int
libusb20_be_remove_dev_quirk(struct libusb20_backend * pbe,struct libusb20_quirk * pq)1161 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1162 struct libusb20_quirk *pq)
1163 {
1164 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1165 }
1166
1167 int
libusb20_be_set_template(struct libusb20_backend * pbe,int temp)1168 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1169 {
1170 return (pbe->methods->root_set_template(pbe, temp));
1171 }
1172
1173 int
libusb20_be_get_template(struct libusb20_backend * pbe,int * ptemp)1174 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1175 {
1176 int temp;
1177
1178 if (ptemp == NULL)
1179 ptemp = &temp;
1180
1181 return (pbe->methods->root_get_template(pbe, ptemp));
1182 }
1183
1184 struct libusb20_device *
libusb20_be_device_foreach(struct libusb20_backend * pbe,struct libusb20_device * pdev)1185 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1186 {
1187 if (pbe == NULL) {
1188 pdev = NULL;
1189 } else if (pdev == NULL) {
1190 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1191 } else {
1192 pdev = TAILQ_NEXT(pdev, dev_entry);
1193 }
1194 return (pdev);
1195 }
1196
1197 struct libusb20_backend *
libusb20_be_alloc(const struct libusb20_backend_methods * methods)1198 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1199 {
1200 struct libusb20_backend *pbe;
1201
1202 pbe = malloc(sizeof(*pbe));
1203 if (pbe == NULL) {
1204 return (NULL);
1205 }
1206 memset(pbe, 0, sizeof(*pbe));
1207
1208 TAILQ_INIT(&(pbe->usb_devs));
1209
1210 pbe->methods = methods; /* set backend methods */
1211
1212 /* do the initial device scan */
1213 if (pbe->methods->init_backend) {
1214 pbe->methods->init_backend(pbe);
1215 }
1216 return (pbe);
1217 }
1218
1219 struct libusb20_backend *
libusb20_be_alloc_linux(void)1220 libusb20_be_alloc_linux(void)
1221 {
1222 struct libusb20_backend *pbe;
1223
1224 #ifdef __linux__
1225 pbe = libusb20_be_alloc(&libusb20_linux_backend);
1226 #else
1227 pbe = NULL;
1228 #endif
1229 return (pbe);
1230 }
1231
1232 struct libusb20_backend *
libusb20_be_alloc_ugen20(void)1233 libusb20_be_alloc_ugen20(void)
1234 {
1235 struct libusb20_backend *pbe;
1236
1237 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1238 pbe = libusb20_be_alloc(&libusb20_ugen20_backend);
1239 #else
1240 pbe = NULL;
1241 #endif
1242 return (pbe);
1243 }
1244
1245 struct libusb20_backend *
libusb20_be_alloc_default(void)1246 libusb20_be_alloc_default(void)
1247 {
1248 struct libusb20_backend *pbe;
1249
1250 pbe = libusb20_be_alloc_linux();
1251 if (pbe) {
1252 return (pbe);
1253 }
1254 pbe = libusb20_be_alloc_ugen20();
1255 if (pbe) {
1256 return (pbe);
1257 }
1258 return (NULL); /* no backend found */
1259 }
1260
1261 void
libusb20_be_free(struct libusb20_backend * pbe)1262 libusb20_be_free(struct libusb20_backend *pbe)
1263 {
1264 struct libusb20_device *pdev;
1265
1266 if (pbe == NULL) {
1267 /* be NULL safe */
1268 return;
1269 }
1270 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1271 libusb20_be_dequeue_device(pbe, pdev);
1272 libusb20_dev_free(pdev);
1273 }
1274 if (pbe->methods->exit_backend) {
1275 pbe->methods->exit_backend(pbe);
1276 }
1277 /* free backend */
1278 free(pbe);
1279 }
1280
1281 void
libusb20_be_enqueue_device(struct libusb20_backend * pbe,struct libusb20_device * pdev)1282 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1283 {
1284 pdev->beMethods = pbe->methods; /* copy backend methods */
1285 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1286 }
1287
1288 void
libusb20_be_dequeue_device(struct libusb20_backend * pbe,struct libusb20_device * pdev)1289 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1290 struct libusb20_device *pdev)
1291 {
1292 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1293 }
1294
1295 const char *
libusb20_strerror(int code)1296 libusb20_strerror(int code)
1297 {
1298 switch (code) {
1299 case LIBUSB20_SUCCESS:
1300 return ("Success");
1301 case LIBUSB20_ERROR_IO:
1302 return ("I/O error");
1303 case LIBUSB20_ERROR_INVALID_PARAM:
1304 return ("Invalid parameter");
1305 case LIBUSB20_ERROR_ACCESS:
1306 return ("Permissions error");
1307 case LIBUSB20_ERROR_NO_DEVICE:
1308 return ("No device");
1309 case LIBUSB20_ERROR_NOT_FOUND:
1310 return ("Not found");
1311 case LIBUSB20_ERROR_BUSY:
1312 return ("Device busy");
1313 case LIBUSB20_ERROR_TIMEOUT:
1314 return ("Timeout");
1315 case LIBUSB20_ERROR_OVERFLOW:
1316 return ("Overflow");
1317 case LIBUSB20_ERROR_PIPE:
1318 return ("Pipe error");
1319 case LIBUSB20_ERROR_INTERRUPTED:
1320 return ("Interrupted");
1321 case LIBUSB20_ERROR_NO_MEM:
1322 return ("Out of memory");
1323 case LIBUSB20_ERROR_NOT_SUPPORTED:
1324 return ("Not supported");
1325 case LIBUSB20_ERROR_OTHER:
1326 return ("Other error");
1327 default:
1328 return ("Unknown error");
1329 }
1330 }
1331
1332 const char *
libusb20_error_name(int code)1333 libusb20_error_name(int code)
1334 {
1335 switch (code) {
1336 case LIBUSB20_SUCCESS:
1337 return ("LIBUSB20_SUCCESS");
1338 case LIBUSB20_ERROR_IO:
1339 return ("LIBUSB20_ERROR_IO");
1340 case LIBUSB20_ERROR_INVALID_PARAM:
1341 return ("LIBUSB20_ERROR_INVALID_PARAM");
1342 case LIBUSB20_ERROR_ACCESS:
1343 return ("LIBUSB20_ERROR_ACCESS");
1344 case LIBUSB20_ERROR_NO_DEVICE:
1345 return ("LIBUSB20_ERROR_NO_DEVICE");
1346 case LIBUSB20_ERROR_NOT_FOUND:
1347 return ("LIBUSB20_ERROR_NOT_FOUND");
1348 case LIBUSB20_ERROR_BUSY:
1349 return ("LIBUSB20_ERROR_BUSY");
1350 case LIBUSB20_ERROR_TIMEOUT:
1351 return ("LIBUSB20_ERROR_TIMEOUT");
1352 case LIBUSB20_ERROR_OVERFLOW:
1353 return ("LIBUSB20_ERROR_OVERFLOW");
1354 case LIBUSB20_ERROR_PIPE:
1355 return ("LIBUSB20_ERROR_PIPE");
1356 case LIBUSB20_ERROR_INTERRUPTED:
1357 return ("LIBUSB20_ERROR_INTERRUPTED");
1358 case LIBUSB20_ERROR_NO_MEM:
1359 return ("LIBUSB20_ERROR_NO_MEM");
1360 case LIBUSB20_ERROR_NOT_SUPPORTED:
1361 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1362 case LIBUSB20_ERROR_OTHER:
1363 return ("LIBUSB20_ERROR_OTHER");
1364 default:
1365 return ("LIBUSB20_ERROR_UNKNOWN");
1366 }
1367 }
1368