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

📄 usbfx2lk.cpp

📁 VisualC++写的一个USB的驱动程序。
💻 CPP
字号:
///////////////////////////////////////////////////////////////////////////////
//
//    (C) Copyright 2005 OSR Open Systems Resources, Inc.
//    All Rights Reserved
//
//    This sofware is supplied for instructional purposes only.
//
//    OSR Open Systems Resources, Inc. (OSR) expressly disclaims any warranty
//    for this software.  THIS SOFTWARE IS PROVIDED  "AS IS" WITHOUT WARRANTY
//    OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
//    THE IMPLIED WARRANTIES OF MECHANTABILITY OR FITNESS FOR A PARTICULAR
//    PURPOSE.  THE ENTIRE RISK ARISING FROM THE USE OF THIS SOFTWARE REMAINS
//    WITH YOU.  OSR's entire liability and your exclusive remedy shall not
//    exceed the price paid for this material.  In no event shall OSR or its
//    suppliers be liable for any damages whatsoever (including, without
//    limitation, damages for loss of business profit, business interruption,
//    loss of business information, or any other pecuniary loss) arising out
//    of the use or inability to use this software, even if OSR has been
//    advised of the possibility of such damages.  Because some states/
//    jurisdictions do not allow the exclusion or limitation of liability for
//    consequential or incidental damages, the above limitation may not apply
//    to you.
//
//    OSR Open Systems Resources, Inc.
//    105 Route 101A Suite 19
//    Amherst, NH 03031  (603) 595-6500 FAX: (603) 595-6503
//    email bugs to: bugs@osr.com
//
//
//    MODULE:
//
//        USBFX2LK -- OSR USBFX2 Learning Kit Driver
//
//    ABSTRACT:
//
//      This file DriverEntry and Unload functions for this driver.
//
//    AUTHOR(S):
//
//        OSR Open Systems Resources, Inc.
// 
///////////////////////////////////////////////////////////////////////////////

#include "usbfx2lk.h"

//
// Forward definition for DriverEntry.   Since this is a .CPP module we must
// declare DriverEntry as extern "C" or the linker will not be able to link
// the driver because the DriverEntry point will be name managled by the
// compiler.
extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObj, PUNICODE_STRING RegistryPath);

#ifdef WPP_TRACING
extern "C" {
#include "usbfx2lk.tmh"
}
#endif //WPP_TRACING

//
// The following pragma allows the DriverEntry code to be discarded once
// initialization is completed
//
#pragma alloc_text(INIT,DriverEntry)


UNICODE_STRING	GlobalRegistryPath = {0,0,0};
ULONG           OsrDebugLevel = 0xFFFFFFFF;

///////////////////////////////////////////////////////////////////////////////
//
//  DriverEntry
//
//    This routine is called by Windows when the driver is first loaded.  It
//    is the responsibility of this routine to initialize the driver...
//    but not any device information or state, this will be handled in our
//    AddDevice routine in our PnP module
//
//  INPUTS:
//
//      DriverObject - Address of the DRIVER_OBJECT created by Windows for this driver.
//
//      RegistryPath - UNICODE_STRING which represents this driver's key in the
//                     Registry.  
//
//  OUTPUTS:
//
//      None.
//
//  RETURNS:
//
//      STATUS_SUCCESS, otherwise an error indicating why the driver could not
//                      load.
//
//  IRQL:
//
//      This routine is called at IRQL == PASSIVE_LEVEL.
//
//  CONTEXT:
//
//      This routine is called in the context of the System process
//
//  NOTES:
//
//
///////////////////////////////////////////////////////////////////////////////
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
{
#if DBG
    DbgPrint("\nOSR " DRIVER_NAME " Driver -- Compiled %s %s\n",__DATE__, __TIME__);
#endif

    //
    // Establish dispatch entry points for the functions we support
    //
    DriverObject->MajorFunction[IRP_MJ_CREATE]         = UsbFx2LkCreate;
    DriverObject->MajorFunction[IRP_MJ_CLOSE]          = UsbFx2LkClose;

    DriverObject->MajorFunction[IRP_MJ_READ]           = UsbFx2LkRead;
    DriverObject->MajorFunction[IRP_MJ_WRITE]          = UsbFx2LkWrite;

    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = UsbFx2LkDeviceControl;
    DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = UsbFx2LkSystemControl;

    DriverObject->MajorFunction[IRP_MJ_PNP]            = UsbFx2LkPnp;
    DriverObject->MajorFunction[IRP_MJ_POWER]          = UsbFx2LkPower;

#ifdef WPP_TRACING
#ifndef W2K
    //
    // Initialize WPP tracing on XP and later. For W2k this is
    // done in UsbFx2LkAddDevice.
    //
    WPP_INIT_TRACING(DriverObject, RegistryPath);

#endif // W2K
#endif  // WPP_TRACING

    //
    // AddDevice function for PnP Manager
    //
    DriverObject->DriverExtension->AddDevice           = UsbFx2LkAddDevice;

    //
    // Unload function
    //
    DriverObject->DriverUnload                         = UsbFx2LkUnload;

    //
    // Save away the registry path for later use.
    //
	RtlZeroMemory(&GlobalRegistryPath,sizeof(UNICODE_STRING));

	GlobalRegistryPath.Buffer = (PWSTR) ExAllocatePool(NonPagedPool,RegistryPath->MaximumLength);

    //
    // Ensure that the buffer was allocated.  If not return an error.
    //
	if(!GlobalRegistryPath.Buffer) {

#if DBG
        DbgPrint("\nOSR " DRIVER_NAME " DriverEntry - Failed to allocate memory.\n");
#endif
		return STATUS_INSUFFICIENT_RESOURCES;

	}

	RtlZeroMemory(GlobalRegistryPath.Buffer,RegistryPath->MaximumLength);

	RtlCopyMemory(GlobalRegistryPath.Buffer,RegistryPath->Buffer,RegistryPath->Length);

	GlobalRegistryPath.Length = RegistryPath->Length;
	GlobalRegistryPath.MaximumLength = RegistryPath->MaximumLength;


#if DBG
    DbgPrint(DRIVER_NAME "DriverEntry: Leaving\n");
#endif

    return(STATUS_SUCCESS);
}


