/*
 *               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.
 *
 */
/*  thread 0: blinks LEDs in Cherry keyboard
 *  thread 1: Read interrupt messages and print raw data.
 */

#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 rdrStack[1024];
static IOSFd fd0;

#include "iosusbtypes.h"
#include "iosusb.h"


static IOSMessage mq[4];
static IOSMessageQueueId qid = -1;
static IOSTimerId t = -1;
static u8 data = 0x21;

static s32 
__blinkLED(IOSFd fd)
{
    s32 rv;

    /* Initialize message queue and timer */
    if (qid < 0) {
        qid = IOS_CreateMessageQueue(mq, sizeof(mq)/sizeof(IOSMessage));
        if (qid < 0) {
            printf("Creating message queue failed: %d\n", qid);
            rv = (s32) qid;
            goto done;
        }
    }
    if (t < 0) {
        t = IOS_CreateTimer(0, 0, qid, IUSB_TIMEOUT_MESSAGE);
        if (t < 0) {
            printf("Creating timer failed: %d\n", t);
            rv = (s32) t;
            goto done;
        }
    }

    rv = IUSB_CtrlMsgWithTimeout(fd, 0x21, 0x09, 0x0200, 0, 1, (u32)&data, 
            100000, qid, t);
    if (rv < 0) {
        printf("control write failed: %d\n", rv);
    }
    if (data & 0x01) { data = 0x20; }
    else { data = 0x21; }
done:
    return rv;
}

void
rdr(u32 arg)
{
    IOSError rv; 
    u8 data[8];
    u32 i;
    while (1) {
        if ((rv = IUSB_ReadIntrMsg(fd0, 0x81, 8, data)) <= 0) {
            printf("Read Intr Msg failed: %d\n", rv);
            usleep(100000); /* main thread will reset dev & reopen fd */
        } else {
            for (i = 0; i < 8; ++i) {
                printf("%x", data[i]);
            }
            printf("\n");
        }
    }
}

#ifdef ENABLE_TEST
void
ohci_test(u32 arg)
#else
void
main(u32 arg)
#endif
{
    usleep(100000); /* wait for device enumeration etc. */
    fd0 = IOS_Open("/dev/usb/oh0/046a/0001", 0);
    if (fd0 < 0) {
        printf("open failed: %d\n", fd0);
        goto done;
    }

    {
        u32 id = IOS_CreateThread((IOSEntryProc) rdr, (void *) fd0, 
                rdrStack + 1024, 1024, IOS_GetThreadPriority(0), IOS_THREAD_CREATE_DETACHED);
        IOS_StartThread(id);
    }
    while (1) {
        if (__blinkLED(fd0) < 0) {
            printf("resetting device\n");
            IUSB_ResetDevice(fd0);
            IOS_Close(fd0);
            fd0 = IOS_Open("/dev/usb/oh0/046a/0001", 0);
            if (fd0 < 0) {
                printf("open failed: %d\n", fd0);
                goto done;
            }
        }
        usleep(40000);
    }
done:
    IOS_DestroyThread(0, NULL);
    return;
}
