/*****************************************************************************
 * Copyright (C) Cinterion Wireless Modules GmbH
 *****************************************************************************
 * Project: Quinn-CE-USB-Driver
 *****************************************************************************
 * Author : Siemens AG Berlin COM WM RD SW
 *****************************************************************************
 * $Workfile: D:/develop/ccviews/ctna0152_ms-d_view/z_ms-d/src/working/Quinn/HcUsbDrv/UsbDriver.cpp $
 * $Revision: /main/ms-d/7 $
 * $Date: 2011-02-14 14:19:12 $
 * $Author: ctna0152 $
 *****************************************************************************
 * Target:
 *  Microsoft Windows CE 5.0
 *****************************************************************************
 * Description:
 *  Quinn-USB-Driver: Implementation of the class CUsbDriver
 *  doing the global USB driver handling.
 *****************************************************************************
 *
 * $Log: D:/develop/ccviews/ctna0152_ms-d_view/z_ms-d/src/working/Quinn/HcUsbDrv/UsbDriver.cpp$ 
 * 
 * /main/ms-d/7   2011-02-14 14:19:12   ctna0152 
 * fix for IPIS100069778
 * removed USE_DEVICE_REQUEST_REDIRECT encapsulation 
 * 
 * /main/ms-d/6   2011-02-02 10:27:47   ctna0450 
 * Removed unused USB-Reset workaround for Uranus USB problems
 *
 * /main/ms-d/5   2010-12-13 14:22:01   ctna0152
 * added USB reset handling
 *
 * /main/ms-d/4   2010-08-17 14:40:11   ctna0450
 * Use compiler flag for "attach before detach" problem to reenable capability for more than one instance handling
 *
 * /main/ms-d/3   2009-02-18 11:49:32   ctna0256
 * Set copyright text
 *
 * /main/ms-d/2   2008-08-20 11:39:52   ctna0450
 * Prepared for new disclaimer handling
 *
 * /main/ms-d/1   2007-07-31 11:33:43   ht37010
 *
*/

//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Cinterion Wireless Modules GmbH
// Copyright (C) 2006-2008 Siemens AG
//
// Version: 1.0
// Date:    dummy
//
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//
// SelectiveSuspend.cpp
//
// This file contains the implementation of the class "CUsbDriver"
// which implements the global handling of the USB driver.
//
//////////////////////////////////////////////////////////////////////////////

#include "precomp.h"

//////////////////////////////////////////////////////////////////////////////

#ifdef ATTACH_BEFORE_DETACH
tPortList CUsbDriver::m_pActivatedPorts[HC_MAX_USB_IFACES] = 
  { {HC_UNKNOWN, -1, NULL, NULL } };
#endif // ATTACH_BEFORE_DETACH

//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////
BOOL CUsbDriver::AddActivatedPort(HC_DEVICE_TYPE DevType, DWORD dwDeviceIndex, 
          CVirtualPort *pVirtPort, CUsbHandling * pUsbHandling) {
#ifdef ATTACH_BEFORE_DETACH
  for (int i= 0; i < HC_MAX_USB_IFACES; i++) {
    if ( m_pActivatedPorts[i].pVirtPort == NULL ) {
      // found free entry ...
      m_pActivatedPorts[i].DevType = DevType;
      m_pActivatedPorts[i].dwDeviceIndex = dwDeviceIndex;
      m_pActivatedPorts[i].pVirtPort = pVirtPort;
      m_pActivatedPorts[i].pUsbHandling = pUsbHandling;
      DEBUGMSG(1, (TEXT("HcUsb:  CUsbDriver::AddActivatedPort(): idx=%d DevType=%i, DevIdx=%d\r\n"), i, DevType, dwDeviceIndex));
      return TRUE;
    }
  }
#endif // ATTACH_BEFORE_DETACH
  return FALSE;
}

//////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////
CVirtualPort *CUsbDriver::GetActivatedPort(HC_DEVICE_TYPE DevType, DWORD dwDeviceIndex) {
#ifdef ATTACH_BEFORE_DETACH
  DEBUGMSG(1, (TEXT("HcUsb:  CUsbDriver::GetActivatedPort(): DevType=%i, DevIdx=%d\r\n"), DevType, dwDeviceIndex));
  for (int i= 0; i < HC_MAX_USB_IFACES; i++) {
    if ((m_pActivatedPorts[i].DevType == DevType) && 
        (m_pActivatedPorts[i].dwDeviceIndex == dwDeviceIndex)) {
      // found port for IfaceNum ...
      DEBUGMSG(1, (TEXT("HcUsb:  CUsbDriver::GetActivatedPort(): idx=%i\r\n"), i));
      return m_pActivatedPorts[i].pVirtPort;
    }
  }
#endif // ATTACH_BEFORE_DETACH
  return NULL;
}

//////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////
BOOL CUsbDriver::RemoveActivatedPort(CVirtualPort *pVirtPort) {
#ifdef ATTACH_BEFORE_DETACH
  for (int i= 0; i < HC_MAX_USB_IFACES; i++) {
    if (m_pActivatedPorts[i].pVirtPort == pVirtPort) {
      // found port to remove ...
      m_pActivatedPorts[i].DevType = HC_UNKNOWN;
      m_pActivatedPorts[i].dwDeviceIndex = -1;
      m_pActivatedPorts[i].pUsbHandling = NULL;
      m_pActivatedPorts[i].pVirtPort = NULL;
      return TRUE;
    }
  }
#endif // ATTACH_BEFORE_DETACH
  return FALSE;
}

