Linux i2c子系統(三) _解決probe無法執行


如果你也遇到了填充了id_match_table,compitible怎么看都一樣,但probe就是不執行(讓我哭一會),你可以回頭看一下上一篇的模板,我們這里雖然使用的是設備樹匹配,但和platform的設備樹匹配只填充i2c_match_table不同,i2c_driver的設備樹匹配需要同時填充i2c_match_table和id_table兩個域,雖然后者是個空。如果你沒有填充后面的成員,不妨試一下我的這種寫法,我敢打賭你的probe也沒有執行^-^。
問題是明確的,探索是漫長的,但是至少答案一定在源碼中,也一定出在匹配的源碼中,帶着這樣的思路,我從"i2c_add_driver"開始一路狂追,結論是使用設備樹的話,只要id_match_table,不需要id_table!, 下面的i2c_device_match即可看出。

i2c_add_driver()
           └── i2c_register_driver
                      └── driver_register
                                 ├── driver_find
                                 │   ├── kset_find_obj
                                 │   ├── kobject_put
                                 │   └── to_driver
                                 └── bus_add_driver
                                            └── driver_attach
                                                       └── bus_for_each_dev
                                                                  ├── next_device
                                                                  └── __driver_attach
                                                                             └─ driver_match_device
                                                                                        └── i2c_device_match
                                                                                                   ├── acpi_driver_match_device
                                                                                                   ├── i2c_match_id
                                                                                                   └── of_driver_match_device
                                                                                                   └── of_match_device
                                                                                                              └── of_match_node
                                                                                                                         └── __of_match_node
                                                                                                                                    └── __of_device_is_compatible

/home/jiang/Pictures/Selection_20170224_001.png
![Selection_20170224_001.png-6.4kB][1]

//3.14.0/drivers/i2c/i2c-core.c
  78 static int i2c_device_match(struct device *dev, struct device_driver *drv)
  79 {
  80         struct i2c_client       *client = i2c_verify_client(dev);
  81         struct i2c_driver       *driver;
  82 
  83         if (!client)
  84                 return 0;
  85 
  86         /* Attempt an OF style match */
  87         if (of_driver_match_device(dev, drv))
  88                 return 1;
  89 
  90         /* Then ACPI style match */
  91         if (acpi_driver_match_device(dev, drv))
  92                 return 1;
  93 
  94         driver = to_i2c_driver(drv);
  95         /* match on an id table if there is one */
  96         if (driver->id_table)              
  97                 return i2c_match_id(driver->id_table, client) != NULL;
  98 
  99         return 0;
 100 }

從i2c_device_match的定義可以看出, 和platform總線一樣, i2c的match函數也是優先選擇設備樹, 如果設備樹已經匹配了, 函數就會返回, 不會再最平台文件的設備信息進行判斷, 即不會理會id_table的值, 確保匹配是一定不需要id_table了,而事實上probe確實沒有執行,那么問題就可能出現在probe的回調過程了,和所有的總線設備一樣,回調probe的過程始於driver_match_id,於是又是一路狂追,終於在i2c_device_probe()中找到了我臆想中的對id_table的檢測

下面是我追的源碼樹,大家可以驗證一下

i2c_add_driver()
           └── i2c_register_driver
                      └── driver_register
                                 ├── driver_find
                                 │   ├── kset_find_obj
                                 │   ├── kobject_put
                                 │   └── to_driver
                                 └── bus_add_driver
                                            └── driver_attach
                                                       └── bus_for_each_dev
                                                                  ├── next_device
                                                                  └── __driver_attach
                                                                             ├── driver_match_device
                                                                             └── driver_probe_device
                                                                                        └── really_probe
                                                                                                   └── i2c_device_probe
                                                                                                              └── i2c_match_id

所以,結論是:即使使用設備樹來匹配,也要對id_table進行有效的賦值,否則probe不會被回調!!!如果你也遇到了這個問題, 可以試試在驅動中定義一個空數組, 將其賦值給id_table^-^


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM