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_get_stats (void *)dummy_int
82 #define dummy_kernel_driver_active (void *)dummy_int
83 #define dummy_detach_kernel_driver (void *)dummy_int
84 #define dummy_do_request_sync (void *)dummy_int
85 #define dummy_tr_open (void *)dummy_int
86 #define dummy_tr_close (void *)dummy_int
87 #define dummy_tr_clear_stall_sync (void *)dummy_int
88 #define dummy_process (void *)dummy_int
89 #define dummy_dev_info (void *)dummy_int
90 #define dummy_dev_get_iface_driver (void *)dummy_int
92 #define dummy_tr_submit (void *)dummy_void
93 #define dummy_tr_cancel_async (void *)dummy_void
95 static const struct libusb20_device_methods libusb20_dummy_methods = {
96 LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
100 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
106 if (!xfer->is_pending) {
107 xfer->status = LIBUSB20_TRANSFER_START;
109 xfer->is_pending = 0;
112 xfer->callback(xfer);
114 if (xfer->is_restart) {
115 xfer->is_restart = 0;
118 if (xfer->is_draining &&
119 (!xfer->is_pending)) {
120 xfer->is_draining = 0;
121 xfer->status = LIBUSB20_TRANSFER_DRAINED;
122 xfer->callback(xfer);
128 libusb20_tr_close(struct libusb20_transfer *xfer)
132 if (!xfer->is_opened) {
133 return (LIBUSB20_ERROR_OTHER);
135 error = xfer->pdev->methods->tr_close(xfer);
140 if (xfer->ppBuffer) {
141 free(xfer->ppBuffer);
143 /* reset variable fields in case the transfer is opened again */
144 xfer->priv_sc0 = NULL;
145 xfer->priv_sc1 = NULL;
147 xfer->is_pending = 0;
149 xfer->is_draining = 0;
150 xfer->is_restart = 0;
157 xfer->maxTotalLength = 0;
158 xfer->maxPacketLen = 0;
163 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
164 uint32_t MaxFrameCount, uint8_t ep_no)
166 return (libusb20_tr_open_stream(xfer, MaxBufSize, MaxFrameCount, ep_no, 0));
170 libusb20_tr_open_stream(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
171 uint32_t MaxFrameCount, uint8_t ep_no, uint16_t stream_id)
178 return (LIBUSB20_ERROR_BUSY);
179 if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
180 MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
182 * The kernel can setup 8 times more frames when
183 * pre-scaling ISOCHRONOUS transfers. Make sure the
184 * length and pointer buffers are big enough:
191 if (MaxFrameCount == 0)
192 return (LIBUSB20_ERROR_INVALID_PARAM);
194 xfer->maxFrames = MaxFrameCount;
196 size = MaxFrameCount * sizeof(xfer->pLength[0]);
197 xfer->pLength = malloc(size);
198 if (xfer->pLength == NULL) {
199 return (LIBUSB20_ERROR_NO_MEM);
201 memset(xfer->pLength, 0, size);
203 size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
204 xfer->ppBuffer = malloc(size);
205 if (xfer->ppBuffer == NULL) {
207 return (LIBUSB20_ERROR_NO_MEM);
209 memset(xfer->ppBuffer, 0, size);
212 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
213 MaxFrameCount / 8, ep_no, stream_id, 1);
215 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
216 MaxFrameCount, ep_no, stream_id, 0);
220 free(xfer->ppBuffer);
228 struct libusb20_transfer *
229 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
231 if (trIndex >= pdev->nTransfer) {
234 return (pdev->pTransfer + trIndex);
238 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
240 return (xfer->aFrames);
244 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
246 return (xfer->timeComplete);
250 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
255 for (x = 0; x != xfer->aFrames; x++) {
256 actlen += xfer->pLength[x];
262 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
264 return (xfer->maxFrames);
268 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
271 * Special Case NOTE: If the packet multiplier is non-zero for
272 * High Speed USB, the value returned is equal to
273 * "wMaxPacketSize * multiplier" !
275 return (xfer->maxPacketLen);
279 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
281 return (xfer->maxTotalLength);
285 libusb20_tr_get_status(struct libusb20_transfer *xfer)
287 return (xfer->status);
291 libusb20_tr_pending(struct libusb20_transfer *xfer)
293 return (xfer->is_pending);
297 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
299 return (xfer->priv_sc0);
303 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
305 return (xfer->priv_sc1);
309 libusb20_tr_stop(struct libusb20_transfer *xfer)
311 if (!xfer->is_opened) {
312 /* transfer is not opened */
315 if (!xfer->is_pending) {
316 /* transfer not pending */
319 if (xfer->is_cancel) {
320 /* already cancelling */
323 xfer->is_cancel = 1; /* we are cancelling */
325 xfer->pdev->methods->tr_cancel_async(xfer);
330 libusb20_tr_drain(struct libusb20_transfer *xfer)
332 if (!xfer->is_opened) {
333 /* transfer is not opened */
336 /* make sure that we are cancelling */
337 libusb20_tr_stop(xfer);
339 if (xfer->is_pending) {
340 xfer->is_draining = 1;
346 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
348 xfer->pdev->methods->tr_clear_stall_sync(xfer);
353 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
355 xfer->ppBuffer[frIndex] = buffer;
360 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
367 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
374 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
376 return (xfer->pLength[frIndex]);
380 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
382 xfer->pLength[frIndex] = length;
387 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
389 xfer->priv_sc0 = sc0;
394 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
396 xfer->priv_sc1 = sc1;
401 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
403 xfer->timeout = timeout;
408 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
410 if (nFrames > xfer->maxFrames) {
411 /* should not happen */
412 nFrames = xfer->maxFrames;
414 xfer->nFrames = nFrames;
419 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
421 xfer->ppBuffer[0] = pBuf;
422 xfer->pLength[0] = length;
423 xfer->timeout = timeout;
429 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
433 xfer->ppBuffer[0] = psetup;
434 xfer->pLength[0] = 8; /* fixed */
435 xfer->timeout = timeout;
437 len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
441 xfer->ppBuffer[1] = pBuf;
442 xfer->pLength[1] = len;
450 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
452 xfer->ppBuffer[0] = pBuf;
453 xfer->pLength[0] = length;
454 xfer->timeout = timeout;
460 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
462 if (frIndex >= xfer->maxFrames) {
463 /* should not happen */
466 xfer->ppBuffer[frIndex] = pBuf;
467 xfer->pLength[frIndex] = length;
472 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
473 void *pbuf, uint32_t length, uint32_t *pactlen,
476 struct libusb20_device *pdev = xfer->pdev;
477 uint32_t transfer_max;
478 uint32_t transfer_act;
481 /* set some sensible default value */
485 /* check for error condition */
486 if (libusb20_tr_pending(xfer))
487 return (LIBUSB20_ERROR_OTHER);
490 /* compute maximum transfer length */
492 libusb20_tr_get_max_total_length(xfer);
494 if (transfer_max > length)
495 transfer_max = length;
497 /* setup bulk or interrupt transfer */
498 libusb20_tr_setup_bulk(xfer, pbuf,
499 transfer_max, timeout);
501 /* start the transfer */
502 libusb20_tr_start(xfer);
504 /* wait for transfer completion */
505 while (libusb20_dev_process(pdev) == 0) {
507 if (libusb20_tr_pending(xfer) == 0)
510 libusb20_dev_wait_process(pdev, -1);
513 transfer_act = libusb20_tr_get_actual_length(xfer);
515 /* update actual length, if any */
517 pactlen[0] += transfer_act;
519 /* check transfer status */
520 retval = libusb20_tr_get_status(xfer);
524 /* check for short transfer */
525 if (transfer_act != transfer_max)
528 /* update buffer pointer and length */
529 pbuf = ((uint8_t *)pbuf) + transfer_max;
530 length = length - transfer_max;
532 } while (length != 0);
538 libusb20_tr_submit(struct libusb20_transfer *xfer)
540 if (!xfer->is_opened) {
541 /* transfer is not opened */
544 if (xfer->is_pending) {
545 /* should not happen */
548 xfer->is_pending = 1; /* we are pending */
549 xfer->is_cancel = 0; /* not cancelling */
550 xfer->is_restart = 0; /* not restarting */
552 xfer->pdev->methods->tr_submit(xfer);
557 libusb20_tr_start(struct libusb20_transfer *xfer)
559 if (!xfer->is_opened) {
560 /* transfer is not opened */
563 if (xfer->is_pending) {
564 if (xfer->is_cancel) {
565 /* cancelling - restart */
566 xfer->is_restart = 1;
568 /* transfer not pending */
571 /* get into the callback */
572 libusb20_tr_callback_wrapper(xfer);
576 /* USB device operations */
579 libusb20_dev_close(struct libusb20_device *pdev)
581 struct libusb20_transfer *xfer;
585 if (!pdev->is_opened) {
586 return (LIBUSB20_ERROR_OTHER);
588 for (x = 0; x != pdev->nTransfer; x++) {
589 xfer = pdev->pTransfer + x;
591 if (!xfer->is_opened) {
592 /* transfer is not opened */
596 libusb20_tr_drain(xfer);
598 libusb20_tr_close(xfer);
601 if (pdev->pTransfer != NULL) {
602 free(pdev->pTransfer);
603 pdev->pTransfer = NULL;
605 error = pdev->beMethods->close_device(pdev);
607 pdev->methods = &libusb20_dummy_methods;
612 * Make sure libusb20_tr_get_pointer() fails:
617 * The following variable is only used by the libusb v0.1
620 pdev->claimed_interface = 0;
623 * The following variable is only used by the libusb v1.0
626 pdev->auto_detach = 0;
632 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
636 error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
640 struct LIBUSB20_DEVICE_DESC_DECODED *
641 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
643 return (&(pdev->ddesc));
647 libusb20_dev_get_fd(struct libusb20_device *pdev)
653 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
657 error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
662 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
664 struct libusb20_transfer *xfer;
669 if (pdev->is_opened) {
670 return (LIBUSB20_ERROR_BUSY);
672 if (nTransferMax >= 256) {
673 return (LIBUSB20_ERROR_INVALID_PARAM);
674 } else if (nTransferMax != 0) {
675 size = sizeof(pdev->pTransfer[0]) * nTransferMax;
676 pdev->pTransfer = malloc(size);
677 if (pdev->pTransfer == NULL) {
678 return (LIBUSB20_ERROR_NO_MEM);
680 memset(pdev->pTransfer, 0, size);
682 /* initialise all transfers */
683 for (x = 0; x != nTransferMax; x++) {
685 xfer = pdev->pTransfer + x;
689 xfer->callback = &dummy_callback;
692 /* set "nTransfer" early */
693 pdev->nTransfer = nTransferMax;
695 error = pdev->beMethods->open_device(pdev, nTransferMax);
698 if (pdev->pTransfer != NULL) {
699 free(pdev->pTransfer);
700 pdev->pTransfer = NULL;
703 pdev->file_ctrl = -1;
712 libusb20_dev_reset(struct libusb20_device *pdev)
716 error = pdev->methods->reset_device(pdev);
721 libusb20_dev_check_connected(struct libusb20_device *pdev)
725 error = pdev->methods->check_connected(pdev);
730 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
734 error = pdev->methods->set_power_mode(pdev, power_mode);
739 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
744 error = pdev->methods->get_power_mode(pdev, &power_mode);
746 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
751 libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
754 if (pdev->port_level == 0) {
756 * Fallback for backends without port path:
759 return (LIBUSB20_ERROR_OVERFLOW);
760 buf[0] = pdev->parent_address;
761 buf[1] = pdev->parent_port;
765 /* check if client buffer is too small */
766 if (pdev->port_level > bufsize)
767 return (LIBUSB20_ERROR_OVERFLOW);
769 /* copy port number information */
770 memcpy(buf, pdev->port_path, pdev->port_level);
772 return (pdev->port_level); /* success */
776 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
779 uint16_t power_usage;
781 error = pdev->methods->get_power_usage(pdev, &power_usage);
784 return (power_usage);
788 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
792 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
797 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
801 error = pdev->methods->set_config_index(pdev, configIndex);
806 libusb20_dev_request_sync(struct libusb20_device *pdev,
807 struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
808 uint16_t *pactlen, uint32_t timeout, uint8_t flags)
812 error = pdev->methods->do_request_sync(pdev,
813 setup, data, pactlen, timeout, flags);
818 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
819 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
821 struct LIBUSB20_CONTROL_SETUP_DECODED req;
825 /* make sure memory is initialised */
830 return (LIBUSB20_ERROR_INVALID_PARAM);
832 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
835 * We need to read the USB string in two steps else some USB
836 * devices will complain.
839 LIBUSB20_REQUEST_TYPE_STANDARD |
840 LIBUSB20_RECIPIENT_DEVICE |
841 LIBUSB20_ENDPOINT_IN;
842 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
843 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
845 req.wLength = 4; /* bytes */
847 error = libusb20_dev_request_sync(pdev, &req,
848 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
850 /* try to request full string */
854 /* extract length and request full string */
855 req.wLength = *(uint8_t *)ptr;
856 flags = LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK;
858 if (req.wLength > len) {
859 /* partial string read */
862 error = libusb20_dev_request_sync(pdev, &req, ptr, NULL, 1000, flags);
866 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING)
867 return (LIBUSB20_ERROR_OTHER);
868 return (0); /* success */
872 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
873 uint8_t str_index, void *ptr, uint16_t len)
884 /* the following code derives from the FreeBSD USB kernel */
886 if ((len < 1) || (ptr == NULL)) {
887 /* too short buffer */
888 return (LIBUSB20_ERROR_INVALID_PARAM);
890 error = libusb20_dev_req_string_sync(pdev,
891 0, 0, temp, sizeof(temp));
893 *(uint8_t *)ptr = 0; /* zero terminate */
896 langid = temp[2] | (temp[3] << 8);
898 error = libusb20_dev_req_string_sync(pdev, str_index,
899 langid, temp, sizeof(temp));
901 *(uint8_t *)ptr = 0; /* zero terminate */
905 /* string length is too short */
906 *(uint8_t *)ptr = 0; /* zero terminate */
907 return (LIBUSB20_ERROR_OTHER);
909 /* reserve one byte for terminating zero */
912 /* find maximum length */
913 n = (temp[0] / 2) - 1;
917 /* reset swap state */
920 /* setup output buffer pointer */
923 /* convert and filter */
924 for (i = 0; (i != n); i++) {
925 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
927 /* convert from Unicode, handle buggy strings */
928 if (((c & 0xff00) == 0) && (swap & 1)) {
929 /* Little Endian, default */
932 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
937 /* skip invalid character */
941 * Filter by default - we don't allow greater and less than
942 * signs because they might confuse the dmesg printouts!
944 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
945 /* skip invalid character */
950 *buf = 0; /* zero terminate string */
955 struct libusb20_config *
956 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
958 struct libusb20_config *retval = NULL;
965 * Catch invalid configuration descriptor reads early on to
966 * avoid issues with devices that don't check for a valid USB
967 * configuration read request.
969 if (configIndex >= pdev->ddesc.bNumConfigurations)
972 if (!pdev->is_opened) {
973 error = libusb20_dev_open(pdev, 0);
981 error = pdev->methods->get_config_desc_full(pdev,
982 &ptr, &len, configIndex);
987 /* parse new config descriptor */
988 retval = libusb20_parse_config_desc(ptr);
990 /* free config descriptor */
995 error = libusb20_dev_close(pdev);
1000 struct libusb20_device *
1001 libusb20_dev_alloc(void)
1003 struct libusb20_device *pdev;
1005 pdev = malloc(sizeof(*pdev));
1009 memset(pdev, 0, sizeof(*pdev));
1012 pdev->file_ctrl = -1;
1013 pdev->methods = &libusb20_dummy_methods;
1018 libusb20_dev_get_config_index(struct libusb20_device *pdev)
1024 if (!pdev->is_opened) {
1025 error = libusb20_dev_open(pdev, 0);
1035 error = pdev->methods->get_config_index(pdev, &cfg_index);
1037 cfg_index = 0xFF; /* current config index */
1039 if (libusb20_dev_close(pdev)) {
1047 libusb20_dev_get_mode(struct libusb20_device *pdev)
1049 return (pdev->usb_mode);
1053 libusb20_dev_get_speed(struct libusb20_device *pdev)
1055 return (pdev->usb_speed);
1059 libusb20_dev_get_stats(struct libusb20_device *pdev, struct libusb20_device_stats *pstats)
1064 if (!pdev->is_opened) {
1065 error = libusb20_dev_open(pdev, 0);
1075 error = pdev->methods->get_stats(pdev, pstats);
1078 (void) libusb20_dev_close(pdev);
1083 /* if this function returns an error, the device is gone */
1085 libusb20_dev_process(struct libusb20_device *pdev)
1089 error = pdev->methods->process(pdev);
1094 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1096 struct pollfd pfd[1];
1098 if (!pdev->is_opened) {
1101 pfd[0].fd = pdev->file;
1102 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1105 if (poll(pfd, 1, timeout)) {
1106 /* ignore any error */
1112 libusb20_dev_free(struct libusb20_device *pdev)
1118 if (pdev->is_opened) {
1119 if (libusb20_dev_close(pdev)) {
1120 /* ignore any errors */
1128 libusb20_dev_get_info(struct libusb20_device *pdev,
1129 struct usb_device_info *pinfo)
1132 return (LIBUSB20_ERROR_INVALID_PARAM);
1134 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1138 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1140 return (pdev->beMethods->get_backend_name());
1144 libusb20_dev_get_desc(struct libusb20_device *pdev)
1146 return (pdev->usb_desc);
1150 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1152 pdev->debug = debug;
1157 libusb20_dev_get_debug(struct libusb20_device *pdev)
1159 return (pdev->debug);
1163 libusb20_dev_get_address(struct libusb20_device *pdev)
1165 return (pdev->device_address);
1169 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1171 return (pdev->parent_address);
1175 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1177 return (pdev->parent_port);
1181 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1183 return (pdev->bus_number);
1187 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1188 uint8_t iface_index, char *buf, uint8_t len)
1190 if ((buf == NULL) || (len == 0))
1191 return (LIBUSB20_ERROR_INVALID_PARAM);
1193 buf[0] = 0; /* set default string value */
1195 return (pdev->beMethods->dev_get_iface_desc(
1196 pdev, iface_index, buf, len));
1199 /* USB backend operations */
1202 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1203 uint16_t quirk_index, struct libusb20_quirk *pq)
1205 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1209 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1210 uint16_t quirk_index, struct libusb20_quirk *pq)
1212 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1216 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1217 struct libusb20_quirk *pq)
1219 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1223 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1224 struct libusb20_quirk *pq)
1226 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1230 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1232 return (pbe->methods->root_set_template(pbe, temp));
1236 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1243 return (pbe->methods->root_get_template(pbe, ptemp));
1246 struct libusb20_device *
1247 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1251 } else if (pdev == NULL) {
1252 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1254 pdev = TAILQ_NEXT(pdev, dev_entry);
1259 struct libusb20_backend *
1260 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1262 struct libusb20_backend *pbe;
1264 pbe = malloc(sizeof(*pbe));
1268 memset(pbe, 0, sizeof(*pbe));
1270 TAILQ_INIT(&(pbe->usb_devs));
1272 pbe->methods = methods; /* set backend methods */
1274 /* do the initial device scan */
1275 if (pbe->methods->init_backend) {
1276 pbe->methods->init_backend(pbe);
1281 struct libusb20_backend *
1282 libusb20_be_alloc_linux(void)
1287 struct libusb20_backend *
1288 libusb20_be_alloc_ugen20(void)
1290 return (libusb20_be_alloc(&libusb20_ugen20_backend));
1293 struct libusb20_backend *
1294 libusb20_be_alloc_default(void)
1296 struct libusb20_backend *pbe;
1299 pbe = libusb20_be_alloc_linux();
1304 pbe = libusb20_be_alloc_ugen20();
1308 return (NULL); /* no backend found */
1312 libusb20_be_free(struct libusb20_backend *pbe)
1314 struct libusb20_device *pdev;
1320 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1321 libusb20_be_dequeue_device(pbe, pdev);
1322 libusb20_dev_free(pdev);
1324 if (pbe->methods->exit_backend) {
1325 pbe->methods->exit_backend(pbe);
1332 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1334 pdev->beMethods = pbe->methods; /* copy backend methods */
1335 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1339 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1340 struct libusb20_device *pdev)
1342 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1346 libusb20_strerror(int code)
1349 case LIBUSB20_SUCCESS:
1351 case LIBUSB20_ERROR_IO:
1352 return ("I/O error");
1353 case LIBUSB20_ERROR_INVALID_PARAM:
1354 return ("Invalid parameter");
1355 case LIBUSB20_ERROR_ACCESS:
1356 return ("Permissions error");
1357 case LIBUSB20_ERROR_NO_DEVICE:
1358 return ("No device");
1359 case LIBUSB20_ERROR_NOT_FOUND:
1360 return ("Not found");
1361 case LIBUSB20_ERROR_BUSY:
1362 return ("Device busy");
1363 case LIBUSB20_ERROR_TIMEOUT:
1365 case LIBUSB20_ERROR_OVERFLOW:
1366 return ("Overflow");
1367 case LIBUSB20_ERROR_PIPE:
1368 return ("Pipe error");
1369 case LIBUSB20_ERROR_INTERRUPTED:
1370 return ("Interrupted");
1371 case LIBUSB20_ERROR_NO_MEM:
1372 return ("Out of memory");
1373 case LIBUSB20_ERROR_NOT_SUPPORTED:
1374 return ("Not supported");
1375 case LIBUSB20_ERROR_OTHER:
1376 return ("Other error");
1378 return ("Unknown error");
1383 libusb20_error_name(int code)
1386 case LIBUSB20_SUCCESS:
1387 return ("LIBUSB20_SUCCESS");
1388 case LIBUSB20_ERROR_IO:
1389 return ("LIBUSB20_ERROR_IO");
1390 case LIBUSB20_ERROR_INVALID_PARAM:
1391 return ("LIBUSB20_ERROR_INVALID_PARAM");
1392 case LIBUSB20_ERROR_ACCESS:
1393 return ("LIBUSB20_ERROR_ACCESS");
1394 case LIBUSB20_ERROR_NO_DEVICE:
1395 return ("LIBUSB20_ERROR_NO_DEVICE");
1396 case LIBUSB20_ERROR_NOT_FOUND:
1397 return ("LIBUSB20_ERROR_NOT_FOUND");
1398 case LIBUSB20_ERROR_BUSY:
1399 return ("LIBUSB20_ERROR_BUSY");
1400 case LIBUSB20_ERROR_TIMEOUT:
1401 return ("LIBUSB20_ERROR_TIMEOUT");
1402 case LIBUSB20_ERROR_OVERFLOW:
1403 return ("LIBUSB20_ERROR_OVERFLOW");
1404 case LIBUSB20_ERROR_PIPE:
1405 return ("LIBUSB20_ERROR_PIPE");
1406 case LIBUSB20_ERROR_INTERRUPTED:
1407 return ("LIBUSB20_ERROR_INTERRUPTED");
1408 case LIBUSB20_ERROR_NO_MEM:
1409 return ("LIBUSB20_ERROR_NO_MEM");
1410 case LIBUSB20_ERROR_NOT_SUPPORTED:
1411 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1412 case LIBUSB20_ERROR_OTHER:
1413 return ("LIBUSB20_ERROR_OTHER");
1415 return ("LIBUSB20_ERROR_UNKNOWN");