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_kernel_driver_active (void *)dummy_int
75 #define dummy_detach_kernel_driver (void *)dummy_int
76 #define dummy_do_request_sync (void *)dummy_int
77 #define dummy_tr_open (void *)dummy_int
78 #define dummy_tr_close (void *)dummy_int
79 #define dummy_tr_clear_stall_sync (void *)dummy_int
80 #define dummy_process (void *)dummy_int
81 #define dummy_dev_info (void *)dummy_int
82 #define dummy_dev_get_iface_driver (void *)dummy_int
84 #define dummy_tr_submit (void *)dummy_void
85 #define dummy_tr_cancel_async (void *)dummy_void
87 static const struct libusb20_device_methods libusb20_dummy_methods = {
88 LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
92 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
98 if (!xfer->is_pending) {
99 xfer->status = LIBUSB20_TRANSFER_START;
101 xfer->is_pending = 0;
104 xfer->callback(xfer);
106 if (xfer->is_restart) {
107 xfer->is_restart = 0;
110 if (xfer->is_draining &&
111 (!xfer->is_pending)) {
112 xfer->is_draining = 0;
113 xfer->status = LIBUSB20_TRANSFER_DRAINED;
114 xfer->callback(xfer);
120 libusb20_tr_close(struct libusb20_transfer *xfer)
124 if (!xfer->is_opened) {
125 return (LIBUSB20_ERROR_OTHER);
127 error = xfer->pdev->methods->tr_close(xfer);
132 if (xfer->ppBuffer) {
133 free(xfer->ppBuffer);
135 /* reset variable fields in case the transfer is opened again */
139 xfer->is_pending = 0;
141 xfer->is_draining = 0;
142 xfer->is_restart = 0;
149 xfer->maxTotalLength = 0;
150 xfer->maxPacketLen = 0;
155 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
156 uint32_t MaxFrameCount, uint8_t ep_no)
161 if (xfer->is_opened) {
162 return (LIBUSB20_ERROR_BUSY);
164 if (MaxFrameCount == 0) {
165 return (LIBUSB20_ERROR_INVALID_PARAM);
167 xfer->maxFrames = MaxFrameCount;
169 size = MaxFrameCount * sizeof(xfer->pLength[0]);
170 xfer->pLength = malloc(size);
171 if (xfer->pLength == NULL) {
172 return (LIBUSB20_ERROR_NO_MEM);
174 memset(xfer->pLength, 0, size);
176 size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
177 xfer->ppBuffer = malloc(size);
178 if (xfer->ppBuffer == NULL) {
180 return (LIBUSB20_ERROR_NO_MEM);
182 memset(xfer->ppBuffer, 0, size);
184 error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
185 MaxFrameCount, ep_no);
188 free(xfer->ppBuffer);
196 struct libusb20_transfer *
197 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
199 if (trIndex >= pdev->nTransfer) {
202 return (pdev->pTransfer + trIndex);
206 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
208 return (xfer->aFrames);
212 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
214 return (xfer->timeComplete);
218 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
223 for (x = 0; x != xfer->aFrames; x++) {
224 actlen += xfer->pLength[x];
230 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
232 return (xfer->maxFrames);
236 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
239 * Special Case NOTE: If the packet multiplier is non-zero for
240 * High Speed USB, the value returned is equal to
241 * "wMaxPacketSize * multiplier" !
243 return (xfer->maxPacketLen);
247 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
249 return (xfer->maxTotalLength);
253 libusb20_tr_get_status(struct libusb20_transfer *xfer)
255 return (xfer->status);
259 libusb20_tr_pending(struct libusb20_transfer *xfer)
261 return (xfer->is_pending);
265 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
267 return (xfer->priv_sc0);
271 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
273 return (xfer->priv_sc1);
277 libusb20_tr_stop(struct libusb20_transfer *xfer)
279 if (!xfer->is_opened) {
280 /* transfer is not opened */
283 if (!xfer->is_pending) {
284 /* transfer not pending */
287 if (xfer->is_cancel) {
288 /* already cancelling */
291 xfer->is_cancel = 1; /* we are cancelling */
293 xfer->pdev->methods->tr_cancel_async(xfer);
298 libusb20_tr_drain(struct libusb20_transfer *xfer)
300 if (!xfer->is_opened) {
301 /* transfer is not opened */
304 /* make sure that we are cancelling */
305 libusb20_tr_stop(xfer);
307 if (xfer->is_pending) {
308 xfer->is_draining = 1;
314 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
316 xfer->pdev->methods->tr_clear_stall_sync(xfer);
321 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
323 xfer->ppBuffer[frIndex] = buffer;
328 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
335 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
342 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
344 return (xfer->pLength[frIndex]);
348 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
350 xfer->pLength[frIndex] = length;
355 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
357 xfer->priv_sc0 = sc0;
362 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
364 xfer->priv_sc1 = sc1;
369 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
371 xfer->timeout = timeout;
376 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
378 if (nFrames > xfer->maxFrames) {
379 /* should not happen */
380 nFrames = xfer->maxFrames;
382 xfer->nFrames = nFrames;
387 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
389 xfer->ppBuffer[0] = pBuf;
390 xfer->pLength[0] = length;
391 xfer->timeout = timeout;
397 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
401 xfer->ppBuffer[0] = psetup;
402 xfer->pLength[0] = 8; /* fixed */
403 xfer->timeout = timeout;
405 len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
409 xfer->ppBuffer[1] = pBuf;
410 xfer->pLength[1] = len;
418 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
420 xfer->ppBuffer[0] = pBuf;
421 xfer->pLength[0] = length;
422 xfer->timeout = timeout;
428 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
430 if (frIndex >= xfer->maxFrames) {
431 /* should not happen */
434 xfer->ppBuffer[frIndex] = pBuf;
435 xfer->pLength[frIndex] = length;
440 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
441 void *pbuf, uint32_t length, uint32_t *pactlen,
444 struct libusb20_device *pdev = xfer->pdev;
445 uint32_t transfer_max;
446 uint32_t transfer_act;
449 /* set some sensible default value */
453 /* check for error condition */
454 if (libusb20_tr_pending(xfer))
455 return (LIBUSB20_ERROR_OTHER);
458 /* compute maximum transfer length */
460 libusb20_tr_get_max_total_length(xfer);
462 if (transfer_max > length)
463 transfer_max = length;
465 /* setup bulk or interrupt transfer */
466 libusb20_tr_setup_bulk(xfer, pbuf,
467 transfer_max, timeout);
469 /* start the transfer */
470 libusb20_tr_start(xfer);
472 /* wait for transfer completion */
473 while (libusb20_dev_process(pdev) == 0) {
475 if (libusb20_tr_pending(xfer) == 0)
478 libusb20_dev_wait_process(pdev, -1);
481 transfer_act = libusb20_tr_get_actual_length(xfer);
483 /* update actual length, if any */
485 pactlen[0] += transfer_act;
487 /* check transfer status */
488 retval = libusb20_tr_get_status(xfer);
492 /* check for short transfer */
493 if (transfer_act != transfer_max)
496 /* update buffer pointer and length */
497 pbuf = ((uint8_t *)pbuf) + transfer_max;
498 length = length - transfer_max;
500 } while (length != 0);
506 libusb20_tr_submit(struct libusb20_transfer *xfer)
508 if (!xfer->is_opened) {
509 /* transfer is not opened */
512 if (xfer->is_pending) {
513 /* should not happen */
516 xfer->is_pending = 1; /* we are pending */
517 xfer->is_cancel = 0; /* not cancelling */
518 xfer->is_restart = 0; /* not restarting */
520 xfer->pdev->methods->tr_submit(xfer);
525 libusb20_tr_start(struct libusb20_transfer *xfer)
527 if (!xfer->is_opened) {
528 /* transfer is not opened */
531 if (xfer->is_pending) {
532 if (xfer->is_cancel) {
533 /* cancelling - restart */
534 xfer->is_restart = 1;
536 /* transfer not pending */
539 /* get into the callback */
540 libusb20_tr_callback_wrapper(xfer);
544 /* USB device operations */
547 libusb20_dev_close(struct libusb20_device *pdev)
549 struct libusb20_transfer *xfer;
553 if (!pdev->is_opened) {
554 return (LIBUSB20_ERROR_OTHER);
556 for (x = 0; x != pdev->nTransfer; x++) {
557 xfer = pdev->pTransfer + x;
559 if (!xfer->is_opened) {
560 /* transfer is not opened */
564 libusb20_tr_drain(xfer);
566 libusb20_tr_close(xfer);
569 if (pdev->pTransfer != NULL) {
570 free(pdev->pTransfer);
571 pdev->pTransfer = NULL;
573 error = pdev->beMethods->close_device(pdev);
575 pdev->methods = &libusb20_dummy_methods;
580 * The following variable is only used by the libusb v0.1
583 pdev->claimed_interface = 0;
589 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
593 error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
597 struct LIBUSB20_DEVICE_DESC_DECODED *
598 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
600 return (&(pdev->ddesc));
604 libusb20_dev_get_fd(struct libusb20_device *pdev)
610 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
614 error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
619 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
621 struct libusb20_transfer *xfer;
626 if (pdev->is_opened) {
627 return (LIBUSB20_ERROR_BUSY);
629 if (nTransferMax >= 256) {
630 return (LIBUSB20_ERROR_INVALID_PARAM);
631 } else if (nTransferMax != 0) {
632 size = sizeof(pdev->pTransfer[0]) * nTransferMax;
633 pdev->pTransfer = malloc(size);
634 if (pdev->pTransfer == NULL) {
635 return (LIBUSB20_ERROR_NO_MEM);
637 memset(pdev->pTransfer, 0, size);
639 /* initialise all transfers */
640 for (x = 0; x != nTransferMax; x++) {
642 xfer = pdev->pTransfer + x;
646 xfer->callback = &dummy_callback;
649 /* set "nTransfer" early */
650 pdev->nTransfer = nTransferMax;
652 error = pdev->beMethods->open_device(pdev, nTransferMax);
655 if (pdev->pTransfer != NULL) {
656 free(pdev->pTransfer);
657 pdev->pTransfer = NULL;
660 pdev->file_ctrl = -1;
669 libusb20_dev_reset(struct libusb20_device *pdev)
673 error = pdev->methods->reset_device(pdev);
678 libusb20_dev_check_connected(struct libusb20_device *pdev)
682 error = pdev->methods->check_connected(pdev);
687 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
691 error = pdev->methods->set_power_mode(pdev, power_mode);
696 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
701 error = pdev->methods->get_power_mode(pdev, &power_mode);
703 power_mode = LIBUSB20_POWER_ON; /* fake power mode */
708 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
712 error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
717 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
721 error = pdev->methods->set_config_index(pdev, configIndex);
726 libusb20_dev_request_sync(struct libusb20_device *pdev,
727 struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
728 uint16_t *pactlen, uint32_t timeout, uint8_t flags)
732 error = pdev->methods->do_request_sync(pdev,
733 setup, data, pactlen, timeout, flags);
738 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
739 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
741 struct LIBUSB20_CONTROL_SETUP_DECODED req;
744 /* make sure memory is initialised */
749 return (LIBUSB20_ERROR_INVALID_PARAM);
751 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
754 * We need to read the USB string in two steps else some USB
755 * devices will complain.
758 LIBUSB20_REQUEST_TYPE_STANDARD |
759 LIBUSB20_RECIPIENT_DEVICE |
760 LIBUSB20_ENDPOINT_IN;
761 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
762 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
764 req.wLength = 4; /* bytes */
766 error = libusb20_dev_request_sync(pdev, &req,
767 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
771 req.wLength = *(uint8_t *)ptr; /* bytes */
772 if (req.wLength > len) {
773 /* partial string read */
776 error = libusb20_dev_request_sync(pdev, &req,
777 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
782 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) {
783 return (LIBUSB20_ERROR_OTHER);
785 return (0); /* success */
789 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
790 uint8_t str_index, void *ptr, uint16_t len)
801 /* the following code derives from the FreeBSD USB kernel */
803 if ((len < 1) || (ptr == NULL)) {
804 /* too short buffer */
805 return (LIBUSB20_ERROR_INVALID_PARAM);
807 error = libusb20_dev_req_string_sync(pdev,
808 0, 0, temp, sizeof(temp));
810 *(uint8_t *)ptr = 0; /* zero terminate */
813 langid = temp[2] | (temp[3] << 8);
815 error = libusb20_dev_req_string_sync(pdev, str_index,
816 langid, temp, sizeof(temp));
818 *(uint8_t *)ptr = 0; /* zero terminate */
822 /* string length is too short */
823 *(uint8_t *)ptr = 0; /* zero terminate */
824 return (LIBUSB20_ERROR_OTHER);
826 /* reserve one byte for terminating zero */
829 /* find maximum length */
830 n = (temp[0] / 2) - 1;
834 /* reset swap state */
837 /* setup output buffer pointer */
840 /* convert and filter */
841 for (i = 0; (i != n); i++) {
842 c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
844 /* convert from Unicode, handle buggy strings */
845 if (((c & 0xff00) == 0) && (swap & 1)) {
846 /* Little Endian, default */
849 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
854 /* skip invalid character */
858 * Filter by default - we don't allow greater and less than
859 * signs because they might confuse the dmesg printouts!
861 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
862 /* skip invalid character */
867 *buf = 0; /* zero terminate string */
872 struct libusb20_config *
873 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
875 struct libusb20_config *retval = NULL;
881 if (!pdev->is_opened) {
882 error = libusb20_dev_open(pdev, 0);
890 error = pdev->methods->get_config_desc_full(pdev,
891 &ptr, &len, configIndex);
896 /* parse new config descriptor */
897 retval = libusb20_parse_config_desc(ptr);
899 /* free config descriptor */
904 error = libusb20_dev_close(pdev);
909 struct libusb20_device *
910 libusb20_dev_alloc(void)
912 struct libusb20_device *pdev;
914 pdev = malloc(sizeof(*pdev));
918 memset(pdev, 0, sizeof(*pdev));
921 pdev->file_ctrl = -1;
922 pdev->methods = &libusb20_dummy_methods;
927 libusb20_dev_get_config_index(struct libusb20_device *pdev)
933 if (!pdev->is_opened) {
934 error = libusb20_dev_open(pdev, 0);
944 error = pdev->methods->get_config_index(pdev, &cfg_index);
946 cfg_index = 0 - 1; /* current config index */
949 if (libusb20_dev_close(pdev)) {
957 libusb20_dev_get_mode(struct libusb20_device *pdev)
959 return (pdev->usb_mode);
963 libusb20_dev_get_speed(struct libusb20_device *pdev)
965 return (pdev->usb_speed);
968 /* if this function returns an error, the device is gone */
970 libusb20_dev_process(struct libusb20_device *pdev)
974 error = pdev->methods->process(pdev);
979 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
981 struct pollfd pfd[1];
983 if (!pdev->is_opened) {
986 pfd[0].fd = pdev->file;
987 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
990 if (poll(pfd, 1, timeout)) {
991 /* ignore any error */
997 libusb20_dev_free(struct libusb20_device *pdev)
1003 if (pdev->is_opened) {
1004 if (libusb20_dev_close(pdev)) {
1005 /* ignore any errors */
1013 libusb20_dev_get_info(struct libusb20_device *pdev,
1014 struct usb_device_info *pinfo)
1017 return (LIBUSB20_ERROR_INVALID_PARAM);
1019 return (pdev->beMethods->dev_get_info(pdev, pinfo));
1023 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1025 return (pdev->beMethods->get_backend_name());
1029 libusb20_dev_get_desc(struct libusb20_device *pdev)
1031 return (pdev->usb_desc);
1035 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1037 pdev->debug = debug;
1042 libusb20_dev_get_debug(struct libusb20_device *pdev)
1044 return (pdev->debug);
1048 libusb20_dev_get_address(struct libusb20_device *pdev)
1050 return (pdev->device_address);
1054 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1056 return (pdev->bus_number);
1060 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1061 uint8_t iface_index, char *buf, uint8_t len)
1063 if ((buf == NULL) || (len == 0))
1064 return (LIBUSB20_ERROR_INVALID_PARAM);
1066 return (pdev->beMethods->dev_get_iface_desc(
1067 pdev, iface_index, buf, len));
1070 /* USB backend operations */
1073 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1074 uint16_t quirk_index, struct libusb20_quirk *pq)
1076 return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1080 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1081 uint16_t quirk_index, struct libusb20_quirk *pq)
1083 return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1087 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1088 struct libusb20_quirk *pq)
1090 return (pbe->methods->root_add_dev_quirk(pbe, pq));
1094 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1095 struct libusb20_quirk *pq)
1097 return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1101 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1103 return (pbe->methods->root_set_template(pbe, temp));
1107 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1114 return (pbe->methods->root_get_template(pbe, ptemp));
1117 struct libusb20_device *
1118 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1122 } else if (pdev == NULL) {
1123 pdev = TAILQ_FIRST(&(pbe->usb_devs));
1125 pdev = TAILQ_NEXT(pdev, dev_entry);
1130 struct libusb20_backend *
1131 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1133 struct libusb20_backend *pbe;
1135 pbe = malloc(sizeof(*pbe));
1139 memset(pbe, 0, sizeof(*pbe));
1141 TAILQ_INIT(&(pbe->usb_devs));
1143 pbe->methods = methods; /* set backend methods */
1145 /* do the initial device scan */
1146 if (pbe->methods->init_backend) {
1147 pbe->methods->init_backend(pbe);
1152 struct libusb20_backend *
1153 libusb20_be_alloc_linux(void)
1155 struct libusb20_backend *pbe;
1158 pbe = libusb20_be_alloc(&libusb20_linux_backend);
1165 struct libusb20_backend *
1166 libusb20_be_alloc_ugen20(void)
1168 struct libusb20_backend *pbe;
1171 pbe = libusb20_be_alloc(&libusb20_ugen20_backend);
1178 struct libusb20_backend *
1179 libusb20_be_alloc_default(void)
1181 struct libusb20_backend *pbe;
1183 pbe = libusb20_be_alloc_linux();
1187 pbe = libusb20_be_alloc_ugen20();
1191 return (NULL); /* no backend found */
1195 libusb20_be_free(struct libusb20_backend *pbe)
1197 struct libusb20_device *pdev;
1203 while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1204 libusb20_be_dequeue_device(pbe, pdev);
1205 libusb20_dev_free(pdev);
1207 if (pbe->methods->exit_backend) {
1208 pbe->methods->exit_backend(pbe);
1215 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1217 pdev->beMethods = pbe->methods; /* copy backend methods */
1218 TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1222 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1223 struct libusb20_device *pdev)
1225 TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);