blob: 6ef38a8ee95cb22ff7c6d0bc25a09c5c61b39752 [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
Erico Nunesd6760b12016-05-03 15:45:43 -030017#include <linux/cdev.h>
Wolfram Sangf01adfa2020-01-30 21:23:12 +010018#include <linux/compat.h>
Jean Delvare9ea3e942011-03-20 14:50:52 +010019#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/i2c-dev.h>
Wolfram Sanga8766072016-02-20 23:33:38 +010022#include <linux/i2c.h>
23#include <linux/init.h>
Jean Delvarecd97f392009-02-24 19:19:49 +010024#include <linux/jiffies.h>
Wolfram Sanga8766072016-02-20 23:33:38 +010025#include <linux/kernel.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/notifier.h>
29#include <linux/slab.h>
Farid Hammaneae5624f2010-05-21 18:40:59 +020030#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
David Brownell907135a2007-11-15 19:24:01 +010032/*
33 * An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a
34 * slave (i2c_client) with which messages will be exchanged. It's coupled
35 * with a character special file which is accessed by user mode drivers.
36 *
37 * The list of i2c_dev structures is parallel to the i2c_adapter lists
Jean Delvare9ea3e942011-03-20 14:50:52 +010038 * maintained by the driver model, and is updated using bus notifications.
David Brownell907135a2007-11-15 19:24:01 +010039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040struct i2c_dev {
Jean Delvaref3b3aad2006-07-01 17:17:38 +020041 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct i2c_adapter *adap;
Kevin Hao1413ef62019-10-11 23:00:14 +080043 struct device dev;
Erico Nunesd6760b12016-05-03 15:45:43 -030044 struct cdev cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Chengguang Xu8a6d5082019-02-12 14:06:57 +080047#define I2C_MINORS (MINORMASK + 1)
Jean Delvaref3b3aad2006-07-01 17:17:38 +020048static LIST_HEAD(i2c_dev_list);
49static DEFINE_SPINLOCK(i2c_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
52{
53 struct i2c_dev *i2c_dev;
54
Jean Delvaref3b3aad2006-07-01 17:17:38 +020055 spin_lock(&i2c_dev_list_lock);
56 list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
57 if (i2c_dev->adap->nr == index)
58 goto found;
59 }
60 i2c_dev = NULL;
61found:
62 spin_unlock(&i2c_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return i2c_dev;
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
67{
68 struct i2c_dev *i2c_dev;
69
Jean Delvaref3b3aad2006-07-01 17:17:38 +020070 if (adap->nr >= I2C_MINORS) {
71 printk(KERN_ERR "i2c-dev: Out of device minors (%d)\n",
72 adap->nr);
73 return ERR_PTR(-ENODEV);
74 }
75
Deepak Saxena5263ebb2005-10-17 23:09:43 +020076 i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!i2c_dev)
78 return ERR_PTR(-ENOMEM);
Jean Delvare9455e4c2006-07-01 17:16:57 +020079 i2c_dev->adap = adap;
Jean Delvaref3b3aad2006-07-01 17:17:38 +020080
81 spin_lock(&i2c_dev_list_lock);
82 list_add_tail(&i2c_dev->list, &i2c_dev_list);
83 spin_unlock(&i2c_dev_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return i2c_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Kevin Hao1413ef62019-10-11 23:00:14 +080087static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Jean Delvaref3b3aad2006-07-01 17:17:38 +020089 spin_lock(&i2c_dev_list_lock);
90 list_del(&i2c_dev->list);
91 spin_unlock(&i2c_dev_list_lock);
Kevin Hao1413ef62019-10-11 23:00:14 +080092 if (del_cdev)
93 cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev);
94 put_device(&i2c_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Guenter Roeck45f176a2013-09-26 19:36:11 -070097static ssize_t name_show(struct device *dev,
98 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Greg Kroah-Hartmanac11d062006-07-03 13:46:24 -0700100 struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800101
102 if (!i2c_dev)
103 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return sprintf(buf, "%s\n", i2c_dev->adap->name);
105}
Guenter Roeck45f176a2013-09-26 19:36:11 -0700106static DEVICE_ATTR_RO(name);
107
108static struct attribute *i2c_attrs[] = {
109 &dev_attr_name.attr,
110 NULL,
111};
112ATTRIBUTE_GROUPS(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David Brownell907135a2007-11-15 19:24:01 +0100114/* ------------------------------------------------------------------------- */
115
116/*
117 * After opening an instance of this character special file, a file
118 * descriptor starts out associated only with an i2c_adapter (and bus).
119 *
120 * Using the I2C_RDWR ioctl(), you can then *immediately* issue i2c_msg
121 * traffic to any devices on the bus used by that adapter. That's because
122 * the i2c_msg vectors embed all the addressing information they need, and
123 * are submitted directly to an i2c_adapter. However, SMBus-only adapters
124 * don't support that interface.
125 *
126 * To use read()/write() system calls on that file descriptor, or to use
127 * SMBus interfaces (and work with SMBus-only hosts!), you must first issue
128 * an I2C_SLAVE (or I2C_SLAVE_FORCE) ioctl. That configures an anonymous
129 * (never registered) i2c_client so it holds the addressing information
130 * needed by those system calls and by this SMBus interface.
131 */
132
Farid Hammaneae5624f2010-05-21 18:40:59 +0200133static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
134 loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 char *tmp;
137 int ret;
138
H Hartley Sweeten0be16c32010-05-21 18:40:57 +0200139 struct i2c_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (count > 8192)
142 count = 8192;
143
Farid Hammaneae5624f2010-05-21 18:40:59 +0200144 tmp = kmalloc(count, GFP_KERNEL);
145 if (tmp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -ENOMEM;
147
David Brownell2ce5b342008-08-10 22:56:16 +0200148 pr_debug("i2c-dev: i2c-%d reading %zu bytes.\n",
Al Viro496ad9a2013-01-23 17:07:38 -0500149 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)
Farid Hammaneae5624f2010-05-21 18:40:59 +0200153 ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 kfree(tmp);
155 return ret;
156}
157
Farid Hammaneae5624f2010-05-21 18:40:59 +0200158static ssize_t i2cdev_write(struct file *file, const char __user *buf,
159 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 int ret;
162 char *tmp;
H Hartley Sweeten0be16c32010-05-21 18:40:57 +0200163 struct i2c_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 if (count > 8192)
166 count = 8192;
167
Julia Lawallf1c2e332010-08-11 18:20:55 +0200168 tmp = memdup_user(buf, count);
169 if (IS_ERR(tmp))
170 return PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
David Brownell2ce5b342008-08-10 22:56:16 +0200172 pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n",
Al Viro496ad9a2013-01-23 17:07:38 -0500173 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 Sangfaed3152021-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;
529 struct i2c_msg32 *p;
530 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
676 pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
677 adap->name, adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Jean Delvare9ea3e942011-03-20 14:50:52 +0100681static int i2cdev_detach_adapter(struct device *dev, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Jean Delvare9ea3e942011-03-20 14:50:52 +0100683 struct i2c_adapter *adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct i2c_dev *i2c_dev;
685
Jean Delvare9ea3e942011-03-20 14:50:52 +0100686 if (dev->type != &i2c_adapter_type)
687 return 0;
688 adap = to_i2c_adapter(dev);
689
Jean Delvare9455e4c2006-07-01 17:16:57 +0200690 i2c_dev = i2c_dev_get_by_minor(adap->nr);
Jean Delvareb32d20d2006-09-03 22:19:25 +0200691 if (!i2c_dev) /* attach_adapter must have failed */
692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Kevin Hao1413ef62019-10-11 23:00:14 +0800694 put_i2c_dev(i2c_dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200696 pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699
Shubhrajyoti Deff245c2011-11-23 11:33:07 +0100700static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
Jean Delvare9ea3e942011-03-20 14:50:52 +0100701 void *data)
702{
703 struct device *dev = data;
704
705 switch (action) {
706 case BUS_NOTIFY_ADD_DEVICE:
707 return i2cdev_attach_adapter(dev, NULL);
708 case BUS_NOTIFY_DEL_DEVICE:
709 return i2cdev_detach_adapter(dev, NULL);
710 }
711
712 return 0;
713}
714
715static struct notifier_block i2cdev_notifier = {
716 .notifier_call = i2cdev_notifier_call,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717};
718
David Brownell907135a2007-11-15 19:24:01 +0100719/* ------------------------------------------------------------------------- */
720
721/*
722 * module load/unload record keeping
723 */
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static int __init i2c_dev_init(void)
726{
727 int res;
728
729 printk(KERN_INFO "i2c /dev entries driver\n");
730
Erico Nunesd6760b12016-05-03 15:45:43 -0300731 res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (res)
733 goto out;
734
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800735 i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
Sven Wegenere74783e2008-09-24 13:39:21 +0200736 if (IS_ERR(i2c_dev_class)) {
737 res = PTR_ERR(i2c_dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto out_unreg_chrdev;
Sven Wegenere74783e2008-09-24 13:39:21 +0200739 }
Guenter Roeck45f176a2013-09-26 19:36:11 -0700740 i2c_dev_class->dev_groups = i2c_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jean Delvare9ea3e942011-03-20 14:50:52 +0100742 /* Keep track of adapters which will be added or removed later */
743 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (res)
745 goto out_unreg_class;
746
Jean Delvare9ea3e942011-03-20 14:50:52 +0100747 /* Bind to already existing adapters right away */
748 i2c_for_each_dev(NULL, i2cdev_attach_adapter);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751
752out_unreg_class:
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800753 class_destroy(i2c_dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754out_unreg_chrdev:
Erico Nunesd6760b12016-05-03 15:45:43 -0300755 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756out:
757 printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
758 return res;
759}
760
761static void __exit i2c_dev_exit(void)
762{
Jean Delvare9ea3e942011-03-20 14:50:52 +0100763 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
764 i2c_for_each_dev(NULL, i2cdev_detach_adapter);
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800765 class_destroy(i2c_dev_class);
Erico Nunesd6760b12016-05-03 15:45:43 -0300766 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Jarkko Nikulaf80531c2020-06-11 14:05:34 +0300769MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
770MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771MODULE_DESCRIPTION("I2C /dev entries driver");
772MODULE_LICENSE("GPL");
773
774module_init(i2c_dev_init);
775module_exit(i2c_dev_exit);