linux 内核配置与驱动注册
menuconfig使用
使用menuconfig工具实现linux内核的裁剪。进入内核配置界面的方法包括以下几种:
#make config
这是基于文本的最为传统的配置界面,不推荐使用
#make menuconfig
基于文本菜单的配置界面,现在大部分都是使用这个工具来裁减配置内核的。
#make xconfig
要求 QT 被安装,用的比较少。
#make gconfig
要求 GTK,用的比较少。
#make oldconfig
如果只想在原来内核配置的基础上修改一些小地方,会省去不少麻烦
执行 make menuconfig进如配置界面 使用空格键进行选择 /键搜索
执行make menuconfig的最终目的是生成.config文件
设备注册
1.打开4412开发板的平台文件,注册一个hello_ctl的设备
设备注册时要用到一个平台设备注册结构体,注册平台结构体platform_device
vim arch/arm/mach-exynos/mach-itop4412.c
#ifdef platform_device s3c_device_hello_ctl={
.name="hello_ctl",
.id=-1;
}
#endif
添加宏定义文件
#ifdef CONFIG_HELLO_CTL
&s3c_device_hello_ctl,
#endif
内核文件的Kconfig文件添加menuconfig列表
config HELLO_CTL
tristate "Enable HELLO config"
default y
help
Enable HELLO config
使用ls /sys/devices/platform/查看新注册的设备
驱动注册
注册设备时一般先注册设备,后注册驱动。驱动注册需要和设备注册相互匹配名,设备注册时需要用到平台设备注册结构体platform_driver_register,下面是驱动注册例程,注册驱动名称为hello_ctl
#include <linux/module.h>//与module相关的信息
#include <linux/kernel.h>
#include <linux/init.h> //与init相关的函数
#include <linux/platform_device.h>
/*********************************
extern int platform_driver_register(struct platform_driver *);
extern void platform_driver_unregister(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 *);
**********************************/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangsan");
#define DRIVER_NAME "hello_ctl"
// 设备名称一定要和前面的相同
//这个函数是驱动找到,注册设备的结构体的关键
//如果设备和驱动匹配成功就会进入函数 ,并打印相关信息
static int hello_probe(struct platform_device *pdv)
{
printk(KERN_INFO "Hello_probe\n");
return 0;
}
static int hello_remove(struct platform_device *pdv)
{
printk(KERN_INFO "Hello_remove\n");
return 0;
}
static void hello_shutdown(struct platform_device *pdv)
{
printk(KERN_INFO "hello_shutdown\n");
}
static int hello_suspend(struct platform_device *pdv)
{
printk(KERN_INFO "Hello_suspend\n");
return 0;
}
static int hello_resume(struct platform_device *pdv)
{
printk(KERN_INFO "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,
}
};
static int hellodriver_init()
{
int Driverstate;
printk(KERN_INFO "Hello_init\n");
Driverstate=platform_driver_register(&hello_driver);
printk(KERN_INFO "Driverstate is %d\n",Driverstate);
return 0;
}
static void hellodriver_exit()
{
printk(KERN_INFO "Hello_exit\n");
platform_driver_unregister(&hello_driver);
}
module_init(hellodriver_init);
module_exit(hellodriver_exit);

1301

被折叠的 条评论
为什么被折叠?



