i2c驅動之設備模型建立

一:設備模型建立流程圖
i2c驅動之設備模型建立 - 塵 - 我的電子小屋
i2c驅動之設備模型建立 - 塵 - 我的電子小屋
 
二:重要函數分析
(1)新適配器加入內核:
 
int i2c_add_numbered_adapter(struct i2c_adapter *adap)
{
 int id;
 int status;
 if (adap->nr & ~MAX_ID_MASK)
  return -EINVAL;
retry://爲i2c_adapter_idr分配內存
 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
  *///讓指針adap關聯一個ID,這個ID值從adap->nr開始
 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
 if (status == 0 && id != adap->nr) {
  status = -EBUSY;//分配的ID值必須與adap->nr相等
  idr_remove(&i2c_adapter_idr, id);
 }
 mutex_unlock(&core_lock);
 if (status == -EAGAIN)
  goto retry;
 if (status == 0)//表示ID分配成功。
  status = i2c_register_adapter(adap);//註冊一個適配器。
 return status;
}
 
static int i2c_register_adapter(struct i2c_adapter *adap)
{
 int res = 0, dummy;
 /* Can't register until after driver model init */
 if (unlikely(WARN_ON(!i2c_bus_type.p)))
  return -EAGAIN;
 mutex_init(&adap->bus_lock);
 mutex_init(&adap->clist_lock);
 INIT_LIST_HEAD(&adap->clients);
 mutex_lock(&core_lock);
 /* Add the adapter to the driver core.
  * If the parent pointer is not set up,
  * we add this adapter to the host bus.
  */
 if (adap->dev.parent == NULL) {
  adap->dev.parent = &platform_bus;
  pr_debug("I2C adapter driver [%s] forgot to specify "
    "physical device\n", adap->name);
 }
 /* Set default timeout to 1 second if not already set */
 if (adap->timeout == 0)
  adap->timeout = HZ;
//設置適配器的名
 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
 adap->dev.release = &i2c_adapter_dev_release;
 ////將適配器設備歸類,在驅動註冊的時候會根據這個類找尋
 //這個類中匹配的適配器。
 //這個類表示該設備是一個適配器,而adap->class表示該設備支持的設備類型
 adap->dev.class = &i2c_adapter_class;
 res = device_register(&adap->dev);//註冊該設備
 //到此適配器設備已被完全註冊到了內核。
 if (res)
  goto out_list;
 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
 /* create pre-declared device nodes for new-style drivers */
 /*
 結構體board_info包含了一些平臺信息,和I2C設備地址。該結構
 由函數i2c_register_board_info()加到鏈表__i2c_board_list上。
 函數i2c_scan_static_board_info在該鏈表上找一個滿足這樣條件的
 devinfo->busnum == adapter->nr的結構體,然後根據board_info中的
 client->flags = info->flags;
 client->addr = info->addr;
 client->irq = info->irq;
  註冊一個client(它代表一個具體的設備),並且
  client->dev.parent = &client->adapter->dev;最終將將client->dev
  掛到鏈表adapter->dev->p->klist_children上。
  函數device_for_each_child就是根據該鏈表來找出適配器上的每一個client的。
 */
 if (adap->nr < __i2c_first_dynamic_bus_num)
  i2c_scan_static_board_info(adap);
 /* Notify drivers */
/*
尋找每一個總線類型爲i2c_bus_type的驅動,這些驅動都被包裝爲i2c_driver.
如果驅動類型i2c_driver->class和適配器類型adapter->class匹配,並且
驅動支持的設備地址driver->address_data在適配器中沒有被佔用。就會根據
這個地址註冊一個client掛到這個適配器上。
另一個任務是調用函數driver->attach_adapter,註冊一個有設備號的設備
並將這個適配器與這個設備關聯。該driver是i2c_driver並不是所有的driver
都有爲一個適配器創建設備節點的能力即有函數driver->attach_adapter。。。。
*/
 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
     i2c_do_add_adapter);
out_unlock:
 mutex_unlock(&core_lock);
 return res;
out_list:
 idr_remove(&i2c_adapter_idr, adap->nr);
 goto out_unlock;
}
 
