1 /* $FreeBSD: head/lib/libusb/libusb01.c 248236 2013-03-13 12:23:14Z hselasky $ */
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 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
32 #include LIBUSB_GLOBAL_INCLUDE_FILE
39 #include <sys/queue.h>
43 #include "libusb20_desc.h"
44 #include "libusb20_int.h"
48 * The two following macros were taken from the original LibUSB v0.1
49 * for sake of compatibility:
51 #define LIST_ADD(begin, ent) \
55 ent->next->prev = ent; \
63 #define LIST_DEL(begin, ent) \
66 ent->prev->next = ent->next; \
71 ent->next->prev = ent->prev; \
77 struct usb_bus *usb_busses = NULL;
79 static struct usb_bus usb_global_bus = {
80 .dirname = {"/dev/usb"},
85 static struct libusb20_backend *usb_backend = NULL;
87 struct usb_parse_state {
90 struct libusb20_endpoint *currep;
91 struct libusb20_interface *currifc;
92 struct libusb20_config *currcfg;
93 struct libusb20_me_struct *currextra;
97 struct usb_config_descriptor *currcfg;
98 struct usb_interface_descriptor *currifc;
99 struct usb_endpoint_descriptor *currep;
100 struct usb_interface *currifcw;
107 static struct libusb20_transfer *
108 usb_get_transfer_by_ep_no(usb_dev_handle * dev, uint8_t ep_no)
110 struct libusb20_device *pdev = (void *)dev;
111 struct libusb20_transfer *xfer;
117 x = (ep_no & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 2;
119 if (ep_no & LIBUSB20_ENDPOINT_DIR_MASK) {
120 /* this is an IN endpoint */
123 speed = libusb20_dev_get_speed(pdev);
125 /* select a sensible buffer size */
126 if (speed == LIBUSB20_SPEED_LOW) {
128 } else if (speed == LIBUSB20_SPEED_FULL) {
134 xfer = libusb20_tr_get_pointer(pdev, x);
139 err = libusb20_tr_open(xfer, bufsize, 1, ep_no);
140 if (err == LIBUSB20_ERROR_BUSY) {
151 usb_open(struct usb_device *dev)
155 err = libusb20_dev_open(dev->dev, 16 * 2);
156 if (err == LIBUSB20_ERROR_BUSY) {
158 * Workaround buggy USB applications which open the USB
159 * device multiple times:
167 * Dequeue USB device from backend queue so that it does not get
168 * freed when the backend is re-scanned:
170 libusb20_be_dequeue_device(usb_backend, dev->dev);
176 usb_close(usb_dev_handle * udev)
178 struct usb_device *dev;
181 err = libusb20_dev_close((void *)udev);
186 if (usb_backend != NULL) {
188 * Enqueue USB device to backend queue so that it gets freed
189 * when the backend is re-scanned:
191 libusb20_be_enqueue_device(usb_backend, (void *)udev);
194 * The backend is gone. Free device data so that we
195 * don't start leaking memory!
197 dev = usb_device(udev);
198 libusb20_dev_free((void *)udev);
199 LIST_DEL(usb_global_bus.devices, dev);
206 usb_get_string(usb_dev_handle * dev, int strindex,
207 int langid, char *buf, size_t buflen)
217 err = libusb20_dev_req_string_sync((void *)dev,
218 strindex, langid, buf, buflen);
227 usb_get_string_simple(usb_dev_handle * dev, int strindex,
228 char *buf, size_t buflen)
238 err = libusb20_dev_req_string_simple_sync((void *)dev,
239 strindex, buf, buflen);
244 return (strlen(buf));
248 usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type,
249 uint8_t ep_index, void *buf, int size)
251 memset(buf, 0, size);
259 return (usb_control_msg(udev, ep | USB_ENDPOINT_IN,
260 USB_REQ_GET_DESCRIPTOR, (type << 8) + ep_index, 0,
265 usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t desc_index,
268 memset(buf, 0, size);
276 return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
277 (type << 8) + desc_index, 0, buf, size, 1000));
281 usb_parse_descriptor(uint8_t *source, char *description, void *dest)
283 uint8_t *sp = source;
289 for (cp = description; *cp; cp++) {
291 case 'b': /* 8-bit byte */
295 * 16-bit word, convert from little endian to CPU
298 w = (sp[1] << 8) | sp[0];
300 /* Align to word boundary */
301 dp += ((dp - (uint8_t *)0) & 1);
302 *((uint16_t *)dp) = w;
306 * 32-bit dword, convert from little endian to CPU
309 d = (sp[3] << 24) | (sp[2] << 16) |
310 (sp[1] << 8) | sp[0];
312 /* Align to word boundary */
313 dp += ((dp - (uint8_t *)0) & 1);
314 /* Align to double word boundary */
315 dp += ((dp - (uint8_t *)0) & 2);
316 *((uint32_t *)dp) = d;
321 return (sp - source);
325 usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen)
330 ptr = ps->a.currextra->ptr;
331 len = ps->a.currextra->len;
333 if (ps->preparse == 0) {
334 memcpy(ps->b.currextra, ptr, len);
335 *pptr = ps->b.currextra;
338 ps->b.currextra += len;
343 usb_parse_endpoint(struct usb_parse_state *ps)
345 struct usb_endpoint_descriptor *bep;
346 struct libusb20_endpoint *aep;
349 bep = ps->b.currep++;
351 if (ps->preparse == 0) {
352 /* copy descriptor fields */
353 bep->bLength = aep->desc.bLength;
354 bep->bDescriptorType = aep->desc.bDescriptorType;
355 bep->bEndpointAddress = aep->desc.bEndpointAddress;
356 bep->bmAttributes = aep->desc.bmAttributes;
357 bep->wMaxPacketSize = aep->desc.wMaxPacketSize;
358 bep->bInterval = aep->desc.bInterval;
359 bep->bRefresh = aep->desc.bRefresh;
360 bep->bSynchAddress = aep->desc.bSynchAddress;
362 ps->a.currextra = &aep->extra;
363 usb_parse_extra(ps, &bep->extra, &bep->extralen);
368 usb_parse_iface_sub(struct usb_parse_state *ps)
370 struct libusb20_interface *aifc;
371 struct usb_interface_descriptor *bifc;
374 aifc = ps->a.currifc;
375 bifc = ps->b.currifc++;
377 if (ps->preparse == 0) {
378 /* copy descriptor fields */
379 bifc->bLength = aifc->desc.bLength;
380 bifc->bDescriptorType = aifc->desc.bDescriptorType;
381 bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber;
382 bifc->bAlternateSetting = aifc->desc.bAlternateSetting;
383 bifc->bNumEndpoints = aifc->num_endpoints;
384 bifc->bInterfaceClass = aifc->desc.bInterfaceClass;
385 bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass;
386 bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol;
387 bifc->iInterface = aifc->desc.iInterface;
388 bifc->endpoint = ps->b.currep;
390 for (x = 0; x != aifc->num_endpoints; x++) {
391 ps->a.currep = aifc->endpoints + x;
392 usb_parse_endpoint(ps);
395 ps->a.currextra = &aifc->extra;
396 usb_parse_extra(ps, &bifc->extra, &bifc->extralen);
401 usb_parse_iface(struct usb_parse_state *ps)
403 struct libusb20_interface *aifc;
404 struct usb_interface *bifc;
407 aifc = ps->a.currifc;
408 bifc = ps->b.currifcw++;
410 if (ps->preparse == 0) {
411 /* initialise interface wrapper */
412 bifc->altsetting = ps->b.currifc;
413 bifc->num_altsetting = aifc->num_altsetting + 1;
415 usb_parse_iface_sub(ps);
417 for (x = 0; x != aifc->num_altsetting; x++) {
418 ps->a.currifc = aifc->altsetting + x;
419 usb_parse_iface_sub(ps);
425 usb_parse_config(struct usb_parse_state *ps)
427 struct libusb20_config *acfg;
428 struct usb_config_descriptor *bcfg;
431 acfg = ps->a.currcfg;
432 bcfg = ps->b.currcfg;
434 if (ps->preparse == 0) {
435 /* initialise config wrapper */
436 bcfg->bLength = acfg->desc.bLength;
437 bcfg->bDescriptorType = acfg->desc.bDescriptorType;
438 bcfg->wTotalLength = acfg->desc.wTotalLength;
439 bcfg->bNumInterfaces = acfg->num_interface;
440 bcfg->bConfigurationValue = acfg->desc.bConfigurationValue;
441 bcfg->iConfiguration = acfg->desc.iConfiguration;
442 bcfg->bmAttributes = acfg->desc.bmAttributes;
443 bcfg->MaxPower = acfg->desc.bMaxPower;
444 bcfg->interface = ps->b.currifcw;
446 for (x = 0; x != acfg->num_interface; x++) {
447 ps->a.currifc = acfg->interface + x;
451 ps->a.currextra = &acfg->extra;
452 usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen);
457 usb_parse_configuration(struct usb_config_descriptor *config,
460 struct usb_parse_state ps;
467 if ((buffer == NULL) || (config == NULL)) {
470 memset(&ps, 0, sizeof(ps));
472 ps.a.currcfg = libusb20_parse_config_desc(buffer);
473 ps.b.currcfg = config;
474 if (ps.a.currcfg == NULL) {
475 /* could not parse config or out of memory */
478 /* do the pre-parse */
480 usb_parse_config(&ps);
482 a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0));
483 b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0));
484 c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0));
485 d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0));
487 /* allocate memory for our configuration */
488 ptr = malloc(a + b + c + d);
490 /* free config structure */
495 /* "currifcw" must be first, hence this pointer is freed */
496 ps.b.currifcw = (void *)(ptr);
497 ps.b.currifc = (void *)(ptr + a);
498 ps.b.currep = (void *)(ptr + a + b);
499 ps.b.currextra = (void *)(ptr + a + b + c);
501 /* generate a libusb v0.1 compatible structure */
503 usb_parse_config(&ps);
505 /* free config structure */
508 return (0); /* success */
512 usb_destroy_configuration(struct usb_device *dev)
516 if (dev->config == NULL) {
519 for (c = 0; c != dev->descriptor.bNumConfigurations; c++) {
520 struct usb_config_descriptor *cf = &dev->config[c];
522 if (cf->interface != NULL) {
524 cf->interface = NULL;
534 usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
536 struct usb_device *dev;
537 struct libusb20_device *pdev;
548 dev = usb_device(udev);
551 if (dev->descriptor.bNumConfigurations == 0) {
555 size = dev->descriptor.bNumConfigurations *
556 sizeof(struct usb_config_descriptor);
558 dev->config = malloc(size);
559 if (dev->config == NULL) {
563 memset(dev->config, 0, size);
565 for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {
567 error = (pdev->methods->get_config_desc_full) (
568 pdev, &ptr, &len, x);
571 usb_destroy_configuration(dev);
574 usb_parse_configuration(dev->config + x, ptr);
576 /* free config buffer */
583 usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size,
584 int timeout, int is_intr)
586 struct libusb20_transfer *xfer;
592 xfer = usb_get_transfer_by_ep_no(dev, ep);
596 if (libusb20_tr_pending(xfer)) {
597 /* there is already a transfer ongoing */
600 maxsize = libusb20_tr_get_max_total_length(xfer);
604 * We allow transferring zero bytes which is the same
605 * equivalent to a zero length USB packet.
610 if (temp > maxsize) {
611 /* find maximum possible length */
615 libusb20_tr_setup_intr(xfer, bytes, temp, timeout);
617 libusb20_tr_setup_bulk(xfer, bytes, temp, timeout);
619 libusb20_tr_start(xfer);
623 if (libusb20_dev_process((void *)dev) != 0) {
624 /* device detached */
627 if (libusb20_tr_pending(xfer) == 0) {
628 /* transfer complete */
631 /* wait for USB event from kernel */
632 libusb20_dev_wait_process((void *)dev, -1);
635 switch (libusb20_tr_get_status(xfer)) {
639 case LIBUSB20_TRANSFER_TIMED_OUT:
640 /* transfer timeout */
643 /* other transfer error */
646 actlen = libusb20_tr_get_actual_length(xfer);
651 if (actlen != temp) {
657 return (bytes - oldbytes);
661 usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes,
662 int size, int timeout)
664 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
665 bytes, size, timeout, 0));
669 usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes,
670 int size, int timeout)
672 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
673 bytes, size, timeout, 0));
677 usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes,
678 int size, int timeout)
680 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
681 bytes, size, timeout, 1));
685 usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes,
686 int size, int timeout)
688 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
689 bytes, size, timeout, 1));
693 usb_control_msg(usb_dev_handle * dev, int requesttype, int request,
694 int value, int wIndex, char *bytes, int size, int timeout)
696 struct LIBUSB20_CONTROL_SETUP_DECODED req;
700 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
702 req.bmRequestType = requesttype;
703 req.bRequest = request;
708 err = libusb20_dev_request_sync((void *)dev, &req, bytes,
709 &actlen, timeout, 0);
718 usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
720 struct usb_device *dev;
725 * Need to translate from "bConfigurationValue" to
726 * configuration index:
729 if (bConfigurationValue == 0) {
733 /* lookup configuration index */
734 dev = usb_device(udev);
736 /* check if the configuration array is not there */
737 if (dev->config == NULL) {
741 if (i == dev->descriptor.bNumConfigurations) {
742 /* "bConfigurationValue" not found */
745 if ((dev->config + i)->bConfigurationValue ==
746 bConfigurationValue) {
752 err = libusb20_dev_set_config_index((void *)udev, i);
761 usb_claim_interface(usb_dev_handle * dev, int interface)
763 struct libusb20_device *pdev = (void *)dev;
765 pdev->claimed_interface = interface;
771 usb_release_interface(usb_dev_handle * dev, int interface)
778 usb_set_altinterface(usb_dev_handle * dev, int alternate)
780 struct libusb20_device *pdev = (void *)dev;
784 iface = pdev->claimed_interface;
786 err = libusb20_dev_set_alt_index((void *)dev, iface, alternate);
795 usb_resetep(usb_dev_handle * dev, unsigned int ep)
797 /* emulate an endpoint reset through clear-STALL */
798 return (usb_clear_halt(dev, ep));
802 usb_clear_halt(usb_dev_handle * dev, unsigned int ep)
804 struct libusb20_transfer *xfer;
806 xfer = usb_get_transfer_by_ep_no(dev, ep);
810 libusb20_tr_clear_stall_sync(xfer);
816 usb_reset(usb_dev_handle * dev)
820 err = libusb20_dev_reset((void *)dev);
826 * Be compatible with LibUSB from sourceforge and close the
827 * handle after reset!
829 return (usb_close(dev));
833 usb_check_connected(usb_dev_handle * dev)
837 err = libusb20_dev_check_connected((void *)dev);
849 return ("Unknown error");
860 usb_set_debug(int level)
862 /* use kernel UGEN debugging if you need to see what is going on */
867 usb_find_busses(void)
869 usb_busses = &usb_global_bus;
874 usb_find_devices(void)
876 struct libusb20_device *pdev;
877 struct usb_device *udev;
878 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
882 /* cleanup after last device search */
883 /* close all opened devices, if any */
885 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
886 udev = pdev->privLuData;
887 libusb20_be_dequeue_device(usb_backend, pdev);
888 libusb20_dev_free(pdev);
890 LIST_DEL(usb_global_bus.devices, udev);
895 /* free old USB backend, if any */
897 libusb20_be_free(usb_backend);
899 /* do a new backend device search */
900 usb_backend = libusb20_be_alloc_default();
901 if (usb_backend == NULL) {
904 /* iterate all devices */
908 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
909 udev = malloc(sizeof(*udev));
913 memset(udev, 0, sizeof(*udev));
915 udev->bus = &usb_global_bus;
917 snprintf(udev->filename, sizeof(udev->filename),
919 libusb20_dev_get_bus_number(pdev),
920 libusb20_dev_get_address(pdev));
922 ddesc = libusb20_dev_get_device_desc(pdev);
924 udev->descriptor.bLength = sizeof(udev->descriptor);
925 udev->descriptor.bDescriptorType = ddesc->bDescriptorType;
926 udev->descriptor.bcdUSB = ddesc->bcdUSB;
927 udev->descriptor.bDeviceClass = ddesc->bDeviceClass;
928 udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass;
929 udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol;
930 udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0;
931 udev->descriptor.idVendor = ddesc->idVendor;
932 udev->descriptor.idProduct = ddesc->idProduct;
933 udev->descriptor.bcdDevice = ddesc->bcdDevice;
934 udev->descriptor.iManufacturer = ddesc->iManufacturer;
935 udev->descriptor.iProduct = ddesc->iProduct;
936 udev->descriptor.iSerialNumber = ddesc->iSerialNumber;
937 udev->descriptor.bNumConfigurations =
938 ddesc->bNumConfigurations;
939 if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
940 /* truncate number of configurations */
941 udev->descriptor.bNumConfigurations = USB_MAXCONFIG;
943 udev->devnum = devnum++;
944 /* link together the two structures */
946 pdev->privLuData = udev;
948 err = libusb20_dev_open(pdev, 0);
950 /* XXX get all config descriptors by default */
951 usb_fetch_and_parse_descriptors((void *)pdev);
952 libusb20_dev_close(pdev);
954 LIST_ADD(usb_global_bus.devices, udev);
957 return (devnum - 1); /* success */
961 usb_device(usb_dev_handle * dev)
963 struct libusb20_device *pdev;
967 return (pdev->privLuData);
977 usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen)
979 struct libusb20_device *pdev;
992 err = libusb20_dev_get_iface_desc(pdev, interface, name, namelen);
996 /* we only want the driver name */
997 ptr = strstr(name, ":");
1005 usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface)
1007 struct libusb20_device *pdev;
1015 err = libusb20_dev_detach_kernel_driver(pdev, interface);