static int usb_host_open(USBHostDevice *dev, int bus_num,

                         int addr, const char *port,

                         const char *prod_name, int speed)

{

    int fd = -1, ret;



    trace_usb_host_open_started(bus_num, addr);



    if (dev->fd != -1) {

        goto fail;

    }



    fd = usb_host_open_device(bus_num, addr);

    if (fd < 0) {

        goto fail;

    }

    DPRINTF("husb: opened %s\n", buf);



    dev->bus_num = bus_num;

    dev->addr = addr;

    strcpy(dev->port, port);

    dev->fd = fd;



    /* read the device description */

    dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));

    if (dev->descr_len <= 0) {

        perror("husb: reading device data failed");

        goto fail;

    }



#ifdef DEBUG

    {

        int x;

        printf("=== begin dumping device descriptor data ===\n");

        for (x = 0; x < dev->descr_len; x++) {

            printf("%02x ", dev->descr[x]);

        }

        printf("\n=== end dumping device descriptor data ===\n");

    }

#endif





    /* start unconfigured -- we'll wait for the guest to set a configuration */

    if (!usb_host_claim_interfaces(dev, 0)) {

        goto fail;

    }



    usb_ep_init(&dev->dev);

    usb_linux_update_endp_table(dev);



    if (speed == -1) {

        struct usbdevfs_connectinfo ci;



        ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);

        if (ret < 0) {

            perror("usb_host_device_open: USBDEVFS_CONNECTINFO");

            goto fail;

        }



        if (ci.slow) {

            speed = USB_SPEED_LOW;

        } else {

            speed = USB_SPEED_HIGH;

        }

    }

    dev->dev.speed = speed;

    dev->dev.speedmask = (1 << speed);

    if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {

        dev->dev.speedmask |= USB_SPEED_MASK_FULL;

    }



    trace_usb_host_open_success(bus_num, addr);



    if (!prod_name || prod_name[0] == '\0') {

        snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),

                 "host:%d.%d", bus_num, addr);

    } else {

        pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),

                prod_name);

    }



    ret = usb_device_attach(&dev->dev);

    if (ret) {

        goto fail;

    }



    /* USB devio uses 'write' flag to check for async completions */

    qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);



    return 0;



fail:

    trace_usb_host_open_failure(bus_num, addr);

    if (dev->fd != -1) {

        close(dev->fd);

        dev->fd = -1;

    }

    return -1;

}

static int usb_host_open(USBHostDevice *dev, int bus_num,

                         int addr, const char *port,

                         const char *prod_name, int speed)

{

    int fd = -1, ret;



    trace_usb_host_open_started(bus_num, addr);



    if (dev->fd != -1) {

        goto fail;

    }



    fd = usb_host_open_device(bus_num, addr);

    if (fd < 0) {

        goto fail;

    }

    DPRINTF("husb: opened %s\n", buf);



    dev->bus_num = bus_num;

    dev->addr = addr;

    strcpy(dev->port, port);

    dev->fd = fd;



    

    dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));

    if (dev->descr_len <= 0) {

        perror("husb: reading device data failed");

        goto fail;

    }



#ifdef DEBUG

    {

        int x;

        printf("=== begin dumping device descriptor data ===\n");

        for (x = 0; x < dev->descr_len; x++) {

            printf("%02x ", dev->descr[x]);

        }

        printf("\n=== end dumping device descriptor data ===\n");

    }

#endif





    

    if (!usb_host_claim_interfaces(dev, 0)) {

        goto fail;

    }



    usb_ep_init(&dev->dev);

    usb_linux_update_endp_table(dev);



    if (speed == -1) {

        struct usbdevfs_connectinfo ci;



        ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);

        if (ret < 0) {

            perror("usb_host_device_open: USBDEVFS_CONNECTINFO");

            goto fail;

        }



        if (ci.slow) {

            speed = USB_SPEED_LOW;

        } else {

            speed = USB_SPEED_HIGH;

        }

    }

    dev->dev.speed = speed;

    dev->dev.speedmask = (1 << speed);

    if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {

        dev->dev.speedmask |= USB_SPEED_MASK_FULL;

    }



    trace_usb_host_open_success(bus_num, addr);



    if (!prod_name || prod_name[0] == '\0') {

        snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),

                 "host:%d.%d", bus_num, addr);

    } else {

        pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),

                prod_name);

    }



    ret = usb_device_attach(&dev->dev);

    if (ret) {

        goto fail;

    }



    

    qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);



    return 0;



fail:

    trace_usb_host_open_failure(bus_num, addr);

    if (dev->fd != -1) {

        close(dev->fd);

        dev->fd = -1;

    }

    return -1;

}

    strcpy(dev->port, port);
static int FUNC_0(USBHostDevice *VAR_0, int VAR_1,

                         int VAR_2, const char *VAR_3,

                         const char *VAR_4, int VAR_5)

