平台设备驱动之我见一

本章介绍Linux平台设备的基础知识。

1
2
3
4
5
6
7
8
_______  _        _______ _________ _______  _______  _______  _______
( ____ )( \ ( ___ )\__ __/( ____ \( ___ )( ____ )( )
| ( )|| ( | ( ) | ) ( | ( \/| ( ) || ( )|| () () |
| (____)|| | | (___) | | | | (__ | | | || (____)|| || || |
| _____)| | | ___ | | | | __) | | | || __)| |(_)| |
| ( | | | ( ) | | | | ( | | | || (\ ( | | | |
| ) | (____/\| ) ( | | | | ) | (___) || ) \ \__| ) ( |
|/ (_______/|/ \| )_( |/ (_______)|/ \__/|/ \|

1.platform_bus_type

总线驱动设备三个基本要素中,总线居于中心位置,通过其中的match函数,将驱动和设备建立联系。

1
2
3
4
5
6
7
struct bus_type platform_bus_type = {
.name = "platform",
.dev_attrs = platform_dev_attrs,
.match = platform_match,
.uevent = platform_uevent,
.pm = &platform_dev_pm_ops,
};

platform_match源码可知,如果存在id则使用id进行匹配,否则使用name进行字符串匹配。

1
2
3
4
5
6
7
8
9
10
11
12
static int platform_match(struct device *dev, struct device_driver *drv)
{
struct platform_device *pdev = to_platform_device(dev);
struct platform_driver *pdrv = to_platform_driver(drv);

/* match against the id table first */
if (pdrv->id_table)
return platform_match_id(pdrv->id_table, pdev) != NULL;

/* fall-back to driver name match */
return (strcmp(pdev->name, drv->name) == 0);
}

所以就能够理解驱动源码中的id_tables的含义了

1
2
3
4
5
6
7
8
9
static struct platform_device_id s3c24xx_driver_ids[] = {
{
.name = "s3c2410-i2c",
.driver_data = TYPE_S3C2410,
}, {
.name = "s3c2440-i2c",
.driver_data = TYPE_S3C2440,
}, { },
};

每一项表征能够支持一款设备。

2.platform_device

1
2
3
4
5
6
7
8
9
10
struct platform_device {
const char * name;
int id;
struct device dev;
u32 num_resources;
struct resource * resource;
struct platform_device_id *id_entry;
/* arch specific additions */
struct pdev_archdata archdata;
};

在该结构体中定义了ID以及name,在想平台总线注册设备的时候就会依据name或ID进行匹配。
dev,真正的设备,通过 container_of ,就能找到整个platform_device。

3.platform_device_dirver

1
2
3
4
5
6
7
8
9
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;
struct platform_device_id *id_table;
};

在driver中主要要实现probe函数,这是平台匹配到设备时第一个要调用的函数,同时设置好id_tables这样平台驱动才能够进行匹配。

4.接口

4.1设备接口

对于设备平台提供了如下接口,这也就是说我们在编写设备文件时能够调用这些接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* include/linux/platform_device.h */
//Platform设备的注册/注销接口
extern int platform_device_register(struct platform_device *);
extern void platform_device_unregister(struct platform_device *);

//arch_setup_pdev_archdata,设置platform_device变量中的archdata指针。
extern void arch_setup_pdev_archdata(struct platform_device *);

//以下这些接口,可以获取platform_device变量中的resource信息,以及直接获取IRQ的number等等。
extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
extern int platform_get_irq(struct platform_device *, unsigned int);
extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, const char *);
extern int platform_get_irq_byname(struct platform_device *, const char *);
extern int platform_add_devices(struct platform_device **, int);

extern struct platform_device *platform_device_register_full(const struct platform_device_info *pdevinfo);

static inline struct platform_device *platform_device_register_resndata(
struct device *parent, const char *name, int id,
const struct resource *res, unsigned int num,
const void *data, size_t size)

static inline struct platform_device *platform_device_register_simple(
const char *name, int id,
const struct resource *res, unsigned int num)

static inline struct platform_device *platform_device_register_data(
struct device *parent, const char *name, int id,
const void *data, size_t size)

extern struct platform_device *platform_device_alloc(const char *name, int id);
extern int platform_device_add_resources(struct platform_device *pdev,
const struct resource *res,
unsigned int num);
extern int platform_device_add_data(struct platform_device *pdev,
const void *data, size_t size);
extern int platform_device_add(struct platform_device *pdev);
extern void platform_device_del(struct platform_device *pdev);
extern void platform_device_put(struct platform_device *pdev);

4.2驱动接口

1
2
3
platform_driver_registe、platform_driver_unregister,platform driver的注册、注销接口。
platform_driver_probe,主动执行probe动作。
platform_set_drvdata、platform_get_drvdata,设置或者获取driver保存在device变量中的私有数据。

5.简单示例

设备文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <linux/input.h>
#include <linux/platform_device.h>
// 设备资源
static struct resource led_resource[] = { //jz2440的参数,驱动未测试
[0] = {
.start = 0x56000010,
.end = 0x56000010 + 8 - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = 5,
.end = 5,
.flags = IORESOURCE_IRQ,
},
};

static void led_release(struct device *dev){

}

// 创建一个设备
static struct platform_device led_dev = {
.name = "myled", //设备名字 与 驱动相匹配
.id = -1,
.num_resources = ARRAY_SIZE(led_resource),
.resource = led_resource,

.dev = {
.release = led_release,
//.devt = MKDEV(252, 1),
},
};

static int led_dev_init(void){

//向bus注册led_dev match drv链表进行配对
platform_device_register(&led_dev);
return 0;
}

static void led_dev_exit(void){
platform_device_unregister(&led_dev);
}
module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");

驱动文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/irq.h>
#include <asm/uaccess.h>

#include <linux/platform_device.h>
#include <linux/io.h>

static int major;

static struct class *cls;
static struct device *dev;

static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin;

static int led_open(struct inode *inode, struct file *file){

*gpio_con &= ~(0x03 << (pin*2));
*gpio_con |= (0x01 << (pin*2));
return 0;
}

static ssize_t led_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos){

int val;
copy_from_user(&val, buf, count);

if(val == 1){

*gpio_dat &= ~(1<<pin);
}else{

*gpio_dat &= (1<<pin);
}

return 0;
}

static struct file_operations led_fops = {

.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};

static int led_probe(struct platform_device *pdev){

struct resource *res;
// 最后一个参数 0 表示第1个该类型的资源
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
gpio_con = ioremap(res->start, res->end - res->start + 1);
gpio_dat = gpio_con + 1;

res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
pin = res->start;

printk("led_probe, found led\n");

// 注册设备驱动 创建设备节点
major = register_chrdev(0, "myled", &led_fops);
// 创建类
cls = class_create(THIS_MODULE, "myled");
// 创建设备节点
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "led");

return 0;
}

static int led_remove(struct platform_device *pdev){

printk("led_remove, remove led\n");
// 删除设备节点
device_unregister(dev);
// 销毁类
class_destroy(cls);
// 取消注册设备驱动
unregister_chrdev(major, "myled");
// 取消内存映射
iounmap(gpio_con);

return 0;
}

struct platform_driver led_drv = {

.probe = led_probe, //匹配到dev之后调用probe
.remove = led_remove,
.driver = {
.name = "myled",
},
};

static int led_drv_init(void){

platform_driver_register(&led_drv);
return 0;
}

static void led_drv_exit(void){

platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");

版权声明:本文为博主原创文章,转载需声明为转载内容并添加原文地址。

原文地址:https://coderdock.com

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2017-2020 Dock
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信