一、用户态驱动模型
1.1 I2C通用驱动代码
i2c_dev_init:
-
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;
-
}
-
-
res = i2c_add_driver(&i2cdev_driver); //注册一个I2C的设备驱动
-
if (res)
-
goto out_unreg_class;
-
-
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;
-
}
i2cdev_fops:
-
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,
-
};
i2cdev_ioctl:
-
static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
-
{
-
struct i2c_client *client = (struct i2c_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;
-
}
i2cdev_ioctl_rdrw:
-
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, //传给ioctl的arg值,强制转换成i2c_rdwr_ioctl_data
-
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 = (struct i2c_msg *)
-
kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
-
GFP_KERNEL);
-
if (!rdwr_pa)
-
return -ENOMEM;
-
-
if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
-
rdwr_arg.nmsgs * sizeof(struct i2c_msg))) {
-
kfree(rdwr_pa);
-
return -EFAULT;
-
}
-
-
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;
-
* and don't let length change either. */
-
if ((rdwr_pa[i].len > 8192) ||
-
(rdwr_pa[i].flags & I2C_M_RECV_LEN)) {
-
res = -EINVAL;
-
break;
-
}
-
data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
-
rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
-
if (rdwr_pa[i].buf == NULL) {
-
res = -ENOMEM;
-
break;
-
}
-
if (copy_from_user(rdwr_pa[i].buf, data_ptrs[i],
-
rdwr_pa[i].len)) {
-
++i; /* Needs to be kfreed too */
-
res = -EFAULT;
-
break;
-
}
-
}
-
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); //发送数据来源于i2c核心
-
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;
-
}
i2c_rdwr_ioctl_data:
-
struct i2c_rdwr_ioctl_data {
-
struct i2c_msg __user *msgs; /* pointers to i2c_msgs */ 消息指针
-
__u32 nmsgs; /* number of i2c_msgs */ 有多少条消息
-
};
一条消息(一个读操作和写操作):
-
struct i2c_msg {
-
__u16 addr; /* slave address */ //从设备地址
-
__u16 flags; //读还是写
-
#define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
-
#define I2C_M_RD 0x0001 /* read data, from slave to master */
-
#define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
-
#define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
-
#define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
-
#define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
-
#define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
-
__u16 len; /* msg length */ //消息里面有多少字节
-
__u8 *buf; /* pointer to msg data */ //消息里面的数据
-
};
i2c_transfer:
-
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
-
{
-
int ret;
-
-
if (adap->algo->master_xfer) {
-
#ifdef DEBUG
-
for (ret = 0; ret < num; ret++) {
-
dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
-
"len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
-
? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
-
(msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
-
}
-
#endif
-
-
if (in_atomic() || irqs_disabled()) {
-
ret = mutex_trylock(&adap->bus_lock);
-
if (!ret)
-
/* I2C activity is ongoing. */
-
return -EAGAIN;
-
} else {
-
mutex_lock_nested(&adap->bus_lock, adap->level);
-
}
-
-
ret = adap->algo->master_xfer(adap,msgs,num); //找到adapter(控制器)->algo(算法)->master_xfer(发送)
-
mutex_unlock(&adap->bus_lock);
-
-
return ret;
-
} else {
-
dev_dbg(&adap->dev, "I2C level transfers not supported\n");
-
return -EOPNOTSUPP;
-
}
-
}
二、EEPROM用户态驱动编写
i2c-app-drv.c:
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
#include <stdio.h>
-
#include <unistd.h>
-
#include <linux/types.h>
-
-
#define I2C_RDWR 0x0707
-
-
struct i2c_msg {
-
unsigned short addr; /* slave address */
-
unsigned short flags;
-
unsigned short len; /* msg length */
-
unsigned char *buf; /* pointer to msg data */
-
};
-
-
struct i2c_rdwr_ioctl_data {
-
struct i2c_msg *msgs; /* pointers to i2c_msgs */
-
unsigned int nmsgs; /* number of i2c_msgs */
-
};
-
-
int main()
-
{
-
int fd;
-
struct i2c_rdwr_ioctl_data e2prom_data;
-
-
//1.打开通用设备文件
-
fd = open("/dev/i2c-0", O_RDWR);
-
-
e2prom_data.msgs = (struct i2c_msg *)malloc(2*sizeof(struct i2c_msg));
-
-
//2.构造写数据到eeprom的消息
-
e2prom_data.nmsgs = 1;
-
(e2prom_data.msgs[0]).len = 2;
-
(e2prom_data.msgs[0]).addr = 0x50;
-
(e2prom_data.msgs[0]).flags = 0;
-
(e2prom_data.msgs[0]).buf = (unsigned char *)malloc(2);
-
(e2prom_data.msgs[0]).buf[0] = 0x10;
-
(e2prom_data.msgs[0]).buf[1] = 0xF0;
-
-
//3.利用ioctl写入数据
-
ioctl(fd, I2C_RDWR, (unsigned long)&e2prom_data);
-
-
//4.构造从eeprom读书据的消息
-
e2prom_data.nmsgs = 2;
-
(e2prom_data.msgs[0]).len = 1;
-
(e2prom_data.msgs[0]).addr = 0x50;
-
(e2prom_data.msgs[0]).flags = 0;
-
(e2prom_data.msgs[0]).buf[0] = 0x10;
-
-
(e2prom_data.msgs[1]).len = 1;
-
(e2prom_data.msgs[1]).addr = 0x50;
-
(e2prom_data.msgs[1]).flags = 1;
-
(e2prom_data.msgs[1]).buf = (unsigned char *)malloc(2);
-
(e2prom_data.msgs[1]).buf[0] = 0;
-
-
//5.使用ioctl读出数据
-
ioctl(fd, I2C_RDWR, (unsigned long)&e2prom_data);
-
printf("buf[0]=%x\n",(e2prom_data.msgs[1]).buf[0]);
-
//6.关闭设备
-
close(fd);
-
}
遇到的问题:
-
# ./i2c-app-drv
-
s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)
-
s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)
-
s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)
-
s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)
-
buf[0]=0
内核中的i2c和视频模块冲突了。所以要在内核中去掉视频模块:
Device Drivers>>Multimedia devices>>Video For Linux
结果:
-
# ./i2c-app-drv
-
buf[0]=f0
阅读(679) | 评论(0) | 转发(0) |