2014年9月1日 星期一

裝置實例-I2C (3)

    接下來我們就來看看i2c-dev.c吧,它已經是一個完整的字元裝置,在整個I2C架構中就是扮演abstraction layer的角色,其實它已經可以直接拿來使用,不太需要再修改內容。可是了解他在做些什麼事情還是很重要,例如呼叫了哪些i2c-core提供的API,又提供了哪些file operations的方法給user使用...等等,了解它後在之後應用程式的開發上會比較清楚。不過這邊不會詳細說明裡面的內容,會比較針對在file operations中跟user space的操作上,其他部分就只簡單的說明。

static int i2cdev_attach_adapter(struct device *dev, void *dummy)
{
struct i2c_adapter *adap;
struct i2c_dev *i2c_dev;
int res;

if (dev->type != &i2c_adapter_type)
return 0;
adap = to_i2c_adapter(dev);

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;
}

static int i2cdev_detach_adapter(struct device *dev, void *dummy)
{
struct i2c_adapter *adap;
struct i2c_dev *i2c_dev;

if (dev->type != &i2c_adapter_type)
return 0;
adap = to_i2c_adapter(dev);

i2c_dev = i2c_dev_get_by_minor(adap->nr);
if (!i2c_dev) /* attach_adapter must have failed */
return 0;

device_remove_file(i2c_dev->dev, &dev_attr_name);
return_i2c_dev(i2c_dev);
device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));

pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
return 0;
}

static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action, void *data)
{
struct device *dev = data;

switch (action) {
case BUS_NOTIFY_ADD_DEVICE:
return i2cdev_attach_adapter(dev, NULL);
case BUS_NOTIFY_DEL_DEVICE:
return i2cdev_detach_adapter(dev, NULL);
}

return 0;
}

static struct notifier_block i2cdev_notifier = {
.notifier_call = i2cdev_notifier_call,
};

static const struct file_operations i2cdev_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.read = i2cdev_read,
.write = i2cdev_write,
.unlocked_ioctl = i2cdev_ioctl,
.open = i2cdev_open,
.release = i2cdev_release,
};

static int __init i2c_dev_init(void)
{
int res;

printk(KERN_INFO "i2c /dev entries driver\n");

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;
}

/* Keep track of adapters which will be added or removed later */
res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
if (res)
goto out_unreg_class;

/* Bind to already existing adapters right away */
i2c_for_each_dev(NULL, i2cdev_attach_adapter);

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;
}

static void __exit i2c_dev_exit(void)
{
bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
i2c_for_each_dev(NULL, i2cdev_detach_adapter);
class_destroy(i2c_dev_class);
unregister_chrdev(I2C_MAJOR, "i2c");
}

    就先從載入和卸載函式開始吧,字元裝置的註冊一定是不可少的,這裡也用到了kernel的通知機制,主要工作就是當有i2c_adapter被註冊或卸載時,在/dev資料夾下建立或移除相對應的裝置檔案,和我們之前在test_chrdevˋ中提到的一樣。i2c_for_each_dev()函式其實就是調用bus_for_each_dev()搜尋i2c_bus上已註冊的i2c_adapter,並在/dev資料夾下建立裝置檔案。

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->adapter = adap;
file->private_data = client;

return 0;
}

static int i2cdev_release(struct inode *inode, struct file *file)
{
struct i2c_client *client = file->private_data;

i2c_put_adapter(client->adapter);
kfree(client);
file->private_data = NULL;

return 0;
}

    因為在i2cdev_attach_adapter()中裝置檔案建立的次裝置號採用i2c_adapter中nr欄位,所以在open()方法也可以透過次裝置號找到相對應的i2c_adapter,接下來就看到了之前提過的一個資料結構i2c_client,在這邊它初始化adapter的欄位並加到file的私有資料中,讓file operations其他方法都可以使用。

static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
{
char *tmp;
int ret;

struct i2c_client *client = file->private_data;

if (count > 8192)
count = 8192;

tmp = kmalloc(count, GFP_KERNEL);
if (tmp == NULL)
return -ENOMEM;

pr_debug("i2c-dev: i2c-%d reading %zu bytes.\n",
iminor(file_inode(file)), count);

ret = i2c_master_recv(client, tmp, count);
if (ret >= 0)
ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;
kfree(tmp);
return ret;
}

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_inode(file)), count);

