Kernel Mode Driver Framework>> Pascal (PNP) >> File

WdfDeviceIoDirect(DMA)


參考資訊:
1. Source Code

由於我們練習的驅動程式位於上層,本身並沒有觸及過多DMA的細節操作,因此,WdfDeviceIoDirect的DMA作法會跟PIO一樣,唯一的區別就是取得DMA位址的方式,必須使用特殊的API取得,這篇練習在於說明有這樣的記憶體差別,當使用者實際觸及DMA驅動程式時,必須詳細閱讀DMA的操作方式。

記憶體指標:

Event Buffer Length
EvtIoRead Step 1. WdfRequestRetrieveOutputWdmMdl()
Step 2. MmGetMdlVirtualAddress()
傳入的第三個參數
EvtIoWrite Step 1. WdfRequestRetrieveInputWdmMdl()
Step 2. MmGetMdlVirtualAddress()
傳入的第三個參數

main.pas

unit main;

interface
  uses
    DDDK;
    
  const
    DEV_NAME = '\Device\MyDriver';
    SYM_NAME = '\DosDevices\MyDriver';

  function __DriverEntry(pOurDriver:PDRIVER_OBJECT; pOurRegistry:PUNICODE_STRING):NTSTATUS; stdcall;

implementation
var
  szBuffer: array[0..255] of char;
  
procedure IrpFileCreate(Device:WDFDEVICE; Request:WDFREQUEST; FileObject:WDFFILEOBJECT); stdcall;
begin
  DbgPrint('IrpFileCreate', []);
  WdfRequestComplete(Request, STATUS_SUCCESS);
end;

procedure IrpFileClose(FileObject:WDFFILEOBJECT); stdcall;
begin
  DbgPrint('IrpFileClose', []);
end;

procedure IrpRead(Queue:WDFQUEUE; Request:WDFREQUEST; Length:ULONG); stdcall;
var
  mdl: PMDL;
  len: ULONG;
  dst: Pointer;

begin
  DbgPrint('IrpRead', []);
  len:= strlen(@szBuffer) + 1;
  WdfRequestRetrieveOutputWdmMdl(Request, @mdl);
  dst:= MmGetMdlVirtualAddress(mdl);
  memcpy(dst, @szBuffer, len);
  WdfRequestCompleteWithInformation(Request, STATUS_SUCCESS, len);
end;

procedure IrpWrite(Queue:WDFQUEUE; Request:WDFREQUEST; Length:ULONG); stdcall;
var
  mdl: PMDL;
  src: Pointer;

begin
  DbgPrint('IrpWrite', []);
  WdfRequestRetrieveInputWdmMdl(Request, @mdl);
  src:= MmGetMdlVirtualAddress(mdl);
  memcpy(@szBuffer, src, Length);
  DbgPrint('Buffer: %s, Length:%d', [szBuffer, Length]);
  WdfRequestCompleteWithInformation(Request, STATUS_SUCCESS, Length);
end;

function AddDevice(pOurDriver:WDFDRIVER; pDeviceInit:PWDFDEVICE_INIT):NTSTATUS; stdcall;
var
  device: WDFDEVICE;
  suDevName: UNICODE_STRING;
  szSymName: UNICODE_STRING;
  file_cfg: WDF_FILEOBJECT_CONFIG;
  ioqueue_cfg: WDF_IO_QUEUE_CONFIG;

begin
  WdfDeviceInitSetIoType(pDeviceInit, WdfDeviceIoDirect);
  WDF_FILEOBJECT_CONFIG_INIT(@file_cfg, @IrpFileCreate, @IrpFileClose, Nil);
  WdfDeviceInitSetFileObjectConfig(pDeviceInit, @file_cfg, WDF_NO_OBJECT_ATTRIBUTES);
  
  RtlInitUnicodeString(@suDevName, DEV_NAME);
  RtlInitUnicodeString(@szSymName, SYM_NAME);
  WdfDeviceInitAssignName(pDeviceInit, @suDevName);
  WdfDeviceCreate(@pDeviceInit, WDF_NO_OBJECT_ATTRIBUTES, @device);
  WdfDeviceCreateSymbolicLink(device, @szSymName);
  
  WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(@ioqueue_cfg, WdfIoQueueDispatchSequential);
  ioqueue_cfg.EvtIoRead:= @IrpRead;
  ioqueue_cfg.EvtIoWrite:= @IrpWrite;
  Result:= WdfIoQueueCreate(device, @ioqueue_cfg, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE);
end;

function __DriverEntry(pOurDriver:PDRIVER_OBJECT; pOurRegistry:PUNICODE_STRING):NTSTATUS; stdcall;
var
  config: WDF_DRIVER_CONFIG;
  
begin
  WDF_DRIVER_CONFIG_INIT(@config, AddDevice);
  WdfDriverCreate(pOurDriver, pOurRegistry, WDF_NO_OBJECT_ATTRIBUTES, @config, WDF_NO_HANDLE);
  Result:= STATUS_SUCCESS;
end;
end.

IrpWrite()複製User Application的資料到szBuffer
IrpRead()回填資料給User Application

app.pas

program main;

{$APPTYPE CONSOLE}

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  DIALOGS;

var
  fd: DWORD;
  ret: DWORD;
  len: DWORD;
  szBuf: array[0..255] of char;

begin
  fd:= CreateFile('\\.\MyDriver', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, Nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (fd <> INVALID_HANDLE_VALUE) then
  begin
    StrCopy(szBuf, 'I am error');
    len:= strlen(szBuf)+1;
    WriteLn(Output, Format('WR: %s, %d', [szBuf, len]));
    WriteFile(fd, szBuf, len, ret, Nil);
    ReadFile(fd, szBuf, len, ret, Nil);
    WriteLn(Output, Format('RD: %s, %d', [szBuf, ret]));
    CloseHandle(fd);
  end else
  begin
    WriteLn(Output, 'failed to open mydriver');
  end;
end.

結果


返回上一頁