Linux驱动之Platform Driver
# Platform Driver 平台驱动
Linux中的所有设备驱动都需要注册到系统平台下,这此操作由platform_device.h中定义的一组函数完成.我们先来看看struct platform_driver这个结构体:
view plaincopy to clipboardprint?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;
const struct platform_device_id *id_table;
};
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;
const struct platform_device_id *id_table;
};
该结构中包含了一组操作函数和一个struct device_driver的对像. 在我们自己的驱动中首先要做的就是定义platform_driver中的函数,并创建这个结构的一个对象实例, 然后在init()函数中调用platform_driver_register()向系统注册我们的驱动.
如kernel-3.0的leds-s3c24xx.c 中的platform_driver的定义为:
view plaincopy to clipboardprint?static struct platform_driver s3c24xx_led_driver = {
.probe = s3c24xx_led_probe,
.remove = s3c24xx_led_remove,
.driver = {
.name = "s3c24xx_led",
.owner = THIS_MODULE,
},
};
static struct platform_driver s3c24xx_led_driver = {
.probe = s3c24xx_led_probe,
.remove = s3c24xx_led_remove,
.driver = {
.name = "s3c24xx_led",
.owner = THIS_MODULE,
},
};
其它中指定了.driver的名字和所有者. 在调用platform_driver_register()注册驱动时,platform首先指定该设备是在系统中platform_bus主总线下,这只是一个虚拟总线驱动.然后platform驱动在调用driver_register(), driver_register在调用bus_add_driver()向系统总线管理器中注册驱动.如果成功会进行设备扫描,在dd.c中的driver_attach()中进行.实际会调用__driver_attach(),该函数又调用driver_probe_device(), driver_probe_device()分别会调用驱动注册时指定的总线probe和驱动自己的probe函数, 即我们上面注册的s3c24xx_led_probe()函数, 总线probe函数由platform_driver_register()中设置, 还设置了默认的Driver的操作函数.
在我们的probe函数中主要是进行设备的探测和初始化,这里2410的led是连接到gpio上的,所以总是存在,但是如果是一些像usb那样的外设,我们就需要先探测对外的外设是否存在.在sc324xx_led_probe中主要做了以下3件
相关新闻>>
- 发表评论
-
- 最新评论 更多>>