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

#define USB_STD_LANG_EN_US 0x0409
#define OHCI_READ(regfield) \
        IO_READ(&(regfield))
#define OHCI_WRITE(regfield,value) \
        IO_WRITE(&(regfield), (value))

#define CPU_TO_OHCI32(val) \
	((u32)( \
		(((u32)(val) & (u32)0x000000ffUL) << 24) | \
		(((u32)(val) & (u32)0x0000ff00UL) <<  8) | \
		(((u32)(val) & (u32)0x00ff0000UL) >>  8) | \
		(((u32)(val) & (u32)0xff000000UL) >> 24) ))
   
#define OHCI_TO_CPU32(val) \
	((u32)( \
		(((u32)(val) & (u32)0x000000ffUL) << 24) | \
		(((u32)(val) & (u32)0x0000ff00UL) <<  8) | \
		(((u32)(val) & (u32)0x00ff0000UL) >>  8) | \
		(((u32)(val) & (u32)0xff000000UL) >> 24) ))

#define OHCI_TO_CPU16(val) \
	((u16)( \
		(((u16)(val) & (u16)0x00ffUL) << 8) | \
		(((u16)(val) & (u16)0xff00) >> 8) ))
#define CPU_TO_OHCI16(val) \
	((u16)( \
		(((u16)(val) & (u16)0x00ffUL) << 8) | \
		(((u16)(val) & (u16)0xff00) >> 8) ))

#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"

USBDeviceInfo deviceList[64];
u8 numDevices = 0;

static void
__dumpDevDescr(struct USB_DevDescr d)
{
  printf("USB Version(BCD): 0x%x\n", OHCI_TO_CPU16(d.bcdUSB));
  printf("DeviceClass: 0x%02x\n", d.bDeviceClass);
  printf("DeviceSubClass: 0x%02x\n", d.bDeviceSubClass);
  printf("DeviceProtocol: 0x%02x\n", d.bDeviceProtocol);
  printf("Max default endpoint size: 0x%02x\n", d.bMaxPacketSize0);
  printf("Number of configurations = %u\n", d.bNumConfigurations);
  printf("Vendor id: 0x%x\n", OHCI_TO_CPU16(d.idVendor));
  printf("Product id: 0x%x\n", OHCI_TO_CPU16(d.idProduct));
  printf("Device release number: 0x%x\n", OHCI_TO_CPU16(d.bcdDevice));
}

static void
__dumpDevices(IOSFd fd)
{
    struct USB_DevDescr d;
    char buf[256];
    IUSB_GetDevDescr(fd, &d); 

    if (d.iProduct) {
        IUSB_GetAsciiStr(fd, 0, d.iProduct, USB_STD_LANG_EN_US, buf, 255);
        printf("Product: %s\n", buf);
    }
    if (d.iManufacturer) {
        IUSB_GetAsciiStr(fd, 0, d.iManufacturer, USB_STD_LANG_EN_US, buf, 255);
        printf("Manufacturer: %s\n", buf);
    }
    if (d.iSerialNumber) {
        IUSB_GetAsciiStr(fd, 0, d.iSerialNumber, USB_STD_LANG_EN_US, buf, 255);
        printf("Serial number: %s\n", buf);
    }

    __dumpDevDescr(d);
} 

static IOSError 
__getDeviceList(void)
{
    IOSError rv = IUSB_GetDeviceList("/dev/usb/oh0", deviceList, 
            sizeof(deviceList)/sizeof(USBDeviceInfo), 0, &numDevices);
    if (rv < 0) {
        printf("Get device list failed: %d\n", rv);
        goto done;
    }
    if (0 == numDevices) {
      printf("No devices connected\n");
      goto done;
  }
  { 
      u32 i;
      printf("Detected %d devices\n", numDevices);
      for (i = 0; i < numDevices; ++i) {
          printf("\tdevice %u vid = %x pid = %x\n", i, deviceList[i].vid,
                  deviceList[i].pid);
      }
  }
done:
  return rv;
}

#ifdef ENABLE_TEST
void
ohci_test(u32 arg)
#else
void
main(u32 arg)
#endif
{
    usleep(100000); /* Wait for all devices to be enumerated */
    __getDeviceList();
    { 
        u32 i;
        IOSFd fd;
        char path[64];
        for (i = 0; i < numDevices; ++i) {
            memset(path, 0, sizeof(path));
            snprintf(path, sizeof(path),"/dev/usb/oh0/%x/%x",
                    deviceList[i].vid, deviceList[i].pid);
            fd = IOS_Open(path, 0);
            if (fd < 0) {
                printf("Failed to open %s: %d\n", path, fd);
                continue;
            }
            printf("-----------------------------------------------\n");
            printf("Device %u: Vendor Id  = 0x%x Product Id = 0x%x\n", 
                    i, deviceList[i].vid, deviceList[i].pid);
            __dumpDevices(fd);
            printf("-----------------------------------------------\n");
        }
    }
    IOS_DestroyThread(0, NULL);
    return;
}
