linux内核中常用的一些宏及函数

来源:互联网 发布:云计算技术与应用就业 编辑:程序博客网 时间:2024/06/05 13:30

 1 ARRAY_SIZE

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))  // 该宏可以方便的求出一个数组(x)中有多少数据成员;

2 struct resource

struct resource结构实际上描述了该设备占用的硬件资源(如地址空间,中断号等s), 如:

static struct resource s3c_lcd_resource[] = {
       [0] = {
              .start = S3C24XX_PA_LCD,
              .end   = S3C24XX_PA_LCD + S3C24XX_SZ_LCD - 1,
              .flags = IORESOURCE_MEM,   //地址空间
       },
       [1] = {
              .start = IRQ_LCD,
              .end   = IRQ_LCD,
              .flags = IORESOURCE_IRQ,  //中断资源
       }
};

 

3 platform(struct platform_driver struct platform_device )

3.1 struct platform_device 相关

struct platform_device s3c_device_rtc = {
.name    = "s3c2410-rtc",//需与驱动里面的name要一样
.id    = -1,
.num_resources   = ARRAY_SIZE(s3c_rtc_resource),
.resource   = s3c_rtc_resource,
};

EXPORT_SYMBOL(s3c_device_rtc);

arch/arm/mach-s3c2440/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,
&s3c_device_rtc,   //上面定义
};
static void __init smdk2440_machine_init(void)
{
s3c24xx_fb_set_platdata(&smdk2440_fb_info);
s3c_i2c0_set_platdata(NULL);

platform_add_devices(smdk2440_devices, ARRAY_SIZE(smdk2440_devices));   //添加上面定义的设备
smdk_machine_init();
}

3.2 struct platform_driver

static struct platform_driver rtc_driver = 
{
    .probe   = rtc_probe, 
    .remove = __devexit_p(rtc_remove

    .suspend = rtc_suspend,  
    .resume = rtc_resume,  
    .driver =
    { 
        .name   = "s3c2410-rtc",   // 一定要和平台设备的名称一致,这样才能把二者联系起来
        .owner = THIS_MODULE,
    },
};

static int __init rtc_init(void)

    return platform_driver_register(&rtc_driver);
}

static void __exit rtc_exit(void)

    platform_driver_unregister(&rtc_driver);
}

module_init(rtc_init);
module_exit(rtc_exit);

3.3 设备类的实现(class)