static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
{
 struct i2c_devinfo *devinfo;
 mutex_lock(&__i2c_board_lock);
 /*
 devinfo被函數i2c_register_board_info掛到鏈表__i2c_board_list上。
 本函數是在鏈表__i2c_board_list上找一個結構體devinfo,該結構體滿足
   devinfo->busnum == adapter->nr。
   devinfo中包含了地址,flags,type等信息,可用於初始化一個client
   並註冊到內核。
  
   結構體devinfo中的信息與平臺相關。因此該結構的註冊應該是在平臺
   的初始化時進行。
 */
 list_for_each_entry(devinfo, &__i2c_board_list, list) {
  if (devinfo->busnum == adapter->nr
    && !i2c_new_device(adapter,
      &devinfo->board_info))
   dev_err(&adapter->dev,
    "Can't create device at 0x%02x\n",
    devinfo->board_info.addr);
 }
 mutex_unlock(&__i2c_board_lock);
}
 
 
(2)一個新的驅動加入內核:
 
int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
{
 int res;
 /* Can't register until after driver model init */
 if (unlikely(WARN_ON(!i2c_bus_type.p)))
  return -EAGAIN;
 /* new style driver methods can't mix with legacy ones */
 /*
 "new style" driver:
     is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
 "legacy"driver:
     driver->detach_adapter || driver->detach_client
attach_adapter和detach_adapter;detach_client和attach_client是兩對
操作相反的函數,必須成對出現。   
  這兩類驅動的區別就是他們所擁有的函數不一樣。
  不能出現這兩類驅動的混合體,即兼有這兩類驅動的標誌函數的驅動。  
 */
 if (is_newstyle_driver(driver)) {
  if (driver->detach_adapter || driver->detach_client) {
   printk(KERN_WARNING
     "i2c-core: driver [%s] is confused\n",
     driver->driver.name);
   return -EINVAL;
  }
 }
 /* add the driver to the list of i2c drivers in the driver core */
 driver->driver.owner = owner;
 driver->driver.bus = &i2c_bus_type;//設置它的總線類型
 /* for new style drivers, when registration returns the driver core
  * will have called probe() for all matching-but-unbound devices.
  */
 res = driver_register(&driver->driver);//註冊驅動
 if (res)
  return res;
/*
到這裏該驅動已被完全註冊到了內核。它可以去匹配內核中已存在的
client而後執行probe函數了。
但是一個i2c_driver還有着其他的使命。如果該驅動是一個攜帶地址信息的
"new style" driver,則它要把這些每一個地址對應一個client註冊到內核。
如果是一個"legacy"driver,則它要用它的driver->attach_adapter函數
去處理每一個adapter->nr空閒的適配器。
每一個函數driver->attach_adapter的操作都跟adapter->nr有關,在這些函數
中都會對adapter->nr進行判斷,所以driver->attach_adapter並不是,一個驅動
的函數attach_adapter對每一個適配器都會成功執行。

比如在驅動i2cdev_driver中函數attach_adapter的執行會創建一個設備節點,
但是對同一個適配器兩次執行函數attach_adapter就不會產生兩個設備節點,
因爲產生設備節點的次設備號是由adapter->nr來決定的。內核是不會允許
對同一個設備號註冊兩次的。
*/
 mutex_lock(&core_lock);
 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
 INIT_LIST_HEAD(&driver->clients);//初始化鏈表頭用於掛接client
 /* Walk the adapters that are already present */
 /*
 尋找類型爲i2c_adapter_class的所有設備,即適配器。
 
 如果驅動爲"new style" driver將在函數__attach_adapter中
 執行函數i2c_detect,如果當前找到的適配器所支持的設備類型
 與驅動所支持的設備類型相匹配,就將驅動所攜帶的地址信息
 driver->address_data,對應着client註冊到內核,並插入該適配器。
 適配器所支持的設備類型由adapter->class決定,而不是
 adapter->dev->class,adapter->dev->class的類型應爲i2c_adapter_class。
 驅動所支持的設備類型爲driver->class。
 
 如果該驅動爲"legacy"driver,則在函數__attach_adapter中將執行
 函數driver->attach_adapter,將在類i2c_adapter_class上找到的
 所有適配器都執行一遍本驅動的driver->attach_adapter函數。
 當然並不是所有驅動都能成功執行該函數。該函數的執行會判斷
 adapter->nr。
 */
 class_for_each_device(&i2c_adapter_class, NULL, driver,
         __attach_adapter);
 mutex_unlock(&core_lock);
 return 0;
}
 
 
(3)適配器與設備進行類型匹配,探測驅動中的可用地址,並將地址對應於一個client註冊到內核
 
    在適配器設備和驅動註冊到內核後調用了兩個相似的函數如下:
 
   在適配器設備註冊到內核後調用函數:
