⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.c

📁 usbhostDriver, 设计USB的目标就是使不同厂家所生产的设备可以在一个开放的体系下广泛的使用。该规范改进了便携商务或家用电脑的现有体系结构
💻 C
字号:
/*
**********************************************************************
*                          Micrium, Inc.
*                      949 Crestview Circle
*                     Weston,  FL 33327-1848
*
*                           uC/USB-Bulk
*
*             (c) Copyright 2003 - 2004, Micrium, Inc.
*                      All rights reserved.
*
***********************************************************************

----------------------------------------------------------------------
File        : usb.c
Purpose     : Part of the USB bulk driver
---------------------------END-OF-HEADER------------------------------
*/

#include "Main.h"

GLOBALS Globals;

/*********************************************************************
*
*       _DriverUnload
*/
static void _DriverUnload(IN DRIVER_OBJECT * pDriverObj) {
  DPRINT(("<usb> -> _DriverUnload\n"));

  if (Globals.RegPath.Buffer) {
    ExFreePool(Globals.RegPath.Buffer);
    Globals.RegPath.Buffer = NULL;
    DPRINT(("<usb>    Buffer freed\n"));
  }
  DPRINT(("<usb> <- _DriverUnload\n"));
}

/*********************************************************************
*
*       _AddDevice
*/
static NTSTATUS _AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT PhysicalDeviceObject) {
  NTSTATUS           ntS;
  DEVICE_OBJECT *    deviceObject;
  DEVICE_EXTENSION * pDevExt;
  POWER_STATE        state;
  UNICODE_STRING     uDevPath;
  UNICODE_STRING     uDevUsrPath;

  DPRINT(("<usb> -> _AddDevice\n"));

  deviceObject = NULL;
  RtlInitUnicodeString(&uDevPath,    DEVICE_NAME);
  RtlInitUnicodeString(&uDevUsrPath, DOS_DEVICE_NAME);

  ntS = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), &uDevPath, FILE_DEVICE_UNKNOWN, 0, FALSE, &deviceObject);
  if(!NT_SUCCESS(ntS)) {
      DPRINT(("<usb> AddDevice::IoCreateDevice error %x\n", ntS));
      return(ntS);
  }
  
  ntS = IoCreateSymbolicLink(&uDevUsrPath, &uDevPath);

  // init dev ext
  pDevExt = (DEVICE_EXTENSION *) deviceObject->DeviceExtension;
  pDevExt->FunctionalDeviceObject = deviceObject;
  pDevExt->PhysicalDeviceObject = PhysicalDeviceObject;
  deviceObject->Flags |= DO_DIRECT_IO;

  KeInitializeSpinLock(&pDevExt->DevStateLock);
  INITIALIZE_PNP_STATE(pDevExt);

  pDevExt->OpenHandleCount = 0;
  //
  // init queue
  //
  pDevExt->QueueState = HoldRequests;
  InitializeListHead(&pDevExt->NewRequestsQueue);
  KeInitializeSpinLock(&pDevExt->QueueLock);
  //
  // Init "RemoveEvent"
  KeInitializeEvent(&pDevExt->RemoveEvent, SynchronizationEvent, FALSE);   
  //
  // Init "StopEvent", signaled when OutStandingIO = 1
  //
  KeInitializeEvent(&pDevExt->StopEvent, SynchronizationEvent, TRUE);
  // 1 --> 0 = removing device
  // ? --> 1 = device is stoppable
  pDevExt->OutStandingIO = 1;
  KeInitializeSpinLock(&pDevExt->IOCountLock);
  KeInitializeSpinLock(&pDevExt->BufferLock);
  //
  // copy underlying pdo flags
  //
  if (PhysicalDeviceObject->Flags & DO_POWER_PAGABLE) {
    deviceObject->Flags |= DO_POWER_PAGABLE;
  }
  //
  // init power info
  //
  pDevExt->DevPower = PowerDeviceD0;
  pDevExt->SysPower = PowerSystemWorking;
  state.DeviceState = PowerDeviceD0;
  PoSetPowerState(deviceObject, DevicePowerState, state);
  //
  // attach device
  //
  pDevExt->TopOfStackDeviceObject = IoAttachDeviceToDeviceStack(deviceObject, PhysicalDeviceObject);
  if (pDevExt->TopOfStackDeviceObject == NULL) {
    DPRINT(("<usb> error AddDevice::IoAttachDeviceToDeviceStack"));
    IoDeleteDevice(deviceObject);
    ntS = STATUS_NO_SUCH_DEVICE;
  } else {
    // Register device interfaces
    ntS = IoRegisterDeviceInterface(pDevExt->PhysicalDeviceObject, &GUID_CLASS_USBDEVICE_BULK, NULL, &pDevExt->InterfaceName);
    if(!NT_SUCCESS(ntS)) {
      DPRINT(("<usb> error AddDevice::IoRegisterDeviceInterface %x", ntS));
      IoDetachDevice(pDevExt->TopOfStackDeviceObject);
      IoDeleteDevice(deviceObject);
    } else {
      deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
    }
  }

  pDevExt->NumBytesInBuffer = 0;

  DPRINT(("<usb> <- _AddDevice\n"));
  return(ntS);
}

