/*
 *               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>
#include "ohci.h"
#include "ohci_regs.h"
#include "ohci_core.h"

extern struct ohci_device __ohci_dev[OHCI_DEV_MAX]; 
void
initOHCIDevNodes(void)
{ 
  memset(__ohci_dev, 0, sizeof(__ohci_dev)); /* UNUSED == 0 */
}

s8 
getEndpointIdx(u8 devnum, u8 epAddr)
{
  s8 rv;
  for (rv = 1; rv < OHCI_DEV_MAX_EP; ++rv) 
     if (epAddr == __ohci_dev[devnum].dev.dev.ep[rv].ep) 
         goto done;
  rv = -1;
done:
  return rv;
}

void
setDevNodeAddr(u8 devnum, u8 addr)
{
    u32 flags;
    struct ohci_ed *ed0 = __ohci_dev[devnum].dev.dev.ep[0].ed;
    flags = OHCI_TO_CPU32(ed0->flags);
    flags |= (devnum << OHCI_ED_FA_SHIFT);
    ed0->flags = CPU_TO_OHCI32(flags);
    return;
}

void 
setEp0MaxP(u8 devnum, u16 maxP)
{
    u32 flags;
    struct ohci_ed *ed0 = __ohci_dev[devnum].dev.dev.ep[0].ed;
    flags = OHCI_TO_CPU32(ed0->flags);
    flags &= ~OHCI_ED_MPS_MASK;
    flags |= (maxP << OHCI_ED_MPS_SHIFT);
    ed0->flags = CPU_TO_OHCI32(flags);
    __ohci_dev[devnum].dev.dev.ep[0].maxP = maxP;
    return;
}
void    
setDevVid(u8 devnum, u16 idVendor)
{
  __ohci_dev[devnum].dev.dev.vid = idVendor;
}

void
setDevPid(u8 devnum, u16 idProduct)
{
 __ohci_dev[devnum].dev.dev.pid= idProduct;
}

void
setDevClass(u8 devnum, u8 devClass, u8 subClass, u8 protocol)
{
     __ohci_dev[devnum].dev.dev.devClass= devClass;
     __ohci_dev[devnum].dev.dev.subClass= subClass;
     __ohci_dev[devnum].dev.dev.protocol= protocol;
      OHCI_DBG("set class of device %u to %u\n", devnum, devClass);
}

void
setDevMaxPower(u8 devnum, u8 maxPower)
{
 __ohci_dev[devnum].dev.dev.maxPower = maxPower;
}

/* This is called once per interface. It doesn't check
 * if the interface is already present in the list.
 */
void 
setDevIntf(u8 devnum, u8 intf, u8 alt,
        u8 devClass, u8 subClass, u8 protocol)
{
    u8 nintf;
    struct usb_intf *devIntf;
    
    nintf  =  __ohci_dev[devnum].dev.dev.nintf;
    devIntf = &__ohci_dev[devnum].dev.dev.intf[nintf];
    if (0 == devIntf->devClass) {
        devIntf->intf = intf;
        devIntf->alt = alt;
        devIntf->subClass= subClass;
        devIntf->devClass= devClass;
        devIntf->protocol= protocol;
        ++__ohci_dev[devnum].dev.dev.nintf;
        OHCI_DBG("devnum = %u: intf = %u alt = %u class = %u subClass = %u protocol= %u\n", devnum, intf, alt, devClass, subClass, protocol);
    }
}

struct usb_intf *
getDevIntf(u8 devnum)
{
    return __ohci_dev[devnum].dev.dev.intf;
}

s16
getDeviceId(u16 vid, u16 pid)
{
    s16 devnum;
    for (devnum = 0; devnum < OHCI_DEV_MAX; ++devnum) {
       if ( ( __ohci_dev[devnum].dev.dev.vid == vid)
               && ( __ohci_dev[devnum].dev.dev.pid == pid)) {
           OHCI_DBG("devnum for vid = 0x%x pid = 0x%x = %d\n", vid, pid, devnum);
           goto done;
       }
    }
    devnum = -1; 
done:
    return devnum;
}

/* Send NULL vid, pid pointer, to just check valid devnum */
IOSError 
getVidPid(u8 devnum, u16 *vid, u16 *pid)
{
    IOSError rv = IOS_ERROR_OK;
    if ((devnum > OHCI_DEV_MAX) ||
         (OHCI_DEV_UNUSED == __ohci_dev[devnum].type)) {
        rv = IOS_ERROR_INVALID;
        goto done;
    }
    if (NULL != vid)
        *vid =  __ohci_dev[devnum].dev.dev.vid;
    if (NULL != pid)
        *pid =  __ohci_dev[devnum].dev.dev.pid; 
done:
    return rv;
}

