首先从这里开始:
static int __init pm8058_rtc_init(void)
{
return platform_driver_register(&pm8058_rtc_driver);
}
//__init标志对内核来讲是一个暗示,表明该函数只是在初始化时使用。模块加载器在模块加载后会丢掉这个初始化函数,这样可将该函数占用的内存释放出来,以作他用。
1-------------------------------------------------
平台设备注册函数原型:
注册平台设备驱动结构体pm8058_rtc_driver中driver成员中到bus、probe、remove、shutdown函数,这些函数在pm8058_rtc_driver结构体定义时并没有。参考platform_driver结构体原型和device_driver结构体原型
int platform_driver_register(struct platform_driver *drv)
{
drv->driver.bus = &platform_bus_type;
if (drv->probe)
drv->driver.probe = platform_drv_probe;
if (drv->remove)
drv->driver.remove = platform_drv_remove;
if (drv->shutdown)
drv->driver.shutdown = platform_drv_shutdown;
int driver_register(&drv->driver); //调用driver_register 函数,进行驱动注册;driver中成员现在包括bus、prob、remove、shutdown和name、owner、pm。
};
1.1-------------------------------------------------------
int driver_register(struct device_driver *drv)
{
int ret;
struct device_driver *other;
BUG_ON(!drv->bus->p); //参考关于BUG_ON()的一点笔记 //Linux内核的Oops//panic 和 bug_on //如果pm8058_rtc_driver结构体成员bus中p(bus_type_private结构体类型)为空时,会触发kernel painic
if ((drv->bus->probe && drv->probe) || //用来检查是否同时关联了设备驱动探测函数
(drv->bus->remove && drv->remove) ||
(drv->bus->shutdown && drv->shutdown))
printk(KERN_WARNING "Driver '%s' needs updating - please use "
"bus_type methods\n", drv->name);
other = driver_find(drv->name, drv->bus);
if (other) {
put_driver(other);
printk(KERN_ERR "Error: Driver '%s' is already registered, "
"aborting...\n", drv->name);
return -EBUSY;
}
ret = bus_add_driver(drv);
if (ret)
return ret;
ret = driver_add_groups(drv, drv->groups);
if (ret)
bus_remove_driver(drv);
return ret;
}
2----------------------------------------------------------
pm8058_rtc_driver结构体
pm8058_rtc_driver是平台结构体类型,通过作为platform_driver_register函数的参数传递到函数体内。
static struct platform_driver pm8058_rtc_driver = {
.probe = pm8058_rtc_probe,
.remove = __devexit_p(pm8058_rtc_remove),
.shutdown = pm8058_rtc_shutdown,
.driver = {
.name = "pm8058-rtc",
.owner = THIS_MODULE,
#ifdef CONFIG_PM
.pm = &pm8058_rtc_pm_ops,
#endif
},
};
2.1----------------------------------------------------
platform_driver结构体原型
struct platform_driver {
int (*probe)(struct platform_device *);//指向函数的指针probe,参数是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;
};
2.1.1----------------------------------------------------
device_driver 结构体原型
struct device_driver {
const char * name;
struct bus_type * bus;
struct completion unloaded;
struct kobject kobj;
struct klist klist_devices;
struct klist_node knode_bus;
struct module * owner;
int (*probe) (struct device * dev);
int (*remove) (struct device * dev);
void (*shutdown) (struct device * dev);
int (*suspend) (struct device * dev, pm_message_t state);
int (*resume) (struct device * dev);
};
参考文档:http://www.linuxforum.net/forum/showflat.php?Board=embedded&Number=754921