3 * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/queue.h>
36 #include "libusb20_desc.h"
37 #include "libusb20_int.h"
42 return (LIBUSB20_ERROR_NOT_SUPPORTED);
52 dummy_callback(struct libusb20_transfer *xfer)
55 switch (libusb20_tr_get_status(xfer)) {
56 case LIBUSB20_TRANSFER_START:
57 libusb20_tr_submit(xfer);
60 /* complete or error */
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
86 #define dummy_tr_submit (void *)dummy_void
87 #define dummy_tr_cancel_async (void *)dummy_void
89 static const struct libusb20_device_methods libusb20_dummy_methods = {
90 LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
94 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
100 if (!xfer->is_pending) {
101 xfer->status = LIBUSB20_TRANSFER_START;
103 xfer->is_pending = 0;
106 xfer->callback(xfer);
108 if (xfer->is_restart) {
109 xfer->is_restart = 0;
112 if (xfer->is_draining &&
113 (!xfer->is_pending)) {
114 xfer->is_draining = 0;
115 xfer->status = LIBUSB20_TRANSFER_DRAINED;
116 xfer->callback(xfer);
122 libusb20_tr_close(struct libusb20_transfer *xfer)
126 if (!xfer->is_opened) {
127 return (LIBUSB20_ERROR_OTHER);
129 error = xfer->pdev->methods->tr_close(xfer);
134 if (xfer->ppBuffer) {
135 free(xfer->ppBuffer);
137 /* reset variable fields in case the transfer is opened again */
141 xfer->is_pending = 0;
143 xfer->is_draining = 0;
144 xfer->is_restart = 0;
151 xfer->maxTotalLength = 0;
152 xfer->maxPacketLen = 0;
157 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
158 uint32_t MaxFrameCount, uint8_t ep_no)
165 return (LIBUSB20_ERROR_BUSY);
166 if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
167 MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
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:
178 if (MaxFrameCount == 0)
179 return (LIBUSB20_ERROR_INVALID_PARAM);
181 xfer->maxFrames = MaxFrameCount;
183 size = MaxFrameCount * sizeof(xfer->pLength[0]);
184 xfer->pLength = malloc(size);
185 if (xfer->pLength == NULL) {
186 return (LIBUSB20_ERROR_NO_MEM);
188 memset(xfer->pLength, 0, size);
190 size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
191 xfer->ppBuffer = malloc(size);
192 if (xfer->ppBuffer == NULL) {
194 return (LIBUSB20_ERROR_NO_MEM);
196 memset(xfer->ppBuffer, 0, size);
199 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
200 MaxFrameCount / 8, ep_no, 1);
202 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
203 MaxFrameCount, ep_no, 0);
207 free(xfer->ppBuffer);
215 struct libusb20_transfer *
216 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
218 if (trIndex >= pdev->nTransfer) {
221 return (pdev->pTransfer + trIndex);
225 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
227 return (xfer->aFrames);
231 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
233 return (xfer->timeComplete);
237 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
242 for (x = 0; x != xfer->aFrames; x++) {
243 actlen += xfer->pLength[x];
249 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
251 return (xfer->maxFrames);
255 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
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" !
262 return (xfer->maxPacketLen);
266 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
268 return (xfer->maxTotalLength);
272 libusb20_tr_get_status(struct libusb20_transfer *xfer)
274 return (xfer->status);
278 libusb20_tr_pending(struct libusb20_transfer *xfer)
280 return (xfer->is_pending);
284 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
286 return (xfer->priv_sc0);
290 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
292 return (xfer->priv_sc1);
296 libusb20_tr_stop(struct libusb20_transfer *xfer)
298 if (!xfer->is_opened) {
299 /* transfer is not opened */
302 if (!xfer->is_pending) {
303 /* transfer not pending */
306 if (xfer->is_cancel) {
307 /* already cancelling */
310 xfer->is_cancel = 1; /* we are cancelling */
312 xfer->pdev->methods->tr_cancel_async(xfer);
317 libusb20_tr_drain(struct libusb20_transfer *xfer)
319 if (!xfer->is_opened) {
320 /* transfer is not opened */
323 /* make sure that we are cancelling */
324 libusb20_tr_stop(xfer);
326 if (xfer->is_pending) {
327 xfer->is_draining = 1;
333 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
335 xfer->pdev->methods->tr_clear_stall_sync(xfer);
340 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
342 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(buffer);
347 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
354 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
361 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
363 return (xfer->pLength[frIndex]);
367 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
369 xfer->pLength[frIndex] = length;
374 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
376 xfer->priv_sc0 = sc0;
381 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
383 xfer->priv_sc1 = sc1;
388 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
390 xfer->timeout = timeout;
395 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
397 if (nFrames > xfer->maxFrames) {
398 /* should not happen */
399 nFrames = xfer->maxFrames;
401 xfer->nFrames = nFrames;
406 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
408 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
409 xfer->pLength[0] = length;
410 xfer->timeout = timeout;
416 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
420 xfer->ppBuffer[0] = libusb20_pass_ptr(psetup);
421 xfer->pLength[0] = 8; /* fixed */
422 xfer->timeout = timeout;
424 len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
428 xfer->ppBuffer[1] = libusb20_pass_ptr(pBuf);
429 xfer->pLength[1] = len;
437 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
439 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
440 xfer->pLength[0] = length;
441 xfer->timeout = timeout;
447 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
449 if (frIndex >= xfer->maxFrames) {
450 /* should not happen */
453 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(pBuf);
454 xfer->pLength[frIndex] = length;
459 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
460 void *pbuf, uint32_t length, uint32_t *pactlen,
463 struct libusb20_device *pdev = xfer->pdev;
464 uint32_t transfer_max;
465 uint32_t transfer_act;
468 /* set some sensible default value */
472 /* check for error condition */
473 if (libusb20_tr_pending(xfer))
474 return (LIBUSB20_ERROR_OTHER);
477 /* compute maximum transfer length */
479 libusb20_tr_get_max_total_length(xfer);
481 if (transfer_max > length)
482 transfer_max = length;
484 /* setup bulk or interrupt transfer */
485 libusb20_tr_setup_bulk(xfer, pbuf,
486 transfer_max, timeout);
488 /* start the transfer */
489 libusb20_tr_start(xfer);
491 /* wait for transfer completion */
492 while (libusb20_dev_process(pdev) == 0) {
494 if (libusb20_tr_pending(xfer) == 0)
497 libusb20_dev_wait_process(pdev, -1);
500 transfer_act = libusb20_tr_get_actual_length(xfer);
502 /* update actual length, if any */
504 pactlen[0] += transfer_act;
506 /* check transfer status */
507 retval = libusb20_tr_get_status(xfer);
511 /* check for short transfer */
512 if (transfer_act != transfer_max)
515 /* update buffer pointer and length */
516 pbuf = ((uint8_t *)pbuf) + transfer_max;
517 length = length - transfer_max;
519 } while (length != 0);
525 libusb20_tr_submit(struct libusb20_transfer *xfer)
527 if (!xfer->is_opened) {
528 /* transfer is not opened */
531 if (xfer->is_pending) {
532 /* should not happen */
535 xfer->is_pending = 1; /* we are pending */
536 xfer->is_cancel = 0; /* not cancelling */
537 xfer->is_restart = 0; /* not restarting */
539 xfer->pdev->methods->tr_submit(xfer);
544 libusb20_tr_start(struct libusb20_transfer *xfer)
546 if (!xfer->is_opened) {
547 /* transfer is not opened */
550 if (xfer->is_pending) {
551 if (xfer->is_cancel) {
552 /* cancelling - restart */
553 xfer->is_restart = 1;
555 /* transfer not pending */
558 /* get into the callback */
559 libusb20_tr_callback_wrapper(xfer);
563 /* USB device operations */
566 libusb20_dev_close(struct libusb20_device *pdev)
568 struct libusb20_transfer *xfer;
572 if (!pdev->is_opened) {
573 return (LIBUSB20_ERROR_OTHER);
575 for (x = 0; x != pdev->nTransfer; x++) {
576 xfer = pdev->pTransfer + x;
578 if (!xfer->is_opened) {
579 /* transfer is not opened */
583 libusb20_tr_drain(xfer);
585 libusb20_tr_close(xfer);
588 if (pdev->pTransfer != NULL) {
589 free(pdev->pTransfer);
590 pdev->pTransfer = NULL;
592 error = pdev->beMethods->close_device(pdev);
594 pdev->methods = &libusb20_dummy_methods;
599 * The following variable is only used by the libusb v0.1
602 pdev->claimed_interface = 0;
605 * The following variable is only used by the libusb v1.0
608 pdev->auto_detach = 0;
614 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
618 error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
622 struct LIBUSB20_DEVICE_DESC_DECODED *
623 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
625 return (&(pdev->ddesc));
629 libusb20_dev_get_fd(struct libusb20_device *pdev)
635 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
639 error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
644 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
646 struct libusb20_transfer *xfer;
651 if (pdev->is_opened) {
652 return (LIBUSB20_ERROR_BUSY);
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);
662 memset(pdev->pTransfer, 0, size);
664 /* initialise all transfers */
665 for (x = 0; x != nTransferMax; x++) {
667 xfer = pdev->pTransfer + x;
671 xfer->callback = &dummy_callback;
674 /* set "nTransfer" early */
675 pdev->nTransfer = nTransferMax;
677 error = pdev->beMethods->open_device(pdev, nTransferMax);
680 if (pdev->pTransfer != NULL) {
681 free(pdev->pTransfer);
682 pdev->pTransfer = NULL;
685 pdev->file_ctrl = -1;
694 libusb20_dev_reset(struct libusb20_device *pdev)
698 error = pdev->methods->reset_device(pdev);
703 libusb20_dev_check_connected(struct libusb20_device *pdev)
707 error = pdev->methods->check_connected(pdev);
712 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
716 error = pdev->methods->set_power_mode(pdev, power_mode);
721 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
726 error = pdev->methods->get_power_mode(pdev, &power_mode);
728 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
733 libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
735 return (pdev->methods->get_port_path(pdev, buf, bufsize));
739 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
742 uint16_t power_usage;
744 error = pdev->methods->get_power_usage(pdev, &power_usage);
747 return (power_usage);
751 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
755 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
760 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
764 error = pdev->methods->set_config_index(pdev, configIndex);
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)
775 error = pdev->methods->do_request_sync(pdev,
776 setup, data, pactlen, timeout, flags);
781 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
782 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
784 struct LIBUSB20_CONTROL_SETUP_DECODED req;
787 /* make sure memory is initialised */
792 return (LIBUSB20_ERROR_INVALID_PARAM);
794 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
797 * We need to read the USB string in two steps else some USB
798 * devices will complain.
801 LIBUSB20_REQUEST_TYPE_STANDARD |
802 LIBUSB20_RECIPIENT_DEVICE |
803 LIBUSB20_ENDPOINT_IN;
804 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
805 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
807 req.wLength = 4; /* bytes */
809 error = libusb20_dev_request_sync(pdev, &req,
810 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
814 req.wLength = *(uint8_t *)ptr; /* bytes */
815 if (req.wLength > len) {
816 /* partial string read */
819 error = libusb20_dev_request_sync(pdev, &req,
820 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
825 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) {
826 return (LIBUSB20_ERROR_OTHER);
828 return (0); /* success */
832 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
833 uint8_t str_index, void *ptr, uint16_t len)
844 /* the following code derives from the FreeBSD USB kernel */
846 if ((len < 1) || (ptr == NULL)) {
847 /* too short buffer */
848 return (LIBUSB20_ERROR_INVALID_PARAM);
850 error = libusb20_dev_req_string_sync(pdev,
851 0, 0, temp, sizeof(temp));
853 *(uint8_t *)ptr = 0; /* zero terminate */
856 langid = temp[2] | (temp[3] << 8);
858 error = libusb20_dev_req_string_sync(pdev, str_index,
859 langid, temp, sizeof(temp));
861 *(uint8_t *)ptr = 0; /* zero terminate */
865 /* string length is too short */
866 *(uint8_t *)ptr = 0; /* zero terminate */
867 return (LIBUSB20_ERROR_OTHER);
869 /* reserve one byte for terminating zero */
872 /* find maximum length */
873 n = (temp[0] / 2) - 1;
877 /* reset swap state */
880 /* setup output buffer pointer */
883 /* convert and filter */
884 for (i = 0; (i != n); i++) {
885 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
887 /* convert from Unicode, handle buggy strings */
888 if (((c & 0xff00) == 0) && (swap & 1)) {
889 /* Little Endian, default */
892 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
897 /* skip invalid character */
901 * Filter by default - we don't allow greater and less than
902 * signs because they might confuse the dmesg printouts!
904 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
905 /* skip invalid character */
910 *buf = 0; /* zero terminate string */
915 struct libusb20_config *
916 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
918 struct libusb20_config *retval = NULL;
924 if (!pdev->is_opened) {
925 error = libusb20_dev_open(pdev, 0);
933 error = pdev->methods->get_config_desc_full(pdev,
934 &ptr, &len, configIndex);
939 /* parse new config descriptor */
940 retval = libusb20_parse_config_desc(ptr);
942 /* free config descriptor */
947 error = libusb20_dev_close(pdev);
952 struct libusb20_device *
953 libusb20_dev_alloc(void)
955 struct libusb20_device *pdev;
957 pdev = malloc(sizeof(*pdev));
961 memset(pdev, 0, sizeof(*pdev));
964 pdev->file_ctrl = -1;
965 pdev->methods = &libusb20_dummy_methods;
970 libusb20_dev_get_config_index(struct libusb20_device *pdev)
976 if (!pdev->is_opened) {
977 error = libusb20_dev_open(pdev, 0);
987 error = pdev->methods->get_config_index(pdev, &cfg_index);
989 cfg_index = 0xFF; /* current config index */
991 if (libusb20_dev_close(pdev)) {
999 libusb20_dev_get_mode(struct libusb20_device *pdev)
1001 return (pdev->usb_mode);
1005 libusb20_dev_get_speed(struct libusb20_device *pdev)
1007 return (pdev->usb_speed);
1010 /* if this function returns an error, the device is gone */
1012 libusb20_dev_process(struct libusb20_device *pdev)
1016 error = pdev->methods->process(pdev);
1021 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1023 struct pollfd pfd[1];
1025 if (!pdev->is_opened) {
1028 pfd[0].fd = pdev->file;
1029 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1032 if (poll(pfd, 1, timeout)) {
1033 /* ignore any error */
1039 libusb20_dev_free(struct libusb20_device *pdev)
1045 if (pdev->is_opened) {
1046 if (libusb20_dev_close(pdev)) {
1047 /* ignore any errors */
1055 libusb20_dev_get_info(struct libusb20_device *pdev,
1056 struct usb_device_info *pinfo)
1059 return (LIBUSB20_ERROR_INVALID_PARAM);
1061 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1065 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1067 return (pdev->beMethods->get_backend_name());
1071 libusb20_dev_get_desc(struct libusb20_device *pdev)
1073 return (pdev->usb_desc);
1077 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1079 pdev->debug = debug;
1084 libusb20_dev_get_debug(struct libusb20_device *pdev)
1086 return (pdev->debug);
1090 libusb20_dev_get_address(struct libusb20_device *pdev)
1092 return (pdev->device_address);
1096 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1098 return (pdev->parent_address);
1102 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1104 return (pdev->parent_port);
1108 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1110 return (pdev->bus_number);
1114 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1115 uint8_t iface_index, char *buf, uint8_t len)
1117 if ((buf == NULL) || (len == 0))
1118 return (LIBUSB20_ERROR_INVALID_PARAM);
1120 buf[0] = 0; /* set default string value */
1122 return (pdev->beMethods->dev_get_iface_desc(
1123 pdev, iface_index, buf, len));
1126 /* USB backend operations */
1129 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1130 uint16_t quirk_index, struct libusb20_quirk *pq)
1132 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1136 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1137 uint16_t quirk_index, struct libusb20_quirk *pq)
1139 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1143 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1144 struct libusb20_quirk *pq)
1146 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1150 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1151 struct libusb20_quirk *pq)
1153 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1157 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1159 return (pbe->methods->root_set_template(pbe, temp));
1163 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1170 return (pbe->methods->root_get_template(pbe, ptemp));
1173 struct libusb20_device *
1174 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1178 } else if (pdev == NULL) {
1179 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1181 pdev = TAILQ_NEXT(pdev, dev_entry);
1186 struct libusb20_backend *
1187 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1189 struct libusb20_backend *pbe;
1191 pbe = malloc(sizeof(*pbe));
1195 memset(pbe, 0, sizeof(*pbe));
1197 TAILQ_INIT(&(pbe->usb_devs));
1199 pbe->methods = methods; /* set backend methods */
1201 /* do the initial device scan */
1202 if (pbe->methods->init_backend) {
1203 pbe->methods->init_backend(pbe);
1208 struct libusb20_backend *
1209 libusb20_be_alloc_linux(void)
1211 struct libusb20_backend *pbe;
1214 pbe = libusb20_be_alloc(&libusb20_linux_backend);
1221 struct libusb20_backend *
1222 libusb20_be_alloc_ugen20(void)
1224 struct libusb20_backend *pbe;
1226 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1227 pbe = libusb20_be_alloc(&libusb20_ugen20_backend);
1234 struct libusb20_backend *
1235 libusb20_be_alloc_default(void)
1237 struct libusb20_backend *pbe;
1239 pbe = libusb20_be_alloc_linux();
1243 pbe = libusb20_be_alloc_ugen20();
1247 return (NULL); /* no backend found */
1251 libusb20_be_free(struct libusb20_backend *pbe)
1253 struct libusb20_device *pdev;
1259 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1260 libusb20_be_dequeue_device(pbe, pdev);
1261 libusb20_dev_free(pdev);
1263 if (pbe->methods->exit_backend) {
1264 pbe->methods->exit_backend(pbe);
1271 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1273 pdev->beMethods = pbe->methods; /* copy backend methods */
1274 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1278 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1279 struct libusb20_device *pdev)
1281 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1285 libusb20_strerror(int code)
1288 case LIBUSB20_SUCCESS:
1290 case LIBUSB20_ERROR_IO:
1291 return ("I/O error");
1292 case LIBUSB20_ERROR_INVALID_PARAM:
1293 return ("Invalid parameter");
1294 case LIBUSB20_ERROR_ACCESS:
1295 return ("Permissions error");
1296 case LIBUSB20_ERROR_NO_DEVICE:
1297 return ("No device");
1298 case LIBUSB20_ERROR_NOT_FOUND:
1299 return ("Not found");
1300 case LIBUSB20_ERROR_BUSY:
1301 return ("Device busy");
1302 case LIBUSB20_ERROR_TIMEOUT:
1304 case LIBUSB20_ERROR_OVERFLOW:
1305 return ("Overflow");
1306 case LIBUSB20_ERROR_PIPE:
1307 return ("Pipe error");
1308 case LIBUSB20_ERROR_INTERRUPTED:
1309 return ("Interrupted");
1310 case LIBUSB20_ERROR_NO_MEM:
1311 return ("Out of memory");
1312 case LIBUSB20_ERROR_NOT_SUPPORTED:
1313 return ("Not supported");
1314 case LIBUSB20_ERROR_OTHER:
1315 return ("Other error");
1317 return ("Unknown error");
1322 libusb20_error_name(int code)
1325 case LIBUSB20_SUCCESS:
1326 return ("LIBUSB20_SUCCESS");
1327 case LIBUSB20_ERROR_IO:
1328 return ("LIBUSB20_ERROR_IO");
1329 case LIBUSB20_ERROR_INVALID_PARAM:
1330 return ("LIBUSB20_ERROR_INVALID_PARAM");
1331 case LIBUSB20_ERROR_ACCESS:
1332 return ("LIBUSB20_ERROR_ACCESS");
1333 case LIBUSB20_ERROR_NO_DEVICE:
1334 return ("LIBUSB20_ERROR_NO_DEVICE");
1335 case LIBUSB20_ERROR_NOT_FOUND:
1336 return ("LIBUSB20_ERROR_NOT_FOUND");
1337 case LIBUSB20_ERROR_BUSY:
1338 return ("LIBUSB20_ERROR_BUSY");
1339 case LIBUSB20_ERROR_TIMEOUT:
1340 return ("LIBUSB20_ERROR_TIMEOUT");
1341 case LIBUSB20_ERROR_OVERFLOW:
1342 return ("LIBUSB20_ERROR_OVERFLOW");
1343 case LIBUSB20_ERROR_PIPE:
1344 return ("LIBUSB20_ERROR_PIPE");
1345 case LIBUSB20_ERROR_INTERRUPTED:
1346 return ("LIBUSB20_ERROR_INTERRUPTED");
1347 case LIBUSB20_ERROR_NO_MEM:
1348 return ("LIBUSB20_ERROR_NO_MEM");
1349 case LIBUSB20_ERROR_NOT_SUPPORTED:
1350 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1351 case LIBUSB20_ERROR_OTHER:
1352 return ("LIBUSB20_ERROR_OTHER");
1354 return ("LIBUSB20_ERROR_UNKNOWN");