u8 
getPortNum(u8 devnum)
{
  return  __ohci_dev[devnum].dev.dev.port;
}

u8
initDevNode(struct ohci_hcd *hcd, u8 port, u8 hub, u8 spd)
{
    u8 devnum = 0;
    u8 k;
    struct ohci_td * td0;
    struct ohci_ed * ed0;
    u32 flags;
    u16 maxP;
  
  for (devnum = 1; devnum < OHCI_DEV_MAX; ++devnum) {
    if (OHCI_DEV_UNUSED == __ohci_dev[devnum].type) {
        __ohci_dev[devnum].type = OHCI_DEV_DEV; /*XXX horti: adding hubs later */
        __ohci_dev[devnum].dev.dev.port = port;
        __ohci_dev[devnum].dev.dev.hub = hub;

        /* Initialize info for endpoint 0 */
        __ohci_dev[devnum].dev.dev.ep[0].ep =0;
        __ohci_dev[devnum].dev.dev.ep[0].ed = ed0 = allocED();
        __ohci_dev[devnum].dev.dev.ep[0].td_dummy = td0 = allocTD();

        __ohci_dev[devnum].dev.dev.spd = (spd == 0)? 0:1;
        if (spd == 1) { /* slow */
            __ohci_dev[devnum].dev.dev.ep[0].maxP = maxP = 8;
        }
        else {
            /* Initial guess. Can be 8, 16, 32, 64. 
             * Get device descriptor and set it later.
             */
            __ohci_dev[devnum].dev.dev.ep[0].maxP = maxP = 32;
        }
        for ( k = 1; k < OHCI_DEV_MAX_EP; ++k) 
            __ohci_dev[devnum].dev.dev.ep[k].ep = 0;
        __ohci_dev[devnum].dev.dev.nintf = 0;


        /* Add ed0 to HCD */
        ed0->headP = ed0->tailP = CPU_TO_OHCI32((u32) td0);
        ed0->nextED = 0;
        /* Initially set address of device = 0.
         * Set to actual address after a SET_ADDRESS operation
         * is completed.
         */
        flags =  OHCI_ED_DIR_TD1
            | ((spd << OHCI_ED_SPD_SHIFT) & OHCI_ED_SPD_MASK)
            | ((maxP << OHCI_ED_MPS_SHIFT) & OHCI_ED_MPS_MASK);
        ed0->flags = CPU_TO_OHCI32(flags);
        enqueueCtrlED(hcd, ed0);
        goto done;
    }
  }
done:
  return devnum;
}

IOSError
delDevNode(struct ohci_hcd *hcd, u8 port)
{
  IOSError rv = IOS_ERROR_INVALID;
  u8 devnum;
  for (devnum = 1; devnum < OHCI_DEV_MAX; ++devnum) {
    if (OHCI_DEV_DEV == __ohci_dev[devnum].type) {
        if (port == __ohci_dev[devnum].dev.dev.port) {
            OHCI_DBG("Deleting device node for port %u\n");
            freeED(__ohci_dev[devnum].dev.dev.ep[0].ed);
            freeTD(__ohci_dev[devnum].dev.dev.ep[0].td_dummy);
            memset(&__ohci_dev[devnum].dev, 0, sizeof(struct ohci_device));
            __ohci_dev[devnum].type = OHCI_DEV_UNUSED;
            rv = IOS_ERROR_OK;
            goto done;
        }
    }
  }
done:
  return rv;
}

/* Find at most 'size' devices of class 'class' and save the number of devices
 * found in 'num'.
 */

static void
__addDevInfo(OHCDeviceInfo *list, u8 *n, u32 i)
{
    list[*n].vid = __ohci_dev[i].dev.dev.vid;
    list[*n].pid = __ohci_dev[i].dev.dev.pid;
    *n = *n + 1;
}

IOSError
getDevList(OHCDeviceInfo *list, u8 size, u8 devClass, u8 *num)
{
  u32 i;

  *num = 0;
  for (i = 1; i < OHCI_DEV_MAX; ++i) { /* Exclude dev 0 (root hub) */
      if (OHCI_DEV_UNUSED == __ohci_dev[i].type) 
          continue;

      /* Check if dev descr has this class */
      if (devClass == __ohci_dev[i].dev.dev.devClass) {
          __addDevInfo(list, num, i);
          goto device_done;
      }
      /* Look in interface descrs */
      {
          struct usb_intf *intfList = __ohci_dev[i].dev.dev.intf;
          u8 nintf = __ohci_dev[i].dev.dev.nintf;
          u8 k;
          for (k = 0; k < nintf; ++k) { 
              if (devClass == intfList[k].devClass) {
                  __addDevInfo(list, num, i);
                  goto device_done;
              }
          }
      }
device_done:
      if (*num == size)
          break;
  }
  return IOS_ERROR_OK;
}