///////////////////////////////////////////////////////////////////////////////
//
//  UsbFx2LkgUnload
//
//    This routine is our dynamic unload entry point.  We are called here when
//    the OS wants to unload our driver.  It is our responsibility to release
//    any global resources we allocated.  Since we delete our device object
//    (and its symbolic link) during PnP REMOVE_DEVICE processing, there's
//    really nothing to do here.
//
//  INPUTS:
//
//      DriverObj - Address of our DRIVER_OBJECT.
//
//  OUTPUTS:
//
//      None.
//
//  RETURNS:
//
//      None.
//
//  IRQL:
//
//    This routine is called at IRQL_PASSIVE_LEVEL.
//
//  NOTES:
//
///////////////////////////////////////////////////////////////////////////////
VOID UsbFx2LkUnload(PDRIVER_OBJECT DriverObject)
{
    OsrTracePrint(TRACE_LEVEL_INFORMATION,OSRDBG_PNP_INFO,(DRIVER_NAME " UsbFx2LkUnload: Starting\n"));

    //
    // Delete the memory we allocated in Driver Entry.
    //
    if(GlobalRegistryPath.Buffer) {
        ExFreePool(GlobalRegistryPath.Buffer);
    }

    //
    // If Tracing Cleanup on XP and Later
    // 
#ifdef WPP_TRACING
#ifndef W2K

    WPP_CLEANUP(DriverObject);

#endif // W2K
#endif //WPP_TRACING

    //
    // The device object should already be deleted (and GONE) at
    // this point 
    //
    ASSERT(!DriverObject->DeviceObject);

#if DBG
    DbgPrint(DRIVER_NAME " Unload: Leaving... \n");
#endif
}

#ifdef WPP_TRACING
#ifdef W2K
///////////////////////////////////////////////////////////////////////////////
//
//  W2KInitializeWPPTracing
//
//    This routine initializes WPP tracing if we are running on a W2K system.
//
//  INPUTS:
//
//      DeviceObject - Address of our Device Object.
//      RegistryPath - Registry Path
//
//  OUTPUTS:
//
//      None.
//
//  RETURNS:
//
//      None.
//
//  IRQL:
//
//    This routine is called at IRQL_PASSIVE_LEVEL.
//
//  NOTES:
//  
//
///////////////////////////////////////////////////////////////////////////////
VOID W2KInitializeWPPTracing(PDEVICE_OBJECT DeviceObject,PUNICODE_STRING RegistryPath)
{
    WPP_INIT_TRACING(DeviceObject, RegistryPath);
}

///////////////////////////////////////////////////////////////////////////////
//
//  W2KCleanupWPPTracing
//
//    This routine ends WPP tracing if we are running on a W2K system.
//
//  INPUTS:
//
//      DeviceObject - Address of our Device Object.
//
//  OUTPUTS:
//
//      None.
//
//  RETURNS:
//
//      None.
//
//  IRQL:
//
//    This routine is called at IRQL_PASSIVE_LEVEL.
//
//  NOTES:
//
///////////////////////////////////////////////////////////////////////////////
VOID W2KCleanupWPPTracing(PDEVICE_OBJECT DeviceObject)
{
    WPP_CLEANUP(DeviceObject);
}
#endif // W2K_BUILD
#endif //WPP_TRACING

⌨️ 快捷键说明

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