static void usb_host_auto_check(void *unused)

{

    struct USBHostDevice *s;

    struct USBAutoFilter *f;

    libusb_device **devs;

    struct libusb_device_descriptor ddesc;

    int unconnected = 0;

    int i, n;



    if (usb_host_init() != 0) {

        return;

    }



    if (runstate_is_running()) {

        n = libusb_get_device_list(ctx, &devs);

        for (i = 0; i < n; i++) {

            if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {

                continue;

            }

            if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {

                continue;

            }

            QTAILQ_FOREACH(s, &hostdevs, next) {

                f = &s->match;

                if (f->bus_num > 0 &&

                    f->bus_num != libusb_get_bus_number(devs[i])) {

                    continue;

                }

                if (f->addr > 0 &&

                    f->addr != libusb_get_device_address(devs[i])) {

                    continue;

                }

                if (f->port != NULL) {

                    char port[16] = "-";

                    usb_host_get_port(devs[i], port, sizeof(port));

                    if (strcmp(f->port, port) != 0) {

                        continue;

                    }

                }

                if (f->vendor_id > 0 &&

                    f->vendor_id != ddesc.idVendor) {

                    continue;

                }

                if (f->product_id > 0 &&

                    f->product_id != ddesc.idProduct) {

                    continue;

                }



                /* We got a match */

                s->seen++;

                if (s->errcount >= 3) {

                    continue;

                }

                if (s->dh != NULL) {

                    continue;

                }

                if (usb_host_open(s, devs[i]) < 0) {

                    s->errcount++;

                    continue;

                }

                break;

            }

        }

        libusb_free_device_list(devs, 1);



        QTAILQ_FOREACH(s, &hostdevs, next) {

            if (s->dh == NULL) {

                unconnected++;

            }

            if (s->seen == 0) {

                if (s->dh) {

                    usb_host_close(s);

                }

                s->errcount = 0;

            }

            s->seen = 0;

        }



#if 0

        if (unconnected == 0) {

            /* nothing to watch */

            if (usb_auto_timer) {

                timer_del(usb_auto_timer);

                trace_usb_host_auto_scan_disabled();

            }

            return;

        }

#endif

    }



    if (!usb_vmstate) {

        usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);

    }

    if (!usb_auto_timer) {

        usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL);

        if (!usb_auto_timer) {

            return;

        }

        trace_usb_host_auto_scan_enabled();

    }

    timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000);

}

static void usb_host_auto_check(void *unused)

{

    struct USBHostDevice *s;

    struct USBAutoFilter *f;

    libusb_device **devs;

    struct libusb_device_descriptor ddesc;

    int unconnected = 0;

    int i, n;



    if (usb_host_init() != 0) {

        return;

    }



    if (runstate_is_running()) {

        n = libusb_get_device_list(ctx, &devs);

        for (i = 0; i < n; i++) {

            if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {

                continue;

            }

            if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {

                continue;

            }

            QTAILQ_FOREACH(s, &hostdevs, next) {

                f = &s->match;

                if (f->bus_num > 0 &&

                    f->bus_num != libusb_get_bus_number(devs[i])) {

                    continue;

                }

                if (f->addr > 0 &&

                    f->addr != libusb_get_device_address(devs[i])) {

                    continue;

                }

                if (f->port != NULL) {

                    char port[16] = "-";

                    usb_host_get_port(devs[i], port, sizeof(port));

                    if (strcmp(f->port, port) != 0) {

                        continue;

                    }

                }

                if (f->vendor_id > 0 &&

                    f->vendor_id != ddesc.idVendor) {

                    continue;

                }

                if (f->product_id > 0 &&

                    f->product_id != ddesc.idProduct) {

                    continue;

                }



                

                s->seen++;

                if (s->errcount >= 3) {

                    continue;

                }

                if (s->dh != NULL) {

                    continue;

                }

                if (usb_host_open(s, devs[i]) < 0) {

                    s->errcount++;

                    continue;

                }

                break;

            }

        }

        libusb_free_device_list(devs, 1);



        QTAILQ_FOREACH(s, &hostdevs, next) {

            if (s->dh == NULL) {

                unconnected++;

            }

            if (s->seen == 0) {

                if (s->dh) {

                    usb_host_close(s);

                }

                s->errcount = 0;

            }

            s->seen = 0;

        }



#if 0

        if (unconnected == 0) {

            

            if (usb_auto_timer) {

                timer_del(usb_auto_timer);

                trace_usb_host_auto_scan_disabled();

            }

            return;

        }

#endif

    }



    if (!usb_vmstate) {

        usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);

    }

    if (!usb_auto_timer) {

        usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL);

        if (!usb_auto_timer) {

            return;

        }

        trace_usb_host_auto_scan_enabled();

    }

    timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000);

}

    libusb_device **devs;     libusb_device **devs;
static void FUNC_0(void *VAR_0)

