static int usb_net_handle_control(USBDevice *dev, int request, int value,

                int index, int length, uint8_t *data)

{

    USBNetState *s = (USBNetState *) dev;

    int ret = 0;



    switch(request) {

    case DeviceRequest | USB_REQ_GET_STATUS:

        data[0] = (1 << USB_DEVICE_SELF_POWERED) |

                (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);

        data[1] = 0x00;

        ret = 2;

        break;



    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:

        if (value == USB_DEVICE_REMOTE_WAKEUP) {

            dev->remote_wakeup = 0;

        } else {

            goto fail;

        }

        ret = 0;

        break;



    case DeviceOutRequest | USB_REQ_SET_FEATURE:

        if (value == USB_DEVICE_REMOTE_WAKEUP) {

            dev->remote_wakeup = 1;

        } else {

            goto fail;

        }

        ret = 0;

        break;



    case DeviceOutRequest | USB_REQ_SET_ADDRESS:

        dev->addr = value;

        ret = 0;

        break;



    case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:

        if (!s->rndis || value || index != 0)

            goto fail;

#ifdef TRAFFIC_DEBUG

        {

            unsigned int i;

            fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");

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

                if (!(i & 15))

                    fprintf(stderr, "\n%04x:", i);

                fprintf(stderr, " %02x", data[i]);

            }

            fprintf(stderr, "\n\n");

        }

#endif

        ret = rndis_parse(s, data, length);

        break;



    case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:

        if (!s->rndis || value || index != 0)

            goto fail;

        ret = rndis_get_response(s, data);

        if (!ret) {

            data[0] = 0;

            ret = 1;

        }

#ifdef TRAFFIC_DEBUG

        {

            unsigned int i;

            fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");

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

                if (!(i & 15))

                    fprintf(stderr, "\n%04x:", i);

                fprintf(stderr, " %02x", data[i]);

            }

            fprintf(stderr, "\n\n");

        }

#endif

        break;



    case DeviceRequest | USB_REQ_GET_DESCRIPTOR:

        switch(value >> 8) {

        case USB_DT_DEVICE:

            ret = sizeof(qemu_net_dev_descriptor);

            memcpy(data, qemu_net_dev_descriptor, ret);

            break;



        case USB_DT_CONFIG:

            switch (value & 0xff) {

            case 0:

                ret = sizeof(qemu_net_rndis_config_descriptor);

                memcpy(data, qemu_net_rndis_config_descriptor, ret);

                break;



            case 1:

                ret = sizeof(qemu_net_cdc_config_descriptor);

                memcpy(data, qemu_net_cdc_config_descriptor, ret);

                break;



            default:

                goto fail;

            }



            data[2] = ret & 0xff;

            data[3] = ret >> 8;

            break;



        case USB_DT_STRING:

            switch (value & 0xff) {

            case 0:

                /* language ids */

                data[0] = 4;

                data[1] = 3;

                data[2] = 0x09;

                data[3] = 0x04;

                ret = 4;

                break;



            case STRING_ETHADDR:

                ret = set_usb_string(data, s->usbstring_mac);

                break;



            default:

                if (usb_net_stringtable[value & 0xff]) {

                    ret = set_usb_string(data,

                                    usb_net_stringtable[value & 0xff]);

                    break;

                }



                goto fail;

            }

            break;



        default:

            goto fail;

        }

        break;



    case DeviceRequest | USB_REQ_GET_CONFIGURATION:

        data[0] = s->rndis ? DEV_RNDIS_CONFIG_VALUE : DEV_CONFIG_VALUE;

        ret = 1;

        break;



    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:

        switch (value & 0xff) {

        case DEV_CONFIG_VALUE:

            s->rndis = 0;

            break;



        case DEV_RNDIS_CONFIG_VALUE:

            s->rndis = 1;

            break;



        default:

            goto fail;

        }

        ret = 0;

        break;



    case DeviceRequest | USB_REQ_GET_INTERFACE:

    case InterfaceRequest | USB_REQ_GET_INTERFACE:

        data[0] = 0;

        ret = 1;

        break;



    case DeviceOutRequest | USB_REQ_SET_INTERFACE:

    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:

        ret = 0;

        break;



    default:

    fail:

        fprintf(stderr, "usbnet: failed control transaction: "

                        "request 0x%x value 0x%x index 0x%x length 0x%x\n",

                        request, value, index, length);

        ret = USB_RET_STALL;

        break;

    }

    return ret;

}

static int usb_net_handle_control(USBDevice *dev, int request, int value,

                int index, int length, uint8_t *data)

