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

/* Test for bug 544 */
#include "iosusbtypes.h"
#include "iosusb.h"

void
__ctrlTransferTests(IOSFd fd)
{
    u8 buf[IOS_USB_STR_MAX];
    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);
}
 
void
__registerRemovalHandlerTest(IOSFd fd, IOSMessageQueueId qid, 
        IOSResourceRequest reply)
{
    IOSError rv;
    rv = IUSB_RegisterRemovalNotify(fd, qid, &reply);
    if (rv < 0) {
        printf("registering removal notification handler failed: %d\n", rv);
    }
    else {
        printf("registering removal notification handler succeeded: %d\n", rv);
        /* Don't wait for device removal */  
    }
}

void
__bug544Test(void)
{
  u8 numDev;
  u32 i, j;
  IOSError rv;
  
  USBDeviceInfo deviceList[8];
  IOSFd fd[8];
  IOSMessageQueueId qid[8];
  IOSMessage msgQ[8];
  IOSResourceRequest reply[8]; 

  
  rv = IUSB_GetDeviceList("/dev/usb/oh0", deviceList, 8, 0, &numDev);
  if (rv < 0) {
      printf("Get device list failed: %d\n", rv);
      goto done;
  }
  for (i = 0; i < 8; ++i) {
          fd[i] = -1;
          qid[i] = IOS_CreateMessageQueue(&msgQ[i], 1);
  }
  for (j = 0; j < 25; ++j) {
      for (i = 0; i < numDev; ++i) {
          char path[128];
          printf("Test %u:\n", j);
          printf("\tDevice %u vid = %x pid = %x\n", i, deviceList[i].vid,
                  deviceList[i].pid);
          memset(path, 0, sizeof(path));
          snprintf(path, 127, "/dev/usb/oh0/%x/%x", 
                  deviceList[i].vid, deviceList[i].pid);
          fd[i] = IOS_Open(path, 0);
          if (fd[i] < 0) {
              printf("Opening %s failed: %d\n", path, fd[i]);
          } else {
              printf("Opening %s succeeded: %d\n", path, fd[i]);
              __registerRemovalHandlerTest(fd[i], qid[i], reply[i]);
              __ctrlTransferTests(fd[i]);
          }
      }
      for (i = 0; i < numDev; ++i) {
          if (fd[i] >= 0) {
              IOS_Close(fd[i]);
              fd[i] = -1;
          }
      }
  }
done:
  return;
}

#ifdef ENABLE_TEST
void
ohci_test(u32 arg)
#else
void
main(u32 arg)
#endif
{
    usleep(1000000); /* wait for devices to be enumerated */
    __bug544Test();
    IOS_DestroyThread(0, NULL);
    return;
}
