3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
30 #include LIBUSB_GLOBAL_INCLUDE_FILE
38 #include <sys/queue.h>
42 #include "libusb20_desc.h"
43 #include "libusb20_int.h"
48 return (LIBUSB20_ERROR_NOT_SUPPORTED);
58 dummy_callback(struct libusb20_transfer *xfer)
61 switch (libusb20_tr_get_status(xfer)) {
62 case LIBUSB20_TRANSFER_START:
63 libusb20_tr_submit(xfer);
66 /* complete or error */
72 #define dummy_get_config_desc_full (void *)dummy_int
73 #define dummy_get_config_index (void *)dummy_int
74 #define dummy_set_config_index (void *)dummy_int
75 #define dummy_set_alt_index (void *)dummy_int
76 #define dummy_reset_device (void *)dummy_int
77 #define dummy_check_connected (void *)dummy_int
78 #define dummy_set_power_mode (void *)dummy_int
79 #define dummy_get_power_mode (void *)dummy_int
80 #define dummy_get_power_usage (void *)dummy_int
81 #define dummy_kernel_driver_active (void *)dummy_int
82 #define dummy_detach_kernel_driver (void *)dummy_int
83 #define dummy_do_request_sync (void *)dummy_int
84 #define dummy_tr_open (void *)dummy_int
85 #define dummy_tr_close (void *)dummy_int
86 #define dummy_tr_clear_stall_sync (void *)dummy_int
87 #define dummy_process (void *)dummy_int
88 #define dummy_dev_info (void *)dummy_int
89 #define dummy_dev_get_iface_driver (void *)dummy_int
91 #define dummy_tr_submit (void *)dummy_void
92 #define dummy_tr_cancel_async (void *)dummy_void
94 static const struct libusb20_device_methods libusb20_dummy_methods = {
95 LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
99 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
105 if (!xfer->is_pending) {
106 xfer->status = LIBUSB20_TRANSFER_START;
108 xfer->is_pending = 0;
111 xfer->callback(xfer);
113 if (xfer->is_restart) {
114 xfer->is_restart = 0;
117 if (xfer->is_draining &&
118 (!xfer->is_pending)) {
119 xfer->is_draining = 0;
120 xfer->status = LIBUSB20_TRANSFER_DRAINED;
121 xfer->callback(xfer);
127 libusb20_tr_close(struct libusb20_transfer *xfer)
131 if (!xfer->is_opened) {
132 return (LIBUSB20_ERROR_OTHER);
134 error = xfer->pdev->methods->tr_close(xfer);
139 if (xfer->ppBuffer) {
140 free(xfer->ppBuffer);
142 /* reset variable fields in case the transfer is opened again */
143 xfer->priv_sc0 = NULL;
144 xfer->priv_sc1 = NULL;
146 xfer->is_pending = 0;
148 xfer->is_draining = 0;
149 xfer->is_restart = 0;
156 xfer->maxTotalLength = 0;
157 xfer->maxPacketLen = 0;
162 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
163 uint32_t MaxFrameCount, uint8_t ep_no)
165 return (libusb20_tr_open_stream(xfer, MaxBufSize, MaxFrameCount, ep_no, 0));
169 libusb20_tr_open_stream(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
170 uint32_t MaxFrameCount, uint8_t ep_no, uint16_t stream_id)
177 return (LIBUSB20_ERROR_BUSY);
178 if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
179 MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
181 * The kernel can setup 8 times more frames when
182 * pre-scaling ISOCHRONOUS transfers. Make sure the
183 * length and pointer buffers are big enough:
190 if (MaxFrameCount == 0)
191 return (LIBUSB20_ERROR_INVALID_PARAM);
193 xfer->maxFrames = MaxFrameCount;
195 size = MaxFrameCount * sizeof(xfer->pLength[0]);
196 xfer->pLength = malloc(size);
197 if (xfer->pLength == NULL) {
198 return (LIBUSB20_ERROR_NO_MEM);
200 memset(xfer->pLength, 0, size);
202 size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
203 xfer->ppBuffer = malloc(size);
204 if (xfer->ppBuffer == NULL) {
206 return (LIBUSB20_ERROR_NO_MEM);
208 memset(xfer->ppBuffer, 0, size);
211 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
212 MaxFrameCount / 8, ep_no, stream_id, 1);
214 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
215 MaxFrameCount, ep_no, stream_id, 0);
219 free(xfer->ppBuffer);
227 struct libusb20_transfer *
228 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
230 if (trIndex >= pdev->nTransfer) {
233 return (pdev->pTransfer + trIndex);
237 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
239 return (xfer->aFrames);
243 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
245 return (xfer->timeComplete);
249 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
254 for (x = 0; x != xfer->aFrames; x++) {
255 actlen += xfer->pLength[x];
261 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
263 return (xfer->maxFrames);
267 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
270 * Special Case NOTE: If the packet multiplier is non-zero for
271 * High Speed USB, the value returned is equal to
272 * "wMaxPacketSize * multiplier" !
274 return (xfer->maxPacketLen);
278 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
280 return (xfer->maxTotalLength);
284 libusb20_tr_get_status(struct libusb20_transfer *xfer)
286 return (xfer->status);
290 libusb20_tr_pending(struct libusb20_transfer *xfer)
292 return (xfer->is_pending);
296 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
298 return (xfer->priv_sc0);
302 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
304 return (xfer->priv_sc1);
308 libusb20_tr_stop(struct libusb20_transfer *xfer)
310 if (!xfer->is_opened) {
311 /* transfer is not opened */
314 if (!xfer->is_pending) {
315 /* transfer not pending */
318 if (xfer->is_cancel) {
319 /* already cancelling */
322 xfer->is_cancel = 1; /* we are cancelling */
324 xfer->pdev->methods->tr_cancel_async(xfer);
329 libusb20_tr_drain(struct libusb20_transfer *xfer)
331 if (!xfer->is_opened) {
332 /* transfer is not opened */
335 /* make sure that we are cancelling */
336 libusb20_tr_stop(xfer);
338 if (xfer->is_pending) {
339 xfer->is_draining = 1;
345 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
347 xfer->pdev->methods->tr_clear_stall_sync(xfer);
352 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
354 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(buffer);
359 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
366 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
373 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
375 return (xfer->pLength[frIndex]);
379 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
381 xfer->pLength[frIndex] = length;
386 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
388 xfer->priv_sc0 = sc0;
393 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
395 xfer->priv_sc1 = sc1;
400 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
402 xfer->timeout = timeout;
407 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
409 if (nFrames > xfer->maxFrames) {
410 /* should not happen */
411 nFrames = xfer->maxFrames;
413 xfer->nFrames = nFrames;
418 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
420 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
421 xfer->pLength[0] = length;
422 xfer->timeout = timeout;
428 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
432 xfer->ppBuffer[0] = libusb20_pass_ptr(psetup);
433 xfer->pLength[0] = 8; /* fixed */
434 xfer->timeout = timeout;
436 len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
440 xfer->ppBuffer[1] = libusb20_pass_ptr(pBuf);
441 xfer->pLength[1] = len;
449 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
451 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
452 xfer->pLength[0] = length;
453 xfer->timeout = timeout;
459 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
461 if (frIndex >= xfer->maxFrames) {
462 /* should not happen */
465 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(pBuf);
466 xfer->pLength[frIndex] = length;
471 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
472 void *pbuf, uint32_t length, uint32_t *pactlen,
475 struct libusb20_device *pdev = xfer->pdev;
476 uint32_t transfer_max;
477 uint32_t transfer_act;
480 /* set some sensible default value */
484 /* check for error condition */
485 if (libusb20_tr_pending(xfer))
486 return (LIBUSB20_ERROR_OTHER);
489 /* compute maximum transfer length */
491 libusb20_tr_get_max_total_length(xfer);
493 if (transfer_max > length)
494 transfer_max = length;
496 /* setup bulk or interrupt transfer */
497 libusb20_tr_setup_bulk(xfer, pbuf,
498 transfer_max, timeout);
500 /* start the transfer */
501 libusb20_tr_start(xfer);
503 /* wait for transfer completion */
504 while (libusb20_dev_process(pdev) == 0) {
506 if (libusb20_tr_pending(xfer) == 0)
509 libusb20_dev_wait_process(pdev, -1);
512 transfer_act = libusb20_tr_get_actual_length(xfer);
514 /* update actual length, if any */
516 pactlen[0] += transfer_act;
518 /* check transfer status */
519 retval = libusb20_tr_get_status(xfer);
523 /* check for short transfer */
524 if (transfer_act != transfer_max)
527 /* update buffer pointer and length */
528 pbuf = ((uint8_t *)pbuf) + transfer_max;
529 length = length - transfer_max;
531 } while (length != 0);
537 libusb20_tr_submit(struct libusb20_transfer *xfer)
539 if (!xfer->is_opened) {
540 /* transfer is not opened */
543 if (xfer->is_pending) {
544 /* should not happen */
547 xfer->is_pending = 1; /* we are pending */
548 xfer->is_cancel = 0; /* not cancelling */
549 xfer->is_restart = 0; /* not restarting */
551 xfer->pdev->methods->tr_submit(xfer);
556 libusb20_tr_start(struct libusb20_transfer *xfer)
558 if (!xfer->is_opened) {
559 /* transfer is not opened */
562 if (xfer->is_pending) {
563 if (xfer->is_cancel) {
564 /* cancelling - restart */
565 xfer->is_restart = 1;
567 /* transfer not pending */
570 /* get into the callback */
571 libusb20_tr_callback_wrapper(xfer);
575 /* USB device operations */
578 libusb20_dev_close(struct libusb20_device *pdev)
580 struct libusb20_transfer *xfer;
584 if (!pdev->is_opened) {
585 return (LIBUSB20_ERROR_OTHER);
587 for (x = 0; x != pdev->nTransfer; x++) {
588 xfer = pdev->pTransfer + x;
590 if (!xfer->is_opened) {
591 /* transfer is not opened */
595 libusb20_tr_drain(xfer);
597 libusb20_tr_close(xfer);
600 if (pdev->pTransfer != NULL) {
601 free(pdev->pTransfer);
602 pdev->pTransfer = NULL;
604 error = pdev->beMethods->close_device(pdev);
606 pdev->methods = &libusb20_dummy_methods;
611 * The following variable is only used by the libusb v0.1
614 pdev->claimed_interface = 0;
617 * The following variable is only used by the libusb v1.0
620 pdev->auto_detach = 0;
626 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
630 error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
634 struct LIBUSB20_DEVICE_DESC_DECODED *
635 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
637 return (&(pdev->ddesc));
641 libusb20_dev_get_fd(struct libusb20_device *pdev)
647 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
651 error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
656 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
658 struct libusb20_transfer *xfer;
663 if (pdev->is_opened) {
664 return (LIBUSB20_ERROR_BUSY);
666 if (nTransferMax >= 256) {
667 return (LIBUSB20_ERROR_INVALID_PARAM);
668 } else if (nTransferMax != 0) {
669 size = sizeof(pdev->pTransfer[0]) * nTransferMax;
670 pdev->pTransfer = malloc(size);
671 if (pdev->pTransfer == NULL) {
672 return (LIBUSB20_ERROR_NO_MEM);
674 memset(pdev->pTransfer, 0, size);
676 /* initialise all transfers */
677 for (x = 0; x != nTransferMax; x++) {
679 xfer = pdev->pTransfer + x;
683 xfer->callback = &dummy_callback;
686 /* set "nTransfer" early */
687 pdev->nTransfer = nTransferMax;
689 error = pdev->beMethods->open_device(pdev, nTransferMax);
692 if (pdev->pTransfer != NULL) {
693 free(pdev->pTransfer);
694 pdev->pTransfer = NULL;
697 pdev->file_ctrl = -1;
706 libusb20_dev_reset(struct libusb20_device *pdev)
710 error = pdev->methods->reset_device(pdev);
715 libusb20_dev_check_connected(struct libusb20_device *pdev)
719 error = pdev->methods->check_connected(pdev);
724 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
728 error = pdev->methods->set_power_mode(pdev, power_mode);
733 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
738 error = pdev->methods->get_power_mode(pdev, &power_mode);
740 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
745 libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
748 if (pdev->port_level == 0) {
750 * Fallback for backends without port path:
753 return (LIBUSB20_ERROR_OVERFLOW);
754 buf[0] = pdev->parent_address;
755 buf[1] = pdev->parent_port;
759 /* check if client buffer is too small */
760 if (pdev->port_level > bufsize)
761 return (LIBUSB20_ERROR_OVERFLOW);
763 /* copy port number information */
764 memcpy(buf, pdev->port_path, pdev->port_level);
766 return (pdev->port_level); /* success */
770 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
773 uint16_t power_usage;
775 error = pdev->methods->get_power_usage(pdev, &power_usage);
778 return (power_usage);
782 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
786 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
791 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
795 error = pdev->methods->set_config_index(pdev, configIndex);
800 libusb20_dev_request_sync(struct libusb20_device *pdev,
801 struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
802 uint16_t *pactlen, uint32_t timeout, uint8_t flags)
806 error = pdev->methods->do_request_sync(pdev,
807 setup, data, pactlen, timeout, flags);
812 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
813 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
815 struct LIBUSB20_CONTROL_SETUP_DECODED req;
819 /* make sure memory is initialised */
824 return (LIBUSB20_ERROR_INVALID_PARAM);
826 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
829 * We need to read the USB string in two steps else some USB
830 * devices will complain.
833 LIBUSB20_REQUEST_TYPE_STANDARD |
834 LIBUSB20_RECIPIENT_DEVICE |
835 LIBUSB20_ENDPOINT_IN;
836 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
837 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
839 req.wLength = 4; /* bytes */
841 error = libusb20_dev_request_sync(pdev, &req,
842 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
844 /* try to request full string */
848 /* extract length and request full string */
849 req.wLength = *(uint8_t *)ptr;
850 flags = LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK;
852 if (req.wLength > len) {
853 /* partial string read */
856 error = libusb20_dev_request_sync(pdev, &req, ptr, NULL, 1000, flags);
860 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING)
861 return (LIBUSB20_ERROR_OTHER);
862 return (0); /* success */
866 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
867 uint8_t str_index, void *ptr, uint16_t len)
878 /* the following code derives from the FreeBSD USB kernel */
880 if ((len < 1) || (ptr == NULL)) {
881 /* too short buffer */
882 return (LIBUSB20_ERROR_INVALID_PARAM);
884 error = libusb20_dev_req_string_sync(pdev,
885 0, 0, temp, sizeof(temp));
887 *(uint8_t *)ptr = 0; /* zero terminate */
890 langid = temp[2] | (temp[3] << 8);
892 error = libusb20_dev_req_string_sync(pdev, str_index,
893 langid, temp, sizeof(temp));
895 *(uint8_t *)ptr = 0; /* zero terminate */
899 /* string length is too short */
900 *(uint8_t *)ptr = 0; /* zero terminate */
901 return (LIBUSB20_ERROR_OTHER);
903 /* reserve one byte for terminating zero */
906 /* find maximum length */
907 n = (temp[0] / 2) - 1;
911 /* reset swap state */
914 /* setup output buffer pointer */
917 /* convert and filter */
918 for (i = 0; (i != n); i++) {
919 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
921 /* convert from Unicode, handle buggy strings */
922 if (((c & 0xff00) == 0) && (swap & 1)) {
923 /* Little Endian, default */
926 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
931 /* skip invalid character */
935 * Filter by default - we don't allow greater and less than
936 * signs because they might confuse the dmesg printouts!
938 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
939 /* skip invalid character */
944 *buf = 0; /* zero terminate string */
949 struct libusb20_config *
950 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
952 struct libusb20_config *retval = NULL;
959 * Catch invalid configuration descriptor reads early on to
960 * avoid issues with devices that don't check for a valid USB
961 * configuration read request.
963 if (configIndex >= pdev->ddesc.bNumConfigurations)
966 if (!pdev->is_opened) {
967 error = libusb20_dev_open(pdev, 0);
975 error = pdev->methods->get_config_desc_full(pdev,
976 &ptr, &len, configIndex);
981 /* parse new config descriptor */
982 retval = libusb20_parse_config_desc(ptr);
984 /* free config descriptor */
989 error = libusb20_dev_close(pdev);
994 struct libusb20_device *
995 libusb20_dev_alloc(void)
997 struct libusb20_device *pdev;
999 pdev = malloc(sizeof(*pdev));
1003 memset(pdev, 0, sizeof(*pdev));
1006 pdev->file_ctrl = -1;
1007 pdev->methods = &libusb20_dummy_methods;
1012 libusb20_dev_get_config_index(struct libusb20_device *pdev)
1018 if (!pdev->is_opened) {
1019 error = libusb20_dev_open(pdev, 0);
1029 error = pdev->methods->get_config_index(pdev, &cfg_index);
1031 cfg_index = 0xFF; /* current config index */
1033 if (libusb20_dev_close(pdev)) {
1041 libusb20_dev_get_mode(struct libusb20_device *pdev)
1043 return (pdev->usb_mode);
1047 libusb20_dev_get_speed(struct libusb20_device *pdev)
1049 return (pdev->usb_speed);
1052 /* if this function returns an error, the device is gone */
1054 libusb20_dev_process(struct libusb20_device *pdev)
1058 error = pdev->methods->process(pdev);
1063 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1065 struct pollfd pfd[1];
1067 if (!pdev->is_opened) {
1070 pfd[0].fd = pdev->file;
1071 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1074 if (poll(pfd, 1, timeout)) {
1075 /* ignore any error */
1081 libusb20_dev_free(struct libusb20_device *pdev)
1087 if (pdev->is_opened) {
1088 if (libusb20_dev_close(pdev)) {
1089 /* ignore any errors */
1097 libusb20_dev_get_info(struct libusb20_device *pdev,
1098 struct usb_device_info *pinfo)
1101 return (LIBUSB20_ERROR_INVALID_PARAM);
1103 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1107 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1109 return (pdev->beMethods->get_backend_name());
1113 libusb20_dev_get_desc(struct libusb20_device *pdev)
1115 return (pdev->usb_desc);
1119 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1121 pdev->debug = debug;
1126 libusb20_dev_get_debug(struct libusb20_device *pdev)
1128 return (pdev->debug);
1132 libusb20_dev_get_address(struct libusb20_device *pdev)
1134 return (pdev->device_address);
1138 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1140 return (pdev->parent_address);
1144 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1146 return (pdev->parent_port);
1150 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1152 return (pdev->bus_number);
1156 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1157 uint8_t iface_index, char *buf, uint8_t len)
1159 if ((buf == NULL) || (len == 0))
1160 return (LIBUSB20_ERROR_INVALID_PARAM);
1162 buf[0] = 0; /* set default string value */
1164 return (pdev->beMethods->dev_get_iface_desc(
1165 pdev, iface_index, buf, len));
1168 /* USB backend operations */
1171 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1172 uint16_t quirk_index, struct libusb20_quirk *pq)
1174 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1178 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1179 uint16_t quirk_index, struct libusb20_quirk *pq)
1181 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1185 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1186 struct libusb20_quirk *pq)
1188 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1192 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1193 struct libusb20_quirk *pq)
1195 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1199 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1201 return (pbe->methods->root_set_template(pbe, temp));
1205 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1212 return (pbe->methods->root_get_template(pbe, ptemp));
1215 struct libusb20_device *
1216 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1220 } else if (pdev == NULL) {
1221 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1223 pdev = TAILQ_NEXT(pdev, dev_entry);
1228 struct libusb20_backend *
1229 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1231 struct libusb20_backend *pbe;
1233 pbe = malloc(sizeof(*pbe));
1237 memset(pbe, 0, sizeof(*pbe));
1239 TAILQ_INIT(&(pbe->usb_devs));
1241 pbe->methods = methods; /* set backend methods */
1243 /* do the initial device scan */
1244 if (pbe->methods->init_backend) {
1245 pbe->methods->init_backend(pbe);
1250 struct libusb20_backend *
1251 libusb20_be_alloc_linux(void)
1256 struct libusb20_backend *
1257 libusb20_be_alloc_ugen20(void)
1259 return (libusb20_be_alloc(&libusb20_ugen20_backend));
1262 struct libusb20_backend *
1263 libusb20_be_alloc_default(void)
1265 struct libusb20_backend *pbe;
1268 pbe = libusb20_be_alloc_linux();
1273 pbe = libusb20_be_alloc_ugen20();
1277 return (NULL); /* no backend found */
1281 libusb20_be_free(struct libusb20_backend *pbe)
1283 struct libusb20_device *pdev;
1289 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1290 libusb20_be_dequeue_device(pbe, pdev);
1291 libusb20_dev_free(pdev);
1293 if (pbe->methods->exit_backend) {
1294 pbe->methods->exit_backend(pbe);
1301 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1303 pdev->beMethods = pbe->methods; /* copy backend methods */
1304 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1308 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1309 struct libusb20_device *pdev)
1311 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1315 libusb20_strerror(int code)
1318 case LIBUSB20_SUCCESS:
1320 case LIBUSB20_ERROR_IO:
1321 return ("I/O error");
1322 case LIBUSB20_ERROR_INVALID_PARAM:
1323 return ("Invalid parameter");
1324 case LIBUSB20_ERROR_ACCESS:
1325 return ("Permissions error");
1326 case LIBUSB20_ERROR_NO_DEVICE:
1327 return ("No device");
1328 case LIBUSB20_ERROR_NOT_FOUND:
1329 return ("Not found");
1330 case LIBUSB20_ERROR_BUSY:
1331 return ("Device busy");
1332 case LIBUSB20_ERROR_TIMEOUT:
1334 case LIBUSB20_ERROR_OVERFLOW:
1335 return ("Overflow");
1336 case LIBUSB20_ERROR_PIPE:
1337 return ("Pipe error");
1338 case LIBUSB20_ERROR_INTERRUPTED:
1339 return ("Interrupted");
1340 case LIBUSB20_ERROR_NO_MEM:
1341 return ("Out of memory");
1342 case LIBUSB20_ERROR_NOT_SUPPORTED:
1343 return ("Not supported");
1344 case LIBUSB20_ERROR_OTHER:
1345 return ("Other error");
1347 return ("Unknown error");
1352 libusb20_error_name(int code)
1355 case LIBUSB20_SUCCESS:
1356 return ("LIBUSB20_SUCCESS");
1357 case LIBUSB20_ERROR_IO:
1358 return ("LIBUSB20_ERROR_IO");
1359 case LIBUSB20_ERROR_INVALID_PARAM:
1360 return ("LIBUSB20_ERROR_INVALID_PARAM");
1361 case LIBUSB20_ERROR_ACCESS:
1362 return ("LIBUSB20_ERROR_ACCESS");
1363 case LIBUSB20_ERROR_NO_DEVICE:
1364 return ("LIBUSB20_ERROR_NO_DEVICE");
1365 case LIBUSB20_ERROR_NOT_FOUND:
1366 return ("LIBUSB20_ERROR_NOT_FOUND");
1367 case LIBUSB20_ERROR_BUSY:
1368 return ("LIBUSB20_ERROR_BUSY");
1369 case LIBUSB20_ERROR_TIMEOUT:
1370 return ("LIBUSB20_ERROR_TIMEOUT");
1371 case LIBUSB20_ERROR_OVERFLOW:
1372 return ("LIBUSB20_ERROR_OVERFLOW");
1373 case LIBUSB20_ERROR_PIPE:
1374 return ("LIBUSB20_ERROR_PIPE");
1375 case LIBUSB20_ERROR_INTERRUPTED:
1376 return ("LIBUSB20_ERROR_INTERRUPTED");
1377 case LIBUSB20_ERROR_NO_MEM:
1378 return ("LIBUSB20_ERROR_NO_MEM");
1379 case LIBUSB20_ERROR_NOT_SUPPORTED:
1380 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1381 case LIBUSB20_ERROR_OTHER:
1382 return ("LIBUSB20_ERROR_OTHER");
1384 return ("LIBUSB20_ERROR_UNKNOWN");