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_port_path (void *)dummy_int
75 #define dummy_get_power_usage (void *)dummy_int
76 #define dummy_kernel_driver_active (void *)dummy_int
77 #define dummy_detach_kernel_driver (void *)dummy_int
78 #define dummy_do_request_sync (void *)dummy_int
79 #define dummy_tr_open (void *)dummy_int
80 #define dummy_tr_close (void *)dummy_int
81 #define dummy_tr_clear_stall_sync (void *)dummy_int
82 #define dummy_process (void *)dummy_int
83 #define dummy_dev_info (void *)dummy_int
84 #define dummy_dev_get_iface_driver (void *)dummy_int
86 #define dummy_tr_submit (void *)dummy_void
87 #define dummy_tr_cancel_async (void *)dummy_void
89 static const struct libusb20_device_methods libusb20_dummy_methods = {
90 LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
94 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
100 if (!xfer->is_pending) {
101 xfer->status = LIBUSB20_TRANSFER_START;
103 xfer->is_pending = 0;
106 xfer->callback(xfer);
108 if (xfer->is_restart) {
109 xfer->is_restart = 0;
112 if (xfer->is_draining &&
113 (!xfer->is_pending)) {
114 xfer->is_draining = 0;
115 xfer->status = LIBUSB20_TRANSFER_DRAINED;
116 xfer->callback(xfer);
122 libusb20_tr_close(struct libusb20_transfer *xfer)
126 if (!xfer->is_opened) {
127 return (LIBUSB20_ERROR_OTHER);
129 error = xfer->pdev->methods->tr_close(xfer);
134 if (xfer->ppBuffer) {
135 free(xfer->ppBuffer);
137 /* reset variable fields in case the transfer is opened again */
141 xfer->is_pending = 0;
143 xfer->is_draining = 0;
144 xfer->is_restart = 0;
151 xfer->maxTotalLength = 0;
152 xfer->maxPacketLen = 0;
157 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
158 uint32_t MaxFrameCount, uint8_t ep_no)
165 return (LIBUSB20_ERROR_BUSY);
166 if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
167 MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
172 if (MaxFrameCount == 0)
173 return (LIBUSB20_ERROR_INVALID_PARAM);
175 xfer->maxFrames = MaxFrameCount;
177 size = MaxFrameCount * sizeof(xfer->pLength[0]);
178 xfer->pLength = malloc(size);
179 if (xfer->pLength == NULL) {
180 return (LIBUSB20_ERROR_NO_MEM);
182 memset(xfer->pLength, 0, size);
184 size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
185 xfer->ppBuffer = malloc(size);
186 if (xfer->ppBuffer == NULL) {
188 return (LIBUSB20_ERROR_NO_MEM);
190 memset(xfer->ppBuffer, 0, size);
192 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
193 MaxFrameCount, ep_no, pre_scale);
196 free(xfer->ppBuffer);
204 struct libusb20_transfer *
205 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
207 if (trIndex >= pdev->nTransfer) {
210 return (pdev->pTransfer + trIndex);
214 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
216 return (xfer->aFrames);
220 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
222 return (xfer->timeComplete);
226 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
231 for (x = 0; x != xfer->aFrames; x++) {
232 actlen += xfer->pLength[x];
238 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
240 return (xfer->maxFrames);
244 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
247 * Special Case NOTE: If the packet multiplier is non-zero for
248 * High Speed USB, the value returned is equal to
249 * "wMaxPacketSize * multiplier" !
251 return (xfer->maxPacketLen);
255 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
257 return (xfer->maxTotalLength);
261 libusb20_tr_get_status(struct libusb20_transfer *xfer)
263 return (xfer->status);
267 libusb20_tr_pending(struct libusb20_transfer *xfer)
269 return (xfer->is_pending);
273 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
275 return (xfer->priv_sc0);
279 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
281 return (xfer->priv_sc1);
285 libusb20_tr_stop(struct libusb20_transfer *xfer)
287 if (!xfer->is_opened) {
288 /* transfer is not opened */
291 if (!xfer->is_pending) {
292 /* transfer not pending */
295 if (xfer->is_cancel) {
296 /* already cancelling */
299 xfer->is_cancel = 1; /* we are cancelling */
301 xfer->pdev->methods->tr_cancel_async(xfer);
306 libusb20_tr_drain(struct libusb20_transfer *xfer)
308 if (!xfer->is_opened) {
309 /* transfer is not opened */
312 /* make sure that we are cancelling */
313 libusb20_tr_stop(xfer);
315 if (xfer->is_pending) {
316 xfer->is_draining = 1;
322 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
324 xfer->pdev->methods->tr_clear_stall_sync(xfer);
329 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
331 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(buffer);
336 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
343 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
350 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
352 return (xfer->pLength[frIndex]);
356 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
358 xfer->pLength[frIndex] = length;
363 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
365 xfer->priv_sc0 = sc0;
370 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
372 xfer->priv_sc1 = sc1;
377 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
379 xfer->timeout = timeout;
384 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
386 if (nFrames > xfer->maxFrames) {
387 /* should not happen */
388 nFrames = xfer->maxFrames;
390 xfer->nFrames = nFrames;
395 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
397 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
398 xfer->pLength[0] = length;
399 xfer->timeout = timeout;
405 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
409 xfer->ppBuffer[0] = libusb20_pass_ptr(psetup);
410 xfer->pLength[0] = 8; /* fixed */
411 xfer->timeout = timeout;
413 len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
417 xfer->ppBuffer[1] = libusb20_pass_ptr(pBuf);
418 xfer->pLength[1] = len;
426 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
428 xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
429 xfer->pLength[0] = length;
430 xfer->timeout = timeout;
436 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
438 if (frIndex >= xfer->maxFrames) {
439 /* should not happen */
442 xfer->ppBuffer[frIndex] = libusb20_pass_ptr(pBuf);
443 xfer->pLength[frIndex] = length;
448 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
449 void *pbuf, uint32_t length, uint32_t *pactlen,
452 struct libusb20_device *pdev = xfer->pdev;
453 uint32_t transfer_max;
454 uint32_t transfer_act;
457 /* set some sensible default value */
461 /* check for error condition */
462 if (libusb20_tr_pending(xfer))
463 return (LIBUSB20_ERROR_OTHER);
466 /* compute maximum transfer length */
468 libusb20_tr_get_max_total_length(xfer);
470 if (transfer_max > length)
471 transfer_max = length;
473 /* setup bulk or interrupt transfer */
474 libusb20_tr_setup_bulk(xfer, pbuf,
475 transfer_max, timeout);
477 /* start the transfer */
478 libusb20_tr_start(xfer);
480 /* wait for transfer completion */
481 while (libusb20_dev_process(pdev) == 0) {
483 if (libusb20_tr_pending(xfer) == 0)
486 libusb20_dev_wait_process(pdev, -1);
489 transfer_act = libusb20_tr_get_actual_length(xfer);
491 /* update actual length, if any */
493 pactlen[0] += transfer_act;
495 /* check transfer status */
496 retval = libusb20_tr_get_status(xfer);
500 /* check for short transfer */
501 if (transfer_act != transfer_max)
504 /* update buffer pointer and length */
505 pbuf = ((uint8_t *)pbuf) + transfer_max;
506 length = length - transfer_max;
508 } while (length != 0);
514 libusb20_tr_submit(struct libusb20_transfer *xfer)
516 if (!xfer->is_opened) {
517 /* transfer is not opened */
520 if (xfer->is_pending) {
521 /* should not happen */
524 xfer->is_pending = 1; /* we are pending */
525 xfer->is_cancel = 0; /* not cancelling */
526 xfer->is_restart = 0; /* not restarting */
528 xfer->pdev->methods->tr_submit(xfer);
533 libusb20_tr_start(struct libusb20_transfer *xfer)
535 if (!xfer->is_opened) {
536 /* transfer is not opened */
539 if (xfer->is_pending) {
540 if (xfer->is_cancel) {
541 /* cancelling - restart */
542 xfer->is_restart = 1;
544 /* transfer not pending */
547 /* get into the callback */
548 libusb20_tr_callback_wrapper(xfer);
552 /* USB device operations */
555 libusb20_dev_close(struct libusb20_device *pdev)
557 struct libusb20_transfer *xfer;
561 if (!pdev->is_opened) {
562 return (LIBUSB20_ERROR_OTHER);
564 for (x = 0; x != pdev->nTransfer; x++) {
565 xfer = pdev->pTransfer + x;
567 if (!xfer->is_opened) {
568 /* transfer is not opened */
572 libusb20_tr_drain(xfer);
574 libusb20_tr_close(xfer);
577 if (pdev->pTransfer != NULL) {
578 free(pdev->pTransfer);
579 pdev->pTransfer = NULL;
581 error = pdev->beMethods->close_device(pdev);
583 pdev->methods = &libusb20_dummy_methods;
588 * The following variable is only used by the libusb v0.1
591 pdev->claimed_interface = 0;
597 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
601 error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
605 struct LIBUSB20_DEVICE_DESC_DECODED *
606 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
608 return (&(pdev->ddesc));
612 libusb20_dev_get_fd(struct libusb20_device *pdev)
618 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
622 error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
627 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
629 struct libusb20_transfer *xfer;
634 if (pdev->is_opened) {
635 return (LIBUSB20_ERROR_BUSY);
637 if (nTransferMax >= 256) {
638 return (LIBUSB20_ERROR_INVALID_PARAM);
639 } else if (nTransferMax != 0) {
640 size = sizeof(pdev->pTransfer[0]) * nTransferMax;
641 pdev->pTransfer = malloc(size);
642 if (pdev->pTransfer == NULL) {
643 return (LIBUSB20_ERROR_NO_MEM);
645 memset(pdev->pTransfer, 0, size);
647 /* initialise all transfers */
648 for (x = 0; x != nTransferMax; x++) {
650 xfer = pdev->pTransfer + x;
654 xfer->callback = &dummy_callback;
657 /* set "nTransfer" early */
658 pdev->nTransfer = nTransferMax;
660 error = pdev->beMethods->open_device(pdev, nTransferMax);
663 if (pdev->pTransfer != NULL) {
664 free(pdev->pTransfer);
665 pdev->pTransfer = NULL;
668 pdev->file_ctrl = -1;
677 libusb20_dev_reset(struct libusb20_device *pdev)
681 error = pdev->methods->reset_device(pdev);
686 libusb20_dev_check_connected(struct libusb20_device *pdev)
690 error = pdev->methods->check_connected(pdev);
695 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
699 error = pdev->methods->set_power_mode(pdev, power_mode);
704 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
709 error = pdev->methods->get_power_mode(pdev, &power_mode);
711 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
716 libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
718 return (pdev->methods->get_port_path(pdev, buf, bufsize));
722 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
725 uint16_t power_usage;
727 error = pdev->methods->get_power_usage(pdev, &power_usage);
730 return (power_usage);
734 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
738 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
743 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
747 error = pdev->methods->set_config_index(pdev, configIndex);
752 libusb20_dev_request_sync(struct libusb20_device *pdev,
753 struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
754 uint16_t *pactlen, uint32_t timeout, uint8_t flags)
758 error = pdev->methods->do_request_sync(pdev,
759 setup, data, pactlen, timeout, flags);
764 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
765 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
767 struct LIBUSB20_CONTROL_SETUP_DECODED req;
770 /* make sure memory is initialised */
775 return (LIBUSB20_ERROR_INVALID_PARAM);
777 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
780 * We need to read the USB string in two steps else some USB
781 * devices will complain.
784 LIBUSB20_REQUEST_TYPE_STANDARD |
785 LIBUSB20_RECIPIENT_DEVICE |
786 LIBUSB20_ENDPOINT_IN;
787 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
788 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
790 req.wLength = 4; /* bytes */
792 error = libusb20_dev_request_sync(pdev, &req,
793 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
797 req.wLength = *(uint8_t *)ptr; /* bytes */
798 if (req.wLength > len) {
799 /* partial string read */
802 error = libusb20_dev_request_sync(pdev, &req,
803 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
808 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) {
809 return (LIBUSB20_ERROR_OTHER);
811 return (0); /* success */
815 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
816 uint8_t str_index, void *ptr, uint16_t len)
827 /* the following code derives from the FreeBSD USB kernel */
829 if ((len < 1) || (ptr == NULL)) {
830 /* too short buffer */
831 return (LIBUSB20_ERROR_INVALID_PARAM);
833 error = libusb20_dev_req_string_sync(pdev,
834 0, 0, temp, sizeof(temp));
836 *(uint8_t *)ptr = 0; /* zero terminate */
839 langid = temp[2] | (temp[3] << 8);
841 error = libusb20_dev_req_string_sync(pdev, str_index,
842 langid, temp, sizeof(temp));
844 *(uint8_t *)ptr = 0; /* zero terminate */
848 /* string length is too short */
849 *(uint8_t *)ptr = 0; /* zero terminate */
850 return (LIBUSB20_ERROR_OTHER);
852 /* reserve one byte for terminating zero */
855 /* find maximum length */
856 n = (temp[0] / 2) - 1;
860 /* reset swap state */
863 /* setup output buffer pointer */
866 /* convert and filter */
867 for (i = 0; (i != n); i++) {
868 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
870 /* convert from Unicode, handle buggy strings */
871 if (((c & 0xff00) == 0) && (swap & 1)) {
872 /* Little Endian, default */
875 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
880 /* skip invalid character */
884 * Filter by default - we don't allow greater and less than
885 * signs because they might confuse the dmesg printouts!
887 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
888 /* skip invalid character */
893 *buf = 0; /* zero terminate string */
898 struct libusb20_config *
899 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
901 struct libusb20_config *retval = NULL;
907 if (!pdev->is_opened) {
908 error = libusb20_dev_open(pdev, 0);
916 error = pdev->methods->get_config_desc_full(pdev,
917 &ptr, &len, configIndex);
922 /* parse new config descriptor */
923 retval = libusb20_parse_config_desc(ptr);
925 /* free config descriptor */
930 error = libusb20_dev_close(pdev);
935 struct libusb20_device *
936 libusb20_dev_alloc(void)
938 struct libusb20_device *pdev;
940 pdev = malloc(sizeof(*pdev));
944 memset(pdev, 0, sizeof(*pdev));
947 pdev->file_ctrl = -1;
948 pdev->methods = &libusb20_dummy_methods;
953 libusb20_dev_get_config_index(struct libusb20_device *pdev)
959 if (!pdev->is_opened) {
960 error = libusb20_dev_open(pdev, 0);
970 error = pdev->methods->get_config_index(pdev, &cfg_index);
972 cfg_index = 0xFF; /* current config index */
974 if (libusb20_dev_close(pdev)) {
982 libusb20_dev_get_mode(struct libusb20_device *pdev)
984 return (pdev->usb_mode);
988 libusb20_dev_get_speed(struct libusb20_device *pdev)
990 return (pdev->usb_speed);
993 /* if this function returns an error, the device is gone */
995 libusb20_dev_process(struct libusb20_device *pdev)
999 error = pdev->methods->process(pdev);
1004 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1006 struct pollfd pfd[1];
1008 if (!pdev->is_opened) {
1011 pfd[0].fd = pdev->file;
1012 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1015 if (poll(pfd, 1, timeout)) {
1016 /* ignore any error */
1022 libusb20_dev_free(struct libusb20_device *pdev)
1028 if (pdev->is_opened) {
1029 if (libusb20_dev_close(pdev)) {
1030 /* ignore any errors */
1038 libusb20_dev_get_info(struct libusb20_device *pdev,
1039 struct usb_device_info *pinfo)
1042 return (LIBUSB20_ERROR_INVALID_PARAM);
1044 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1048 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1050 return (pdev->beMethods->get_backend_name());
1054 libusb20_dev_get_desc(struct libusb20_device *pdev)
1056 return (pdev->usb_desc);
1060 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1062 pdev->debug = debug;
1067 libusb20_dev_get_debug(struct libusb20_device *pdev)
1069 return (pdev->debug);
1073 libusb20_dev_get_address(struct libusb20_device *pdev)
1075 return (pdev->device_address);
1079 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1081 return (pdev->parent_address);
1085 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1087 return (pdev->parent_port);
1091 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1093 return (pdev->bus_number);
1097 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1098 uint8_t iface_index, char *buf, uint8_t len)
1100 if ((buf == NULL) || (len == 0))
1101 return (LIBUSB20_ERROR_INVALID_PARAM);
1103 buf[0] = 0; /* set default string value */
1105 return (pdev->beMethods->dev_get_iface_desc(
1106 pdev, iface_index, buf, len));
1109 /* USB backend operations */
1112 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1113 uint16_t quirk_index, struct libusb20_quirk *pq)
1115 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1119 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1120 uint16_t quirk_index, struct libusb20_quirk *pq)
1122 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1126 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1127 struct libusb20_quirk *pq)
1129 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1133 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1134 struct libusb20_quirk *pq)
1136 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1140 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1142 return (pbe->methods->root_set_template(pbe, temp));
1146 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1153 return (pbe->methods->root_get_template(pbe, ptemp));
1156 struct libusb20_device *
1157 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1161 } else if (pdev == NULL) {
1162 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1164 pdev = TAILQ_NEXT(pdev, dev_entry);
1169 struct libusb20_backend *
1170 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1172 struct libusb20_backend *pbe;
1174 pbe = malloc(sizeof(*pbe));
1178 memset(pbe, 0, sizeof(*pbe));
1180 TAILQ_INIT(&(pbe->usb_devs));
1182 pbe->methods = methods; /* set backend methods */
1184 /* do the initial device scan */
1185 if (pbe->methods->init_backend) {
1186 pbe->methods->init_backend(pbe);
1191 struct libusb20_backend *
1192 libusb20_be_alloc_linux(void)
1194 struct libusb20_backend *pbe;
1197 pbe = libusb20_be_alloc(&libusb20_linux_backend);
1204 struct libusb20_backend *
1205 libusb20_be_alloc_ugen20(void)
1207 struct libusb20_backend *pbe;
1209 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1210 pbe = libusb20_be_alloc(&libusb20_ugen20_backend);
1217 struct libusb20_backend *
1218 libusb20_be_alloc_default(void)
1220 struct libusb20_backend *pbe;
1222 pbe = libusb20_be_alloc_linux();
1226 pbe = libusb20_be_alloc_ugen20();
1230 return (NULL); /* no backend found */
1234 libusb20_be_free(struct libusb20_backend *pbe)
1236 struct libusb20_device *pdev;
1242 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1243 libusb20_be_dequeue_device(pbe, pdev);
1244 libusb20_dev_free(pdev);
1246 if (pbe->methods->exit_backend) {
1247 pbe->methods->exit_backend(pbe);
1254 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1256 pdev->beMethods = pbe->methods; /* copy backend methods */
1257 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1261 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1262 struct libusb20_device *pdev)
1264 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1268 libusb20_strerror(int code)
1271 case LIBUSB20_SUCCESS:
1273 case LIBUSB20_ERROR_IO:
1274 return ("I/O error");
1275 case LIBUSB20_ERROR_INVALID_PARAM:
1276 return ("Invalid parameter");
1277 case LIBUSB20_ERROR_ACCESS:
1278 return ("Permissions error");
1279 case LIBUSB20_ERROR_NO_DEVICE:
1280 return ("No device");
1281 case LIBUSB20_ERROR_NOT_FOUND:
1282 return ("Not found");
1283 case LIBUSB20_ERROR_BUSY:
1284 return ("Device busy");
1285 case LIBUSB20_ERROR_TIMEOUT:
1287 case LIBUSB20_ERROR_OVERFLOW:
1288 return ("Overflow");
1289 case LIBUSB20_ERROR_PIPE:
1290 return ("Pipe error");
1291 case LIBUSB20_ERROR_INTERRUPTED:
1292 return ("Interrupted");
1293 case LIBUSB20_ERROR_NO_MEM:
1294 return ("Out of memory");
1295 case LIBUSB20_ERROR_NOT_SUPPORTED:
1296 return ("Not supported");
1297 case LIBUSB20_ERROR_OTHER:
1298 return ("Other error");
1300 return ("Unknown error");
1305 libusb20_error_name(int code)
1308 case LIBUSB20_SUCCESS:
1309 return ("LIBUSB20_SUCCESS");
1310 case LIBUSB20_ERROR_IO:
1311 return ("LIBUSB20_ERROR_IO");
1312 case LIBUSB20_ERROR_INVALID_PARAM:
1313 return ("LIBUSB20_ERROR_INVALID_PARAM");
1314 case LIBUSB20_ERROR_ACCESS:
1315 return ("LIBUSB20_ERROR_ACCESS");
1316 case LIBUSB20_ERROR_NO_DEVICE:
1317 return ("LIBUSB20_ERROR_NO_DEVICE");
1318 case LIBUSB20_ERROR_NOT_FOUND:
1319 return ("LIBUSB20_ERROR_NOT_FOUND");
1320 case LIBUSB20_ERROR_BUSY:
1321 return ("LIBUSB20_ERROR_BUSY");
1322 case LIBUSB20_ERROR_TIMEOUT:
1323 return ("LIBUSB20_ERROR_TIMEOUT");
1324 case LIBUSB20_ERROR_OVERFLOW:
1325 return ("LIBUSB20_ERROR_OVERFLOW");
1326 case LIBUSB20_ERROR_PIPE:
1327 return ("LIBUSB20_ERROR_PIPE");
1328 case LIBUSB20_ERROR_INTERRUPTED:
1329 return ("LIBUSB20_ERROR_INTERRUPTED");
1330 case LIBUSB20_ERROR_NO_MEM:
1331 return ("LIBUSB20_ERROR_NO_MEM");
1332 case LIBUSB20_ERROR_NOT_SUPPORTED:
1333 return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1334 case LIBUSB20_ERROR_OTHER:
1335 return ("LIBUSB20_ERROR_OTHER");
1337 return ("LIBUSB20_ERROR_UNKNOWN");