设备注册
分析平台文件中的 platform_device结构体和platfor_add_devices函数
- 平台文件arch/arm/mach-exynos/mach-itop4412.c
- 设备注册函数文件driver/base/platform.c
int paltform_add_devices(struct platform_device **devs,)
{
int i ,ret = 0;
for(int i = 0;i < num;i++){
ret = platform_device_register(dev[i]);
if(ret){
while(--i >= 0){
platform_device_unregister(dev[i]);
break;
}
}
}
return ret;
}
以Module 的方式注册设备
#defind DRIVER_NAME "hello_ctl";
#include <linux/init.h>
#include <linux/module.h>
// 驱动注册的头文件和设备注册的头文件和卸载函数
#include <linux/platform_device.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("xubaipei")
module_init(hello_init);
module_exit(hello_exit);
struct platform_device platform_device_hello = {
.name = "my_code_led",
.id = -1,
.dev = {
.release = leds_release,
}
};
static void leds_realease(struct device* dev)
{
printk("leds_realease");
}
static int hello_init(void)
{
platform_device_register(&platform_device_hello);
return 0;
}
static int hello_exit(void)
{
platform_device_unregister(&platform_device_hello);
return 0;
}
编译完成后加载insmod xxx.ko 文件后在 /sys/devices/platform 中能查看到添加的设备‘my_code_led’
在驱动中调用设备的参数,设备信息打印前需要注册设备
#defind DRIVER_NAME "my_code_led";
#include <linux/init.h>
#include <linux/module.h>
// 驱动注册的头文件和设备注册的头文件和卸载函数
#include <linux/platform_device.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("xubaipei")
struct platform_driver hello_driver={
.probe = hello_probe,
.remove = hello_remove,
.suspend = hello_suspend,
.shutdown = hello_shutdown,
.resume = hello_resume,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
}
};
// 设备名匹配就会进入
static int hello_probe(struct platform_device *pdv)
{
printk(DRIVER_NAME,"\t initialized \n");
printk("pdv-> name is %s\n",pdv->name);
printk("pdv-> id is %s\n",pdv->id);
pdv->dev.realease(&pdv->dev);
return 0;
}
static int hello_remove(struct platform_device *pdv)
{
return 0;
}
static int hello_suspend(struct platform_device *pdv)
{
return 0;
}
static void hello_shutdown(struct platform_device *pdv)
{
}
static int hello_resume(struct platform_device *pdv)
{
return 0;
}
module_init(hello_init);
module_exit(hello_exit);
static void leds_realease(struct device* dev)
{
printk("leds_realease");
}
static int hello_init(void)
{
platform_device_register(&platform_device_hello);
return 0;
}
static int hello_exit(void)
{
platform_device_unregister(&platform_device_hello);
return 0;
}