{

    struct USBHostDevice *VAR_1;

    struct USBAutoFilter *VAR_2;

    libusb_device **devs;

    struct libusb_device_descriptor VAR_3;

    int VAR_4 = 0;

    int VAR_5, VAR_6;



    if (usb_host_init() != 0) {

        return;

    }



    if (runstate_is_running()) {

        VAR_6 = libusb_get_device_list(ctx, &devs);

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

            if (libusb_get_device_descriptor(devs[VAR_5], &VAR_3) != 0) {

                continue;

            }

            if (VAR_3.bDeviceClass == LIBUSB_CLASS_HUB) {

                continue;

            }

            QTAILQ_FOREACH(VAR_1, &hostdevs, next) {

                VAR_2 = &VAR_1->match;

                if (VAR_2->bus_num > 0 &&

                    VAR_2->bus_num != libusb_get_bus_number(devs[VAR_5])) {

                    continue;

                }

                if (VAR_2->addr > 0 &&

                    VAR_2->addr != libusb_get_device_address(devs[VAR_5])) {

                    continue;

                }

                if (VAR_2->port != NULL) {

                    char port[16] = "-";

                    usb_host_get_port(devs[VAR_5], port, sizeof(port));

                    if (strcmp(VAR_2->port, port) != 0) {

                        continue;

                    }

                }

                if (VAR_2->vendor_id > 0 &&

                    VAR_2->vendor_id != VAR_3.idVendor) {

                    continue;

                }

                if (VAR_2->product_id > 0 &&

                    VAR_2->product_id != VAR_3.idProduct) {

                    continue;

                }



                

                VAR_1->seen++;

                if (VAR_1->errcount >= 3) {

                    continue;

                }

                if (VAR_1->dh != NULL) {

                    continue;

                }

                if (usb_host_open(VAR_1, devs[VAR_5]) < 0) {

                    VAR_1->errcount++;

                    continue;

                }

                break;

            }

        }

        libusb_free_device_list(devs, 1);



        QTAILQ_FOREACH(VAR_1, &hostdevs, next) {

            if (VAR_1->dh == NULL) {

                VAR_4++;

            }

            if (VAR_1->seen == 0) {

                if (VAR_1->dh) {

                    usb_host_close(VAR_1);

                }

                VAR_1->errcount = 0;

            }

            VAR_1->seen = 0;

        }



#if 0

        if (VAR_4 == 0) {

            

            if (usb_auto_timer) {

                timer_del(usb_auto_timer);

                trace_usb_host_auto_scan_disabled();

            }

            return;

        }

#endif

    }



    if (!usb_vmstate) {

        usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);

    }

    if (!usb_auto_timer) {

        usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, FUNC_0, NULL);

        if (!usb_auto_timer) {

            return;

        }

        trace_usb_host_auto_scan_enabled();

    }

    timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000);

}

static void FUNC_0(void *VAR_0)
{ struct USBHostDevice *VAR_1; struct USBAutoFilter *VAR_2; libusb_device **devs; struct libusb_device_descriptor VAR_3; int VAR_4 = 0; int VAR_5, VAR_6; if (usb_host_init() != 0) { return; } if (runstate_is_running()) { VAR_6 = libusb_get_device_list(ctx, &devs); for (VAR_5 = 0; VAR_5 < VAR_6; VAR_5++) { if (libusb_get_device_descriptor(devs[VAR_5], &VAR_3) != 0) { continue; } if (VAR_3.bDeviceClass == LIBUSB_CLASS_HUB) { continue; } QTAILQ_FOREACH(VAR_1, &hostdevs, next) { VAR_2 = &VAR_1->match; if (VAR_2->bus_num > 0 &&
VAR_2->bus_num != libusb_get_bus_number(devs[VAR_5])) { continue; } if (VAR_2->addr > 0 &&
VAR_2->addr != libusb_get_device_address(devs[VAR_5])) { continue; } if (VAR_2->port != NULL) { char port[16] = "-"; usb_host_get_port(devs[VAR_5], port, sizeof(port)); if (strcmp(VAR_2->port, port) != 0) { continue; } } if (VAR_2->vendor_id > 0 &&
VAR_2->vendor_id != VAR_3.idVendor) { continue; } if (VAR_2->product_id > 0 &&
VAR_2->product_id != VAR_3.idProduct) { continue; } VAR_1->seen++; if (VAR_1->errcount >= 3) { continue; } if (VAR_1->dh != NULL) { continue; } if (usb_host_open(VAR_1, devs[VAR_5]) < 0) { VAR_1->errcount++; continue; } break; } } libusb_free_device_list(devs, 1); QTAILQ_FOREACH(VAR_1, &hostdevs, next) { if (VAR_1->dh == NULL) { VAR_4++; } if (VAR_1->seen == 0) { if (VAR_1->dh) { usb_host_close(VAR_1); } VAR_1->errcount = 0; } VAR_1->seen = 0; } #if 0
if (VAR_4 == 0) { if (usb_auto_timer) { timer_del(usb_auto_timer); trace_usb_host_auto_scan_disabled(); } return; } #endif
} if (!usb_vmstate) { usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL); } if (!usb_auto_timer) { usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, FUNC_0, NULL); if (!usb_auto_timer) { return; } trace_usb_host_auto_scan_enabled(); } timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000); }