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

📄 profim.c

📁 profibus-dp主站源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
        }

        deviceExtension->HIDCounter++;

        break;
      }

      //*********************************************************************

    case IRP_MJ_CLOSE:
      {
        if ( irpStack->FileObject )
        {
          PB_DbgPrintL3( "ProfiM: IRP_MJ_CLOSE (HID=%d)\n",
                    ( int ) irpStack->FileObject->FsContext );
          IrpB_DeleteAllByHID( & deviceExtension->IrpB, ( int ) irpStack->FileObject->FsContext );
          //while ( ResB_DeleteByHID( & deviceExtension->PB.ResB, ( int ) irpStack->FileObject->FsContext ) );
        }
        else
          DbgPrint( "ProfiM: IRP_MJ_CLOSE\n" );
        break;
      }

      //*********************************************************************

    case IRP_MJ_READ:
      {
        DbgPrint( "ProfiM: IRP_MJ_READ not supported - use IOCTL_PROFIM_GET_RESULT.\n" );
        // ProfiM_Read( deviceExtension, Irp );
        break;
      }

      //*********************************************************************

    case IRP_MJ_WRITE:
      {
        PB_DbgPrintL2( "ProfiM: IRP_MJ_WRITE\n" );
        //ProfiM_Write( deviceExtension, Irp );
        deviceExtension->TempIrp = Irp;
        KeSynchronizeExecution( deviceExtension->InterruptObject,
                                ProfiM_Write, deviceExtension );
        break;
      }

      //*********************************************************************

    case IRP_MJ_DEVICE_CONTROL:
      {
        //RS_DbgPrint( "ProfiM: IRP_MJ_DEVICE_CONTROL - " );

        ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;

        switch ( ioControlCode )
        {
          //******************************************************************   
          
          case IOCTL_PROFIM_GET_RESULT:
            {
              PB_DbgPrintL2( "ProfiM: GET_RESULT" );

              if ( inputBufferLength < 8 )
              {
                Irp->IoStatus.Information = 0;
                break;
              }
              //ProfiM_Read( deviceExtension, Irp );

              IoAcquireCancelSpinLock( &OldIrql );
              IoSetCancelRoutine( Irp, &ProfiM_IrpCancel );
              IoReleaseCancelSpinLock( OldIrql );     
              
              deviceExtension->TempIrp = Irp;
              KeSynchronizeExecution( deviceExtension->InterruptObject,
                                      ProfiM_Read, deviceExtension );              
              break;
            }
           
          //******************************************************************   
            
          case IOCTL_PROFIM_CANCEL_WAITING:
            {            
              if ( inputBufferLength < 4 )
              {             
                Irp->IoStatus.Information = 0;
                break;
              }
                
                
              MID     = * ((int *)(Irp->AssociatedIrp.SystemBuffer)); 
              if (irpStack->FileObject)   
              {
                DelIrp=IrpB_DeleteIrp( & deviceExtension->IrpB, ( int ) irpStack->FileObject->FsContext , MID );
                PB_DbgPrintL2( "ProfiM: CANCEL_WAITING: MID=%u IRP=%u", MID, (int)DelIrp );
                if (DelIrp)
                {
                  DelIrp->IoStatus.Status = STATUS_SUCCESS;    
                  DelIrp->IoStatus.Information = 0;
                  IoCompleteRequest( DelIrp, IO_NO_INCREMENT );               
                  PB_DbgPrintL2( "ProfiM: Irp=%u canceled.", (int)DelIrp );                  
                }              
              }             
              Irp->IoStatus.Information = 0;
              break;
            }
            
          //******************************************************************

          case IOCTL_PROFIM_GET_MASTER_STATE:
            {
              RS_DbgPrint( "ProfiM: GET_MASTER_STATE\n" );
              if ( outputBufferLength >= 4 )
              {
                *( ULONG * ) ioBuffer = deviceExtension->PB.StationStatus;

                Irp->IoStatus.Information = 4;
              }
              break;
            }
            
          //******************************************************************

          case IOCTL_PROFIM_RESTART_MASTER:
            {
              RS_DbgPrint( "ProfiM: RESTART_MASTER\n" );
              if ( outputBufferLength >= 4 )
              {
                *( ULONG * ) ioBuffer = deviceExtension->PB.StationStatus;

                Irp->IoStatus.Information = 4;
              }
              break;
            }                
            
          //******************************************************************

          default:
            {
              PB_DbgPrintL1( "ProfiM: Unknown IRP_MJ_DEVICE_CONTROL\n" );
              Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
              break;
            }
        }

        break;
      }

      //*********************************************************************

    default:
      {
        PB_DbgPrintL1( "ProfiM: Unhandled IRP_MJ function\n" );
        Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
        break;
      }
  }

  //
  // DON'T get cute and try to use the status field of
  // the irp in the return status.  That IRP IS GONE as
  // soon as you call IoCompleteRequest.
  //

  ntStatus = Irp->IoStatus.Status;

  if ( deviceExtension->CompleteIrp )
    IoCompleteRequest( Irp, IO_NO_INCREMENT );

  PB_DbgPrintL1( "ProfiM: DisptachRoutine exit.\n" );  

  return ntStatus;
}



