1. 程式人生 > >Linux驅動開發(3)——以module方式註冊裝置

Linux驅動開發(3)——以module方式註冊裝置

通過 s3c_device_leds_ctl->*smdk4x12_devices[]->platform_add_devices()->platform_device_register()
可以直接使用“platform_device_register()”來註冊裝置

#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 void	leds_release(struct device *dev)
{
	printk("leds_release");
}

struct platform_device platform_device_hello = {
	.name   = "my_code_led",
	.id     = -1,
	.dev = {
		.release = leds_release,//必須向核心提供一個 release 函式,否則釋放的時候會報錯
	}
};

static int hello_init(void)
{
	platform_device_register(&platform_device_hello);
	return 0;
}

static void hello_exit(void)
{
	platform_device_unregister(&platform_device_hello);
}

module_init(hello_init);
module_exit(hello_exit);

  • platform_device_register(struct platform_device *pde)
    功能:註冊裝置
    引數:要註冊裝置的結構體

  • platform_device_unregister(struct platform_device *pde)

  •  功能:解除安裝裝置
     引數:要解除安裝裝置的結構體
    

platform_device 結構體包括名字、id和釋放函式三要素