static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td,

                          uint32_t *int_mask, bool queuing)

{

    UHCIAsync *async;

    int len = 0, max_len;

    uint8_t pid;

    USBDevice *dev;

    USBEndpoint *ep;



    /* Is active ? */

    if (!(td->ctrl & TD_CTRL_ACTIVE))

        return TD_RESULT_NEXT_QH;



    async = uhci_async_find_td(s, addr, td);

    if (async) {

        /* Already submitted */

        async->queue->valid = 32;



        if (!async->done)

            return TD_RESULT_ASYNC_CONT;

        if (queuing) {

            /* we are busy filling the queue, we are not prepared

               to consume completed packages then, just leave them

               in async state */

            return TD_RESULT_ASYNC_CONT;

        }



        uhci_async_unlink(async);

        goto done;

    }



    /* Allocate new packet */

    async = uhci_async_alloc(uhci_queue_get(s, td), addr);



    /* valid needs to be large enough to handle 10 frame delay

     * for initial isochronous requests

     */

    async->queue->valid = 32;

    async->isoc  = td->ctrl & TD_CTRL_IOS;



    max_len = ((td->token >> 21) + 1) & 0x7ff;

    pid = td->token & 0xff;



    dev = uhci_find_device(s, (td->token >> 8) & 0x7f);

    ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);

    usb_packet_setup(&async->packet, pid, ep);

    qemu_sglist_add(&async->sgl, td->buffer, max_len);

    usb_packet_map(&async->packet, &async->sgl);



    switch(pid) {

    case USB_TOKEN_OUT:

    case USB_TOKEN_SETUP:

        len = usb_handle_packet(dev, &async->packet);

        if (len >= 0)

            len = max_len;

        break;



    case USB_TOKEN_IN:

        len = usb_handle_packet(dev, &async->packet);

        break;



    default:

        /* invalid pid : frame interrupted */

        uhci_async_free(async);

        s->status |= UHCI_STS_HCPERR;

        uhci_update_irq(s);

        return TD_RESULT_STOP_FRAME;

    }

 

    if (len == USB_RET_ASYNC) {

        uhci_async_link(async);

        return TD_RESULT_ASYNC_START;

    }



    async->packet.result = len;



done:

    len = uhci_complete_td(s, td, async, int_mask);

    usb_packet_unmap(&async->packet);

    uhci_async_free(async);

    return len;

}

static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td,

                          uint32_t *int_mask, bool queuing)

{

    UHCIAsync *async;

    int len = 0, max_len;

    uint8_t pid;

    USBDevice *dev;

    USBEndpoint *ep;



    

    if (!(td->ctrl & TD_CTRL_ACTIVE))

        return TD_RESULT_NEXT_QH;



    async = uhci_async_find_td(s, addr, td);

    if (async) {

        

        async->queue->valid = 32;



        if (!async->done)

            return TD_RESULT_ASYNC_CONT;

        if (queuing) {

            





            return TD_RESULT_ASYNC_CONT;

        }



        uhci_async_unlink(async);

        goto done;

    }



    

    async = uhci_async_alloc(uhci_queue_get(s, td), addr);



    





    async->queue->valid = 32;

    async->isoc  = td->ctrl & TD_CTRL_IOS;



    max_len = ((td->token >> 21) + 1) & 0x7ff;

    pid = td->token & 0xff;



    dev = uhci_find_device(s, (td->token >> 8) & 0x7f);

    ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);

    usb_packet_setup(&async->packet, pid, ep);

    qemu_sglist_add(&async->sgl, td->buffer, max_len);

    usb_packet_map(&async->packet, &async->sgl);



    switch(pid) {

    case USB_TOKEN_OUT:

    case USB_TOKEN_SETUP:

        len = usb_handle_packet(dev, &async->packet);

        if (len >= 0)

            len = max_len;

        break;



    case USB_TOKEN_IN:

        len = usb_handle_packet(dev, &async->packet);

        break;



    default:

        

        uhci_async_free(async);

        s->status |= UHCI_STS_HCPERR;

        uhci_update_irq(s);

        return TD_RESULT_STOP_FRAME;

    }

 

    if (len == USB_RET_ASYNC) {

        uhci_async_link(async);

        return TD_RESULT_ASYNC_START;

    }



    async->packet.result = len;



done:

    len = uhci_complete_td(s, td, async, int_mask);

    usb_packet_unmap(&async->packet);

    uhci_async_free(async);

    return len;

}

    usb_packet_unmap(&async->packet);
static int FUNC_0(UHCIState *VAR_0, uint32_t VAR_1, UHCI_TD *VAR_2,

                          uint32_t *VAR_3, bool VAR_4)