{

    USBNetState *s = (USBNetState *) dev;

    int ret = 0;



    switch(request) {

    case DeviceRequest | USB_REQ_GET_STATUS:

        data[0] = (1 << USB_DEVICE_SELF_POWERED) |

                (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);

        data[1] = 0x00;

        ret = 2;

        break;



    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:

        if (value == USB_DEVICE_REMOTE_WAKEUP) {

            dev->remote_wakeup = 0;

        } else {

            goto fail;

        }

        ret = 0;

        break;



    case DeviceOutRequest | USB_REQ_SET_FEATURE:

        if (value == USB_DEVICE_REMOTE_WAKEUP) {

            dev->remote_wakeup = 1;

        } else {

            goto fail;

        }

        ret = 0;

        break;



    case DeviceOutRequest | USB_REQ_SET_ADDRESS:

        dev->addr = value;

        ret = 0;

        break;



    case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:

        if (!s->rndis || value || index != 0)

            goto fail;

#ifdef TRAFFIC_DEBUG

        {

            unsigned int i;

            fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");

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

                if (!(i & 15))

                    fprintf(stderr, "\n%04x:", i);

                fprintf(stderr, " %02x", data[i]);

            }

            fprintf(stderr, "\n\n");

        }

#endif

        ret = rndis_parse(s, data, length);

        break;



    case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:

        if (!s->rndis || value || index != 0)

            goto fail;

        ret = rndis_get_response(s, data);

        if (!ret) {

            data[0] = 0;

            ret = 1;

        }

#ifdef TRAFFIC_DEBUG

        {

            unsigned int i;

            fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");

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

                if (!(i & 15))

                    fprintf(stderr, "\n%04x:", i);

                fprintf(stderr, " %02x", data[i]);

            }

            fprintf(stderr, "\n\n");

        }

#endif

        break;



    case DeviceRequest | USB_REQ_GET_DESCRIPTOR:

        switch(value >> 8) {

        case USB_DT_DEVICE:

            ret = sizeof(qemu_net_dev_descriptor);

            memcpy(data, qemu_net_dev_descriptor, ret);

            break;



        case USB_DT_CONFIG:

            switch (value & 0xff) {

            case 0:

                ret = sizeof(qemu_net_rndis_config_descriptor);

                memcpy(data, qemu_net_rndis_config_descriptor, ret);

                break;



            case 1:

                ret = sizeof(qemu_net_cdc_config_descriptor);

                memcpy(data, qemu_net_cdc_config_descriptor, ret);

                break;



            default:

                goto fail;

            }



            data[2] = ret & 0xff;

            data[3] = ret >> 8;

            break;



        case USB_DT_STRING:

            switch (value & 0xff) {

            case 0:

                

                data[0] = 4;

                data[1] = 3;

                data[2] = 0x09;

                data[3] = 0x04;

                ret = 4;

                break;



            case STRING_ETHADDR:

                ret = set_usb_string(data, s->usbstring_mac);

                break;



            default:

                if (usb_net_stringtable[value & 0xff]) {

                    ret = set_usb_string(data,

                                    usb_net_stringtable[value & 0xff]);

                    break;

                }



                goto fail;

            }

            break;



        default:

            goto fail;

        }

        break;



    case DeviceRequest | USB_REQ_GET_CONFIGURATION:

        data[0] = s->rndis ? DEV_RNDIS_CONFIG_VALUE : DEV_CONFIG_VALUE;

        ret = 1;

        break;



    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:

        switch (value & 0xff) {

        case DEV_CONFIG_VALUE:

            s->rndis = 0;

            break;



        case DEV_RNDIS_CONFIG_VALUE:

            s->rndis = 1;

            break;



        default:

            goto fail;

        }

        ret = 0;

        break;



    case DeviceRequest | USB_REQ_GET_INTERFACE:

    case InterfaceRequest | USB_REQ_GET_INTERFACE:

        data[0] = 0;

        ret = 1;

        break;



    case DeviceOutRequest | USB_REQ_SET_INTERFACE:

    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:

        ret = 0;

        break;



    default:

    fail:

        fprintf(stderr, "usbnet: failed control transaction: "

                        "request 0x%x value 0x%x index 0x%x length 0x%x\n",

                        request, value, index, length);

        ret = USB_RET_STALL;

        break;

    }

    return ret;

}

static int FUNC_0(USBDevice *VAR_0, int VAR_1, int VAR_2,

                int VAR_3, int VAR_4, uint8_t *VAR_5)