static int i2c_do_add_adapter(struct device_driver *d, void *data)
{
 struct i2c_driver *driver = to_i2c_driver(d);
 struct i2c_adapter *adap = data;
 /* Detect supported devices on that bus, and instantiate them */
 //如果驅動的類與適配器的類相匹配,則將驅動支持的設備插入適配器。
 i2c_detect(adap, driver);
 /* Let legacy drivers scan this bus for matching devices */
 if (driver->attach_adapter) {//使適配器關聯一個設備號或者。。。。。。
  /* We ignore the return code; if it fails, too bad */
  driver->attach_adapter(adap);
 }
 return 0;
}
驅動註冊到內核後調用函數:
static int __attach_adapter(struct device *dev, void *data)
{
 struct i2c_adapter *adapter = to_i2c_adapter(dev);
 struct i2c_driver *driver = data;
 //如果驅動的類與適配器的類相匹配,則將驅動支持的設備插入適配器。
 i2c_detect(adapter, driver);
 /* Legacy drivers scan i2c busses directly */
 if (driver->attach_adapter)//使適配器關聯一個設備號或者。。。。。。
  driver->attach_adapter(adapter);
 return 0;
}

 兩函數的對比:
static int __attach_adapter(struct device *dev, void *data)
                   與
 static int i2c_do_add_adapter(struct device_driver *d, void *data)
 前者是在新的驅動加入內核時調用函數class_for_each_device時調用的函數。
 函數class_for_each_device是在類i2c_adapter_class中找一個適配器設備
 與新驅動匹配。該函數變化的是設備不變的是加入的新的驅動(void *data)
 
 後者是在新的適配器加入內核時調用函數 bus_for_each_drv時調用的函數。
 函數bus_for_each_drv是在總線類型爲i2c_bus_type的驅動中找到一個驅動
 與新加入的適配器匹配。該函數變化的是驅動,不變的是新加入的適配器
 (void *data)。
 
 相同點都是執行函數i2c_detect和函數driver->attach_adapter。
 其實在bus_for_each_drv和class_for_each_device並不執行匹配的操作。
 只是簡單的取出所有的驅動或是適配器,執行函數i2c_detect和attach_adapter。
 真正的匹配是在函數i2c_detect和attach_adapter中進行的。
 函數i2c_detect中會去判斷驅動支持的設備類和適配器所支持的設備類
 是否匹配。
 函數driver->attach_adapter中會判斷adapter->nr是不是自己需要處理的
 適配器,或者是是不是該適配器已被處理過了。
 當然函數i2c_detect和函數driver->attach_adapter是絕不會同時執行的。
 因爲函數i2c_detect的執行需要用到函數driver->detect而該函數是
 "new style" drivers的標誌函數。而函數driver->attach_adapter是
 "legacy" drivers的標誌函數。i2c_driver中是沒有兼有這兩種驅動的混合體
 驅動的。
 
