注册设备的两种方式:
方法一:
一:对于平台设备可以直接在板文件中添加代码(arch / arm / mach-s3cxxxx / mach-smdk2440.c)
代码 1: static struct platfrom_device hello_device = {
.name = "hello ",
.id = -1,
.resource = hello_resource,
...................
.......................
}; 添加设备结构体
代码2:继续添加设备资源(arch / arm / mach-s3cxxxx / mach-smdk2440.c)
static struct resource hello_resource[] = {
[0] = { //IO端口资源
.start = S3C24XX_PA_WATCHDOG,
.end = S3C24XX_PA_WATCHDOG + S3C24XX_SZ_WATCHDOG - 1,
.flags = IORESOURCE_MEM,
},
[1] = { //中断资源
.start = IRQ_WDT, .end = IRQ_WDT,
.flags = IORESOURCE_IRQ,
}
};
3:继续把设备加入到设备数组中去arch / arm / mach-s3cxxxx / mach-smdk2440.c
- static struct platform_device *smdk2440_devices[] __initdata = {
- &s3c_device_usb,
- &s3c_device_lcd,
- &s3c_device_wdt,
- &s3c_device_i2c0,
- &s3c_device_iis,
- &hello_device,
- };
-
之后系统在加载该驱动的时候会自动的调用platform_add_devices把设备注册到系统中去,该函数内部会调用platfrom_device_register().设备注册完闭
方法二:
直接定义即可:1。定义设备结构体
static struct platfrom_device hello_device = {
.name = "hello ",
.id = -1,
.resource = hello_resource,
...................
.......................
};
2。定义设备资源
static struct resource hello_resource[] = {
[0] = { //IO端口资源
.start = S3C24XX_PA_WATCHDOG,
.end = S3C24XX_PA_WATCHDOG + S3C24XX_SZ_WATCHDOG - 1,
.flags = IORESOURCE_MEM,
},
3。注册设备 platfrom_device_register().
static int __init hello_init(void)
{
printk(banner);
return platform_device_register(&hello_device);
}
static void __exit hello_exit(void)
{
platform_device_unregister(&hello_device );
}
module_init(hello_init);
module_exit(hello_exit);