freebsd-skq/lib/libusb20/libusb20_compat01.c
Alfred Perlstein eabe30fc9c Bring in USB4BSD, Hans Petter Selasky rework of the USB stack
that includes significant features and SMP safety.

This commit includes a more or less complete rewrite of the *BSD USB
stack, including Host Controller and Device Controller drivers and
updating all existing USB drivers to use the new USB API:

1) A brief feature list:

  - A new and mutex enabled USB API.

  - Many USB drivers are now running Giant free.

  - Linux USB kernel compatibility layer.

  - New UGEN backend and libusb library, finally solves the "driver
    unloading" problem. The new BSD licensed libusb20 library is fully
    compatible with libusb-0.1.12 from sourceforge.

  - New "usbconfig" utility, for easy configuration of USB.

  - Full support for Split transactions, which means you can use your
    full speed USB audio device on a high speed USB HUB.

  - Full support for HS ISOC transactions, which makes writing drivers
    for various HS webcams possible, for example.

  - Full support for USB on embedded platforms, mostly cache flushing
    and buffer invalidating stuff.

  - Safer parsing of USB descriptors.

  - Autodetect of annoying USB install disks.

  - Support for USB device side mode, also called USB gadget mode,
    using the same API like the USB host side. In other words the new
    USB stack is symmetric with regard to host and device side.

  - Support for USB transfers like I/O vectors, means more throughput
    and less interrupts.

  - ... see the FreeBSD quarterly status reports under "USB project"

2) To enable the driver in the default kernel build:

2.a) Remove all existing USB device options from your kernel config
file.

2.b) Add the following USB device options to your kernel configuration
file:

# USB core support
device          usb2_core

# USB controller support
device		usb2_controller
device		usb2_controller_ehci
device		usb2_controller_ohci
device		usb2_controller_uhci

# USB mass storage support
device		usb2_storage
device		usb2_storage_mass

# USB ethernet support, requires miibus
device		usb2_ethernet
device		usb2_ethernet_aue
device		usb2_ethernet_axe
device		usb2_ethernet_cdce
device		usb2_ethernet_cue
device		usb2_ethernet_kue
device		usb2_ethernet_rue
device		usb2_ethernet_dav

# USB wireless LAN support
device		usb2_wlan
device		usb2_wlan_rum
device		usb2_wlan_ral
device		usb2_wlan_zyd

# USB serial device support
device		usb2_serial
device		usb2_serial_ark
device		usb2_serial_bsa
device		usb2_serial_bser
device		usb2_serial_chcom
device		usb2_serial_cycom
device		usb2_serial_foma
device		usb2_serial_ftdi
device		usb2_serial_gensa
device		usb2_serial_ipaq
device		usb2_serial_lpt
device		usb2_serial_mct
device		usb2_serial_modem
device		usb2_serial_moscom
device		usb2_serial_plcom
device		usb2_serial_visor
device		usb2_serial_vscom

# USB bluetooth support
device		usb2_bluetooth
device		usb2_bluetooth_ng

# USB input device support
device		usb2_input
device		usb2_input_hid
device		usb2_input_kbd
device		usb2_input_ms

# USB sound and MIDI device support
device		usb2_sound

2) To enable the driver at runtime:

2.a) Unload all existing USB modules. If USB is compiled into the
kernel then you might have to build a new kernel.

2.b) Load the "usb2_xxx.ko" modules under /boot/kernel having the same
base name like the kernel device option.

Submitted by: Hans Petter Selasky hselasky at c2i dot net
Reviewed by: imp, alfred
2008-11-04 02:31:03 +00:00

903 lines
19 KiB
C

