30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/device.h>
35 #include <linux/slab.h>
37 #include <linux/list.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c-dev.h>
57 #define I2C_MINORS 256
61 static struct i2c_dev *i2c_dev_get_by_minor(
unsigned index)
65 spin_lock(&i2c_dev_list_lock);
67 if (i2c_dev->
adap->nr == index)
72 spin_unlock(&i2c_dev_list_lock);
78 struct i2c_dev *i2c_dev;
86 i2c_dev = kzalloc(
sizeof(*i2c_dev),
GFP_KERNEL);
91 spin_lock(&i2c_dev_list_lock);
93 spin_unlock(&i2c_dev_list_lock);
97 static void return_i2c_dev(
struct i2c_dev *i2c_dev)
99 spin_lock(&i2c_dev_list_lock);
101 spin_unlock(&i2c_dev_list_lock);
108 struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(
MINOR(dev->
devt));
150 pr_debug(
"i2c-dev: i2c-%d reading %zu bytes.\n",
151 iminor(file->
f_path.dentry->d_inode), count);
160 static ssize_t i2cdev_write(
struct file *file,
const char __user *buf,
161 size_t count, loff_t *offset)
174 pr_debug(
"i2c-dev: i2c-%d writing %zu bytes.\n",
175 iminor(file->
f_path.dentry->d_inode), count);
182 static int i2cdev_check(
struct device *dev,
void *addrp)
186 if (!client || client->
addr != *(
unsigned int *)addrp)
199 if (!result && parent)
200 result = i2cdev_check_mux_parents(parent, addr);
206 static int i2cdev_check_mux_children(
struct device *dev,
void *addrp)
212 i2cdev_check_mux_children);
214 result = i2cdev_check(dev, addrp);
222 static int i2cdev_check_addr(
struct i2c_adapter *adapter,
unsigned int addr)
224 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
228 result = i2cdev_check_mux_parents(parent, addr);
232 i2cdev_check_mux_children);
256 rdwr_arg.nmsgs *
sizeof(
struct i2c_msg));
258 return PTR_ERR(rdwr_pa);
261 if (data_ptrs ==
NULL) {
267 for (i = 0; i < rdwr_arg.nmsgs; i++) {
269 if (rdwr_pa[i].
len > 8192) {
274 data_ptrs[
i] = (
u8 __user *)rdwr_pa[i].buf;
276 if (IS_ERR(rdwr_pa[i].buf)) {
277 res = PTR_ERR(rdwr_pa[i].buf);
294 rdwr_pa[i].buf[0] < 1 ||
295 rdwr_pa[i].
len < rdwr_pa[i].buf[0] +
306 for (j = 0; j <
i; ++
j)
307 kfree(rdwr_pa[j].buf);
320 kfree(rdwr_pa[i].buf);
348 "size out of range (%x) in ioctl I2C_SMBUS.\n",
357 "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
358 data_arg.read_write);
369 client->
flags, data_arg.read_write,
370 data_arg.command, data_arg.size,
NULL);
372 if (data_arg.data ==
NULL) {
374 "data is NULL pointer in ioctl I2C_SMBUS.\n");
380 datasize =
sizeof(data_arg.data->byte);
383 datasize =
sizeof(data_arg.data->word);
385 datasize =
sizeof(data_arg.data->block);
402 data_arg.read_write, data_arg.command, data_arg.size, &
temp);
412 static long i2cdev_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
417 dev_dbg(&client->
adapter->dev,
"ioctl, cmd=0x%02x, arg=0x%02lx\n",
454 funcs = i2c_get_functionality(client->
adapter);
455 return put_user(funcs, (
unsigned long __user *)arg);
458 return i2cdev_ioctl_rdrw(client, arg);
461 return i2cdev_ioctl_smbus(client, arg);
483 static int i2cdev_open(
struct inode *
inode,
struct file *file)
485 unsigned int minor = iminor(inode);
488 struct i2c_dev *i2c_dev;
490 i2c_dev = i2c_dev_get_by_minor(minor);
505 client = kzalloc(
sizeof(*client),
GFP_KERNEL);
518 static int i2cdev_release(
struct inode *inode,
struct file *file)
533 .write = i2cdev_write,
534 .unlocked_ioctl = i2cdev_ioctl,
536 .release = i2cdev_release,
541 static struct class *i2c_dev_class;
543 static int i2cdev_attach_adapter(
struct device *dev,
void *
dummy)
546 struct i2c_dev *i2c_dev;
553 i2c_dev = get_free_i2c_dev(adap);
555 return PTR_ERR(i2c_dev);
561 if (IS_ERR(i2c_dev->
dev)) {
562 res = PTR_ERR(i2c_dev->
dev);
569 pr_debug(
"i2c-dev: adapter [%s] registered as minor %d\n",
575 return_i2c_dev(i2c_dev);
579 static int i2cdev_detach_adapter(
struct device *dev,
void *dummy)
582 struct i2c_dev *i2c_dev;
588 i2c_dev = i2c_dev_get_by_minor(adap->
nr);
593 return_i2c_dev(i2c_dev);
596 pr_debug(
"i2c-dev: adapter [%s] unregistered\n", adap->
name);
607 return i2cdev_attach_adapter(dev,
NULL);
609 return i2cdev_detach_adapter(dev,
NULL);
616 .notifier_call = i2cdev_notifier_call,
625 static int __init i2c_dev_init(
void)
631 res = register_chrdev(
I2C_MAJOR,
"i2c", &i2cdev_fops);
636 if (IS_ERR(i2c_dev_class)) {
637 res = PTR_ERR(i2c_dev_class);
638 goto out_unreg_chrdev;
644 goto out_unreg_class;
660 static void __exit i2c_dev_exit(
void)