在将具体代码之前,先来科普一下ideapad是什么。
IdeaPad,中文名为“思想本”,是联想公司于2008年推出的笔记本电脑品牌。目的是为了弥补收购欧洲第四大PC制造商Packard Bell失败后国际市场的空缺,定义为消费级产品(与商务级的ThinkPad呼应),并取代的国际上的LENOVO3000系列和国内的天逸系列。
由于不是本文重点,简单介绍到这里就好。下边正式开始分析代码。
ideapad-laptop.c文件位于Linux内核源码根目录/drivers/platform/x86/下,将近1750行代码(Kernel版本5.18.8)。由于代码很长,分段进行分析。先来看第1段代码:
- MODULE_AUTHOR("David Woodhouse
" ); - MODULE_DESCRIPTION("IdeaPad ACPI Extras");
- MODULE_LICENSE("GPL");
这段代码无需多言,分别列出了内核模块的作者、功能描述和遵守的许可证。
再来看第2段代码:
- static const struct acpi_device_id ideapad_device_ids[] = {
- {"VPC2004", 0},
- {"", 0},
- };
- MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
-
- static struct platform_driver ideapad_acpi_driver = {
- .probe = ideapad_acpi_add,
- .remove = ideapad_acpi_remove,
- .driver = {
- .name = "ideapad_acpi",
- .pm = &ideapad_pm,
- .acpi_match_table = ACPI_PTR(ideapad_device_ids),
- },
- };
-
- module_platform_driver(ideapad_acpi_driver);
module_platform_driver是一个宏,位于Linux内核源码根目录/include/linux/platform_device.h中,其定义如下:
- /* module_platform_driver() - Helper macro for drivers that don't do
- * anything special in module init/exit. This eliminates a lot of
- * boilerplate. Each module may only use this macro once, and
- * calling it replaces module_init() and module_exit()
- */
- #define module_platform_driver(__platform_driver) \
- module_driver(__platform_driver, platform_driver_register, \
- platform_driver_unregister)
由代码注释可以看到,module_platform_driver替代了传统的module_init和module_exit。
module_driver函数在include/linux/device/driver.h中,定义如下:
- /**
- * module_driver() - Helper macro for drivers that don't do anything
- * special in module init/exit. This eliminates a lot of boilerplate.
- * Each module may only use this macro once, and calling it replaces
- * module_init() and module_exit().
- *
- * @__driver: driver name
- * @__register: register function for this driver type
- * @__unregister: unregister function for this driver type
- * @...: Additional arguments to be passed to __register and __unregister.
- *
- * Use this macro to construct bus specific macros for registering
- * drivers, and do not use it on its own.
- */
- #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);
将宏定义完全展开,过程及最终代码如下:
module_platform_driver(ideapad_acpi_driver)
---> 展开为:
module_driver(ideapad_acpi_driver, platform_driver_register, platform_driver_unregister)
---> 进一步展开为:
- static int __init ideapad_acpi_driver_init, platform_driver_register, platform_driver_unregister)
- {
- return platform_driver_resgister(&ideapad_acpi_driver);
- }
- module_init(ideapad_acpi_driver_init);
- static void __exit ideapad_acpi_driver_exit(void)
- {
- platform_driver_unregister(&ideapad_acpi_driver);
- }
- module_exit(ideapad_acpi_driver_exit);
其实还是“旧瓶装新酒”,所谓“换汤不换药,还是那一套”。
ideapad_acpi_driver中各个成员的解析,且听下回分解。