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] = libusb20_pass_ptr(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] = libusb20_pass_ptr(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] = libusb20_pass_ptr(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] = libusb20_pass_ptr(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] = libusb20_pass_ptr(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] = libusb20_pass_ptr(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 * The following variable is only used by the libusb v0.1
615 pdev->claimed_interface = 0;
618 * The following variable is only used by the libusb v1.0
621 pdev->auto_detach = 0;
627 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
631 error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
635 struct LIBUSB20_DEVICE_DESC_DECODED *
636 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
638 return (&(pdev->ddesc));
642 libusb20_dev_get_fd(struct libusb20_device *pdev)
648 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
652 error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
657 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
659 struct libusb20_transfer *xfer;
664 if (pdev->is_opened) {
665 return (LIBUSB20_ERROR_BUSY);
667 if (nTransferMax >= 256) {
668 return (LIBUSB20_ERROR_INVALID_PARAM);
669 } else if (nTransferMax != 0) {
670 size = sizeof(pdev->pTransfer[0]) * nTransferMax;
671 pdev->pTransfer = malloc(size);
672 if (pdev->pTransfer == NULL) {
673 return (LIBUSB20_ERROR_NO_MEM);
675 memset(pdev->pTransfer, 0, size);
677 /* initialise all transfers */
678 for (x = 0; x != nTransferMax; x++) {
680 xfer = pdev->pTransfer + x;
684 xfer->callback = &dummy_callback;
687 /* set "nTransfer" early */
688 pdev->nTransfer = nTransferMax;
690 error = pdev->beMethods->open_device(pdev, nTransferMax);
693 if (pdev->pTransfer != NULL) {
694 free(pdev->pTransfer);
695 pdev->pTransfer = NULL;
698 pdev->file_ctrl = -1;
707 libusb20_dev_reset(struct libusb20_device *pdev)
711 error = pdev->methods->reset_device(pdev);
716 libusb20_dev_check_connected(struct libusb20_device *pdev)
720 error = pdev->methods->check_connected(pdev);
725 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
729 error = pdev->methods->set_power_mode(pdev, power_mode);
734 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
739 error = pdev->methods->get_power_mode(pdev, &power_mode);
741 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
746 libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
749 if (pdev->port_level == 0) {
751 * Fallback for backends without port path:
754 return (LIBUSB20_ERROR_OVERFLOW);
755 buf[0] = pdev->parent_address;
756 buf[1] = pdev->parent_port;
760 /* check if client buffer is too small */
761 if (pdev->port_level > bufsize)
762 return (LIBUSB20_ERROR_OVERFLOW);
764 /* copy port number information */
765 memcpy(buf, pdev->port_path, pdev->port_level);
767 return (pdev->port_level); /* success */
771 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
774 uint16_t power_usage;
776 error = pdev->methods->get_power_usage(pdev, &power_usage);
779 return (power_usage);
783 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
787 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
792 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
796 error = pdev->methods->set_config_index(pdev, configIndex);
801 libusb20_dev_request_sync(struct libusb20_device *pdev,
802 struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
803 uint16_t *pactlen, uint32_t timeout, uint8_t flags)
807 error = pdev->methods->do_request_sync(pdev,
808 setup, data, pactlen, timeout, flags);
813 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
814 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
816 struct LIBUSB20_CONTROL_SETUP_DECODED req;
820 /* make sure memory is initialised */
825 return (LIBUSB20_ERROR_INVALID_PARAM);
827 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
830 * We need to read the USB string in two steps else some USB
831 * devices will complain.
834 LIBUSB20_REQUEST_TYPE_STANDARD |
835 LIBUSB20_RECIPIENT_DEVICE |
836 LIBUSB20_ENDPOINT_IN;
837 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
838 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
840 req.wLength = 4; /* bytes */
842 error = libusb20_dev_request_sync(pdev, &req,
843 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
845 /* try to request full string */
849 /* extract length and request full string */
850 req.wLength = *(uint8_t *)ptr;
851 flags = LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK;
853 if (req.wLength > len) {
854 /* partial string read */
857 error = libusb20_dev_request_sync(pdev, &req, ptr, NULL, 1000, flags);
861 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING)
862 return (LIBUSB20_ERROR_OTHER);
863 return (0); /* success */
867 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
868 uint8_t str_index, void *ptr, uint16_t len)
879 /* the following code derives from the FreeBSD USB kernel */
881 if ((len < 1) || (ptr == NULL)) {
882 /* too short buffer */
883 return (LIBUSB20_ERROR_INVALID_PARAM);
885 error = libusb20_dev_req_string_sync(pdev,
886 0, 0, temp, sizeof(temp));
888 *(uint8_t *)ptr = 0; /* zero terminate */
891 langid = temp[2] | (temp[3] << 8);
893 error = libusb20_dev_req_string_sync(pdev, str_index,
894 langid, temp, sizeof(temp));
896 *(uint8_t *)ptr = 0; /* zero terminate */
900 /* string length is too short */
901 *(uint8_t *)ptr = 0; /* zero terminate */
902 return (LIBUSB20_ERROR_OTHER);
904 /* reserve one byte for terminating zero */
907 /* find maximum length */
908 n = (temp[0] / 2) - 1;
912 /* reset swap state */
915 /* setup output buffer pointer */
918 /* convert and filter */
919 for (i = 0; (i != n); i++) {
920 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
922 /* convert from Unicode, handle buggy strings */
923 if (((c & 0xff00) == 0) && (swap & 1)) {
924 /* Little Endian, default */
927 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
932 /* skip invalid character */
936 * Filter by default - we don't allow greater and less than
937 * signs because they might confuse the dmesg printouts!
939 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
940 /* skip invalid character */
945 *buf = 0; /* zero terminate string */
950 struct libusb20_config *
951 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
953 struct libusb20_config *retval = NULL;
960 * Catch invalid configuration descriptor reads early on to
961 * avoid issues with devices that don't check for a valid USB
962 * configuration read request.
964 if (configIndex >= pdev->ddesc.bNumConfigurations)
967 if (!pdev->is_opened) {
968 error = libusb20_dev_open(pdev, 0);
976 error = pdev->methods->get_config_desc_full(pdev,
977 &ptr, &len, configIndex);
982 /* parse new config descriptor */
983 retval = libusb20_parse_config_desc(ptr);
985 /* free config descriptor */
990 error = libusb20_dev_close(pdev);
995 struct libusb20_device *
996 libusb20_dev_alloc(void)
998 struct libusb20_device *pdev;
1000 pdev = malloc(sizeof(*pdev));
1004 memset(pdev, 0, sizeof(*pdev));
1007 pdev->file_ctrl = -1;
1008 pdev->methods = &libusb20_dummy_methods;
1013 libusb20_dev_get_config_index(struct libusb20_device *pdev)
1019 if (!pdev->is_opened) {
1020 error = libusb20_dev_open(pdev, 0);
1030 error = pdev->methods->get_config_index(pdev, &cfg_index);
1032 cfg_index = 0xFF; /* current config index */
1034 if (libusb20_dev_close(pdev)) {
1042 libusb20_dev_get_mode(struct libusb20_device *pdev)
1044 return (pdev->usb_mode);
1048 libusb20_dev_get_speed(struct libusb20_device *pdev)
1050 return (pdev->usb_speed);
1054 libusb20_dev_get_stats(struct libusb20_device *pdev, struct libusb20_device_stats *pstats)
1059 if (!pdev->is_opened) {
1060 error = libusb20_dev_open(pdev, 0);
1070 error = pdev->methods->get_stats(pdev, pstats);
1073 (void) libusb20_dev_close(pdev);
1078 /* if this function returns an error, the device is gone */
1080 libusb20_dev_process(struct libusb20_device *pdev)
1084 error = pdev->methods->process(pdev);
1089 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1091 struct pollfd pfd[1];
1093 if (!pdev->is_opened) {
1096 pfd[0].fd = pdev->file;
1097 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1100 if (poll(pfd, 1, timeout)) {
1101 /* ignore any error */
1107 libusb20_dev_free(struct libusb20_device *pdev)
1113 if (pdev->is_opened) {
1114 if (libusb20_dev_close(pdev)) {
1115 /* ignore any errors */
1123 libusb20_dev_get_info(struct libusb20_device *pdev,
1124 struct usb_device_info *pinfo)
1127 return (LIBUSB20_ERROR_INVALID_PARAM);
1129 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1133 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1135 return (pdev->beMethods->get_backend_name());
1139 libusb20_dev_get_desc(struct libusb20_device *pdev)
1141 return (pdev->usb_desc);
1145 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1147 pdev->debug = debug;
1152 libusb20_dev_get_debug(struct libusb20_device *pdev)
1154 return (pdev->debug);
1158 libusb20_dev_get_address(struct libusb20_device *pdev)
1160 return (pdev->device_address);
1164 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1166 return (pdev->parent_address);
1170 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1172 return (pdev->parent_port);
1176 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1178 return (pdev->bus_number);
1182 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1183 uint8_t iface_index, char *buf, uint8_t len)
1185 if ((buf == NULL) || (len == 0))
1186 return (LIBUSB20_ERROR_INVALID_PARAM);
1188 buf[0] = 0; /* set default string value */
1190 return (pdev->beMethods->dev_get_iface_desc(
1191 pdev, iface_index, buf, len));
1194 /* USB backend operations */
1197 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1198 uint16_t quirk_index, struct libusb20_quirk *pq)
1200 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1204 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1205 uint16_t quirk_index, struct libusb20_quirk *pq)
1207 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1211 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1212 struct libusb20_quirk *pq)
1214 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1218 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1219 struct libusb20_quirk *pq)
1221 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1225 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1227 return (pbe->methods->root_set_template(pbe, temp));
1231 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1238 return (pbe->methods->root_get_template(pbe, ptemp));
1241 struct libusb20_device *
1242 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1246 } else if (pdev == NULL) {
1247 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1249 pdev = TAILQ_NEXT(pdev, dev_entry);
1254 struct libusb20_backend *
1255 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1257 struct libusb20_backend *pbe;
1259 pbe = malloc(sizeof(*pbe));
1263 memset(pbe, 0, sizeof(*pbe));
1265 TAILQ_INIT(&(pbe->usb_devs));
1267 pbe->methods = methods; /* set backend methods */
1269 /* do the initial device scan */
1270 if (pbe->methods->init_backend) {
1271 pbe->methods->init_backend(pbe);
1276 struct libusb20_backend *
1277 libusb20_be_alloc_linux(void)
1282 struct libusb20_backend *
1283 libusb20_be_alloc_ugen20(void)
1285 return (libusb20_be_alloc(&libusb20_ugen20_backend));
1288 struct libusb20_backend *
1289 libusb20_be_alloc_default(void)
1291 struct libusb20_backend *pbe;
1294 pbe = libusb20_be_alloc_linux();
1299 pbe = libusb20_be_alloc_ugen20();
1303 return (NULL); /* no backend found */
1307 libusb20_be_free(struct libusb20_backend *pbe)
1309 struct libusb20_device *pdev;
1315 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1316 libusb20_be_dequeue_device(pbe, pdev);
1317 libusb20_dev_free(pdev);
1319 if (pbe->methods->exit_backend) {
1320 pbe->methods->exit_backend(pbe);
1327 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1329 pdev->beMethods = pbe->methods; /* copy backend methods */
1330 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1334 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1335 struct libusb20_device *pdev)
1337 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1341 libusb20_strerror(int code)
1344 case LIBUSB20_SUCCESS:
1346 case LIBUSB20_ERROR_IO:
1347 return ("I/O error");
1348 case LIBUSB20_ERROR_INVALID_PARAM:
1349 return ("Invalid parameter");
1350 case LIBUSB20_ERROR_ACCESS:
1351 return ("Permissions error");
1352 case LIBUSB20_ERROR_NO_DEVICE:
1353 return ("No device");
1354 case LIBUSB20_ERROR_NOT_FOUND:
1355 return ("Not found");
1356 case LIBUSB20_ERROR_BUSY:
1357 return ("Device busy");
1358 case LIBUSB20_ERROR_TIMEOUT:
1360 case LIBUSB20_ERROR_OVERFLOW:
1361 return ("Overflow");
1362 case LIBUSB20_ERROR_PIPE:
1363 return ("Pipe error");
1364 case LIBUSB20_ERROR_INTERRUPTED:
1365 return ("Interrupted");
1366 case LIBUSB20_ERROR_NO_MEM:
1367 return ("Out of memory");
1368 case LIBUSB20_ERROR_NOT_SUPPORTED:
1369 return ("Not supported");
1370 case LIBUSB20_ERROR_OTHER:
1371 return ("Other error");
1373 return ("Unknown error");
1378 libusb20_error_name(int code)
1381 case LIBUSB20_SUCCESS:
1382 return ("LIBUSB20_SUCCESS");
1383 case LIBUSB20_ERROR_IO:
1384 return ("LIBUSB20_ERROR_IO");
1385 case LIBUSB20_ERROR_INVALID_PARAM:
1386 return ("LIBUSB20_ERROR_INVALID_PARAM");
1387 case LIBUSB20_ERROR_ACCESS:
1388 return ("LIBUSB20_ERROR_ACCESS");
1389 case LIBUSB20_ERROR_NO_DEVICE:
1390 return ("LIBUSB20_ERROR_NO_DEVICE");
1391 case LIBUSB20_ERROR_NOT_FOUND:
1392 return ("LIBUSB20_ERROR_NOT_FOUND");
1393 case LIBUSB20_ERROR_BUSY:
1394 return ("LIBUSB20_ERROR_BUSY");
1395 case LIBUSB20_ERROR_TIMEOUT:
1396 return ("LIBUSB20_ERROR_TIMEOUT");
1397 case LIBUSB20_ERROR_OVERFLOW:
1398 return ("LIBUSB20_ERROR_OVERFLOW");
1399 case LIBUSB20_ERROR_PIPE:
1400 return ("LIBUSB20_ERROR_PIPE");
1401 case LIBUSB20_ERROR_INTERRUPTED:
1402 return ("LIBUSB20_ERROR_INTERRUPTED");
1403 case LIBUSB20_ERROR_NO_MEM:
1404 return ("LIBUSB20_ERROR_NO_MEM");
1405 case LIBUSB20_ERROR_NOT_SUPPORTED:
1406 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1407 case LIBUSB20_ERROR_OTHER:
1408 return ("LIBUSB20_ERROR_OTHER");
1410 return ("LIBUSB20_ERROR_UNKNOWN");