/* $FreeBSD$ */
/*-
* Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* This file contains the emulation layer for LibUSB v0.1 from sourceforge.
*/
#include <sys/queue.h>
#include <stdlib.h>
#include <stdio.h>
#include "libusb20.h"
#include "libusb20_desc.h"
#include "libusb20_int.h"
#include "libusb20_compat01.h"
/*
* The two following macros were taken from the original LibUSB v0.1
* for sake of compatibility:
*/
#define LIST_ADD(begin, ent) \
do { \
if (begin) { \
ent->next = begin; \
ent->next->prev = ent; \
} else { \
ent->next = NULL; \
} \
ent->prev = NULL; \
begin = ent; \
} while(0)
#define LIST_DEL(begin, ent) \
do { \
if (ent->prev) { \
ent->prev->next = ent->next; \
} else { \
begin = ent->next; \
} \
if (ent->next) { \
ent->next->prev = ent->prev; \
} \
ent->prev = NULL; \
ent->next = NULL; \
} while (0)
struct usb_bus *usb_busses = NULL;
static struct usb_bus usb_global_bus = {
.dirname = {"/dev/usb"},
.root_dev = NULL,
.devices = NULL,
};
static struct libusb20_backend *usb_backend = NULL;
struct usb_parse_state {
struct {
struct libusb20_endpoint *currep;
struct libusb20_interface *currifc;
struct libusb20_config *currcfg;
struct libusb20_me_struct *currextra;
} a;
struct {
struct usb_config_descriptor *currcfg;
struct usb_interface_descriptor *currifc;
struct usb_endpoint_descriptor *currep;
struct usb_interface *currifcw;
uint8_t *currextra;
} b;
uint8_t preparse;
};
static uint8_t
usb_get_first_claimed_interface(usb_dev_handle * dev)
{
struct libusb20_device *pdev = (void *)dev;
uint32_t x;
uint8_t y;
x = pdev->claimed_interfaces;
for (y = 0; y != 32; y++) {
if (x & (1 << y))
break;
}
if (y == 32)
y = 0xFF; /* dummy */
return (y);
}
static struct libusb20_transfer *
usb_get_transfer_by_ep_no(usb_dev_handle * dev, uint8_t ep_no)
{
struct libusb20_device *pdev = (void *)dev;
struct libusb20_transfer *xfer;
int err;
uint32_t bufsize;
uint8_t x;
uint8_t speed;
x = (ep_no & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 2;
if (ep_no & LIBUSB20_ENDPOINT_DIR_MASK) {
/* this is a IN endpoint */
x |= 1;
}
speed = libusb20_dev_get_speed(pdev);
/* select a sensible buffer size */
if (speed == LIBUSB20_SPEED_LOW) {
bufsize = 256;
} else if (speed == LIBUSB20_SPEED_FULL) {
bufsize = 4096;
} else {
bufsize = 16384;
}
xfer = libusb20_tr_get_pointer(pdev, x);
if (xfer == NULL)
return (xfer);
err = libusb20_tr_open(xfer, bufsize, 1, ep_no);
if (err == LIBUSB20_ERROR_BUSY) {
/* already opened */
return (xfer);
} else if (err) {
return (NULL);
}
/* success */
return (xfer);
}
usb_dev_handle *
usb_open(struct usb_device *dev)
{
int err;
err = libusb20_dev_open(dev->dev, 16 * 2);
if (err == LIBUSB20_ERROR_BUSY) {
/*
* Workaround buggy USB applications which open the USB
* device multiple times:
*/
return (dev->dev);
}
if (err)
return (NULL);
return (dev->dev);
}
int
usb_close(usb_dev_handle * dev)
{
int err;
err = libusb20_dev_close((void *)dev);
if (err)
return (-1);
return (0);
}
int
usb_get_string(usb_dev_handle * dev, int index,
int langid, char *buf, size_t buflen)
{
int err;
err = libusb20_dev_req_string_sync((void *)dev,
index, langid, buf, buflen);
if (err)
return (-1);
return (0);
}
int
usb_get_string_simple(usb_dev_handle * dev, int index,
char *buf, size_t buflen)
{
int err;
err = libusb20_dev_req_string_simple_sync((void *)dev,
index, buf, buflen);
if (err)
return (-1);
return (strlen(buf));
}
int
usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type,
uint8_t index, void *buf, int size)
{
memset(buf, 0, size);
return (usb_control_msg(udev, ep | USB_ENDPOINT_IN,
USB_REQ_GET_DESCRIPTOR, (type << 8) + index, 0,
buf, size, 1000));
}
int
usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t index,
void *buf, int size)
{
memset(buf, 0, size);
return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
(type << 8) + index, 0, buf, size, 1000));
}
int
usb_parse_descriptor(uint8_t *source, char *description, void *dest)
{
uint8_t *sp = source;
uint8_t *dp = dest;
uint16_t w;
uint32_t d;
char *cp;
for (cp = description; *cp; cp++) {
switch (*cp) {
case 'b': /* 8-bit byte */
*dp++ = *sp++;
break;
/*
* 16-bit word, convert from little endian to CPU
*/
case 'w':
w = (sp[1] << 8) | sp[0];
sp += 2;
/* Align to word boundary */
dp += ((dp - (uint8_t *)0) & 1);
*((uint16_t *)dp) = w;
dp += 2;
break;
/*
* 32-bit dword, convert from little endian to CPU
*/
case 'd':
d = (sp[3] << 24) | (sp[2] << 16) |
(sp[1] << 8) | sp[0];
sp += 4;
/* Align to word boundary */
dp += ((dp - (uint8_t *)0) & 1);
/* Align to double word boundary */
dp += ((dp - (uint8_t *)0) & 2);
*((uint32_t *)dp) = d;
dp += 4;
break;
}
}
return (sp - source);
}
static void
usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen)
{
void *ptr;
uint16_t len;
ptr = ps->a.currextra->ptr;
len = ps->a.currextra->len;
if (ps->preparse == 0) {
memcpy(ps->b.currextra, ptr, len);
*pptr = ps->b.currextra;
*plen = len;
}
ps->b.currextra += len;
return;
}
static void
usb_parse_endpoint(struct usb_parse_state *ps)
{
struct usb_endpoint_descriptor *bep;
struct libusb20_endpoint *aep;
aep = ps->a.currep;
bep = ps->b.currep++;
if (ps->preparse == 0) {
/* copy descriptor fields */
bep->bLength = aep->desc.bLength;
bep->bDescriptorType = aep->desc.bDescriptorType;
bep->bEndpointAddress = aep->desc.bEndpointAddress;
bep->bmAttributes = aep->desc.bmAttributes;
bep->wMaxPacketSize = aep->desc.wMaxPacketSize;
bep->bInterval = aep->desc.bInterval;
bep->bRefresh = aep->desc.bRefresh;
bep->bSynchAddress = aep->desc.bSynchAddress;
}
ps->a.currextra = &aep->extra;
usb_parse_extra(ps, &bep->extra, &bep->extralen);
return;
}
static void
usb_parse_iface_sub(struct usb_parse_state *ps)
{
struct libusb20_interface *aifc;
struct usb_interface_descriptor *bifc;
uint8_t x;
aifc = ps->a.currifc;
bifc = ps->b.currifc++;
if (ps->preparse == 0) {
/* copy descriptor fields */
bifc->bLength = aifc->desc.bLength;
bifc->bDescriptorType = aifc->desc.bDescriptorType;
bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber;
bifc->bAlternateSetting = aifc->desc.bAlternateSetting;
bifc->bNumEndpoints = aifc->num_endpoints;
bifc->bInterfaceClass = aifc->desc.bInterfaceClass;
bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass;
bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol;
bifc->iInterface = aifc->desc.iInterface;
bifc->endpoint = ps->b.currep;
}
for (x = 0; x != aifc->num_endpoints; x++) {
ps->a.currep = aifc->endpoints + x;
usb_parse_endpoint(ps);
}
ps->a.currextra = &aifc->extra;
usb_parse_extra(ps, &bifc->extra, &bifc->extralen);
return;
}
static void
usb_parse_iface(struct usb_parse_state *ps)
{
struct libusb20_interface *aifc;
struct usb_interface *bifc;
uint8_t x;
aifc = ps->a.currifc;
bifc = ps->b.currifcw++;
if (ps->preparse == 0) {
/* initialise interface wrapper */
bifc->altsetting = ps->b.currifc;
bifc->num_altsetting = aifc->num_altsetting + 1;
}
usb_parse_iface_sub(ps);
for (x = 0; x != aifc->num_altsetting; x++) {
ps->a.currifc = aifc->altsetting + x;
usb_parse_iface_sub(ps);
}
return;
}
static void
usb_parse_config(struct usb_parse_state *ps)
{
struct libusb20_config *acfg;
struct usb_config_descriptor *bcfg;
uint8_t x;
acfg = ps->a.currcfg;
bcfg = ps->b.currcfg;
if (ps->preparse == 0) {
/* initialise config wrapper */
bcfg->bLength = acfg->desc.bLength;
bcfg->bDescriptorType = acfg->desc.bDescriptorType;
bcfg->wTotalLength = acfg->desc.wTotalLength;
bcfg->bNumInterfaces = acfg->num_interface;
bcfg->bConfigurationValue = acfg->desc.bConfigurationValue;
bcfg->iConfiguration = acfg->desc.iConfiguration;
bcfg->bmAttributes = acfg->desc.bmAttributes;
bcfg->MaxPower = acfg->desc.bMaxPower;
bcfg->interface = ps->b.currifcw;
}
for (x = 0; x != acfg->num_interface; x++) {
ps->a.currifc = acfg->interface + x;
usb_parse_iface(ps);
}
ps->a.currextra = &acfg->extra;
usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen);
return;
}
int
usb_parse_configuration(struct usb_config_descriptor *config,
uint8_t *buffer)
{
struct usb_parse_state ps;
uint8_t *ptr;
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
if ((buffer == NULL) || (config == NULL)) {
return (-1);
}
memset(&ps, 0, sizeof(ps));
ps.a.currcfg = libusb20_parse_config_desc(buffer);
ps.b.currcfg = config;
if (ps.a.currcfg == NULL) {
/* could not parse config or out of memory */
return (-1);
}
/* do the pre-parse */
ps.preparse = 1;
usb_parse_config(&ps);
a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0));
b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0));
c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0));
d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0));
/* allocate memory for our configuration */
ptr = malloc(a + b + c + d);
/* "currifcw" must be first, hence this pointer is freed */
ps.b.currifcw = (void *)(ptr);
ps.b.currifc = (void *)(ptr + a);
ps.b.currep = (void *)(ptr + a + b);
ps.b.currextra = (void *)(ptr + a + b + c);
/* generate a libusb v0.1 compatible structure */
ps.preparse = 0;
usb_parse_config(&ps);
/* free config structure */
free(ps.a.currcfg);
return (0); /* success */
}
void
usb_destroy_configuration(struct usb_device *dev)
{
uint8_t c;
if (dev->config == NULL) {
return;
}
for (c = 0; c != dev->descriptor.bNumConfigurations; c++) {
struct usb_config_descriptor *cf = &dev->config[c];
if (cf->interface != NULL) {
free(cf->interface);
cf->interface = NULL;
}
}
free(dev->config);
dev->config = NULL;
return;
}
void
usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
{
struct usb_device *dev;
struct libusb20_device *pdev;
uint8_t *ptr;
int error;
uint32_t size;
uint16_t len;
uint8_t x;
if (udev == NULL) {
/* be NULL safe */
return;
}
dev = usb_device(udev);
pdev = (void *)udev;
if (dev->descriptor.bNumConfigurations == 0) {
/* invalid device */
return;
}
size = dev->descriptor.bNumConfigurations *
sizeof(struct usb_config_descriptor);
dev->config = malloc(size);
if (dev->config == NULL) {
/* out of memory */
return;
}
memset(dev->config, 0, size);
for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {
error = (pdev->methods->get_config_desc_full) (
pdev, &ptr, &len, x);
if (error) {
usb_destroy_configuration(dev);
return;
}
usb_parse_configuration(dev->config + x, ptr);
/* free config buffer */
free(ptr);
}
return;
}
static int
usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size,
int timeout, int is_intr)
{
struct libusb20_transfer *xfer;
uint32_t temp;
uint32_t maxsize;
uint32_t actlen;
char *oldbytes;
xfer = usb_get_transfer_by_ep_no(dev, ep);
if (xfer == NULL)
return (-1);
if (libusb20_tr_pending(xfer)) {
/* there is already a transfer ongoing */
return (-1);
}
maxsize = libusb20_tr_get_max_total_length(xfer);
oldbytes = bytes;
/*
* We allow transferring zero bytes which is the same
* equivalent to a zero length USB packet.
*/
do {
temp = size;
if (temp > maxsize) {
/* find maximum possible length */
temp = maxsize;
}
if (is_intr)
libusb20_tr_setup_intr(xfer, bytes, temp, timeout);
else
libusb20_tr_setup_bulk(xfer, bytes, temp, timeout);
libusb20_tr_start(xfer);
while (1) {
if (libusb20_dev_process((void *)dev) != 0) {
/* device detached */
return (-1);
}
if (libusb20_tr_pending(xfer) == 0) {
/* transfer complete */
break;
}
/* wait for USB event from kernel */
libusb20_dev_wait_process((void *)dev, -1);
}
if (libusb20_tr_get_status(xfer)) {
/* transfer error */
return (-1);
}
actlen = libusb20_tr_get_actual_length(xfer);
bytes += actlen;
size -= actlen;
if (actlen != temp) {
/* short transfer */
break;
}
} while (size > 0);
return (bytes - oldbytes);
}
int
usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes,
int size, int timeout)
{
return (usb_std_io(dev, ep, bytes, size, timeout, 0));
}
int
usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes,
int size, int timeout)
{
return (usb_std_io(dev, ep, bytes, size, timeout, 0));
}
int
usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes,
int size, int timeout)
{
return (usb_std_io(dev, ep, bytes, size, timeout, 1));
}
int
usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes,
int size, int timeout)
{
return (usb_std_io(dev, ep, bytes, size, timeout, 1));
}
int
usb_control_msg(usb_dev_handle * dev, int requesttype, int request,
int value, int index, char *bytes, int size, int timeout)
{
struct LIBUSB20_CONTROL_SETUP_DECODED req;
int err;
uint16_t actlen;
LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
req.bmRequestType = requesttype;
req.bRequest = request;
req.wValue = value;
req.wIndex = index;
req.wLength = size;
err = libusb20_dev_request_sync((void *)dev, &req, bytes,
&actlen, timeout, 0);
if (err)
return (-1);
return (actlen);
}
int
usb_set_configuration(usb_dev_handle * dev, int configuration)
{
int err;
err = libusb20_dev_set_config_index((void *)dev, configuration);
if (err)
return (-1);
return (0);
}
int
usb_claim_interface(usb_dev_handle * dev, int interface)
{
int err;
err = libusb20_dev_claim_interface((void *)dev, interface);
if (err)
return (-1);
return (0);
}
int
usb_release_interface(usb_dev_handle * dev, int interface)
{
int err;
err = libusb20_dev_release_interface((void *)dev, interface);
if (err)
return (-1);
return (0);
}
int
usb_set_altinterface(usb_dev_handle * dev, int alternate)
{
int err;
uint8_t iface;
iface = usb_get_first_claimed_interface(dev);
err = libusb20_dev_set_alt_index((void *)dev, iface, alternate);
if (err)
return (-1);
return (0);
}
int
usb_resetep(usb_dev_handle * dev, unsigned int ep)
{
/* emulate an endpoint reset through clear-STALL */
return (usb_clear_halt(dev, ep));
}
int
usb_clear_halt(usb_dev_handle * dev, unsigned int ep)
{
struct libusb20_transfer *xfer;
xfer = usb_get_transfer_by_ep_no(dev, ep);
if (xfer == NULL)
return (-1);
libusb20_tr_clear_stall_sync(xfer);
return (0);
}
int
usb_reset(usb_dev_handle * dev)
{
int err;
err = libusb20_dev_reset((void *)dev);
if (err)
return (-1);
return (0);
}
char *
usb_strerror(void)
{
/* TODO */
return ("Unknown error");
}
void
usb_init(void)
{
/* nothing to do */
return;
}
void
usb_set_debug(int level)
{
/* use kernel UGEN debugging if you need to see what is going on */
return;
}
int
usb_find_busses(void)
{
usb_busses = &usb_global_bus;
return (0);
}
int
usb_find_devices(void)
{
struct libusb20_device *pdev;
struct usb_device *udev;
struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
struct libusb20_backend *pold;
int err;
/* cleanup after last device search */
pold = usb_backend;
pdev = NULL;
while ((pdev = libusb20_be_device_foreach(pold, pdev))) {
if (!pdev->is_opened) {
/*
* if the device has not been opened we free the
* device data
*/
udev = pdev->priv01Data;
libusb20_be_dequeue_device(pold, pdev);
libusb20_dev_free(pdev);
if (udev != NULL) {
LIST_DEL(usb_global_bus.devices, udev);
free(udev);
}
pdev = NULL; /* restart search */
}
}
/* do a new backend device search */
usb_backend = libusb20_be_alloc_default();
if (usb_backend == NULL) {
usb_backend = pold; /* restore */
return (-1);
}
/* iterate all devices */
pdev = NULL;
while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
udev = malloc(sizeof(*udev));
if (udev == NULL)
break;
memset(udev, 0, sizeof(*udev));
udev->bus = &usb_global_bus;
snprintf(udev->filename, sizeof(udev->filename),
"/dev/ugen%u.%u",
libusb20_dev_get_bus_number(pdev),
libusb20_dev_get_address(pdev));
ddesc = libusb20_dev_get_device_desc(pdev);
udev->descriptor.bLength = sizeof(udev->descriptor);
udev->descriptor.bDescriptorType = ddesc->bDescriptorType;
udev->descriptor.bcdUSB = ddesc->bcdUSB;
udev->descriptor.bDeviceClass = ddesc->bDeviceClass;
udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass;
udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol;
udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0;
udev->descriptor.idVendor = ddesc->idVendor;
udev->descriptor.idProduct = ddesc->idProduct;
udev->descriptor.bcdDevice = ddesc->bcdDevice;
udev->descriptor.iManufacturer = ddesc->iManufacturer;
udev->descriptor.iProduct = ddesc->iProduct;
udev->descriptor.iSerialNumber = ddesc->iSerialNumber;
udev->descriptor.bNumConfigurations =
ddesc->bNumConfigurations;
if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
/* truncate number of configurations */
udev->descriptor.bNumConfigurations = USB_MAXCONFIG;
}
/* link together the two structures */
udev->dev = pdev;
pdev->priv01Data = udev;
err = libusb20_dev_open(pdev, 0);
if (err == 0) {
/* XXX get all config descriptors by default */
usb_fetch_and_parse_descriptors((void *)pdev);
libusb20_dev_close(pdev);
}
LIST_ADD(usb_global_bus.devices, udev);
}
/* move old devices over to the new USB backend */
while ((pdev = libusb20_be_device_foreach(pold, pdev))) {
libusb20_be_dequeue_device(pold, pdev);
libusb20_be_enqueue_device(usb_backend, pdev);
}
/* free old backend, if any */
libusb20_be_free(pold);
return (0); /* success */
}
struct usb_device *
usb_device(usb_dev_handle * dev)
{
struct libusb20_device *pdev;
pdev = (void *)dev;
return (pdev->priv01Data);
}
struct usb_bus *
usb_get_busses(void)
{
return (usb_busses);
}