void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)

{

    VirtQueueElement *elem;

    unsigned have, need;

    int i, len;



    if (!vinput->active) {

        return;

    }



    /* queue up events ... */

    if (vinput->qindex == vinput->qsize) {

        vinput->qsize++;

        vinput->queue = g_realloc(vinput->queue, vinput->qsize *

                                  sizeof(virtio_input_event));

    }

    vinput->queue[vinput->qindex++] = *event;



    /* ... until we see a report sync ... */

    if (event->type != cpu_to_le16(EV_SYN) ||

        event->code != cpu_to_le16(SYN_REPORT)) {

        return;

    }



    /* ... then check available space ... */

    need = sizeof(virtio_input_event) * vinput->qindex;

    virtqueue_get_avail_bytes(vinput->evt, &have, NULL, need, 0);

    if (have < need) {

        vinput->qindex = 0;

        trace_virtio_input_queue_full();

        return;

    }



    /* ... and finally pass them to the guest */

    for (i = 0; i < vinput->qindex; i++) {

        elem = virtqueue_pop(vinput->evt, sizeof(VirtQueueElement));

        if (!elem) {

            /* should not happen, we've checked for space beforehand */

            fprintf(stderr, "%s: Huh?  No vq elem available ...\n", __func__);

            return;

        }

        len = iov_from_buf(elem->in_sg, elem->in_num,

                           0, vinput->queue+i, sizeof(virtio_input_event));

        virtqueue_push(vinput->evt, elem, len);

        g_free(elem);

    }

    virtio_notify(VIRTIO_DEVICE(vinput), vinput->evt);

    vinput->qindex = 0;

}

void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)

{

    VirtQueueElement *elem;

    unsigned have, need;

    int i, len;



    if (!vinput->active) {

        return;

    }



    

    if (vinput->qindex == vinput->qsize) {

        vinput->qsize++;

        vinput->queue = g_realloc(vinput->queue, vinput->qsize *

                                  sizeof(virtio_input_event));

    }

    vinput->queue[vinput->qindex++] = *event;



    

    if (event->type != cpu_to_le16(EV_SYN) ||

        event->code != cpu_to_le16(SYN_REPORT)) {

        return;

    }



    

    need = sizeof(virtio_input_event) * vinput->qindex;

    virtqueue_get_avail_bytes(vinput->evt, &have, NULL, need, 0);

    if (have < need) {

        vinput->qindex = 0;

        trace_virtio_input_queue_full();

        return;

    }



    

    for (i = 0; i < vinput->qindex; i++) {

        elem = virtqueue_pop(vinput->evt, sizeof(VirtQueueElement));

        if (!elem) {

            

            fprintf(stderr, "%s: Huh?  No vq elem available ...\n", __func__);

            return;

        }

        len = iov_from_buf(elem->in_sg, elem->in_num,

                           0, vinput->queue+i, sizeof(virtio_input_event));

        virtqueue_push(vinput->evt, elem, len);

        g_free(elem);

    }

    virtio_notify(VIRTIO_DEVICE(vinput), vinput->evt);

    vinput->qindex = 0;

}

void FUNC_0(VirtIOInput *VAR_0, virtio_input_event *VAR_1)

{

    VirtQueueElement *elem;

    unsigned VAR_2, VAR_3;

    int VAR_4, VAR_5;



    if (!VAR_0->active) {

        return;

    }



    

    if (VAR_0->qindex == VAR_0->qsize) {

        VAR_0->qsize++;

        VAR_0->queue = g_realloc(VAR_0->queue, VAR_0->qsize *

                                  sizeof(virtio_input_event));

    }

    VAR_0->queue[VAR_0->qindex++] = *VAR_1;



    

    if (VAR_1->type != cpu_to_le16(EV_SYN) ||

        VAR_1->code != cpu_to_le16(SYN_REPORT)) {

        return;

    }



    

    VAR_3 = sizeof(virtio_input_event) * VAR_0->qindex;

    virtqueue_get_avail_bytes(VAR_0->evt, &VAR_2, NULL, VAR_3, 0);

    if (VAR_2 < VAR_3) {

        VAR_0->qindex = 0;

        trace_virtio_input_queue_full();

        return;

    }



    

    for (VAR_4 = 0; VAR_4 < VAR_0->qindex; VAR_4++) {

        elem = virtqueue_pop(VAR_0->evt, sizeof(VirtQueueElement));

        if (!elem) {

            

            fprintf(stderr, "%s: Huh?  No vq elem available ...\n", __func__);

            return;

        }

        VAR_5 = iov_from_buf(elem->in_sg, elem->in_num,

                           0, VAR_0->queue+VAR_4, sizeof(virtio_input_event));

        virtqueue_push(VAR_0->evt, elem, VAR_5);

        g_free(elem);

    }

    virtio_notify(VIRTIO_DEVICE(VAR_0), VAR_0->evt);

    VAR_0->qindex = 0;

}

void FUNC_0(VirtIOInput *VAR_0, virtio_input_event *VAR_1)
{ VirtQueueElement *elem; unsigned VAR_2, VAR_3; int VAR_4, VAR_5; if (!VAR_0->active) { return; } if (VAR_0->qindex == VAR_0->qsize) { VAR_0->qsize++; VAR_0->queue = g_realloc(VAR_0->queue, VAR_0->qsize *
sizeof(virtio_input_event)); } VAR_0->queue[VAR_0->qindex++] = *VAR_1; if (VAR_1->type != cpu_to_le16(EV_SYN) ||
VAR_1->code != cpu_to_le16(SYN_REPORT)) { return; } VAR_3 = sizeof(virtio_input_event) * VAR_0->qindex; virtqueue_get_avail_bytes(VAR_0->evt, &VAR_2, NULL, VAR_3, 0); if (VAR_2 < VAR_3) { VAR_0->qindex = 0; trace_virtio_input_queue_full(); return; } for (VAR_4 = 0; VAR_4 < VAR_0->qindex; VAR_4++) { elem = virtqueue_pop(VAR_0->evt, sizeof(VirtQueueElement)); if (!elem) { fprintf(stderr, "%s: Huh?  No vq elem available ...\n", __func__); return; } VAR_5 = iov_from_buf(elem->in_sg, elem->in_num,
0, VAR_0->queue+VAR_4, sizeof(virtio_input_event)); virtqueue_push(VAR_0->evt, elem, VAR_5); g_free(elem); } virtio_notify(VIRTIO_DEVICE(VAR_0), VAR_0->evt); VAR_0->qindex = 0; }