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) {
129 xfer = libusb20_tr_get_pointer(pdev, x);
134 err = libusb20_tr_open(xfer, bufsize, 1, ep_no);
135 if (err == LIBUSB20_ERROR_BUSY) {
146 usb_open(struct usb_device *dev)
150 err = libusb20_dev_open(dev->dev, 16 * 2);
151 if (err == LIBUSB20_ERROR_BUSY) {
153 * Workaround buggy USB applications which open the USB
154 * device multiple times:
162 * Dequeue USB device from backend queue so that it does not get
163 * freed when the backend is re-scanned:
165 libusb20_be_dequeue_device(usb_backend, dev->dev);
171 usb_close(usb_dev_handle * udev)
173 struct usb_device *dev;
176 err = libusb20_dev_close((void *)udev);
181 if (usb_backend != NULL) {
183 * Enqueue USB device to backend queue so that it gets freed
184 * when the backend is re-scanned:
186 libusb20_be_enqueue_device(usb_backend, (void *)udev);
189 * The backend is gone. Free device data so that we
190 * don't start leaking memory!
192 dev = usb_device(udev);
193 libusb20_dev_free((void *)udev);
194 LIST_DEL(usb_global_bus.devices, dev);
201 usb_get_string(usb_dev_handle * dev, int strindex,
202 int langid, char *buf, size_t buflen)
212 err = libusb20_dev_req_string_sync((void *)dev,
213 strindex, langid, buf, buflen);
222 usb_get_string_simple(usb_dev_handle * dev, int strindex,
223 char *buf, size_t buflen)
233 err = libusb20_dev_req_string_simple_sync((void *)dev,
234 strindex, buf, buflen);
239 return (strlen(buf));
243 usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type,
244 uint8_t ep_index, void *buf, int size)
246 memset(buf, 0, size);
254 return (usb_control_msg(udev, ep | USB_ENDPOINT_IN,
255 USB_REQ_GET_DESCRIPTOR, (type << 8) + ep_index, 0,
260 usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t desc_index,
263 memset(buf, 0, size);
271 return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
272 (type << 8) + desc_index, 0, buf, size, 1000));
276 usb_parse_descriptor(uint8_t *source, char *description, void *dest)
278 uint8_t *sp = source;
284 for (cp = description; *cp; cp++) {
286 case 'b': /* 8-bit byte */
290 * 16-bit word, convert from little endian to CPU
293 w = (sp[1] << 8) | sp[0];
295 /* Align to word boundary */
296 dp += ((dp - (uint8_t *)0) & 1);
297 *((uint16_t *)dp) = w;
301 * 32-bit dword, convert from little endian to CPU
304 d = (sp[3] << 24) | (sp[2] << 16) |
305 (sp[1] << 8) | sp[0];
307 /* Align to word boundary */
308 dp += ((dp - (uint8_t *)0) & 1);
309 /* Align to double word boundary */
310 dp += ((dp - (uint8_t *)0) & 2);
311 *((uint32_t *)dp) = d;
316 return (sp - source);
320 usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen)
325 ptr = ps->a.currextra->ptr;
326 len = ps->a.currextra->len;
328 if (ps->preparse == 0) {
329 memcpy(ps->b.currextra, ptr, len);
330 *pptr = ps->b.currextra;
333 ps->b.currextra += len;
338 usb_parse_endpoint(struct usb_parse_state *ps)
340 struct usb_endpoint_descriptor *bep;
341 struct libusb20_endpoint *aep;
344 bep = ps->b.currep++;
346 if (ps->preparse == 0) {
347 /* copy descriptor fields */
348 bep->bLength = aep->desc.bLength;
349 bep->bDescriptorType = aep->desc.bDescriptorType;
350 bep->bEndpointAddress = aep->desc.bEndpointAddress;
351 bep->bmAttributes = aep->desc.bmAttributes;
352 bep->wMaxPacketSize = aep->desc.wMaxPacketSize;
353 bep->bInterval = aep->desc.bInterval;
354 bep->bRefresh = aep->desc.bRefresh;
355 bep->bSynchAddress = aep->desc.bSynchAddress;
357 ps->a.currextra = &aep->extra;
358 usb_parse_extra(ps, &bep->extra, &bep->extralen);
363 usb_parse_iface_sub(struct usb_parse_state *ps)
365 struct libusb20_interface *aifc;
366 struct usb_interface_descriptor *bifc;
369 aifc = ps->a.currifc;
370 bifc = ps->b.currifc++;
372 if (ps->preparse == 0) {
373 /* copy descriptor fields */
374 bifc->bLength = aifc->desc.bLength;
375 bifc->bDescriptorType = aifc->desc.bDescriptorType;
376 bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber;
377 bifc->bAlternateSetting = aifc->desc.bAlternateSetting;
378 bifc->bNumEndpoints = aifc->num_endpoints;
379 bifc->bInterfaceClass = aifc->desc.bInterfaceClass;
380 bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass;
381 bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol;
382 bifc->iInterface = aifc->desc.iInterface;
383 bifc->endpoint = ps->b.currep;
385 for (x = 0; x != aifc->num_endpoints; x++) {
386 ps->a.currep = aifc->endpoints + x;
387 usb_parse_endpoint(ps);
390 ps->a.currextra = &aifc->extra;
391 usb_parse_extra(ps, &bifc->extra, &bifc->extralen);
396 usb_parse_iface(struct usb_parse_state *ps)
398 struct libusb20_interface *aifc;
399 struct usb_interface *bifc;
402 aifc = ps->a.currifc;
403 bifc = ps->b.currifcw++;
405 if (ps->preparse == 0) {
406 /* initialise interface wrapper */
407 bifc->altsetting = ps->b.currifc;
408 bifc->num_altsetting = aifc->num_altsetting + 1;
410 usb_parse_iface_sub(ps);
412 for (x = 0; x != aifc->num_altsetting; x++) {
413 ps->a.currifc = aifc->altsetting + x;
414 usb_parse_iface_sub(ps);
420 usb_parse_config(struct usb_parse_state *ps)
422 struct libusb20_config *acfg;
423 struct usb_config_descriptor *bcfg;
426 acfg = ps->a.currcfg;
427 bcfg = ps->b.currcfg;
429 if (ps->preparse == 0) {
430 /* initialise config wrapper */
431 bcfg->bLength = acfg->desc.bLength;
432 bcfg->bDescriptorType = acfg->desc.bDescriptorType;
433 bcfg->wTotalLength = acfg->desc.wTotalLength;
434 bcfg->bNumInterfaces = acfg->num_interface;
435 bcfg->bConfigurationValue = acfg->desc.bConfigurationValue;
436 bcfg->iConfiguration = acfg->desc.iConfiguration;
437 bcfg->bmAttributes = acfg->desc.bmAttributes;
438 bcfg->MaxPower = acfg->desc.bMaxPower;
439 bcfg->interface = ps->b.currifcw;
441 for (x = 0; x != acfg->num_interface; x++) {
442 ps->a.currifc = acfg->interface + x;
446 ps->a.currextra = &acfg->extra;
447 usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen);
452 usb_parse_configuration(struct usb_config_descriptor *config,
455 struct usb_parse_state ps;
462 if ((buffer == NULL) || (config == NULL)) {
465 memset(&ps, 0, sizeof(ps));
467 ps.a.currcfg = libusb20_parse_config_desc(buffer);
468 ps.b.currcfg = config;
469 if (ps.a.currcfg == NULL) {
470 /* could not parse config or out of memory */
473 /* do the pre-parse */
475 usb_parse_config(&ps);
477 a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0));
478 b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0));
479 c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0));
480 d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0));
482 /* allocate memory for our configuration */
483 ptr = malloc(a + b + c + d);
485 /* free config structure */
490 /* "currifcw" must be first, hence this pointer is freed */
491 ps.b.currifcw = (void *)(ptr);
492 ps.b.currifc = (void *)(ptr + a);
493 ps.b.currep = (void *)(ptr + a + b);
494 ps.b.currextra = (void *)(ptr + a + b + c);
496 /* generate a libusb v0.1 compatible structure */
498 usb_parse_config(&ps);
500 /* free config structure */
503 return (0); /* success */
507 usb_destroy_configuration(struct usb_device *dev)
511 if (dev->config == NULL) {
514 for (c = 0; c != dev->descriptor.bNumConfigurations; c++) {
515 struct usb_config_descriptor *cf = &dev->config[c];
517 if (cf->interface != NULL) {
519 cf->interface = NULL;
529 usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
531 struct usb_device *dev;
532 struct libusb20_device *pdev;
543 dev = usb_device(udev);
546 if (dev->descriptor.bNumConfigurations == 0) {
550 size = dev->descriptor.bNumConfigurations *
551 sizeof(struct usb_config_descriptor);
553 dev->config = malloc(size);
554 if (dev->config == NULL) {
558 memset(dev->config, 0, size);
560 for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {
562 error = (pdev->methods->get_config_desc_full) (
563 pdev, &ptr, &len, x);
566 usb_destroy_configuration(dev);
569 usb_parse_configuration(dev->config + x, ptr);
571 /* free config buffer */
578 usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size,
579 int timeout, int is_intr)
581 struct libusb20_transfer *xfer;
587 xfer = usb_get_transfer_by_ep_no(dev, ep);
591 if (libusb20_tr_pending(xfer)) {
592 /* there is already a transfer ongoing */
595 maxsize = libusb20_tr_get_max_total_length(xfer);
599 * We allow transferring zero bytes which is the same
600 * equivalent to a zero length USB packet.
605 if (temp > maxsize) {
606 /* find maximum possible length */
610 libusb20_tr_setup_intr(xfer, bytes, temp, timeout);
612 libusb20_tr_setup_bulk(xfer, bytes, temp, timeout);
614 libusb20_tr_start(xfer);
618 if (libusb20_dev_process((void *)dev) != 0) {
619 /* device detached */
622 if (libusb20_tr_pending(xfer) == 0) {
623 /* transfer complete */
626 /* wait for USB event from kernel */
627 libusb20_dev_wait_process((void *)dev, -1);
630 switch (libusb20_tr_get_status(xfer)) {
634 case LIBUSB20_TRANSFER_TIMED_OUT:
635 /* transfer timeout */
638 /* other transfer error */
641 actlen = libusb20_tr_get_actual_length(xfer);
646 if (actlen != temp) {
652 return (bytes - oldbytes);
656 usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes,
657 int size, int timeout)
659 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
660 bytes, size, timeout, 0));
664 usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes,
665 int size, int timeout)
667 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
668 bytes, size, timeout, 0));
672 usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes,
673 int size, int timeout)
675 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
676 bytes, size, timeout, 1));
680 usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes,
681 int size, int timeout)
683 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
684 bytes, size, timeout, 1));
688 usb_control_msg(usb_dev_handle * dev, int requesttype, int request,
689 int value, int wIndex, char *bytes, int size, int timeout)
691 struct LIBUSB20_CONTROL_SETUP_DECODED req;
695 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
697 req.bmRequestType = requesttype;
698 req.bRequest = request;
703 err = libusb20_dev_request_sync((void *)dev, &req, bytes,
704 &actlen, timeout, 0);
713 usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
715 struct usb_device *dev;
720 * Need to translate from "bConfigurationValue" to
721 * configuration index:
724 if (bConfigurationValue == 0) {
728 /* lookup configuration index */
729 dev = usb_device(udev);
731 /* check if the configuration array is not there */
732 if (dev->config == NULL) {
736 if (i == dev->descriptor.bNumConfigurations) {
737 /* "bConfigurationValue" not found */
740 if ((dev->config + i)->bConfigurationValue ==
741 bConfigurationValue) {
747 err = libusb20_dev_set_config_index((void *)udev, i);
756 usb_claim_interface(usb_dev_handle * dev, int interface)
758 struct libusb20_device *pdev = (void *)dev;
760 pdev->claimed_interface = interface;
766 usb_release_interface(usb_dev_handle * dev, int interface)
773 usb_set_altinterface(usb_dev_handle * dev, int alternate)
775 struct libusb20_device *pdev = (void *)dev;
779 iface = pdev->claimed_interface;
781 err = libusb20_dev_set_alt_index((void *)dev, iface, alternate);
790 usb_resetep(usb_dev_handle * dev, unsigned int ep)
792 /* emulate an endpoint reset through clear-STALL */
793 return (usb_clear_halt(dev, ep));
797 usb_clear_halt(usb_dev_handle * dev, unsigned int ep)
799 struct libusb20_transfer *xfer;
801 xfer = usb_get_transfer_by_ep_no(dev, ep);
805 libusb20_tr_clear_stall_sync(xfer);
811 usb_reset(usb_dev_handle * dev)
815 err = libusb20_dev_reset((void *)dev);
821 * Be compatible with LibUSB from sourceforge and close the
822 * handle after reset!
824 return (usb_close(dev));
828 usb_check_connected(usb_dev_handle * dev)
832 err = libusb20_dev_check_connected((void *)dev);
844 return ("Unknown error");
855 usb_set_debug(int level)
857 /* use kernel UGEN debugging if you need to see what is going on */
862 usb_find_busses(void)
864 usb_busses = &usb_global_bus;
869 usb_find_devices(void)
871 struct libusb20_device *pdev;
872 struct usb_device *udev;
873 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
877 /* cleanup after last device search */
878 /* close all opened devices, if any */
880 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
881 udev = pdev->privLuData;
882 libusb20_be_dequeue_device(usb_backend, pdev);
883 libusb20_dev_free(pdev);
885 LIST_DEL(usb_global_bus.devices, udev);
890 /* free old USB backend, if any */
892 libusb20_be_free(usb_backend);
894 /* do a new backend device search */
895 usb_backend = libusb20_be_alloc_default();
896 if (usb_backend == NULL) {
899 /* iterate all devices */
903 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
904 udev = malloc(sizeof(*udev));
908 memset(udev, 0, sizeof(*udev));
910 udev->bus = &usb_global_bus;
912 snprintf(udev->filename, sizeof(udev->filename),
914 libusb20_dev_get_bus_number(pdev),
915 libusb20_dev_get_address(pdev));
917 ddesc = libusb20_dev_get_device_desc(pdev);
919 udev->descriptor.bLength = sizeof(udev->descriptor);
920 udev->descriptor.bDescriptorType = ddesc->bDescriptorType;
921 udev->descriptor.bcdUSB = ddesc->bcdUSB;
922 udev->descriptor.bDeviceClass = ddesc->bDeviceClass;
923 udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass;
924 udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol;
925 udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0;
926 udev->descriptor.idVendor = ddesc->idVendor;
927 udev->descriptor.idProduct = ddesc->idProduct;
928 udev->descriptor.bcdDevice = ddesc->bcdDevice;
929 udev->descriptor.iManufacturer = ddesc->iManufacturer;
930 udev->descriptor.iProduct = ddesc->iProduct;
931 udev->descriptor.iSerialNumber = ddesc->iSerialNumber;
932 udev->descriptor.bNumConfigurations =
933 ddesc->bNumConfigurations;
934 if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
935 /* truncate number of configurations */
936 udev->descriptor.bNumConfigurations = USB_MAXCONFIG;
938 udev->devnum = devnum++;
939 /* link together the two structures */
941 pdev->privLuData = udev;
943 err = libusb20_dev_open(pdev, 0);
945 /* XXX get all config descriptors by default */
946 usb_fetch_and_parse_descriptors((void *)pdev);
947 libusb20_dev_close(pdev);
949 LIST_ADD(usb_global_bus.devices, udev);
952 return (devnum - 1); /* success */
956 usb_device(usb_dev_handle * dev)
958 struct libusb20_device *pdev;
962 return (pdev->privLuData);
972 usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen)
974 struct libusb20_device *pdev;
987 err = libusb20_dev_get_iface_desc(pdev, interface, name, namelen);
991 /* we only want the driver name */
992 ptr = strstr(name, ":");
1000 usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface)
1002 struct libusb20_device *pdev;
1010 err = libusb20_dev_detach_kernel_driver(pdev, interface);