标签:
2.构建file_operations结构体.open.write -->利用子系统核心层的函数来找到下层的操作方法.read
2.i2c核心层(i2c-core.c)提供一些通用函数接口,提供了注册/注销的方法
1.i2c总线驱动层(i2c控制器驱动/i2c适配器驱动) :提供操作一个硬件的能力做准备工作--》1.构建一个适配器结构体struct i2c_adapter(用来描述一个i2c设备)
1. 成员:任何一个控制器都应该有套算法const struct i2c_algorithm *algo;2. 任何一套算法都应该有一套函数来实现*master_xfer //实现这个函数--》实现读写操作方法
2.初始化控制器--》会用到平台设备驱动机制3.注册到子系统当中去 i2c_add_adapter
======================================================================
硬件: i2c控制器设备||\/
i2c设备(e2prom)
应用层app: open("设备文件") write read
1.i2c设备驱动: 两种写法 (1)万能接口驱动(i2c-dev.c)
(2)e2prom驱动、传感器驱动1.构建file_operations结构体
.open.write.read
2.创建设备文件
kernel:
驱动: 2.i2c核心层
3.i2c总线驱动
.driver = {.name = "dev_driver",},.attach_adapter = i2cdev_attach_adapter,.detach_adapter = i2cdev_detach_adapter,
i2c_dev_init/*注册*/1.register_chrdev();/*创建一个类*/2.class_create();/*加载i2c驱动*/3.i2c_add_driver();1.加载i2c设备驱动结构体,加载到一个链表中2.搜索i2c_adapter链表,每搜索一个都会调用.attach_adapter
sys_openfile_operations--> i2cdev_fops.open = i2cdev_open
/*1.获取次设备号*/unsigned int minor = iminor(inode);
/*2.创建三个结构体*/struct i2c_client *client;struct i2c_adapter *adap;struct i2c_dev *i2c_dev;
/*3.获取i2c_dev*/i2c_dev = i2c_dev_get_by_minor();
/*4.获取i2c配置*/adap = i2c_get_adapter();
/*5.设置i2c-client*/client->adapter = adap;file->private_data = client;
sys_ioctl
file_operations --> i2cdev_fops.ioctl = i2cdev_ioctl
/*1.从file->private_data里面取出i2c_client*/struct i2c_client *client = (struct i2c_client *)file->private_data;case I2C_SLAVE:
client->addr = arg;
file_operations--> i2cdev_fops.write = i2cdev_write //i2c设备层/*1.从file->private_data里面取出i2c_client*/struct i2c_client *client = (struct i2c_client *)file->private_data;/*2.发送数据*/ret = i2c_master_send(); //开始进入i2c核心层struct i2c_adapter *adap = client->adapter;struct i2c_msg msg; //构建一个写消息结构体msg.addr = client->addr; //上面ioctl写入msg.flags = client->flags *I2C_M_TEN;msg.len = count;msg.buf = (char *)buf;/*转接到底层的函数*/i2c_transfer();adap->algo->master_xfer(); //开始进入总线驱动层
file_operations--> i2cdev_fops.read = i2cdev_read //i2c设备层/*1.从file->private_data里面取出i2c_client*/struct i2c_client *client = (struct i2c_client *)file->private_data;tmp = kmalloc(count, GFP_KERNEL);ret = i2c_master_recv();struct i2c_adapter *adap = client->adapter;struct i2c_msg msg; //构建一个写消息结构体msg.addr = client->addr; //上面ioctl写入msg.flags = client->flags *I2C_M_TEN;msg.flags |= I2C_M_RD; //将标记改为读msg.len = count;msg.buf = (char *)buf;copy_to_user(buf, tmp, count);
.addr = 0xa0.adapter =.driver
.__u16 addr; /*器件地址*/.__u16 flags; /*要读还是写*/0:写 1:读.__u16 len; /*数据长度*/.__u8 *buf; /*要写入的数据,或者读出来的数据放在这里*/
标签:
原文地址:http://blog.csdn.net/chengtong222/article/details/51335759