增加 Linux Kernel Devm APIs.
Signed-off-by: rick.chan <chenyang@autoai.com>
This commit is contained in:
parent
e580f87fe8
commit
f10258d407
|
@ -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。
|
Loading…
Reference in New Issue