1. 程式人生 > >Linux驅動開發(4)——驅動註冊

Linux驅動開發(4)——驅動註冊

結構體platform_driver

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 *);//復位
        struct device_driver driver;
        const struct platform_device_id *id_table;
};

extern int platform_driver_register(struct platform_driver *);
extern void platform_driver_unregister(struct platform_driver *);

  • device_driver資料結構的兩個引數
    name和註冊的裝置name要一致
    owner一般賦值THIS_MODULE
    demo如下
#include <linux/init.h>
#include <linux/module.h>

/*驅動註冊的標頭檔案,包含驅動的結構體和註冊和解除安裝的函式*/
#include <linux/platform_device.h>

#define DRIVER_NAME "hello_ctl"

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");

static int hello_probe(struct platform_device *pdv){
	
	printk(KERN_EMERG "\tinitialized\n");
	
	return 0;
}

static int hello_remove(struct platform_device *pdv){
	
	return 0;
}

static void hello_shutdown(struct platform_device *pdv){
	
	;
}

static int hello_suspend(struct platform_device *pdv){
	
	return 0;
}

static int hello_resume(struct platform_device *pdv){
	
	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 hello_init(void)
{
	int DriverState;
	
	printk(KERN_EMERG "HELLO WORLD enter!\n");
	DriverState = platform_driver_register(&hello_driver);
	
	printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
	return 0;
}

static void hello_exit(void)
{
	printk(KERN_EMERG "HELLO WORLD exit!\n");
	
	platform_driver_unregister(&hello_driver);	
}

module_init(hello_init);
module_exit(hello_exit);

  • List platform_driver_register(struct platform_driver *)
    功能:註冊驅動
    引數:驅動結構體

  • platform_driver_unregisteritem(struct platform_driver *)
    功能:註冊驅動
    引數:驅動結構體