static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)

{

    int dev_descr_len, config_descr_len;

    int interface, nb_interfaces;

    int ret, i;



    if (configuration == 0) /* address state - ignore */

        return 1;



    DPRINTF("husb: claiming interfaces. config %d\n", configuration);



    i = 0;

    dev_descr_len = dev->descr[0];

    if (dev_descr_len > dev->descr_len) {

        goto fail;

    }



    i += dev_descr_len;

    while (i < dev->descr_len) {

        DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",

                i, dev->descr_len,

               dev->descr[i], dev->descr[i+1]);



        if (dev->descr[i+1] != USB_DT_CONFIG) {

            i += dev->descr[i];

            continue;

        }

        config_descr_len = dev->descr[i];



        printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);



        if (configuration < 0 || configuration == dev->descr[i + 5]) {

            configuration = dev->descr[i + 5];

            break;

        }



        i += config_descr_len;

    }



    if (i >= dev->descr_len) {

        fprintf(stderr,

                "husb: update iface failed. no matching configuration\n");

        goto fail;

    }

    nb_interfaces = dev->descr[i + 4];



#ifdef USBDEVFS_DISCONNECT

    /* earlier Linux 2.4 do not support that */

    {

        struct usbdevfs_ioctl ctrl;

        for (interface = 0; interface < nb_interfaces; interface++) {

            ctrl.ioctl_code = USBDEVFS_DISCONNECT;

            ctrl.ifno = interface;

            ctrl.data = 0;

            ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);

            if (ret < 0 && errno != ENODATA) {

                perror("USBDEVFS_DISCONNECT");

                goto fail;

            }

        }

    }

#endif



    /* XXX: only grab if all interfaces are free */

    for (interface = 0; interface < nb_interfaces; interface++) {

        ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);

        if (ret < 0) {

            if (errno == EBUSY) {

                printf("husb: update iface. device already grabbed\n");

            } else {

                perror("husb: failed to claim interface");

            }

        fail:

            return 0;

        }

    }



    printf("husb: %d interfaces claimed for configuration %d\n",

           nb_interfaces, configuration);



    dev->ninterfaces   = nb_interfaces;

    dev->configuration = configuration;

    return 1;

}

static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)

{

    int dev_descr_len, config_descr_len;

    int interface, nb_interfaces;

    int ret, i;



    if (configuration == 0) 

        return 1;



    DPRINTF("husb: claiming interfaces. config %d\n", configuration);



    i = 0;

    dev_descr_len = dev->descr[0];

    if (dev_descr_len > dev->descr_len) {

        goto fail;

    }



    i += dev_descr_len;

    while (i < dev->descr_len) {

        DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",

                i, dev->descr_len,

               dev->descr[i], dev->descr[i+1]);



        if (dev->descr[i+1] != USB_DT_CONFIG) {

            i += dev->descr[i];

            continue;

        }

        config_descr_len = dev->descr[i];



        printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);



        if (configuration < 0 || configuration == dev->descr[i + 5]) {

            configuration = dev->descr[i + 5];

            break;

        }



        i += config_descr_len;

    }



    if (i >= dev->descr_len) {

        fprintf(stderr,

                "husb: update iface failed. no matching configuration\n");

        goto fail;

    }

    nb_interfaces = dev->descr[i + 4];



#ifdef USBDEVFS_DISCONNECT

    

    {

        struct usbdevfs_ioctl ctrl;

        for (interface = 0; interface < nb_interfaces; interface++) {

            ctrl.ioctl_code = USBDEVFS_DISCONNECT;

            ctrl.ifno = interface;

            ctrl.data = 0;

            ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);

            if (ret < 0 && errno != ENODATA) {

                perror("USBDEVFS_DISCONNECT");

                goto fail;

            }

        }

    }

#endif



    

    for (interface = 0; interface < nb_interfaces; interface++) {

        ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);

        if (ret < 0) {

            if (errno == EBUSY) {

                printf("husb: update iface. device already grabbed\n");

            } else {

                perror("husb: failed to claim interface");

            }

        fail:

            return 0;

        }

    }



    printf("husb: %d interfaces claimed for configuration %d\n",

           nb_interfaces, configuration);



    dev->ninterfaces   = nb_interfaces;

    dev->configuration = configuration;

    return 1;

}

static int FUNC_0(USBHostDevice *VAR_0, int VAR_1)

