libc: Include <unistd.h> for ftell/ftruncate/truncate prototypes.
[dragonfly.git] / lib / libusb / libusb01.c
blobe858c038b73d8a9c17e843f66cba34fa7a621f83
1 /* $FreeBSD: head/lib/libusb/libusb01.c 264344 2014-04-11 14:11:55Z hselasky $ */
2 /*-
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
7 * are met:
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
24 * SUCH DAMAGE.
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
33 #else
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <sys/queue.h>
40 #endif
42 #include "libusb20.h"
43 #include "libusb20_desc.h"
44 #include "libusb20_int.h"
45 #include "usb.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) \
52 do { \
53 if (begin) { \
54 ent->next = begin; \
55 ent->next->prev = ent; \
56 } else { \
57 ent->next = NULL; \
58 } \
59 ent->prev = NULL; \
60 begin = ent; \
61 } while(0)
63 #define LIST_DEL(begin, ent) \
64 do { \
65 if (ent->prev) { \
66 ent->prev->next = ent->next; \
67 } else { \
68 begin = ent->next; \
69 } \
70 if (ent->next) { \
71 ent->next->prev = ent->prev; \
72 } \
73 ent->prev = NULL; \
74 ent->next = NULL; \
75 } while (0)
77 struct usb_bus *usb_busses = NULL;
79 static struct usb_bus usb_global_bus = {
80 .dirname = {"/dev/usb"},
81 .root_dev = NULL,
82 .devices = NULL,
85 static struct libusb20_backend *usb_backend = NULL;
87 struct usb_parse_state {
89 struct {
90 struct libusb20_endpoint *currep;
91 struct libusb20_interface *currifc;
92 struct libusb20_config *currcfg;
93 struct libusb20_me_struct *currextra;
94 } a;
96 struct {
97 struct usb_config_descriptor *currcfg;
98 struct usb_interface_descriptor *currifc;
99 struct usb_endpoint_descriptor *currep;
100 struct usb_interface *currifcw;
101 uint8_t *currextra;
102 } b;
104 uint8_t preparse;
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;
112 int err;
113 uint32_t bufsize;
114 uint8_t x;
115 uint8_t speed;
117 x = (ep_no & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 2;
119 if (ep_no & LIBUSB20_ENDPOINT_DIR_MASK) {
120 /* this is an IN endpoint */
121 x |= 1;
123 speed = libusb20_dev_get_speed(pdev);
125 /* select a sensible buffer size */
126 if (speed == LIBUSB20_SPEED_LOW) {
127 bufsize = 256;
128 } else if (speed == LIBUSB20_SPEED_FULL) {
129 bufsize = 4096;
130 } else if (speed == LIBUSB20_SPEED_SUPER) {
131 bufsize = 65536;
132 } else {
133 bufsize = 16384;
136 xfer = libusb20_tr_get_pointer(pdev, x);
138 if (xfer == NULL)
139 return (xfer);
141 err = libusb20_tr_open(xfer, bufsize, 1, ep_no);
142 if (err == LIBUSB20_ERROR_BUSY) {
143 /* already opened */
144 return (xfer);
145 } else if (err) {
146 return (NULL);
148 /* success */
149 return (xfer);
152 usb_dev_handle *
153 usb_open(struct usb_device *dev)
155 int err;
157 err = libusb20_dev_open(dev->dev, 16 * 2);
158 if (err == LIBUSB20_ERROR_BUSY) {
160 * Workaround buggy USB applications which open the USB
161 * device multiple times:
163 return (dev->dev);
165 if (err)
166 return (NULL);
169 * Dequeue USB device from backend queue so that it does not get
170 * freed when the backend is re-scanned:
172 libusb20_be_dequeue_device(usb_backend, dev->dev);
174 return (dev->dev);
178 usb_close(usb_dev_handle * udev)
180 struct usb_device *dev;
181 int err;
183 err = libusb20_dev_close((void *)udev);
185 if (err)
186 return (-1);
188 if (usb_backend != NULL) {
190 * Enqueue USB device to backend queue so that it gets freed
191 * when the backend is re-scanned:
193 libusb20_be_enqueue_device(usb_backend, (void *)udev);
194 } else {
196 * The backend is gone. Free device data so that we
197 * don't start leaking memory!
199 dev = usb_device(udev);
200 libusb20_dev_free((void *)udev);
201 LIST_DEL(usb_global_bus.devices, dev);
202 free(dev);
204 return (0);
208 usb_get_string(usb_dev_handle * dev, int strindex,
209 int langid, char *buf, size_t buflen)
211 int err;
213 if (dev == NULL)
214 return (-1);
216 if (buflen > 65535)
217 buflen = 65535;
219 err = libusb20_dev_req_string_sync((void *)dev,
220 strindex, langid, buf, buflen);
222 if (err)
223 return (-1);
225 return (0);
229 usb_get_string_simple(usb_dev_handle * dev, int strindex,
230 char *buf, size_t buflen)
232 int err;
234 if (dev == NULL)
235 return (-1);
237 if (buflen > 65535)
238 buflen = 65535;
240 err = libusb20_dev_req_string_simple_sync((void *)dev,
241 strindex, buf, buflen);
243 if (err)
244 return (-1);
246 return (strlen(buf));
250 usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type,
251 uint8_t ep_index, void *buf, int size)
253 memset(buf, 0, size);
255 if (udev == NULL)
256 return (-1);
258 if (size > 65535)
259 size = 65535;
261 return (usb_control_msg(udev, ep | USB_ENDPOINT_IN,
262 USB_REQ_GET_DESCRIPTOR, (type << 8) + ep_index, 0,
263 buf, size, 1000));
267 usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t desc_index,
268 void *buf, int size)
270 memset(buf, 0, size);
272 if (udev == NULL)
273 return (-1);
275 if (size > 65535)
276 size = 65535;
278 return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
279 (type << 8) + desc_index, 0, buf, size, 1000));
283 usb_parse_descriptor(uint8_t *source, char *description, void *dest)
285 uint8_t *sp = source;
286 uint8_t *dp = dest;
287 uint16_t w;
288 uint32_t d;
289 char *cp;
291 for (cp = description; *cp; cp++) {
292 switch (*cp) {
293 case 'b': /* 8-bit byte */
294 *dp++ = *sp++;
295 break;
297 * 16-bit word, convert from little endian to CPU
299 case 'w':
300 w = (sp[1] << 8) | sp[0];
301 sp += 2;
302 /* Align to word boundary */
303 dp += ((dp - (uint8_t *)0) & 1);
304 *((uint16_t *)dp) = w;
305 dp += 2;
306 break;
308 * 32-bit dword, convert from little endian to CPU
310 case 'd':
311 d = (sp[3] << 24) | (sp[2] << 16) |
312 (sp[1] << 8) | sp[0];
313 sp += 4;
314 /* Align to word boundary */
315 dp += ((dp - (uint8_t *)0) & 1);
316 /* Align to double word boundary */
317 dp += ((dp - (uint8_t *)0) & 2);
318 *((uint32_t *)dp) = d;
319 dp += 4;
320 break;
323 return (sp - source);
326 static void
327 usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen)
329 void *ptr;
330 uint16_t len;
332 ptr = ps->a.currextra->ptr;
333 len = ps->a.currextra->len;
335 if (ps->preparse == 0) {
336 memcpy(ps->b.currextra, ptr, len);
337 *pptr = ps->b.currextra;
338 *plen = len;
340 ps->b.currextra += len;
341 return;
344 static void
345 usb_parse_endpoint(struct usb_parse_state *ps)
347 struct usb_endpoint_descriptor *bep;
348 struct libusb20_endpoint *aep;
350 aep = ps->a.currep;
351 bep = ps->b.currep++;
353 if (ps->preparse == 0) {
354 /* copy descriptor fields */
355 bep->bLength = aep->desc.bLength;
356 bep->bDescriptorType = aep->desc.bDescriptorType;
357 bep->bEndpointAddress = aep->desc.bEndpointAddress;
358 bep->bmAttributes = aep->desc.bmAttributes;
359 bep->wMaxPacketSize = aep->desc.wMaxPacketSize;
360 bep->bInterval = aep->desc.bInterval;
361 bep->bRefresh = aep->desc.bRefresh;
362 bep->bSynchAddress = aep->desc.bSynchAddress;
364 ps->a.currextra = &aep->extra;
365 usb_parse_extra(ps, &bep->extra, &bep->extralen);
366 return;
369 static void
370 usb_parse_iface_sub(struct usb_parse_state *ps)
372 struct libusb20_interface *aifc;
373 struct usb_interface_descriptor *bifc;
374 uint8_t x;
376 aifc = ps->a.currifc;
377 bifc = ps->b.currifc++;
379 if (ps->preparse == 0) {
380 /* copy descriptor fields */
381 bifc->bLength = aifc->desc.bLength;
382 bifc->bDescriptorType = aifc->desc.bDescriptorType;
383 bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber;
384 bifc->bAlternateSetting = aifc->desc.bAlternateSetting;
385 bifc->bNumEndpoints = aifc->num_endpoints;
386 bifc->bInterfaceClass = aifc->desc.bInterfaceClass;
387 bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass;
388 bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol;
389 bifc->iInterface = aifc->desc.iInterface;
390 bifc->endpoint = ps->b.currep;
392 for (x = 0; x != aifc->num_endpoints; x++) {
393 ps->a.currep = aifc->endpoints + x;
394 usb_parse_endpoint(ps);
397 ps->a.currextra = &aifc->extra;
398 usb_parse_extra(ps, &bifc->extra, &bifc->extralen);
399 return;
402 static void
403 usb_parse_iface(struct usb_parse_state *ps)
405 struct libusb20_interface *aifc;
406 struct usb_interface *bifc;
407 uint8_t x;
409 aifc = ps->a.currifc;
410 bifc = ps->b.currifcw++;
412 if (ps->preparse == 0) {
413 /* initialise interface wrapper */
414 bifc->altsetting = ps->b.currifc;
415 bifc->num_altsetting = aifc->num_altsetting + 1;
417 usb_parse_iface_sub(ps);
419 for (x = 0; x != aifc->num_altsetting; x++) {
420 ps->a.currifc = aifc->altsetting + x;
421 usb_parse_iface_sub(ps);
423 return;
426 static void
427 usb_parse_config(struct usb_parse_state *ps)
429 struct libusb20_config *acfg;
430 struct usb_config_descriptor *bcfg;
431 uint8_t x;
433 acfg = ps->a.currcfg;
434 bcfg = ps->b.currcfg;
436 if (ps->preparse == 0) {
437 /* initialise config wrapper */
438 bcfg->bLength = acfg->desc.bLength;
439 bcfg->bDescriptorType = acfg->desc.bDescriptorType;
440 bcfg->wTotalLength = acfg->desc.wTotalLength;
441 bcfg->bNumInterfaces = acfg->num_interface;
442 bcfg->bConfigurationValue = acfg->desc.bConfigurationValue;
443 bcfg->iConfiguration = acfg->desc.iConfiguration;
444 bcfg->bmAttributes = acfg->desc.bmAttributes;
445 bcfg->MaxPower = acfg->desc.bMaxPower;
446 bcfg->interface = ps->b.currifcw;
448 for (x = 0; x != acfg->num_interface; x++) {
449 ps->a.currifc = acfg->interface + x;
450 usb_parse_iface(ps);
453 ps->a.currextra = &acfg->extra;
454 usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen);
455 return;
459 usb_parse_configuration(struct usb_config_descriptor *config,
460 uint8_t *buffer)
462 struct usb_parse_state ps;
463 uint8_t *ptr;
464 uint32_t a;
465 uint32_t b;
466 uint32_t c;
467 uint32_t d;
469 if ((buffer == NULL) || (config == NULL)) {
470 return (-1);
472 memset(&ps, 0, sizeof(ps));
474 ps.a.currcfg = libusb20_parse_config_desc(buffer);
475 ps.b.currcfg = config;
476 if (ps.a.currcfg == NULL) {
477 /* could not parse config or out of memory */
478 return (-1);
480 /* do the pre-parse */
481 ps.preparse = 1;
482 usb_parse_config(&ps);
484 a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0));
485 b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0));
486 c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0));
487 d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0));
489 /* allocate memory for our configuration */
490 ptr = malloc(a + b + c + d);
491 if (ptr == NULL) {
492 /* free config structure */
493 free(ps.a.currcfg);
494 return (-1);
497 /* "currifcw" must be first, hence this pointer is freed */
498 ps.b.currifcw = (void *)(ptr);
499 ps.b.currifc = (void *)(ptr + a);
500 ps.b.currep = (void *)(ptr + a + b);
501 ps.b.currextra = (void *)(ptr + a + b + c);
503 /* generate a libusb v0.1 compatible structure */
504 ps.preparse = 0;
505 usb_parse_config(&ps);
507 /* free config structure */
508 free(ps.a.currcfg);
510 return (0); /* success */
513 void
514 usb_destroy_configuration(struct usb_device *dev)
516 uint8_t c;
518 if (dev->config == NULL) {
519 return;
521 for (c = 0; c != dev->descriptor.bNumConfigurations; c++) {
522 struct usb_config_descriptor *cf = &dev->config[c];
524 if (cf->interface != NULL) {
525 free(cf->interface);
526 cf->interface = NULL;
530 free(dev->config);
531 dev->config = NULL;
532 return;
535 void
536 usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
538 struct usb_device *dev;
539 struct libusb20_device *pdev;
540 uint8_t *ptr;
541 int error;
542 uint32_t size;
543 uint16_t len;
544 uint8_t x;
546 if (udev == NULL) {
547 /* be NULL safe */
548 return;
550 dev = usb_device(udev);
551 pdev = (void *)udev;
553 if (dev->descriptor.bNumConfigurations == 0) {
554 /* invalid device */
555 return;
557 size = dev->descriptor.bNumConfigurations *
558 sizeof(struct usb_config_descriptor);
560 dev->config = malloc(size);
561 if (dev->config == NULL) {
562 /* out of memory */
563 return;
565 memset(dev->config, 0, size);
567 for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {
569 error = (pdev->methods->get_config_desc_full) (
570 pdev, &ptr, &len, x);
572 if (error) {
573 usb_destroy_configuration(dev);
574 return;
576 usb_parse_configuration(dev->config + x, ptr);
578 /* free config buffer */
579 free(ptr);
581 return;
584 static int
585 usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size,
586 int timeout, int is_intr)
588 struct libusb20_transfer *xfer;
589 uint32_t temp;
590 uint32_t maxsize;
591 uint32_t actlen;
592 char *oldbytes;
594 xfer = usb_get_transfer_by_ep_no(dev, ep);
595 if (xfer == NULL)
596 return (-1);
598 if (libusb20_tr_pending(xfer)) {
599 /* there is already a transfer ongoing */
600 return (-1);
602 maxsize = libusb20_tr_get_max_total_length(xfer);
603 oldbytes = bytes;
606 * We allow transferring zero bytes which is the same
607 * equivalent to a zero length USB packet.
609 do {
611 temp = size;
612 if (temp > maxsize) {
613 /* find maximum possible length */
614 temp = maxsize;
616 if (is_intr)
617 libusb20_tr_setup_intr(xfer, bytes, temp, timeout);
618 else
619 libusb20_tr_setup_bulk(xfer, bytes, temp, timeout);
621 libusb20_tr_start(xfer);
623 while (1) {
625 if (libusb20_dev_process((void *)dev) != 0) {
626 /* device detached */
627 return (-1);
629 if (libusb20_tr_pending(xfer) == 0) {
630 /* transfer complete */
631 break;
633 /* wait for USB event from kernel */
634 libusb20_dev_wait_process((void *)dev, -1);
637 switch (libusb20_tr_get_status(xfer)) {
638 case 0:
639 /* success */
640 break;
641 case LIBUSB20_TRANSFER_TIMED_OUT:
642 /* transfer timeout */
643 return (-ETIMEDOUT);
644 default:
645 /* other transfer error */
646 return (-ENXIO);
648 actlen = libusb20_tr_get_actual_length(xfer);
650 bytes += actlen;
651 size -= actlen;
653 if (actlen != temp) {
654 /* short transfer */
655 break;
657 } while (size > 0);
659 return (bytes - oldbytes);
663 usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes,
664 int size, int timeout)
666 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
667 bytes, size, timeout, 0));
671 usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes,
672 int size, int timeout)
674 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
675 bytes, size, timeout, 0));
679 usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes,
680 int size, int timeout)
682 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
683 bytes, size, timeout, 1));
687 usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes,
688 int size, int timeout)
690 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
691 bytes, size, timeout, 1));
695 usb_control_msg(usb_dev_handle * dev, int requesttype, int request,
696 int value, int wIndex, char *bytes, int size, int timeout)
698 struct LIBUSB20_CONTROL_SETUP_DECODED req;
699 int err;
700 uint16_t actlen;
702 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
704 req.bmRequestType = requesttype;
705 req.bRequest = request;
706 req.wValue = value;
707 req.wIndex = wIndex;
708 req.wLength = size;
710 err = libusb20_dev_request_sync((void *)dev, &req, bytes,
711 &actlen, timeout, 0);
713 if (err)
714 return (-1);
716 return (actlen);
720 usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
722 struct usb_device *dev;
723 int err;
724 uint8_t i;
727 * Need to translate from "bConfigurationValue" to
728 * configuration index:
731 if (bConfigurationValue == 0) {
732 /* unconfigure */
733 i = 255;
734 } else {
735 /* lookup configuration index */
736 dev = usb_device(udev);
738 /* check if the configuration array is not there */
739 if (dev->config == NULL) {
740 return (-1);
742 for (i = 0;; i++) {
743 if (i == dev->descriptor.bNumConfigurations) {
744 /* "bConfigurationValue" not found */
745 return (-1);
747 if ((dev->config + i)->bConfigurationValue ==
748 bConfigurationValue) {
749 break;
754 err = libusb20_dev_set_config_index((void *)udev, i);
756 if (err)
757 return (-1);
759 return (0);
763 usb_claim_interface(usb_dev_handle * dev, int interface)
765 struct libusb20_device *pdev = (void *)dev;
767 pdev->claimed_interface = interface;
769 return (0);
773 usb_release_interface(usb_dev_handle * dev, int interface)
775 /* do nothing */
776 return (0);
780 usb_set_altinterface(usb_dev_handle * dev, int alternate)
782 struct libusb20_device *pdev = (void *)dev;
783 int err;
784 uint8_t iface;
786 iface = pdev->claimed_interface;
788 err = libusb20_dev_set_alt_index((void *)dev, iface, alternate);
790 if (err)
791 return (-1);
793 return (0);
797 usb_resetep(usb_dev_handle * dev, unsigned int ep)
799 /* emulate an endpoint reset through clear-STALL */
800 return (usb_clear_halt(dev, ep));
804 usb_clear_halt(usb_dev_handle * dev, unsigned int ep)
806 struct libusb20_transfer *xfer;
808 xfer = usb_get_transfer_by_ep_no(dev, ep);
809 if (xfer == NULL)
810 return (-1);
812 libusb20_tr_clear_stall_sync(xfer);
814 return (0);
818 usb_reset(usb_dev_handle * dev)
820 int err;
822 err = libusb20_dev_reset((void *)dev);
824 if (err)
825 return (-1);
828 * Be compatible with LibUSB from sourceforge and close the
829 * handle after reset!
831 return (usb_close(dev));
835 usb_check_connected(usb_dev_handle * dev)
837 int err;
839 err = libusb20_dev_check_connected((void *)dev);
841 if (err)
842 return (-1);
844 return (0);
847 const char *
848 usb_strerror(void)
850 /* TODO */
851 return ("Unknown error");
854 void
855 usb_init(void)
857 /* nothing to do */
858 return;
861 void
862 usb_set_debug(int level)
864 /* use kernel UGEN debugging if you need to see what is going on */
865 return;
869 usb_find_busses(void)
871 usb_busses = &usb_global_bus;
872 return (1);
876 usb_find_devices(void)
878 struct libusb20_device *pdev;
879 struct usb_device *udev;
880 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
881 int devnum;
882 int err;
884 /* cleanup after last device search */
885 /* close all opened devices, if any */
887 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
888 udev = pdev->privLuData;
889 libusb20_be_dequeue_device(usb_backend, pdev);
890 libusb20_dev_free(pdev);
891 if (udev != NULL) {
892 LIST_DEL(usb_global_bus.devices, udev);
893 free(udev);
897 /* free old USB backend, if any */
899 libusb20_be_free(usb_backend);
901 /* do a new backend device search */
902 usb_backend = libusb20_be_alloc_default();
903 if (usb_backend == NULL) {
904 return (-1);
906 /* iterate all devices */
908 devnum = 1;
909 pdev = NULL;
910 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
911 udev = malloc(sizeof(*udev));
912 if (udev == NULL)
913 break;
915 memset(udev, 0, sizeof(*udev));
917 udev->bus = &usb_global_bus;
919 snprintf(udev->filename, sizeof(udev->filename),
920 "/dev/ugen%u.%u",
921 libusb20_dev_get_bus_number(pdev),
922 libusb20_dev_get_address(pdev));
924 ddesc = libusb20_dev_get_device_desc(pdev);
926 udev->descriptor.bLength = sizeof(udev->descriptor);
927 udev->descriptor.bDescriptorType = ddesc->bDescriptorType;
928 udev->descriptor.bcdUSB = ddesc->bcdUSB;
929 udev->descriptor.bDeviceClass = ddesc->bDeviceClass;
930 udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass;
931 udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol;
932 udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0;
933 udev->descriptor.idVendor = ddesc->idVendor;
934 udev->descriptor.idProduct = ddesc->idProduct;
935 udev->descriptor.bcdDevice = ddesc->bcdDevice;
936 udev->descriptor.iManufacturer = ddesc->iManufacturer;
937 udev->descriptor.iProduct = ddesc->iProduct;
938 udev->descriptor.iSerialNumber = ddesc->iSerialNumber;
939 udev->descriptor.bNumConfigurations =
940 ddesc->bNumConfigurations;
941 if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
942 /* truncate number of configurations */
943 udev->descriptor.bNumConfigurations = USB_MAXCONFIG;
945 udev->devnum = devnum++;
946 /* link together the two structures */
947 udev->dev = pdev;
948 pdev->privLuData = udev;
950 err = libusb20_dev_open(pdev, 0);
951 if (err == 0) {
952 /* XXX get all config descriptors by default */
953 usb_fetch_and_parse_descriptors((void *)pdev);
954 libusb20_dev_close(pdev);
956 LIST_ADD(usb_global_bus.devices, udev);
959 return (devnum - 1); /* success */
962 struct usb_device *
963 usb_device(usb_dev_handle * dev)
965 struct libusb20_device *pdev;
967 pdev = (void *)dev;
969 return (pdev->privLuData);
972 struct usb_bus *
973 usb_get_busses(void)
975 return (usb_busses);
979 usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen)
981 struct libusb20_device *pdev;
982 char *ptr;
983 int err;
985 pdev = (void *)dev;
987 if (pdev == NULL)
988 return (-1);
989 if (namelen < 1)
990 return (-1);
991 if (namelen > 255)
992 namelen = 255;
994 err = libusb20_dev_get_iface_desc(pdev, interface, name, namelen);
995 if (err != 0)
996 return (-1);
998 /* we only want the driver name */
999 ptr = strstr(name, ":");
1000 if (ptr != NULL)
1001 *ptr = 0;
1003 return (0);
1007 usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface)
1009 struct libusb20_device *pdev;
1010 int err;
1012 pdev = (void *)dev;
1014 if (pdev == NULL)
1015 return (-1);
1017 err = libusb20_dev_detach_kernel_driver(pdev, interface);
1018 if (err != 0)
1019 return (-1);
1021 return (0);