blob: da020acc9bbd4f3c4d3ce1bdacc01e96f07982ec [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
Al Viro7d5cb452017-09-20 01:02:27 -0400443 /* Put an arbitrary limit on the number of messages that can
444 * be sent at once */
445 if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
446 return -EINVAL;
447
448 rdwr_pa = memdup_user(rdwr_arg.msgs,
449 rdwr_arg.nmsgs * sizeof(struct i2c_msg));
450 if (IS_ERR(rdwr_pa))
451 return PTR_ERR(rdwr_pa);
452
453 return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
454 }
455
456 case I2C_SMBUS: {
457 struct i2c_smbus_ioctl_data data_arg;
458 if (copy_from_user(&data_arg,
459 (struct i2c_smbus_ioctl_data __user *) arg,
460 sizeof(struct i2c_smbus_ioctl_data)))
461 return -EFAULT;
462 return i2cdev_ioctl_smbus(client, data_arg.read_write,
463 data_arg.command,
464 data_arg.size,
465 data_arg.data);
466 }
David Brownell53be7952007-10-13 23:56:32 +0200467 case I2C_RETRIES:
Yi Zeng6ebec9612019-01-09 15:33:07 +0800468 if (arg > INT_MAX)
469 return -EINVAL;
470
David Brownell53be7952007-10-13 23:56:32 +0200471 client->adapter->retries = arg;
472 break;
473 case I2C_TIMEOUT:
Yi Zeng6ebec9612019-01-09 15:33:07 +0800474 if (arg > INT_MAX)
475 return -EINVAL;
476
Jean Delvarecd97f392009-02-24 19:19:49 +0100477 /* For historical reasons, user-space sets the timeout
478 * value in units of 10 ms.
479 */
480 client->adapter->timeout = msecs_to_jiffies(arg * 10);
David Brownell53be7952007-10-13 23:56:32 +0200481 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 default:
David Brownell53be7952007-10-13 23:56:32 +0200483 /* NOTE: returning a fault code here could cause trouble
484 * in buggy userspace code. Some old kernel bugs returned
485 * zero in this case, and userspace code might accidentally
486 * have depended on that bug.
487 */
488 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 return 0;
491}
492
Al Viro7d5cb452017-09-20 01:02:27 -0400493#ifdef CONFIG_COMPAT
494
495struct i2c_smbus_ioctl_data32 {
496 u8 read_write;
497 u8 command;
498 u32 size;
499 compat_caddr_t data; /* union i2c_smbus_data *data */
500};
501
502struct i2c_msg32 {
503 u16 addr;
504 u16 flags;
505 u16 len;
506 compat_caddr_t buf;
507};
508
509struct i2c_rdwr_ioctl_data32 {
510 compat_caddr_t msgs; /* struct i2c_msg __user *msgs */
511 u32 nmsgs;
512};
513
514static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
515{
516 struct i2c_client *client = file->private_data;
517 unsigned long funcs;
518 switch (cmd) {
519 case I2C_FUNCS:
520 funcs = i2c_get_functionality(client->adapter);
521 return put_user(funcs, (compat_ulong_t __user *)arg);
522 case I2C_RDWR: {
523 struct i2c_rdwr_ioctl_data32 rdwr_arg;
524 struct i2c_msg32 *p;
525 struct i2c_msg *rdwr_pa;
526 int i;
527
528 if (copy_from_user(&rdwr_arg,
529 (struct i2c_rdwr_ioctl_data32 __user *)arg,
530 sizeof(rdwr_arg)))
531 return -EFAULT;
532
533 if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
534 return -EINVAL;
535
536 rdwr_pa = kmalloc_array(rdwr_arg.nmsgs, sizeof(struct i2c_msg),
537 GFP_KERNEL);
538 if (!rdwr_pa)
539 return -ENOMEM;
540
541 p = compat_ptr(rdwr_arg.msgs);
542 for (i = 0; i < rdwr_arg.nmsgs; i++) {
543 struct i2c_msg32 umsg;
544 if (copy_from_user(&umsg, p + i, sizeof(umsg))) {
545 kfree(rdwr_pa);
546 return -EFAULT;
547 }
548 rdwr_pa[i] = (struct i2c_msg) {
549 .addr = umsg.addr,
550 .flags = umsg.flags,
551 .len = umsg.len,
552 .buf = compat_ptr(umsg.buf)
553 };
554 }
555
556 return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
557 }
558 case I2C_SMBUS: {
559 struct i2c_smbus_ioctl_data32 data32;
560 if (copy_from_user(&data32,
561 (void __user *) arg,
562 sizeof(data32)))
563 return -EFAULT;
564 return i2cdev_ioctl_smbus(client, data32.read_write,
565 data32.command,
566 data32.size,
567 compat_ptr(data32.data));
568 }
569 default:
570 return i2cdev_ioctl(file, cmd, arg);
571 }
572}
573#else
574#define compat_i2cdev_ioctl NULL
575#endif
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static int i2cdev_open(struct inode *inode, struct file *file)
578{
579 unsigned int minor = iminor(inode);
580 struct i2c_client *client;
581 struct i2c_adapter *adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
viresh kumar5136ed42016-07-05 19:57:06 -0700583 adap = i2c_get_adapter(minor);
Vincent Sanders9669f542009-12-06 17:06:26 +0100584 if (!adap)
585 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
David Brownell907135a2007-11-15 19:24:01 +0100587 /* This creates an anonymous i2c_client, which may later be
588 * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
589 *
590 * This client is ** NEVER REGISTERED ** with the driver model
591 * or I2C core code!! It just holds private copies of addressing
592 * information and maybe a PEC flag.
593 */
Jean Delvare22f76e72006-07-01 17:20:57 +0200594 client = kzalloc(sizeof(*client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!client) {
596 i2c_put_adapter(adap);
Vincent Sanders9669f542009-12-06 17:06:26 +0100597 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Jean Delvare22f76e72006-07-01 17:20:57 +0200599 snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 client->adapter = adap;
602 file->private_data = client;
603
Vincent Sanders9669f542009-12-06 17:06:26 +0100604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static int i2cdev_release(struct inode *inode, struct file *file)
608{
609 struct i2c_client *client = file->private_data;
610
611 i2c_put_adapter(client->adapter);
612 kfree(client);
613 file->private_data = NULL;
614
615 return 0;
616}
617
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800618static const struct file_operations i2cdev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 .owner = THIS_MODULE,
620 .llseek = no_llseek,
621 .read = i2cdev_read,
622 .write = i2cdev_write,
Alan Cox77e38bf2008-07-14 22:38:27 +0200623 .unlocked_ioctl = i2cdev_ioctl,
Al Viro7d5cb452017-09-20 01:02:27 -0400624 .compat_ioctl = compat_i2cdev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 .open = i2cdev_open,
626 .release = i2cdev_release,
627};
628
David Brownell907135a2007-11-15 19:24:01 +0100629/* ------------------------------------------------------------------------- */
630
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800631static struct class *i2c_dev_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Kevin Hao1413ef62019-10-11 23:00:14 +0800633static void i2cdev_dev_release(struct device *dev)
634{
635 struct i2c_dev *i2c_dev;
636
637 i2c_dev = container_of(dev, struct i2c_dev, dev);
638 kfree(i2c_dev);
639}
640
Jean Delvare9ea3e942011-03-20 14:50:52 +0100641static int i2cdev_attach_adapter(struct device *dev, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Jean Delvare9ea3e942011-03-20 14:50:52 +0100643 struct i2c_adapter *adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct i2c_dev *i2c_dev;
Jean Delvaredefcb462006-08-15 18:30:24 +0200645 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jean Delvare9ea3e942011-03-20 14:50:52 +0100647 if (dev->type != &i2c_adapter_type)
648 return 0;
649 adap = to_i2c_adapter(dev);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 i2c_dev = get_free_i2c_dev(adap);
652 if (IS_ERR(i2c_dev))
653 return PTR_ERR(i2c_dev);
654
Erico Nunesd6760b12016-05-03 15:45:43 -0300655 cdev_init(&i2c_dev->cdev, &i2cdev_fops);
656 i2c_dev->cdev.owner = THIS_MODULE;
Erico Nunesd6760b12016-05-03 15:45:43 -0300657
Kevin Hao1413ef62019-10-11 23:00:14 +0800658 device_initialize(&i2c_dev->dev);
659 i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr);
660 i2c_dev->dev.class = i2c_dev_class;
661 i2c_dev->dev.parent = &adap->dev;
662 i2c_dev->dev.release = i2cdev_dev_release;
663 dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
664
665 res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
666 if (res) {
667 put_i2c_dev(i2c_dev, false);
668 return res;
Jean Delvaredefcb462006-08-15 18:30:24 +0200669 }
Jean Delvareb32d20d2006-09-03 22:19:25 +0200670
671 pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
672 adap->name, adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Jean Delvare9ea3e942011-03-20 14:50:52 +0100676static int i2cdev_detach_adapter(struct device *dev, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Jean Delvare9ea3e942011-03-20 14:50:52 +0100678 struct i2c_adapter *adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct i2c_dev *i2c_dev;
680
Jean Delvare9ea3e942011-03-20 14:50:52 +0100681 if (dev->type != &i2c_adapter_type)
682 return 0;
683 adap = to_i2c_adapter(dev);
684
Jean Delvare9455e4c2006-07-01 17:16:57 +0200685 i2c_dev = i2c_dev_get_by_minor(adap->nr);
Jean Delvareb32d20d2006-09-03 22:19:25 +0200686 if (!i2c_dev) /* attach_adapter must have failed */
687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Kevin Hao1413ef62019-10-11 23:00:14 +0800689 put_i2c_dev(i2c_dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200691 pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693}
694
Shubhrajyoti Deff245c2011-11-23 11:33:07 +0100695static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
Jean Delvare9ea3e942011-03-20 14:50:52 +0100696 void *data)
697{
698 struct device *dev = data;
699
700 switch (action) {
701 case BUS_NOTIFY_ADD_DEVICE:
702 return i2cdev_attach_adapter(dev, NULL);
703 case BUS_NOTIFY_DEL_DEVICE:
704 return i2cdev_detach_adapter(dev, NULL);
705 }
706
707 return 0;
708}
709
710static struct notifier_block i2cdev_notifier = {
711 .notifier_call = i2cdev_notifier_call,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712};
713
David Brownell907135a2007-11-15 19:24:01 +0100714/* ------------------------------------------------------------------------- */
715
716/*
717 * module load/unload record keeping
718 */
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static int __init i2c_dev_init(void)
721{
722 int res;
723
724 printk(KERN_INFO "i2c /dev entries driver\n");
725
Erico Nunesd6760b12016-05-03 15:45:43 -0300726 res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (res)
728 goto out;
729
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800730 i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
Sven Wegenere74783e2008-09-24 13:39:21 +0200731 if (IS_ERR(i2c_dev_class)) {
732 res = PTR_ERR(i2c_dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 goto out_unreg_chrdev;
Sven Wegenere74783e2008-09-24 13:39:21 +0200734 }
Guenter Roeck45f176a2013-09-26 19:36:11 -0700735 i2c_dev_class->dev_groups = i2c_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jean Delvare9ea3e942011-03-20 14:50:52 +0100737 /* Keep track of adapters which will be added or removed later */
738 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (res)
740 goto out_unreg_class;
741
Jean Delvare9ea3e942011-03-20 14:50:52 +0100742 /* Bind to already existing adapters right away */
743 i2c_for_each_dev(NULL, i2cdev_attach_adapter);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return 0;
746
747out_unreg_class:
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800748 class_destroy(i2c_dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749out_unreg_chrdev:
Erico Nunesd6760b12016-05-03 15:45:43 -0300750 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751out:
752 printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
753 return res;
754}
755
756static void __exit i2c_dev_exit(void)
757{
Jean Delvare9ea3e942011-03-20 14:50:52 +0100758 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
759 i2c_for_each_dev(NULL, i2cdev_detach_adapter);
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800760 class_destroy(i2c_dev_class);
Erico Nunesd6760b12016-05-03 15:45:43 -0300761 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
765 "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
766MODULE_DESCRIPTION("I2C /dev entries driver");
767MODULE_LICENSE("GPL");
768
769module_init(i2c_dev_init);
770module_exit(i2c_dev_exit);