www.pudn.com > usbfx2lk_v1.1.zip > usbfx2lk_createclose.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_CreateClose.cpp 
// 
//    ABSTRACT: 
// 
//      This file contains the routines that handle IRP_MJ_CREATE and 
//      IRP_MJ_CLOSE processing for the OSR USB FX2 Learning Kit Device 
// 
//    AUTHOR(S): 
// 
//      OSR Open Systems Resources, Inc. 
//  
/////////////////////////////////////////////////////////////////////////////// 
#include "usbfx2lk.h" 
 
#ifdef WPP_TRACING 
// 
// Include the necessary tmh file - this is  
//  just a matter of course if you're using WPP tracing. 
// 
extern "C" { 
#include "usbfx2lk_createclose.tmh" 
} 
#endif 
 
 
/////////////////////////////////////////////////////////////////////////////// 
// 
// UsbFx2LkCreate 
// 
//  This routine is called by the IO Manager to process a IRP_MJ_CREATE 
//  Irp. 
// 
// 
//  INPUTS: 
// 
//      DeviceObject  -  One of our Device Objects. 
//      Irp  -  The Irp to process. 
// 
//  OUTPUTS: 
// 
//      None 
// 
//  RETURNS: 
// 
//      None 
// 
//  IRQL: 
// 
//      IRQL == PASSIVE_LEVEL 
// 
//  CONTEXT: 
// 
//      User Context 
// 
//  NOTES: 
// 
/////////////////////////////////////////////////////////////////////////////// 
NTSTATUS UsbFx2LkCreate(PDEVICE_OBJECT DeviceObject,PIRP Irp) 
{ 
    PUSBFX2LK_EXT       devExt = (PUSBFX2LK_EXT)DeviceObject->DeviceExtension; 
    NTSTATUS            status = STATUS_SUCCESS; 
    KIRQL               oldIrql; 
    
    // 
    // We treat this routine as pageable 
    // 
    PAGED_CODE(); 
 
    // 
    // Increment the count of outstanding IOs. 
    // 
    OsrIncrementOutstandingIoCount(devExt,__FILE__,__LINE__); 
 
    // 
    // Are we not in a state that allows a  
    //  CREATE to occur? 
    // 
    KeAcquireSpinLock(&devExt->IoStateLock,&oldIrql); 
    if (devExt->DevicePnPState < STATE_ALL_BELOW_FAIL) { 
 
        KeReleaseSpinLock(&devExt->IoStateLock,oldIrql); 
 
        status = STATUS_INVALID_DEVICE_STATE; 
 
        OsrTracePrint(TRACE_LEVEL_ERROR,OSRDBG_CREATE_CLOSE_INFO,  
            ("OsrCreateClose: Attempt to open handle denied due to Pnp State! Current PnP state - %s\n", 
            OsrPrintState(devExt))); 
 
 
        // 
        // Complete the request! 
        // 
        Irp->IoStatus.Status      = status; 
        Irp->IoStatus.Information = 0; 
 
        IoCompleteRequest(Irp, IO_NO_INCREMENT); 
 
        // 
        // We're done with this request. Decrement the count 
        // of outstanding IOs 
        // 
        OsrDecrementOutstandingIoCount(devExt,__FILE__,__LINE__); 
 
        return status; 
 
    } 
 
    // 
    // See if the device is already opened.  If it is, don't allow another open.   We do this so 
    // that we can have multiple test boards and test programs running simultaneously on different 
    // devices and they don't interfer with each other. 
    // 
    if(devExt->Opened) { 
        status = STATUS_ACCESS_DENIED; 
        OsrTracePrint(TRACE_LEVEL_ERROR,OSRDBG_CREATE_CLOSE_INFO,("OsrCreateClose: Device Already Opened.\n")); 
    } else { 
        devExt->Opened = TRUE; 
    } 
 
    KeReleaseSpinLock(&devExt->IoStateLock,oldIrql); 
 
    // 
    // Complete the request! 
    // 
    Irp->IoStatus.Status      = status; 
    Irp->IoStatus.Information = 0; 
     
    IoCompleteRequest(Irp, IO_NO_INCREMENT); 
     
    // 
    // We're done with this request. Decrement the count 
    // of outstanding IOs 
    // 
    OsrDecrementOutstandingIoCount(devExt,__FILE__,__LINE__); 
 
    return status; 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
// 
// UsbFx2LkClose 
// 
//  This routine is called by the IO Manager to process a IRP_MJ_CLOSE 
//  Irp. 
// 
// 
//  INPUTS: 
// 
//      DeviceObject  -  One of our Device Objects. 
//      Irp  -  The Irp to process. 
// 
//  OUTPUTS: 
// 
//      None 
// 
//  RETURNS: 
// 
//      None 
// 
//  IRQL: 
// 
//      IRQL == PASSIVE_LEVEL 
// 
//  CONTEXT: 
// 
//      User Context 
// 
//  NOTES: 
// 
/////////////////////////////////////////////////////////////////////////////// 
NTSTATUS UsbFx2LkClose(PDEVICE_OBJECT DeviceObject,PIRP Irp) 
{ 
    PUSBFX2LK_EXT   devExt = (PUSBFX2LK_EXT)DeviceObject->DeviceExtension; 
    KIRQL           oldIrql; 
 
    // 
    // We treat this routine as pageable 
    // 
    PAGED_CODE(); 
 
    // 
    // Increment the count of outstanding IOs. 
    // 
    OsrIncrementOutstandingIoCount(devExt,__FILE__,__LINE__); 
 
    // 
    // Clear the Opened Flag.   This allows someone else to open 
    // the device, since we are through with it. 
    // 
    KeAcquireSpinLock(&devExt->IoStateLock,&oldIrql); 
    devExt->Opened = FALSE; 
    KeReleaseSpinLock(&devExt->IoStateLock,oldIrql); 
 
    // 
    // Finish the request 
    // 
    Irp->IoStatus.Status = STATUS_SUCCESS; 
    Irp->IoStatus.Information = 0; 
    IoCompleteRequest(Irp,IO_NO_INCREMENT); 
 
    // 
    // We're done with this request 
    // 
    OsrDecrementOutstandingIoCount(devExt,__FILE__,__LINE__); 
 
    return STATUS_SUCCESS; 
 
}