static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
{
 const struct i2c_client_address_data *address_data;
 struct i2c_client *temp_client;
 int i, err = 0;
 int adap_id = i2c_adapter_id(adapter);
 address_data = driver->address_data;
 /*
 因爲註冊一個client要調用函數i2c_new_device(adapter, &info);
 該函數要用結構體info爲client提供必要的信息。所以函數
 driver->detect的主要作用是填充結構體info。
 client代表一個具體設備而每個設備都有它的地址address_data。
 所以driver->detect和address_data都是必不可少。
 */
 if (!driver->detect || !address_data)
  return 0;
 /* Set up a temporary client to help detect callback */
 /*
 分配一個臨時的temp_client,因爲temp_client得設備並沒有被註冊,
 也沒有被插入適配器,它只是暫存了一些client信息。
 */
 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
 if (!temp_client)
  return -ENOMEM;
 temp_client->adapter = adapter;
 /* Force entries are done first, and are not affected by ignore
    entries */
/*
爲什麼是forces?
因爲跳過了適配器支持的設備類和驅動支持的設備類匹配判斷
 if (!(adapter->class & driver->class))
  goto exit_free;
也就是說即使不匹配,這些設備地址只要滿足某些條件也要將其插入適配器。
*/   
 if (address_data->forces) {
  const unsigned short * const *forces = address_data->forces;
  int kind;
  for (kind = 0; forces[kind]; kind++) {
   for (i = 0; forces[kind][i] != I2C_CLIENT_END;
        i += 2) {//adap_id即是分配的與結構體adapter相關聯的ID
    if (forces[kind][i] == adap_id
     || forces[kind][i] == ANY_I2C_BUS) {
     dev_dbg(&adapter->dev, "found force "
      "parameter for adapter %d, "
      "addr 0x%02x, kind %d\n",
      adap_id, forces[kind][i + 1],
      kind);
     temp_client->addr = forces[kind][i + 1];
//將這個地址的設備插入適配器。     
     err = i2c_detect_address(temp_client,
      kind, driver);
     if (err)
      goto exit_free;
    }
   }
  }
 }
 /* Stop here if the classes do not match */
 //適配器和驅動的類型要匹配此處的驅動爲i2c_driver
 if (!(adapter->class & driver->class))
  goto exit_free;
 /* Stop here if we can't use SMBUS_QUICK */
 //如果該適配器不支持I2C_FUNC_SMBUS_QUICK功能則就此結束。
 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
  if (address_data->probe[0] == I2C_CLIENT_END
   && address_data->normal_i2c[0] == I2C_CLIENT_END)
   goto exit_free;
  dev_warn(&adapter->dev, "SMBus Quick command not supported, "
    "can't probe for chips\n");
  err = -EOPNOTSUPP;
  goto exit_free;
 }
 /* Probe entries are done second, and are not affected by ignore
    entries either */
 //address_data->probe支持的設備地址不受
 //address_data->ignore(忽略某些地址)的影響
 //而address_data->normal_i2c[i]中的地址就要受到ignore的影響
 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
  if (address_data->probe[i] == adap_id
   || address_data->probe[i] == ANY_I2C_BUS) {
   dev_dbg(&adapter->dev, "found probe parameter for "
    "adapter %d, addr 0x%02x\n", adap_id,
    address_data->probe[i + 1]);
   temp_client->addr = address_data->probe[i + 1];
//將這個地址對應的設備插入適配器。   
   err = i2c_detect_address(temp_client, -1, driver);
   if (err)
    goto exit_free;
  }
 }
 /* Normal entries are done last, unless shadowed by an ignore entry */
 //驅動中所支持的地址一般都存於address_data->normal_i2c中
 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
  int j, ignore;
  ignore = 0;
  for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
       j += 2) {
   if ((address_data->ignore[j] == adap_id ||
        address_data->ignore[j] == ANY_I2C_BUS)
    && address_data->ignore[j + 1]
       == address_data->normal_i2c[i]) {
    dev_dbg(&adapter->dev, "found ignore "
     "parameter for adapter %d, "
     "addr 0x%02x\n", adap_id,
     address_data->ignore[j + 1]);
    ignore = 1;
    break;
   }
  }
  if (ignore)
   continue;
  dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
   "addr 0x%02x\n", adap_id,
   address_data->normal_i2c[i]);
  temp_client->addr = address_data->normal_i2c[i];
//將這個地址對應的設備插入適配器。  
  err = i2c_detect_address(temp_client, -1, driver);
  if (err)
   goto exit_free;
 }
 exit_free:
 kfree(temp_client);
 return err;
}
 
static int i2c_detect_address(struct i2c_client *temp_client, int kind,
         struct i2c_driver *driver)
{
 struct i2c_board_info info;
 struct i2c_adapter *adapter = temp_client->adapter;
 int addr = temp_client->addr;
 int err;
//有效地設備地址必須在這個範圍內
 /* Make sure the address is valid */
 if (addr < 0x03 || addr > 0x77) {
  dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
    addr);
  return -EINVAL;
 }
 /* Skip if already in use */
