/*
 *               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 <iostypes.h>
#include <ios.h>
#include <iop.h>
#include <ioslibc.h>
#include "usb11.h"
#include "iosusbtypes.h"

static s32
__IntrBlkMsg(IOSFd fd, u32 ep, u32 buflen, char *buf, u8 typ)
{
    IOSError rv = IOS_ERROR_OK;
    USBIntrBlkReq req;
    req.ep = ep;
    req.data = (u32)buf;
    req.wLength = buflen;
    rv = IOS_Ioctl(fd, typ, &req, sizeof(USBIntrBlkReq), 0, 0);

//    printf("intr/blk ioctl returned: %d\n", rv);
    return rv;
}

s32
IUSB_ReadIntrMsg(IOSFd fd, u32 ep, u32 buflen, char *buf)
{
    return __IntrBlkMsg(fd, ep, buflen, buf, OHC_INTR_MSG);
}

s32
IUSB_WriteIntrMsg(IOSFd fd, u32 ep, u32 buflen, char *buf)
{
    return __IntrBlkMsg(fd, ep, buflen, buf, OHC_INTR_MSG);
}

s32
IUSB_ReadBlkMsg(IOSFd fd, u32 ep, u32 buflen, char *buf)
{
    return __IntrBlkMsg(fd, ep, buflen, buf, OHC_BLK_MSG);
}

s32
IUSB_WriteBlkMsg(IOSFd fd, u32 ep, u32 buflen, char *buf)
{
    return __IntrBlkMsg(fd, ep, buflen, buf, OHC_BLK_MSG);
}

static s32
__CtrlMsg(IOSFd fd, u8 reqType, u8 request, u16 value,
        u16 index, u16 length, u32 data)
{
    IOSError rv = IOS_ERROR_OK;
    USBCtrlReq req;

    req.bmRequestType = reqType;
    req.bRequest = request;
    req.wValue = SWAP16(value);
    req.wIndex = SWAP16(index) ;
    req.wLength = SWAP16(length);
    req.data = (u32)data;
    req.ep = 0;

    rv = IOS_Ioctl(fd, OHC_CTRL_MSG, &req, sizeof(USBCtrlReq), 0, 0);
    return rv;
}

s32
IUSB_ReadCtrlMsg(IOSFd fd, u8 reqType, u8 request, u16 value,
        u16 index, u16 length, u32 data)
{
    return __CtrlMsg(fd, reqType, request, value, index, length, data);
}

s32
IUSB_WriteCtrlMsg(IOSFd fd, u8 reqType, u8 request, u16 value,
        u16 index, u16 length, u32 data)
{
    return __CtrlMsg(fd, reqType, request, value, index, length, data);
}

static s8
unicode2ascii(char *tbuf, int buflen)
{
    char buf[32];
    int di, si;
    if (tbuf[1] != USB_DT_STRING )
        return -1;
    for (di = 0, si = 2; si < tbuf[0]; si += 2) {
        if (di >= (buflen - 1))
            break;
        if (tbuf[si + 1])   /* high byte */
            buf[di++] = '?';
        else
            buf[di++] = tbuf[si];
    }
    buf[di] = 0;
    memcpy(tbuf, buf, di + 1);
    return (di);
}

/* How to use IUSB_CtrlMsg */
s32 
IUSB_GetAsciiStr(IOSFd fd, u8 ep, u16 index, u16 langId,
        char *buf, u16 buflen)
{
    s8 len = __CtrlMsg(fd, USB_REQTYPE_GETDESC, USB_REQ_GET_DESCRIPTOR,
            (USB_DT_STRING << 8) + index,  langId, buflen, (u32)buf);
    if (len < 0)
        goto done;

    if ((len = unicode2ascii(buf, buflen)) < 0) 
        printf("failed to convert buffer from unicode 2 ascii\n");
    else
        buf[len] = '\0';
done:
    return len;
}

IOSError
IUSB_GetDevDescr(IOSFd fd, struct USB_DevDescr *d)
{
    return  __CtrlMsg(fd, USB_REQTYPE_GETDESC, USB_REQ_GET_DESCRIPTOR,
            SWAP16(0x01), 0, sizeof(struct USB_DevDescr), (u32)d);
}

IOSError
IUSB_SuspendDevice(IOSFd fd)
{
    return IOS_Ioctl(fd, OHC_SUSPEND_MSG, NULL, 0, 0, 0);
}

IOSError
IUSB_ResumeDevice(IOSFd fd)
{
    return IOS_Ioctl(fd, OHC_RESUME_MSG, NULL, 0, 0, 0);
}

IOSError 
IUSB_GetDeviceList(const char *path, USBDeviceInfo *deviceList, u8 maxDev, 
        u8 deviceClass, u8 *numDev)
{
  IOSError rv = IOS_ERROR_OK;
  USBGetDevListReq req;

  IOSFd fd = IOS_Open(path, 0);
  if (fd < 0) {
      rv = fd;
      goto done;
  }
  
  req.deviceList = deviceList;
  req.maxDev = maxDev;
  req.deviceClass = deviceClass;
  req.numDev = numDev;
  rv = IOS_Ioctl(fd, OHC_GET_DEVLIST_MSG, &req, sizeof(req), 0, 0);
  IOS_Close(fd);
done:
  return rv;
}

#if 0
/* How to call ioctl directly */
IOSError
IUSB_GetDevDescr(IOSFd fd, struct USB_DevDescr *d)
{
    IOSError rv = IOS_ERROR_OK;
    USBCtrlReq req;
    req.bmRequestType = USB_REQTYPE_GETDESC;
    req.bRequest = USB_REQ_GET_DESCRIPTOR;
    req.wValue = 0x01; /* Device descr */
    req.wIndex = 0;
    req.wLength = SWAP16((u16)sizeof(struct USB_DevDescr));
    req.data = (u32)d;
    req.ep = 0;

    rv = IOS_Ioctl(fd, OHC_CTRL_MSG, &req, sizeof(USBCtrlReq), 0, 0);
    return rv;
}
#endif
