2016/6/28

driver init

static struct platform_driver imx_wm8962_driver = {
        .driver = {
                .name = "imx-wm8962",
                .owner = THIS_MODULE,
                .pm = &snd_soc_pm_ops,
                .of_match_table = imx_wm8962_dt_ids,
        },
        .probe = imx_wm8962_probe,
        .remove = imx_wm8962_remove,
};
module_platform_driver(imx_wm8962_driver);


module_platform_driver(imx_wm8962_drvier) 展開:(platform_device.h)
#define module_platform_driver(__platform_driver) \
        module_driver(__platform_driver, platform_driver_register, \
                        platform_driver_unregister)
所以
module_driver(imx_wm8962_driver, platform_driver_register, platform_driver_unregister)


module_driver 再展開..(device.h)
#define module_driver(__driver, __register, __unregister, ...) \
static int __init __driver##_init(void) \
{ \
        return __register(&(__driver) , ##__VA_ARGS__); \
} \
module_init(__driver##_init); \
static void __exit __driver##_exit(void) \
{ \
        __unregister(&(__driver) , ##__VA_ARGS__); \
} \
module_exit(__driver##_exit);

所以..

static int __init imx_wm8962_driver_init(void)
{
    return platform_driver_register( &(imx_wm8962_driver) );
}
module_init(imx_wm8962_driver_init)'

static void __exit imx_wm8962_driver_exit(void)
{
    platform_driver_unregister(&(imx_wm8962_driver));
}
module_exit(imx_wm8962_exit);


最後的 macro : module_init, module_exit: (init.h)
#define module_init(x)  __initcall(x);
#define module_exit(x)  __exitcall(x);

然後

#define __initcall(fn) device_initcall(fn)

#define __exitcall(fn) \
        static exitcall_t __exitcall_##fn __exit_call = fn

所以要去看 platform_driver_register ( platform.c )

int platform_driver_register(struct platform_driver *drv)
{
        printk("%s -- %pS\n",__func__,__builtin_return_address(0));
        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;

        return driver_register(&drv->driver);
}
他把 依照宣告的 platform_driver 變數內容,設定好 driver.driver 的資料,call driver_register( )
-- 其中 platform_drv_probe/remove/shutdown 也都在這個 source file 中。

driver_register -- bus_add_driver -- driver__attach -- driver_probe_device -- really_probe



bus: 'platform': driver_probe_device: matched device sound-wm8962.33 with driver imx-wm8962


log:
do_one_initcall imx_wm8962_driver_init
platform_driver_register
bus_add_driver
...

沒有留言:

張貼留言