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

#ifndef ENABLE_TEST
#define STACK_SIZE 2048
const u8        _initStack[STACK_SIZE];
const u32       _initStackSize = sizeof(_initStack);
const u32       _initPriority = 20;
#endif /* ENABLE_TEST */

/* Sample mouse driver */
#include "iosusbtypes.h"
#include "iosusb.h"

static void
__ctrlTransferTests(IOSFd fd)
{
    u8 buf[64];
    struct USB_DevDescr d;
    IUSB_GetDevDescr(fd, &d); 

    printf("--- Ctrl transfer tests \n");
    if ( (d.iManufacturer != 0 )
            && (0 < IUSB_GetAsciiStr(fd, 0, d.iManufacturer, 
                    USB_LANG_EN_US, buf, sizeof(buf))))
        printf("Manufacturer: %s\n", buf);
    if ( (d.iProduct != 0)
            && (0 < IUSB_GetAsciiStr(fd, 0, d.iProduct, 
                    USB_LANG_EN_US, buf, sizeof(buf))) ) 
        printf("Product: %s\n", buf);

    if ( (d.iSerialNumber != 0)
            && (0 < IUSB_GetAsciiStr(fd, 0, d.iSerialNumber, 
                    USB_LANG_EN_US, buf, sizeof(buf))) ) 
        printf("Serial number: %s\n", buf);
}


static void 
__getConfDescr(IOSFd fd, u8 *buf, u16 size)
{
    IUSB_ReadCtrlMsg(fd, 0x80, 0x06, SWAP16(0x0002), 0, size, buf);
}

static void 
__confDescrTest(IOSFd fd, u8 *buf, u16 size)
{
    u8 i;
    u8 dbuf[32]; /* size < 32 */
    __getConfDescr(fd, buf, size);
    __getConfDescr(fd, dbuf, size);
    for ( i = 0; i < size; ++i) {
     if (buf[i] != dbuf[i]) printf("***ERROR:  ");
     
     printf("    %u: DDR[%p] = 0x%02x  SRAM[%p] = 0x%02x\n",
             i, dbuf+i, dbuf[i], buf+i,  buf[i]);
    }
}


static void
__test1byteTransfer(IOSFd fd, u8 *buf)
{
    printf("--- 1 byte transfer test: \n");
    printf("    Get 9 bytes of conf descr in: %p\n", buf);
    __confDescrTest(fd, buf, 9);
}

static void
__test2byteTransfer(IOSFd fd, u8 *buf)
{
    printf("--- 2 byte transfer test: \n");
    printf("    Get 18 bytes of conf descr in supplied buffer : %p\n", buf);
    __confDescrTest(fd, buf, 18);
}

static void
__test4byteTransfer(IOSFd fd, u8 *buf)
{
    printf("--- 4 byte transfer test: \n");
    printf("    Get 20 bytes of conf descr in supplied buffer : %p\n", buf);
    __confDescrTest(fd, buf, 20);
}

/* Interrupt transfer test */
static void
__intrTransferTest(IOSFd fd, u8 *data)
{
    u8 i;
    u8 k;
    printf("--- Interrupt transfer tests to dstn buffer: %p\n", data);
    for (i = 0; i < 4; ++i) {
        printf("mouse: waiting for data sample %u: \n", i);
        memset(data, 0, 8);
        IUSB_ReadIntrMsg(fd, 0x81, 8, data);
        for (k = 0; k < 8; ++k )
            printf("mouse: data[0x%08x] = 0x%02x\n", data + k, data[k] & 0xff);
    }
}

/* Generate error */
#ifdef GENERATE_CTRL_ERR
static void
__genError(IOSFd fd0)
{
    USBCtrlReq req;

    req.bmRequestType = 0x80; /* USB_REQTYPE_GETDESC */
    req.bRequest = 0xff; /* junk */
    req.wValue = 0;
    req.wIndex = 0;
    req.wLength = SWAP16(24); /* junk */
    req.data = (u32)0;
    req.ep = 0; /* junk */
    printf("sending junk ioctl\n");
    IOS_Ioctl(fd0, OHC_CTRL_MSG, &req, sizeof(USBCtrlReq), 0, 0);
}
#endif /* GENERATE_CTRL_ERR */

void
__getDevListTest(void)
{
  u8 numDev;
  u8 class = 0x03;
  u8 i = 0;
  IOSError rv;
  USBDeviceInfo deviceList[8];
  rv = IUSB_GetDeviceList("/dev/usb/oh0", deviceList, 8, class, &numDev);
  if (rv < 0) {
      printf("Get device list failed: %d\n", rv);
      goto done;
  }
  printf("Found %u device(s) of class %u\n", numDev, class);
  for (i = 0; i < numDev; ++i) {
      printf("device %u vid = %x pid = %x\n", i, deviceList[i].vid,
              deviceList[i].pid);
  }
done:
  return;
}


#ifdef ENABLE_TEST
void
ohci_test(u32 arg)
#else
void
main(u32 arg)
#endif
{
    IOSFd fd0;
#if OH0
    u8 data[8];
    u8 data1[8];
    u8 data2[32];
    u8 data3[32];
#endif
    /* XXX debug:
     * Started before driver in this test.
     * Wait for driver to come up.
     * The vid, pid are for a logitech optical mouse.
     */
    do {
#if OH0
        fd0 = IOS_Open("/dev/usb/oh0/046d/c016", 0);
#elif OH1
        fd0 = IOS_Open("/dev/usb/oh1/046d/c016", 0); 
#endif
    }
    while (fd0 < 0);

    __getDevListTest();    
#ifdef GENERATE_CTRL_ERR
    __genError(fd0);
#endif /* GENERATE_CTRL_ERR */

#if OH0
    __intrTransferTest(fd0, data);
    __test1byteTransfer(fd0, data1);
    __test2byteTransfer(fd0, data3);
    __test4byteTransfer(fd0, data2);
#endif

#if OH1  /* Try writing to buffer in SRAM */
    {
        u32 reg;
        u8 *sdata = (u8 *) 0xfffffff0;
        u8 *sdata1 = (u8 *) 0xfffffff0;
        u8 *sdata2 = (u8 *) 0xffffff00;
        u8 *sdata3 = (u8 *) 0xfffffe00;

        /* Setup SRN permission */
        reg = busRd32(ACRSRNPROT_REG);
        reg |= ACRSRNPROT_OH1EN_MASK;
        busWrt32(ACRSRNPROT_REG, reg);
        
        __intrTransferTest(fd0, sdata);
        __test1byteTransfer(fd0, sdata1);
        __test2byteTransfer(fd0, sdata2);
        __test4byteTransfer(fd0, sdata3);
    }
#endif /* OH1 */

    __ctrlTransferTests(fd0);
    IUSB_SuspendDevice(fd0);
    usleep(5000000);
    IUSB_ResumeDevice(fd0);
    printf("mouse: driver exiting \n");
    IOS_DestroyThread(0, NULL);
    return;
}
