目录
实例简述:
static const struct i2c_device_id pca953x_id[] = {
{ "pca6416", 16 | PCA953X_TYPE | PCA_INT, },
{ "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
{ "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
{ "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
...
}
MODULE_DEVICE_TABLE(i2c, pca953x_id);
1. 声明一个外部变量,且该变量引用了pca953x_id
变量名: extern typeof(pca953x_id) __mod_i2c__pca953x_id_device_table
变量属性: __attribute__ ((unused, alias(__stringify(pca953x_id))))
2. file2alias.c
用于解析由 MODULE_DEVICE_TABLE
宏定义的设备表,
从而生成设备别名,这些别名随后被用于自动模块加载。
3. 查看驱动支持的设备(驱动别名)
方式1: 执行 命令 modinfo xxx.ko
方式2: 查看文件: /lib/modules/$(uname -r)/modules.alias
定义
//file: include/linux/module.h
#ifdef MODULE
/* Creates an alias so file2alias.c can find device table. */
#define MODULE_DEVICE_TABLE(type, name) \
extern typeof(name) __mod_##type##__##name##_device_table \
__attribute__ ((unused, alias(__stringify(name))))
#else /* !MODULE */
#define MODULE_DEVICE_TABLE(type, name)
#endif
实操
1. 修改配置,编译为模块
ni@ni-P3571:~/xxx/seperate/linux$ diff -u config_init .config
--- config_init 2024-04-03 14:11:43.855620237 +0800
+++ .config 2024-04-03 14:14:09.224088600 +0800
@@ -2100,7 +2100,7 @@
# CONFIG_GPIO_GW_PLD is not set
# CONFIG_GPIO_MAX7300 is not set
# CONFIG_GPIO_MAX732X is not set
-CONFIG_GPIO_PCA953X=y
+CONFIG_GPIO_PCA953X=m
# CONFIG_GPIO_PCA953X_IRQ is not set
# CONFIG_GPIO_PCA9570 is not set
# CONFIG_GPIO_PCF857X is not set
2. 查看模块的别名
ni@ni-P3571:~/xxx/linux$ modinfo drivers/gpio/gpio-pca953x.ko
filename: /xxxx/linux/drivers/gpio/gpio-pca953x.ko
license: GPL
description: GPIO expander driver for PCA953x
author: eric miao <eric.miao@marvell.com>
alias: i2c:xra1202
alias: i2c:tca9554
alias: i2c:tca9539
alias: i2c:tca6424
...
alias: acpi*:INT3491:*
alias: of:N*T*Cexar,xra1202C*
alias: of:N*T*Cexar,xra1202
alias: of:N*T*Connn,pca9655C*
alias: of:N*T*Connn,pca9655
alias: of:N*T*Connn,pca9654C*
....
depends:
intree: Y
name: gpio_pca953x
vermagic: 5.10.109-rt65 SMP preempt mod_unload aarch64