From f10258d4074af83da45a42632f394716b0ab87a7 Mon Sep 17 00:00:00 2001 From: "rick.chan" Date: Fri, 5 Jun 2020 17:29:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Linux=20Kernel=20Devm=20AP?= =?UTF-8?q?Is.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: rick.chan --- .../Kernel/API/Linux_Kernel_Devm_APIs.md | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 Software/Development/OperatingSystem/Linux/Kernel/API/Linux_Kernel_Devm_APIs.md diff --git a/Software/Development/OperatingSystem/Linux/Kernel/API/Linux_Kernel_Devm_APIs.md b/Software/Development/OperatingSystem/Linux/Kernel/API/Linux_Kernel_Devm_APIs.md new file mode 100644 index 0000000..1a0bdc8 --- /dev/null +++ b/Software/Development/OperatingSystem/Linux/Kernel/API/Linux_Kernel_Devm_APIs.md @@ -0,0 +1,162 @@ +# Linux Kernel Devm APIs + +Devm APIs 是一组便捷易用的接口,通过该接口申请的内核资源是跟设备(device)有关的,在设备(device)被detached或者驱动(driver)卸载(unloaded)时,会被自动释放。 + +## devm_kmalloc 函数 + +**函数原型:** + +```cpp +void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp); +``` + +**说明:** + +与 kmalloc 类似,用于申请内存。 + +**参数:** + +dev:设备结构体; + +size:要分配的内存大小,以字节为单位; + +其他:参见 kmalloc 的参数说明。 + +**返回值:** + +申请成功返回有效的内存首地址,失败返回 NULL。 + +## devm_kzalloc 函数 + +**函数原型:** + +```cpp +static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp); +``` + +**说明:** + +与 kzalloc 类似,用于申请内存,实际上只是额外附加了 __GFP_ZERO 标志。所以它除了申请内核内存外,还会对申请到的内存内容清零。 + +**参数:** + +dev:设备结构体; + +size:要分配的内存大小,以字节为单位; + +其他:参见 kmalloc 的参数说明。 + +**返回值:** + +申请成功返回有效的内存首地址,失败返回 NULL。 + +## devm_ioremap_resource 函数 + +**函数原型:** + +```cpp +void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res); +``` + +**说明:** + +check, request region, and ioremap resource。 + +Checks that a resource is a valid memory region, requests the memory region and ioremaps it. All operations are managed and will be undone on driver detach. + +Usage example: + +```cpp +res = platform_get_resource(pdev, IORESOURCE_MEM, 0); +base = devm_ioremap_resource(&pdev->dev, res); +if (IS_ERR(base)) + return PTR_ERR(base); +``` + +**参数:** + +dev:设备结构体; + +res:resource to be handled。 + +**返回值:** + +Returns a pointer to the remapped memory or an ERR_PTR() encoded error code on failure. + +## devm_request_mem_region 函数 + +**函数原型:** + +```cpp +#define devm_request_mem_region(dev,start,n,name) +``` + +**说明:** + +与 request_mem_region 类似,用于请求 mmio 内存空间。 + +**参数:** + +dev:设备结构体; + +start:要请求的首地址; + +n:请求长度; + +name:该 mmio 空间的名称。 + +**返回值:** + +成功返回非 0 值,失败返回 NULL。 + +## devm_ioremap 函数 + +**函数原型:** + +```cpp +void __iomem *devm_ioremap(struct device *dev, resource_size_t offset, + resource_size_t size); +``` + +**说明:** + +Managed ioremap(). Map is automatically unmapped on driver detach. + +**参数:** + +dev:设备结构体; + +offset:Resource address to map; + +size:Size of map. + +**返回值:** + +成功则返回映射内存首地址,失败返回 NULL。 + +## devm_ioremap_nocache 函数 + +**函数原型:** + +```cpp +void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset, + resource_size_t size); +``` + +**说明:** + +Managed ioremap_nocache(). Map is automatically unmapped on driver detach. + +用于实现非缓存的内存映射。 + +**参数:** + +dev:设备结构体; + +offset:Resource address to map; + +size:Size of map. + +**返回值:** + +成功则返回映射内存首地址,失败返回 NULL。