Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     i2c-dev.c - i2c-bus driver, char device interface
0004 
0005     Copyright (C) 1995-97 Simon G. Vogl
0006     Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
0007     Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
0008 
0009 */
0010 
0011 /* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
0012    But I have used so much of his original code and ideas that it seems
0013    only fair to recognize him as co-author -- Frodo */
0014 
0015 /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
0016 
0017 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0018 
0019 #include <linux/cdev.h>
0020 #include <linux/compat.h>
0021 #include <linux/device.h>
0022 #include <linux/fs.h>
0023 #include <linux/i2c-dev.h>
0024 #include <linux/i2c.h>
0025 #include <linux/init.h>
0026 #include <linux/jiffies.h>
0027 #include <linux/kernel.h>
0028 #include <linux/list.h>
0029 #include <linux/module.h>
0030 #include <linux/notifier.h>
0031 #include <linux/slab.h>
0032 #include <linux/uaccess.h>
0033 
0034 /*
0035  * An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a
0036  * slave (i2c_client) with which messages will be exchanged.  It's coupled
0037  * with a character special file which is accessed by user mode drivers.
0038  *
0039  * The list of i2c_dev structures is parallel to the i2c_adapter lists
0040  * maintained by the driver model, and is updated using bus notifications.
0041  */
0042 struct i2c_dev {
0043     struct list_head list;
0044     struct i2c_adapter *adap;
0045     struct device dev;
0046     struct cdev cdev;
0047 };
0048 
0049 #define I2C_MINORS  (MINORMASK + 1)
0050 static LIST_HEAD(i2c_dev_list);
0051 static DEFINE_SPINLOCK(i2c_dev_list_lock);
0052 
0053 static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
0054 {
0055     struct i2c_dev *i2c_dev;
0056 
0057     spin_lock(&i2c_dev_list_lock);
0058     list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
0059         if (i2c_dev->adap->nr == index)
0060             goto found;
0061     }
0062     i2c_dev = NULL;
0063 found:
0064     spin_unlock(&i2c_dev_list_lock);
0065     return i2c_dev;
0066 }
0067 
0068 static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
0069 {
0070     struct i2c_dev *i2c_dev;
0071 
0072     if (adap->nr >= I2C_MINORS) {
0073         pr_err("Out of device minors (%d)\n", adap->nr);
0074         return ERR_PTR(-ENODEV);
0075     }
0076 
0077     i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
0078     if (!i2c_dev)
0079         return ERR_PTR(-ENOMEM);
0080     i2c_dev->adap = adap;
0081 
0082     spin_lock(&i2c_dev_list_lock);
0083     list_add_tail(&i2c_dev->list, &i2c_dev_list);
0084     spin_unlock(&i2c_dev_list_lock);
0085     return i2c_dev;
0086 }
0087 
0088 static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev)
0089 {
0090     spin_lock(&i2c_dev_list_lock);
0091     list_del(&i2c_dev->list);
0092     spin_unlock(&i2c_dev_list_lock);
0093     if (del_cdev)
0094         cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev);
0095     put_device(&i2c_dev->dev);
0096 }
0097 
0098 static ssize_t name_show(struct device *dev,
0099              struct device_attribute *attr, char *buf)
0100 {
0101     struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
0102 
0103     if (!i2c_dev)
0104         return -ENODEV;
0105     return sysfs_emit(buf, "%s\n", i2c_dev->adap->name);
0106 }
0107 static DEVICE_ATTR_RO(name);
0108 
0109 static struct attribute *i2c_attrs[] = {
0110     &dev_attr_name.attr,
0111     NULL,
0112 };
0113 ATTRIBUTE_GROUPS(i2c);
0114 
0115 /* ------------------------------------------------------------------------- */
0116 
0117 /*
0118  * After opening an instance of this character special file, a file
0119  * descriptor starts out associated only with an i2c_adapter (and bus).
0120  *
0121  * Using the I2C_RDWR ioctl(), you can then *immediately* issue i2c_msg
0122  * traffic to any devices on the bus used by that adapter.  That's because
0123  * the i2c_msg vectors embed all the addressing information they need, and
0124  * are submitted directly to an i2c_adapter.  However, SMBus-only adapters
0125  * don't support that interface.
0126  *
0127  * To use read()/write() system calls on that file descriptor, or to use
0128  * SMBus interfaces (and work with SMBus-only hosts!), you must first issue
0129  * an I2C_SLAVE (or I2C_SLAVE_FORCE) ioctl.  That configures an anonymous
0130  * (never registered) i2c_client so it holds the addressing information
0131  * needed by those system calls and by this SMBus interface.
0132  */
0133 
0134 static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
0135         loff_t *offset)
0136 {
0137     char *tmp;
0138     int ret;
0139 
0140     struct i2c_client *client = file->private_data;
0141 
0142     if (count > 8192)
0143         count = 8192;
0144 
0145     tmp = kzalloc(count, GFP_KERNEL);
0146     if (tmp == NULL)
0147         return -ENOMEM;
0148 
0149     pr_debug("i2c-%d reading %zu bytes.\n", iminor(file_inode(file)), count);
0150 
0151     ret = i2c_master_recv(client, tmp, count);
0152     if (ret >= 0)
0153         if (copy_to_user(buf, tmp, ret))
0154             ret = -EFAULT;
0155     kfree(tmp);
0156     return ret;
0157 }
0158 
0159 static ssize_t i2cdev_write(struct file *file, const char __user *buf,
0160         size_t count, loff_t *offset)
0161 {
0162     int ret;
0163     char *tmp;
0164     struct i2c_client *client = file->private_data;
0165 
0166     if (count > 8192)
0167         count = 8192;
0168 
0169     tmp = memdup_user(buf, count);
0170     if (IS_ERR(tmp))
0171         return PTR_ERR(tmp);
0172 
0173     pr_debug("i2c-%d writing %zu bytes.\n", iminor(file_inode(file)), count);
0174 
0175     ret = i2c_master_send(client, tmp, count);
0176     kfree(tmp);
0177     return ret;
0178 }
0179 
0180 static int i2cdev_check(struct device *dev, void *addrp)
0181 {
0182     struct i2c_client *client = i2c_verify_client(dev);
0183 
0184     if (!client || client->addr != *(unsigned int *)addrp)
0185         return 0;
0186 
0187     return dev->driver ? -EBUSY : 0;
0188 }
0189 
0190 /* walk up mux tree */
0191 static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
0192 {
0193     struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
0194     int result;
0195 
0196     result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
0197     if (!result && parent)
0198         result = i2cdev_check_mux_parents(parent, addr);
0199 
0200     return result;
0201 }
0202 
0203 /* recurse down mux tree */
0204 static int i2cdev_check_mux_children(struct device *dev, void *addrp)
0205 {
0206     int result;
0207 
0208     if (dev->type == &i2c_adapter_type)
0209         result = device_for_each_child(dev, addrp,
0210                         i2cdev_check_mux_children);
0211     else
0212         result = i2cdev_check(dev, addrp);
0213 
0214     return result;
0215 }
0216 
0217 /* This address checking function differs from the one in i2c-core
0218    in that it considers an address with a registered device, but no
0219    driver bound to it, as NOT busy. */
0220 static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
0221 {
0222     struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
0223     int result = 0;
0224 
0225     if (parent)
0226         result = i2cdev_check_mux_parents(parent, addr);
0227 
0228     if (!result)
0229         result = device_for_each_child(&adapter->dev, &addr,
0230                         i2cdev_check_mux_children);
0231 
0232     return result;
0233 }
0234 
0235 static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
0236         unsigned nmsgs, struct i2c_msg *msgs)
0237 {
0238     u8 __user **data_ptrs;
0239     int i, res;
0240 
0241     data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
0242     if (data_ptrs == NULL) {
0243         kfree(msgs);
0244         return -ENOMEM;
0245     }
0246 
0247     res = 0;
0248     for (i = 0; i < nmsgs; i++) {
0249         /* Limit the size of the message to a sane amount */
0250         if (msgs[i].len > 8192) {
0251             res = -EINVAL;
0252             break;
0253         }
0254 
0255         data_ptrs[i] = (u8 __user *)msgs[i].buf;
0256         msgs[i].buf = memdup_user(data_ptrs[i], msgs[i].len);
0257         if (IS_ERR(msgs[i].buf)) {
0258             res = PTR_ERR(msgs[i].buf);
0259             break;
0260         }
0261         /* memdup_user allocates with GFP_KERNEL, so DMA is ok */
0262         msgs[i].flags |= I2C_M_DMA_SAFE;
0263 
0264         /*
0265          * If the message length is received from the slave (similar
0266          * to SMBus block read), we must ensure that the buffer will
0267          * be large enough to cope with a message length of
0268          * I2C_SMBUS_BLOCK_MAX as this is the maximum underlying bus
0269          * drivers allow. The first byte in the buffer must be
0270          * pre-filled with the number of extra bytes, which must be
0271          * at least one to hold the message length, but can be
0272          * greater (for example to account for a checksum byte at
0273          * the end of the message.)
0274          */
0275         if (msgs[i].flags & I2C_M_RECV_LEN) {
0276             if (!(msgs[i].flags & I2C_M_RD) ||
0277                 msgs[i].len < 1 || msgs[i].buf[0] < 1 ||
0278                 msgs[i].len < msgs[i].buf[0] +
0279                          I2C_SMBUS_BLOCK_MAX) {
0280                 i++;
0281                 res = -EINVAL;
0282                 break;
0283             }
0284 
0285             msgs[i].len = msgs[i].buf[0];
0286         }
0287     }
0288     if (res < 0) {
0289         int j;
0290         for (j = 0; j < i; ++j)
0291             kfree(msgs[j].buf);
0292         kfree(data_ptrs);
0293         kfree(msgs);
0294         return res;
0295     }
0296 
0297     res = i2c_transfer(client->adapter, msgs, nmsgs);
0298     while (i-- > 0) {
0299         if (res >= 0 && (msgs[i].flags & I2C_M_RD)) {
0300             if (copy_to_user(data_ptrs[i], msgs[i].buf,
0301                      msgs[i].len))
0302                 res = -EFAULT;
0303         }
0304         kfree(msgs[i].buf);
0305     }
0306     kfree(data_ptrs);
0307     kfree(msgs);
0308     return res;
0309 }
0310 
0311 static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
0312         u8 read_write, u8 command, u32 size,
0313         union i2c_smbus_data __user *data)
0314 {
0315     union i2c_smbus_data temp = {};
0316     int datasize, res;
0317 
0318     if ((size != I2C_SMBUS_BYTE) &&
0319         (size != I2C_SMBUS_QUICK) &&
0320         (size != I2C_SMBUS_BYTE_DATA) &&
0321         (size != I2C_SMBUS_WORD_DATA) &&
0322         (size != I2C_SMBUS_PROC_CALL) &&
0323         (size != I2C_SMBUS_BLOCK_DATA) &&
0324         (size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
0325         (size != I2C_SMBUS_I2C_BLOCK_DATA) &&
0326         (size != I2C_SMBUS_BLOCK_PROC_CALL)) {
0327         dev_dbg(&client->adapter->dev,
0328             "size out of range (%x) in ioctl I2C_SMBUS.\n",
0329             size);
0330         return -EINVAL;
0331     }
0332     /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
0333        so the check is valid if size==I2C_SMBUS_QUICK too. */
0334     if ((read_write != I2C_SMBUS_READ) &&
0335         (read_write != I2C_SMBUS_WRITE)) {
0336         dev_dbg(&client->adapter->dev,
0337             "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
0338             read_write);
0339         return -EINVAL;
0340     }
0341 
0342     /* Note that command values are always valid! */
0343 
0344     if ((size == I2C_SMBUS_QUICK) ||
0345         ((size == I2C_SMBUS_BYTE) &&
0346         (read_write == I2C_SMBUS_WRITE)))
0347         /* These are special: we do not use data */
0348         return i2c_smbus_xfer(client->adapter, client->addr,
0349                       client->flags, read_write,
0350                       command, size, NULL);
0351 
0352     if (data == NULL) {
0353         dev_dbg(&client->adapter->dev,
0354             "data is NULL pointer in ioctl I2C_SMBUS.\n");
0355         return -EINVAL;
0356     }
0357 
0358     if ((size == I2C_SMBUS_BYTE_DATA) ||
0359         (size == I2C_SMBUS_BYTE))
0360         datasize = sizeof(data->byte);
0361     else if ((size == I2C_SMBUS_WORD_DATA) ||
0362          (size == I2C_SMBUS_PROC_CALL))
0363         datasize = sizeof(data->word);
0364     else /* size == smbus block, i2c block, or block proc. call */
0365         datasize = sizeof(data->block);
0366 
0367     if ((size == I2C_SMBUS_PROC_CALL) ||
0368         (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
0369         (size == I2C_SMBUS_I2C_BLOCK_DATA) ||
0370         (read_write == I2C_SMBUS_WRITE)) {
0371         if (copy_from_user(&temp, data, datasize))
0372             return -EFAULT;
0373     }
0374     if (size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
0375         /* Convert old I2C block commands to the new
0376            convention. This preserves binary compatibility. */
0377         size = I2C_SMBUS_I2C_BLOCK_DATA;
0378         if (read_write == I2C_SMBUS_READ)
0379             temp.block[0] = I2C_SMBUS_BLOCK_MAX;
0380     }
0381     res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0382           read_write, command, size, &temp);
0383     if (!res && ((size == I2C_SMBUS_PROC_CALL) ||
0384              (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
0385              (read_write == I2C_SMBUS_READ))) {
0386         if (copy_to_user(data, &temp, datasize))
0387             return -EFAULT;
0388     }
0389     return res;
0390 }
0391 
0392 static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0393 {
0394     struct i2c_client *client = file->private_data;
0395     unsigned long funcs;
0396 
0397     dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
0398         cmd, arg);
0399 
0400     switch (cmd) {
0401     case I2C_SLAVE:
0402     case I2C_SLAVE_FORCE:
0403         if ((arg > 0x3ff) ||
0404             (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
0405             return -EINVAL;
0406         if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
0407             return -EBUSY;
0408         /* REVISIT: address could become busy later */
0409         client->addr = arg;
0410         return 0;
0411     case I2C_TENBIT:
0412         if (arg)
0413             client->flags |= I2C_M_TEN;
0414         else
0415             client->flags &= ~I2C_M_TEN;
0416         return 0;
0417     case I2C_PEC:
0418         /*
0419          * Setting the PEC flag here won't affect kernel drivers,
0420          * which will be using the i2c_client node registered with
0421          * the driver model core.  Likewise, when that client has
0422          * the PEC flag already set, the i2c-dev driver won't see
0423          * (or use) this setting.
0424          */
0425         if (arg)
0426             client->flags |= I2C_CLIENT_PEC;
0427         else
0428             client->flags &= ~I2C_CLIENT_PEC;
0429         return 0;
0430     case I2C_FUNCS:
0431         funcs = i2c_get_functionality(client->adapter);
0432         return put_user(funcs, (unsigned long __user *)arg);
0433 
0434     case I2C_RDWR: {
0435         struct i2c_rdwr_ioctl_data rdwr_arg;
0436         struct i2c_msg *rdwr_pa;
0437 
0438         if (copy_from_user(&rdwr_arg,
0439                    (struct i2c_rdwr_ioctl_data __user *)arg,
0440                    sizeof(rdwr_arg)))
0441             return -EFAULT;
0442 
0443         if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
0444             return -EINVAL;
0445 
0446         /*
0447          * Put an arbitrary limit on the number of messages that can
0448          * be sent at once
0449          */
0450         if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
0451             return -EINVAL;
0452 
0453         rdwr_pa = memdup_user(rdwr_arg.msgs,
0454                       rdwr_arg.nmsgs * sizeof(struct i2c_msg));
0455         if (IS_ERR(rdwr_pa))
0456             return PTR_ERR(rdwr_pa);
0457 
0458         return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
0459     }
0460 
0461     case I2C_SMBUS: {
0462         struct i2c_smbus_ioctl_data data_arg;
0463         if (copy_from_user(&data_arg,
0464                    (struct i2c_smbus_ioctl_data __user *) arg,
0465                    sizeof(struct i2c_smbus_ioctl_data)))
0466             return -EFAULT;
0467         return i2cdev_ioctl_smbus(client, data_arg.read_write,
0468                       data_arg.command,
0469                       data_arg.size,
0470                       data_arg.data);
0471     }
0472     case I2C_RETRIES:
0473         if (arg > INT_MAX)
0474             return -EINVAL;
0475 
0476         client->adapter->retries = arg;
0477         break;
0478     case I2C_TIMEOUT:
0479         if (arg > INT_MAX)
0480             return -EINVAL;
0481 
0482         /* For historical reasons, user-space sets the timeout
0483          * value in units of 10 ms.
0484          */
0485         client->adapter->timeout = msecs_to_jiffies(arg * 10);
0486         break;
0487     default:
0488         /* NOTE:  returning a fault code here could cause trouble
0489          * in buggy userspace code.  Some old kernel bugs returned
0490          * zero in this case, and userspace code might accidentally
0491          * have depended on that bug.
0492          */
0493         return -ENOTTY;
0494     }
0495     return 0;
0496 }
0497 
0498 #ifdef CONFIG_COMPAT
0499 
0500 struct i2c_smbus_ioctl_data32 {
0501     u8 read_write;
0502     u8 command;
0503     u32 size;
0504     compat_caddr_t data; /* union i2c_smbus_data *data */
0505 };
0506 
0507 struct i2c_msg32 {
0508     u16 addr;
0509     u16 flags;
0510     u16 len;
0511     compat_caddr_t buf;
0512 };
0513 
0514 struct i2c_rdwr_ioctl_data32 {
0515     compat_caddr_t msgs; /* struct i2c_msg __user *msgs */
0516     u32 nmsgs;
0517 };
0518 
0519 static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0520 {
0521     struct i2c_client *client = file->private_data;
0522     unsigned long funcs;
0523     switch (cmd) {
0524     case I2C_FUNCS:
0525         funcs = i2c_get_functionality(client->adapter);
0526         return put_user(funcs, (compat_ulong_t __user *)arg);
0527     case I2C_RDWR: {
0528         struct i2c_rdwr_ioctl_data32 rdwr_arg;
0529         struct i2c_msg32 __user *p;
0530         struct i2c_msg *rdwr_pa;
0531         int i;
0532 
0533         if (copy_from_user(&rdwr_arg,
0534                    (struct i2c_rdwr_ioctl_data32 __user *)arg,
0535                    sizeof(rdwr_arg)))
0536             return -EFAULT;
0537 
0538         if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
0539             return -EINVAL;
0540 
0541         if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
0542             return -EINVAL;
0543 
0544         rdwr_pa = kmalloc_array(rdwr_arg.nmsgs, sizeof(struct i2c_msg),
0545                       GFP_KERNEL);
0546         if (!rdwr_pa)
0547             return -ENOMEM;
0548 
0549         p = compat_ptr(rdwr_arg.msgs);
0550         for (i = 0; i < rdwr_arg.nmsgs; i++) {
0551             struct i2c_msg32 umsg;
0552             if (copy_from_user(&umsg, p + i, sizeof(umsg))) {
0553                 kfree(rdwr_pa);
0554                 return -EFAULT;
0555             }
0556             rdwr_pa[i] = (struct i2c_msg) {
0557                 .addr = umsg.addr,
0558                 .flags = umsg.flags,
0559                 .len = umsg.len,
0560                 .buf = (__force __u8 *)compat_ptr(umsg.buf),
0561             };
0562         }
0563 
0564         return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
0565     }
0566     case I2C_SMBUS: {
0567         struct i2c_smbus_ioctl_data32   data32;
0568         if (copy_from_user(&data32,
0569                    (void __user *) arg,
0570                    sizeof(data32)))
0571             return -EFAULT;
0572         return i2cdev_ioctl_smbus(client, data32.read_write,
0573                       data32.command,
0574                       data32.size,
0575                       compat_ptr(data32.data));
0576     }
0577     default:
0578         return i2cdev_ioctl(file, cmd, arg);
0579     }
0580 }
0581 #else
0582 #define compat_i2cdev_ioctl NULL
0583 #endif
0584 
0585 static int i2cdev_open(struct inode *inode, struct file *file)
0586 {
0587     unsigned int minor = iminor(inode);
0588     struct i2c_client *client;
0589     struct i2c_adapter *adap;
0590 
0591     adap = i2c_get_adapter(minor);
0592     if (!adap)
0593         return -ENODEV;
0594 
0595     /* This creates an anonymous i2c_client, which may later be
0596      * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
0597      *
0598      * This client is ** NEVER REGISTERED ** with the driver model
0599      * or I2C core code!!  It just holds private copies of addressing
0600      * information and maybe a PEC flag.
0601      */
0602     client = kzalloc(sizeof(*client), GFP_KERNEL);
0603     if (!client) {
0604         i2c_put_adapter(adap);
0605         return -ENOMEM;
0606     }
0607     snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
0608 
0609     client->adapter = adap;
0610     file->private_data = client;
0611 
0612     return 0;
0613 }
0614 
0615 static int i2cdev_release(struct inode *inode, struct file *file)
0616 {
0617     struct i2c_client *client = file->private_data;
0618 
0619     i2c_put_adapter(client->adapter);
0620     kfree(client);
0621     file->private_data = NULL;
0622 
0623     return 0;
0624 }
0625 
0626 static const struct file_operations i2cdev_fops = {
0627     .owner      = THIS_MODULE,
0628     .llseek     = no_llseek,
0629     .read       = i2cdev_read,
0630     .write      = i2cdev_write,
0631     .unlocked_ioctl = i2cdev_ioctl,
0632     .compat_ioctl   = compat_i2cdev_ioctl,
0633     .open       = i2cdev_open,
0634     .release    = i2cdev_release,
0635 };
0636 
0637 /* ------------------------------------------------------------------------- */
0638 
0639 static struct class *i2c_dev_class;
0640 
0641 static void i2cdev_dev_release(struct device *dev)
0642 {
0643     struct i2c_dev *i2c_dev;
0644 
0645     i2c_dev = container_of(dev, struct i2c_dev, dev);
0646     kfree(i2c_dev);
0647 }
0648 
0649 static int i2cdev_attach_adapter(struct device *dev, void *dummy)
0650 {
0651     struct i2c_adapter *adap;
0652     struct i2c_dev *i2c_dev;
0653     int res;
0654 
0655     if (dev->type != &i2c_adapter_type)
0656         return 0;
0657     adap = to_i2c_adapter(dev);
0658 
0659     i2c_dev = get_free_i2c_dev(adap);
0660     if (IS_ERR(i2c_dev))
0661         return PTR_ERR(i2c_dev);
0662 
0663     cdev_init(&i2c_dev->cdev, &i2cdev_fops);
0664     i2c_dev->cdev.owner = THIS_MODULE;
0665 
0666     device_initialize(&i2c_dev->dev);
0667     i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr);
0668     i2c_dev->dev.class = i2c_dev_class;
0669     i2c_dev->dev.parent = &adap->dev;
0670     i2c_dev->dev.release = i2cdev_dev_release;
0671 
0672     res = dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
0673     if (res)
0674         goto err_put_i2c_dev;
0675 
0676     res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
0677     if (res)
0678         goto err_put_i2c_dev;
0679 
0680     pr_debug("adapter [%s] registered as minor %d\n", adap->name, adap->nr);
0681     return 0;
0682 
0683 err_put_i2c_dev:
0684     put_i2c_dev(i2c_dev, false);
0685     return res;
0686 }
0687 
0688 static int i2cdev_detach_adapter(struct device *dev, void *dummy)
0689 {
0690     struct i2c_adapter *adap;
0691     struct i2c_dev *i2c_dev;
0692 
0693     if (dev->type != &i2c_adapter_type)
0694         return 0;
0695     adap = to_i2c_adapter(dev);
0696 
0697     i2c_dev = i2c_dev_get_by_minor(adap->nr);
0698     if (!i2c_dev) /* attach_adapter must have failed */
0699         return 0;
0700 
0701     put_i2c_dev(i2c_dev, true);
0702 
0703     pr_debug("adapter [%s] unregistered\n", adap->name);
0704     return 0;
0705 }
0706 
0707 static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
0708              void *data)
0709 {
0710     struct device *dev = data;
0711 
0712     switch (action) {
0713     case BUS_NOTIFY_ADD_DEVICE:
0714         return i2cdev_attach_adapter(dev, NULL);
0715     case BUS_NOTIFY_DEL_DEVICE:
0716         return i2cdev_detach_adapter(dev, NULL);
0717     }
0718 
0719     return 0;
0720 }
0721 
0722 static struct notifier_block i2cdev_notifier = {
0723     .notifier_call = i2cdev_notifier_call,
0724 };
0725 
0726 /* ------------------------------------------------------------------------- */
0727 
0728 /*
0729  * module load/unload record keeping
0730  */
0731 
0732 static int __init i2c_dev_init(void)
0733 {
0734     int res;
0735 
0736     pr_info("i2c /dev entries driver\n");
0737 
0738     res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
0739     if (res)
0740         goto out;
0741 
0742     i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
0743     if (IS_ERR(i2c_dev_class)) {
0744         res = PTR_ERR(i2c_dev_class);
0745         goto out_unreg_chrdev;
0746     }
0747     i2c_dev_class->dev_groups = i2c_groups;
0748 
0749     /* Keep track of adapters which will be added or removed later */
0750     res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
0751     if (res)
0752         goto out_unreg_class;
0753 
0754     /* Bind to already existing adapters right away */
0755     i2c_for_each_dev(NULL, i2cdev_attach_adapter);
0756 
0757     return 0;
0758 
0759 out_unreg_class:
0760     class_destroy(i2c_dev_class);
0761 out_unreg_chrdev:
0762     unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
0763 out:
0764     pr_err("Driver Initialisation failed\n");
0765     return res;
0766 }
0767 
0768 static void __exit i2c_dev_exit(void)
0769 {
0770     bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
0771     i2c_for_each_dev(NULL, i2cdev_detach_adapter);
0772     class_destroy(i2c_dev_class);
0773     unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
0774 }
0775 
0776 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
0777 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
0778 MODULE_DESCRIPTION("I2C /dev entries driver");
0779 MODULE_LICENSE("GPL");
0780 
0781 module_init(i2c_dev_init);
0782 module_exit(i2c_dev_exit);