手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux设备模型之i2c子系统

[日期:2012-01-30] 来源:csdn  作者:gdt_a20 [字体:大 中 小]

五、i2c关于dev下节点的产生及其操作

  该部分的代码位于rivers/i2c/i2c-dev.c下,我们从头看起:

 

[cpp]
  1. static int __init i2c_dev_init(void)  
  2. {  
  3.     int res;  
  4.     printk(KERN_INFO "i2c /dev entries driver/n");  
  5.   /* 
  6.    * 注册名称为i2c主设备号为89的一个字符设备 
  7.    */  
  8.     res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);  
  9.     if (res)  
  10.         goto out;  
  11.   /* 
  12.    * 在class下产生i2c-dev节点,用于自动产生设备文件 
  13.    */  
  14.     i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");  
  15.     if (IS_ERR(i2c_dev_class)) {  
  16.         res = PTR_ERR(i2c_dev_class);  
  17.         goto out_unreg_chrdev;  
  18.     }  
  19.     
  20.   /* 
  21.    * i2c_add_driver在上面已经分析过了 
  22.    */  
  23.     res = i2c_add_driver(&i2cdev_driver);  
  24.     if (res)  
  25.         goto out_unreg_class;  
  26.     return 0;  
  27. out_unreg_class:  
  28.     class_destroy(i2c_dev_class);  
  29. out_unreg_chrdev:  
  30.     unregister_chrdev(I2C_MAJOR, "i2c");  
  31. out:  
  32.     printk(KERN_ERR "%s: Driver Initialisation failed/n", __FILE__);  
  33.     return res;  
  34. }  
  35. //其中i2cdev_driver结构为:   
  36. static struct i2c_driver i2cdev_driver = {  
  37.     .driver = {  
  38.         .name   = "dev_driver",  
  39.     },  
  40.     .attach_adapter = i2cdev_attach_adapter,  
  41.     .detach_adapter = i2cdev_detach_adapter,  
  42. };  

     由于没有关于client的支持表的定义,因此匹配client时就会直接返回, 由于存在成员attach_adapter,因此当匹配adapter时会进入该函数。

[cpp]
  1. /************************************ 
  2. * 来看一下i2cdev_attach_adapter函数* 
  3. ************************************/  
  4. static int i2cdev_attach_adapter(struct i2c_adapter *adap)  
  5. {  
  6.     struct i2c_dev *i2c_dev;  
  7.     int res;  
  8.     i2c_dev = get_free_i2c_dev(adap);  
  9.     if (IS_ERR(i2c_dev))  
  10.         return PTR_ERR(i2c_dev);  
  11.     /* register this i2c device with the driver core */  
  12.       
  13.     /* 
  14.    * 以上面注册的i2c_dev_class为父节点在目录class/i2c-dev下 
  15.    * 产生i2c-0之类的节点,这样上层udev会根据该节点在dev目录下 
  16.    * 自动创建对应的设备文件 
  17.    */  
  18.     i2c_dev->dev = device_create(i2c_dev_class, &adap->dev,  
  19.                      MKDEV(I2C_MAJOR, adap->nr), NULL,  
  20.                      "i2c-%d", adap->nr);  
  21.     if (IS_ERR(i2c_dev->dev)) {  
  22.         res = PTR_ERR(i2c_dev->dev);  
  23.         goto error;  
  24.     }  
  25.       
  26.   /* 
  27.    * 产生相关属性文件 
  28.    */  
  29.     res = device_create_file(i2c_dev->dev, &dev_attr_name);  
  30.     if (res)  
  31.         goto error_destroy;  
  32.     pr_debug("i2c-dev: adapter [%s] registered as minor %d/n",  
  33.          adap->name, adap->nr);  
  34.     return 0;  
  35. error_destroy:  
  36.     device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));  
  37. error:  
  38.     return_i2c_dev(i2c_dev);  
  39.     return res;  
  40. }  

    通过以上分析可以看到i2c-dev层,找到一个adapter就会自动为其创建设备节点,形式类似于i2c-*,那么当应用层open对应的设备节点的时候,内核会自动调用刚才注册的字符设备的操作函数, 我们先来看一下刚才注册的字符设备的操作集:

[cpp]
  1.  static const struct file_operations i2cdev_fops = {  
  2.     .owner      = THIS_MODULE,  
  3.     .llseek     = no_llseek,  
  4.     .read       = i2cdev_read,  
  5.     .write      = i2cdev_write,  
  6.     .unlocked_ioctl = i2cdev_ioctl,  
  7.     .open       = i2cdev_open,  
  8.     .release    = i2cdev_release,  
  9. };  

    按照用户层的流程先看一下open函数i2cdev_open:

