通用驱动i2c-dev分析

参考资料:

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

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

  • AT24cxx.pdf

通用的i2c-dev驱动走的还是字符设备驱动那一套,怎么编写字符设备驱动程序?

  • 创建file_operations结构体吗,在里面填充drv_open/drv_read/drv_ioctl等函数

  • 入口函数调用register_chrdev(major, &fops, name)注册file_operations结构体

  • 创建类class_create

  • 在类型创建设备节点 device_create

i2c-dev.c注册过程分析

i2c-dev驱动的注册过程

../../_images/047_i2c-dev_register.pngimage-20210226164128588

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../../_images/048_i2c-dev_interface.pngimage-20210226165250492

i2cdev_open

../../_images/049_i2cdev_open.pngimage-20210226170350844

i2cdev_ioctl: 设置器件地址 I2C_SLAVE/I2C_SLAVE_FORCE

../../_images/050_ioctl_I2C_SLAVE_FORCE.pngimage-20210226172800990

i2cdev_ioctl: 传输数据i2c的形式 I2C_RDWR

../../_images/051_ioctl_I2C_RDWR.pngimage-20210226173625871

i2cdev_ioctl: 传输数据smbus的形式I2C_SMBUS

../../_images/052_ioctl_I2C_SMBUS.png

总结

../../_images/053_app_to_i2c_driver.png