最主要是 module_i2c_driver(mpc42013_driver);
,其 struct
定義:
static struct i2c_driver mpc42013_driver
.driver
.probe_new
remove
.id_table
欄位名稱 | 類型 | 作用 | 什麼時候會被呼叫 |
---|---|---|---|
.driver |
struct device_driver |
提供「驅動的基本資訊」與「配對資訊」,像是名字、支援哪些 Device Tree 相容字串等 | 核心 driver model 用來做比對與註冊 |
.probe_new |
int (*)(struct i2c_client *client) |
驅動成功匹配裝置時會執行的初始化函式(新 API,建議使用) | 有相容裝置註冊時由 i2c_device_probe() 呼叫 |
.remove |
void (*)(struct i2c_client *client) |
當裝置被移除時(例如 hotplug 拔除、模組卸載),這個函式會負責做清理 | 在驅動卸載、裝置移除時由 i2c_device_remove() 呼叫 |
.id_table |
const struct i2c_device_id * |
用於 legacy 比對(非 Device Tree 的平台),像 x86 上的硬體 | 傳統平台配對用,會傳入 probe() 的第二參數 |
這是來自 include/linux/i2c.h
的 marco
module_i2c_driver
include/linux/device.h
的 module_driver
裡面的要填寫的結構定義在 include/linux/i2c.h
其中 .driver
是在 include/linux/device.h
的struct device_driver
include/linux/device.h
欄位 | 說明 |
---|---|
name |
驅動名稱,用於 sysfs (/sys/bus/.../drivers/ ) |
bus |
所屬的 bus(如 i2c_bus_type , pci_bus_type ) |
owner |
通常填入 THIS_MODULE ,讓 kernel 知道這個 driver 的 module 是誰 |
probe() |
當有裝置與此驅動匹配時,會被呼叫 |
remove() |
裝置移除時呼叫 |
of_match_table |
Device Tree 的 compatible 比對表 |
acpi_match_table |
ACPI 裝置比對表(在 x86 平台常用) |
.driver
.compatible
: 會去找對應在 Device Tree 中的 compatible 欄位.data
(optional).probe
include/linux/i2c.h
裡的 struct i2c_driver
中:drivers/i2c/i2c-core-base.c
裡面的 i2c_device_probe()
被呼叫,這是當有驅動與裝置配對成功時名稱 | 傳統 API | 新 API(建議用) |
---|---|---|
probe() |
帶 id_table |
否 |
probe_new() |
不帶 id_table |
✅ 較簡單現代寫法 |
差異 | probe() |
probe_new() |
---|---|---|
參數 | struct i2c_client *, const struct i2c_device_id * |
struct i2c_client * |
匹配來源 | 傳統 I2C core 用 .id_table 傳入 |
Device Tree / ACPI 直接用 .of_match_table |
適用平台 | 非 OF、非 ACPI 系統 | 現代嵌入式系統(使用 Device Tree 或 ACPI) |
哪個比較新? | ✅ 比較舊 | ✅ 建議使用的新式方式 |
.id_table
legacy 平台(非 Device Tree)配對用
client->name
比對 mychip
字串probe_new
的時候不一定要寫dev_info(&client->dev, "Using probe_new() for device %s\n", dev_name(&client->dev));