嵌入式学习笔记(六)-设备注册

设备注册

分析平台文件中的 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值