/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
/* Isoc endpoints and transfers. */
#include <iostypes.h>
#include <ios.h>
#include <iop.h>
#include <ioslibc.h>

#include "ohci.h"
#include "ohci_regs.h"
#include "ohci_core.h"
#include "usb11.h"
#include "ohci_types.h"

extern struct ohci_device __ohci_dev[OHCI_DEV_MAX]; 

#if OH0
/* Format:
 *  1 read buffer with ep.
 *  At least 1 write buffer. At most 8 write buffers.
 *  Write buffer constraints: [OHCI specs: Sec 4.3.2.3.5.4]
 *      - buffers should be contiguous
 *      - max buffer size = 1023*8 bytes
 */

static IOSError
__checkIsoArgs(IOSResourceIoctlv *args)
{
    IOSError rv = IOS_ERROR_INVALID;
    u16 *pktSize, buflen;
    u8 numPackets, i;

    CHK_RDWR(3, 2);
    if (    CHK_VEC(0, sizeof(u8))     /* ep */
            || CHK_VEC(1, sizeof(u16))    /* bufLen */
            || CHK_VEC(2, sizeof(u8))     /* numPackets */
            || CHK_VEC(3, (*((u8 *)args->vector[2].base)) * sizeof(u16)) /* pktSize */
            || CHK_VEC(4, (*((u16*)args->vector[1].base))) /* buf */
       ) {
        goto done;
    }
    buflen = *((u16 *)args->vector[1].base);
    numPackets = *((u8 *)args->vector[2].base);
    pktSize = (u16 *)args->vector[3].base;

    {
        u32 i, sz = 0;
        for (i = 0; i < numPackets; ++i) {
            pktSize[i] &= 0xfff;
            if (pktSize[i] > 1023) {
                goto done;
            }
            sz += pktSize[i];
        }
        if (buflen != sz) {
            goto done;
        }
    }

    if (numPackets > 8) {
        OHCI_ERR("%s: Too many packets: %u\n", __FUNCTION__, numPackets);
        goto done;
    }
    for (i = 0; i < numPackets; ++i) {
        if ((pktSize[i] & 0xfff) > 1023) {
            OHCI_ERR("%s: packet %u too long %u\n", __FUNCTION__, i, pktSize[i] & 0xfff);
            goto done;
        }
    }
    rv = IOS_ERROR_OK;
done:
    return rv;
}

static void
__fillITD(ohci_td *td, OHCIrp *irp, u8 *buf, u16 buflen, 
        u8 numPackets, u16 *pktSize)
{
    u8 i;
    u8 flag = 1;
    u32 tmp;
    u8 fc = 0;
    u32 psw = 0;

    td->cbp = CPU_TO_OHCI32(((u32)buf) & OHCI_ITD_BP0_MASK); /* bp0 */
    td->be =  CPU_TO_OHCI32(((u32)buf) + buflen  - 1);
    td->irp = irp;
    td->len = buflen;
    ++irp->ntd;
    memset((u32 *)td->psw, 0, sizeof(u32)*4);
    tmp = ((u32)buf & 0xfff); /* Offset within 1st 4K page */

    OHCI_DBG("numpackets = %d start offset = 0x%x\n", numPackets, tmp);
    for (i = 0; i < numPackets; ++i) {
        if (flag) {
            psw = 0xe000 |tmp;
            flag = 0;
            if ((numPackets - 1) == i) {
                td->psw[i/2] = CPU_TO_OHCI32(psw);
            }
        } else {
            psw |= 0xe0000000 | (tmp << 16);
            td->psw[(i - 1)/2] = CPU_TO_OHCI32(psw);
            flag = 1;
        }
        tmp += pktSize[i];
    }
    fc = numPackets - 1;
    td->flags =  CPU_TO_OHCI32(fc << OHCI_ITD_FC_SHIFT |0xf0000000
            |OHCI_TD_TYPE_ITD); /* Set SF later */
#ifdef ENABLE_OHCI_DBG
    dumpITD(td);
#endif
}

static void
__fillITDSF(ohci_td *td, u32 fno)
{
    OHCI_DBG("frame: %x\n", fno);
    td->flags = CPU_TO_OHCI32((OHCI_TO_CPU32(td->flags) & 0xffff0000)| (fno & 0xffff));
}

