PLX_DeviceList.cs
上传用户:huajielb
上传日期:2022-07-29
资源大小:626k
文件大小:5k
源码类别:

驱动编程

开发平台:

Visual C++

  1. using System;
  2. using System.Collections;
  3. using Jungo.wdapi_dotnet;
  4. using wdc_err = Jungo.wdapi_dotnet.WD_ERROR_CODES;
  5. using DWORD = System.UInt32;
  6. using BOOL = System.Boolean;
  7. using WDC_DRV_OPEN_OPTIONS = System.UInt32;    
  8. namespace Jungo.plx_lib
  9. {
  10.     public class PLX_DeviceList: ArrayList
  11.     {
  12.         private string PCI_DEFAULT_LICENSE_STRING  = "12345abcde1234.license";
  13.         private static PLX_DeviceList instance;
  14.         public static PLX_DeviceList TheDeviceList()
  15.         {
  16.             if (instance == null)
  17.             {
  18.                 instance = new PLX_DeviceList();
  19.             }
  20.             return instance;
  21.         }
  22.         private PLX_DeviceList(){}
  23.         public DWORD Init()
  24.         {
  25.             DWORD dwStatus = wdc_lib_decl.WDC_SetDebugOptions(wdc_lib_consts.WDC_DBG_DEFAULT,
  26.                 null);
  27.             if (dwStatus != (DWORD)wdc_err.WD_STATUS_SUCCESS)
  28.             {
  29.                 Log.ErrLog("PLX_DeviceList: Failed to initialize debug options for " +
  30.                     " WDC library. Error 0x" + dwStatus.ToString("X") + 
  31.                     utils.Stat2Str(dwStatus));        
  32.                 return dwStatus;
  33.             }  
  34.             
  35.             dwStatus = wdc_lib_decl.WDC_DriverOpen(
  36.                 (WDC_DRV_OPEN_OPTIONS)wdc_lib_consts.WDC_DRV_OPEN_DEFAULT,
  37.                 PCI_DEFAULT_LICENSE_STRING);
  38.             if (dwStatus != (DWORD)wdc_err.WD_STATUS_SUCCESS)
  39.             {
  40.                 Log.ErrLog("PLX_DeviceList: Failed to initialize the WDC library. "
  41.                     + "Error 0x" + dwStatus.ToString("X") + utils.Stat2Str(dwStatus));
  42.                 return dwStatus;
  43.             }            
  44.             return (DWORD)wdc_err.WD_STATUS_SUCCESS;
  45.         }
  46.         public PLX_Device Get(int index)
  47.         {
  48.             if(index >= this.Count || index < 0)
  49.                 return null;
  50.             return (PLX_Device)this[index];
  51.         }
  52.         public PLX_Device Get(WD_PCI_SLOT slot)
  53.         {
  54.             foreach(PLX_Device device in this)
  55.             {
  56.                 if(device.IsMySlot(ref slot))
  57.                     return device;
  58.             }
  59.             return null;
  60.         }
  61.         public int Populate(DWORD dwVendorId, DWORD dwDeviceId)
  62.         {
  63.             return Populate(dwVendorId, dwDeviceId, "");
  64.         }
  65.         public int Populate(DWORD dwVendorId, DWORD dwDeviceId, string sKP_DRIVER_NAME)
  66.         {
  67.             int retIndex=0;
  68.             DWORD dwStatus;
  69.             WDC_PCI_SCAN_RESULT scanResult = new WDC_PCI_SCAN_RESULT();
  70.             dwStatus = wdc_lib_decl.WDC_PciScanDevices(dwVendorId, 
  71.                 dwDeviceId, scanResult);
  72.             if ((DWORD)wdc_err.WD_STATUS_SUCCESS != dwStatus)
  73.             {
  74.                 Log.ErrLog("PLX_Sample.FindPLXDevices: Failed scanning "
  75.                     + "the PCI bus");
  76.                 return -1;
  77.             }
  78.             if (scanResult.dwNumDevices == 0)
  79.             {
  80.                 Log.ErrLog("PLX_Sample.FindPLXDevices: No matching PLX " +
  81.                     "device was found for search criteria " + dwVendorId.ToString("X") 
  82.                     + ", " + dwDeviceId.ToString("X"));
  83.                 return -1;
  84.             }
  85.             for (int i = 0; i < scanResult.dwNumDevices; ++i)
  86.             {
  87.                 PLX_Device device;
  88.                 WD_PCI_SLOT slot = scanResult.deviceSlot[i];
  89.                 if(PLX_Device.IsMasterDevice(ref slot))
  90.                 {
  91.                     device = new
  92.                         PLX_MasterDevice(scanResult.deviceId[i].dwVendorId,
  93.                             scanResult.deviceId[i].dwDeviceId, slot, sKP_DRIVER_NAME);
  94.                 }
  95.                 else
  96.                 {
  97.                     device = new
  98.                         PLX_TargetDevice(scanResult.deviceId[i].dwVendorId,
  99.                             scanResult.deviceId[i].dwDeviceId, slot, sKP_DRIVER_NAME);
  100.                 }
  101.                 retIndex = this.Add(device);                                
  102.             }                        
  103.             return retIndex;
  104.         }
  105.         public void Dispose()
  106.         {
  107.             foreach (PLX_Device device in this)
  108.                 device.Dispose();
  109.             this.Clear();
  110.             DWORD dwStatus = wdc_lib_decl.WDC_DriverClose();
  111.             if(dwStatus != (DWORD)wdc_err.WD_STATUS_SUCCESS)
  112.             {
  113.                 Exception excp = new Exception("PLX_Sample. PLX_Sample_Closing: " +
  114.                     "Failed to uninit the WDC library. Error 0x" +
  115.                     dwStatus.ToString("X") + utils.Stat2Str(dwStatus));
  116.                 throw excp;
  117.             }
  118.         }
  119.     };
  120. }
  121.