如前文所述(http://www.linuxidc.com/Linux/2012-08/68263.htm),在probe函數會調用函數i2c_add_number_adapter(),通過此函數來實現adapter device和client device的注冊
i2c_add_number_adapter()函數如下:
- int i2c_add_numbered_adapter(struct i2c_adapter *adap)
- {
- int id;
- int status;
-
- if (adap->nr & ~MAX_ID_MASK)
- return -EINVAL;
-
- retry:
- if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
- return -ENOMEM;
-
- mutex_lock(&core_lock);
- /* "above" here means "above or equal to", sigh;
- * we need the "equal to" result to force the result
- */
- status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
- if (status == 0 && id != adap->nr) {
- status = -EBUSY;
- idr_remove(&i2c_adapter_idr, id);
- }
- mutex_unlock(&core_lock);
- if (status == -EAGAIN)
- goto retry;
-
- if (status == 0)
- status = i2c_register_adapter(adap);
- return status;
- }
函數前面部分應該是用來提高搜索效率相關,此處省略分析。直接分析
i2c_register_adapter()函數,如下:
- static int i2c_register_adapter(struct i2c_adapter *adap)
- {
- int res = 0;
-
- 。。。 。。。
- dev_set_name(&adap->dev, "i2c-%d", adap->nr);
- adap->dev.bus = &i2c_bus_type;
- adap->dev.type = &i2c_adapter_type;
- res = device_register(&adap->dev);
- 。。。 。。。
-
- /* create pre-declared device nodes */
- if (adap->nr < __i2c_first_dynamic_bus_num)
- i2c_scan_static_board_info(adap);
-
- /* Notify drivers */
- printk("bus_search\n");
- mutex_lock(&core_lock);
- bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
- mutex_unlock(&core_lock);
-
- return 0;
-
- out_list:
- mutex_lock(&core_lock);
- idr_remove(&i2c_adapter_idr, adap->nr);
- mutex_unlock(&core_lock);
- return res;
- }
函數首先通過:
- dev_set_name(&adap->dev, "i2c-%d", adap->nr);
- adap->dev.bus = &i2c_bus_type;
- adap->dev.type = &i2c_adapter_type;
- res = device_register(&adap->dev);
語句,將名為i2c-0的adapter設備注冊到i2c_bus_type型的i2c總線上上,其中通過
- adap->dev.type = &i2c_adapter_type;