{

    USBNetState *s = (USBNetState *) VAR_0;

    int VAR_6 = 0;



    switch(VAR_1) {

    case DeviceRequest | USB_REQ_GET_STATUS:

        VAR_5[0] = (1 << USB_DEVICE_SELF_POWERED) |

                (VAR_0->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);

        VAR_5[1] = 0x00;

        VAR_6 = 2;

        break;



    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:

        if (VAR_2 == USB_DEVICE_REMOTE_WAKEUP) {

            VAR_0->remote_wakeup = 0;

        } else {

            goto fail;

        }

        VAR_6 = 0;

        break;



    case DeviceOutRequest | USB_REQ_SET_FEATURE:

        if (VAR_2 == USB_DEVICE_REMOTE_WAKEUP) {

            VAR_0->remote_wakeup = 1;

        } else {

            goto fail;

        }

        VAR_6 = 0;

        break;



    case DeviceOutRequest | USB_REQ_SET_ADDRESS:

        VAR_0->addr = VAR_2;

        VAR_6 = 0;

        break;



    case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:

        if (!s->rndis || VAR_2 || VAR_3 != 0)

            goto fail;

#ifdef TRAFFIC_DEBUG

        {

            unsigned int i;

            fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:");

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

                if (!(i & 15))

                    fprintf(stderr, "\n%04x:", i);

                fprintf(stderr, " %02x", VAR_5[i]);

            }

            fprintf(stderr, "\n\n");

        }

#endif

        VAR_6 = rndis_parse(s, VAR_5, VAR_4);

        break;



    case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:

        if (!s->rndis || VAR_2 || VAR_3 != 0)

            goto fail;

        VAR_6 = rndis_get_response(s, VAR_5);

        if (!VAR_6) {

            VAR_5[0] = 0;

            VAR_6 = 1;

        }

#ifdef TRAFFIC_DEBUG

        {

            unsigned int i;

            fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:");

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

                if (!(i & 15))

                    fprintf(stderr, "\n%04x:", i);

                fprintf(stderr, " %02x", VAR_5[i]);

            }

            fprintf(stderr, "\n\n");

        }

#endif

        break;



    case DeviceRequest | USB_REQ_GET_DESCRIPTOR:

        switch(VAR_2 >> 8) {

        case USB_DT_DEVICE:

            VAR_6 = sizeof(qemu_net_dev_descriptor);

            memcpy(VAR_5, qemu_net_dev_descriptor, VAR_6);

            break;



        case USB_DT_CONFIG:

            switch (VAR_2 & 0xff) {

            case 0:

                VAR_6 = sizeof(qemu_net_rndis_config_descriptor);

                memcpy(VAR_5, qemu_net_rndis_config_descriptor, VAR_6);

                break;



            case 1:

                VAR_6 = sizeof(qemu_net_cdc_config_descriptor);

                memcpy(VAR_5, qemu_net_cdc_config_descriptor, VAR_6);

                break;



            default:

                goto fail;

            }



            VAR_5[2] = VAR_6 & 0xff;

            VAR_5[3] = VAR_6 >> 8;

            break;



        case USB_DT_STRING:

            switch (VAR_2 & 0xff) {

            case 0:

                

                VAR_5[0] = 4;

                VAR_5[1] = 3;

                VAR_5[2] = 0x09;

                VAR_5[3] = 0x04;

                VAR_6 = 4;

                break;



            case STRING_ETHADDR:

                VAR_6 = set_usb_string(VAR_5, s->usbstring_mac);

                break;



            default:

                if (usb_net_stringtable[VAR_2 & 0xff]) {

                    VAR_6 = set_usb_string(VAR_5,

                                    usb_net_stringtable[VAR_2 & 0xff]);

                    break;

                }



                goto fail;

            }

            break;



        default:

            goto fail;

        }

        break;



    case DeviceRequest | USB_REQ_GET_CONFIGURATION:

        VAR_5[0] = s->rndis ? DEV_RNDIS_CONFIG_VALUE : DEV_CONFIG_VALUE;

        VAR_6 = 1;

        break;



    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:

        switch (VAR_2 & 0xff) {

        case DEV_CONFIG_VALUE:

            s->rndis = 0;

            break;



        case DEV_RNDIS_CONFIG_VALUE:

            s->rndis = 1;

            break;



        default:

            goto fail;

        }

        VAR_6 = 0;

        break;



    case DeviceRequest | USB_REQ_GET_INTERFACE:

    case InterfaceRequest | USB_REQ_GET_INTERFACE:

        VAR_5[0] = 0;

        VAR_6 = 1;

        break;



    case DeviceOutRequest | USB_REQ_SET_INTERFACE:

    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:

        VAR_6 = 0;

        break;



    default:

    fail:

        fprintf(stderr, "usbnet: failed control transaction: "

                        "VAR_1 0x%x VAR_2 0x%x VAR_3 0x%x VAR_4 0x%x\n",

                        VAR_1, VAR_2, VAR_3, VAR_4);

        VAR_6 = USB_RET_STALL;

        break;

    }

    return VAR_6;

}

