i2c_driver結(jié)構(gòu)體原型如下:
struct i2c_driver {
 unsigned int class;  int (*attach_adapter)(struct i2c_adapter *);  int (*detach_adapter)(struct i2c_adapter *);  int (*probe)(struct i2c_client *, const struct i2c_device_id *);  int (*remove)(struct i2c_client *);  void (*shutdown)(struct i2c_client *);  int (*suspend)(struct i2c_client *, pm_message_t mesg);  int (*resume)(struct i2c_client *);  void (*alert)(struct i2c_client *, unsigned int data);  int (*command)(struct i2c_client *client, unsigned int cmd, void*arg);  struct device_driver driver;  const struct i2c_device_id *id_table;  int (*detect)(struct i2c_client *, struct i2c_board_info *);  const unsigned short *address_list;  struct list_head clients; }; |
主要的成員描述如下:
attach_adapter:依附i2c_adapter函數(shù)指針
detach_adapter:脫離i2c_adapter函數(shù)指針
driver:struct device_driver類型的成員,指定驅(qū)動(dòng)程序的名稱和所屬的總線類型。
probe:指向設(shè)備探測(cè)函數(shù)的回調(diào)函數(shù)指針,在設(shè)備匹配時(shí)調(diào)用。
remove:指向設(shè)備移除函數(shù)的回調(diào)函數(shù)指針,在設(shè)備被卸載時(shí)調(diào)用。
id_table:指向一個(gè)數(shù)組的指針,用于匹配驅(qū)動(dòng)程序和I2C設(shè)備。
detect:指向設(shè)備檢測(cè)函數(shù)的回調(diào)函數(shù)指針,用于檢測(cè)特定類型的設(shè)備是否存在。
當(dāng)I2C設(shè)備和驅(qū)動(dòng)匹配成功以后probe函數(shù)就會(huì)執(zhí)行,和platform驅(qū)動(dòng)一樣,簡(jiǎn)單注冊(cè)示例如下:
#include <linux/i2c.h>
static int my_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) { // I2C設(shè)備探測(cè)函數(shù),處理設(shè)備初始化和配置 // ... return 0; } static int my_i2c_remove(struct i2c_client *client) { // I2C設(shè)備移除函數(shù),處理設(shè)備的清理操作 // ... return 0; } //傳統(tǒng)匹配方式 ID 列表 static const struct i2c_device_id my_i2c_id[] = { { "my_i2c_device", 0 }, { } }; //設(shè)備樹匹配列表 static const struct of_device_id my_i2c_of_match[] = {   { .compatible = "my_i2c_device" },   { /* Sentinel */ } }; MODULE_DEVICE_TABLE(i2c, my_i2c_id); static struct i2c_driver my_i2c_driver = { .driver = { .name = "my_i2c_driver", .owner = THIS_MODULE, .of_match_table = my_i2c_of_match }, .probe = my_i2c_probe, .remove = my_i2c_remove, .id_table = my_i2c_id, }; // 驅(qū)動(dòng)程序初始化函數(shù) static int __init my_i2c_driver_init(void) { return i2c_add_driver(&my_i2c_driver); } // 驅(qū)動(dòng)程序卸載函數(shù) static void __exit my_i2c_driver_exit(void) { i2c_del_driver(&my_i2c_driver); } module_init(my_i2c_driver_init); module_exit(my_i2c_driver_exit); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Sample I2C Driver"); MODULE_LICENSE("GPL"); |