//***************************************************************************
//  GetConfiguration
//
// Description:
//      Obtains driver configuration information from the Registry.
//
// Arguments:
//      DeviceExtension - Pointer to the device extension.
//      RegistryPath    - Pointer to the null-terminated Unicode name of the
//                        registry path for this driver.  
//
// Return Value:
//      NTSTATUS
// 
NTSTATUS GetConfiguration( IN PPROFIM_DEVICE_EXTENSION DeviceExtension,
                           IN PUNICODE_STRING RegistryPath )
{
  PRTL_QUERY_REGISTRY_TABLE parameters      = NULL;
  UNICODE_STRING            parametersPath;

  ULONG                     notThereDefault = 1234567;
  ULONG                     PortAddressDefault;
  ULONG                     IRQLineDefault;
  ULONG                     BaudRateDefault;
  ULONG                     BufferSizeDefault;

  NTSTATUS                  status          = STATUS_SUCCESS;
  PWSTR                     path            = NULL;
  USHORT                    queriesPlusOne  = 4;


  if ( !DeviceExtension )
  {
    DbgPrint( "ProfiM: GetConfiguration Error - DeviceExtension is NULL!" );
    return STATUS_UNSUCCESSFUL;
  }          
  if ( DeviceExtension->magic != PROFIM_MAGIC )
  {
    DbgPrint( "ProfiM: GetConfiguration Error - Device Extension MAGIC is invalid!" );
    return STATUS_UNSUCCESSFUL;
  }        


  parametersPath.Buffer = NULL;

  //
  // Registry path is already null-terminated, so just use it.
  //

  path = RegistryPath->Buffer;

  //
  // Allocate the Rtl query table.
  //

  parameters = ExAllocatePool( PagedPool,
                               sizeof( RTL_QUERY_REGISTRY_TABLE ) * queriesPlusOne );

  if ( !parameters )
  {
    RS_DbgPrint( "ProfiM: ExAllocatePool failed for Rtl in GetConfiguration\n" );
    status = STATUS_UNSUCCESSFUL;
  }
  else
  {
    RtlZeroMemory( parameters,
                   sizeof( RTL_QUERY_REGISTRY_TABLE ) * queriesPlusOne );

    //
    // Form a path to this driver's Parameters subkey.
    //

    RtlInitUnicodeString( &parametersPath, NULL );

    parametersPath.MaximumLength = RegistryPath->Length +
                                   sizeof( L"\\Parameters" );

    parametersPath.Buffer = ExAllocatePool( PagedPool,
                                            parametersPath.MaximumLength );

    if ( !parametersPath.Buffer )
    {
      RS_DbgPrint( "ProfiM: ExAllocatePool failed for Path in GetConfiguration\n" );
      status = STATUS_UNSUCCESSFUL;
    }
  }

  if ( NT_SUCCESS( status ) )
  {
    //
    // Form the parameters path.
    //

    RtlZeroMemory( parametersPath.Buffer, parametersPath.MaximumLength );
    RtlAppendUnicodeToString( &parametersPath, path );
    RtlAppendUnicodeToString( &parametersPath, L"\\Parameters" );

    //
    // Gather all of the "user specified" information from
    // the registry.
    //

    parameters[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
    parameters[0].Name = L"Port Address";
    parameters[0].EntryContext = &PortAddressDefault;
    parameters[0].DefaultType = REG_DWORD;
    parameters[0].DefaultData = &notThereDefault;
    parameters[0].DefaultLength = sizeof( ULONG );

    parameters[1].Flags = RTL_QUERY_REGISTRY_DIRECT;
    parameters[1].Name = L"IRQ Line";
    parameters[1].EntryContext = &IRQLineDefault;
    parameters[1].DefaultType = REG_DWORD;
    parameters[1].DefaultData = &notThereDefault;
    parameters[1].DefaultLength = sizeof( ULONG );

    parameters[2].Flags = RTL_QUERY_REGISTRY_DIRECT;
    parameters[2].Name = L"Baud Rate";
    parameters[2].EntryContext = &BaudRateDefault;
    parameters[2].DefaultType = REG_DWORD;
    parameters[2].DefaultData = &notThereDefault;
    parameters[2].DefaultLength = sizeof( ULONG );
/*
    parameters[3].Flags = RTL_QUERY_REGISTRY_DIRECT;
    parameters[3].Name = L"Buffer Size";
    parameters[3].EntryContext = &BufferSizeDefault;
    parameters[3].DefaultType = REG_DWORD;
    parameters[3].DefaultData = &notThereDefault;
    parameters[3].DefaultLength = sizeof( ULONG );
*/
    status = RtlQueryRegistryValues( RTL_REGISTRY_ABSOLUTE |
                                     RTL_REGISTRY_OPTIONAL,
                                     parametersPath.Buffer,
                                     parameters,
                                     NULL,
                                     NULL );

    if ( !NT_SUCCESS( status ) )
    {
      RS_DbgPrint( "ProfiM: RtlQueryRegistryValues failed\n" );
    }

    status = STATUS_SUCCESS;
  }

  //
  // Go ahead and assign driver defaults.
  //

  if ( PortAddressDefault == notThereDefault )
  {
    DeviceExtension->PortAddress = ( PUCHAR ) DEF_PORT_ADDRESS;
  }
  else
  {
    DeviceExtension->PortAddress = ( PVOID ) PortAddressDefault;
  }

  if ( IRQLineDefault == notThereDefault )
  {
    DeviceExtension->IRQLine = DEF_IRQ_LINE;
  }
  else
  {
    DeviceExtension->IRQLine = ( KIRQL ) IRQLineDefault;
  }

  if ( BaudRateDefault == notThereDefault )
  {
    DeviceExtension->BaudRate = DEF_BAUD_RATE;
  }
  else
  {
    DeviceExtension->BaudRate = BaudRateDefault;
  }

/*  if ( BufferSizeDefault == notThereDefault )
  {*/
    DeviceExtension->BufferSize = DEF_BUFFER_SIZE;
/*  }
  else
  {
    DeviceExtension->BufferSize = BufferSizeDefault;
  }*/

  DbgPrint( "ProfiM: GetConfiguration - BufferSize=%d",
            DeviceExtension->BufferSize );
  if ( DeviceExtension->BufferSize == 0 )
  {
    DbgPrint( "ProfiM: Error BufferSize can't be null!" );
    return STATUS_UNSUCCESSFUL;
  }    
  DbgPrint( "ProfiM: GetConfiguration - BaudRate=%d",
            DeviceExtension->BaudRate );    

  //
  // Free the allocated memory before returning.
  //

  if ( parametersPath.Buffer )
    ExFreePool( parametersPath.Buffer );
  if ( parameters )
    ExFreePool( parameters );
  return ( status );
}


//***************************************************************************
//  GetPnPConfiguration
//
// Description:
//      Obtains driver configuration information from the Registry in PnP 
//      mode.
//
// Arguments:
//      DeviceExtension - Pointer to the device extension.
//
// Return Value:
//      NTSTATUS

⌨️ 快捷键说明

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