1 /* $FreeBSD: head/lib/libusb/libusb01.c 264344 2014-04-11 14:11:55Z 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
) {
130 } else if (speed
== LIBUSB20_SPEED_SUPER
) {
136 xfer
= libusb20_tr_get_pointer(pdev
, x
);
141 err
= libusb20_tr_open(xfer
, bufsize
, 1, ep_no
);
142 if (err
== LIBUSB20_ERROR_BUSY
) {
153 usb_open(struct usb_device
*dev
)
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:
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
);
178 usb_close(usb_dev_handle
* udev
)
180 struct usb_device
*dev
;
183 err
= libusb20_dev_close((void *)udev
);
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
);
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
);
208 usb_get_string(usb_dev_handle
* dev
, int strindex
,
209 int langid
, char *buf
, size_t buflen
)
219 err
= libusb20_dev_req_string_sync((void *)dev
,
220 strindex
, langid
, buf
, buflen
);
229 usb_get_string_simple(usb_dev_handle
* dev
, int strindex
,
230 char *buf
, size_t buflen
)
240 err
= libusb20_dev_req_string_simple_sync((void *)dev
,
241 strindex
, buf
, buflen
);
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
);
261 return (usb_control_msg(udev
, ep
| USB_ENDPOINT_IN
,
262 USB_REQ_GET_DESCRIPTOR
, (type
<< 8) + ep_index
, 0,
267 usb_get_descriptor(usb_dev_handle
* udev
, uint8_t type
, uint8_t desc_index
,
270 memset(buf
, 0, size
);
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
;
291 for (cp
= description
; *cp
; cp
++) {
293 case 'b': /* 8-bit byte */
297 * 16-bit word, convert from little endian to CPU
300 w
= (sp
[1] << 8) | sp
[0];
302 /* Align to word boundary */
303 dp
+= ((dp
- (uint8_t *)0) & 1);
304 *((uint16_t *)dp
) = w
;
308 * 32-bit dword, convert from little endian to CPU
311 d
= (sp
[3] << 24) | (sp
[2] << 16) |
312 (sp
[1] << 8) | sp
[0];
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
;
323 return (sp
- source
);
327 usb_parse_extra(struct usb_parse_state
*ps
, uint8_t **pptr
, int *plen
)
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
;
340 ps
->b
.currextra
+= len
;
345 usb_parse_endpoint(struct usb_parse_state
*ps
)
347 struct usb_endpoint_descriptor
*bep
;
348 struct libusb20_endpoint
*aep
;
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
);
370 usb_parse_iface_sub(struct usb_parse_state
*ps
)
372 struct libusb20_interface
*aifc
;
373 struct usb_interface_descriptor
*bifc
;
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
);
403 usb_parse_iface(struct usb_parse_state
*ps
)
405 struct libusb20_interface
*aifc
;
406 struct usb_interface
*bifc
;
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
);
427 usb_parse_config(struct usb_parse_state
*ps
)
429 struct libusb20_config
*acfg
;
430 struct usb_config_descriptor
*bcfg
;
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
;
453 ps
->a
.currextra
= &acfg
->extra
;
454 usb_parse_extra(ps
, &bcfg
->extra
, &bcfg
->extralen
);
459 usb_parse_configuration(struct usb_config_descriptor
*config
,
462 struct usb_parse_state ps
;
469 if ((buffer
== NULL
) || (config
== NULL
)) {
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 */
480 /* do the pre-parse */
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
);
492 /* free config structure */
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 */
505 usb_parse_config(&ps
);
507 /* free config structure */
510 return (0); /* success */
514 usb_destroy_configuration(struct usb_device
*dev
)
518 if (dev
->config
== NULL
) {
521 for (c
= 0; c
!= dev
->descriptor
.bNumConfigurations
; c
++) {
522 struct usb_config_descriptor
*cf
= &dev
->config
[c
];
524 if (cf
->interface
!= NULL
) {
526 cf
->interface
= NULL
;
536 usb_fetch_and_parse_descriptors(usb_dev_handle
* udev
)
538 struct usb_device
*dev
;
539 struct libusb20_device
*pdev
;
550 dev
= usb_device(udev
);
553 if (dev
->descriptor
.bNumConfigurations
== 0) {
557 size
= dev
->descriptor
.bNumConfigurations
*
558 sizeof(struct usb_config_descriptor
);
560 dev
->config
= malloc(size
);
561 if (dev
->config
== NULL
) {
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
);
573 usb_destroy_configuration(dev
);
576 usb_parse_configuration(dev
->config
+ x
, ptr
);
578 /* free config buffer */
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
;
594 xfer
= usb_get_transfer_by_ep_no(dev
, ep
);
598 if (libusb20_tr_pending(xfer
)) {
599 /* there is already a transfer ongoing */
602 maxsize
= libusb20_tr_get_max_total_length(xfer
);
606 * We allow transferring zero bytes which is the same
607 * equivalent to a zero length USB packet.
612 if (temp
> maxsize
) {
613 /* find maximum possible length */
617 libusb20_tr_setup_intr(xfer
, bytes
, temp
, timeout
);
619 libusb20_tr_setup_bulk(xfer
, bytes
, temp
, timeout
);
621 libusb20_tr_start(xfer
);
625 if (libusb20_dev_process((void *)dev
) != 0) {
626 /* device detached */
629 if (libusb20_tr_pending(xfer
) == 0) {
630 /* transfer complete */
633 /* wait for USB event from kernel */
634 libusb20_dev_wait_process((void *)dev
, -1);
637 switch (libusb20_tr_get_status(xfer
)) {
641 case LIBUSB20_TRANSFER_TIMED_OUT
:
642 /* transfer timeout */
645 /* other transfer error */
648 actlen
= libusb20_tr_get_actual_length(xfer
);
653 if (actlen
!= temp
) {
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
;
702 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP
, &req
);
704 req
.bmRequestType
= requesttype
;
705 req
.bRequest
= request
;
710 err
= libusb20_dev_request_sync((void *)dev
, &req
, bytes
,
711 &actlen
, timeout
, 0);
720 usb_set_configuration(usb_dev_handle
* udev
, int bConfigurationValue
)
722 struct usb_device
*dev
;
727 * Need to translate from "bConfigurationValue" to
728 * configuration index:
731 if (bConfigurationValue
== 0) {
735 /* lookup configuration index */
736 dev
= usb_device(udev
);
738 /* check if the configuration array is not there */
739 if (dev
->config
== NULL
) {
743 if (i
== dev
->descriptor
.bNumConfigurations
) {
744 /* "bConfigurationValue" not found */
747 if ((dev
->config
+ i
)->bConfigurationValue
==
748 bConfigurationValue
) {
754 err
= libusb20_dev_set_config_index((void *)udev
, i
);
763 usb_claim_interface(usb_dev_handle
* dev
, int interface
)
765 struct libusb20_device
*pdev
= (void *)dev
;
767 pdev
->claimed_interface
= interface
;
773 usb_release_interface(usb_dev_handle
* dev
, int interface
)
780 usb_set_altinterface(usb_dev_handle
* dev
, int alternate
)
782 struct libusb20_device
*pdev
= (void *)dev
;
786 iface
= pdev
->claimed_interface
;
788 err
= libusb20_dev_set_alt_index((void *)dev
, iface
, alternate
);
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
);
812 libusb20_tr_clear_stall_sync(xfer
);
818 usb_reset(usb_dev_handle
* dev
)
822 err
= libusb20_dev_reset((void *)dev
);
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
)
839 err
= libusb20_dev_check_connected((void *)dev
);
851 return ("Unknown error");
862 usb_set_debug(int level
)
864 /* use kernel UGEN debugging if you need to see what is going on */
869 usb_find_busses(void)
871 usb_busses
= &usb_global_bus
;
876 usb_find_devices(void)
878 struct libusb20_device
*pdev
;
879 struct usb_device
*udev
;
880 struct LIBUSB20_DEVICE_DESC_DECODED
*ddesc
;
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
);
892 LIST_DEL(usb_global_bus
.devices
, 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
) {
906 /* iterate all devices */
910 while ((pdev
= libusb20_be_device_foreach(usb_backend
, pdev
))) {
911 udev
= malloc(sizeof(*udev
));
915 memset(udev
, 0, sizeof(*udev
));
917 udev
->bus
= &usb_global_bus
;
919 snprintf(udev
->filename
, sizeof(udev
->filename
),
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 */
948 pdev
->privLuData
= udev
;
950 err
= libusb20_dev_open(pdev
, 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 */
963 usb_device(usb_dev_handle
* dev
)
965 struct libusb20_device
*pdev
;
969 return (pdev
->privLuData
);
979 usb_get_driver_np(usb_dev_handle
* dev
, int interface
, char *name
, int namelen
)
981 struct libusb20_device
*pdev
;
994 err
= libusb20_dev_get_iface_desc(pdev
, interface
, name
, namelen
);
998 /* we only want the driver name */
999 ptr
= strstr(name
, ":");
1007 usb_detach_kernel_driver_np(usb_dev_handle
* dev
, int interface
)
1009 struct libusb20_device
*pdev
;
1017 err
= libusb20_dev_detach_kernel_driver(pdev
, interface
);