//檢測這個地址在適配器adapter中是否被佔用。
 if (i2c_check_addr(adapter, addr))
  return 0;
 /* Make sure there is something at this address, unless forced */
 if (kind < 0) {
  if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
       I2C_SMBUS_QUICK, NULL) < 0)
   return 0;
  /* prevent 24RF08 corruption */
  if ((addr & ~0x0f) == 0x50)
   i2c_smbus_xfer(adapter, addr, 0, 0, 0,
           I2C_SMBUS_QUICK, NULL);
 }
 /* Finally call the custom detection function */
 //清空構體info準備存放信息
 memset(&info, 0, sizeof(struct i2c_board_info));
 info.addr = addr;
 //該函數爲結構體info填充一些信息。
 err = driver->detect(temp_client, kind, &info);
 if (err) {
  /* -ENODEV is returned if the detection fails. We catch it
     here as this isn't an error. */
  return err == -ENODEV ? 0 : err;
 }
 /* Consistency check */
 /*
  info.type在函數driver->detect中將把driver->name拷給它。
  info.type最終將在函數i2c_new_device中
  strlcpy(client->name, info->type, sizeof(client->name));
  client代表一個具體的設備,驅動對應着設備,當驅動與設備
  相匹配時執行初始化函數函數probe。
  在總線i2c_bus_type上驅動與設備匹配的條件是,name相同。
 */
 if (info.type[0] == '\0') {
  dev_err(&adapter->dev, "%s detection function provided "
   "no name for 0x%x\n", driver->driver.name,
   addr);
 } else {
  struct i2c_client *client;
  /* Detection succeeded, instantiate the device */
  dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
   info.type, info.addr);
//獲取了足夠的建立client的信息(info)則註冊client,並插入適配器  
  client = i2c_new_device(adapter, &info);
  if (client)//這些設備都是driver所支持的,所以要掛到driver->clients上。
   list_add_tail(&client->detected, &driver->clients);
  else
   dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
    info.type, info.addr);
 }
 return 0;
}
 
 
struct i2c_client *
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
 struct i2c_client *client;
 int   status;
//將結構體info中存儲的各種信息用來初始化client。
//函數driver->detect(temp_client, kind, &info);
//是用來填充info中的值的,由此可見該函數的重要性。
 client = kzalloc(sizeof *client, GFP_KERNEL);
 if (!client)
  return NULL;
//
 client->adapter = adap;
 client->dev.platform_data = info->platform_data;
 if (info->archdata)
  client->dev.archdata = *info->archdata;
 client->flags = info->flags;
 client->addr = info->addr;
 client->irq = info->irq;
//爲結構體client命名該名來至攜帶client地址信息的驅動
 strlcpy(client->name, info->type, sizeof(client->name));
 /* a new style driver may be bound to this device when we
  * return from this function, or any later moment (e.g. maybe
  * hotplugging will load the driver module).  and the device
  * refcount model is the standard driver model one.
  */
//device_register(&client->dev);註冊設備並 
//client->dev.parent = &client->adapter->dev;
//以上設置很重要是函數device_for_each_child實現的基礎。
 status = i2c_attach_client(client);
 if (status < 0) {
  kfree(client);
  client = NULL;
 }
 return client;
}
 
int i2c_attach_client(struct i2c_client *client)
{
 struct i2c_adapter *adapter = client->adapter;
 int res;
 /* Check for address business */
 //檢測地址client->addr在適配器adapter中是否已被佔據
 res = i2c_check_addr(adapter, client->addr);
 if (res)
  return res;
//該適配器是所有它支持的設備的父設備
 client->dev.parent = &client->adapter->dev;
 client->dev.bus = &i2c_bus_type;
 if (client->driver)
  client->dev.driver = &client->driver->driver;
/*
有兩種驅動new-style drivers 和"legacy" drivers前者只註冊
client,匹配相應驅動,執行函數probe()。後者是添加適配器
創建設備節點。
*/
 if (client->driver && !is_newstyle_driver(client->driver)) {
  /*
   在函數i2c_client_release中調用complete(&client->released);
   通知完成released。在函數i2c_detach_client中有
   wait_for_completion(&client->released);
  */
  client->dev.release = i2c_client_release;
  dev_set_uevent_suppress(&client->dev, 1);
 } else
  client->dev.release = i2c_client_dev_release;
//注意client->dev->name與client->name的區分
 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter),
       client->addr);
 res = device_register(&client->dev);//註冊設備
 if (res)
  goto out_err;
 mutex_lock(&adapter->clist_lock);
 //將適配器支持的設備插入適配器
 list_add_tail(&client->list, &adapter->clients);
 mutex_unlock(&adapter->clist_lock);
 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
  client->name, dev_name(&client->dev));
//好像該做的都做完了,函數adapter->client_register或許
//在某些特定的場合有着它特定的作用
 if (adapter->client_register)  {
  if (adapter->client_register(client)) {
   dev_dbg(&adapter->dev, "client_register "
    "failed for client [%s] at 0x%02x\n",
    client->name, client->addr);
  }
 }
 return 0;
out_err:
 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
  "(%d)\n", client->name, client->addr, res);
 return res;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章