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

u8 stack[1024];

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

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

void
thd(u32 fd)
{
    u8 data[8];
    __intrTransferTest((IOSFd)fd, data);
}

#ifdef ENABLE_TEST
void
ohci_test(u32 arg)
#else
void
main(u32 arg)
#endif
{
    IOSFd fd0;
    u32 id;
    /* XXX debug:
     * Started before driver in this test.
     * Wait for driver to come up.
     * The vid, pid are for a logitech optical mouse.
     */
    do {
        fd0 = IOS_Open("/dev/usb/oh0/413c/3200", 0);
        printf("fd0 = %d\n", fd0);
    }
    while (fd0 < 0);

    id = IOS_CreateThread((IOSEntryProc)thd, (void *) fd0, stack+1024, 1024, IOS_GetThreadPriority(0), IOS_THREAD_CREATE_DETACHED);
    IOS_StartThread(id);

    usleep(100);
    IOS_Close(fd0);
    usleep(1000);
    fd0 = IOS_Open("/dev/usb/oh0/413c/3200", 0);
    printf("new fd0 = %d\n", fd0);

    printf("mouse: main thread sleeping for 10s\n");
    usleep(10000000);
    printf("mouse: driver exiting \n");
    IOS_DestroyThread(0, NULL);
    return;
}
