blob: bce0e8bb785208e8380e0dde8b3dd4f45fccb432 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
David Brownell438d6c22006-12-10 21:21:31 +01003 i2c-dev.c - i2c-bus driver, char device interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
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>
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009*/
10
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 */
14
15/* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
16
Andy Shevchenko295e0e72021-07-12 17:23:22 +030017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Erico Nunesd6760b12016-05-03 15:45:43 -030019#include <linux/cdev.h>
Wolfram Sangf01adfa2020-01-30 21:23:12 +010020#include <linux/compat.h>
Jean Delvare9ea3e942011-03-20 14:50:52 +010021#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/i2c-dev.h>
Wolfram Sanga8766072016-02-20 23:33:38 +010024#include <linux/i2c.h>
25#include <linux/init.h>
Jean Delvarecd97f392009-02-24 19:19:49 +010026#include <linux/jiffies.h>
Wolfram Sanga8766072016-02-20 23:33:38 +010027#include <linux/kernel.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/notifier.h>
31#include <linux/slab.h>
Farid Hammaneae5624f2010-05-21 18:40:59 +020032#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
David Brownell907135a2007-11-15 19:24:01 +010034/*
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.
38 *
39 * The list of i2c_dev structures is parallel to the i2c_adapter lists
Jean Delvare9ea3e942011-03-20 14:50:52 +010040 * maintained by the driver model, and is updated using bus notifications.
David Brownell907135a2007-11-15 19:24:01 +010041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042struct i2c_dev {
Jean Delvaref3b3aad2006-07-01 17:17:38 +020043 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct i2c_adapter *adap;
Kevin Hao1413ef62019-10-11 23:00:14 +080045 struct device dev;
Erico Nunesd6760b12016-05-03 15:45:43 -030046 struct cdev cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Chengguang Xu8a6d5082019-02-12 14:06:57 +080049#define I2C_MINORS (MINORMASK + 1)
Jean Delvaref3b3aad2006-07-01 17:17:38 +020050static LIST_HEAD(i2c_dev_list);
51static DEFINE_SPINLOCK(i2c_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
54{
55 struct i2c_dev *i2c_dev;
56
Jean Delvaref3b3aad2006-07-01 17:17:38 +020057 spin_lock(&i2c_dev_list_lock);
58 list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
59 if (i2c_dev->adap->nr == index)
60 goto found;
61 }
62 i2c_dev = NULL;
63found:
64 spin_unlock(&i2c_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return i2c_dev;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
69{
70 struct i2c_dev *i2c_dev;
71
Jean Delvaref3b3aad2006-07-01 17:17:38 +020072 if (adap->nr >= I2C_MINORS) {
Andy Shevchenko295e0e72021-07-12 17:23:22 +030073 pr_err("Out of device minors (%d)\n", adap->nr);
Jean Delvaref3b3aad2006-07-01 17:17:38 +020074 return ERR_PTR(-ENODEV);
75 }
76
Deepak Saxena5263ebb2005-10-17 23:09:43 +020077 i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!i2c_dev)
79 return ERR_PTR(-ENOMEM);
Jean Delvare9455e4c2006-07-01 17:16:57 +020080 i2c_dev->adap = adap;
Jean Delvaref3b3aad2006-07-01 17:17:38 +020081
82 spin_lock(&i2c_dev_list_lock);
83 list_add_tail(&i2c_dev->list, &i2c_dev_list);
84 spin_unlock(&i2c_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return i2c_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Kevin Hao1413ef62019-10-11 23:00:14 +080088static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Jean Delvaref3b3aad2006-07-01 17:17:38 +020090 spin_lock(&i2c_dev_list_lock);
91 list_del(&i2c_dev->list);
92 spin_unlock(&i2c_dev_list_lock);
Kevin Hao1413ef62019-10-11 23:00:14 +080093 if (del_cdev)
94 cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev);
95 put_device(&i2c_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Guenter Roeck45f176a2013-09-26 19:36:11 -070098static ssize_t name_show(struct device *dev,
99 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Greg Kroah-Hartmanac11d062006-07-03 13:46:24 -0700101 struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800102
103 if (!i2c_dev)
104 return -ENODEV;
Andy Shevchenkob18f32d2021-07-12 17:23:23 +0300105 return sysfs_emit(buf, "%s\n", i2c_dev->adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
Guenter Roeck45f176a2013-09-26 19:36:11 -0700107static DEVICE_ATTR_RO(name);
108
109static struct attribute *i2c_attrs[] = {
110 &dev_attr_name.attr,
111 NULL,
112};
113ATTRIBUTE_GROUPS(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
David Brownell907135a2007-11-15 19:24:01 +0100115/* ------------------------------------------------------------------------- */
116
117/*
118 * After opening an instance of this character special file, a file
119 * descriptor starts out associated only with an i2c_adapter (and bus).
120 *
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.
126 *
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.
132 */
133
Farid Hammaneae5624f2010-05-21 18:40:59 +0200134static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
135 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 char *tmp;
138 int ret;
139
H Hartley Sweeten0be16c32010-05-21 18:40:57 +0200140 struct i2c_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (count > 8192)
143 count = 8192;
144
Greg Kroah-Hartman86ff25e2021-07-29 16:35:32 +0200145 tmp = kzalloc(count, GFP_KERNEL);
Farid Hammaneae5624f2010-05-21 18:40:59 +0200146 if (tmp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -ENOMEM;
148
Andy Shevchenko295e0e72021-07-12 17:23:22 +0300149 pr_debug("i2c-%d reading %zu bytes.\n", iminor(file_inode(file)), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Farid Hammaneae5624f2010-05-21 18:40:59 +0200151 ret = i2c_master_recv(client, tmp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (ret >= 0)
Greg Kroah-Hartman86ff25e2021-07-29 16:35:32 +0200153 if (copy_to_user(buf, tmp, ret))
154 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 kfree(tmp);
156 return ret;
157}
158
Farid Hammaneae5624f2010-05-21 18:40:59 +0200159static ssize_t i2cdev_write(struct file *file, const char __user *buf,
160 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 int ret;
163 char *tmp;
H Hartley Sweeten0be16c32010-05-21 18:40:57 +0200164 struct i2c_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (count > 8192)
167 count = 8192;
168
Julia Lawallf1c2e332010-08-11 18:20:55 +0200169 tmp = memdup_user(buf, count);
170 if (IS_ERR(tmp))
171 return PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Andy Shevchenko295e0e72021-07-12 17:23:22 +0300173 pr_debug("i2c-%d writing %zu bytes.\n", iminor(file_inode(file)), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Farid Hammaneae5624f2010-05-21 18:40:59 +0200175 ret = i2c_master_send(client, tmp, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 kfree(tmp);
177 return ret;
178}
179
David Brownell9b766b82008-01-27 18:14:51 +0100180static int i2cdev_check(struct device *dev, void *addrp)
181{
182 struct i2c_client *client = i2c_verify_client(dev);
183
184 if (!client || client->addr != *(unsigned int *)addrp)
185 return 0;
186
187 return dev->driver ? -EBUSY : 0;
188}
189
Michael Lawnick08263742010-08-11 18:21:02 +0200190/* walk up mux tree */
191static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
192{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200193 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200194 int result;
195
196 result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
Jean Delvare97cc4d42010-10-24 18:16:57 +0200197 if (!result && parent)
198 result = i2cdev_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200199
200 return result;
201}
202
203/* recurse down mux tree */
204static int i2cdev_check_mux_children(struct device *dev, void *addrp)
205{
206 int result;
207
208 if (dev->type == &i2c_adapter_type)
209 result = device_for_each_child(dev, addrp,
210 i2cdev_check_mux_children);
211 else
212 result = i2cdev_check(dev, addrp);
213
214 return result;
215}
216
Jean Delvarebd4217d2007-11-15 19:24:01 +0100217/* This address checking function differs from the one in i2c-core
218 in that it considers an address with a registered device, but no
David Brownell9b766b82008-01-27 18:14:51 +0100219 driver bound to it, as NOT busy. */
Jean Delvarebd4217d2007-11-15 19:24:01 +0100220static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
221{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200222 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200223 int result = 0;
224
Jean Delvare97cc4d42010-10-24 18:16:57 +0200225 if (parent)
226 result = i2cdev_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200227
228 if (!result)
229 result = device_for_each_child(&adapter->dev, &addr,
230 i2cdev_check_mux_children);
231
232 return result;
Jean Delvarebd4217d2007-11-15 19:24:01 +0100233}
234
Jean Delvarec57d3e72015-09-08 11:05:49 +0200235static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
Al Viro7d5cb452017-09-20 01:02:27 -0400236 unsigned nmsgs, struct i2c_msg *msgs)
Jean Delvaredba79972008-04-22 22:16:47 +0200237{
Jean Delvaredba79972008-04-22 22:16:47 +0200238 u8 __user **data_ptrs;
239 int i, res;
240
Kees Cook6da2ec52018-06-12 13:55:00 -0700241 data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
Jean Delvaredba79972008-04-22 22:16:47 +0200242 if (data_ptrs == NULL) {
Al Viro7d5cb452017-09-20 01:02:27 -0400243 kfree(msgs);
Jean Delvaredba79972008-04-22 22:16:47 +0200244 return -ENOMEM;
245 }
246
247 res = 0;
Al Viro7d5cb452017-09-20 01:02:27 -0400248 for (i = 0; i < nmsgs; i++) {
Jean Delvare838bfa62012-05-30 10:55:34 +0200249 /* Limit the size of the message to a sane amount */
Al Viro7d5cb452017-09-20 01:02:27 -0400250 if (msgs[i].len > 8192) {
Jean Delvaredba79972008-04-22 22:16:47 +0200251 res = -EINVAL;
252 break;
253 }
Jean Delvare838bfa62012-05-30 10:55:34 +0200254
Al Viro7d5cb452017-09-20 01:02:27 -0400255 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);
Jean Delvaredba79972008-04-22 22:16:47 +0200259 break;
260 }
Wolfram Sang978336d2017-11-04 21:20:03 +0100261 /* memdup_user allocates with GFP_KERNEL, so DMA is ok */
262 msgs[i].flags |= I2C_M_DMA_SAFE;
Jean Delvare838bfa62012-05-30 10:55:34 +0200263
264 /*
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.)
274 */
Al Viro7d5cb452017-09-20 01:02:27 -0400275 if (msgs[i].flags & I2C_M_RECV_LEN) {
276 if (!(msgs[i].flags & I2C_M_RD) ||
Alexander Popov23a27722018-04-19 15:29:22 +0300277 msgs[i].len < 1 || msgs[i].buf[0] < 1 ||
Al Viro7d5cb452017-09-20 01:02:27 -0400278 msgs[i].len < msgs[i].buf[0] +
Jean Delvare838bfa62012-05-30 10:55:34 +0200279 I2C_SMBUS_BLOCK_MAX) {
Yingjoe Chena0692f02019-05-07 22:20:32 +0800280 i++;
Jean Delvare838bfa62012-05-30 10:55:34 +0200281 res = -EINVAL;
282 break;
283 }
284
Al Viro7d5cb452017-09-20 01:02:27 -0400285 msgs[i].len = msgs[i].buf[0];
Jean Delvare838bfa62012-05-30 10:55:34 +0200286 }
Jean Delvaredba79972008-04-22 22:16:47 +0200287 }
288 if (res < 0) {
289 int j;
290 for (j = 0; j < i; ++j)
Al Viro7d5cb452017-09-20 01:02:27 -0400291 kfree(msgs[j].buf);
Jean Delvaredba79972008-04-22 22:16:47 +0200292 kfree(data_ptrs);
Al Viro7d5cb452017-09-20 01:02:27 -0400293 kfree(msgs);
Jean Delvaredba79972008-04-22 22:16:47 +0200294 return res;
295 }
296
Al Viro7d5cb452017-09-20 01:02:27 -0400297 res = i2c_transfer(client->adapter, msgs, nmsgs);
Jean Delvaredba79972008-04-22 22:16:47 +0200298 while (i-- > 0) {
Al Viro7d5cb452017-09-20 01:02:27 -0400299 if (res >= 0 && (msgs[i].flags & I2C_M_RD)) {
300 if (copy_to_user(data_ptrs[i], msgs[i].buf,
301 msgs[i].len))
Jean Delvaredba79972008-04-22 22:16:47 +0200302 res = -EFAULT;
303 }
Al Viro7d5cb452017-09-20 01:02:27 -0400304 kfree(msgs[i].buf);
Jean Delvaredba79972008-04-22 22:16:47 +0200305 }
306 kfree(data_ptrs);
Al Viro7d5cb452017-09-20 01:02:27 -0400307 kfree(msgs);
Jean Delvaredba79972008-04-22 22:16:47 +0200308 return res;
309}
310
311static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
Al Viro7d5cb452017-09-20 01:02:27 -0400312 u8 read_write, u8 command, u32 size,
313 union i2c_smbus_data __user *data)
Jean Delvaredba79972008-04-22 22:16:47 +0200314{
Vlad Tsyrklevich30f939f2017-01-09 22:53:36 +0700315 union i2c_smbus_data temp = {};
Jean Delvaredba79972008-04-22 22:16:47 +0200316 int datasize, res;
317
Al Viro7d5cb452017-09-20 01:02:27 -0400318 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)) {
Jean Delvaredba79972008-04-22 22:16:47 +0200327 dev_dbg(&client->adapter->dev,
328 "size out of range (%x) in ioctl I2C_SMBUS.\n",
Al Viro7d5cb452017-09-20 01:02:27 -0400329 size);
Jean Delvaredba79972008-04-22 22:16:47 +0200330 return -EINVAL;
331 }
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. */
Al Viro7d5cb452017-09-20 01:02:27 -0400334 if ((read_write != I2C_SMBUS_READ) &&
335 (read_write != I2C_SMBUS_WRITE)) {
Jean Delvaredba79972008-04-22 22:16:47 +0200336 dev_dbg(&client->adapter->dev,
337 "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
Al Viro7d5cb452017-09-20 01:02:27 -0400338 read_write);
Jean Delvaredba79972008-04-22 22:16:47 +0200339 return -EINVAL;
340 }
341
342 /* Note that command values are always valid! */
343
Al Viro7d5cb452017-09-20 01:02:27 -0400344 if ((size == I2C_SMBUS_QUICK) ||
345 ((size == I2C_SMBUS_BYTE) &&
346 (read_write == I2C_SMBUS_WRITE)))
Jean Delvaredba79972008-04-22 22:16:47 +0200347 /* These are special: we do not use data */
348 return i2c_smbus_xfer(client->adapter, client->addr,
Al Viro7d5cb452017-09-20 01:02:27 -0400349 client->flags, read_write,
350 command, size, NULL);
Jean Delvaredba79972008-04-22 22:16:47 +0200351
Al Viro7d5cb452017-09-20 01:02:27 -0400352 if (data == NULL) {
Jean Delvaredba79972008-04-22 22:16:47 +0200353 dev_dbg(&client->adapter->dev,
354 "data is NULL pointer in ioctl I2C_SMBUS.\n");
355 return -EINVAL;
356 }
357
Al Viro7d5cb452017-09-20 01:02:27 -0400358 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);
Jean Delvaredba79972008-04-22 22:16:47 +0200364 else /* size == smbus block, i2c block, or block proc. call */
Al Viro7d5cb452017-09-20 01:02:27 -0400365 datasize = sizeof(data->block);
Jean Delvaredba79972008-04-22 22:16:47 +0200366
Al Viro7d5cb452017-09-20 01:02:27 -0400367 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))
Jean Delvaredba79972008-04-22 22:16:47 +0200372 return -EFAULT;
373 }
Al Viro7d5cb452017-09-20 01:02:27 -0400374 if (size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
Jean Delvaredba79972008-04-22 22:16:47 +0200375 /* Convert old I2C block commands to the new
376 convention. This preserves binary compatibility. */
Al Viro7d5cb452017-09-20 01:02:27 -0400377 size = I2C_SMBUS_I2C_BLOCK_DATA;
378 if (read_write == I2C_SMBUS_READ)
Jean Delvaredba79972008-04-22 22:16:47 +0200379 temp.block[0] = I2C_SMBUS_BLOCK_MAX;
380 }
381 res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Al Viro7d5cb452017-09-20 01:02:27 -0400382 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))
Jean Delvaredba79972008-04-22 22:16:47 +0200387 return -EFAULT;
388 }
389 return res;
390}
391
Alan Cox77e38bf2008-07-14 22:38:27 +0200392static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
H Hartley Sweeten0be16c32010-05-21 18:40:57 +0200394 struct i2c_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned long funcs;
396
Jean Delvaree8aafcb2005-10-07 23:06:27 +0200397 dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
398 cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Farid Hammaneae5624f2010-05-21 18:40:59 +0200400 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 case I2C_SLAVE:
402 case I2C_SLAVE_FORCE:
David Brownell438d6c22006-12-10 21:21:31 +0100403 if ((arg > 0x3ff) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
405 return -EINVAL;
Jean Delvarebd4217d2007-11-15 19:24:01 +0100406 if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return -EBUSY;
Jean Delvarebd4217d2007-11-15 19:24:01 +0100408 /* REVISIT: address could become busy later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 client->addr = arg;
410 return 0;
411 case I2C_TENBIT:
412 if (arg)
413 client->flags |= I2C_M_TEN;
414 else
415 client->flags &= ~I2C_M_TEN;
416 return 0;
417 case I2C_PEC:
Jean Delvare9e685c82015-09-11 11:27:18 +0200418 /*
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.
424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (arg)
426 client->flags |= I2C_CLIENT_PEC;
427 else
428 client->flags &= ~I2C_CLIENT_PEC;
429 return 0;
430 case I2C_FUNCS:
431 funcs = i2c_get_functionality(client->adapter);
Jean Delvare2c003e82006-12-10 21:21:30 +0100432 return put_user(funcs, (unsigned long __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Al Viro7d5cb452017-09-20 01:02:27 -0400434 case I2C_RDWR: {
435 struct i2c_rdwr_ioctl_data rdwr_arg;
436 struct i2c_msg *rdwr_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Al Viro7d5cb452017-09-20 01:02:27 -0400438 if (copy_from_user(&rdwr_arg,
439 (struct i2c_rdwr_ioctl_data __user *)arg,
440 sizeof(rdwr_arg)))
441 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Wolfram Sang71581562021-03-12 12:57:34 +0100443 if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
444 return -EINVAL;
445
446 /*
447 * Put an arbitrary limit on the number of messages that can
448 * be sent at once
449 */
Al Viro7d5cb452017-09-20 01:02:27 -0400450 if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
451 return -EINVAL;
452
453 rdwr_pa = memdup_user(rdwr_arg.msgs,
454 rdwr_arg.nmsgs * sizeof(struct i2c_msg));
455 if (IS_ERR(rdwr_pa))
456 return PTR_ERR(rdwr_pa);
457
458 return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
459 }
460
461 case I2C_SMBUS: {
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)))
466 return -EFAULT;
467 return i2cdev_ioctl_smbus(client, data_arg.read_write,
468 data_arg.command,
469 data_arg.size,
470 data_arg.data);
471 }
David Brownell53be7952007-10-13 23:56:32 +0200472 case I2C_RETRIES:
Yi Zeng6ebec9612019-01-09 15:33:07 +0800473 if (arg > INT_MAX)
474 return -EINVAL;
475
David Brownell53be7952007-10-13 23:56:32 +0200476 client->adapter->retries = arg;
477 break;
478 case I2C_TIMEOUT:
Yi Zeng6ebec9612019-01-09 15:33:07 +0800479 if (arg > INT_MAX)
480 return -EINVAL;
481
Jean Delvarecd97f392009-02-24 19:19:49 +0100482 /* For historical reasons, user-space sets the timeout
483 * value in units of 10 ms.
484 */
485 client->adapter->timeout = msecs_to_jiffies(arg * 10);
David Brownell53be7952007-10-13 23:56:32 +0200486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 default:
David Brownell53be7952007-10-13 23:56:32 +0200488 /* 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.
492 */
493 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 return 0;
496}
497
Al Viro7d5cb452017-09-20 01:02:27 -0400498#ifdef CONFIG_COMPAT
499
500struct i2c_smbus_ioctl_data32 {
501 u8 read_write;
502 u8 command;
503 u32 size;
504 compat_caddr_t data; /* union i2c_smbus_data *data */
505};
506
507struct i2c_msg32 {
508 u16 addr;
509 u16 flags;
510 u16 len;
511 compat_caddr_t buf;
512};
513
514struct i2c_rdwr_ioctl_data32 {
515 compat_caddr_t msgs; /* struct i2c_msg __user *msgs */
516 u32 nmsgs;
517};
518
519static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
520{
521 struct i2c_client *client = file->private_data;
522 unsigned long funcs;
523 switch (cmd) {
524 case I2C_FUNCS:
525 funcs = i2c_get_functionality(client->adapter);
526 return put_user(funcs, (compat_ulong_t __user *)arg);
527 case I2C_RDWR: {
528 struct i2c_rdwr_ioctl_data32 rdwr_arg;
Andreas Hecht3265a7e2021-06-24 17:25:35 +0200529 struct i2c_msg32 __user *p;
Al Viro7d5cb452017-09-20 01:02:27 -0400530 struct i2c_msg *rdwr_pa;
531 int i;
532
533 if (copy_from_user(&rdwr_arg,
534 (struct i2c_rdwr_ioctl_data32 __user *)arg,
535 sizeof(rdwr_arg)))
536 return -EFAULT;
537
538 if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
539 return -EINVAL;
540
541 rdwr_pa = kmalloc_array(rdwr_arg.nmsgs, sizeof(struct i2c_msg),
542 GFP_KERNEL);
543 if (!rdwr_pa)
544 return -ENOMEM;
545
546 p = compat_ptr(rdwr_arg.msgs);
547 for (i = 0; i < rdwr_arg.nmsgs; i++) {
548 struct i2c_msg32 umsg;
549 if (copy_from_user(&umsg, p + i, sizeof(umsg))) {
550 kfree(rdwr_pa);
551 return -EFAULT;
552 }
553 rdwr_pa[i] = (struct i2c_msg) {
554 .addr = umsg.addr,
555 .flags = umsg.flags,
556 .len = umsg.len,
557 .buf = compat_ptr(umsg.buf)
558 };
559 }
560
561 return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
562 }
563 case I2C_SMBUS: {
564 struct i2c_smbus_ioctl_data32 data32;
565 if (copy_from_user(&data32,
566 (void __user *) arg,
567 sizeof(data32)))
568 return -EFAULT;
569 return i2cdev_ioctl_smbus(client, data32.read_write,
570 data32.command,
571 data32.size,
572 compat_ptr(data32.data));
573 }
574 default:
575 return i2cdev_ioctl(file, cmd, arg);
576 }
577}
578#else
579#define compat_i2cdev_ioctl NULL
580#endif
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static int i2cdev_open(struct inode *inode, struct file *file)
583{
584 unsigned int minor = iminor(inode);
585 struct i2c_client *client;
586 struct i2c_adapter *adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
viresh kumar5136ed42016-07-05 19:57:06 -0700588 adap = i2c_get_adapter(minor);
Vincent Sanders9669f542009-12-06 17:06:26 +0100589 if (!adap)
590 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
David Brownell907135a2007-11-15 19:24:01 +0100592 /* This creates an anonymous i2c_client, which may later be
593 * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
594 *
595 * This client is ** NEVER REGISTERED ** with the driver model
596 * or I2C core code!! It just holds private copies of addressing
597 * information and maybe a PEC flag.
598 */
Jean Delvare22f76e72006-07-01 17:20:57 +0200599 client = kzalloc(sizeof(*client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!client) {
601 i2c_put_adapter(adap);
Vincent Sanders9669f542009-12-06 17:06:26 +0100602 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Jean Delvare22f76e72006-07-01 17:20:57 +0200604 snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 client->adapter = adap;
607 file->private_data = client;
608
Vincent Sanders9669f542009-12-06 17:06:26 +0100609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612static int i2cdev_release(struct inode *inode, struct file *file)
613{
614 struct i2c_client *client = file->private_data;
615
616 i2c_put_adapter(client->adapter);
617 kfree(client);
618 file->private_data = NULL;
619
620 return 0;
621}
622
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800623static const struct file_operations i2cdev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 .owner = THIS_MODULE,
625 .llseek = no_llseek,
626 .read = i2cdev_read,
627 .write = i2cdev_write,
Alan Cox77e38bf2008-07-14 22:38:27 +0200628 .unlocked_ioctl = i2cdev_ioctl,
Al Viro7d5cb452017-09-20 01:02:27 -0400629 .compat_ioctl = compat_i2cdev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .open = i2cdev_open,
631 .release = i2cdev_release,
632};
633
David Brownell907135a2007-11-15 19:24:01 +0100634/* ------------------------------------------------------------------------- */
635
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800636static struct class *i2c_dev_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Kevin Hao1413ef62019-10-11 23:00:14 +0800638static void i2cdev_dev_release(struct device *dev)
639{
640 struct i2c_dev *i2c_dev;
641
642 i2c_dev = container_of(dev, struct i2c_dev, dev);
643 kfree(i2c_dev);
644}
645
Jean Delvare9ea3e942011-03-20 14:50:52 +0100646static int i2cdev_attach_adapter(struct device *dev, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jean Delvare9ea3e942011-03-20 14:50:52 +0100648 struct i2c_adapter *adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct i2c_dev *i2c_dev;
Jean Delvaredefcb462006-08-15 18:30:24 +0200650 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jean Delvare9ea3e942011-03-20 14:50:52 +0100652 if (dev->type != &i2c_adapter_type)
653 return 0;
654 adap = to_i2c_adapter(dev);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 i2c_dev = get_free_i2c_dev(adap);
657 if (IS_ERR(i2c_dev))
658 return PTR_ERR(i2c_dev);
659
Erico Nunesd6760b12016-05-03 15:45:43 -0300660 cdev_init(&i2c_dev->cdev, &i2cdev_fops);
661 i2c_dev->cdev.owner = THIS_MODULE;
Erico Nunesd6760b12016-05-03 15:45:43 -0300662
Kevin Hao1413ef62019-10-11 23:00:14 +0800663 device_initialize(&i2c_dev->dev);
664 i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr);
665 i2c_dev->dev.class = i2c_dev_class;
666 i2c_dev->dev.parent = &adap->dev;
667 i2c_dev->dev.release = i2cdev_dev_release;
668 dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
669
670 res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
671 if (res) {
672 put_i2c_dev(i2c_dev, false);
673 return res;
Jean Delvaredefcb462006-08-15 18:30:24 +0200674 }
Jean Delvareb32d20d2006-09-03 22:19:25 +0200675
Andy Shevchenko295e0e72021-07-12 17:23:22 +0300676 pr_debug("adapter [%s] registered as minor %d\n", adap->name, adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Jean Delvare9ea3e942011-03-20 14:50:52 +0100680static int i2cdev_detach_adapter(struct device *dev, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Jean Delvare9ea3e942011-03-20 14:50:52 +0100682 struct i2c_adapter *adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct i2c_dev *i2c_dev;
684
Jean Delvare9ea3e942011-03-20 14:50:52 +0100685 if (dev->type != &i2c_adapter_type)
686 return 0;
687 adap = to_i2c_adapter(dev);
688
Jean Delvare9455e4c2006-07-01 17:16:57 +0200689 i2c_dev = i2c_dev_get_by_minor(adap->nr);
Jean Delvareb32d20d2006-09-03 22:19:25 +0200690 if (!i2c_dev) /* attach_adapter must have failed */
691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Kevin Hao1413ef62019-10-11 23:00:14 +0800693 put_i2c_dev(i2c_dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Andy Shevchenko295e0e72021-07-12 17:23:22 +0300695 pr_debug("adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
697}
698
Shubhrajyoti Deff245c2011-11-23 11:33:07 +0100699static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
Jean Delvare9ea3e942011-03-20 14:50:52 +0100700 void *data)
701{
702 struct device *dev = data;
703
704 switch (action) {
705 case BUS_NOTIFY_ADD_DEVICE:
706 return i2cdev_attach_adapter(dev, NULL);
707 case BUS_NOTIFY_DEL_DEVICE:
708 return i2cdev_detach_adapter(dev, NULL);
709 }
710
711 return 0;
712}
713
714static struct notifier_block i2cdev_notifier = {
715 .notifier_call = i2cdev_notifier_call,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716};
717
David Brownell907135a2007-11-15 19:24:01 +0100718/* ------------------------------------------------------------------------- */
719
720/*
721 * module load/unload record keeping
722 */
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static int __init i2c_dev_init(void)
725{
726 int res;
727
Andy Shevchenko295e0e72021-07-12 17:23:22 +0300728 pr_info("i2c /dev entries driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Erico Nunesd6760b12016-05-03 15:45:43 -0300730 res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (res)
732 goto out;
733
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800734 i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
Sven Wegenere74783e2008-09-24 13:39:21 +0200735 if (IS_ERR(i2c_dev_class)) {
736 res = PTR_ERR(i2c_dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto out_unreg_chrdev;
Sven Wegenere74783e2008-09-24 13:39:21 +0200738 }
Guenter Roeck45f176a2013-09-26 19:36:11 -0700739 i2c_dev_class->dev_groups = i2c_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jean Delvare9ea3e942011-03-20 14:50:52 +0100741 /* Keep track of adapters which will be added or removed later */
742 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (res)
744 goto out_unreg_class;
745
Jean Delvare9ea3e942011-03-20 14:50:52 +0100746 /* Bind to already existing adapters right away */
747 i2c_for_each_dev(NULL, i2cdev_attach_adapter);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
750
751out_unreg_class:
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800752 class_destroy(i2c_dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753out_unreg_chrdev:
Erico Nunesd6760b12016-05-03 15:45:43 -0300754 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755out:
Andy Shevchenko295e0e72021-07-12 17:23:22 +0300756 pr_err("Driver Initialisation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return res;
758}
759
760static void __exit i2c_dev_exit(void)
761{
Jean Delvare9ea3e942011-03-20 14:50:52 +0100762 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
763 i2c_for_each_dev(NULL, i2cdev_detach_adapter);
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800764 class_destroy(i2c_dev_class);
Erico Nunesd6760b12016-05-03 15:45:43 -0300765 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Jarkko Nikulaf80531c2020-06-11 14:05:34 +0300768MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
769MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770MODULE_DESCRIPTION("I2C /dev entries driver");
771MODULE_LICENSE("GPL");
772
773module_init(i2c_dev_init);
774module_exit(i2c_dev_exit);