3 * Copyright (c) 2008 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
28 * This file contains the emulation layer for LibUSB v0.1 from sourceforge.
31 #include <sys/queue.h>
38 #include "libusb20_desc.h"
39 #include "libusb20_int.h"
43 * The two following macros were taken from the original LibUSB v0.1
44 * for sake of compatibility:
46 #define LIST_ADD(begin, ent) \
50 ent->next->prev = ent; \
58 #define LIST_DEL(begin, ent) \
61 ent->prev->next = ent->next; \
66 ent->next->prev = ent->prev; \
72 struct usb_bus *usb_busses = NULL;
74 static struct usb_bus usb_global_bus = {
75 .dirname = {"/dev/usb"},
80 static struct libusb20_backend *usb_backend = NULL;
82 struct usb_parse_state {
85 struct libusb20_endpoint *currep;
86 struct libusb20_interface *currifc;
87 struct libusb20_config *currcfg;
88 struct libusb20_me_struct *currextra;
92 struct usb_config_descriptor *currcfg;
93 struct usb_interface_descriptor *currifc;
94 struct usb_endpoint_descriptor *currep;
95 struct usb_interface *currifcw;
102 static struct libusb20_transfer *
103 usb_get_transfer_by_ep_no(usb_dev_handle * dev, uint8_t ep_no)
105 struct libusb20_device *pdev = (void *)dev;
106 struct libusb20_transfer *xfer;
112 x = (ep_no & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 2;
114 if (ep_no & LIBUSB20_ENDPOINT_DIR_MASK) {
115 /* this is an IN endpoint */
118 speed = libusb20_dev_get_speed(pdev);
120 /* select a sensible buffer size */
121 if (speed == LIBUSB20_SPEED_LOW) {
123 } else if (speed == LIBUSB20_SPEED_FULL) {
125 } else if (speed == LIBUSB20_SPEED_SUPER) {
131 xfer = libusb20_tr_get_pointer(pdev, x);
136 err = libusb20_tr_open(xfer, bufsize, 1, ep_no);
137 if (err == LIBUSB20_ERROR_BUSY) {
148 usb_open(struct usb_device *dev)
152 err = libusb20_dev_open(dev->dev, 16 * 2);
153 if (err == LIBUSB20_ERROR_BUSY) {
155 * Workaround buggy USB applications which open the USB
156 * device multiple times:
164 * Dequeue USB device from backend queue so that it does not get
165 * freed when the backend is re-scanned:
167 libusb20_be_dequeue_device(usb_backend, dev->dev);
173 usb_close(usb_dev_handle * udev)
175 struct usb_device *dev;
178 err = libusb20_dev_close((void *)udev);
183 if (usb_backend != NULL) {
185 * Enqueue USB device to backend queue so that it gets freed
186 * when the backend is re-scanned:
188 libusb20_be_enqueue_device(usb_backend, (void *)udev);
191 * The backend is gone. Free device data so that we
192 * don't start leaking memory!
194 dev = usb_device(udev);
195 libusb20_dev_free((void *)udev);
196 LIST_DEL(usb_global_bus.devices, dev);
203 usb_get_string(usb_dev_handle * dev, int strindex,
204 int langid, char *buf, size_t buflen)
214 err = libusb20_dev_req_string_sync((void *)dev,
215 strindex, langid, buf, buflen);
224 usb_get_string_simple(usb_dev_handle * dev, int strindex,
225 char *buf, size_t buflen)
235 err = libusb20_dev_req_string_simple_sync((void *)dev,
236 strindex, buf, buflen);
241 return (strlen(buf));
245 usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type,
246 uint8_t ep_index, void *buf, int size)
248 memset(buf, 0, size);
256 return (usb_control_msg(udev, ep | USB_ENDPOINT_IN,
257 USB_REQ_GET_DESCRIPTOR, (type << 8) + ep_index, 0,
262 usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t desc_index,
265 memset(buf, 0, size);
273 return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
274 (type << 8) + desc_index, 0, buf, size, 1000));
278 usb_parse_descriptor(uint8_t *source, char *description, void *dest)
280 uint8_t *sp = source;
286 for (cp = description; *cp; cp++) {
288 case 'b': /* 8-bit byte */
292 * 16-bit word, convert from little endian to CPU
295 w = (sp[1] << 8) | sp[0];
297 /* Align to word boundary */
298 dp += ((dp - (uint8_t *)0) & 1);
299 *((uint16_t *)dp) = w;
303 * 32-bit dword, convert from little endian to CPU
306 d = (sp[3] << 24) | (sp[2] << 16) |
307 (sp[1] << 8) | sp[0];
309 /* Align to word boundary */
310 dp += ((dp - (uint8_t *)0) & 1);
311 /* Align to double word boundary */
312 dp += ((dp - (uint8_t *)0) & 2);
313 *((uint32_t *)dp) = d;
318 return (sp - source);
322 usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen)
327 ptr = ps->a.currextra->ptr;
328 len = ps->a.currextra->len;
330 if (ps->preparse == 0) {
331 memcpy(ps->b.currextra, ptr, len);
332 *pptr = ps->b.currextra;
335 ps->b.currextra += len;
340 usb_parse_endpoint(struct usb_parse_state *ps)
342 struct usb_endpoint_descriptor *bep;
343 struct libusb20_endpoint *aep;
346 bep = ps->b.currep++;
348 if (ps->preparse == 0) {
349 /* copy descriptor fields */
350 bep->bLength = aep->desc.bLength;
351 bep->bDescriptorType = aep->desc.bDescriptorType;
352 bep->bEndpointAddress = aep->desc.bEndpointAddress;
353 bep->bmAttributes = aep->desc.bmAttributes;
354 bep->wMaxPacketSize = aep->desc.wMaxPacketSize;
355 bep->bInterval = aep->desc.bInterval;
356 bep->bRefresh = aep->desc.bRefresh;
357 bep->bSynchAddress = aep->desc.bSynchAddress;
359 ps->a.currextra = &aep->extra;
360 usb_parse_extra(ps, &bep->extra, &bep->extralen);
365 usb_parse_iface_sub(struct usb_parse_state *ps)
367 struct libusb20_interface *aifc;
368 struct usb_interface_descriptor *bifc;
371 aifc = ps->a.currifc;
372 bifc = ps->b.currifc++;
374 if (ps->preparse == 0) {
375 /* copy descriptor fields */
376 bifc->bLength = aifc->desc.bLength;
377 bifc->bDescriptorType = aifc->desc.bDescriptorType;
378 bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber;
379 bifc->bAlternateSetting = aifc->desc.bAlternateSetting;
380 bifc->bNumEndpoints = aifc->num_endpoints;
381 bifc->bInterfaceClass = aifc->desc.bInterfaceClass;
382 bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass;
383 bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol;
384 bifc->iInterface = aifc->desc.iInterface;
385 bifc->endpoint = ps->b.currep;
387 for (x = 0; x != aifc->num_endpoints; x++) {
388 ps->a.currep = aifc->endpoints + x;
389 usb_parse_endpoint(ps);
392 ps->a.currextra = &aifc->extra;
393 usb_parse_extra(ps, &bifc->extra, &bifc->extralen);
398 usb_parse_iface(struct usb_parse_state *ps)
400 struct libusb20_interface *aifc;
401 struct usb_interface *bifc;
404 aifc = ps->a.currifc;
405 bifc = ps->b.currifcw++;
407 if (ps->preparse == 0) {
408 /* initialise interface wrapper */
409 bifc->altsetting = ps->b.currifc;
410 bifc->num_altsetting = aifc->num_altsetting + 1;
412 usb_parse_iface_sub(ps);
414 for (x = 0; x != aifc->num_altsetting; x++) {
415 ps->a.currifc = aifc->altsetting + x;
416 usb_parse_iface_sub(ps);
422 usb_parse_config(struct usb_parse_state *ps)
424 struct libusb20_config *acfg;
425 struct usb_config_descriptor *bcfg;
428 acfg = ps->a.currcfg;
429 bcfg = ps->b.currcfg;
431 if (ps->preparse == 0) {
432 /* initialise config wrapper */
433 bcfg->bLength = acfg->desc.bLength;
434 bcfg->bDescriptorType = acfg->desc.bDescriptorType;
435 bcfg->wTotalLength = acfg->desc.wTotalLength;
436 bcfg->bNumInterfaces = acfg->num_interface;
437 bcfg->bConfigurationValue = acfg->desc.bConfigurationValue;
438 bcfg->iConfiguration = acfg->desc.iConfiguration;
439 bcfg->bmAttributes = acfg->desc.bmAttributes;
440 bcfg->MaxPower = acfg->desc.bMaxPower;
441 bcfg->interface = ps->b.currifcw;
443 for (x = 0; x != acfg->num_interface; x++) {
444 ps->a.currifc = acfg->interface + x;
448 ps->a.currextra = &acfg->extra;
449 usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen);
454 usb_parse_configuration(struct usb_config_descriptor *config,
457 struct usb_parse_state ps;
464 if ((buffer == NULL) || (config == NULL)) {
467 memset(&ps, 0, sizeof(ps));
469 ps.a.currcfg = libusb20_parse_config_desc(buffer);
470 ps.b.currcfg = config;
471 if (ps.a.currcfg == NULL) {
472 /* could not parse config or out of memory */
475 /* do the pre-parse */
477 usb_parse_config(&ps);
479 a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0));
480 b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0));
481 c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0));
482 d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0));
484 /* allocate memory for our configuration */
485 ptr = malloc(a + b + c + d);
487 /* free config structure */
492 /* "currifcw" must be first, hence this pointer is freed */
493 ps.b.currifcw = (void *)(ptr);
494 ps.b.currifc = (void *)(ptr + a);
495 ps.b.currep = (void *)(ptr + a + b);
496 ps.b.currextra = (void *)(ptr + a + b + c);
498 /* generate a libusb v0.1 compatible structure */
500 usb_parse_config(&ps);
502 /* free config structure */
505 return (0); /* success */
509 usb_destroy_configuration(struct usb_device *dev)
513 if (dev->config == NULL) {
516 for (c = 0; c != dev->descriptor.bNumConfigurations; c++) {
517 struct usb_config_descriptor *cf = &dev->config[c];
519 if (cf->interface != NULL) {
521 cf->interface = NULL;
531 usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
533 struct usb_device *dev;
534 struct libusb20_device *pdev;
545 dev = usb_device(udev);
548 if (dev->descriptor.bNumConfigurations == 0) {
552 size = dev->descriptor.bNumConfigurations *
553 sizeof(struct usb_config_descriptor);
555 dev->config = malloc(size);
556 if (dev->config == NULL) {
560 memset(dev->config, 0, size);
562 for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {
564 error = (pdev->methods->get_config_desc_full) (
565 pdev, &ptr, &len, x);
568 usb_destroy_configuration(dev);
571 usb_parse_configuration(dev->config + x, ptr);
573 /* free config buffer */
580 usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size,
581 int timeout, int is_intr)
583 struct libusb20_transfer *xfer;
589 xfer = usb_get_transfer_by_ep_no(dev, ep);
593 if (libusb20_tr_pending(xfer)) {
594 /* there is already a transfer ongoing */
597 maxsize = libusb20_tr_get_max_total_length(xfer);
601 * We allow transferring zero bytes which is the same
602 * equivalent to a zero length USB packet.
607 if (temp > maxsize) {
608 /* find maximum possible length */
612 libusb20_tr_setup_intr(xfer, bytes, temp, timeout);
614 libusb20_tr_setup_bulk(xfer, bytes, temp, timeout);
616 libusb20_tr_start(xfer);
620 if (libusb20_dev_process((void *)dev) != 0) {
621 /* device detached */
624 if (libusb20_tr_pending(xfer) == 0) {
625 /* transfer complete */
628 /* wait for USB event from kernel */
629 libusb20_dev_wait_process((void *)dev, -1);
632 switch (libusb20_tr_get_status(xfer)) {
636 case LIBUSB20_TRANSFER_TIMED_OUT:
637 /* transfer timeout */
640 /* other transfer error */
643 actlen = libusb20_tr_get_actual_length(xfer);
648 if (actlen != temp) {
654 return (bytes - oldbytes);
658 usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes,
659 int size, int timeout)
661 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
662 bytes, size, timeout, 0));
666 usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes,
667 int size, int timeout)
669 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
670 bytes, size, timeout, 0));
674 usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes,
675 int size, int timeout)
677 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
678 bytes, size, timeout, 1));
682 usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes,
683 int size, int timeout)
685 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
686 bytes, size, timeout, 1));
690 usb_control_msg(usb_dev_handle * dev, int requesttype, int request,
691 int value, int wIndex, char *bytes, int size, int timeout)
693 struct LIBUSB20_CONTROL_SETUP_DECODED req;
697 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
699 req.bmRequestType = requesttype;
700 req.bRequest = request;
705 err = libusb20_dev_request_sync((void *)dev, &req, bytes,
706 &actlen, timeout, 0);
715 usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
717 struct usb_device *dev;
722 * Need to translate from "bConfigurationValue" to
723 * configuration index:
726 if (bConfigurationValue == 0) {
730 /* lookup configuration index */
731 dev = usb_device(udev);
733 /* check if the configuration array is not there */
734 if (dev->config == NULL) {
738 if (i == dev->descriptor.bNumConfigurations) {
739 /* "bConfigurationValue" not found */
742 if ((dev->config + i)->bConfigurationValue ==
743 bConfigurationValue) {
749 err = libusb20_dev_set_config_index((void *)udev, i);
758 usb_claim_interface(usb_dev_handle * dev, int interface)
760 struct libusb20_device *pdev = (void *)dev;
762 pdev->claimed_interface = interface;
768 usb_release_interface(usb_dev_handle * dev, int interface)
775 usb_set_altinterface(usb_dev_handle * dev, int alternate)
777 struct libusb20_device *pdev = (void *)dev;
781 iface = pdev->claimed_interface;
783 err = libusb20_dev_set_alt_index((void *)dev, iface, alternate);
792 usb_resetep(usb_dev_handle * dev, unsigned int ep)
794 /* emulate an endpoint reset through clear-STALL */
795 return (usb_clear_halt(dev, ep));
799 usb_clear_halt(usb_dev_handle * dev, unsigned int ep)
801 struct libusb20_transfer *xfer;
803 xfer = usb_get_transfer_by_ep_no(dev, ep);
807 libusb20_tr_clear_stall_sync(xfer);
813 usb_reset(usb_dev_handle * dev)
817 err = libusb20_dev_reset((void *)dev);
823 * Be compatible with LibUSB from sourceforge and close the
824 * handle after reset!
826 return (usb_close(dev));
830 usb_check_connected(usb_dev_handle * dev)
834 err = libusb20_dev_check_connected((void *)dev);
846 return ("Unknown error");
857 usb_set_debug(int level)
859 /* use kernel UGEN debugging if you need to see what is going on */
864 usb_find_busses(void)
866 usb_busses = &usb_global_bus;
871 usb_find_devices(void)
873 struct libusb20_device *pdev;
874 struct usb_device *udev;
875 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
879 /* cleanup after last device search */
880 /* close all opened devices, if any */
882 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
883 udev = pdev->privLuData;
884 libusb20_be_dequeue_device(usb_backend, pdev);
885 libusb20_dev_free(pdev);
887 LIST_DEL(usb_global_bus.devices, udev);
892 /* free old USB backend, if any */
894 libusb20_be_free(usb_backend);
896 /* do a new backend device search */
897 usb_backend = libusb20_be_alloc_default();
898 if (usb_backend == NULL) {
901 /* iterate all devices */
905 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
906 udev = malloc(sizeof(*udev));
910 memset(udev, 0, sizeof(*udev));
912 udev->bus = &usb_global_bus;
914 snprintf(udev->filename, sizeof(udev->filename),
916 libusb20_dev_get_bus_number(pdev),
917 libusb20_dev_get_address(pdev));
919 ddesc = libusb20_dev_get_device_desc(pdev);
921 udev->descriptor.bLength = sizeof(udev->descriptor);
922 udev->descriptor.bDescriptorType = ddesc->bDescriptorType;
923 udev->descriptor.bcdUSB = ddesc->bcdUSB;
924 udev->descriptor.bDeviceClass = ddesc->bDeviceClass;
925 udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass;
926 udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol;
927 udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0;
928 udev->descriptor.idVendor = ddesc->idVendor;
929 udev->descriptor.idProduct = ddesc->idProduct;
930 udev->descriptor.bcdDevice = ddesc->bcdDevice;
931 udev->descriptor.iManufacturer = ddesc->iManufacturer;
932 udev->descriptor.iProduct = ddesc->iProduct;
933 udev->descriptor.iSerialNumber = ddesc->iSerialNumber;
934 udev->descriptor.bNumConfigurations =
935 ddesc->bNumConfigurations;
936 if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
937 /* truncate number of configurations */
938 udev->descriptor.bNumConfigurations = USB_MAXCONFIG;
940 udev->devnum = devnum++;
941 /* link together the two structures */
943 pdev->privLuData = udev;
945 err = libusb20_dev_open(pdev, 0);
947 /* XXX get all config descriptors by default */
948 usb_fetch_and_parse_descriptors((void *)pdev);
949 libusb20_dev_close(pdev);
951 LIST_ADD(usb_global_bus.devices, udev);
954 return (devnum - 1); /* success */
958 usb_device(usb_dev_handle * dev)
960 struct libusb20_device *pdev;
964 return (pdev->privLuData);
974 usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen)
976 struct libusb20_device *pdev;
989 err = libusb20_dev_get_iface_desc(pdev, interface, name, namelen);
993 /* we only want the driver name */
994 ptr = strstr(name, ":");
1002 usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface)
1004 struct libusb20_device *pdev;
1012 err = libusb20_dev_detach_kernel_driver(pdev, interface);