[cpp]
  1. static int i2cdev_open(struct inode *inode, struct file *file)  
  2. {  
  3.     unsigned int minor = iminor(inode);           //得到次设备号   
  4.     struct i2c_client *client;  
  5.     struct i2c_adapter *adap;  
  6.     struct i2c_dev *i2c_dev;  
  7.     
  8.   /* 
  9.    * 次设备号其实是对应i2c总线号,下面函数遍历由次设备构成的链表 
  10.    * i2c_dev_list,找到上面挂接的号码对应的i2c_dev结构 
  11.    */  
  12.     i2c_dev = i2c_dev_get_by_minor(minor);  
  13.       
  14.     if (!i2c_dev)                                 //没找到,出错   
  15.         return -ENODEV;  
  16.     adap = i2c_get_adapter(i2c_dev->adap->nr);    //得到绑定的adapter   
  17.     if (!adap)  
  18.         return -ENODEV;  
  19.     /* This creates an anonymous i2c_client, which may later be 
  20.      * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE. 
  21.      * 
  22.      * This client is ** NEVER REGISTERED ** with the driver model 
  23.      * or I2C core code!!  It just holds private copies of addressing 
  24.      * information and maybe a PEC flag. 
  25.      */  
  26.     client = kzalloc(sizeof(*client), GFP_KERNEL); //申请个client内存   
  27.     if (!client) {  
  28.         i2c_put_adapter(adap);  
  29.         return -ENOMEM;  
  30.     }  
  31.       
  32.     //命名,依据adapter   
  33.     snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);  
  34.       
  35.     //指定driver,代表创建他的driver   
  36.     client->driver = &i2cdev_driver;  
  37.     
  38.         //指定适配器   
  39.     client->adapter = adap;  
  40.     //通过file的私有成员传递创建的这个client   
  41.     file->private_data = client;  
  42.     return 0;  
  43. }  

      由open可见,我们要操作i2c下的设备,始终是需要通过adapter,物理上也是如此,操作设备都是通过控制器进行读写的,因此我们打开的始终是adapter而open过程中会创建client,来表述我们主观上是要操作设备。下面在看一下read函数:

[cpp]
  1. static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,  
  2.         loff_t *offset)  
  3. {  
  4.     char *tmp;  
  5.     int ret;  
  6.   //得到由open传递过来的创建的client   
  7.     struct i2c_client *client = file->private_data;  
  8.     
  9.   //大小不能超过8192   
  10.     if (count > 8192)  
  11.         count = 8192;  
  12.     
  13.   //申请count大小内存   
  14.     tmp = kmalloc(count, GFP_KERNEL);  
  15.     if (tmp == NULL)  
  16.         return -ENOMEM;  
  17.     pr_debug("i2c-dev: i2c-%d reading %zu bytes./n",  
  18.         iminor(file->f_path.dentry->d_inode), count);  
  19.     
  20.   //调用i2c_master_recv进行进一步传送   
  21.     ret = i2c_master_recv(client, tmp, count);  
  22.     if (ret >= 0)  
  23.         //read的信息反馈给用户   
  24.         ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;  
  25.     kfree(tmp);  
  26.     return ret;  
  27. }  
  28. /**************************** 
  29. * 其中i2c_master_recv函数为* 
  30. ****************************/  
  31. int i2c_master_recv(struct i2c_client *client, char *buf, int count)  
  32. {  
  33.     struct i2c_adapter *adap = client->adapter;  
  34.     struct i2c_msg msg;  
  35.     int ret;  
  36.     
  37.   //利用msg组织信息结构   
  38.     msg.addr = client->addr;  
  39.     msg.flags = client->flags & I2C_M_TEN;  
  40.     msg.flags |= I2C_M_RD;  
  41.     msg.len = count;  
  42.     msg.buf = buf;  
  43.     
  44.   //调用i2c_transfer发送   
  45.     ret = i2c_transfer(adap, &msg, 1);  
  46.     /* If everything went ok (i.e. 1 msg transmitted), return #bytes 
  47.        transmitted, else error code. */  
  48.     return (ret == 1) ? count : ret;  
  49. }  

    i2c_transfer函数上面已经分析过了,其会最终调用client所在adapter的adap->algo->master_xfer函数发送。

六、总结

    分析了linux下i2c子系统模型及其关键点,针对核心的平台无关代码进行了描述,以上为个人观点,如有不妥,还望指正 ^_^

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款