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的调用):
1 设备树: 2 reg = <0x15240000 0x2c000>, 3 <0x15204000 0x20000>; 4 reg-names = "slimbus_physical", "slimbus_bam_physical"; 5 interrupts = <0 291 0>, <0 292 0>; 6 interrupt-names = "slimbus_irq", "slimbus_bam_irq"; 7 8 驱动: 9 10 struct resource *bam_mem; 11 struct resource *slim_mem; 12 struct resource *irq, *bam_irq; 13 14 15 slim_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, 16 "slimbus_physical"); 17 等价于: platform_get_resource(pdev, IORESOURCE_MEM, 0) 18 19 bam_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, 20 "slimbus_bam_physical"); 21 等价于: platform_get_resource(pdev, IORESOURCE_MEM, 1) 22 23 irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, 24 "slimbus_irq"); 25 等价于: platform_get_resource(pdev, IORESOURCE_IRQ, 0) 26 27 bam_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, 28 "slimbus_bam_irq"); 29 等价于: platform_get_resource(pdev, IORESOURCE_IRQ, 1)
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是否合法:
1 设备树:
2 qcom,cdc-us-euro-gpios = <&tlmm 128 0>
3
4 驱动:
5 pdata->us_euro_gpio = of_get_named_gpio(pdev->dev.of_node,
6 "qcom,cdc-us-euro-gpios", 0);
2、 int of_get_gpio(struct device_node *np, int index)
从设备树的gpios属性中解析出第index个gpio资源:
1 设备树: 2 gpios = <&gpf1 5 GPIO_ACTIVE_HIGH>, <&gpf1 4 GPIO_ACTIVE_HIGH>; 3 驱动: 4 int sda_pin, scl_pin; 5 6 sda_pin = of_get_gpio(np, 0); 7 scl_pin = of_get_gpio(np, 1);
三、端口映射
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相匹配的那一项, 如:
1 设备树如下:
2 sound {
3 status = "okay";
4 compatible = "qcom,sdm660-asoc-snd";
5 ... ...
6 }
7
8 驱动如下:
9 static const struct of_device_id sdm660_asoc_machine_of_match[] = {
10 { .compatible = "qcom,sdm660-asoc-snd",
11 .data = "internal_codec"},
12 { .compatible = "qcom,sdm660-asoc-snd-tasha",
13 .data = "tasha_codec"},
14 { .compatible = "qcom,sdm660-asoc-snd-tavil",
15 .data = "tavil_codec"},
16 {},
17 };
18
19 static int msm_asoc_machine_probe(struct platform_device *pdev)
20 {
21 const struct of_device_id *match;
22
23 match = of_match_node(sdm660_asoc_machine_of_match,
24 pdev->dev.of_node);
25 ... ...
26 }
27
28 static struct platform_driver sdm660_asoc_machine_driver = {
29 .driver = {
30 ......
31 .of_match_table = sdm660_asoc_machine_of_match,
32 },
33 ......
34 };
35 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, 如:
1 设备树:
2
3 sound {
4 status = "okay";
5 compatible = "qcom,sdm660-asoc-snd";
6 ... ...
7 qcom,us-euro-gpios = <0x1b4>;
8 ... ...
9 };
10
11 msm_cdc_pinctrl@75 {
12 compatible = "qcom,msm-cdc-pinctrl";
13 ... ...
14 linux,phandle = <0x1b4>;
15 phandle = <0x1b4>;
16 };
17
18
19
20 驱动:
21 struct device_node *node;
22 node = of_parse_phandle(pdev->dev.of_node,
23 "qcom,us-euro-gpios", 0);
五、中断
内核中其他一些不常见的用法示例:
完。
本文来自博客园,作者:摩斯电码,未经同意,禁止转载