kzs
kzs

Reputation: 1875

how to get struct i2c_client *client structure inside the ioctl?

I am moving userspace sysfs interaction to the "/dev" using miscregister using ioctl method.

Can we resolve client structure(struct i2c_client) from Inode of please somebody tell how to get client structure inside ioctl. I need to do i2c transfer inside ioctl.

I referred this link :

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

but coudln get any answer.

please someone give solution.

Upvotes: 0

Views: 3241

Answers (2)

Pramod Gurav
Pramod Gurav

Reputation: 163

while you open your device in kernel using your open function. (this part of code is copied from one of the mainline drivers (drivers/i2c/i2c-dev.c) to make things easy for you)

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;

}

when you call ioctl you can retrieve the i2c_client from the file pointer of your device:

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

}

Hope this makes your life easy.'

Upvotes: 2

ionela.voinescu
ionela.voinescu

Reputation: 384

This reference might help:

Reason to pass data using struct inode and struct file in Linux device driver programming

You build yourself a structure equivalent with "struct scull_dev" in the example above and you store there a reference to the i2c_client structure. In the IOCTL function you can retrieve later on the main control structure and the reference to the i2c_client through container_of.

Upvotes: 0

Related Questions