3 * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
28 #include LIBUSB_GLOBAL_INCLUDE_FILE
36 #include <sys/queue.h>
40 #include "libusb20_desc.h"
41 #include "libusb20_int.h"
46 return (LIBUSB20_ERROR_NOT_SUPPORTED);
56 dummy_callback(struct libusb20_transfer *xfer)
59 switch (libusb20_tr_get_status(xfer)) {
60 case LIBUSB20_TRANSFER_START:
61 libusb20_tr_submit(xfer);
64 /* complete or error */
70 #define dummy_get_config_desc_full (void *)dummy_int
71 #define dummy_get_config_index (void *)dummy_int
72 #define dummy_set_config_index (void *)dummy_int
73 #define dummy_set_alt_index (void *)dummy_int
74 #define dummy_reset_device (void *)dummy_int
75 #define dummy_check_connected (void *)dummy_int
76 #define dummy_set_power_mode (void *)dummy_int
77 #define dummy_get_power_mode (void *)dummy_int
78 #define dummy_get_port_path (void *)dummy_int
79 #define dummy_get_power_usage (void *)dummy_int
80 #define dummy_kernel_driver_active (void *)dummy_int
81 #define dummy_detach_kernel_driver (void *)dummy_int
82 #define dummy_do_request_sync (void *)dummy_int
83 #define dummy_tr_open (void *)dummy_int
84 #define dummy_tr_close (void *)dummy_int
85 #define dummy_tr_clear_stall_sync (void *)dummy_int
86 #define dummy_process (void *)dummy_int
87 #define dummy_dev_info (void *)dummy_int
88 #define dummy_dev_get_iface_driver (void *)dummy_int
90 #define dummy_tr_submit (void *)dummy_void
91 #define dummy_tr_cancel_async (void *)dummy_void
93 static const struct libusb20_device_methods libusb20_dummy_methods = {
94 LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
98 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
104 if (!xfer->is_pending) {
105 xfer->status = LIBUSB20_TRANSFER_START;
107 xfer->is_pending = 0;
110 xfer->callback(xfer);
112 if (xfer->is_restart) {
113 xfer->is_restart = 0;
116 if (xfer->is_draining &&
117 (!xfer->is_pending)) {
118 xfer->is_draining = 0;
119 xfer->status = LIBUSB20_TRANSFER_DRAINED;
120 xfer->callback(xfer);
126 libusb20_tr_close(struct libusb20_transfer *xfer)
130 if (!xfer->is_opened) {
131 return (LIBUSB20_ERROR_OTHER);
133 error = xfer->pdev->methods->tr_close(xfer);
138 if (xfer->ppBuffer) {
139 free(xfer->ppBuffer);
141 /* reset variable fields in case the transfer is opened again */
145 xfer->is_pending = 0;
147 xfer->is_draining = 0;
148 xfer->is_restart = 0;
155 xfer->maxTotalLength = 0;
156 xfer->maxPacketLen = 0;
161 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
162 uint32_t MaxFrameCount, uint8_t ep_no)
164 return (libusb20_tr_open_stream(xfer, MaxBufSize, MaxFrameCount, ep_no, 0));
168 libusb20_tr_open_stream(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
169 uint32_t MaxFrameCount, uint8_t ep_no, uint16_t stream_id)
176 return (LIBUSB20_ERROR_BUSY);
177 if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
178 MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
180 * The kernel can setup 8 times more frames when
181 * pre-scaling ISOCHRONOUS transfers. Make sure the
182 * length and pointer buffers are big enough:
189 if (MaxFrameCount == 0)
190 return (LIBUSB20_ERROR_INVALID_PARAM);
192 xfer->maxFrames = MaxFrameCount;
194 size = MaxFrameCount * sizeof(xfer->pLength[0]);
195 xfer->pLength = malloc(size);
196 if (xfer->pLength == NULL) {
197 return (LIBUSB20_ERROR_NO_MEM);
199 memset(xfer->pLength, 0, size);
201 size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
202 xfer->ppBuffer = malloc(size);
203 if (xfer->ppBuffer == NULL) {
205 return (LIBUSB20_ERROR_NO_MEM);
207 memset(xfer->ppBuffer, 0, size);
210 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
211 MaxFrameCount / 8, ep_no, stream_id, 1);
213 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
214 MaxFrameCount, ep_no, stream_id, 0);
218 free(xfer->ppBuffer);
226 struct libusb20_transfer *
227 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
229 if (trIndex >= pdev->nTransfer) {
232 return (pdev->pTransfer + trIndex);
236 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
238 return (xfer->aFrames);
242 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
244 return (xfer->timeComplete);
248 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
253 for (x = 0; x != xfer->aFrames; x++) {
254 actlen += xfer->pLength[x];
260 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
262 return (xfer->maxFrames);
266 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
269 * Special Case NOTE: If the packet multiplier is non-zero for
270 * High Speed USB, the value returned is equal to
271 * "wMaxPacketSize * multiplier" !
273 return (xfer->maxPacketLen);
277 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
279 return (xfer->maxTotalLength);
283 libusb20_tr_get_status(struct libusb20_transfer *xfer)
285 return (xfer->status);
289 libusb20_tr_pending(struct libusb20_transfer *xfer)
291 return (xfer->is_pending);
295 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
297 return (xfer->priv_sc0);
301 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
303 return (xfer->priv_sc1);
307 libusb20_tr_stop(struct libusb20_transfer *xfer)
309 if (!xfer->is_opened) {
310 /* transfer is not opened */
313 if (!xfer->is_pending) {
314 /* transfer not pending */
317 if (xfer->is_cancel) {
318 /* already cancelling */
321 xfer->is_cancel = 1; /* we are cancelling */
323 xfer->pdev->methods->tr_cancel_async(xfer);
328 libusb20_tr_drain(struct libusb20_transfer *xfer)
330 if (!xfer->is_opened) {
331 /* transfer is not opened */
334 /* make sure that we are cancelling */
335 libusb20_tr_stop(xfer);
337 if (xfer->is_pending) {
338 xfer->is_draining = 1;
344 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
346 xfer->pdev->methods->tr_clear_stall_sync(xfer);
351 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
353 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(buffer);
358 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
365 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
372 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
374 return (xfer->pLength[frIndex]);
378 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
380 xfer->pLength[frIndex] = length;
385 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
387 xfer->priv_sc0 = sc0;
392 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
394 xfer->priv_sc1 = sc1;
399 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
401 xfer->timeout = timeout;
406 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
408 if (nFrames > xfer->maxFrames) {
409 /* should not happen */
410 nFrames = xfer->maxFrames;
412 xfer->nFrames = nFrames;
417 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
419 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
420 xfer->pLength[0] = length;
421 xfer->timeout = timeout;
427 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
431 xfer->ppBuffer[0] = libusb20_pass_ptr(psetup);
432 xfer->pLength[0] = 8; /* fixed */
433 xfer->timeout = timeout;
435 len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
439 xfer->ppBuffer[1] = libusb20_pass_ptr(pBuf);
440 xfer->pLength[1] = len;
448 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
450 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
451 xfer->pLength[0] = length;
452 xfer->timeout = timeout;
458 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
460 if (frIndex >= xfer->maxFrames) {
461 /* should not happen */
464 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(pBuf);
465 xfer->pLength[frIndex] = length;
470 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
471 void *pbuf, uint32_t length, uint32_t *pactlen,
474 struct libusb20_device *pdev = xfer->pdev;
475 uint32_t transfer_max;
476 uint32_t transfer_act;
479 /* set some sensible default value */
483 /* check for error condition */
484 if (libusb20_tr_pending(xfer))
485 return (LIBUSB20_ERROR_OTHER);
488 /* compute maximum transfer length */
490 libusb20_tr_get_max_total_length(xfer);
492 if (transfer_max > length)
493 transfer_max = length;
495 /* setup bulk or interrupt transfer */
496 libusb20_tr_setup_bulk(xfer, pbuf,
497 transfer_max, timeout);
499 /* start the transfer */
500 libusb20_tr_start(xfer);
502 /* wait for transfer completion */
503 while (libusb20_dev_process(pdev) == 0) {
505 if (libusb20_tr_pending(xfer) == 0)
508 libusb20_dev_wait_process(pdev, -1);
511 transfer_act = libusb20_tr_get_actual_length(xfer);
513 /* update actual length, if any */
515 pactlen[0] += transfer_act;
517 /* check transfer status */
518 retval = libusb20_tr_get_status(xfer);
522 /* check for short transfer */
523 if (transfer_act != transfer_max)
526 /* update buffer pointer and length */
527 pbuf = ((uint8_t *)pbuf) + transfer_max;
528 length = length - transfer_max;
530 } while (length != 0);
536 libusb20_tr_submit(struct libusb20_transfer *xfer)
538 if (!xfer->is_opened) {
539 /* transfer is not opened */
542 if (xfer->is_pending) {
543 /* should not happen */
546 xfer->is_pending = 1; /* we are pending */
547 xfer->is_cancel = 0; /* not cancelling */
548 xfer->is_restart = 0; /* not restarting */
550 xfer->pdev->methods->tr_submit(xfer);
555 libusb20_tr_start(struct libusb20_transfer *xfer)
557 if (!xfer->is_opened) {
558 /* transfer is not opened */
561 if (xfer->is_pending) {
562 if (xfer->is_cancel) {
563 /* cancelling - restart */
564 xfer->is_restart = 1;
566 /* transfer not pending */
569 /* get into the callback */
570 libusb20_tr_callback_wrapper(xfer);
574 /* USB device operations */
577 libusb20_dev_close(struct libusb20_device *pdev)
579 struct libusb20_transfer *xfer;
583 if (!pdev->is_opened) {
584 return (LIBUSB20_ERROR_OTHER);
586 for (x = 0; x != pdev->nTransfer; x++) {
587 xfer = pdev->pTransfer + x;
589 if (!xfer->is_opened) {
590 /* transfer is not opened */
594 libusb20_tr_drain(xfer);
596 libusb20_tr_close(xfer);
599 if (pdev->pTransfer != NULL) {
600 free(pdev->pTransfer);
601 pdev->pTransfer = NULL;
603 error = pdev->beMethods->close_device(pdev);
605 pdev->methods = &libusb20_dummy_methods;
610 * The following variable is only used by the libusb v0.1
613 pdev->claimed_interface = 0;
616 * The following variable is only used by the libusb v1.0
619 pdev->auto_detach = 0;
625 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
629 error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
633 struct LIBUSB20_DEVICE_DESC_DECODED *
634 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
636 return (&(pdev->ddesc));
640 libusb20_dev_get_fd(struct libusb20_device *pdev)
646 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
650 error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
655 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
657 struct libusb20_transfer *xfer;
662 if (pdev->is_opened) {
663 return (LIBUSB20_ERROR_BUSY);
665 if (nTransferMax >= 256) {
666 return (LIBUSB20_ERROR_INVALID_PARAM);
667 } else if (nTransferMax != 0) {
668 size = sizeof(pdev->pTransfer[0]) * nTransferMax;
669 pdev->pTransfer = malloc(size);
670 if (pdev->pTransfer == NULL) {
671 return (LIBUSB20_ERROR_NO_MEM);
673 memset(pdev->pTransfer, 0, size);
675 /* initialise all transfers */
676 for (x = 0; x != nTransferMax; x++) {
678 xfer = pdev->pTransfer + x;
682 xfer->callback = &dummy_callback;
685 /* set "nTransfer" early */
686 pdev->nTransfer = nTransferMax;
688 error = pdev->beMethods->open_device(pdev, nTransferMax);
691 if (pdev->pTransfer != NULL) {
692 free(pdev->pTransfer);
693 pdev->pTransfer = NULL;
696 pdev->file_ctrl = -1;
705 libusb20_dev_reset(struct libusb20_device *pdev)
709 error = pdev->methods->reset_device(pdev);
714 libusb20_dev_check_connected(struct libusb20_device *pdev)
718 error = pdev->methods->check_connected(pdev);
723 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
727 error = pdev->methods->set_power_mode(pdev, power_mode);
732 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
737 error = pdev->methods->get_power_mode(pdev, &power_mode);
739 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
744 libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
746 return (pdev->methods->get_port_path(pdev, buf, bufsize));
750 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
753 uint16_t power_usage;
755 error = pdev->methods->get_power_usage(pdev, &power_usage);
758 return (power_usage);
762 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
766 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
771 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
775 error = pdev->methods->set_config_index(pdev, configIndex);
780 libusb20_dev_request_sync(struct libusb20_device *pdev,
781 struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
782 uint16_t *pactlen, uint32_t timeout, uint8_t flags)
786 error = pdev->methods->do_request_sync(pdev,
787 setup, data, pactlen, timeout, flags);
792 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
793 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
795 struct LIBUSB20_CONTROL_SETUP_DECODED req;
799 /* make sure memory is initialised */
804 return (LIBUSB20_ERROR_INVALID_PARAM);
806 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
809 * We need to read the USB string in two steps else some USB
810 * devices will complain.
813 LIBUSB20_REQUEST_TYPE_STANDARD |
814 LIBUSB20_RECIPIENT_DEVICE |
815 LIBUSB20_ENDPOINT_IN;
816 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
817 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
819 req.wLength = 4; /* bytes */
821 error = libusb20_dev_request_sync(pdev, &req,
822 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
824 /* try to request full string */
828 /* extract length and request full string */
829 req.wLength = *(uint8_t *)ptr;
830 flags = LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK;
832 if (req.wLength > len) {
833 /* partial string read */
836 error = libusb20_dev_request_sync(pdev, &req, ptr, NULL, 1000, flags);
840 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING)
841 return (LIBUSB20_ERROR_OTHER);
842 return (0); /* success */
846 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
847 uint8_t str_index, void *ptr, uint16_t len)
858 /* the following code derives from the FreeBSD USB kernel */
860 if ((len < 1) || (ptr == NULL)) {
861 /* too short buffer */
862 return (LIBUSB20_ERROR_INVALID_PARAM);
864 error = libusb20_dev_req_string_sync(pdev,
865 0, 0, temp, sizeof(temp));
867 *(uint8_t *)ptr = 0; /* zero terminate */
870 langid = temp[2] | (temp[3] << 8);
872 error = libusb20_dev_req_string_sync(pdev, str_index,
873 langid, temp, sizeof(temp));
875 *(uint8_t *)ptr = 0; /* zero terminate */
879 /* string length is too short */
880 *(uint8_t *)ptr = 0; /* zero terminate */
881 return (LIBUSB20_ERROR_OTHER);
883 /* reserve one byte for terminating zero */
886 /* find maximum length */
887 n = (temp[0] / 2) - 1;
891 /* reset swap state */
894 /* setup output buffer pointer */
897 /* convert and filter */
898 for (i = 0; (i != n); i++) {
899 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
901 /* convert from Unicode, handle buggy strings */
902 if (((c & 0xff00) == 0) && (swap & 1)) {
903 /* Little Endian, default */
906 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
911 /* skip invalid character */
915 * Filter by default - we don't allow greater and less than
916 * signs because they might confuse the dmesg printouts!
918 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
919 /* skip invalid character */
924 *buf = 0; /* zero terminate string */
929 struct libusb20_config *
930 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
932 struct libusb20_config *retval = NULL;
938 if (!pdev->is_opened) {
939 error = libusb20_dev_open(pdev, 0);
947 error = pdev->methods->get_config_desc_full(pdev,
948 &ptr, &len, configIndex);
953 /* parse new config descriptor */
954 retval = libusb20_parse_config_desc(ptr);
956 /* free config descriptor */
961 error = libusb20_dev_close(pdev);
966 struct libusb20_device *
967 libusb20_dev_alloc(void)
969 struct libusb20_device *pdev;
971 pdev = malloc(sizeof(*pdev));
975 memset(pdev, 0, sizeof(*pdev));
978 pdev->file_ctrl = -1;
979 pdev->methods = &libusb20_dummy_methods;
984 libusb20_dev_get_config_index(struct libusb20_device *pdev)
990 if (!pdev->is_opened) {
991 error = libusb20_dev_open(pdev, 0);
1001 error = pdev->methods->get_config_index(pdev, &cfg_index);
1003 cfg_index = 0xFF; /* current config index */
1005 if (libusb20_dev_close(pdev)) {
1013 libusb20_dev_get_mode(struct libusb20_device *pdev)
1015 return (pdev->usb_mode);
1019 libusb20_dev_get_speed(struct libusb20_device *pdev)
1021 return (pdev->usb_speed);
1024 /* if this function returns an error, the device is gone */
1026 libusb20_dev_process(struct libusb20_device *pdev)
1030 error = pdev->methods->process(pdev);
1035 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1037 struct pollfd pfd[1];
1039 if (!pdev->is_opened) {
1042 pfd[0].fd = pdev->file;
1043 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1046 if (poll(pfd, 1, timeout)) {
1047 /* ignore any error */
1053 libusb20_dev_free(struct libusb20_device *pdev)
1059 if (pdev->is_opened) {
1060 if (libusb20_dev_close(pdev)) {
1061 /* ignore any errors */
1069 libusb20_dev_get_info(struct libusb20_device *pdev,
1070 struct usb_device_info *pinfo)
1073 return (LIBUSB20_ERROR_INVALID_PARAM);
1075 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1079 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1081 return (pdev->beMethods->get_backend_name());
1085 libusb20_dev_get_desc(struct libusb20_device *pdev)
1087 return (pdev->usb_desc);
1091 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1093 pdev->debug = debug;
1098 libusb20_dev_get_debug(struct libusb20_device *pdev)
1100 return (pdev->debug);
1104 libusb20_dev_get_address(struct libusb20_device *pdev)
1106 return (pdev->device_address);
1110 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1112 return (pdev->parent_address);
1116 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1118 return (pdev->parent_port);
1122 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1124 return (pdev->bus_number);
1128 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1129 uint8_t iface_index, char *buf, uint8_t len)
1131 if ((buf == NULL) || (len == 0))
1132 return (LIBUSB20_ERROR_INVALID_PARAM);
1134 buf[0] = 0; /* set default string value */
1136 return (pdev->beMethods->dev_get_iface_desc(
1137 pdev, iface_index, buf, len));
1140 /* USB backend operations */
1143 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1144 uint16_t quirk_index, struct libusb20_quirk *pq)
1146 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1150 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1151 uint16_t quirk_index, struct libusb20_quirk *pq)
1153 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1157 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1158 struct libusb20_quirk *pq)
1160 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1164 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1165 struct libusb20_quirk *pq)
1167 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1171 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1173 return (pbe->methods->root_set_template(pbe, temp));
1177 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1184 return (pbe->methods->root_get_template(pbe, ptemp));
1187 struct libusb20_device *
1188 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1192 } else if (pdev == NULL) {
1193 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1195 pdev = TAILQ_NEXT(pdev, dev_entry);
1200 struct libusb20_backend *
1201 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1203 struct libusb20_backend *pbe;
1205 pbe = malloc(sizeof(*pbe));
1209 memset(pbe, 0, sizeof(*pbe));
1211 TAILQ_INIT(&(pbe->usb_devs));
1213 pbe->methods = methods; /* set backend methods */
1215 /* do the initial device scan */
1216 if (pbe->methods->init_backend) {
1217 pbe->methods->init_backend(pbe);
1222 struct libusb20_backend *
1223 libusb20_be_alloc_linux(void)
1228 struct libusb20_backend *
1229 libusb20_be_alloc_ugen20(void)
1231 return (libusb20_be_alloc(&libusb20_ugen20_backend));
1234 struct libusb20_backend *
1235 libusb20_be_alloc_default(void)
1237 struct libusb20_backend *pbe;
1240 pbe = libusb20_be_alloc_linux();
1245 pbe = libusb20_be_alloc_ugen20();
1249 return (NULL); /* no backend found */
1253 libusb20_be_free(struct libusb20_backend *pbe)
1255 struct libusb20_device *pdev;
1261 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1262 libusb20_be_dequeue_device(pbe, pdev);
1263 libusb20_dev_free(pdev);
1265 if (pbe->methods->exit_backend) {
1266 pbe->methods->exit_backend(pbe);
1273 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1275 pdev->beMethods = pbe->methods; /* copy backend methods */
1276 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1280 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1281 struct libusb20_device *pdev)
1283 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1287 libusb20_strerror(int code)
1290 case LIBUSB20_SUCCESS:
1292 case LIBUSB20_ERROR_IO:
1293 return ("I/O error");
1294 case LIBUSB20_ERROR_INVALID_PARAM:
1295 return ("Invalid parameter");
1296 case LIBUSB20_ERROR_ACCESS:
1297 return ("Permissions error");
1298 case LIBUSB20_ERROR_NO_DEVICE:
1299 return ("No device");
1300 case LIBUSB20_ERROR_NOT_FOUND:
1301 return ("Not found");
1302 case LIBUSB20_ERROR_BUSY:
1303 return ("Device busy");
1304 case LIBUSB20_ERROR_TIMEOUT:
1306 case LIBUSB20_ERROR_OVERFLOW:
1307 return ("Overflow");
1308 case LIBUSB20_ERROR_PIPE:
1309 return ("Pipe error");
1310 case LIBUSB20_ERROR_INTERRUPTED:
1311 return ("Interrupted");
1312 case LIBUSB20_ERROR_NO_MEM:
1313 return ("Out of memory");
1314 case LIBUSB20_ERROR_NOT_SUPPORTED:
1315 return ("Not supported");
1316 case LIBUSB20_ERROR_OTHER:
1317 return ("Other error");
1319 return ("Unknown error");
1324 libusb20_error_name(int code)
1327 case LIBUSB20_SUCCESS:
1328 return ("LIBUSB20_SUCCESS");
1329 case LIBUSB20_ERROR_IO:
1330 return ("LIBUSB20_ERROR_IO");
1331 case LIBUSB20_ERROR_INVALID_PARAM:
1332 return ("LIBUSB20_ERROR_INVALID_PARAM");
1333 case LIBUSB20_ERROR_ACCESS:
1334 return ("LIBUSB20_ERROR_ACCESS");
1335 case LIBUSB20_ERROR_NO_DEVICE:
1336 return ("LIBUSB20_ERROR_NO_DEVICE");
1337 case LIBUSB20_ERROR_NOT_FOUND:
1338 return ("LIBUSB20_ERROR_NOT_FOUND");
1339 case LIBUSB20_ERROR_BUSY:
1340 return ("LIBUSB20_ERROR_BUSY");
1341 case LIBUSB20_ERROR_TIMEOUT:
1342 return ("LIBUSB20_ERROR_TIMEOUT");
1343 case LIBUSB20_ERROR_OVERFLOW:
1344 return ("LIBUSB20_ERROR_OVERFLOW");
1345 case LIBUSB20_ERROR_PIPE:
1346 return ("LIBUSB20_ERROR_PIPE");
1347 case LIBUSB20_ERROR_INTERRUPTED:
1348 return ("LIBUSB20_ERROR_INTERRUPTED");
1349 case LIBUSB20_ERROR_NO_MEM:
1350 return ("LIBUSB20_ERROR_NO_MEM");
1351 case LIBUSB20_ERROR_NOT_SUPPORTED:
1352 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1353 case LIBUSB20_ERROR_OTHER:
1354 return ("LIBUSB20_ERROR_OTHER");
1356 return ("LIBUSB20_ERROR_UNKNOWN");