虚拟平台总线注册
步骤
- 注册设备 掌握注册设备的函数及流程
- 注册驱动 掌握注册驱动的函数及流程
平台:讯为电子开发板 exynose 4412
设备注册
- 注册函数介绍
5. int platform_device_register(struct platform_device *pdev)
6.所在目录: include/linux/platform_device.h
讯为电子提供的视屏中注册方式为:
1、修改平台下文件mach-itop4412.c
该文件中主要定义了大量注册设备的结构,存放结构的数组变量等;
添加设备时:定义一个设备结构体struct platform_device虚拟总线设备结构
#ifndef CONFIG_HELLO_CTL
struct platform_device s3c_device_hello_ctl = {
.name ="hello_ctl",
.id =-1,
};
endif
可以定义一个宏变量CONFIG_HELLO_CTL ,在driver/char/kconfig中添加如下脚本,目的在make menuconfig 中配置该设备是否加载控制作用。
config HELLO_CTL
bool "Enable HELLO config"
default y
help
Enable HELLO config
讯为电子实现平台使用了一个二维数组,将上述的接口添加到了数组中统一初始化
static struct platform_device *smdk4x12_devices[] __initdata = { #ifndef HELLO_CTL & s3c_device_hello_ctl #endif}
初始化实现
可以看到最终还是调用了platform_deviec_register函数注册设备;
int platform_add_devices(struct platform_device **devs, int num)
{
int i, ret = 0;
for (i = 0; i < num; i++) {
ret = platform_device_register(devs[i]);
if (ret) {
while (--i >= 0)
platform_device_unregister(devs[i]);
break;
}
}
return ret;
}
驱动注册
- 函数
注册:extern int platform_driver_register(struct platform_driver *);
卸载:extern int platform_driver_register(struct platform_driver *);
所在目录:include/linux/platform_device.h
设备驱动对应操作
struct platform_driver {
int (*probe)(struct platform_device *); 设备和驱动名mach后进入初始化函数
int (*remove)(struct platform_device *);移除设备时调用
下面一般是PM电源子系统操作使用
void (*shutdown)(struct platform_device *);关闭
int (*suspend)(struct platform_device *, pm_message_ t state); 挂在
int (*resume)(struct platform_device *); 唤醒
struct device_driver driver; 驱动和设备还有上层总线和kobject的连接点
const struct platform_device_id *id_table;
};
#include <linux/module.h>
#include <linux/init.h>
#define DRIVER_NAME "hello_ctl"
int hello_probe(struct platform_device *dev)
{
printk(KERN_EMERG "ENTER hello_probe \n");
return 0;
}
int hello_remove(struct platform_device *dev)
{
printk(KERN_EMERG "ENTER hello_remove \n");
return 0;
}
void hello_shutdown(struct platform_device *dev)
{
printk(KERN_EMERG "ENTER hello_shutdown \n");
}
int hello_suspend(struct platform_device *dev, pm_message_t state)
{
printk(KERN_EMERG "ENTER hello_suspend \n");
}
int hello_resume(struct platform_device *dev)
{
printk(KERN_EMERG "ENTER hello_resume \n");
return 0;
}
struct platform_driver hello_driver={
.probe = hello_probe,
.remove = hello_remove,
.shutdown=hello_shutdown,
.suspend = hello_suspend,
.resume = hello_resume,
.driver ={
.name = DRIVER_NAME,
.owner = THIS_MODULE,
}
};
int hello_init(void)
{
platform_driver_register(&hello_driver);
printk(KERN_EMERG " enter hello_init\n");
return 0;
}
int hello_exit(void)
{
platform_driver_unregister(&hello_driver);
printk(KERN_EMERG " enter hello_exit\n");
return 0;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LISENCE("Dual BSD/GPL");
Makefile
这个可以参考上讲makefile最简单模块编译
#/bin/bash
obj-m +=hello.o
pwd=$(shell pwd)
KDIR :=/home/topeet/android4.0/iTop4412_Kernel_3.0
all:
make -C $(KDIR) M:=$(pwd) modules
clean:
rm -rf *.o
转载:https://blog.csdn.net/c4679281314/article/details/102172847
查看评论