Linux驱动开发(4)——驱动注册

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kakascx/article/details/83090102

结构体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 *)
    功能:注册驱动
    参数:驱动结构体

猜你喜欢

转载自blog.csdn.net/kakascx/article/details/83090102