1 // SPDX-License-Identifier: GPL-2.0-or-later
3 i2c-dev.c - i2c-bus driver, char device interface
5 Copyright (C) 1995-97 Simon G. Vogl
6 Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
7 Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
11 /* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
12 But I have used so much of his original code and ideas that it seems
13 only fair to recognize him as co-author -- Frodo */
15 /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/cdev.h>
20 #include <linux/compat.h>
21 #include <linux/device.h>
23 #include <linux/i2c-dev.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/jiffies.h>
27 #include <linux/kernel.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/notifier.h>
31 #include <linux/slab.h>
32 #include <linux/uaccess.h>
35 * An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a
36 * slave (i2c_client) with which messages will be exchanged. It's coupled
37 * with a character special file which is accessed by user mode drivers.
39 * The list of i2c_dev structures is parallel to the i2c_adapter lists
40 * maintained by the driver model, and is updated using bus notifications.
43 struct list_head list;
44 struct i2c_adapter *adap;
49 #define I2C_MINORS (MINORMASK + 1)
50 static LIST_HEAD(i2c_dev_list);
51 static DEFINE_SPINLOCK(i2c_dev_list_lock);
53 static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
55 struct i2c_dev *i2c_dev;
57 spin_lock(&i2c_dev_list_lock);
58 list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
59 if (i2c_dev->adap->nr == index)
64 spin_unlock(&i2c_dev_list_lock);
68 static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
70 struct i2c_dev *i2c_dev;
72 if (adap->nr >= I2C_MINORS) {
73 pr_err("Out of device minors (%d)\n", adap->nr);
74 return ERR_PTR(-ENODEV);
77 i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
79 return ERR_PTR(-ENOMEM);
82 spin_lock(&i2c_dev_list_lock);
83 list_add_tail(&i2c_dev->list, &i2c_dev_list);
84 spin_unlock(&i2c_dev_list_lock);
88 static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev)
90 spin_lock(&i2c_dev_list_lock);
91 list_del(&i2c_dev->list);
92 spin_unlock(&i2c_dev_list_lock);
94 cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev);
95 put_device(&i2c_dev->dev);
98 static ssize_t name_show(struct device *dev,
99 struct device_attribute *attr, char *buf)
101 struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
105 return sysfs_emit(buf, "%s\n", i2c_dev->adap->name);
107 static DEVICE_ATTR_RO(name);
109 static struct attribute *i2c_attrs[] = {
113 ATTRIBUTE_GROUPS(i2c);
115 /* ------------------------------------------------------------------------- */
118 * After opening an instance of this character special file, a file
119 * descriptor starts out associated only with an i2c_adapter (and bus).
121 * Using the I2C_RDWR ioctl(), you can then *immediately* issue i2c_msg
122 * traffic to any devices on the bus used by that adapter. That's because
123 * the i2c_msg vectors embed all the addressing information they need, and
124 * are submitted directly to an i2c_adapter. However, SMBus-only adapters
125 * don't support that interface.
127 * To use read()/write() system calls on that file descriptor, or to use
128 * SMBus interfaces (and work with SMBus-only hosts!), you must first issue
129 * an I2C_SLAVE (or I2C_SLAVE_FORCE) ioctl. That configures an anonymous
130 * (never registered) i2c_client so it holds the addressing information
131 * needed by those system calls and by this SMBus interface.
134 static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
140 struct i2c_client *client = file->private_data;
145 tmp = kzalloc(count, GFP_KERNEL);
149 pr_debug("i2c-%d reading %zu bytes.\n", iminor(file_inode(file)), count);
151 ret = i2c_master_recv(client, tmp, count);
153 if (copy_to_user(buf, tmp, ret))
159 static ssize_t i2cdev_write(struct file *file, const char __user *buf,
160 size_t count, loff_t *offset)
164 struct i2c_client *client = file->private_data;
169 tmp = memdup_user(buf, count);
173 pr_debug("i2c-%d writing %zu bytes.\n", iminor(file_inode(file)), count);
175 ret = i2c_master_send(client, tmp, count);
180 static int i2cdev_check(struct device *dev, void *addrp)
182 struct i2c_client *client = i2c_verify_client(dev);
184 if (!client || client->addr != *(unsigned int *)addrp)
187 return dev->driver ? -EBUSY : 0;
190 /* walk up mux tree */
191 static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
193 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
196 result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
197 if (!result && parent)
198 result = i2cdev_check_mux_parents(parent, addr);
203 /* recurse down mux tree */
204 static int i2cdev_check_mux_children(struct device *dev, void *addrp)
208 if (dev->type == &i2c_adapter_type)
209 result = device_for_each_child(dev, addrp,
210 i2cdev_check_mux_children);
212 result = i2cdev_check(dev, addrp);
217 /* This address checking function differs from the one in i2c-core
218 in that it considers an address with a registered device, but no
219 driver bound to it, as NOT busy. */
220 static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
222 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
226 result = i2cdev_check_mux_parents(parent, addr);
229 result = device_for_each_child(&adapter->dev, &addr,
230 i2cdev_check_mux_children);
235 static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
236 unsigned nmsgs, struct i2c_msg *msgs)
238 u8 __user **data_ptrs;
241 data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
242 if (data_ptrs == NULL) {
248 for (i = 0; i < nmsgs; i++) {
249 /* Limit the size of the message to a sane amount */
250 if (msgs[i].len > 8192) {
255 data_ptrs[i] = (u8 __user *)msgs[i].buf;
256 msgs[i].buf = memdup_user(data_ptrs[i], msgs[i].len);
257 if (IS_ERR(msgs[i].buf)) {
258 res = PTR_ERR(msgs[i].buf);
261 /* memdup_user allocates with GFP_KERNEL, so DMA is ok */
262 msgs[i].flags |= I2C_M_DMA_SAFE;
265 * If the message length is received from the slave (similar
266 * to SMBus block read), we must ensure that the buffer will
267 * be large enough to cope with a message length of
268 * I2C_SMBUS_BLOCK_MAX as this is the maximum underlying bus
269 * drivers allow. The first byte in the buffer must be
270 * pre-filled with the number of extra bytes, which must be
271 * at least one to hold the message length, but can be
272 * greater (for example to account for a checksum byte at
273 * the end of the message.)
275 if (msgs[i].flags & I2C_M_RECV_LEN) {
276 if (!(msgs[i].flags & I2C_M_RD) ||
277 msgs[i].len < 1 || msgs[i].buf[0] < 1 ||
278 msgs[i].len < msgs[i].buf[0] +
279 I2C_SMBUS_BLOCK_MAX) {
285 msgs[i].len = msgs[i].buf[0];
290 for (j = 0; j < i; ++j)
297 res = i2c_transfer(client->adapter, msgs, nmsgs);
299 if (res >= 0 && (msgs[i].flags & I2C_M_RD)) {
300 if (copy_to_user(data_ptrs[i], msgs[i].buf,
311 static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
312 u8 read_write, u8 command, u32 size,
313 union i2c_smbus_data __user *data)
315 union i2c_smbus_data temp = {};
318 if ((size != I2C_SMBUS_BYTE) &&
319 (size != I2C_SMBUS_QUICK) &&
320 (size != I2C_SMBUS_BYTE_DATA) &&
321 (size != I2C_SMBUS_WORD_DATA) &&
322 (size != I2C_SMBUS_PROC_CALL) &&
323 (size != I2C_SMBUS_BLOCK_DATA) &&
324 (size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
325 (size != I2C_SMBUS_I2C_BLOCK_DATA) &&
326 (size != I2C_SMBUS_BLOCK_PROC_CALL)) {
327 dev_dbg(&client->adapter->dev,
328 "size out of range (%x) in ioctl I2C_SMBUS.\n",
332 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
333 so the check is valid if size==I2C_SMBUS_QUICK too. */
334 if ((read_write != I2C_SMBUS_READ) &&
335 (read_write != I2C_SMBUS_WRITE)) {
336 dev_dbg(&client->adapter->dev,
337 "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
342 /* Note that command values are always valid! */
344 if ((size == I2C_SMBUS_QUICK) ||
345 ((size == I2C_SMBUS_BYTE) &&
346 (read_write == I2C_SMBUS_WRITE)))
347 /* These are special: we do not use data */
348 return i2c_smbus_xfer(client->adapter, client->addr,
349 client->flags, read_write,
350 command, size, NULL);
353 dev_dbg(&client->adapter->dev,
354 "data is NULL pointer in ioctl I2C_SMBUS.\n");
358 if ((size == I2C_SMBUS_BYTE_DATA) ||
359 (size == I2C_SMBUS_BYTE))
360 datasize = sizeof(data->byte);
361 else if ((size == I2C_SMBUS_WORD_DATA) ||
362 (size == I2C_SMBUS_PROC_CALL))
363 datasize = sizeof(data->word);
364 else /* size == smbus block, i2c block, or block proc. call */
365 datasize = sizeof(data->block);
367 if ((size == I2C_SMBUS_PROC_CALL) ||
368 (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
369 (size == I2C_SMBUS_I2C_BLOCK_DATA) ||
370 (read_write == I2C_SMBUS_WRITE)) {
371 if (copy_from_user(&temp, data, datasize))
374 if (size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
375 /* Convert old I2C block commands to the new
376 convention. This preserves binary compatibility. */
377 size = I2C_SMBUS_I2C_BLOCK_DATA;
378 if (read_write == I2C_SMBUS_READ)
379 temp.block[0] = I2C_SMBUS_BLOCK_MAX;
381 res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
382 read_write, command, size, &temp);
383 if (!res && ((size == I2C_SMBUS_PROC_CALL) ||
384 (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
385 (read_write == I2C_SMBUS_READ))) {
386 if (copy_to_user(data, &temp, datasize))
392 static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
394 struct i2c_client *client = file->private_data;
397 dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
402 case I2C_SLAVE_FORCE:
404 (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
406 if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
408 /* REVISIT: address could become busy later */
413 client->flags |= I2C_M_TEN;
415 client->flags &= ~I2C_M_TEN;
419 * Setting the PEC flag here won't affect kernel drivers,
420 * which will be using the i2c_client node registered with
421 * the driver model core. Likewise, when that client has
422 * the PEC flag already set, the i2c-dev driver won't see
423 * (or use) this setting.
426 client->flags |= I2C_CLIENT_PEC;
428 client->flags &= ~I2C_CLIENT_PEC;
431 funcs = i2c_get_functionality(client->adapter);
432 return put_user(funcs, (unsigned long __user *)arg);
435 struct i2c_rdwr_ioctl_data rdwr_arg;
436 struct i2c_msg *rdwr_pa;
438 if (copy_from_user(&rdwr_arg,
439 (struct i2c_rdwr_ioctl_data __user *)arg,
443 if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
447 * Put an arbitrary limit on the number of messages that can
450 if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
453 rdwr_pa = memdup_user(rdwr_arg.msgs,
454 rdwr_arg.nmsgs * sizeof(struct i2c_msg));
456 return PTR_ERR(rdwr_pa);
458 return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
462 struct i2c_smbus_ioctl_data data_arg;
463 if (copy_from_user(&data_arg,
464 (struct i2c_smbus_ioctl_data __user *) arg,
465 sizeof(struct i2c_smbus_ioctl_data)))
467 return i2cdev_ioctl_smbus(client, data_arg.read_write,
476 client->adapter->retries = arg;
482 /* For historical reasons, user-space sets the timeout
483 * value in units of 10 ms.
485 client->adapter->timeout = msecs_to_jiffies(arg * 10);
488 /* NOTE: returning a fault code here could cause trouble
489 * in buggy userspace code. Some old kernel bugs returned
490 * zero in this case, and userspace code might accidentally
491 * have depended on that bug.
500 struct i2c_smbus_ioctl_data32 {
504 compat_caddr_t data; /* union i2c_smbus_data *data */
514 struct i2c_rdwr_ioctl_data32 {
515 compat_caddr_t msgs; /* struct i2c_msg __user *msgs */
519 static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
521 struct i2c_client *client = file->private_data;
525 funcs = i2c_get_functionality(client->adapter);
526 return put_user(funcs, (compat_ulong_t __user *)arg);
528 struct i2c_rdwr_ioctl_data32 rdwr_arg;
529 struct i2c_msg32 __user *p;
530 struct i2c_msg *rdwr_pa;
533 if (copy_from_user(&rdwr_arg,
534 (struct i2c_rdwr_ioctl_data32 __user *)arg,
538 if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
541 if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
544 rdwr_pa = kmalloc_array(rdwr_arg.nmsgs, sizeof(struct i2c_msg),
549 p = compat_ptr(rdwr_arg.msgs);
550 for (i = 0; i < rdwr_arg.nmsgs; i++) {
551 struct i2c_msg32 umsg;
552 if (copy_from_user(&umsg, p + i, sizeof(umsg))) {
556 rdwr_pa[i] = (struct i2c_msg) {
560 .buf = (__force __u8 *)compat_ptr(umsg.buf),
564 return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
567 struct i2c_smbus_ioctl_data32 data32;
568 if (copy_from_user(&data32,
572 return i2cdev_ioctl_smbus(client, data32.read_write,
575 compat_ptr(data32.data));
578 return i2cdev_ioctl(file, cmd, arg);
582 #define compat_i2cdev_ioctl NULL
585 static int i2cdev_open(struct inode *inode, struct file *file)
587 unsigned int minor = iminor(inode);
588 struct i2c_client *client;
589 struct i2c_adapter *adap;
591 adap = i2c_get_adapter(minor);
595 /* This creates an anonymous i2c_client, which may later be
596 * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
598 * This client is ** NEVER REGISTERED ** with the driver model
599 * or I2C core code!! It just holds private copies of addressing
600 * information and maybe a PEC flag.
602 client = kzalloc(sizeof(*client), GFP_KERNEL);
604 i2c_put_adapter(adap);
607 snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
609 client->adapter = adap;
610 file->private_data = client;
615 static int i2cdev_release(struct inode *inode, struct file *file)
617 struct i2c_client *client = file->private_data;
619 i2c_put_adapter(client->adapter);
621 file->private_data = NULL;
626 static const struct file_operations i2cdev_fops = {
627 .owner = THIS_MODULE,
630 .write = i2cdev_write,
631 .unlocked_ioctl = i2cdev_ioctl,
632 .compat_ioctl = compat_i2cdev_ioctl,
634 .release = i2cdev_release,
637 /* ------------------------------------------------------------------------- */
639 static struct class *i2c_dev_class;
641 static void i2cdev_dev_release(struct device *dev)
643 struct i2c_dev *i2c_dev;
645 i2c_dev = container_of(dev, struct i2c_dev, dev);
649 static int i2cdev_attach_adapter(struct device *dev, void *dummy)
651 struct i2c_adapter *adap;
652 struct i2c_dev *i2c_dev;
655 if (dev->type != &i2c_adapter_type)
657 adap = to_i2c_adapter(dev);
659 i2c_dev = get_free_i2c_dev(adap);
661 return PTR_ERR(i2c_dev);
663 cdev_init(&i2c_dev->cdev, &i2cdev_fops);
664 i2c_dev->cdev.owner = THIS_MODULE;
666 device_initialize(&i2c_dev->dev);
667 i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr);
668 i2c_dev->dev.class = i2c_dev_class;
669 i2c_dev->dev.parent = &adap->dev;
670 i2c_dev->dev.release = i2cdev_dev_release;
672 res = dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
674 goto err_put_i2c_dev;
676 res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
678 goto err_put_i2c_dev;
680 pr_debug("adapter [%s] registered as minor %d\n", adap->name, adap->nr);
684 put_i2c_dev(i2c_dev, false);
688 static int i2cdev_detach_adapter(struct device *dev, void *dummy)
690 struct i2c_adapter *adap;
691 struct i2c_dev *i2c_dev;
693 if (dev->type != &i2c_adapter_type)
695 adap = to_i2c_adapter(dev);
697 i2c_dev = i2c_dev_get_by_minor(adap->nr);
698 if (!i2c_dev) /* attach_adapter must have failed */
701 put_i2c_dev(i2c_dev, true);
703 pr_debug("adapter [%s] unregistered\n", adap->name);
707 static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
710 struct device *dev = data;
713 case BUS_NOTIFY_ADD_DEVICE:
714 return i2cdev_attach_adapter(dev, NULL);
715 case BUS_NOTIFY_DEL_DEVICE:
716 return i2cdev_detach_adapter(dev, NULL);
722 static struct notifier_block i2cdev_notifier = {
723 .notifier_call = i2cdev_notifier_call,
726 /* ------------------------------------------------------------------------- */
729 * module load/unload record keeping
732 static int __init i2c_dev_init(void)
736 pr_info("i2c /dev entries driver\n");
738 res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
742 i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
743 if (IS_ERR(i2c_dev_class)) {
744 res = PTR_ERR(i2c_dev_class);
745 goto out_unreg_chrdev;
747 i2c_dev_class->dev_groups = i2c_groups;
749 /* Keep track of adapters which will be added or removed later */
750 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
752 goto out_unreg_class;
754 /* Bind to already existing adapters right away */
755 i2c_for_each_dev(NULL, i2cdev_attach_adapter);
760 class_destroy(i2c_dev_class);
762 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
764 pr_err("Driver Initialisation failed\n");
768 static void __exit i2c_dev_exit(void)
770 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
771 i2c_for_each_dev(NULL, i2cdev_detach_adapter);
772 class_destroy(i2c_dev_class);
773 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
776 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
777 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
778 MODULE_DESCRIPTION("I2C /dev entries driver");
779 MODULE_LICENSE("GPL");
781 module_init(i2c_dev_init);
782 module_exit(i2c_dev_exit);