0

我正在miscregister使用 ioctl 方法将用户空间 sysfs 交互移动到“/dev”。

我们能否从 Inode 解析客户端结构(struct i2c_client),请有人告诉我如何在 ioctl 中获取客户端结构。我需要在 ioctl 中进行 i2c 传输。

我提到了这个链接:

http://stackoverflow.com/questions/2635038/inode-to-device-information

但可以得到任何答案。

请有人给出解决方案。

4

2 回答 2

2

当您使用 open 函数在内核中打开设备时。(这部分代码是从其中一个主线驱动程序 (drivers/i2c/i2c-dev.c) 中复制的,以便于您)

my_i2c_device_open(struct inode *inode, struct file *file)
{
    unsigned int minor = iminor(inode);
    struct i2c_client *client;
    struct i2c_adapter *adap;
    struct i2c_dev *i2c_dev;

    i2c_dev = i2c_dev_get_by_minor(minor);
    if (!i2c_dev)
        return -ENODEV;

    adap = i2c_get_adapter(i2c_dev->adap->nr);
    if (!adap)
        return -ENODEV;

    client = kzalloc(sizeof(*client), GFP_KERNEL);
    if (!client) {
        i2c_put_adapter(adap);
        return -ENOMEM;
    }
    snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
    client->adapter = adap;
    file->private_data = client;

    return 0;

}

当您调用 ioctl 时,您可以从设备的文件指针中检索 i2c_client:

static long my_i2c_device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    struct i2c_client *client = file->private_data;

}

希望这能让你的生活变得轻松。

于 2013-07-06T09:38:00.157 回答
0

此参考可能会有所帮助:

Linux设备驱动程序编程中使用struct inode和struct file传递数据的原因

在上面的示例中,您自己构建了一个与“struct scull_dev”等效的结构,并在那里存储了对 i2c_client 结构的引用。在 IOCTL 函数中,您可以稍后通过 container_of 检索主控制结构和对 i2c_client 的引用。

于 2013-07-05T16:21:00.090 回答