{

    int VAR_2, VAR_3;

    int VAR_4, VAR_5;

    int VAR_6, VAR_7;



    if (VAR_1 == 0) 

        return 1;



    DPRINTF("husb: claiming interfaces. config %d\n", VAR_1);



    VAR_7 = 0;

    VAR_2 = VAR_0->descr[0];

    if (VAR_2 > VAR_0->descr_len) {

        goto fail;

    }



    VAR_7 += VAR_2;

    while (VAR_7 < VAR_0->descr_len) {

        DPRINTF("husb: VAR_7 is %d, descr_len is %d, dl %d, dt %d\n",

                VAR_7, VAR_0->descr_len,

               VAR_0->descr[VAR_7], VAR_0->descr[VAR_7+1]);



        if (VAR_0->descr[VAR_7+1] != USB_DT_CONFIG) {

            VAR_7 += VAR_0->descr[VAR_7];

            continue;

        }

        VAR_3 = VAR_0->descr[VAR_7];



        printf("husb: config #%d need %d\n", VAR_0->descr[VAR_7 + 5], VAR_1);



        if (VAR_1 < 0 || VAR_1 == VAR_0->descr[VAR_7 + 5]) {

            VAR_1 = VAR_0->descr[VAR_7 + 5];

            break;

        }



        VAR_7 += VAR_3;

    }



    if (VAR_7 >= VAR_0->descr_len) {

        fprintf(stderr,

                "husb: update iface failed. no matching VAR_1\n");

        goto fail;

    }

    VAR_5 = VAR_0->descr[VAR_7 + 4];



#ifdef USBDEVFS_DISCONNECT

    

    {

        struct usbdevfs_ioctl ctrl;

        for (VAR_4 = 0; VAR_4 < VAR_5; VAR_4++) {

            ctrl.ioctl_code = USBDEVFS_DISCONNECT;

            ctrl.ifno = VAR_4;

            ctrl.data = 0;

            VAR_6 = ioctl(VAR_0->fd, USBDEVFS_IOCTL, &ctrl);

            if (VAR_6 < 0 && errno != ENODATA) {

                perror("USBDEVFS_DISCONNECT");

                goto fail;

            }

        }

    }

#endif



    

    for (VAR_4 = 0; VAR_4 < VAR_5; VAR_4++) {

        VAR_6 = ioctl(VAR_0->fd, USBDEVFS_CLAIMINTERFACE, &VAR_4);

        if (VAR_6 < 0) {

            if (errno == EBUSY) {

                printf("husb: update iface. device already grabbed\n");

            } else {

                perror("husb: failed to claim VAR_4");

            }

        fail:

            return 0;

        }

    }



    printf("husb: %d interfaces claimed for VAR_1 %d\n",

           VAR_5, VAR_1);



    VAR_0->ninterfaces   = VAR_5;

    VAR_0->VAR_1 = VAR_1;

    return 1;

}

static int FUNC_0(USBHostDevice *VAR_0, int VAR_1)
{ int VAR_2, VAR_3; int VAR_4, VAR_5; int VAR_6, VAR_7; if (VAR_1 == 0)
return 1; DPRINTF("husb: claiming interfaces. config %d\n", VAR_1); VAR_7 = 0; VAR_2 = VAR_0->descr[0]; if (VAR_2 > VAR_0->descr_len) { goto fail; } VAR_7 += VAR_2; while (VAR_7 < VAR_0->descr_len) { DPRINTF("husb: VAR_7 is %d, descr_len is %d, dl %d, dt %d\n",
VAR_7, VAR_0->descr_len,
VAR_0->descr[VAR_7], VAR_0->descr[VAR_7+1]); if (VAR_0->descr[VAR_7+1] != USB_DT_CONFIG) { VAR_7 += VAR_0->descr[VAR_7]; continue; } VAR_3 = VAR_0->descr[VAR_7]; printf("husb: config #%d need %d\n", VAR_0->descr[VAR_7 + 5], VAR_1); if (VAR_1 < 0 || VAR_1 == VAR_0->descr[VAR_7 + 5]) { VAR_1 = VAR_0->descr[VAR_7 + 5]; break; } VAR_7 += VAR_3; } if (VAR_7 >= VAR_0->descr_len) { fprintf(stderr,
"husb: update iface failed. no matching VAR_1\n"); goto fail; } VAR_5 = VAR_0->descr[VAR_7 + 4]; #ifdef USBDEVFS_DISCONNECT
{ struct usbdevfs_ioctl ctrl; for (VAR_4 = 0; VAR_4 < VAR_5; VAR_4++) { ctrl.ioctl_code = USBDEVFS_DISCONNECT; ctrl.ifno = VAR_4; ctrl.data = 0; VAR_6 = ioctl(VAR_0->fd, USBDEVFS_IOCTL, &ctrl); if (VAR_6 < 0 && errno != ENODATA) { perror("USBDEVFS_DISCONNECT"); goto fail; } } } #endif
for (VAR_4 = 0; VAR_4 < VAR_5; VAR_4++) { VAR_6 = ioctl(VAR_0->fd, USBDEVFS_CLAIMINTERFACE, &VAR_4); if (VAR_6 < 0) { if (errno == EBUSY) { printf("husb: update iface. device already grabbed\n"); } else { perror("husb: failed to claim VAR_4"); } fail:
return 0; } } printf("husb: %d interfaces claimed for VAR_1 %d\n",
VAR_5, VAR_1); VAR_0->ninterfaces   = VAR_5; VAR_0->VAR_1 = VAR_1; return 1; }