Linux Kernel API

时间:2021-05-19 22:52:50
Linux Kernel API

记录一些Linux Device Drivers中常用的API。

Linux官方提供的内核文档:

1、 最新版: https://www.kernel.org/doc/html/latest/

2、 4.10: https://www.kernel.org/doc/html/v4.10/

3、 Understanding the Linux Virtual Memory Manager 或者查看PDF版本


一、内存分配

1.    void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)

2、 void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)

3、 void devm_kfree(struct device *dev, void *p)

4、 struct resource *platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num)
5、 int platform_get_irq(struct platform_device *dev, unsigned int num)
6、 struct resource *platform_get_resource_byname(struct platform_device *dev, unsigned int type, const char *name)
7、 int platform_get_irq_byname(struct platform_device *dev, const char *name)
示例(4、5、6、7几个API的调用):

 设备树:
reg = <0x15240000 0x2c000>,
<0x15204000 0x20000>;
reg-names = "slimbus_physical", "slimbus_bam_physical";
interrupts = < >, < >;
interrupt-names = "slimbus_irq", "slimbus_bam_irq"; 驱动: struct resource *bam_mem;
struct resource *slim_mem;
struct resource *irq, *bam_irq; slim_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
"slimbus_physical");
等价于: platform_get_resource(pdev, IORESOURCE_MEM, ) bam_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM,
"slimbus_bam_physical");
等价于: platform_get_resource(pdev, IORESOURCE_MEM, ) irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
"slimbus_irq");
等价于: platform_get_resource(pdev, IORESOURCE_IRQ, ) bam_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
"slimbus_bam_irq");
等价于: platform_get_resource(pdev, IORESOURCE_IRQ, )

8、  char *kstrdup(const char *s, gfp_t gfp);

功能: 分配一块内存空间(kmalloc + gfp),然后将s指向的字符串拷贝到这块内存中并返回该内存的首地址

9、void *kmemdup(const void *src, size_t len, gfp_t gfp);

功能:分配一块长度为len的内存(kmalloc+gfp),然后将src到(src + len)内存的内容copy到新分配的内存中,然后返回该内存的首地址

10、void *memdup_user(const void __user *src, size_t len);

功能: 分配一块长度为len的内存(kmalloc + gfp),然后将用户空间的地址src到(src+len)范围的内存使用copy_from_user的方式拷贝到

新分配的内存当中,最后返回新分配的内存的首地址

二、GPIO相关

1、static inline int of_get_named_gpio(struct device_node *np, const char *propname, int index)

获得GPIO号, 失败的话,会返回负数, 可以用gpio_is_valid来检查解析到的gpio是否合法:

 设备树:
qcom,cdc-us-euro-gpios = <&tlmm > 驱动:
pdata->us_euro_gpio = of_get_named_gpio(pdev->dev.of_node,
"qcom,cdc-us-euro-gpios", );

2、 int of_get_gpio(struct device_node *np, int index)

从设备树的gpios属性中解析出第index个gpio资源:

 设备树:
gpios = <&gpf1 GPIO_ACTIVE_HIGH>, <&gpf1 GPIO_ACTIVE_HIGH>;
驱动:
int sda_pin, scl_pin; sda_pin = of_get_gpio(np, );
scl_pin = of_get_gpio(np, );

of_get_gpio

三、端口映射

1、void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res)
示例:


2、 void devm_iounmap(struct device *dev, void __iomem *addr)

3、 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,  resource_size_t size)
4、 void __iomem *of_iomap(struct device_node *np, int index)

5、void __iomem *of_io_request_and_map(struct device_node *np, int index, const char *name)

四、设备树相关

1. bool of_property_read_bool(const struct device_node *np, const char *propname)

如果存在该属性,那么返回true,否则返回false
2. int of_property_read_u8(const struct device_node *np, const char *propname, u8 *out_value)
读取一个unsigned char, 成功返回0, 失败的话返回负数
这里需要注意的是: 设备树中对应的属性是 property_name = /bits/ 8 <0x50>

3. int of_property_read_u16(const struct device_node *np, const char *propname, u16 *out_value)
读取一个unsigned short, 成功返回0, 失败的话返回负数

这里需要注意的是: 设备树中对应的属性是 property = /bits/ 16 <0x5000 0x6000 0x7000>

4. int of_property_read_u32(const struct device_node *np, const char *propname, u32 *out_value)
读取一个unsigned int, 成功返回0, 失败的话返回负数

5、const struct of_device_id *of_match_node(const struct of_device_id *matches, const struct device_node *node)
返回matches中跟node相匹配的那一项, 如:

 设备树如下:
sound {
status = "okay";
compatible = "qcom,sdm660-asoc-snd";
... ...
} 驱动如下:
static const struct of_device_id sdm660_asoc_machine_of_match[] = {
{ .compatible = "qcom,sdm660-asoc-snd",
.data = "internal_codec"},
{ .compatible = "qcom,sdm660-asoc-snd-tasha",
.data = "tasha_codec"},
{ .compatible = "qcom,sdm660-asoc-snd-tavil",
.data = "tavil_codec"},
{},
}; static int msm_asoc_machine_probe(struct platform_device *pdev)
{
const struct of_device_id *match; match = of_match_node(sdm660_asoc_machine_of_match,
pdev->dev.of_node);
... ...
} static struct platform_driver sdm660_asoc_machine_driver = {
.driver = {
......
.of_match_table = sdm660_asoc_machine_of_match,
},
......
};
module_platform_driver(sdm660_asoc_machine_driver);

6、 struct device_node *of_parse_phandle(const struct device_node *np, const char *phandle_name, int index)

根据一个handle property解析得到对应的device node, 如:

  设备树:

          sound {
status = "okay";
compatible = "qcom,sdm660-asoc-snd";
... ...
qcom,us-euro-gpios = <0x1b4>;
... ...
}; msm_cdc_pinctrl@ {
compatible = "qcom,msm-cdc-pinctrl";
... ...
linux,phandle = <0x1b4>;
phandle = <0x1b4>;
}; 驱动:
struct device_node *node;
node = of_parse_phandle(pdev->dev.of_node,
"qcom,us-euro-gpios", );

五、中断

内核中其他一些不常见的用法示例:

完。