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_port_path (void *)dummy_int
81 #define dummy_get_power_usage (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)
748 return (pdev->methods->get_port_path(pdev, buf, bufsize));
752 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
755 uint16_t power_usage;
757 error = pdev->methods->get_power_usage(pdev, &power_usage);
760 return (power_usage);
764 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
768 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
773 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
777 error = pdev->methods->set_config_index(pdev, configIndex);
782 libusb20_dev_request_sync(struct libusb20_device *pdev,
783 struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
784 uint16_t *pactlen, uint32_t timeout, uint8_t flags)
788 error = pdev->methods->do_request_sync(pdev,
789 setup, data, pactlen, timeout, flags);
794 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
795 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
797 struct LIBUSB20_CONTROL_SETUP_DECODED req;
800 /* make sure memory is initialised */
805 return (LIBUSB20_ERROR_INVALID_PARAM);
807 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
810 * We need to read the USB string in two steps else some USB
811 * devices will complain.
814 LIBUSB20_REQUEST_TYPE_STANDARD |
815 LIBUSB20_RECIPIENT_DEVICE |
816 LIBUSB20_ENDPOINT_IN;
817 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
818 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
820 req.wLength = 4; /* bytes */
822 error = libusb20_dev_request_sync(pdev, &req,
823 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
827 req.wLength = *(uint8_t *)ptr; /* bytes */
828 if (req.wLength > len) {
829 /* partial string read */
832 error = libusb20_dev_request_sync(pdev, &req,
833 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
838 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) {
839 return (LIBUSB20_ERROR_OTHER);
841 return (0); /* success */
845 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
846 uint8_t str_index, void *ptr, uint16_t len)
857 /* the following code derives from the FreeBSD USB kernel */
859 if ((len < 1) || (ptr == NULL)) {
860 /* too short buffer */
861 return (LIBUSB20_ERROR_INVALID_PARAM);
863 error = libusb20_dev_req_string_sync(pdev,
864 0, 0, temp, sizeof(temp));
866 *(uint8_t *)ptr = 0; /* zero terminate */
869 langid = temp[2] | (temp[3] << 8);
871 error = libusb20_dev_req_string_sync(pdev, str_index,
872 langid, temp, sizeof(temp));
874 *(uint8_t *)ptr = 0; /* zero terminate */
878 /* string length is too short */
879 *(uint8_t *)ptr = 0; /* zero terminate */
880 return (LIBUSB20_ERROR_OTHER);
882 /* reserve one byte for terminating zero */
885 /* find maximum length */
886 n = (temp[0] / 2) - 1;
890 /* reset swap state */
893 /* setup output buffer pointer */
896 /* convert and filter */
897 for (i = 0; (i != n); i++) {
898 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
900 /* convert from Unicode, handle buggy strings */
901 if (((c & 0xff00) == 0) && (swap & 1)) {
902 /* Little Endian, default */
905 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
910 /* skip invalid character */
914 * Filter by default - we don't allow greater and less than
915 * signs because they might confuse the dmesg printouts!
917 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
918 /* skip invalid character */
923 *buf = 0; /* zero terminate string */
928 struct libusb20_config *
929 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
931 struct libusb20_config *retval = NULL;
937 if (!pdev->is_opened) {
938 error = libusb20_dev_open(pdev, 0);
946 error = pdev->methods->get_config_desc_full(pdev,
947 &ptr, &len, configIndex);
952 /* parse new config descriptor */
953 retval = libusb20_parse_config_desc(ptr);
955 /* free config descriptor */
960 error = libusb20_dev_close(pdev);
965 struct libusb20_device *
966 libusb20_dev_alloc(void)
968 struct libusb20_device *pdev;
970 pdev = malloc(sizeof(*pdev));
974 memset(pdev, 0, sizeof(*pdev));
977 pdev->file_ctrl = -1;
978 pdev->methods = &libusb20_dummy_methods;
983 libusb20_dev_get_config_index(struct libusb20_device *pdev)
989 if (!pdev->is_opened) {
990 error = libusb20_dev_open(pdev, 0);
1000 error = pdev->methods->get_config_index(pdev, &cfg_index);
1002 cfg_index = 0xFF; /* current config index */
1004 if (libusb20_dev_close(pdev)) {
1012 libusb20_dev_get_mode(struct libusb20_device *pdev)
1014 return (pdev->usb_mode);
1018 libusb20_dev_get_speed(struct libusb20_device *pdev)
1020 return (pdev->usb_speed);
1023 /* if this function returns an error, the device is gone */
1025 libusb20_dev_process(struct libusb20_device *pdev)
1029 error = pdev->methods->process(pdev);
1034 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1036 struct pollfd pfd[1];
1038 if (!pdev->is_opened) {
1041 pfd[0].fd = pdev->file;
1042 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1045 if (poll(pfd, 1, timeout)) {
1046 /* ignore any error */
1052 libusb20_dev_free(struct libusb20_device *pdev)
1058 if (pdev->is_opened) {
1059 if (libusb20_dev_close(pdev)) {
1060 /* ignore any errors */
1068 libusb20_dev_get_info(struct libusb20_device *pdev,
1069 struct usb_device_info *pinfo)
1072 return (LIBUSB20_ERROR_INVALID_PARAM);
1074 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1078 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1080 return (pdev->beMethods->get_backend_name());
1084 libusb20_dev_get_desc(struct libusb20_device *pdev)
1086 return (pdev->usb_desc);
1090 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1092 pdev->debug = debug;
1097 libusb20_dev_get_debug(struct libusb20_device *pdev)
1099 return (pdev->debug);
1103 libusb20_dev_get_address(struct libusb20_device *pdev)
1105 return (pdev->device_address);
1109 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1111 return (pdev->parent_address);
1115 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1117 return (pdev->parent_port);
1121 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1123 return (pdev->bus_number);
1127 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1128 uint8_t iface_index, char *buf, uint8_t len)
1130 if ((buf == NULL) || (len == 0))
1131 return (LIBUSB20_ERROR_INVALID_PARAM);
1133 buf[0] = 0; /* set default string value */
1135 return (pdev->beMethods->dev_get_iface_desc(
1136 pdev, iface_index, buf, len));
1139 /* USB backend operations */
1142 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1143 uint16_t quirk_index, struct libusb20_quirk *pq)
1145 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1149 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1150 uint16_t quirk_index, struct libusb20_quirk *pq)
1152 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1156 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1157 struct libusb20_quirk *pq)
1159 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1163 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1164 struct libusb20_quirk *pq)
1166 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1170 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1172 return (pbe->methods->root_set_template(pbe, temp));
1176 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1183 return (pbe->methods->root_get_template(pbe, ptemp));
1186 struct libusb20_device *
1187 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1191 } else if (pdev == NULL) {
1192 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1194 pdev = TAILQ_NEXT(pdev, dev_entry);
1199 struct libusb20_backend *
1200 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1202 struct libusb20_backend *pbe;
1204 pbe = malloc(sizeof(*pbe));
1208 memset(pbe, 0, sizeof(*pbe));
1210 TAILQ_INIT(&(pbe->usb_devs));
1212 pbe->methods = methods; /* set backend methods */
1214 /* do the initial device scan */
1215 if (pbe->methods->init_backend) {
1216 pbe->methods->init_backend(pbe);
1221 struct libusb20_backend *
1222 libusb20_be_alloc_linux(void)
1227 struct libusb20_backend *
1228 libusb20_be_alloc_ugen20(void)
1230 return (libusb20_be_alloc(&libusb20_ugen20_backend));
1233 struct libusb20_backend *
1234 libusb20_be_alloc_default(void)
1236 struct libusb20_backend *pbe;
1239 pbe = libusb20_be_alloc_linux();
1244 pbe = libusb20_be_alloc_ugen20();
1248 return (NULL); /* no backend found */
1252 libusb20_be_free(struct libusb20_backend *pbe)
1254 struct libusb20_device *pdev;
1260 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1261 libusb20_be_dequeue_device(pbe, pdev);
1262 libusb20_dev_free(pdev);
1264 if (pbe->methods->exit_backend) {
1265 pbe->methods->exit_backend(pbe);
1272 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1274 pdev->beMethods = pbe->methods; /* copy backend methods */
1275 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1279 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1280 struct libusb20_device *pdev)
1282 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1286 libusb20_strerror(int code)
1289 case LIBUSB20_SUCCESS:
1291 case LIBUSB20_ERROR_IO:
1292 return ("I/O error");
1293 case LIBUSB20_ERROR_INVALID_PARAM:
1294 return ("Invalid parameter");
1295 case LIBUSB20_ERROR_ACCESS:
1296 return ("Permissions error");
1297 case LIBUSB20_ERROR_NO_DEVICE:
1298 return ("No device");
1299 case LIBUSB20_ERROR_NOT_FOUND:
1300 return ("Not found");
1301 case LIBUSB20_ERROR_BUSY:
1302 return ("Device busy");
1303 case LIBUSB20_ERROR_TIMEOUT:
1305 case LIBUSB20_ERROR_OVERFLOW:
1306 return ("Overflow");
1307 case LIBUSB20_ERROR_PIPE:
1308 return ("Pipe error");
1309 case LIBUSB20_ERROR_INTERRUPTED:
1310 return ("Interrupted");
1311 case LIBUSB20_ERROR_NO_MEM:
1312 return ("Out of memory");
1313 case LIBUSB20_ERROR_NOT_SUPPORTED:
1314 return ("Not supported");
1315 case LIBUSB20_ERROR_OTHER:
1316 return ("Other error");
1318 return ("Unknown error");
1323 libusb20_error_name(int code)
1326 case LIBUSB20_SUCCESS:
1327 return ("LIBUSB20_SUCCESS");
1328 case LIBUSB20_ERROR_IO:
1329 return ("LIBUSB20_ERROR_IO");
1330 case LIBUSB20_ERROR_INVALID_PARAM:
1331 return ("LIBUSB20_ERROR_INVALID_PARAM");
1332 case LIBUSB20_ERROR_ACCESS:
1333 return ("LIBUSB20_ERROR_ACCESS");
1334 case LIBUSB20_ERROR_NO_DEVICE:
1335 return ("LIBUSB20_ERROR_NO_DEVICE");
1336 case LIBUSB20_ERROR_NOT_FOUND:
1337 return ("LIBUSB20_ERROR_NOT_FOUND");
1338 case LIBUSB20_ERROR_BUSY:
1339 return ("LIBUSB20_ERROR_BUSY");
1340 case LIBUSB20_ERROR_TIMEOUT:
1341 return ("LIBUSB20_ERROR_TIMEOUT");
1342 case LIBUSB20_ERROR_OVERFLOW:
1343 return ("LIBUSB20_ERROR_OVERFLOW");
1344 case LIBUSB20_ERROR_PIPE:
1345 return ("LIBUSB20_ERROR_PIPE");
1346 case LIBUSB20_ERROR_INTERRUPTED:
1347 return ("LIBUSB20_ERROR_INTERRUPTED");
1348 case LIBUSB20_ERROR_NO_MEM:
1349 return ("LIBUSB20_ERROR_NO_MEM");
1350 case LIBUSB20_ERROR_NOT_SUPPORTED:
1351 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1352 case LIBUSB20_ERROR_OTHER:
1353 return ("LIBUSB20_ERROR_OTHER");
1355 return ("LIBUSB20_ERROR_UNKNOWN");