static void
__startIsocList(struct ohci_hcd *hcd)
{
    u32 reg;
    reg = OHCI_READ(hcd->opregs->ctrl);
    reg |= OHCI_CTRL_IE_MASK | OHCI_CTRL_PLE_MASK;
    OHCI_WRITE(hcd->opregs->ctrl, reg);
}

IOSError 
ohci_IsoIRP(struct ohci_hcd *hcd, IOSResourceRequest *resReq)
{
    IOSError rv = IOS_ERROR_OK;
    u8 devnum;
    s8 idx = 0;
    u8 ep;
    u16 buflen;
    u16 *pktSize;
    u8 numPackets;
    u8 *buf;
    ohci_ed *ed;
    ohci_td *td_data = NULL;
    ohci_td *td_dummy = NULL;
    OHCIrp *irp = NULL;
    IOSResourceIoctlv *args = (IOSResourceIoctlv *)&resReq->args.ioctlv;

    disableMIE(hcd);
    IOS_FlushMem(hcd->hc_wb);
    IOS_InvalidateRdb(IOS_RB_IOD);

    if ((rv = __checkIsoArgs(args)) < 0) {
        OHCI_ERR("%s: invalid iso msg args\n", __FUNCTION__);
        goto done;
    }
    ep = *((u8 *)args->vector[0].base);
    buflen = *((u16 *)args->vector[1].base);
    numPackets = *((u8 *)args->vector[2].base);
    pktSize = ((u16 *)args->vector[3].base);
    buf = args->vector[4].base;

    if (0 == ep || 0 == buflen || 0 == numPackets || NULL == buf) {
        OHCI_ERR("%s: invalid iso msg args\n", __FUNCTION__);
        rv = IOS_ERROR_INVALID;
        goto done;
    }
    //   printf("ISO xfer request: ep %u buf %p buflen %u numPackets = %u\n",
    //           ep, buf, buflen, numPackets);
    OHCI_DBG("ISO transfer requested to endpoint: %u buf %p buflen %u\n", 
            ep, buf, buflen);
    devnum = resReq->handle;

    irp = IOS_Alloc(__ohcdHeapId, sizeof(OHCIrp));
    if (NULL == irp) {
        OHCI_ERR("%s: Failed to alloc irp\n", __FUNCTION__);
        rv = IOS_ERROR_MAX;
        goto err;
    }

    idx = getEndpointIdx(devnum, ep);
    if (idx <= 0) {
        OHCI_ERR("%s: Failed to find ep %u in device %u\n", __FUNCTION__, ep, devnum);
        rv = IOS_ERROR_INVALID;
        goto err;
    }
    ed = __ohci_dev[devnum].dev.ep[idx].ed;

    irp->req = resReq;
    irp->ntd = 0;
    irp->done = 0;
    irp->nxt = NULL;
    irp->ed = ed;
    irp->qid = -1;
    irp->data = (void *)buf;
    irp->datalen = buflen;
    irp->clean1 = NULL;
    irp->pswSize = pktSize;
    irp->numPkts = numPackets;

    if (NULL == (td_dummy = allocTD())) {
        OHCI_ERR("failed to alloc td\n");
        rv = IOS_ERROR_MAX;
        goto err;
    }
    td_data = (struct ohci_td *)OHCI_TO_CPU32(ed->tailP);

    __fillITD(td_data, irp, buf, buflen, numPackets, pktSize);
    td_data->nextTD = CPU_TO_OHCI32(td_dummy);

    { /* Set SF */
        u32 sf = 0;
        if (ed->headP == ed->tailP) { /* idle */
            sf = OHCI_READ(hcd->opregs->frm_num) + 8;
        } else {
            sf  = __ohci_dev[devnum].dev.ep[idx].isoc_nxt + 
                __ohci_dev[devnum].dev.ep[idx].interval;
        }
        __ohci_dev[devnum].dev.ep[idx].isoc_nxt = sf + numPackets - 1;
        __fillITDSF(td_data, sf);
    }
    ed->tailP = CPU_TO_OHCI32(td_dummy);
    IOS_FlushMem(IOS_WB_IOD);
    IOS_InvalidateRdb(hcd->hc_rdb);

    __startIsocList(hcd);
    startED(hcd, ed);
    goto done;
err:
    if (NULL != irp)
        IOS_Free(__ohcdHeapId, irp);
    if (NULL != td_data)
        freeTD(td_data);
    /* Fall through */
done:
    enableMIE(hcd);
    return rv;
}
#endif /* OH0 */