{

    UHCIAsync *async;

    int VAR_5 = 0, VAR_6;

    uint8_t pid;

    USBDevice *dev;

    USBEndpoint *ep;



    

    if (!(VAR_2->ctrl & TD_CTRL_ACTIVE))

        return TD_RESULT_NEXT_QH;



    async = uhci_async_find_td(VAR_0, VAR_1, VAR_2);

    if (async) {

        

        async->queue->valid = 32;



        if (!async->done)

            return TD_RESULT_ASYNC_CONT;

        if (VAR_4) {

            





            return TD_RESULT_ASYNC_CONT;

        }



        uhci_async_unlink(async);

        goto done;

    }



    

    async = uhci_async_alloc(uhci_queue_get(VAR_0, VAR_2), VAR_1);



    





    async->queue->valid = 32;

    async->isoc  = VAR_2->ctrl & TD_CTRL_IOS;



    VAR_6 = ((VAR_2->token >> 21) + 1) & 0x7ff;

    pid = VAR_2->token & 0xff;



    dev = uhci_find_device(VAR_0, (VAR_2->token >> 8) & 0x7f);

    ep = usb_ep_get(dev, pid, (VAR_2->token >> 15) & 0xf);

    usb_packet_setup(&async->packet, pid, ep);

    qemu_sglist_add(&async->sgl, VAR_2->buffer, VAR_6);

    usb_packet_map(&async->packet, &async->sgl);



    switch(pid) {

    case USB_TOKEN_OUT:

    case USB_TOKEN_SETUP:

        VAR_5 = usb_handle_packet(dev, &async->packet);

        if (VAR_5 >= 0)

            VAR_5 = VAR_6;

        break;



    case USB_TOKEN_IN:

        VAR_5 = usb_handle_packet(dev, &async->packet);

        break;



    default:

        

        uhci_async_free(async);

        VAR_0->status |= UHCI_STS_HCPERR;

        uhci_update_irq(VAR_0);

        return TD_RESULT_STOP_FRAME;

    }

 

    if (VAR_5 == USB_RET_ASYNC) {

        uhci_async_link(async);

        return TD_RESULT_ASYNC_START;

    }



    async->packet.result = VAR_5;



done:

    VAR_5 = uhci_complete_td(VAR_0, VAR_2, async, VAR_3);

    usb_packet_unmap(&async->packet);

    uhci_async_free(async);

    return VAR_5;

}

static int FUNC_0(UHCIState *VAR_0, uint32_t VAR_1, UHCI_TD *VAR_2,
uint32_t *VAR_3, bool VAR_4)
{ UHCIAsync *async; int VAR_5 = 0, VAR_6; uint8_t pid; USBDevice *dev; USBEndpoint *ep; if (!(VAR_2->ctrl & TD_CTRL_ACTIVE))
return TD_RESULT_NEXT_QH; async = uhci_async_find_td(VAR_0, VAR_1, VAR_2); if (async) { async->queue->valid = 32; if (!async->done)
return TD_RESULT_ASYNC_CONT; if (VAR_4) { return TD_RESULT_ASYNC_CONT; } uhci_async_unlink(async); goto done; } async = uhci_async_alloc(uhci_queue_get(VAR_0, VAR_2), VAR_1); async->queue->valid = 32; async->isoc  = VAR_2->ctrl & TD_CTRL_IOS; VAR_6 = ((VAR_2->token >> 21) + 1) & 0x7ff; pid = VAR_2->token & 0xff; dev = uhci_find_device(VAR_0, (VAR_2->token >> 8) & 0x7f); ep = usb_ep_get(dev, pid, (VAR_2->token >> 15) & 0xf); usb_packet_setup(&async->packet, pid, ep); qemu_sglist_add(&async->sgl, VAR_2->buffer, VAR_6); usb_packet_map(&async->packet, &async->sgl); switch(pid) { case USB_TOKEN_OUT:
case USB_TOKEN_SETUP:
VAR_5 = usb_handle_packet(dev, &async->packet); if (VAR_5 >= 0)
VAR_5 = VAR_6; break; case USB_TOKEN_IN:
VAR_5 = usb_handle_packet(dev, &async->packet); break; default:
uhci_async_free(async); VAR_0->status |= UHCI_STS_HCPERR; uhci_update_irq(VAR_0); return TD_RESULT_STOP_FRAME; } if (VAR_5 == USB_RET_ASYNC) { uhci_async_link(async); return TD_RESULT_ASYNC_START; } async->packet.result = VAR_5; done:
VAR_5 = uhci_complete_td(VAR_0, VAR_2, async, VAR_3); usb_packet_unmap(&async->packet); uhci_async_free(async); return VAR_5; }