Driver Attributes and Device Attributes

 
Driver Attributes
~~~~~~~~~~~~~~~~~
struct driver_attribute {
          struct attribute         attr;
          ssize_t (*show)(struct device_driver *driver, char *buf);
          ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
};

Device drivers can export attributes via their sysfs directories.
Drivers can declare attributes using a DRIVER_ATTR macro that works
identically to the DEVICE_ATTR macro.

Example:

DRIVER_ATTR(debug,0644,show_debug,store_debug);

This is equivalent to declaring:

struct driver_attribute driver_attr_debug;

This can then be used to add and remove the attribute from the
driver's directory using:

int driver_create_file(struct device_driver *, const struct driver_attribute *);
void driver_remove_file(struct device_driver *, const struct driver_attribute *);


Device Attributes
~~~~~~~~~~~~~~~~~
struct device_attribute {
      struct attribute     attr;
      ssize_t (*show)(struct device *dev, struct device_attribute *attr,
              char *buf);
      ssize_t (*store)(struct device *dev, struct device_attribute *attr,
              const char *buf, size_t count);
};

Attributes of devices can be exported via drivers using a simple
procfs-like interface.

Please see Documentation/filesystems/sysfs.txt for more information
on how sysfs works.

Attributes are declared using a macro called DEVICE_ATTR:

#define DEVICE_ATTR(name,mode,show,store)

Example:

DEVICE_ATTR(power,0644,show_power,store_power);

This declares a structure of type struct device_attribute named
'dev_attr_power'. This can then be added and removed to the device's
directory using:

int device_create_file(struct device *device, struct device_attribute * entry);
void device_remove_file(struct device * dev, struct device_attribute * attr);

Example:

device_create_file(dev,&dev_attr_power);
device_remove_file(dev,&dev_attr_power);

The file name will be 'power' with a mode of 0644 (-rw-r--r--).
好的,下面是一个简单的WDF驱动程序代码,可以访问PCIe配置空间: ```cpp #include <ntddk.h> #include <wdf.h> DRIVER_INITIALIZE DriverEntry; EVT_WDF_DRIVER_UNLOAD OnDriverUnload; EVT_WDF_DRIVER_DEVICE_ADD OnDeviceAdd; EVT_WDF_DEVICE_CONTEXT_CLEANUP OnDeviceCleanup; typedef struct _DEVICE_CONTEXT { WDFDEVICE Device; } DEVICE_CONTEXT, *PDEVICE_CONTEXT; WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(DEVICE_CONTEXT, GetDeviceContext) NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath) { NTSTATUS status; WDF_DRIVER_CONFIG config; WDF_DRIVER_CONFIG_INIT(&config, OnDeviceAdd); config.EvtDriverUnload = OnDriverUnload; status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); if (!NT_SUCCESS(status)) { KdPrint(("WdfDriverCreate failed with status 0x%x\n", status)); return status; } return STATUS_SUCCESS; } VOID OnDriverUnload(_In_ WDFDRIVER Driver) { UNREFERENCED_PARAMETER(Driver); KdPrint(("Driver unloaded\n")); } NTSTATUS OnDeviceAdd(_In_ WDFDRIVER Driver, _Inout_ PWDFDEVICE_INIT DeviceInit) { NTSTATUS status; WDF_OBJECT_ATTRIBUTES attributes; PDEVICE_CONTEXT deviceContext; WDFDEVICE device; UNREFERENCED_PARAMETER(Driver); WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, DEVICE_CONTEXT); status = WdfDeviceCreate(&DeviceInit, &attributes, &device); if (!NT_SUCCESS(status)) { KdPrint(("WdfDeviceCreate failed with status 0x%x\n", status)); return status; } deviceContext = GetDeviceContext(device); // Access PCIe configuration space ULONG pciConfigSpaceAddress = 0x80000000; // example address, replace with your own ULONG data = 0; status = WdfDeviceQueryProperty(device, DevicePropertyPciConfigSpace, sizeof(pciConfigSpaceAddress), &pciConfigSpaceAddress, &data, sizeof(data), NULL); if (!NT_SUCCESS(status)) { KdPrint(("WdfDeviceQueryProperty failed with status 0x%x\n", status)); return status; } // do something with the data return STATUS_SUCCESS; } VOID OnDeviceCleanup(_In_ WDFOBJECT Object) { UNREFERENCED_PARAMETER(Object); KdPrint(("Device cleanup\n")); } ``` 这个驱动程序创建一个设备,并在设备创建时访问PCIe配置空间。在 `OnDeviceAdd` 回调函数中,使用 `WdfDeviceQueryProperty` 函数查询设备的PCIe配置空间,获取相应的数据,然后进行处理即可。其中,`DevicePropertyPciConfigSpace` 参数用于指定查询PCIe配置空间的属性,`pciConfigSpaceAddress` 参数用于指定配置空间地址。这里仅作为一个示例,实际应用中需要根据具体情况修改代码。 需要注意的是,在驱动程序中访问PCIe配置空间需要管理员权限,因此必须以管理员身份运行驱动程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值