8. 用户空间驱动支持
这部分在i2c-dev.c中实现,这部分内容简单的说就是通过内嵌一个具有file_operations的标准字符设备驱动来虚拟i2c设备,这样,就可以在用户空间直接操作i2c设备了。
流程如下图:
余下的就是常规file_operation了,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;
int ret = 0;
lock_kernel();/*内核上锁,一般只在多cpu是有用*/
i2c_dev = i2c_dev_get_by_minor(minor);/*因为有两个adapter,同一个主设备号*/
if (!i2c_dev) {
ret = -ENODEV;
goto out;
}
adap = i2c_get_adapter(i2c_dev->adap->nr);
if (!adap) {
ret = -ENODEV;
goto out;
}
/* 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);
ret = -ENOMEM;
goto out;
}
snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
client->driver = &i2cdev_driver;/*绑定字符设备驱动*/
client->adapter = adap;
file->private_data = client;
out:
unlock_kernel();
return ret;
}