3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * This file contains the emulation layer for LibUSB v0.1 from sourceforge.
33 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
34 #include LIBUSB_GLOBAL_INCLUDE_FILE
41 #include <sys/queue.h>
45 #include "libusb20_desc.h"
46 #include "libusb20_int.h"
50 * The two following macros were taken from the original LibUSB v0.1
51 * for sake of compatibility:
53 #define LIST_ADD(begin, ent) \
57 ent->next->prev = ent; \
65 #define LIST_DEL(begin, ent) \
68 ent->prev->next = ent->next; \
73 ent->next->prev = ent->prev; \
79 struct usb_bus *usb_busses = NULL;
81 static struct usb_bus usb_global_bus = {
82 .dirname = {"/dev/usb"},
87 static struct libusb20_backend *usb_backend = NULL;
89 struct usb_parse_state {
92 struct libusb20_endpoint *currep;
93 struct libusb20_interface *currifc;
94 struct libusb20_config *currcfg;
95 struct libusb20_me_struct *currextra;
99 struct usb_config_descriptor *currcfg;
100 struct usb_interface_descriptor *currifc;
101 struct usb_endpoint_descriptor *currep;
102 struct usb_interface *currifcw;
109 static struct libusb20_transfer *
110 usb_get_transfer_by_ep_no(usb_dev_handle * dev, uint8_t ep_no)
112 struct libusb20_device *pdev = (void *)dev;
113 struct libusb20_transfer *xfer;
119 x = (ep_no & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 2;
121 if (ep_no & LIBUSB20_ENDPOINT_DIR_MASK) {
122 /* this is an IN endpoint */
125 speed = libusb20_dev_get_speed(pdev);
127 /* select a sensible buffer size */
128 if (speed == LIBUSB20_SPEED_LOW) {
130 } else if (speed == LIBUSB20_SPEED_FULL) {
132 } else if (speed == LIBUSB20_SPEED_SUPER) {
138 xfer = libusb20_tr_get_pointer(pdev, x);
143 err = libusb20_tr_open(xfer, bufsize, 1, ep_no);
144 if (err == LIBUSB20_ERROR_BUSY) {
155 usb_open(struct usb_device *dev)
159 err = libusb20_dev_open(dev->dev, 16 * 2);
160 if (err == LIBUSB20_ERROR_BUSY) {
162 * Workaround buggy USB applications which open the USB
163 * device multiple times:
171 * Dequeue USB device from backend queue so that it does not get
172 * freed when the backend is re-scanned:
174 libusb20_be_dequeue_device(usb_backend, dev->dev);
180 usb_close(usb_dev_handle * udev)
182 struct usb_device *dev;
185 err = libusb20_dev_close((void *)udev);
190 if (usb_backend != NULL) {
192 * Enqueue USB device to backend queue so that it gets freed
193 * when the backend is re-scanned:
195 libusb20_be_enqueue_device(usb_backend, (void *)udev);
198 * The backend is gone. Free device data so that we
199 * don't start leaking memory!
201 dev = usb_device(udev);
202 libusb20_dev_free((void *)udev);
203 LIST_DEL(usb_global_bus.devices, dev);
210 usb_get_string(usb_dev_handle * dev, int strindex,
211 int langid, char *buf, size_t buflen)
221 err = libusb20_dev_req_string_sync((void *)dev,
222 strindex, langid, buf, buflen);
231 usb_get_string_simple(usb_dev_handle * dev, int strindex,
232 char *buf, size_t buflen)
242 err = libusb20_dev_req_string_simple_sync((void *)dev,
243 strindex, buf, buflen);
248 return (strlen(buf));
252 usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type,
253 uint8_t ep_index, void *buf, int size)
255 memset(buf, 0, size);
263 return (usb_control_msg(udev, ep | USB_ENDPOINT_IN,
264 USB_REQ_GET_DESCRIPTOR, (type << 8) + ep_index, 0,
269 usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t desc_index,
272 memset(buf, 0, size);
280 return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
281 (type << 8) + desc_index, 0, buf, size, 1000));
285 usb_parse_descriptor(uint8_t *source, char *description, void *dest)
287 uint8_t *sp = source;
293 for (cp = description; *cp; cp++) {
295 case 'b': /* 8-bit byte */
299 * 16-bit word, convert from little endian to CPU
302 w = (sp[1] << 8) | sp[0];
304 /* Align to word boundary */
305 dp += ((dp - (uint8_t *)0) & 1);
306 *((uint16_t *)dp) = w;
310 * 32-bit dword, convert from little endian to CPU
313 d = (sp[3] << 24) | (sp[2] << 16) |
314 (sp[1] << 8) | sp[0];
316 /* Align to word boundary */
317 dp += ((dp - (uint8_t *)0) & 1);
318 /* Align to double word boundary */
319 dp += ((dp - (uint8_t *)0) & 2);
320 *((uint32_t *)dp) = d;
325 return (sp - source);
329 usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen)
334 ptr = ps->a.currextra->ptr;
335 len = ps->a.currextra->len;
337 if (ps->preparse == 0) {
338 memcpy(ps->b.currextra, ptr, len);
339 *pptr = ps->b.currextra;
342 ps->b.currextra += len;
347 usb_parse_endpoint(struct usb_parse_state *ps)
349 struct usb_endpoint_descriptor *bep;
350 struct libusb20_endpoint *aep;
353 bep = ps->b.currep++;
355 if (ps->preparse == 0) {
356 /* copy descriptor fields */
357 bep->bLength = aep->desc.bLength;
358 bep->bDescriptorType = aep->desc.bDescriptorType;
359 bep->bEndpointAddress = aep->desc.bEndpointAddress;
360 bep->bmAttributes = aep->desc.bmAttributes;
361 bep->wMaxPacketSize = aep->desc.wMaxPacketSize;
362 bep->bInterval = aep->desc.bInterval;
363 bep->bRefresh = aep->desc.bRefresh;
364 bep->bSynchAddress = aep->desc.bSynchAddress;
366 ps->a.currextra = &aep->extra;
367 usb_parse_extra(ps, &bep->extra, &bep->extralen);
372 usb_parse_iface_sub(struct usb_parse_state *ps)
374 struct libusb20_interface *aifc;
375 struct usb_interface_descriptor *bifc;
378 aifc = ps->a.currifc;
379 bifc = ps->b.currifc++;
381 if (ps->preparse == 0) {
382 /* copy descriptor fields */
383 bifc->bLength = aifc->desc.bLength;
384 bifc->bDescriptorType = aifc->desc.bDescriptorType;
385 bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber;
386 bifc->bAlternateSetting = aifc->desc.bAlternateSetting;
387 bifc->bNumEndpoints = aifc->num_endpoints;
388 bifc->bInterfaceClass = aifc->desc.bInterfaceClass;
389 bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass;
390 bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol;
391 bifc->iInterface = aifc->desc.iInterface;
392 bifc->endpoint = ps->b.currep;
394 for (x = 0; x != aifc->num_endpoints; x++) {
395 ps->a.currep = aifc->endpoints + x;
396 usb_parse_endpoint(ps);
399 ps->a.currextra = &aifc->extra;
400 usb_parse_extra(ps, &bifc->extra, &bifc->extralen);
405 usb_parse_iface(struct usb_parse_state *ps)
407 struct libusb20_interface *aifc;
408 struct usb_interface *bifc;
411 aifc = ps->a.currifc;
412 bifc = ps->b.currifcw++;
414 if (ps->preparse == 0) {
415 /* initialise interface wrapper */
416 bifc->altsetting = ps->b.currifc;
417 bifc->num_altsetting = aifc->num_altsetting + 1;
419 usb_parse_iface_sub(ps);
421 for (x = 0; x != aifc->num_altsetting; x++) {
422 ps->a.currifc = aifc->altsetting + x;
423 usb_parse_iface_sub(ps);
429 usb_parse_config(struct usb_parse_state *ps)
431 struct libusb20_config *acfg;
432 struct usb_config_descriptor *bcfg;
435 acfg = ps->a.currcfg;
436 bcfg = ps->b.currcfg;
438 if (ps->preparse == 0) {
439 /* initialise config wrapper */
440 bcfg->bLength = acfg->desc.bLength;
441 bcfg->bDescriptorType = acfg->desc.bDescriptorType;
442 bcfg->wTotalLength = acfg->desc.wTotalLength;
443 bcfg->bNumInterfaces = acfg->num_interface;
444 bcfg->bConfigurationValue = acfg->desc.bConfigurationValue;
445 bcfg->iConfiguration = acfg->desc.iConfiguration;
446 bcfg->bmAttributes = acfg->desc.bmAttributes;
447 bcfg->MaxPower = acfg->desc.bMaxPower;
448 bcfg->interface = ps->b.currifcw;
450 for (x = 0; x != acfg->num_interface; x++) {
451 ps->a.currifc = acfg->interface + x;
455 ps->a.currextra = &acfg->extra;
456 usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen);
461 usb_parse_configuration(struct usb_config_descriptor *config,
464 struct usb_parse_state ps;
471 if ((buffer == NULL) || (config == NULL)) {
474 memset(&ps, 0, sizeof(ps));
476 ps.a.currcfg = libusb20_parse_config_desc(buffer);
477 ps.b.currcfg = config;
478 if (ps.a.currcfg == NULL) {
479 /* could not parse config or out of memory */
482 /* do the pre-parse */
484 usb_parse_config(&ps);
486 a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0));
487 b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0));
488 c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0));
489 d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0));
491 /* allocate memory for our configuration */
492 ptr = malloc(a + b + c + d);
494 /* free config structure */
499 /* "currifcw" must be first, hence this pointer is freed */
500 ps.b.currifcw = (void *)(ptr);
501 ps.b.currifc = (void *)(ptr + a);
502 ps.b.currep = (void *)(ptr + a + b);
503 ps.b.currextra = (void *)(ptr + a + b + c);
505 /* generate a libusb v0.1 compatible structure */
507 usb_parse_config(&ps);
509 /* free config structure */
512 return (0); /* success */
516 usb_destroy_configuration(struct usb_device *dev)
520 if (dev->config == NULL) {
523 for (c = 0; c != dev->descriptor.bNumConfigurations; c++) {
524 struct usb_config_descriptor *cf = &dev->config[c];
526 if (cf->interface != NULL) {
528 cf->interface = NULL;
538 usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
540 struct usb_device *dev;
541 struct libusb20_device *pdev;
552 dev = usb_device(udev);
555 if (dev->descriptor.bNumConfigurations == 0) {
559 size = dev->descriptor.bNumConfigurations *
560 sizeof(struct usb_config_descriptor);
562 dev->config = malloc(size);
563 if (dev->config == NULL) {
567 memset(dev->config, 0, size);
569 for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {
571 error = (pdev->methods->get_config_desc_full) (
572 pdev, &ptr, &len, x);
575 usb_destroy_configuration(dev);
578 usb_parse_configuration(dev->config + x, ptr);
580 /* free config buffer */
587 usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size,
588 int timeout, int is_intr)
590 struct libusb20_transfer *xfer;
596 xfer = usb_get_transfer_by_ep_no(dev, ep);
600 if (libusb20_tr_pending(xfer)) {
601 /* there is already a transfer ongoing */
604 maxsize = libusb20_tr_get_max_total_length(xfer);
608 * We allow transferring zero bytes which is the same
609 * equivalent to a zero length USB packet.
614 if (temp > maxsize) {
615 /* find maximum possible length */
619 libusb20_tr_setup_intr(xfer, bytes, temp, timeout);
621 libusb20_tr_setup_bulk(xfer, bytes, temp, timeout);
623 libusb20_tr_start(xfer);
627 if (libusb20_dev_process((void *)dev) != 0) {
628 /* device detached */
631 if (libusb20_tr_pending(xfer) == 0) {
632 /* transfer complete */
635 /* wait for USB event from kernel */
636 libusb20_dev_wait_process((void *)dev, -1);
639 switch (libusb20_tr_get_status(xfer)) {
643 case LIBUSB20_TRANSFER_TIMED_OUT:
644 /* transfer timeout */
647 /* other transfer error */
650 actlen = libusb20_tr_get_actual_length(xfer);
655 if (actlen != temp) {
661 return (bytes - oldbytes);
665 usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes,
666 int size, int timeout)
668 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
669 bytes, size, timeout, 0));
673 usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes,
674 int size, int timeout)
676 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
677 bytes, size, timeout, 0));
681 usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes,
682 int size, int timeout)
684 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
685 bytes, size, timeout, 1));
689 usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes,
690 int size, int timeout)
692 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
693 bytes, size, timeout, 1));
697 usb_control_msg(usb_dev_handle * dev, int requesttype, int request,
698 int value, int wIndex, char *bytes, int size, int timeout)
700 struct LIBUSB20_CONTROL_SETUP_DECODED req;
704 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
706 req.bmRequestType = requesttype;
707 req.bRequest = request;
712 err = libusb20_dev_request_sync((void *)dev, &req, bytes,
713 &actlen, timeout, 0);
722 usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
724 struct usb_device *dev;
729 * Need to translate from "bConfigurationValue" to
730 * configuration index:
733 if (bConfigurationValue == 0) {
737 /* lookup configuration index */
738 dev = usb_device(udev);
740 /* check if the configuration array is not there */
741 if (dev->config == NULL) {
745 if (i == dev->descriptor.bNumConfigurations) {
746 /* "bConfigurationValue" not found */
749 if ((dev->config + i)->bConfigurationValue ==
750 bConfigurationValue) {
756 err = libusb20_dev_set_config_index((void *)udev, i);
765 usb_claim_interface(usb_dev_handle * dev, int interface)
767 struct libusb20_device *pdev = (void *)dev;
769 pdev->claimed_interface = interface;
775 usb_release_interface(usb_dev_handle * dev, int interface)
782 usb_set_altinterface(usb_dev_handle * dev, int alternate)
784 struct libusb20_device *pdev = (void *)dev;
788 iface = pdev->claimed_interface;
790 err = libusb20_dev_set_alt_index((void *)dev, iface, alternate);
799 usb_resetep(usb_dev_handle * dev, unsigned int ep)
801 /* emulate an endpoint reset through clear-STALL */
802 return (usb_clear_halt(dev, ep));
806 usb_clear_halt(usb_dev_handle * dev, unsigned int ep)
808 struct libusb20_transfer *xfer;
810 xfer = usb_get_transfer_by_ep_no(dev, ep);
814 libusb20_tr_clear_stall_sync(xfer);
820 usb_reset(usb_dev_handle * dev)
824 err = libusb20_dev_reset((void *)dev);
830 * Be compatible with LibUSB from sourceforge and close the
831 * handle after reset!
833 return (usb_close(dev));
837 usb_check_connected(usb_dev_handle * dev)
841 err = libusb20_dev_check_connected((void *)dev);
853 return ("Unknown error");
864 usb_set_debug(int level)
866 /* use kernel UGEN debugging if you need to see what is going on */
871 usb_find_busses(void)
873 usb_busses = &usb_global_bus;
878 usb_find_devices(void)
880 struct libusb20_device *pdev;
881 struct usb_device *udev;
882 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
886 /* cleanup after last device search */
887 /* close all opened devices, if any */
889 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
890 udev = pdev->privLuData;
891 libusb20_be_dequeue_device(usb_backend, pdev);
892 libusb20_dev_free(pdev);
894 LIST_DEL(usb_global_bus.devices, udev);
899 /* free old USB backend, if any */
901 libusb20_be_free(usb_backend);
903 /* do a new backend device search */
904 usb_backend = libusb20_be_alloc_default();
905 if (usb_backend == NULL) {
908 /* iterate all devices */
912 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
913 udev = malloc(sizeof(*udev));
917 memset(udev, 0, sizeof(*udev));
919 udev->bus = &usb_global_bus;
921 snprintf(udev->filename, sizeof(udev->filename),
923 libusb20_dev_get_bus_number(pdev),
924 libusb20_dev_get_address(pdev));
926 ddesc = libusb20_dev_get_device_desc(pdev);
928 udev->descriptor.bLength = sizeof(udev->descriptor);
929 udev->descriptor.bDescriptorType = ddesc->bDescriptorType;
930 udev->descriptor.bcdUSB = ddesc->bcdUSB;
931 udev->descriptor.bDeviceClass = ddesc->bDeviceClass;
932 udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass;
933 udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol;
934 udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0;
935 udev->descriptor.idVendor = ddesc->idVendor;
936 udev->descriptor.idProduct = ddesc->idProduct;
937 udev->descriptor.bcdDevice = ddesc->bcdDevice;
938 udev->descriptor.iManufacturer = ddesc->iManufacturer;
939 udev->descriptor.iProduct = ddesc->iProduct;
940 udev->descriptor.iSerialNumber = ddesc->iSerialNumber;
941 udev->descriptor.bNumConfigurations =
942 ddesc->bNumConfigurations;
943 if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
944 /* truncate number of configurations */
945 udev->descriptor.bNumConfigurations = USB_MAXCONFIG;
947 udev->devnum = devnum++;
948 /* link together the two structures */
950 pdev->privLuData = udev;
952 err = libusb20_dev_open(pdev, 0);
954 /* XXX get all config descriptors by default */
955 usb_fetch_and_parse_descriptors((void *)pdev);
956 libusb20_dev_close(pdev);
958 LIST_ADD(usb_global_bus.devices, udev);
961 return (devnum - 1); /* success */
965 usb_device(usb_dev_handle * dev)
967 struct libusb20_device *pdev;
971 return (pdev->privLuData);
981 usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen)
983 struct libusb20_device *pdev;
996 err = libusb20_dev_get_iface_desc(pdev, interface, name, namelen);
1000 /* we only want the driver name */
1001 ptr = strstr(name, ":");
1009 usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface)
1011 struct libusb20_device *pdev;
1019 err = libusb20_dev_detach_kernel_driver(pdev, interface);