ret = i2c_master_send(client, tmp, count);
kfree(tmp);
return ret;
}

    read()和write()方法的行為模式和我們在上一篇引用I2C規格書的Fig 11和Fig 12是匹配的。這邊有一個memdup_user()函式,分配記憶體並從user space複製資料都在裡面做完了,可以讓程式碼更簡潔,並且減少需要自行錯誤處理工作。最後分別調用i2c-core.c提供的i2c_master_recv()i2c_master_send()函式把資料網下層送。

int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
{
struct i2c_adapter *adap = client->adapter;
struct i2c_msg msg;
int ret;

msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
msg.flags |= I2C_M_RD;
msg.len = count;
msg.buf = buf;

ret = i2c_transfer(adap, &msg, 1);

/*
* If everything went ok (i.e. 1 msg received), return #bytes received,
* else error code.
*/
return (ret == 1) ? count : ret;
}

int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
{
int ret;
struct i2c_adapter *adap = client->adapter;
struct i2c_msg msg;

msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
msg.len = count;
msg.buf = (char *)buf;

ret = i2c_transfer(adap, &msg, 1);

/*
* If everything went ok (i.e. 1 msg transmitted), return #bytes
* transmitted, else error code.
*/
return (ret == 1) ? count : ret;
}

int  i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
unsigned long orig_jiffies;
int ret, try;

/* Retry automatically on arbitration loss */
orig_jiffies = jiffies;
for (ret = 0, try = 0; try <= adap->retries; try++) {
ret = adap->algo->master_xfer(adap, msgs, num);
if (ret != -EAGAIN)
break;
if (time_after(jiffies, orig_jiffies + adap->timeout))
break;
}

return ret;
}

可以看到他們的主要工作就是建構好一個i2c_msg結構,最後調用i2c_transfer()。i2c_transfer()則呼叫在XXX_i2c.c中曾經提到過的master_xfer()函式,這裡還採用了重試和time out的機制。還記得上一篇曾經說過這個read的行為模式在很多週邊都是不被支援的,所以read()方法幾乎不戶被使用,因此相對應的write()方法也很少被使用,所以在"裝置實例-I2C (1)"中的架構圖中沒有列出i2c_master_recv()i2c_master_send(),反而使用較多的是ioctl()方法。

static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct i2c_client *client = file->private_data;
unsigned long funcs;

dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
cmd, arg);

switch (cmd) {
case I2C_SLAVE:
case I2C_SLAVE_FORCE:
/* NOTE:  devices set up to work with "new style" drivers
* can't use I2C_SLAVE, even when the device node is not
* bound to a driver.  Only I2C_SLAVE_FORCE will work.
*
* Setting the PEC flag here won't affect kernel drivers,
* which will be using the i2c_client node registered with
* the driver model core.  Likewise, when that client has
* the PEC flag already set, the i2c-dev driver won't see
* (or use) this setting.
*/
if ((arg > 0x3ff) ||
   (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
return -EINVAL;
if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
return -EBUSY;
/* REVISIT: address could become busy later */
client->addr = arg;
return 0;
case I2C_TENBIT:
if (arg)
client->flags |= I2C_M_TEN;
else
client->flags &= ~I2C_M_TEN;
return 0;
case I2C_PEC:
if (arg)
client->flags |= I2C_CLIENT_PEC;
else
client->flags &= ~I2C_CLIENT_PEC;
return 0;
case I2C_FUNCS:
funcs = i2c_get_functionality(client->adapter);
return put_user(funcs, (unsigned long __user *)arg);

case I2C_RDWR:
return i2cdev_ioctl_rdrw(client, arg);

case I2C_SMBUS:
return i2cdev_ioctl_smbus(client, arg);

case I2C_RETRIES:
client->adapter->retries = arg;
break;
case I2C_TIMEOUT:
/* For historical reasons, user-space sets the timeout
* value in units of 10 ms.
*/
client->adapter->timeout = msecs_to_jiffies(arg * 10);
break;
default:
/* NOTE:  returning a fault code here could cause trouble
* in buggy userspace code.  Some old kernel bugs returned
* zero in this case, and userspace code might accidentally
* have depended on that bug.
*/
return -ENOTTY;
}
return 0;
}

struct i2c_rdwr_ioctl_data {
struct i2c_msg __user *msgs; /* pointers to i2c_msgs */
__u32 nmsgs; /* number of i2c_msgs */
};

static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client, 
unsigned long arg)
{
struct i2c_rdwr_ioctl_data rdwr_arg;
struct i2c_msg *rdwr_pa;
u8 __user **data_ptrs;
int i, res;

if (copy_from_user(&rdwr_arg,
  (struct i2c_rdwr_ioctl_data __user *)arg,
  sizeof(rdwr_arg)))
return -EFAULT;

/* Put an arbitrary limit on the number of messages that can
* be sent at once */
if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)
return -EINVAL;

