查看: 2309|回复: 0

[原创] 米尔 mys-y6ull-iot,驱动代码解析

[复制链接]
  • TA的每日心情
    开心
    2024-1-16 17:48
  • 签到天数: 592 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2018-9-8 00:44:08 | 显示全部楼层 |阅读模式
    分享到:
    这个是几个重要的数据结构,看看里面有啥东西:
    1. /* summary-2 */
    2. module:
    3.         kobject
    4.         (ops)
    5. class:
    6.         module
    7.         kobject
    8.         (ops)
    9. device:
    10.         class
    11.         bus_type
    12.         kobject
    13.         device_driver:
    14.                 bus_type
    15.                 module
    16.                 (ops:probe,remove,shutdown,suspend,resume)
    复制代码
    这是宏定义,解析出来没啥东西,对照链接文件看,知道在哪里,后面看内核,知道怎么找到这些东西的:

    1. /* MODULE */
    2.         (include/linux/device.h):       
    3.         MODULE_LICENSE
    4.         MODULE_AUTHOR
    5.         MODULE_DESCRIPTION
    6.         MODULE_ALIAS
    7.         MODULE_SOFTDEP
    8.         MODULE_FIRMWARE
    9.         MODULE_DEVICE_TABLE
    10.         MODULE_VERSION
    11.                
    12. /* MODULE_LICENSE */
    13.         (include/linux/device.h):       
    14.         #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
    15.         static char license[128];
    16.         MODULE_LICENSE("GPL v2")=
    17.                 MODULE_INFO(license, _license)
    18.                
    19.         #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
    20.         MODULE_LICENSE("GPL v2")=
    21.                 MODULE_INFO(license, _license)
    22.         MODULE_LICENSE("GPL v2")=
    23.                 __MODULE_INFO(tag, tag, info)
    24.        
    25.         (include/linux/moduleparam.h)
    26.         #define __MODULE_INFO(tag, name, info)                                          \
    27.         static const char __UNIQUE_ID(name)[]                                          \
    28.                   __used __attribute__((section(".modinfo"), unused, aligned(1)))          \
    29.                   = __stringify(tag) "=" info
    30.         MODULE_LICENSE("GPL v2")=
    31.                 MODULE_INFO(license, _license)
    32.         MODULE_LICENSE("GPL v2")=
    33.                 __MODULE_INFO(tag, tag, info)       
    34.         MODULE_LICENSE("GPL v2")=
    35.                 static const char __UNIQUE_ID(name)[] __used __attribute__((section(".modinfo"), unused, aligned(1)))
    36.                                                                                         = __stringify(tag) "=" info
    37.        
    38.         (include/linux/stringify.h)
    39.         #define __stringify_1(x...)        #x
    40.         #define __stringify(x...)        __stringify_1(x)
    41.         MODULE_LICENSE("GPL v2")=
    42.                 license = "GPL v2"
    43.        
    44. /* MODULE_AUTHOR */
    45.         (include/linux/device.h):
    46.         #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
    47.         MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>")=
    48.                 MODULE_INFO(author, _author)
    49.                 author = "Benjamin Tissoires <benjamin.tissoires@gmail.com>"

    50. /* MODULE_DESCRIPTION */               
    51.         (include/linux/device.h):
    52.         #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
    53.         MODULE_DESCRIPTION("Goodix touchscreen driver")=
    54.                 MODULE_INFO(description, _description)
    55.                 description = "Goodix touchscreen driver"

    56. /* MODULE_ALIAS */
    57.         (include/linux/device.h):
    58.         #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
    59.         MODULE_ALIAS("abc")=
    60.                 MODULE_INFO(alias, _alias)
    61.                 alias = "abc"

    62. /* MODULE_SOFTDEP */
    63.         (include/linux/device.h):
    64.         #define MODULE_SOFTDEP(_softdep) MODULE_INFO(softdep, _softdep)
    65.         MODULE_SOFTDEP("abc")=
    66.                 MODULE_INFO(softdep, _softdep)
    67.                 softdep = "abc"
    68.                                
    69. /* MODULE_FIRMWARE */
    70.         (include/linux/device.h):
    71.         #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
    72.         MODULE_FIRMWARE("abc")=
    73.                 MODULE_INFO(firmware, _firmware)
    74.                 firmware = "abc"
    75.                
    76. /* MODULE_DEVICE_TABLE */
    77.         (include/linux/device.h):
    78.         #define MODULE_DEVICE_TABLE(type, name)                                        \
    79.         extern const typeof(name) __mod_##type##__##name##_device_table                \
    80.           __attribute__ ((unused, alias(__stringify(name))))
    81.         MODULE_DEVICE_TABLE(of, goodix_of_match)=
    82.                 extern const typeof(name) __mod_##type##__##name##_device_table                \
    83.                                                                 __attribute__ ((unused, alias(__stringify(name))))
    84.         MODULE_DEVICE_TABLE(of, goodix_of_match)=
    85.                 extern const typeof(goodix_of_match) __mod_of__goodix_of_match_device_table \
    86.                                                                 __attribute__ ((unused, alias(__stringify(goodix_of_match))))
    87.        
    88. /* MODULE_VERSION */
    89.         (include/linux/device.h):       
    90.         #if defined(MODULE) || !defined(CONFIG_SYSFS)
    91.         #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
    92.         #else
    93.         #define MODULE_VERSION(_version)                                        \
    94.                 static struct module_version_attribute ___modver_attr = {        \
    95.                         .mattr        = {                                                \
    96.                                 .attr        = {                                        \
    97.                                         .name        = "version",                        \
    98.                                         .mode        = S_IRUGO,                        \
    99.                                 },                                                \
    100.                                 .show        = __modver_version_show,                \
    101.                         },                                                        \
    102.                         .module_name        = KBUILD_MODNAME,                        \
    103.                         .version        = _version,                                \
    104.                 };                                                                \
    105.                 static const struct module_version_attribute                        \
    106.                 __used __attribute__ ((__section__ ("__modver")))                \
    107.                 * __moduleparam_const __modver_attr = &___modver_attr
    108.         #endif               
    109.         MODULE_VERSION("abc") =
    110.                 version = "abc"
    111.        
    112.         /* MODULE_VERSION2 */
    113.         MODULE_VERSION("abc") =
    114.                 static struct module_version_attribute ___modver_attr = {
    115.                                 .mattr        = {       
    116.                                                 .attr        = {
    117.                                                         .name        = "version",
    118.                                                         .mode        = S_IRUGO,
    119.                                                         },
    120.                                                 .show        = __modver_version_show,
    121.                                 },
    122.                                 .module_name        = KBUILD_MODNAME,
    123.                                 .version        = _version,
    124.                 };
    125.                 static const struct module_version_attribute __used __attribute__ ((__section__ ("__modver")))
    126.                                 * __moduleparam_const __modver_attr = &___modver_attr
    127.        
    128.         (include/linux/moduleparam.h)
    129.         #define __moduleparam_const const
    130.         MODULE_VERSION("abc") =
    131.                 static struct module_version_attribute ___modver_attr = {
    132.                                 .mattr        = {       
    133.                                                 .attr        = {
    134.                                                         .name        = "version",
    135.                                                         .mode        = S_IRUGO,
    136.                                                         },
    137.                                                 .show        = __modver_version_show,
    138.                                 },
    139.                                 .module_name        = KBUILD_MODNAME,
    140.                                 .version        = "abc",
    141.                 };
    142.                 static const struct module_version_attribute __used __attribute__ ((__section__ ("__modver")))
    143.                                 * const __modver_attr = &___modver_attr
    144.        
    145.        
    复制代码
    这个是 goodix iic 设备驱动:看看结构,很简单:
    1. /* goodix 驱动模型分析 */
    2. (drivers/input/touchscreen/goodix.c:)
    3. module_i2c_driver(goodix_ts_driver);
    4.         static struct i2c_driver goodix_ts_driver = {
    5.                 .probe = goodix_ts_probe,
    6.                 .id_table = goodix_ts_id,
    7.                 .driver = {
    8.                         .name = "Goodix-TS",
    9.                         .owner = THIS_MODULE,
    10.                         .acpi_match_table = ACPI_PTR(goodix_acpi_match),
    11.                         .of_match_table = of_match_ptr(goodix_of_match),
    12.                 },
    13.         };
    14.                 goodix_ts_probe:        实现
    15.                 goodix_ts_id:                实现
    16.                 goodix_acpi_match:        实现
    17.                 goodix_of_match:        实现


    18. /* module_i2c_driver */
    19.         (include/linux/i2c.h):
    20.         #define module_i2c_driver(__i2c_driver) \
    21.                 module_driver(__i2c_driver, i2c_add_driver, \
    22.                                 i2c_del_driver)
    23.                                
    24.         module_i2c_driver(__i2c_driver)=
    25.                 module_driver(__i2c_driver, i2c_add_driver,i2c_del_driver)

    26.         (include/linux/device.h):       
    27.         #define module_driver(__driver, __register, __unregister, ...) \
    28.         static int __init __driver##_init(void) \
    29.         { \
    30.                 return __register(&(__driver) , ##__VA_ARGS__); \
    31.         } \
    32.         module_init(__driver##_init); \
    33.         static void __exit __driver##_exit(void) \
    34.         { \
    35.                 __unregister(&(__driver) , ##__VA_ARGS__); \
    36.         } \
    37.         module_exit(__driver##_exit);

    38.         module_i2c_driver(__i2c_driver)=
    39.                 module_driver(__i2c_driver, i2c_add_driver,i2c_del_driver)
    40.         module_i2c_driver(__i2c_driver)=
    41.                 static int __init __driver##_init(void)
    42.                 {
    43.                         return __register(&(__driver) , ##__VA_ARGS__);
    44.                 }
    45.                 module_init(__driver##_init);
    46.                 static void __exit __driver##_exit(void)
    47.                 {
    48.                         __unregister(&(__driver) , ##__VA_ARGS__);
    49.                 }
    50.                 module_exit(__driver##_exit);

    51. /* struct i2c_driver */
    52. (include/linux/i2c.h:)
    53. struct i2c_driver {
    54.         unsigned int class;

    55.         /* Notifies the driver that a new bus has appeared. You should avoid
    56.          * using this, it will be removed in a near future.
    57.          */
    58.         int (*attach_adapter)(struct i2c_adapter *) __deprecated;

    59.         /* Standard driver model interfaces */
    60.         int (*probe)(struct i2c_client *, const struct i2c_device_id *);
    61.         int (*remove)(struct i2c_client *);

    62.         /* driver model interfaces that don't relate to enumeration  */
    63.         void (*shutdown)(struct i2c_client *);

    64.         /* Alert callback, for example for the SMBus alert protocol.
    65.          * The format and meaning of the data value depends on the protocol.
    66.          * For the SMBus alert protocol, there is a single bit of data passed
    67.          * as the alert response's low bit ("event flag").
    68.          */
    69.         void (*alert)(struct i2c_client *, unsigned int data);

    70.         /* a ioctl like command that can be used to perform specific functions
    71.          * with the device.
    72.          */
    73.         int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);

    74.         struct device_driver driver;
    75.         const struct i2c_device_id *id_table;

    76.         /* Device detection callback for automatic device creation */
    77.         int (*detect)(struct i2c_client *, struct i2c_board_info *);
    78.         const unsigned short *address_list;
    79.         struct list_head clients;
    80. };

    81. /* struct i2c_device_id */
    82. (include/linux/mod_devicetable.h)
    83. struct i2c_device_id {
    84.         char name[I2C_NAME_SIZE];
    85.         kernel_ulong_t driver_data;        /* Data private to the driver */
    86. };

    复制代码


    评分

    参与人数 1与非币 +60 收起 理由
    satoll + 60 感谢分享

    查看全部评分

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-30 10:12 , Processed in 0.127972 second(s), 19 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.