分析i2c-Dev的注册,此设备是个通用的设备,2cdev只是一个虚拟的从设备,因为它并不对应一个实际的从设备,而是和IIC控制器adapter绑定,只有当用户调用open()打开一个设备文件时,才会创建一个虚拟的client。那么怎么知道该i2cdev对应哪个从设备呢?其实很简单,只需要用户在用户空间多做一个工作,就是通过Iocntl()函数来设定从设备的地址,当然这个从设备必须是挂接在对应的adapter下的。所以i2cdev作为一个虚拟设备,本身并不对应任何设备,但又可以对应任意一个挂接在其依附的adapter下的设备。
struct i2c_dev {
struct list_head list;
struct i2c_adapter *adap;
struct device *dev;
};
static int __init i2c_dev_init(void) { int res; printk(KERN_INFO "i2c /dev entries driver\n"); /*注册i2c设备,为字符设备*/ res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops); if (res) goto out; i2c_dev_class = class_create(THIS_MODULE, "i2c-dev"); if (IS_ERR(i2c_dev_class)) { res = PTR_ERR(i2c_dev_class); goto out_unreg_chrdev; } /*向总线中加载驱动*/ res = i2c_add_driver(&i2cdev_driver); if (res) goto out_unreg_class; return 0; out_unreg_class: class_destroy(i2c_dev_class); out_unreg_chrdev: unregister_chrdev(I2C_MAJOR, "i2c"); out: printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__); return res; }
在i2c_add_driver(&i2cdev_driver)后会调用<pre name="code" class="cpp" style="color: rgb(51, 51, 51); line-height: 25.984375px;">i2c_register_driver 两个主要操作;
1:driver_register 并绑定device;
2:<span style="font-family: Arial;">bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);</span>
int i2c_register_driver(struct module *owner, struct i2c_driver *driver) { <span style="white-space:pre"> </span>int res; <span style="white-space:pre"> </span>/* Can't register until after driver model init */ <span style="white-space:pre"> </span>if (unlikely(WARN_ON(!i2c_bus_type.p))) <span style="white-space:pre"> </span>/* add the driver to the list of i2c drivers in the driver core */ <span style="white-space:pre"> </span>driver->driver.owner = owner; <span style="white-space:pre"> </span>driver->driver.bus = &i2c_bus_type; <span style="white-space:pre"> </span>/* When registration returns, the driver core <span style="white-space:pre"> </span> * will have called probe() for all matching-but-unbound devices. <span style="white-space:pre"> </span> */ <span style="white-space:pre"> </span>res = driver_register(&driver->driver); <span style="white-space:pre"> </span>if (res) <span style="white-space:pre"> </span>return res; <span style="white-space:pre"> </span>/* Drivers should switch to dev_pm_ops instead. */ <span style="white-space:pre"> </span>if (driver->suspend) <span style="white-space:pre"> </span>pr_warn("i2c-core: driver [%s] using legacy suspend method\n", <span style="white-space:pre"> </span>driver->driver.name); <span style="white-space:pre"> </span>if (driver->resume) <span style="white-space:pre"> </span>pr_warn("i2c-core: driver [%s] using legacy resume method\n", <span style="white-space:pre"> </span>driver->driver.name); <span style="white-space:pre"> </span>pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); <span style="white-space:pre"> </span>INIT_LIST_HEAD(&driver->clients); <span style="white-space:pre"> </span>/* Walk the adapters that are already present */ <span style="white-space:pre"> </span>mutex_lock(&core_lock); <span style="white-space:pre"> </span>bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver); <span style="white-space:pre"> </span>mutex_unlock(&core_lock); <span style="white-space:pre"> </span>return 0; }
</pre>在driver_register()并不会绑定;<span style="color:rgb(51,51,51); font-family:Arial; font-size:18.18181800842285px; line-height:25.99431800842285px">因为它无法完成i2cdev_driver和从设备的匹配</span><p></p><p><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:Arial; line-height:25.99431800842285px"></span></span></p><pre name="code" class="objc">static int i2c_device_match(struct device *dev, struct device_driver *drv) { struct i2c_client *client = i2c_verify_client(dev); struct i2c_driver *driver; if (!client) return 0; /* Attempt an OF style match */ if (of_driver_match_device(dev, drv)) return 1; driver = to_i2c_driver(drv); /* match on an id table if there is one */ if (driver->id_table) return i2c_match_id(driver->id_table, client) != NULL; return 0; }
static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, const struct i2c_client *client) { while (id->name[0]) { if (strcmp(client->name, id->name) == 0) return id; id++; } return NULL; }因为i2c_driver 中没有id table
static struct i2c_driver i2cdev_driver = {
.driver = {
.name = "dev_driver",
},
.attach_adapter= i2cdev_attach_adapter,
.detach_adapter= i2cdev_detach_adapter,
};
调用bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
最后调用static int i2c_do_add_adapter(struct i2c_driver *driver,struct i2c_adapter *adap)
{
/* 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 struct i2c_driver i2cdev_driver = { .driver = { .name = "dev_driver", }, .attach_adapter = i2cdev_attach_adapter, .detach_adapter = i2cdev_detach_adapter, };
即i2c_driver的i2cdev_attach_adapter
static int i2cdev_attach_adapter(struct i2c_adapter *adap) { struct i2c_dev *i2c_dev; int res; i2c_dev = get_free_i2c_dev(adap); if (IS_ERR(i2c_dev)) return PTR_ERR(i2c_dev); /* register this i2c device with the driver core */ i2c_dev->dev = device_create(i2c_dev_class, &adap->dev, MKDEV(I2C_MAJOR, adap->nr), NULL, "i2c-%d", adap->nr); if (IS_ERR(i2c_dev->dev)) { res = PTR_ERR(i2c_dev->dev); goto error; } res = device_create_file(i2c_dev->dev, &dev_attr_name); if (res) goto error_destroy; pr_debug("i2c-dev: adapter [%s] registered as minor %d\n", adap->name, adap->nr); return 0; error_destroy: device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr)); error: return_i2c_dev(i2c_dev); return res; }get_free_i2c_dev实现的功能有
<pre name="code" class="cpp">static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap) { struct i2c_dev *i2c_dev; if (adap->nr >= I2C_MINORS) { printk(KERN_ERR "i2c-dev: Out of device minors (%d)\n", adap->nr); return ERR_PTR(-ENODEV); } i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL); if (!i2c_dev) return ERR_PTR(-ENOMEM); i2c_dev->adap = adap; spin_lock(&i2c_dev_list_lock); list_add_tail(&i2c_dev->list, &i2c_dev_list); spin_unlock(&i2c_dev_list_lock); return i2c_dev; }
在调用device_create()函数时
i2c_dev->dev = device_create(i2c_dev_class, &adap->dev, MKDEV(I2C_MAJOR, adap->nr),
NULL,"i2c-%d",adap->nr);
i2c_dev_class是在i2c_dev_init(void)中i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");创建的
到此i2c注册完成;
open函数的映射;
static int i2cdev_open(struct inode *inode, struct file *file) { unsigned int minor = iminor(inode); struct i2c_client *client; struct i2c_adapter *adap; struct i2c_dev *i2c_dev; i2c_dev = i2c_dev_get_by_minor(minor); if (!i2c_dev) return -ENODEV; adap = i2c_get_adapter(i2c_dev->adap->nr); if (!adap) return -ENODEV; /* This creates an anonymous i2c_client, which may later be * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE. * * This client is ** NEVER REGISTERED ** with the driver model * or I2C core code!! It just holds private copies of addressing * information and maybe a PEC flag. */ client = kzalloc(sizeof(*client), GFP_KERNEL); if (!client) { i2c_put_adapter(adap); return -ENOMEM; } snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr); client->driver = &i2cdev_driver; client->adapter = adap; file->private_data = client; return 0; }
static ssize_t i2cdev_write(struct file *file, const char __user *buf,
size_t count, loff_t *offset)
{
int ret;
char *tmp;
struct i2c_client *client = file->private_data;
if (count > 8192)
count = 8192;
tmp = memdup_user(buf, count);
if (IS_ERR(tmp))
return PTR_ERR(tmp);
pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n",
iminor(file->f_path.dentry->d_inode), count);
ret = i2c_master_send(client, tmp, count);
kfree(tmp);
return ret;
}
int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) { unsigned long orig_jiffies; int ret, try; /* REVISIT the fault reporting model here is weak: * * - When we get an error after receiving N bytes from a slave, * there is no way to report "N". * * - When we get a NAK after transmitting N bytes to a slave, * there is no way to report "N" ... or to let the master * continue executing the rest of this combined message, if * that's the appropriate response. * * - When for example "num" is two and we successfully complete * the first message but get an error part way through the * second, it's unclear whether that should be reported as * one (discarding status on the second message) or errno * (discarding status on the first one). */ if (adap->algo->master_xfer) { #ifdef DEBUG for (ret = 0; ret < num; ret++) { dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W', msgs[ret].addr, msgs[ret].len, (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); } #endif if (in_atomic() || irqs_disabled()) { ret = i2c_trylock_adapter(adap); if (!ret) /* I2C activity is ongoing. */ return -EAGAIN; } else { i2c_lock_adapter(adap); } /* Retry automatically on arbitration loss */ orig_jiffies = jiffies; for (ret = 0, try = 0; try <= adap->retries; try++) { <span style="color:#ff0000;">ret = adap->algo->master_xfer(adap, msgs, num);</span> if (ret != -EAGAIN) break; if (time_after(jiffies, orig_jiffies + adap->timeout)) break; } i2c_unlock_adapter(adap); return ret; } else { dev_dbg(&adap->dev, "I2C level transfers not supported\n"); return -EOPNOTSUPP; } }