static int FUNC_0(USBDevice *VAR_0, int VAR_1, int VAR_2,
int VAR_3, int VAR_4, uint8_t *VAR_5)
{ USBNetState *s = (USBNetState *) VAR_0; int VAR_6 = 0; switch(VAR_1) { case DeviceRequest | USB_REQ_GET_STATUS:
VAR_5[0] = (1 << USB_DEVICE_SELF_POWERED) |
(VAR_0->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP); VAR_5[1] = 0x00; VAR_6 = 2; break; case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
if (VAR_2 == USB_DEVICE_REMOTE_WAKEUP) { VAR_0->remote_wakeup = 0; } else { goto fail; } VAR_6 = 0; break; case DeviceOutRequest | USB_REQ_SET_FEATURE:
if (VAR_2 == USB_DEVICE_REMOTE_WAKEUP) { VAR_0->remote_wakeup = 1; } else { goto fail; } VAR_6 = 0; break; case DeviceOutRequest | USB_REQ_SET_ADDRESS:
VAR_0->addr = VAR_2; VAR_6 = 0; break; case ClassInterfaceOutRequest | USB_CDC_SEND_ENCAPSULATED_COMMAND:
if (!s->rndis || VAR_2 || VAR_3 != 0)
goto fail; #ifdef TRAFFIC_DEBUG
{ unsigned int i; fprintf(stderr, "SEND_ENCAPSULATED_COMMAND:"); for (i = 0; i < VAR_4; i++) { if (!(i & 15))
fprintf(stderr, "\n%04x:", i); fprintf(stderr, " %02x", VAR_5[i]); } fprintf(stderr, "\n\n"); } #endif
VAR_6 = rndis_parse(s, VAR_5, VAR_4); break; case ClassInterfaceRequest | USB_CDC_GET_ENCAPSULATED_RESPONSE:
if (!s->rndis || VAR_2 || VAR_3 != 0)
goto fail; VAR_6 = rndis_get_response(s, VAR_5); if (!VAR_6) { VAR_5[0] = 0; VAR_6 = 1; } #ifdef TRAFFIC_DEBUG
{ unsigned int i; fprintf(stderr, "GET_ENCAPSULATED_RESPONSE:"); for (i = 0; i < VAR_6; i++) { if (!(i & 15))
fprintf(stderr, "\n%04x:", i); fprintf(stderr, " %02x", VAR_5[i]); } fprintf(stderr, "\n\n"); } #endif
break; case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
switch(VAR_2 >> 8) { case USB_DT_DEVICE:
VAR_6 = sizeof(qemu_net_dev_descriptor); memcpy(VAR_5, qemu_net_dev_descriptor, VAR_6); break; case USB_DT_CONFIG:
switch (VAR_2 & 0xff) { case 0:
VAR_6 = sizeof(qemu_net_rndis_config_descriptor); memcpy(VAR_5, qemu_net_rndis_config_descriptor, VAR_6); break; case 1:
VAR_6 = sizeof(qemu_net_cdc_config_descriptor); memcpy(VAR_5, qemu_net_cdc_config_descriptor, VAR_6); break; default:
goto fail; } VAR_5[2] = VAR_6 & 0xff; VAR_5[3] = VAR_6 >> 8; break; case USB_DT_STRING:
switch (VAR_2 & 0xff) { case 0:
VAR_5[0] = 4; VAR_5[1] = 3; VAR_5[2] = 0x09; VAR_5[3] = 0x04; VAR_6 = 4; break; case STRING_ETHADDR:
VAR_6 = set_usb_string(VAR_5, s->usbstring_mac); break; default:
if (usb_net_stringtable[VAR_2 & 0xff]) { VAR_6 = set_usb_string(VAR_5,
usb_net_stringtable[VAR_2 & 0xff]); break; } goto fail; } break; default:
goto fail; } break; case DeviceRequest | USB_REQ_GET_CONFIGURATION:
VAR_5[0] = s->rndis ? DEV_RNDIS_CONFIG_VALUE : DEV_CONFIG_VALUE; VAR_6 = 1; break; case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
switch (VAR_2 & 0xff) { case DEV_CONFIG_VALUE:
s->rndis = 0; break; case DEV_RNDIS_CONFIG_VALUE:
s->rndis = 1; break; default:
goto fail; } VAR_6 = 0; break; case DeviceRequest | USB_REQ_GET_INTERFACE:
case InterfaceRequest | USB_REQ_GET_INTERFACE:
VAR_5[0] = 0; VAR_6 = 1; break; case DeviceOutRequest | USB_REQ_SET_INTERFACE:
case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
VAR_6 = 0; break; default:
fail:
fprintf(stderr, "usbnet: failed control transaction: "
"VAR_1 0x%x VAR_2 0x%x VAR_3 0x%x VAR_4 0x%x\n",
VAR_1, VAR_2, VAR_3, VAR_4); VAR_6 = USB_RET_STALL; break; } return VAR_6; }