/*
 *              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.
 *
 */

#include <sc/sc.h>
#include <sc/ios.h>
#include <ioslibc.h>
#include "usb.h"
#include "usbdev.h"
#include "usbhw.h"
#include "ch9.h"
#include "disk.h"
#include "priority.h"

u8 ep0In[EP0_PKT_SIZE]  DCACHE_ALIGN;       /* Ep0 In buffer */
u8 ep0Out[EP0_PKT_SIZE] DCACHE_ALIGN;       /* Ep0 Out buffer */
u8 *outPtr, *inPtr;
int inLen, outLen, inPos, outPos;
int hostReqLen;
Ch9CallBack *pCh9Callback=NULL;
extern int curUsbDev;

IOSError 
setupCh9Callback(Ch9CallBack *ptr)
{
    pCh9Callback = ptr;

    return IOS_ERROR_OK;
}

IOSError
Ch9Setup()
{
    return usbHwRead(0, ep0Out, 8);
}

IOSError
Ch9RecvSetup()
{
    IOSError rv = IOS_ERROR_OK;
    USBSetupPacket *setup;
   
    setup = (USBSetupPacket *) ep0Out;
    if ((USB_REQ_TYPE(setup->reqType) == USB_REQ_CLASS)  && (curUsbDev == USB_DEV_MSD_ID)) {
        inLen = msdClassReq(setup, ep0In);
        hostReqLen  = setup->wLength;
        if (inLen > hostReqLen) inLen = hostReqLen;
        if (inLen > 0) usbHwWrite(0, ep0In, inLen >= 8 ? 8 : inLen);
        goto out;
    } else if (USB_REQ_TYPE(setup->reqType) != USB_REQ_STANDARD) {
        USB_LOG("Ch9 reqType=0x%x not supported\n", USB_REQ_TYPE(setup->reqType));
        rv = IOS_ERROR_INVALID;
        goto out;
    } 

    outPtr = ep0Out;
    inPtr = ep0In;
    outLen = 8;
    outPos = inLen = inPos = 0;   
    hostReqLen = -1; 

    switch (setup->req) {
    case USB_REQ_GET_STATUS:
        ep0In[0] = ep0In[1] = 0;
        usbHwWrite(0, ep0In, 8);
        break;
    case USB_REQ_CLEAR_FEATURE:
        /* XXX add stall and wakeup if necessary */
        usbHwWrite(0, ep0In, 0); 
        break;
    case USB_REQ_SET_FEATURE:
        /* XXX add stall and wakeup if necessary */
        usbHwWrite(0, ep0In, 0); 
        break;
    case USB_REQ_SET_ADDRESS: 
        IO_WRITE(USB_CTRL_REG, USB_CTRL_A_WE | ((setup->wValue) << USB_CTRL_USB_ADDR_SHIFT));
        usbHwWrite(0, ep0In, 0);
        break;
    case USB_REQ_GET_DESCRIPTOR:
        switch (U16_HIGH_BYTE(setup->wValue)) {
            case USB_GET_DESC_DEV:
                inPtr = pCh9Callback->getDevDesc(&inLen);
                break;
            case USB_GET_DESC_CONF:
                inPtr = pCh9Callback->getConfDesc(&inLen);  
                break;
            case USB_GET_DESC_STRING:
                inPtr = pCh9Callback->getDescString(
				            U16_LOW_BYTE(setup->wValue), &inLen);
                break;
            default:
                USB_ERROR("Invalid Desc request");
                inPtr = NULL;
                break;
        }

        if (inPtr) {
            hostReqLen  = setup->wLength;
            if (inLen > hostReqLen)  inLen = hostReqLen;
            memcpy(ep0In, inPtr, 8);
            usbHwWrite(0, ep0In, inLen >= 8 ? 8 : inLen);
        }
        break;
    case USB_REQ_SET_DESCRIPTOR:
        usbHwWrite(0, ep0In, 0);
        break;
    case USB_REQ_GET_CONFIGURATION:
        ep0In[0] = ep0In[1] = 0;
        usbHwWrite(0, ep0In, 0); 
        break;
    case USB_REQ_SET_CONFIGURATION:
        IO_WRITE(USB_CTRL_REG, (IO_READ(USB_CTRL_REG) & USB_CTRL_INFO_MASK) |
                                USB_CTRL_CFG_DONE | USB_CTRL_WE);
        usbHwWrite(0, ep0In, 0);
        IOS_SetThreadPriority(0, IOS_PRIORITY_USB);
        pCh9Callback->enumDone();
        break;
    } 

out:
    usbHwRead(0, ep0Out, 8);
    return rv;
}


IOSError
Ch9SendData(int sLen)
{
    int len;

    inPos += sLen;
    if ((inPos <= inLen) && (inLen > 0) && sLen) {
        len = (inLen - inPos) > 8 ? 8 : inLen - inPos;
        if (len == 0) {
            /* Try to avoid cancel case here */
            if (hostReqLen > inLen) usbHwWrite(0, ep0In, 0);
        } else {
            memcpy(ep0In, inPtr+inPos, len);
            usbHwWrite(0, ep0In, len);
        }
    } else {
        inLen = -1;
        inPtr = NULL;
    }
    return IOS_ERROR_OK;
}

IOSError
Ch9RecvData(int len)
{
    /* Cancel any IN transaction if we recv OUT packet */
    if (IO_READ(USB_IN0_PTR_REG) & USB_IN0_PTR_VALID) {
        /* Recv 0 length OUT, cancel Read */
        IO_WRITE(USB_IN0_LEN_REG, USB_IN0_LEN_CANCEL);
        if (USB_IN0_LEN_CAN_STATUS(IO_READ(USB_IN0_LEN_REG)) != 2) {
            USB_ERROR("Cannot cancle pending read");
        } else {
            inPtr = NULL;
            inLen =  inPos = 0;
            IO_WRITE(USB_IN0_PTR_REG, USB_IN0_PTR_INT);
        }
    } 

    /* Just discard all Host OUT data */
    usbHwRead(0, ep0Out, 8);

    return IOS_ERROR_OK;
}