/*********************************************************************
*
*       DriverEntry
*/
NTSTATUS DriverEntry(IN DRIVER_OBJECT * pDriverObject, IN UNICODE_STRING * UniRegistryPath) {
  NTSTATUS ntS;
  UNICODE_STRING * pRegPath;

#if DBG
  char ac[1024];
  MISC_Unicode2ASCII(ac, UniRegistryPath, sizeof(ac));
  DPRINT(("\n\n\n<usb> ***********************************************************************\n"));
  DPRINT(("<usb> Compiled "  __TIME__ ", " __DATE__ "\n"
         "<usb> -> DriverEntry( ..., %s\n", ac));
#endif

  pRegPath = &Globals.RegPath;
  // reg routines are called @ PASSIVE_LEVEL
  pRegPath->MaximumLength = UniRegistryPath->Length + sizeof(UNICODE_NULL);
  pRegPath->Length        = UniRegistryPath->Length;
  pRegPath->Buffer        = ExAllocatePool(PagedPool, pRegPath->MaximumLength);

  if (pRegPath->Buffer) {
    RtlZeroMemory(pRegPath->Buffer, pRegPath->MaximumLength);
    RtlMoveMemory(pRegPath->Buffer, UniRegistryPath->Buffer, UniRegistryPath->Length);
    // IRP dispatchers
    pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchIoctl;
    pDriverObject->MajorFunction[IRP_MJ_POWER]          = BulkUsb_DispatchPower; // generic IRP_MJ_POWER dispatcher
    pDriverObject->MajorFunction[IRP_MJ_PNP]            = BulkUsb_DispatchPnP;   // generic IRP_MJ_PNP dispatcher
    pDriverObject->MajorFunction[IRP_MJ_CREATE]         = DispatchCreate;
    pDriverObject->MajorFunction[IRP_MJ_CLOSE]          = DispatchClose;
    pDriverObject->MajorFunction[IRP_MJ_CLEANUP]        = DispatchClean;
    pDriverObject->MajorFunction[IRP_MJ_READ]           = DispatchRead;
    pDriverObject->MajorFunction[IRP_MJ_WRITE]          = DispatchWrite;
    // unload/add_device routines
    pDriverObject->DriverUnload                         = _DriverUnload;
    pDriverObject->DriverExtension->AddDevice           = _AddDevice;
    ntS = STATUS_SUCCESS;
    DPRINT(("<usb>    Driver loaded\n"));
  } else {
    ntS = STATUS_INSUFFICIENT_RESOURCES;
    DPRINT(("<usb> error: allocating registry path\n"));
  }
  DPRINT(("<usb> <- DriverEntry\n"));
  return(ntS);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -