一:
static int __init gpio_keys_init(void)
{ /* 总线-设备-驱动 模型 :向上注册一个设备模型【纯软件方面】 */
return platform_driver_register(&gpio_keys_device_driver);
}
//入口函数里面对平台设备进行注册,平台设备内容为下面这个结构体
struct platform_driver gpio_keys_device_driver = {
.probe = gpio_keys_probe,
.remove = __devexit_p(gpio_keys_remove),
.driver = { .name = "gpio-keys", }
};
二:
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;
if (drv->suspend)
drv->driver.suspend = platform_drv_suspend;
if (drv->resume)
drv->driver.resume = platform_drv_resume;
return driver_register(&drv->driver);
}
三:
struct bus_type platform_bus_type = {
.name = "platform",
.dev_attrs = platform_dev_attrs,
.match = platform_match,
.uevent = platform_uevent,
.suspend = platform_suspend,
.suspend_late = platform_suspend_late,
.resume_early = platform_resume_early,
.resume = platform_resume,
};
static int platform_match(struct device * dev, struct device_driver * drv)
{
struct platform_device *pdev = container_of(dev, struct platform_device, dev);
return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
}//通过对名称进行比较判断device.name是否与drv.name相同
注释:
在这里struct platform_device定义为:
struct platform_device {
const char * name;
u32 id;
struct device dev;
u32 num_resources;
struct resource * resource;
};
注意,在这个结构体里面包含了一个叫device的结构体,可以将struct platform_device称为父结构体,将struct device称为子结构体。
在上述函数中,传递进来的dev指针将指向platform_device这个父结构体中的dev成员,而contianer_of这个宏的作用则在于通过这个结构体成员的指针获得其父结构体的指针,在这里就是platform_device这一结构体的指针。
四:
通过比较后如果匹配就会调用gpio_keys_probe函数进行下一步的操作。操作内容自定。