1. 程式人生 > >linux核心配置與驅動註冊

linux核心配置與驅動註冊

linux 核心配置與驅動註冊

使用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);