通用驱动i2c-dev分析 -7

参考资料:

  • Linux驱动程序: drivers/i2c/i2c-dev.c

  • I2C-Tools-4.2: https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/

  • AT24cxx.pdf

1. 回顾字符设备驱动程序

怎么编写字符设备驱动程序?

  • 确定主设备号

  • 创建file_operations结构体

    • 在里面填充drv_open/drv_read/drv_ioctl等函数

  • 注册file_operations结构体

    • register_chrdev(major, &fops, name)

  • 谁调用register_chrdev?在入口函数调用

  • 有入口自然就有出口

    • 在出口函数unregister_chrdev

  • 辅助函数(帮助系统自动创建设备节点)

    • class_create

    • device_create

 

2. i2c-dev.c注册过程分析

2.1 register_chrdev的内部实现

从baseminor开始的count个次设备号的设备都对应这个驱动

 

2.2 i2c-dev驱动的注册过程

 主设备号I2C_MAJOR,次设备号从0开始的I2C_MINORS个次设备

对于每一个adapter,注册主设备号是I2C_MAJOR,次设备号是adap->nr的设备,adapter里面有传输函数。

/dev/i2c-n 就对应的adap->nr,标示那个i2c设备

3. file_operations函数分析

i2c-dev.c的核心:

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

主要的系统调用:open, ioctl:

 

要理解这些接口,记住一句话:APP通过I2C Controller与I2C Device传输数据。

3.1 i2cdev_open  用来找到adapter

3.2 i2cdev_ioctl: I2C_SLAVE/I2C_SLAVE_FORCE(不管是否有其他I2C驱动程序,强制调用i2cdev操作)     设置设备地址

 

3.3 i2cdev_ioctl: I2C_RDWR

copy_from_user先从用户空间复制数据,判断有多少个mesg,在进入i2cdev_ioctl_rdwr()循环调用多次把多个mesg复制进内核态,在发起一次I2C传输

 

3.4 i2cdev_ioctl: I2C_SMBUS

 

3.5 总结

 

posted on 2023-07-30 17:04  拉风摊主  阅读(30)  评论(0编辑  收藏  举报

导航