rdwr_pa = memdup_user(rdwr_arg.msgs,
     rdwr_arg.nmsgs * sizeof(struct i2c_msg));
if (IS_ERR(rdwr_pa))
return PTR_ERR(rdwr_pa);

data_ptrs = kmalloc(rdwr_arg.nmsgs * sizeof(u8 __user *), GFP_KERNEL);
if (data_ptrs == NULL) {
kfree(rdwr_pa);
return -ENOMEM;
}

res = 0;
for (i = 0; i < rdwr_arg.nmsgs; i++) {
/* Limit the size of the message to a sane amount */
if (rdwr_pa[i].len > 8192) {
res = -EINVAL;
break;
}

data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
rdwr_pa[i].buf = memdup_user(data_ptrs[i], rdwr_pa[i].len);
if (IS_ERR(rdwr_pa[i].buf)) {
res = PTR_ERR(rdwr_pa[i].buf);
break;
}

/*
* If the message length is received from the slave (similar
* to SMBus block read), we must ensure that the buffer will
* be large enough to cope with a message length of
* I2C_SMBUS_BLOCK_MAX as this is the maximum underlying bus
* drivers allow. The first byte in the buffer must be
* pre-filled with the number of extra bytes, which must be
* at least one to hold the message length, but can be
* greater (for example to account for a checksum byte at
* the end of the message.)
*/
if (rdwr_pa[i].flags & I2C_M_RECV_LEN) {
if (!(rdwr_pa[i].flags & I2C_M_RD) ||
   rdwr_pa[i].buf[0] < 1 ||
   rdwr_pa[i].len < rdwr_pa[i].buf[0] +
    I2C_SMBUS_BLOCK_MAX) {
res = -EINVAL;
break;
}

rdwr_pa[i].len = rdwr_pa[i].buf[0];
}
}
if (res < 0) {
int j;
for (j = 0; j < i; ++j)
kfree(rdwr_pa[j].buf);
kfree(data_ptrs);
kfree(rdwr_pa);
return res;
}

res = i2c_transfer(client->adapter, rdwr_pa, rdwr_arg.nmsgs);
while (i-- > 0) {
if (res >= 0 && (rdwr_pa[i].flags & I2C_M_RD)) {
if (copy_to_user(data_ptrs[i], rdwr_pa[i].buf,
rdwr_pa[i].len))
res = -EFAULT;
}
kfree(rdwr_pa[i].buf);
}
kfree(data_ptrs);
kfree(rdwr_pa);
return res;
}

    ioctl()方法裡定義了很多目前不會用到的命令,目前比較需要關心的是I2C_RDWR這個命令和i2cdev_ioctl_rdwr()這個函式。user space的process呼叫ioctl這個system call的時候還需要把i2c_rdwr_ioctl_data這個資料結構當成一個參數傳過來,這個結構裡面有i2c_msg指標的欄位和msg的數量,也就是說在user space裡面就要先把i2c_msg依照周邊的要求建構好。在i2cdev_ioctl_rdwr()函式裡就是分別把i2c_rdwr_ioctl_data結構、i2c_msg結構和資料(i2c_msg中的buf欄位)都複製一份到kernel裡,接下來就是調用i2c_transfer()把資料往下層送,最後如果是讀的msg,就把讀到的資料複製到user space去。所以在應用程式中要完成的工作流程大概是open() -> 依需求建構i2c_msg陣列 -> 建構i2c_rdwr_ioctl_data結構 -> ioctl() -> close()

    到這邊為止,整個I2C的架構算是介紹完了,我認為這個架構算是一個通用的架構,如果是比較舊的kernel的話,對於EEPROM類型的裝置還有另一種架構,不過新的kernel好像取消了。除了一些增加的資料結構,多了i2c-core.c把一些API包裝起來外,有沒有發現跟我們自己創立的架構很類似呢?