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)
206 err = libusb20_dev_req_string_sync((void *)dev,
207 strindex, langid, buf, buflen);
216 usb_get_string_simple(usb_dev_handle * dev, int strindex,
217 char *buf, size_t buflen)
221 err = libusb20_dev_req_string_simple_sync((void *)dev,
222 strindex, buf, buflen);
227 return (strlen(buf));
231 usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type,
232 uint8_t ep_index, void *buf, int size)
234 memset(buf, 0, size);
236 return (usb_control_msg(udev, ep | USB_ENDPOINT_IN,
237 USB_REQ_GET_DESCRIPTOR, (type << 8) + ep_index, 0,
242 usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t desc_index,
245 memset(buf, 0, size);
247 return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
248 (type << 8) + desc_index, 0, buf, size, 1000));
252 usb_parse_descriptor(uint8_t *source, char *description, void *dest)
254 uint8_t *sp = source;
260 for (cp = description; *cp; cp++) {
262 case 'b': /* 8-bit byte */
266 * 16-bit word, convert from little endian to CPU
269 w = (sp[1] << 8) | sp[0];
271 /* Align to word boundary */
272 dp += ((dp - (uint8_t *)0) & 1);
273 *((uint16_t *)dp) = w;
277 * 32-bit dword, convert from little endian to CPU
280 d = (sp[3] << 24) | (sp[2] << 16) |
281 (sp[1] << 8) | sp[0];
283 /* Align to word boundary */
284 dp += ((dp - (uint8_t *)0) & 1);
285 /* Align to double word boundary */
286 dp += ((dp - (uint8_t *)0) & 2);
287 *((uint32_t *)dp) = d;
292 return (sp - source);
296 usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen)
301 ptr = ps->a.currextra->ptr;
302 len = ps->a.currextra->len;
304 if (ps->preparse == 0) {
305 memcpy(ps->b.currextra, ptr, len);
306 *pptr = ps->b.currextra;
309 ps->b.currextra += len;
314 usb_parse_endpoint(struct usb_parse_state *ps)
316 struct usb_endpoint_descriptor *bep;
317 struct libusb20_endpoint *aep;
320 bep = ps->b.currep++;
322 if (ps->preparse == 0) {
323 /* copy descriptor fields */
324 bep->bLength = aep->desc.bLength;
325 bep->bDescriptorType = aep->desc.bDescriptorType;
326 bep->bEndpointAddress = aep->desc.bEndpointAddress;
327 bep->bmAttributes = aep->desc.bmAttributes;
328 bep->wMaxPacketSize = aep->desc.wMaxPacketSize;
329 bep->bInterval = aep->desc.bInterval;
330 bep->bRefresh = aep->desc.bRefresh;
331 bep->bSynchAddress = aep->desc.bSynchAddress;
333 ps->a.currextra = &aep->extra;
334 usb_parse_extra(ps, &bep->extra, &bep->extralen);
339 usb_parse_iface_sub(struct usb_parse_state *ps)
341 struct libusb20_interface *aifc;
342 struct usb_interface_descriptor *bifc;
345 aifc = ps->a.currifc;
346 bifc = ps->b.currifc++;
348 if (ps->preparse == 0) {
349 /* copy descriptor fields */
350 bifc->bLength = aifc->desc.bLength;
351 bifc->bDescriptorType = aifc->desc.bDescriptorType;
352 bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber;
353 bifc->bAlternateSetting = aifc->desc.bAlternateSetting;
354 bifc->bNumEndpoints = aifc->num_endpoints;
355 bifc->bInterfaceClass = aifc->desc.bInterfaceClass;
356 bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass;
357 bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol;
358 bifc->iInterface = aifc->desc.iInterface;
359 bifc->endpoint = ps->b.currep;
361 for (x = 0; x != aifc->num_endpoints; x++) {
362 ps->a.currep = aifc->endpoints + x;
363 usb_parse_endpoint(ps);
366 ps->a.currextra = &aifc->extra;
367 usb_parse_extra(ps, &bifc->extra, &bifc->extralen);
372 usb_parse_iface(struct usb_parse_state *ps)
374 struct libusb20_interface *aifc;
375 struct usb_interface *bifc;
378 aifc = ps->a.currifc;
379 bifc = ps->b.currifcw++;
381 if (ps->preparse == 0) {
382 /* initialise interface wrapper */
383 bifc->altsetting = ps->b.currifc;
384 bifc->num_altsetting = aifc->num_altsetting + 1;
386 usb_parse_iface_sub(ps);
388 for (x = 0; x != aifc->num_altsetting; x++) {
389 ps->a.currifc = aifc->altsetting + x;
390 usb_parse_iface_sub(ps);
396 usb_parse_config(struct usb_parse_state *ps)
398 struct libusb20_config *acfg;
399 struct usb_config_descriptor *bcfg;
402 acfg = ps->a.currcfg;
403 bcfg = ps->b.currcfg;
405 if (ps->preparse == 0) {
406 /* initialise config wrapper */
407 bcfg->bLength = acfg->desc.bLength;
408 bcfg->bDescriptorType = acfg->desc.bDescriptorType;
409 bcfg->wTotalLength = acfg->desc.wTotalLength;
410 bcfg->bNumInterfaces = acfg->num_interface;
411 bcfg->bConfigurationValue = acfg->desc.bConfigurationValue;
412 bcfg->iConfiguration = acfg->desc.iConfiguration;
413 bcfg->bmAttributes = acfg->desc.bmAttributes;
414 bcfg->MaxPower = acfg->desc.bMaxPower;
415 bcfg->interface = ps->b.currifcw;
417 for (x = 0; x != acfg->num_interface; x++) {
418 ps->a.currifc = acfg->interface + x;
422 ps->a.currextra = &acfg->extra;
423 usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen);
428 usb_parse_configuration(struct usb_config_descriptor *config,
431 struct usb_parse_state ps;
438 if ((buffer == NULL) || (config == NULL)) {
441 memset(&ps, 0, sizeof(ps));
443 ps.a.currcfg = libusb20_parse_config_desc(buffer);
444 ps.b.currcfg = config;
445 if (ps.a.currcfg == NULL) {
446 /* could not parse config or out of memory */
449 /* do the pre-parse */
451 usb_parse_config(&ps);
453 a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0));
454 b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0));
455 c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0));
456 d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0));
458 /* allocate memory for our configuration */
459 ptr = malloc(a + b + c + d);
461 /* "currifcw" must be first, hence this pointer is freed */
462 ps.b.currifcw = (void *)(ptr);
463 ps.b.currifc = (void *)(ptr + a);
464 ps.b.currep = (void *)(ptr + a + b);
465 ps.b.currextra = (void *)(ptr + a + b + c);
467 /* generate a libusb v0.1 compatible structure */
469 usb_parse_config(&ps);
471 /* free config structure */
474 return (0); /* success */
478 usb_destroy_configuration(struct usb_device *dev)
482 if (dev->config == NULL) {
485 for (c = 0; c != dev->descriptor.bNumConfigurations; c++) {
486 struct usb_config_descriptor *cf = &dev->config[c];
488 if (cf->interface != NULL) {
490 cf->interface = NULL;
500 usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
502 struct usb_device *dev;
503 struct libusb20_device *pdev;
514 dev = usb_device(udev);
517 if (dev->descriptor.bNumConfigurations == 0) {
521 size = dev->descriptor.bNumConfigurations *
522 sizeof(struct usb_config_descriptor);
524 dev->config = malloc(size);
525 if (dev->config == NULL) {
529 memset(dev->config, 0, size);
531 for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {
533 error = (pdev->methods->get_config_desc_full) (
534 pdev, &ptr, &len, x);
537 usb_destroy_configuration(dev);
540 usb_parse_configuration(dev->config + x, ptr);
542 /* free config buffer */
549 usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size,
550 int timeout, int is_intr)
552 struct libusb20_transfer *xfer;
558 xfer = usb_get_transfer_by_ep_no(dev, ep);
562 if (libusb20_tr_pending(xfer)) {
563 /* there is already a transfer ongoing */
566 maxsize = libusb20_tr_get_max_total_length(xfer);
570 * We allow transferring zero bytes which is the same
571 * equivalent to a zero length USB packet.
576 if (temp > maxsize) {
577 /* find maximum possible length */
581 libusb20_tr_setup_intr(xfer, bytes, temp, timeout);
583 libusb20_tr_setup_bulk(xfer, bytes, temp, timeout);
585 libusb20_tr_start(xfer);
589 if (libusb20_dev_process((void *)dev) != 0) {
590 /* device detached */
593 if (libusb20_tr_pending(xfer) == 0) {
594 /* transfer complete */
597 /* wait for USB event from kernel */
598 libusb20_dev_wait_process((void *)dev, -1);
601 switch (libusb20_tr_get_status(xfer)) {
605 case LIBUSB20_TRANSFER_TIMED_OUT:
606 /* transfer timeout */
609 /* other transfer error */
612 actlen = libusb20_tr_get_actual_length(xfer);
617 if (actlen != temp) {
623 return (bytes - oldbytes);
627 usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes,
628 int size, int timeout)
630 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
631 bytes, size, timeout, 0));
635 usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes,
636 int size, int timeout)
638 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
639 bytes, size, timeout, 0));
643 usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes,
644 int size, int timeout)
646 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
647 bytes, size, timeout, 1));
651 usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes,
652 int size, int timeout)
654 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
655 bytes, size, timeout, 1));
659 usb_control_msg(usb_dev_handle * dev, int requesttype, int request,
660 int value, int wIndex, char *bytes, int size, int timeout)
662 struct LIBUSB20_CONTROL_SETUP_DECODED req;
666 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
668 req.bmRequestType = requesttype;
669 req.bRequest = request;
674 err = libusb20_dev_request_sync((void *)dev, &req, bytes,
675 &actlen, timeout, 0);
684 usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
686 struct usb_device *dev;
691 * Need to translate from "bConfigurationValue" to
692 * configuration index:
695 if (bConfigurationValue == 0) {
699 /* lookup configuration index */
700 dev = usb_device(udev);
702 /* check if the configuration array is not there */
703 if (dev->config == NULL) {
707 if (i == dev->descriptor.bNumConfigurations) {
708 /* "bConfigurationValue" not found */
711 if ((dev->config + i)->bConfigurationValue ==
712 bConfigurationValue) {
718 err = libusb20_dev_set_config_index((void *)udev, i);
727 usb_claim_interface(usb_dev_handle * dev, int interface)
729 struct libusb20_device *pdev = (void *)dev;
731 pdev->claimed_interface = interface;
737 usb_release_interface(usb_dev_handle * dev, int interface)
744 usb_set_altinterface(usb_dev_handle * dev, int alternate)
746 struct libusb20_device *pdev = (void *)dev;
750 iface = pdev->claimed_interface;
752 err = libusb20_dev_set_alt_index((void *)dev, iface, alternate);
761 usb_resetep(usb_dev_handle * dev, unsigned int ep)
763 /* emulate an endpoint reset through clear-STALL */
764 return (usb_clear_halt(dev, ep));
768 usb_clear_halt(usb_dev_handle * dev, unsigned int ep)
770 struct libusb20_transfer *xfer;
772 xfer = usb_get_transfer_by_ep_no(dev, ep);
776 libusb20_tr_clear_stall_sync(xfer);
782 usb_reset(usb_dev_handle * dev)
786 err = libusb20_dev_reset((void *)dev);
792 * Be compatible with LibUSB from sourceforge and close the
793 * handle after reset!
795 return (usb_close(dev));
802 return ("Unknown error");
813 usb_set_debug(int level)
815 /* use kernel UGEN debugging if you need to see what is going on */
820 usb_find_busses(void)
822 usb_busses = &usb_global_bus;
827 usb_find_devices(void)
829 struct libusb20_device *pdev;
830 struct usb_device *udev;
831 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
835 /* cleanup after last device search */
836 /* close all opened devices, if any */
838 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
839 udev = pdev->privLuData;
840 libusb20_be_dequeue_device(usb_backend, pdev);
841 libusb20_dev_free(pdev);
843 LIST_DEL(usb_global_bus.devices, udev);
848 /* free old USB backend, if any */
850 libusb20_be_free(usb_backend);
852 /* do a new backend device search */
853 usb_backend = libusb20_be_alloc_default();
854 if (usb_backend == NULL) {
857 /* iterate all devices */
861 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
862 udev = malloc(sizeof(*udev));
866 memset(udev, 0, sizeof(*udev));
868 udev->bus = &usb_global_bus;
870 snprintf(udev->filename, sizeof(udev->filename),
872 libusb20_dev_get_bus_number(pdev),
873 libusb20_dev_get_address(pdev));
875 ddesc = libusb20_dev_get_device_desc(pdev);
877 udev->descriptor.bLength = sizeof(udev->descriptor);
878 udev->descriptor.bDescriptorType = ddesc->bDescriptorType;
879 udev->descriptor.bcdUSB = ddesc->bcdUSB;
880 udev->descriptor.bDeviceClass = ddesc->bDeviceClass;
881 udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass;
882 udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol;
883 udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0;
884 udev->descriptor.idVendor = ddesc->idVendor;
885 udev->descriptor.idProduct = ddesc->idProduct;
886 udev->descriptor.bcdDevice = ddesc->bcdDevice;
887 udev->descriptor.iManufacturer = ddesc->iManufacturer;
888 udev->descriptor.iProduct = ddesc->iProduct;
889 udev->descriptor.iSerialNumber = ddesc->iSerialNumber;
890 udev->descriptor.bNumConfigurations =
891 ddesc->bNumConfigurations;
892 if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
893 /* truncate number of configurations */
894 udev->descriptor.bNumConfigurations = USB_MAXCONFIG;
896 udev->devnum = devnum++;
897 /* link together the two structures */
899 pdev->privLuData = udev;
901 err = libusb20_dev_open(pdev, 0);
903 /* XXX get all config descriptors by default */
904 usb_fetch_and_parse_descriptors((void *)pdev);
905 libusb20_dev_close(pdev);
907 LIST_ADD(usb_global_bus.devices, udev);
910 return (devnum - 1); /* success */
914 usb_device(usb_dev_handle * dev)
916 struct libusb20_device *pdev;
920 return (pdev->privLuData);