http://hi.baidu.com/tracyangrad/blog/item/97fcce41af25a73373f05d9d.html不管怎样,先列出在linux2.
6.32.2下最简短的i2c程序,因为在跟踪内核代码的过程中你会关注到它。
一、最简驱动
/* at24c08.c */#include #include #include static int at24c08_probe(
struct i2c_client *client,
const struct i2c_device_id *dev_id)
{
printk(
"at24c08_probe\n");
return 0;
}
static int at24c08_remove(
struct i2c_client *client)
{
printk(
"at24c08_remove\n");
return 0;
}
static int at24c08_detect(
struct i2c_client *client,
int kind,
struct i2c_board_info *bd_info)
{
strcpy(bd_info->type,
"at24c08");
//这个必须设置 //Point3 printk(
"at24c08_detect\n");
return 0;
}
static const struct i2c_device_id at24c08_id[] = {
{
"at24c08",
0},
{}
};
static unsigned short ignore[] = { I2C_CLIENT_END };
static const unsigned short normal_i2c[] = {
0x50, I2C_CLIENT_END};
static const struct i2c_client_address_data addr_data = {
.normal_i2c = normal_i2c,
.probe = ignore,
.ignore = ignore,
//.forces Point1};
static struct i2c_driver at24c08_driver= {
.probe = at24c08_probe,
.remove = at24c08_remove,
.driver = {
.name =
"at24c08",
},
.id_table = at24c08_id,
.detect = at24c08_detect,
.address_data = &addr_data,
.
class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
//Point2};
static int at24c08_init(
void)
{
i2c_add_driver(&at24c08_driver);
return 0;
}
static void at24c08_exit(
void)
{
i2c_del_driver(&at24c08_driver);
return ;
}
module_init(at24c08_init);
module_exit(at24c08_exit);
MODULE_LICENSE(
"GPL");
二、代码分析
适写驱动之前我们先从i2c-s3c2410.c开始分析
系统启动初始化运行i2c_adap_s3c_init
i2c_adap_s3c_init(i2c-s3c2410.c)
platform_driver_register (platform.c)
drv->driver.bus = &platform_bus_type;
//设置驱动总线的类型为平台总线 driver_register(&drv->driver) (driver.c)
bus_add_driver(drv) (bus.c)
driver_attach(drv) (db.c)
bus_for_each_dev(drv->bus, NULL, drv, __driver_attach)(bus.c)
fn(dev, data) (bus.c)
<=>__driver_attach (db.c)
driver_match_device(drv, dev) (base.h)
//调用驱动所属总线的match函数即:platform_match driver_probe_device(drv, dev) (db.c)
really_probe(dev, drv) (db.c)
if (dev->bus->probe) {
//不存在 ret = dev->bus->probe(dev);
...
}
else if (drv->probe) {
//或者调用驱动的probe函数即:s3c24xx_i2c_probe ret = drv->probe(dev);
..
}
接上
drv->probe(dev)(db.c)
s3c24xx_i2c_probe (i2c-s3c2410.c)
i2c_add_numbered_adapter(&i2c->adap) (i2c-s3c2410.c)
i2c_register_adapter (i2c-core.c)
if (adap->nr < __i2c_first_dynamic_bus_num)
//静态注册i2c_board_info的适合才会设置__i2c_first_dynamic_bus_num,而且adap->nr也没有设置 i2c_scan_static_board_info(adap);
//不会执行 i2c_do_add_adapter (i2c-core.c)
i2c_detect (i2c-core.c)
if (!driver->detect || !address_data) (i2c-core.c)
//如果我们写的驱动里没有detect函数或者address_data就直接返回了return 0;
//因为subsys_initcall(i2c_adap_s3c_init) (在i2c-s3c2410.c看到,是在系统启动的适合运行的,此时at24c08.ko还未加载,直接返回)至此,i2c-s3c2410.c初始化部分结束
当我们的at24c08.ko被加载后:
at24c08_init (at24c08.c)
i2c_add_driver(&at24c08_driver) (at24c08.c)
i2c_register_driver (i2c.h)
driver->driver.bus = &i2c_bus_type;
driver_register(&driver->driver)
bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter) (bus.c)
fn(dev, data) (bus.c)
<=>__attach_adapter (i2c-core.c)
i2c_detect (i2c-core.c)
if (address_data->forces) {
//forces不存在,见Point1 ...
}
if (!(adapter->
class & driver->
class))
//如果class也不匹配就跳出了,adapter->class在s3c24xx_i2c_probe里设置了goto exit_free
//所以在自己写的驱动程序里也要将driver的class设置相同,见Point2和s3c24xx_i2c_probeif (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
//检查i2c_algorithm里的functionality ...
//在i2c-s3c2410.c已经设置,继续执行后面 }
for (i =
0; address_data->probe[i] != I2C_CLIENT_END; i +=
2) {
//ignore ...
}
for (i =
0; address_data->normal_i2c[i] != I2C_CLIENT_END; i +=
1) {
//正常入口,除非被忽略 ...
i2c_detect_address(temp_client, -
1, driver) (i2c-core.c)
if (i2c_smbus_xfer(adapter, addr,
0,
0,
0,I2C_SMBUS_QUICK, NULL) <
0)
//检查这个地址是否对,不对就返回return 0;
driver->detect(temp_client, kind, &info)
//Point5 <=>at24c08_detect (at24c08.c)
//调用到自己写的探测函数if (info.type[
0] ==
'\0') {
//Point3 ...
}
else {
//要想执行else语句,必须在自定义detect函数里设置info.type字段,即client->namestruct i2c_client *client;
client = i2c_new_device(adapter, &info) (i2c-core.c)
//用info的信息设置client //Point4 client->dev.bus = &i2c_bus_type
device_register(&client->dev) (core.c)
device_add (core.c)
bus_probe_device (bus.c)
device_attach (bus.c)
device_attach (db.c)
bus_for_each_drv(dev->bus, NULL, dev, __device_attach) (bus.c)
fn(drv, data)
<=>__device_attach (db.c)
driver_match_device (base.h)
driver_probe_device (db.c)
really_probe (db.c)
if (dev->bus->probe) {
//dev->bus为i2c_bus_type,则调用i2c_device_probe ret = dev->bus->probe(dev);
driver->probe (i2c-core.c)
<=>at24c08_probe (at24c08.c)
//自定义的probe函数被调用 ...
}
else if (drv->probe) {
ret = drv->probe(dev);
...
}
list_add_tail(&client->detected, &driver->clients);
}
}
}
所以加载上面简单的at24c08.ko,运行结果就是:
at24c08_detect
at24c08_probe
三、编写程序
下面我们看看在i2c_new_device里面怎么设置client,我们必须先在自定义detect函数里设置(Point4)
client->dev.platform_data = info->platform_data;
client->flags = info->flags;
client->addr = info->addr;
client->irq = info->irq;
strlcpy(client->name, info->type,
sizeof(client->name));
1.at24c08_detect里,我们只需要加上
info->addr = client->addr;
2.构建字符设备驱动
在at24c08_probe中添加
register_chrdev_region(major,
1,
"at24c08");
cdev_init(&cdev, &at24c08_fops);
cdev_add(&cdev, MKDEV(major,
0),
1);
class_register(&at24c08_cls);
device_create(&at24c08_cls, NULL, MKDEV(major,
0), NULL,
"at24c08_dev");
另外加上
#define major
155static struct class at24c08_cls = {
.name =
"at24c08_cls",
};
static struct file_operations at24c08_fops = {
.owner = THIS_MODULE,
};
3.完善at24c08_fops
加上读写函数
.read = at24c08_read,
.write = at24c08_write,
i2c通信是通过i2c_msg传输的,
读:先从用户空间获得地址,用i2c_transfer先传地址,再接收i2c返回的数据交给用户
写:先从用户空间获得地址和写入值,用i2c_transfer传一个msg就可以了。
在这里遇到问题:i2c_tranfer需要adapter参数,并且msg需要目的地址。
这个时候应该记得driver->detect(temp_client, kind, &info) Point5传到自定义的temp_client,
也就是at24c08_detect的第一个参数
struct i2c_client *client,它里面就有adapter和addr的信息,
那么就在at24c08_detect里加上at24c08_adapter = client->adapter; addr = bd_info->addr = client->addr;
需先定义
static struct i2c_adapter *at24c08_adapter;
static unsigned short addr;
完整的驱动程序就应该是:
#include #include #include #include #include #include #include #include #define major
155static struct i2c_driver at24c08_driver;
static struct i2c_adapter *at24c08_adapter;
static unsigned short addr;
static struct class at24c08_cls = {
.name =
"at24c08_cls",
};
ssize_t at24c08_read (
struct file *filp,
char __user *buf, size_t sz, loff_t *off)
{
struct i2c_msg msg[
2];
unsigned char args, data;
if (sz !=
1)
return -EINVAL;
copy_from_user((
void *)&args, buf,
1);
/* 先传读地址 */ msg[
0].addr = addr;
msg[
0].buf = &args;
msg[
0].len =
1;
msg[
0].flags =
0;
/* 再 读 */ msg[
1].addr = addr;
msg[
1].buf = &data;
msg[
1].len =
1;
msg[
1].flags =
1;
/* 读 */if (
2 == i2c_transfer(at24c08_adapter, msg,
2)) {
/* 读成功 */ copy_to_user((
void *)buf, &data,
1);
return 1;
}
elsereturn -EIO;
}
ssize_t at24c08_write (
struct file *filp,
const char __user *buf, size_t sz, loff_t *off)
{
struct i2c_msg msg;
unsigned char args[
2];
copy_from_user((
void *)&args, buf,
2);
/* args[0] = addr, args[1] = val */ msg.addr = addr;
msg.buf = args;
msg.len =
2;
msg.flags =
0;
/* 写 */if(
1 == i2c_transfer(at24c08_adapter,&msg,
1))
return 2;
elsereturn -EIO;
}
static struct file_operations at24c08_fops = {
.owner = THIS_MODULE,
.read = at24c08_read,
.write = at24c08_write,
};
static struct cdev cdev;
static int at24c08_probe(
struct i2c_client *client,
const struct i2c_device_id *dev_id)
{
register_chrdev_region(major,
1,
"at24c08");
cdev_init(&cdev, &at24c08_fops);
cdev_add(&cdev, MKDEV(major,
0),
1);
class_register(&at24c08_cls);
device_create(&at24c08_cls, NULL, MKDEV(major,
0), NULL,
"at24c08_dev");
return 0;
}
static int at24c08_remove(
struct i2c_client *client)
{
device_destroy(&at24c08_cls, MKDEV(major,
0));
class_destroy(&at24c08_cls);
cdev_del(&cdev);
unregister_chrdev_region(MKDEV(major,
0),
1);
return 0;
}
static int at24c08_detect(
struct i2c_client *client,
int kind,
struct i2c_board_info *bd_info)
{
strcpy(bd_info->type,
"at24c08");
addr = bd_info->addr = client->addr;
at24c08_adapter = client->adapter;
return 0;
}
static const struct i2c_device_id at24c08_id[] = {
{
"at24c08",
0},
{}
};
static unsigned short ignore[] = { I2C_CLIENT_END };
static const unsigned short normal_i2c[] = {
0x50, I2C_CLIENT_END};
static const struct i2c_client_address_data addr_data = {
.normal_i2c = normal_i2c,
.probe = ignore,
.ignore = ignore,
};
static struct i2c_driver at24c08_driver= {
.probe = at24c08_probe,
.remove = at24c08_remove,
.driver = {
.name =
"at24c08",
},
.id_table = at24c08_id,
.detect = at24c08_detect,
.address_data = &addr_data,
.
class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
};
static int at24c08_init(
void)
{
i2c_add_driver(&at24c08_driver);
return 0;
}
static void at24c08_exit(
void)
{
i2c_del_driver(&at24c08_driver);
return ;
}
module_init(at24c08_init);
module_exit(at24c08_exit);
MODULE_LICENSE(
"GPL");
MODULE_AUTHOR(
"100ask.net Young");
应用程序:
#include #include #include #define USAGE printf(
"./testapp r addr\n./testapp w addr data\n")
int main(
int argc,
char *argv[])
{
int fd;
unsigned char dat[
2];
if ((argc!=
3) && (argc!=
4)) {
USAGE;
return -
1;
}
fd = open(
"/dev/at24c08_dev", O_RDWR);
if (-
1 == fd) {
printf(
"failed to open device.\n");
return -
1;
}
if (!strcmp(argv[
1],
"r")) {
/* 读 */ dat[
0] = strtoul(argv[
2], NULL,
10);
dat[
1] = dat[
0];
read(fd, dat,
1);
printf(
"read addr%d: %c, %d, 0x%2x\n", dat[
1], dat[
0], dat[
0], dat[
0]);
}
else if(!strcmp(argv[
1],
"w")) {
/* 写 */ dat[
0] = strtoul(argv[
2], NULL,
10);
dat[
1] = strtoul(argv[
3], NULL,
10);
write(fd, dat,
2);
}
else {
USAGE;
return -
1;
}
return 0;
}
就是有一个比较奇怪的问题,如果直接随便读一个地址A,读到的值是传送的地址A。如果先将数值B写入C地址,再读C地址就是得到B数值了。
当我卸载模块、重启机器之后再来试验,再读C地址,是正确的得到B数值,所以驱动和用户程序没问题。