{

    int VAR_6 = -1, VAR_7;



    trace_usb_host_open_started(VAR_1, VAR_2);



    if (VAR_0->VAR_6 != -1) {

        goto fail;

    }



    VAR_6 = usb_host_open_device(VAR_1, VAR_2);

    if (VAR_6 < 0) {

        goto fail;

    }

    DPRINTF("husb: opened %s\n", buf);



    VAR_0->VAR_1 = VAR_1;

    VAR_0->VAR_2 = VAR_2;

    strcpy(VAR_0->VAR_3, VAR_3);

    VAR_0->VAR_6 = VAR_6;



    

    VAR_0->descr_len = read(VAR_6, VAR_0->descr, sizeof(VAR_0->descr));

    if (VAR_0->descr_len <= 0) {

        perror("husb: reading device data failed");

        goto fail;

    }



#ifdef DEBUG

    {

        int x;

        printf("=== begin dumping device descriptor data ===\n");

        for (x = 0; x < VAR_0->descr_len; x++) {

            printf("%02x ", VAR_0->descr[x]);

        }

        printf("\n=== end dumping device descriptor data ===\n");

    }

#endif





    

    if (!usb_host_claim_interfaces(VAR_0, 0)) {

        goto fail;

    }



    usb_ep_init(&VAR_0->VAR_0);

    usb_linux_update_endp_table(VAR_0);



    if (VAR_5 == -1) {

        struct usbdevfs_connectinfo VAR_8;



        VAR_7 = ioctl(VAR_6, USBDEVFS_CONNECTINFO, &VAR_8);

        if (VAR_7 < 0) {

            perror("usb_host_device_open: USBDEVFS_CONNECTINFO");

            goto fail;

        }



        if (VAR_8.slow) {

            VAR_5 = USB_SPEED_LOW;

        } else {

            VAR_5 = USB_SPEED_HIGH;

        }

    }

    VAR_0->VAR_0.VAR_5 = VAR_5;

    VAR_0->VAR_0.speedmask = (1 << VAR_5);

    if (VAR_0->VAR_0.VAR_5 == USB_SPEED_HIGH && usb_linux_full_speed_compat(VAR_0)) {

        VAR_0->VAR_0.speedmask |= USB_SPEED_MASK_FULL;

    }



    trace_usb_host_open_success(VAR_1, VAR_2);



    if (!VAR_4 || VAR_4[0] == '\0') {

        snprintf(VAR_0->VAR_0.product_desc, sizeof(VAR_0->VAR_0.product_desc),

                 "host:%d.%d", VAR_1, VAR_2);

    } else {

        pstrcpy(VAR_0->VAR_0.product_desc, sizeof(VAR_0->VAR_0.product_desc),

                VAR_4);

    }



    VAR_7 = usb_device_attach(&VAR_0->VAR_0);

    if (VAR_7) {

        goto fail;

    }



    

    qemu_set_fd_handler(VAR_0->VAR_6, NULL, async_complete, VAR_0);



    return 0;



fail:

    trace_usb_host_open_failure(VAR_1, VAR_2);

    if (VAR_0->VAR_6 != -1) {

        close(VAR_0->VAR_6);

        VAR_0->VAR_6 = -1;

    }

    return -1;

}

static int FUNC_0(USBHostDevice *VAR_0, int VAR_1,
int VAR_2, const char *VAR_3,
const char *VAR_4, int VAR_5)
{ int VAR_6 = -1, VAR_7; trace_usb_host_open_started(VAR_1, VAR_2); if (VAR_0->VAR_6 != -1) { goto fail; } VAR_6 = usb_host_open_device(VAR_1, VAR_2); if (VAR_6 < 0) { goto fail; } DPRINTF("husb: opened %s\n", buf); VAR_0->VAR_1 = VAR_1; VAR_0->VAR_2 = VAR_2; strcpy(VAR_0->VAR_3, VAR_3); VAR_0->VAR_6 = VAR_6; VAR_0->descr_len = read(VAR_6, VAR_0->descr, sizeof(VAR_0->descr)); if (VAR_0->descr_len <= 0) { perror("husb: reading device data failed"); goto fail; } #ifdef DEBUG
{ int x; printf("=== begin dumping device descriptor data ===\n"); for (x = 0; x < VAR_0->descr_len; x++) { printf("%02x ", VAR_0->descr[x]); } printf("\n=== end dumping device descriptor data ===\n"); } #endif
if (!usb_host_claim_interfaces(VAR_0, 0)) { goto fail; } usb_ep_init(&VAR_0->VAR_0); usb_linux_update_endp_table(VAR_0); if (VAR_5 == -1) { struct usbdevfs_connectinfo VAR_8; VAR_7 = ioctl(VAR_6, USBDEVFS_CONNECTINFO, &VAR_8); if (VAR_7 < 0) { perror("usb_host_device_open: USBDEVFS_CONNECTINFO"); goto fail; } if (VAR_8.slow) { VAR_5 = USB_SPEED_LOW; } else { VAR_5 = USB_SPEED_HIGH; } } VAR_0->VAR_0.VAR_5 = VAR_5; VAR_0->VAR_0.speedmask = (1 << VAR_5); if (VAR_0->VAR_0.VAR_5 == USB_SPEED_HIGH && usb_linux_full_speed_compat(VAR_0)) { VAR_0->VAR_0.speedmask |= USB_SPEED_MASK_FULL; } trace_usb_host_open_success(VAR_1, VAR_2); if (!VAR_4 || VAR_4[0] == '\0') { snprintf(VAR_0->VAR_0.product_desc, sizeof(VAR_0->VAR_0.product_desc),
"host:%d.%d", VAR_1, VAR_2); } else { pstrcpy(VAR_0->VAR_0.product_desc, sizeof(VAR_0->VAR_0.product_desc),
VAR_4); } VAR_7 = usb_device_attach(&VAR_0->VAR_0); if (VAR_7) { goto fail; } qemu_set_fd_handler(VAR_0->VAR_6, NULL, async_complete, VAR_0); return 0; fail:
trace_usb_host_open_failure(VAR_1, VAR_2); if (VAR_0->VAR_6 != -1) { close(VAR_0->VAR_6); VAR_0->VAR_6 = -1; } return -1; }