How is the read and write functions in I2C drivers for linux are communicated to linux? In all the drivers for devices on I2C in the linux source, the file_operations
structure is not used to tell the kernel about the functions. How is the various functionalities communicated to kernel, so they can be called from user space without using file_operations
?
Asked
Active
Viewed 1,353 times
1

Sam Protsenko
- 14,045
- 4
- 59
- 75

Kushagra Aggarwal
- 11
- 2
-
1Possible duplicate http://stackoverflow.com/questions/41492850/does-i2c-driver-need-to-be-implemented-just-like-any-other-character-device-driv – 0andriy Jan 21 '17 at 23:29
-
1Definitively a duplicate, the answer is exactly the same. Also OP is mistaken, there is exactly one client driver using file_ops: http://lxr.free-electrons.com/source/drivers/i2c/i2c-dev.c – Alexandre Belloni Jan 22 '17 at 01:30
-
Possible duplicate of [Does i2c driver need to be implemented just like any other character device driver?](http://stackoverflow.com/questions/41492850/does-i2c-driver-need-to-be-implemented-just-like-any-other-character-device-driv) – Sam Protsenko Jan 22 '17 at 12:57
-
@SamProtsenko, That's what I referred to in the first place. – 0andriy Jan 22 '17 at 17:35
-
1@0andriy Yeah. And I agree with your comment. That's why I clicked on "close" link under the question, stating the reason. So that comment of mine was actually left automatically. Never intended to steal that from you or something :) – Sam Protsenko Jan 22 '17 at 23:29
1 Answers
-1
I2C driver in linux support the file operation as well. Because when you start open i2c from your application;
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
file = open(filename, O_RDWR);
It will call to the i2c-dev.c file in linux kernel
static int i2cdev_open(struct inode *inode, struct file *file);
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,
};
But if you want to read and write from the user space without file_operatoin then you can use the kobject in your driver for read and write through sysfs.
For reading from the user space:
static ssize_t module_show_status(struct kobject *kobj,struct kobj_attribute *attr,char *buf);
For writing from the user space
static ssize_t module_store__status(struct kobject *kobj,struct kobj_attribute *attr,const char *buf, size_t count);
But before using these APIs you need to create the kboject in your driver.

vinod maverick
- 670
- 4
- 14