/*
 *               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, u8 ep, u16 buflen, char *buf, u32 typ)
{
    IOSError rv = IOS_ERROR_OK;
    IOSIoVector vector[3];

    if (0 == buflen || NULL == buf ||
            (OHC_INTR_MSG != typ && OHC_BLK_MSG != typ)) {
        rv = IOS_ERROR_INVALID;
        goto done;
    }

    vector[0].base = &ep; 
    vector[0].length = sizeof(u8);
    

    vector[1].base = (u8 *)&buflen; 
    vector[1].length = sizeof(u16);
    
    vector[2].base = (u8*) buf; 
    vector[2].length = buflen;
    rv = IOS_Ioctlv(fd, typ, 2, 1, vector);

done:
    return rv;
}

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

s32
IUSB_WriteIntrMsg(IOSFd fd, u32 ep, u32 buflen, char *buf)
{
    return __IntrBlkMsg(fd, (u8)ep, (u16)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;
    u8 ep = 0;
    IOSIoVector vector[7];
    u16 svalue = SWAP16(value);
    u16 sindex= SWAP16(index) ;
    u16 slength= SWAP16(length);

    vector[0].base = &reqType; /*bmRequestType */
    vector[0].length = sizeof(u8);
    
    vector[1].base = &request; /*bRequest */
    vector[1].length = sizeof(u8);
    
    vector[2].base =  (u8 *) &svalue; /* wValue */
    vector[2].length = sizeof(u16);

    vector[3].base = (u8 *) &sindex;  /* wIndex */
    vector[3].length = sizeof(u16);

    vector[4].base = (u8 *) &slength; /* wLength */
    vector[4].length = sizeof(u16);
    
    vector[5].base =  &ep;    /* endpoint number */
    vector[5].length = sizeof(u8);

    vector[6].base = (u8 *)data; 
    vector[6].length = length;

    rv = IOS_Ioctlv(fd, OHC_CTRL_MSG, 6, 1, vector);
//    printf("ioctlv (fd %d) returned %d\n", fd, rv);
    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[IOS_USB_STR_MAX];
    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);
}

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;
  IOSIoVector vector[4]; 

  if (NULL == path || NULL == deviceList || NULL == numDev) {
      rv = IOS_ERROR_INVALID;
      goto done;
  }
  
  IOSFd fd = IOS_Open(path, 0);
  if (fd < 0) {
      rv = fd;
      goto done;
  }
 
  vector[0].base = &maxDev;
  vector[0].length = sizeof(u8);
  vector[1].base = &deviceClass;
  vector[1].length = sizeof(u8);
  vector[2].base = (u8 *)numDev;
  vector[2].length = sizeof(u8);
  vector[3].base = (u8 *)deviceList;
  vector[3].length = maxDev*sizeof(USBDeviceInfo);

  rv = IOS_Ioctlv(fd, OHC_GET_DEVLIST_MSG, 2, 2, vector);
  IOS_Close(fd);

done:
  return rv;
}

#if defined (ENABLE_TEST_FUNCTIONS)
IOSError 
IUSB_GetRhDesca(IOSFd fd, u32 *desca)
{
  IOSError rv = IOS_ERROR_OK;

  if (NULL == desca) {
      rv = IOS_ERROR_INVALID;
      goto done;
  }
  rv = IOS_Ioctl(fd, OHC_GET_RHDESCA_MSG, 0, 0, desca, sizeof(u32));
done:
  return rv;
}

static IOSError
__getsetRhPortStat(IOSFd fd, u32 cmd, u8 *v0, u32 s0,
        u8 *v1, u32 s1, u32 rd, u32 wr)
{
  IOSError rv;
  IOSIoVector vector[2];
  if (NULL == v0 || NULL == v1) {
      rv = IOS_ERROR_INVALID;
      goto done;
  }
  vector[0].base = v0;
  vector[0].length = s0;
  vector[1].base = v1;
  vector[1].length = s1;
  rv = IOS_Ioctlv(fd, cmd, rd, wr, vector);
done:
  return rv;
}

IOSError 
IUSB_GetRhPortStatus(IOSFd fd, u8 port, u32 *stat)
{
  u8 p __attribute__((aligned(32)));
  p = port;
  return __getsetRhPortStat(fd, OHC_GET_RHPSTAT_MSG, (u8 *)&p, sizeof(u8), 
          (u8 *)stat, sizeof(u32), 1, 1);
}

IOSError 
IUSB_SetRhPortStatus(IOSFd fd, u8 port, u32 stat)
{
  return __getsetRhPortStat(fd, OHC_SET_RHPSTAT_MSG, (u8 *)&port, sizeof(u8),
          (u8 *) &stat, sizeof(u32), 2, 0);
}
#endif /* ENABLE_TEST_FUNCTIONS */

IOSError 
IUSB_RegisterRemovalNotify(IOSFd fd, IOSMessageQueueId mqid, IOSResourceRequest *reply)
{
  return IOS_IoctlAsync(fd, OHC_NOTIFYREMOVAL_MSG, 0, 0, 0, 0, mqid, reply);
}
