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 #include <sys/queue.h>
36 #include "libusb20_desc.h"
37 #include "libusb20_int.h"
42 return (LIBUSB20_ERROR_NOT_SUPPORTED);
52 dummy_callback(struct libusb20_transfer *xfer)
55 switch (libusb20_tr_get_status(xfer)) {
56 case LIBUSB20_TRANSFER_START:
57 libusb20_tr_submit(xfer);
60 /* complete or error */
66 #define dummy_get_config_desc_full (void *)dummy_int
67 #define dummy_get_config_index (void *)dummy_int
68 #define dummy_set_config_index (void *)dummy_int
69 #define dummy_set_alt_index (void *)dummy_int
70 #define dummy_reset_device (void *)dummy_int
71 #define dummy_check_connected (void *)dummy_int
72 #define dummy_set_power_mode (void *)dummy_int
73 #define dummy_get_power_mode (void *)dummy_int
74 #define dummy_get_power_usage (void *)dummy_int
75 #define dummy_kernel_driver_active (void *)dummy_int
76 #define dummy_detach_kernel_driver (void *)dummy_int
77 #define dummy_do_request_sync (void *)dummy_int
78 #define dummy_tr_open (void *)dummy_int
79 #define dummy_tr_close (void *)dummy_int
80 #define dummy_tr_clear_stall_sync (void *)dummy_int
81 #define dummy_process (void *)dummy_int
82 #define dummy_dev_info (void *)dummy_int
83 #define dummy_dev_get_iface_driver (void *)dummy_int
85 #define dummy_tr_submit (void *)dummy_void
86 #define dummy_tr_cancel_async (void *)dummy_void
88 static const struct libusb20_device_methods libusb20_dummy_methods = {
89 LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
93 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
99 if (!xfer->is_pending) {
100 xfer->status = LIBUSB20_TRANSFER_START;
102 xfer->is_pending = 0;
105 xfer->callback(xfer);
107 if (xfer->is_restart) {
108 xfer->is_restart = 0;
111 if (xfer->is_draining &&
112 (!xfer->is_pending)) {
113 xfer->is_draining = 0;
114 xfer->status = LIBUSB20_TRANSFER_DRAINED;
115 xfer->callback(xfer);
121 libusb20_tr_close(struct libusb20_transfer *xfer)
125 if (!xfer->is_opened) {
126 return (LIBUSB20_ERROR_OTHER);
128 error = xfer->pdev->methods->tr_close(xfer);
133 if (xfer->ppBuffer) {
134 free(xfer->ppBuffer);
136 /* reset variable fields in case the transfer is opened again */
140 xfer->is_pending = 0;
142 xfer->is_draining = 0;
143 xfer->is_restart = 0;
150 xfer->maxTotalLength = 0;
151 xfer->maxPacketLen = 0;
156 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
157 uint32_t MaxFrameCount, uint8_t ep_no)
164 return (LIBUSB20_ERROR_BUSY);
165 if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
166 MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
171 if (MaxFrameCount == 0)
172 return (LIBUSB20_ERROR_INVALID_PARAM);
174 xfer->maxFrames = MaxFrameCount;
176 size = MaxFrameCount * sizeof(xfer->pLength[0]);
177 xfer->pLength = malloc(size);
178 if (xfer->pLength == NULL) {
179 return (LIBUSB20_ERROR_NO_MEM);
181 memset(xfer->pLength, 0, size);
183 size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
184 xfer->ppBuffer = malloc(size);
185 if (xfer->ppBuffer == NULL) {
187 return (LIBUSB20_ERROR_NO_MEM);
189 memset(xfer->ppBuffer, 0, size);
191 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
192 MaxFrameCount, ep_no, pre_scale);
195 free(xfer->ppBuffer);
203 struct libusb20_transfer *
204 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
206 if (trIndex >= pdev->nTransfer) {
209 return (pdev->pTransfer + trIndex);
213 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
215 return (xfer->aFrames);
219 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
221 return (xfer->timeComplete);
225 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
230 for (x = 0; x != xfer->aFrames; x++) {
231 actlen += xfer->pLength[x];
237 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
239 return (xfer->maxFrames);
243 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
246 * Special Case NOTE: If the packet multiplier is non-zero for
247 * High Speed USB, the value returned is equal to
248 * "wMaxPacketSize * multiplier" !
250 return (xfer->maxPacketLen);
254 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
256 return (xfer->maxTotalLength);
260 libusb20_tr_get_status(struct libusb20_transfer *xfer)
262 return (xfer->status);
266 libusb20_tr_pending(struct libusb20_transfer *xfer)
268 return (xfer->is_pending);
272 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
274 return (xfer->priv_sc0);
278 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
280 return (xfer->priv_sc1);
284 libusb20_tr_stop(struct libusb20_transfer *xfer)
286 if (!xfer->is_opened) {
287 /* transfer is not opened */
290 if (!xfer->is_pending) {
291 /* transfer not pending */
294 if (xfer->is_cancel) {
295 /* already cancelling */
298 xfer->is_cancel = 1; /* we are cancelling */
300 xfer->pdev->methods->tr_cancel_async(xfer);
305 libusb20_tr_drain(struct libusb20_transfer *xfer)
307 if (!xfer->is_opened) {
308 /* transfer is not opened */
311 /* make sure that we are cancelling */
312 libusb20_tr_stop(xfer);
314 if (xfer->is_pending) {
315 xfer->is_draining = 1;
321 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
323 xfer->pdev->methods->tr_clear_stall_sync(xfer);
328 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
330 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(buffer);
335 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
342 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
349 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
351 return (xfer->pLength[frIndex]);
355 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
357 xfer->pLength[frIndex] = length;
362 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
364 xfer->priv_sc0 = sc0;
369 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
371 xfer->priv_sc1 = sc1;
376 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
378 xfer->timeout = timeout;
383 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
385 if (nFrames > xfer->maxFrames) {
386 /* should not happen */
387 nFrames = xfer->maxFrames;
389 xfer->nFrames = nFrames;
394 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
396 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
397 xfer->pLength[0] = length;
398 xfer->timeout = timeout;
404 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
408 xfer->ppBuffer[0] = libusb20_pass_ptr(psetup);
409 xfer->pLength[0] = 8; /* fixed */
410 xfer->timeout = timeout;
412 len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
416 xfer->ppBuffer[1] = libusb20_pass_ptr(pBuf);
417 xfer->pLength[1] = len;
425 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
427 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
428 xfer->pLength[0] = length;
429 xfer->timeout = timeout;
435 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
437 if (frIndex >= xfer->maxFrames) {
438 /* should not happen */
441 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(pBuf);
442 xfer->pLength[frIndex] = length;
447 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
448 void *pbuf, uint32_t length, uint32_t *pactlen,
451 struct libusb20_device *pdev = xfer->pdev;
452 uint32_t transfer_max;
453 uint32_t transfer_act;
456 /* set some sensible default value */
460 /* check for error condition */
461 if (libusb20_tr_pending(xfer))
462 return (LIBUSB20_ERROR_OTHER);
465 /* compute maximum transfer length */
467 libusb20_tr_get_max_total_length(xfer);
469 if (transfer_max > length)
470 transfer_max = length;
472 /* setup bulk or interrupt transfer */
473 libusb20_tr_setup_bulk(xfer, pbuf,
474 transfer_max, timeout);
476 /* start the transfer */
477 libusb20_tr_start(xfer);
479 /* wait for transfer completion */
480 while (libusb20_dev_process(pdev) == 0) {
482 if (libusb20_tr_pending(xfer) == 0)
485 libusb20_dev_wait_process(pdev, -1);
488 transfer_act = libusb20_tr_get_actual_length(xfer);
490 /* update actual length, if any */
492 pactlen[0] += transfer_act;
494 /* check transfer status */
495 retval = libusb20_tr_get_status(xfer);
499 /* check for short transfer */
500 if (transfer_act != transfer_max)
503 /* update buffer pointer and length */
504 pbuf = ((uint8_t *)pbuf) + transfer_max;
505 length = length - transfer_max;
507 } while (length != 0);
513 libusb20_tr_submit(struct libusb20_transfer *xfer)
515 if (!xfer->is_opened) {
516 /* transfer is not opened */
519 if (xfer->is_pending) {
520 /* should not happen */
523 xfer->is_pending = 1; /* we are pending */
524 xfer->is_cancel = 0; /* not cancelling */
525 xfer->is_restart = 0; /* not restarting */
527 xfer->pdev->methods->tr_submit(xfer);
532 libusb20_tr_start(struct libusb20_transfer *xfer)
534 if (!xfer->is_opened) {
535 /* transfer is not opened */
538 if (xfer->is_pending) {
539 if (xfer->is_cancel) {
540 /* cancelling - restart */
541 xfer->is_restart = 1;
543 /* transfer not pending */
546 /* get into the callback */
547 libusb20_tr_callback_wrapper(xfer);
551 /* USB device operations */
554 libusb20_dev_close(struct libusb20_device *pdev)
556 struct libusb20_transfer *xfer;
560 if (!pdev->is_opened) {
561 return (LIBUSB20_ERROR_OTHER);
563 for (x = 0; x != pdev->nTransfer; x++) {
564 xfer = pdev->pTransfer + x;
566 if (!xfer->is_opened) {
567 /* transfer is not opened */
571 libusb20_tr_drain(xfer);
573 libusb20_tr_close(xfer);
576 if (pdev->pTransfer != NULL) {
577 free(pdev->pTransfer);
578 pdev->pTransfer = NULL;
580 error = pdev->beMethods->close_device(pdev);
582 pdev->methods = &libusb20_dummy_methods;
587 * The following variable is only used by the libusb v0.1
590 pdev->claimed_interface = 0;
596 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
600 error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
604 struct LIBUSB20_DEVICE_DESC_DECODED *
605 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
607 return (&(pdev->ddesc));
611 libusb20_dev_get_fd(struct libusb20_device *pdev)
617 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
621 error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
626 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
628 struct libusb20_transfer *xfer;
633 if (pdev->is_opened) {
634 return (LIBUSB20_ERROR_BUSY);
636 if (nTransferMax >= 256) {
637 return (LIBUSB20_ERROR_INVALID_PARAM);
638 } else if (nTransferMax != 0) {
639 size = sizeof(pdev->pTransfer[0]) * nTransferMax;
640 pdev->pTransfer = malloc(size);
641 if (pdev->pTransfer == NULL) {
642 return (LIBUSB20_ERROR_NO_MEM);
644 memset(pdev->pTransfer, 0, size);
646 /* initialise all transfers */
647 for (x = 0; x != nTransferMax; x++) {
649 xfer = pdev->pTransfer + x;
653 xfer->callback = &dummy_callback;
656 /* set "nTransfer" early */
657 pdev->nTransfer = nTransferMax;
659 error = pdev->beMethods->open_device(pdev, nTransferMax);
662 if (pdev->pTransfer != NULL) {
663 free(pdev->pTransfer);
664 pdev->pTransfer = NULL;
667 pdev->file_ctrl = -1;
676 libusb20_dev_reset(struct libusb20_device *pdev)
680 error = pdev->methods->reset_device(pdev);
685 libusb20_dev_check_connected(struct libusb20_device *pdev)
689 error = pdev->methods->check_connected(pdev);
694 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
698 error = pdev->methods->set_power_mode(pdev, power_mode);
703 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
708 error = pdev->methods->get_power_mode(pdev, &power_mode);
710 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
715 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
718 uint16_t power_usage;
720 error = pdev->methods->get_power_usage(pdev, &power_usage);
723 return (power_usage);
727 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
731 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
736 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
740 error = pdev->methods->set_config_index(pdev, configIndex);
745 libusb20_dev_request_sync(struct libusb20_device *pdev,
746 struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
747 uint16_t *pactlen, uint32_t timeout, uint8_t flags)
751 error = pdev->methods->do_request_sync(pdev,
752 setup, data, pactlen, timeout, flags);
757 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
758 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
760 struct LIBUSB20_CONTROL_SETUP_DECODED req;
763 /* make sure memory is initialised */
768 return (LIBUSB20_ERROR_INVALID_PARAM);
770 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
773 * We need to read the USB string in two steps else some USB
774 * devices will complain.
777 LIBUSB20_REQUEST_TYPE_STANDARD |
778 LIBUSB20_RECIPIENT_DEVICE |
779 LIBUSB20_ENDPOINT_IN;
780 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
781 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
783 req.wLength = 4; /* bytes */
785 error = libusb20_dev_request_sync(pdev, &req,
786 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
790 req.wLength = *(uint8_t *)ptr; /* bytes */
791 if (req.wLength > len) {
792 /* partial string read */
795 error = libusb20_dev_request_sync(pdev, &req,
796 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
801 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) {
802 return (LIBUSB20_ERROR_OTHER);
804 return (0); /* success */
808 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
809 uint8_t str_index, void *ptr, uint16_t len)
820 /* the following code derives from the FreeBSD USB kernel */
822 if ((len < 1) || (ptr == NULL)) {
823 /* too short buffer */
824 return (LIBUSB20_ERROR_INVALID_PARAM);
826 error = libusb20_dev_req_string_sync(pdev,
827 0, 0, temp, sizeof(temp));
829 *(uint8_t *)ptr = 0; /* zero terminate */
832 langid = temp[2] | (temp[3] << 8);
834 error = libusb20_dev_req_string_sync(pdev, str_index,
835 langid, temp, sizeof(temp));
837 *(uint8_t *)ptr = 0; /* zero terminate */
841 /* string length is too short */
842 *(uint8_t *)ptr = 0; /* zero terminate */
843 return (LIBUSB20_ERROR_OTHER);
845 /* reserve one byte for terminating zero */
848 /* find maximum length */
849 n = (temp[0] / 2) - 1;
853 /* reset swap state */
856 /* setup output buffer pointer */
859 /* convert and filter */
860 for (i = 0; (i != n); i++) {
861 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
863 /* convert from Unicode, handle buggy strings */
864 if (((c & 0xff00) == 0) && (swap & 1)) {
865 /* Little Endian, default */
868 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
873 /* skip invalid character */
877 * Filter by default - we don't allow greater and less than
878 * signs because they might confuse the dmesg printouts!
880 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
881 /* skip invalid character */
886 *buf = 0; /* zero terminate string */
891 struct libusb20_config *
892 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
894 struct libusb20_config *retval = NULL;
900 if (!pdev->is_opened) {
901 error = libusb20_dev_open(pdev, 0);
909 error = pdev->methods->get_config_desc_full(pdev,
910 &ptr, &len, configIndex);
915 /* parse new config descriptor */
916 retval = libusb20_parse_config_desc(ptr);
918 /* free config descriptor */
923 error = libusb20_dev_close(pdev);
928 struct libusb20_device *
929 libusb20_dev_alloc(void)
931 struct libusb20_device *pdev;
933 pdev = malloc(sizeof(*pdev));
937 memset(pdev, 0, sizeof(*pdev));
940 pdev->file_ctrl = -1;
941 pdev->methods = &libusb20_dummy_methods;
946 libusb20_dev_get_config_index(struct libusb20_device *pdev)
952 if (!pdev->is_opened) {
953 error = libusb20_dev_open(pdev, 0);
963 error = pdev->methods->get_config_index(pdev, &cfg_index);
965 cfg_index = 0xFF; /* current config index */
967 if (libusb20_dev_close(pdev)) {
975 libusb20_dev_get_mode(struct libusb20_device *pdev)
977 return (pdev->usb_mode);
981 libusb20_dev_get_speed(struct libusb20_device *pdev)
983 return (pdev->usb_speed);
986 /* if this function returns an error, the device is gone */
988 libusb20_dev_process(struct libusb20_device *pdev)
992 error = pdev->methods->process(pdev);
997 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
999 struct pollfd pfd[1];
1001 if (!pdev->is_opened) {
1004 pfd[0].fd = pdev->file;
1005 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1008 if (poll(pfd, 1, timeout)) {
1009 /* ignore any error */
1015 libusb20_dev_free(struct libusb20_device *pdev)
1021 if (pdev->is_opened) {
1022 if (libusb20_dev_close(pdev)) {
1023 /* ignore any errors */
1031 libusb20_dev_get_info(struct libusb20_device *pdev,
1032 struct usb_device_info *pinfo)
1035 return (LIBUSB20_ERROR_INVALID_PARAM);
1037 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1041 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1043 return (pdev->beMethods->get_backend_name());
1047 libusb20_dev_get_desc(struct libusb20_device *pdev)
1049 return (pdev->usb_desc);
1053 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1055 pdev->debug = debug;
1060 libusb20_dev_get_debug(struct libusb20_device *pdev)
1062 return (pdev->debug);
1066 libusb20_dev_get_address(struct libusb20_device *pdev)
1068 return (pdev->device_address);
1072 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1074 return (pdev->parent_address);
1078 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1080 return (pdev->parent_port);
1084 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1086 return (pdev->bus_number);
1090 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1091 uint8_t iface_index, char *buf, uint8_t len)
1093 if ((buf == NULL) || (len == 0))
1094 return (LIBUSB20_ERROR_INVALID_PARAM);
1096 buf[0] = 0; /* set default string value */
1098 return (pdev->beMethods->dev_get_iface_desc(
1099 pdev, iface_index, buf, len));
1102 /* USB backend operations */
1105 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1106 uint16_t quirk_index, struct libusb20_quirk *pq)
1108 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1112 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1113 uint16_t quirk_index, struct libusb20_quirk *pq)
1115 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1119 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1120 struct libusb20_quirk *pq)
1122 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1126 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1127 struct libusb20_quirk *pq)
1129 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1133 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1135 return (pbe->methods->root_set_template(pbe, temp));
1139 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1146 return (pbe->methods->root_get_template(pbe, ptemp));
1149 struct libusb20_device *
1150 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1154 } else if (pdev == NULL) {
1155 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1157 pdev = TAILQ_NEXT(pdev, dev_entry);
1162 struct libusb20_backend *
1163 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1165 struct libusb20_backend *pbe;
1167 pbe = malloc(sizeof(*pbe));
1171 memset(pbe, 0, sizeof(*pbe));
1173 TAILQ_INIT(&(pbe->usb_devs));
1175 pbe->methods = methods; /* set backend methods */
1177 /* do the initial device scan */
1178 if (pbe->methods->init_backend) {
1179 pbe->methods->init_backend(pbe);
1184 struct libusb20_backend *
1185 libusb20_be_alloc_linux(void)
1187 struct libusb20_backend *pbe;
1190 pbe = libusb20_be_alloc(&libusb20_linux_backend);
1197 struct libusb20_backend *
1198 libusb20_be_alloc_ugen20(void)
1200 struct libusb20_backend *pbe;
1202 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1203 pbe = libusb20_be_alloc(&libusb20_ugen20_backend);
1210 struct libusb20_backend *
1211 libusb20_be_alloc_default(void)
1213 struct libusb20_backend *pbe;
1215 pbe = libusb20_be_alloc_linux();
1219 pbe = libusb20_be_alloc_ugen20();
1223 return (NULL); /* no backend found */
1227 libusb20_be_free(struct libusb20_backend *pbe)
1229 struct libusb20_device *pdev;
1235 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1236 libusb20_be_dequeue_device(pbe, pdev);
1237 libusb20_dev_free(pdev);
1239 if (pbe->methods->exit_backend) {
1240 pbe->methods->exit_backend(pbe);
1247 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1249 pdev->beMethods = pbe->methods; /* copy backend methods */
1250 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1254 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1255 struct libusb20_device *pdev)
1257 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1261 libusb20_strerror(int code)
1264 case LIBUSB20_SUCCESS:
1266 case LIBUSB20_ERROR_IO:
1267 return ("I/O error");
1268 case LIBUSB20_ERROR_INVALID_PARAM:
1269 return ("Invalid parameter");
1270 case LIBUSB20_ERROR_ACCESS:
1271 return ("Permissions error");
1272 case LIBUSB20_ERROR_NO_DEVICE:
1273 return ("No device");
1274 case LIBUSB20_ERROR_NOT_FOUND:
1275 return ("Not found");
1276 case LIBUSB20_ERROR_BUSY:
1277 return ("Device busy");
1278 case LIBUSB20_ERROR_TIMEOUT:
1280 case LIBUSB20_ERROR_OVERFLOW:
1281 return ("Overflow");
1282 case LIBUSB20_ERROR_PIPE:
1283 return ("Pipe error");
1284 case LIBUSB20_ERROR_INTERRUPTED:
1285 return ("Interrupted");
1286 case LIBUSB20_ERROR_NO_MEM:
1287 return ("Out of memory");
1288 case LIBUSB20_ERROR_NOT_SUPPORTED:
1289 return ("Not supported");
1290 case LIBUSB20_ERROR_OTHER:
1291 return ("Other error");
1293 return ("Unknown error");
1298 libusb20_error_name(int code)
1301 case LIBUSB20_SUCCESS:
1302 return ("LIBUSB20_SUCCESS");
1303 case LIBUSB20_ERROR_IO:
1304 return ("LIBUSB20_ERROR_IO");
1305 case LIBUSB20_ERROR_INVALID_PARAM:
1306 return ("LIBUSB20_ERROR_INVALID_PARAM");
1307 case LIBUSB20_ERROR_ACCESS:
1308 return ("LIBUSB20_ERROR_ACCESS");
1309 case LIBUSB20_ERROR_NO_DEVICE:
1310 return ("LIBUSB20_ERROR_NO_DEVICE");
1311 case LIBUSB20_ERROR_NOT_FOUND:
1312 return ("LIBUSB20_ERROR_NOT_FOUND");
1313 case LIBUSB20_ERROR_BUSY:
1314 return ("LIBUSB20_ERROR_BUSY");
1315 case LIBUSB20_ERROR_TIMEOUT:
1316 return ("LIBUSB20_ERROR_TIMEOUT");
1317 case LIBUSB20_ERROR_OVERFLOW:
1318 return ("LIBUSB20_ERROR_OVERFLOW");
1319 case LIBUSB20_ERROR_PIPE:
1320 return ("LIBUSB20_ERROR_PIPE");
1321 case LIBUSB20_ERROR_INTERRUPTED:
1322 return ("LIBUSB20_ERROR_INTERRUPTED");
1323 case LIBUSB20_ERROR_NO_MEM:
1324 return ("LIBUSB20_ERROR_NO_MEM");
1325 case LIBUSB20_ERROR_NOT_SUPPORTED:
1326 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1327 case LIBUSB20_ERROR_OTHER:
1328 return ("LIBUSB20_ERROR_OTHER");
1330 return ("LIBUSB20_ERROR_UNKNOWN");