首页 技术 正文
技术 2022年11月9日
0 收藏 835 点赞 3,945 浏览 2901 个字

1.  ExAllocatePool()

函数说明:

ExAllocatePool allocates pool memory of the specified type and returns a pointer to the allocated

block.

函数定义:

PVOID ExAllocatePool(

__in  POOL_TYPE PoolType,

__in  SIZE_T NumberOfBytes

);

代码中用来分配设备描述DMA adepter设备的特性(DEVICE_DESCRIPTION),作为参数传递给IoGetDmaAdapter()函数。在调用IoGetDmaAdapter()函数之前就要对PDEVICE_DESCRIPTION结构进行指定各种特性。

实际中这样使用:

PDEVICE_DESCRIPTION DeviceDescription = (PDEVICE_DESCRIPTION) ExAllocatePool

(PagedPool, sizeof(DEVICE_DESCRIPTION));

2. RtlZeroMemory()

函数说明:

The RtlZeroMemory routine fills a block of memory with zeros, given a pointer to the block and the length, in bytes, to be filled.

函数定义:

VOID RtlZeroMemory(

__in  VOID UNALIGNED *Destination,

__in  SIZE_T Length

);

3. IoGetDmaAdapter()

函数说明:

The IoGetDmaAdapter routine returns a pointer to the DMA adapter structure for a physical device object.

函数定义:

struct _DMA_ADAPTER* IoGetDmaAdapter(

__in_opt  PDEVICE_OBJECT PhysicalDeviceObject,

__in      struct _DEVICE_DESCRIPTION *DeviceDescription,

__out     PULONG NumberOfMapRegisters

);

实际代码中这样使用:

ULONG  NumberOfMapRegisters=100;

pdx->DmaAdapter=IoGetDmaAdapter(pdx->NextStackDevice,DeviceDescription,&NumberOfMapRegisters);// NextStackDevice在AddDevice函数中已经attach了。

4. AllocateCommonBuffer()

函数说明:

The AllocateCommonBuffer routine allocates memory and maps it so that it is simultaneously accessible from both the processor and a device for DMA operations。

函数定义:

PVOID AllocateCommonBuffer(

__in   PDMA_ADAPTER DmaAdapter,

__in   ULONG Length,

__out  PPHYSICAL_ADDRESS LogicalAddress,

__in   BOOLEAN CacheEnabled

);

注:第一个参数是IoGetDmaAdapter()返回的,第三个参数是自己顶一顶一个物理地址类型,用来接收分配得到的物理地址首址。

返回值:返回值为虚拟地址,供上层使用

实际代码中这样使用:

pdx->descAddress=pdx->allocateCommonBuffer(pdx->DmaAdapter,(ULONG)DESC_ADDRESS*PORT_NUM,&pdx->DescLogicalAddress,FALSE);

附:使用该函数必不可少的会使用下面的函数

pdx->DmaAdapter=IoGetDmaAdapter(pdx->NextStackDevice,DeviceDescription,&NumberOfMapRegisters);

//创建一个DMA适配器

pdx->allocateCommonBuffer=*pdx->DmaAdapter->DmaOperations->AllocateCommonBuffer;

//分配连续的物理内存DMA函数

pdx->freeCommonBuffer = *pdx->DmaAdapter->DmaOperations->FreeCommonBuffer;

//释放连续的物理内存DMA函数

pdx->putDmaAdapter=*pdx->DmaAdapter->DmaOperations->PutDmaAdapter;

//释放DMA Adapter对象

当通过AllocateCommonBuffer()分配得到地址之后需要把这个地址分成两部分来处理,与硬件打交道的需要用LogicAddress 与 应用程序打交道需用 Virtual Address.

最好将AllocateCommonBuffer()函数取得的这两个地址保存到设备扩展中特定的变量中(根据内存块的用处),便于以后操作。可以这样做:

pdx->RxDescVirBase=(PCHAR)pdx->descAddress;

pdx->RxDescPhyBase=(ULONG)(pdx->DescLogicalAddress.LowPart); //物理地址

WRITE_REGISTER_ULONG( (PULONG)&pdx->pHBARegs->RxAddr_des_0,pdx->rx_fc_des

c_buf_phy[0]+16);

WRITE_REGISTER_ULONG( (PULONG)&pdx->pHBARegs->RxAddr_des_addr0_ptr,pdx->r

x_fc_desc_buf_phy[0]+4);

注意前面的 +16 和 +4 实际分别代表美一块内存的的偏移位置,由于我们定义了这块内存的结构,并在里面布局,+16 其实代表了struct rx_fc_ctl_tab 结构中的 struct rx_fc_desc_entity entity[RX_FC_DESC_NUM] 域,这里又是一个结构,也就是我们最终会用到的“描述符”。注意:我们这里的操作是将一个地址写到一个寄存器,这个地址必须是物理地址,也就是前面说的LogicAddress。这里pHBARegs是通过pdx->pHBARegs=(PHBA_REGS)pdx->RegsBase;    
获得的,也就是在CmResourceTypeMemory传过来时解析,代表硬件提供的DMA寄存器首址。

对于硬件的初始化就是通过WRITE_REGISTER_ULONG()函数来完成,针对设备的起始地址来进行偏移,自己定义结构来匹配各个寄存器的内容,注意这个自己定义的结构一定要严格跟硬件寄存器相同,一bit都不能差。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902