驱动程序(4) WDF下DMA传输的驱动程序代码详细说明之driver.c

本文将详细解析driver.c文件中关于WDF(Windows Driver Framework)下的DMA传输驱动程序代码,涵盖DriverEntry、PCIe_EvtDeviceAdd和InitializeDMA等关键回调例程。在DriverEntry中注册PCIe_EvtDeviceAdd例程,初始化设备和驱动对象。PCIe_EvtDeviceAdd用于设备枚举,设置同步模式,创建设备并配置MSI中断。由于WDF MSI中断问题,采用WDM重新实现MSI流程。InitializeDMA为DMA传输准备,创建DMAEnabler、CommonBuffer和DMATransaction对象,设置数据缓冲区对齐和地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从这一篇开始介绍几个比较重要的源文件和应用程序,并且会对其中一些关键代码进行说明。这些代码流程都是本人亲身测试可行的,但是由于当时调试时杂七杂八的东西加的太多,现在看起来有的地方的代码风马牛不相及,如果完全照搬的话可能行不通的,还是需要各位读者自行理解然后加以改进的。当然如果有问题也欢迎各位读者指出更正,谢谢!

driver.c

/*++
Module Name: driver.c
Abstract:This file contains the driver entry points and callbacks, sucn as DriverEnrty and EvtDeviceAdd function.
Environment: Kernel-mode Driver Framework
Time: 20181015
--*/

#include "driver.h"
/*
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
#pragma alloc_text (PAGE, PCIe_EvtDeviceAdd)
#pragma alloc_text (PAGE, PCIe_EvtDriverContextCleanup)
#endif
*/

NTSTATUS InitializeDMA(IN WDFDEVICE Device);
#define MAXNLEN 4096 //DMA传输最大字节长度

NTSTATUS
DriverEntry(
    _In_ PDRIVER_OBJECT  DriverObject,
    _In_ PUNICODE_STRING RegistryPath
    )
/*++
Routine Description:
    DriverEntry initializes the driver and is the first routine called by the
    system after the driver is loaded. DriverEntry specifies the other entry
    points in the function driver, such as EvtDevice and DriverUnload.
    
Parameters Description:
    DriverObject - represents the instance of the function driver that is loaded
    into memory. DriverEntry must initialize members of DriverObject before it
    returns to the caller. DriverObject is allocated by the system before the
    driver is loaded, and it is released by the system after the system unloads
    the function driver from memory.
    
    RegistryPath - represents the driver specific path in the Registry.
    The function driver can use the path to store driver related data between
    reboots. The path does not store hardware instance specific data.

Return Value:

    STATUS_SUCCESS if successful,
    STATUS_UNSUCCESSFUL otherwise.
--*/
{
   
   
    WDF_DRIVER_CONFIG config;
    NTSTATUS status = STATUS_SUCCESS;
    WDF_OBJECT_ATTRIBUTES attributes;

	KdPrintEx((DPFLTR_IHVAUDIO_ID, DPFLTR_ERROR_LEVEL, "Function 'DriverEntry' begins\n"));
    WDF_OBJECT_ATTRIBUTES_INIT(&attributes);      //设备属性初始化
    WDF_DRIVER_CONFIG_INIT(&config, PCIe_EvtDeviceAdd); //初始化WDF_DRIVER_CONFIG,并提供DeviceAdd函数

	//创建驱动对象
    status = WdfDriverCreate(DriverObject,
                             RegistryPath,
                             &attributes,
                             &config,
                             WDF_NO_HANDLE
                             );

    if (!NT_SUCCESS(status)) {
   
   
        return status;
    }

	KdPrintEx((DPFLTR_IHVAUDIO_ID, DPFLTR_ERROR_LEVEL, "Function 'DriverEntry' completes successfully and Create wdf driver object\n"));
	//attributes.EvtCleanupCallback = PCIe_EvtDriverContextCleanup;
    return status
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值