i2c: dev: Define pr_fmt() and drop duplication substrings
[linux-block.git] / drivers / i2c / i2c-dev.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
438d6c2c 3 i2c-dev.c - i2c-bus driver, char device interface
1da177e4
LT
4
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
1da177e4
LT
9*/
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
295e0e7b
AS
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
d6760b14 19#include <linux/cdev.h>
f01adfab 20#include <linux/compat.h>
9ea3e941 21#include <linux/device.h>
1da177e4 22#include <linux/fs.h>
1da177e4 23#include <linux/i2c-dev.h>
a8766073
WS
24#include <linux/i2c.h>
25#include <linux/init.h>
cd97f39b 26#include <linux/jiffies.h>
a8766073
WS
27#include <linux/kernel.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/notifier.h>
31#include <linux/slab.h>
ae5624fc 32#include <linux/uaccess.h>
1da177e4 33
907135aa
DB
34/*
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
9ea3e941 40 * maintained by the driver model, and is updated using bus notifications.
907135aa 41 */
1da177e4 42struct i2c_dev {
f3b3aadb 43 struct list_head list;
1da177e4 44 struct i2c_adapter *adap;
1413ef63 45 struct device dev;
d6760b14 46 struct cdev cdev;
1da177e4 47};
1da177e4 48
8a6d508a 49#define I2C_MINORS (MINORMASK + 1)
f3b3aadb
JD
50static LIST_HEAD(i2c_dev_list);
51static DEFINE_SPINLOCK(i2c_dev_list_lock);
1da177e4
LT
52
53static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
54{
55 struct i2c_dev *i2c_dev;
56
f3b3aadb
JD
57 spin_lock(&i2c_dev_list_lock);
58 list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
59 if (i2c_dev->adap->nr == index)
60 goto found;
61 }
62 i2c_dev = NULL;
63found:
64 spin_unlock(&i2c_dev_list_lock);
1da177e4
LT
65 return i2c_dev;
66}
67
1da177e4
LT
68static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
69{
70 struct i2c_dev *i2c_dev;
71
f3b3aadb 72 if (adap->nr >= I2C_MINORS) {
295e0e7b 73 pr_err("Out of device minors (%d)\n", adap->nr);
f3b3aadb
JD
74 return ERR_PTR(-ENODEV);
75 }
76
5263ebb5 77 i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
1da177e4
LT
78 if (!i2c_dev)
79 return ERR_PTR(-ENOMEM);
9455e4c9 80 i2c_dev->adap = adap;
f3b3aadb
JD
81
82 spin_lock(&i2c_dev_list_lock);
83 list_add_tail(&i2c_dev->list, &i2c_dev_list);
84 spin_unlock(&i2c_dev_list_lock);
1da177e4 85 return i2c_dev;
1da177e4
LT
86}
87
1413ef63 88static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev)
1da177e4 89{
f3b3aadb
JD
90 spin_lock(&i2c_dev_list_lock);
91 list_del(&i2c_dev->list);
92 spin_unlock(&i2c_dev_list_lock);
1413ef63
KH
93 if (del_cdev)
94 cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev);
95 put_device(&i2c_dev->dev);
1da177e4
LT
96}
97
45f176ae
GR
98static ssize_t name_show(struct device *dev,
99 struct device_attribute *attr, char *buf)
1da177e4 100{
ac11d060 101 struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
79472132
GKH
102
103 if (!i2c_dev)
104 return -ENODEV;
1da177e4
LT
105 return sprintf(buf, "%s\n", i2c_dev->adap->name);
106}
45f176ae
GR
107static DEVICE_ATTR_RO(name);
108
109static struct attribute *i2c_attrs[] = {
110 &dev_attr_name.attr,
111 NULL,
112};
113ATTRIBUTE_GROUPS(i2c);
1da177e4 114
907135aa
DB
115/* ------------------------------------------------------------------------- */
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
ae5624fc
FH
134static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
135 loff_t *offset)
1da177e4
LT
136{
137 char *tmp;
138 int ret;
139
0be16c30 140 struct i2c_client *client = file->private_data;
1da177e4
LT
141
142 if (count > 8192)
143 count = 8192;
144
ae5624fc
FH
145 tmp = kmalloc(count, GFP_KERNEL);
146 if (tmp == NULL)
1da177e4
LT
147 return -ENOMEM;
148
295e0e7b 149 pr_debug("i2c-%d reading %zu bytes.\n", iminor(file_inode(file)), count);
1da177e4 150
ae5624fc 151 ret = i2c_master_recv(client, tmp, count);
1da177e4 152 if (ret >= 0)
ae5624fc 153 ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;
1da177e4
LT
154 kfree(tmp);
155 return ret;
156}
157
ae5624fc
FH
158static ssize_t i2cdev_write(struct file *file, const char __user *buf,
159 size_t count, loff_t *offset)
1da177e4
LT
160{
161 int ret;
162 char *tmp;
0be16c30 163 struct i2c_client *client = file->private_data;
1da177e4
LT
164
165 if (count > 8192)
166 count = 8192;
167
f1c2e33c
JL
168 tmp = memdup_user(buf, count);
169 if (IS_ERR(tmp))
170 return PTR_ERR(tmp);
1da177e4 171
295e0e7b 172 pr_debug("i2c-%d writing %zu bytes.\n", iminor(file_inode(file)), count);
1da177e4 173
ae5624fc 174 ret = i2c_master_send(client, tmp, count);
1da177e4
LT
175 kfree(tmp);
176 return ret;
177}
178
9b766b81
DB
179static int i2cdev_check(struct device *dev, void *addrp)
180{
181 struct i2c_client *client = i2c_verify_client(dev);
182
183 if (!client || client->addr != *(unsigned int *)addrp)
184 return 0;
185
186 return dev->driver ? -EBUSY : 0;
187}
188
0826374b
ML
189/* walk up mux tree */
190static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
191{
97cc4d49 192 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
0826374b
ML
193 int result;
194
195 result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
97cc4d49
JD
196 if (!result && parent)
197 result = i2cdev_check_mux_parents(parent, addr);
0826374b
ML
198
199 return result;
200}
201
202/* recurse down mux tree */
203static int i2cdev_check_mux_children(struct device *dev, void *addrp)
204{
205 int result;
206
207 if (dev->type == &i2c_adapter_type)
208 result = device_for_each_child(dev, addrp,
209 i2cdev_check_mux_children);
210 else
211 result = i2cdev_check(dev, addrp);
212
213 return result;
214}
215
bd4217d8
JD
216/* This address checking function differs from the one in i2c-core
217 in that it considers an address with a registered device, but no
9b766b81 218 driver bound to it, as NOT busy. */
bd4217d8
JD
219static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
220{
97cc4d49 221 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
0826374b
ML
222 int result = 0;
223
97cc4d49
JD
224 if (parent)
225 result = i2cdev_check_mux_parents(parent, addr);
0826374b
ML
226
227 if (!result)
228 result = device_for_each_child(&adapter->dev, &addr,
229 i2cdev_check_mux_children);
230
231 return result;
bd4217d8
JD
232}
233
c57d3e7a 234static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
7d5cb456 235 unsigned nmsgs, struct i2c_msg *msgs)
1da177e4 236{
1da177e4 237 u8 __user **data_ptrs;
dba7997a
JD
238 int i, res;
239
6da2ec56 240 data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
dba7997a 241 if (data_ptrs == NULL) {
7d5cb456 242 kfree(msgs);
dba7997a
JD
243 return -ENOMEM;
244 }
245
246 res = 0;
7d5cb456 247 for (i = 0; i < nmsgs; i++) {
838bfa60 248 /* Limit the size of the message to a sane amount */
7d5cb456 249 if (msgs[i].len > 8192) {
dba7997a
JD
250 res = -EINVAL;
251 break;
252 }
838bfa60 253
7d5cb456
AV
254 data_ptrs[i] = (u8 __user *)msgs[i].buf;
255 msgs[i].buf = memdup_user(data_ptrs[i], msgs[i].len);
256 if (IS_ERR(msgs[i].buf)) {
257 res = PTR_ERR(msgs[i].buf);
dba7997a
JD
258 break;
259 }
978336d4
WS
260 /* memdup_user allocates with GFP_KERNEL, so DMA is ok */
261 msgs[i].flags |= I2C_M_DMA_SAFE;
838bfa60
JD
262
263 /*
264 * If the message length is received from the slave (similar
265 * to SMBus block read), we must ensure that the buffer will
266 * be large enough to cope with a message length of
267 * I2C_SMBUS_BLOCK_MAX as this is the maximum underlying bus
268 * drivers allow. The first byte in the buffer must be
269 * pre-filled with the number of extra bytes, which must be
270 * at least one to hold the message length, but can be
271 * greater (for example to account for a checksum byte at
272 * the end of the message.)
273 */
7d5cb456
AV
274 if (msgs[i].flags & I2C_M_RECV_LEN) {
275 if (!(msgs[i].flags & I2C_M_RD) ||
23a27722 276 msgs[i].len < 1 || msgs[i].buf[0] < 1 ||
7d5cb456 277 msgs[i].len < msgs[i].buf[0] +
838bfa60 278 I2C_SMBUS_BLOCK_MAX) {
a0692f0e 279 i++;
838bfa60
JD
280 res = -EINVAL;
281 break;
282 }
283
7d5cb456 284 msgs[i].len = msgs[i].buf[0];
838bfa60 285 }
dba7997a
JD
286 }
287 if (res < 0) {
288 int j;
289 for (j = 0; j < i; ++j)
7d5cb456 290 kfree(msgs[j].buf);
dba7997a 291 kfree(data_ptrs);
7d5cb456 292 kfree(msgs);
dba7997a
JD
293 return res;
294 }
295
7d5cb456 296 res = i2c_transfer(client->adapter, msgs, nmsgs);
dba7997a 297 while (i-- > 0) {
7d5cb456
AV
298 if (res >= 0 && (msgs[i].flags & I2C_M_RD)) {
299 if (copy_to_user(data_ptrs[i], msgs[i].buf,
300 msgs[i].len))
dba7997a
JD
301 res = -EFAULT;
302 }
7d5cb456 303 kfree(msgs[i].buf);
dba7997a
JD
304 }
305 kfree(data_ptrs);
7d5cb456 306 kfree(msgs);
dba7997a
JD
307 return res;
308}
309
310static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
7d5cb456
AV
311 u8 read_write, u8 command, u32 size,
312 union i2c_smbus_data __user *data)
dba7997a 313{
30f939fe 314 union i2c_smbus_data temp = {};
dba7997a
JD
315 int datasize, res;
316
7d5cb456
AV
317 if ((size != I2C_SMBUS_BYTE) &&
318 (size != I2C_SMBUS_QUICK) &&
319 (size != I2C_SMBUS_BYTE_DATA) &&
320 (size != I2C_SMBUS_WORD_DATA) &&
321 (size != I2C_SMBUS_PROC_CALL) &&
322 (size != I2C_SMBUS_BLOCK_DATA) &&
323 (size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
324 (size != I2C_SMBUS_I2C_BLOCK_DATA) &&
325 (size != I2C_SMBUS_BLOCK_PROC_CALL)) {
dba7997a
JD
326 dev_dbg(&client->adapter->dev,
327 "size out of range (%x) in ioctl I2C_SMBUS.\n",
7d5cb456 328 size);
dba7997a
JD
329 return -EINVAL;
330 }
331 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
332 so the check is valid if size==I2C_SMBUS_QUICK too. */
7d5cb456
AV
333 if ((read_write != I2C_SMBUS_READ) &&
334 (read_write != I2C_SMBUS_WRITE)) {
dba7997a
JD
335 dev_dbg(&client->adapter->dev,
336 "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
7d5cb456 337 read_write);
dba7997a
JD
338 return -EINVAL;
339 }
340
341 /* Note that command values are always valid! */
342
7d5cb456
AV
343 if ((size == I2C_SMBUS_QUICK) ||
344 ((size == I2C_SMBUS_BYTE) &&
345 (read_write == I2C_SMBUS_WRITE)))
dba7997a
JD
346 /* These are special: we do not use data */
347 return i2c_smbus_xfer(client->adapter, client->addr,
7d5cb456
AV
348 client->flags, read_write,
349 command, size, NULL);
dba7997a 350
7d5cb456 351 if (data == NULL) {
dba7997a
JD
352 dev_dbg(&client->adapter->dev,
353 "data is NULL pointer in ioctl I2C_SMBUS.\n");
354 return -EINVAL;
355 }
356
7d5cb456
AV
357 if ((size == I2C_SMBUS_BYTE_DATA) ||
358 (size == I2C_SMBUS_BYTE))
359 datasize = sizeof(data->byte);
360 else if ((size == I2C_SMBUS_WORD_DATA) ||
361 (size == I2C_SMBUS_PROC_CALL))
362 datasize = sizeof(data->word);
dba7997a 363 else /* size == smbus block, i2c block, or block proc. call */
7d5cb456 364 datasize = sizeof(data->block);
dba7997a 365
7d5cb456
AV
366 if ((size == I2C_SMBUS_PROC_CALL) ||
367 (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
368 (size == I2C_SMBUS_I2C_BLOCK_DATA) ||
369 (read_write == I2C_SMBUS_WRITE)) {
370 if (copy_from_user(&temp, data, datasize))
dba7997a
JD
371 return -EFAULT;
372 }
7d5cb456 373 if (size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
dba7997a
JD
374 /* Convert old I2C block commands to the new
375 convention. This preserves binary compatibility. */
7d5cb456
AV
376 size = I2C_SMBUS_I2C_BLOCK_DATA;
377 if (read_write == I2C_SMBUS_READ)
dba7997a
JD
378 temp.block[0] = I2C_SMBUS_BLOCK_MAX;
379 }
380 res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
7d5cb456
AV
381 read_write, command, size, &temp);
382 if (!res && ((size == I2C_SMBUS_PROC_CALL) ||
383 (size == I2C_SMBUS_BLOCK_PROC_CALL) ||
384 (read_write == I2C_SMBUS_READ))) {
385 if (copy_to_user(data, &temp, datasize))
dba7997a
JD
386 return -EFAULT;
387 }
388 return res;
389}
390
77e38bff 391static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dba7997a 392{
0be16c30 393 struct i2c_client *client = file->private_data;
1da177e4
LT
394 unsigned long funcs;
395
e8aafcb2
JD
396 dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
397 cmd, arg);
1da177e4 398
ae5624fc 399 switch (cmd) {
1da177e4
LT
400 case I2C_SLAVE:
401 case I2C_SLAVE_FORCE:
438d6c2c 402 if ((arg > 0x3ff) ||
1da177e4
LT
403 (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
404 return -EINVAL;
bd4217d8 405 if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
1da177e4 406 return -EBUSY;
bd4217d8 407 /* REVISIT: address could become busy later */
1da177e4
LT
408 client->addr = arg;
409 return 0;
410 case I2C_TENBIT:
411 if (arg)
412 client->flags |= I2C_M_TEN;
413 else
414 client->flags &= ~I2C_M_TEN;
415 return 0;
416 case I2C_PEC:
9e685c84
JD
417 /*
418 * Setting the PEC flag here won't affect kernel drivers,
419 * which will be using the i2c_client node registered with
420 * the driver model core. Likewise, when that client has
421 * the PEC flag already set, the i2c-dev driver won't see
422 * (or use) this setting.
423 */
1da177e4
LT
424 if (arg)
425 client->flags |= I2C_CLIENT_PEC;
426 else
427 client->flags &= ~I2C_CLIENT_PEC;
428 return 0;
429 case I2C_FUNCS:
430 funcs = i2c_get_functionality(client->adapter);
2c003e8e 431 return put_user(funcs, (unsigned long __user *)arg);
1da177e4 432
7d5cb456
AV
433 case I2C_RDWR: {
434 struct i2c_rdwr_ioctl_data rdwr_arg;
435 struct i2c_msg *rdwr_pa;
436
437 if (copy_from_user(&rdwr_arg,
438 (struct i2c_rdwr_ioctl_data __user *)arg,
439 sizeof(rdwr_arg)))
440 return -EFAULT;
441
71581562
WS
442 if (!rdwr_arg.msgs || rdwr_arg.nmsgs == 0)
443 return -EINVAL;
444
445 /*
446 * Put an arbitrary limit on the number of messages that can
447 * be sent at once
448 */
7d5cb456
AV
449 if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
450 return -EINVAL;
1da177e4 451
7d5cb456
AV
452 rdwr_pa = memdup_user(rdwr_arg.msgs,
453 rdwr_arg.nmsgs * sizeof(struct i2c_msg));
454 if (IS_ERR(rdwr_pa))
455 return PTR_ERR(rdwr_pa);
456
457 return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
458 }
1da177e4 459
7d5cb456
AV
460 case I2C_SMBUS: {
461 struct i2c_smbus_ioctl_data data_arg;
462 if (copy_from_user(&data_arg,
463 (struct i2c_smbus_ioctl_data __user *) arg,
464 sizeof(struct i2c_smbus_ioctl_data)))
465 return -EFAULT;
466 return i2cdev_ioctl_smbus(client, data_arg.read_write,
467 data_arg.command,
468 data_arg.size,
469 data_arg.data);
470 }
53be7959 471 case I2C_RETRIES:
6ebec961
YZ
472 if (arg > INT_MAX)
473 return -EINVAL;
474
53be7959
DB
475 client->adapter->retries = arg;
476 break;
477 case I2C_TIMEOUT:
6ebec961
YZ
478 if (arg > INT_MAX)
479 return -EINVAL;
480
cd97f39b
JD
481 /* For historical reasons, user-space sets the timeout
482 * value in units of 10 ms.
483 */
484 client->adapter->timeout = msecs_to_jiffies(arg * 10);
53be7959 485 break;
1da177e4 486 default:
53be7959
DB
487 /* NOTE: returning a fault code here could cause trouble
488 * in buggy userspace code. Some old kernel bugs returned
489 * zero in this case, and userspace code might accidentally
490 * have depended on that bug.
491 */
492 return -ENOTTY;
1da177e4
LT
493 }
494 return 0;
495}
496
7d5cb456
AV
497#ifdef CONFIG_COMPAT
498
499struct i2c_smbus_ioctl_data32 {
500 u8 read_write;
501 u8 command;
502 u32 size;
503 compat_caddr_t data; /* union i2c_smbus_data *data */
504};
505
506struct i2c_msg32 {
507 u16 addr;
508 u16 flags;
509 u16 len;
510 compat_caddr_t buf;
511};
512
513struct i2c_rdwr_ioctl_data32 {
514 compat_caddr_t msgs; /* struct i2c_msg __user *msgs */
515 u32 nmsgs;
516};
517
518static long compat_i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
519{
520 struct i2c_client *client = file->private_data;
521 unsigned long funcs;
522 switch (cmd) {
523 case I2C_FUNCS:
524 funcs = i2c_get_functionality(client->adapter);
525 return put_user(funcs, (compat_ulong_t __user *)arg);
526 case I2C_RDWR: {
527 struct i2c_rdwr_ioctl_data32 rdwr_arg;
3265a7e6 528 struct i2c_msg32 __user *p;
7d5cb456
AV
529 struct i2c_msg *rdwr_pa;
530 int i;
531
532 if (copy_from_user(&rdwr_arg,
533 (struct i2c_rdwr_ioctl_data32 __user *)arg,
534 sizeof(rdwr_arg)))
535 return -EFAULT;
536
537 if (rdwr_arg.nmsgs > I2C_RDWR_IOCTL_MAX_MSGS)
538 return -EINVAL;
539
540 rdwr_pa = kmalloc_array(rdwr_arg.nmsgs, sizeof(struct i2c_msg),
541 GFP_KERNEL);
542 if (!rdwr_pa)
543 return -ENOMEM;
544
545 p = compat_ptr(rdwr_arg.msgs);
546 for (i = 0; i < rdwr_arg.nmsgs; i++) {
547 struct i2c_msg32 umsg;
548 if (copy_from_user(&umsg, p + i, sizeof(umsg))) {
549 kfree(rdwr_pa);
550 return -EFAULT;
551 }
552 rdwr_pa[i] = (struct i2c_msg) {
553 .addr = umsg.addr,
554 .flags = umsg.flags,
555 .len = umsg.len,
556 .buf = compat_ptr(umsg.buf)
557 };
558 }
559
560 return i2cdev_ioctl_rdwr(client, rdwr_arg.nmsgs, rdwr_pa);
561 }
562 case I2C_SMBUS: {
563 struct i2c_smbus_ioctl_data32 data32;
564 if (copy_from_user(&data32,
565 (void __user *) arg,
566 sizeof(data32)))
567 return -EFAULT;
568 return i2cdev_ioctl_smbus(client, data32.read_write,
569 data32.command,
570 data32.size,
571 compat_ptr(data32.data));
572 }
573 default:
574 return i2cdev_ioctl(file, cmd, arg);
575 }
576}
577#else
578#define compat_i2cdev_ioctl NULL
579#endif
580
1da177e4
LT
581static int i2cdev_open(struct inode *inode, struct file *file)
582{
583 unsigned int minor = iminor(inode);
584 struct i2c_client *client;
585 struct i2c_adapter *adap;
1da177e4 586
5136ed4f 587 adap = i2c_get_adapter(minor);
9669f541
VS
588 if (!adap)
589 return -ENODEV;
1da177e4 590
907135aa
DB
591 /* This creates an anonymous i2c_client, which may later be
592 * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
593 *
594 * This client is ** NEVER REGISTERED ** with the driver model
595 * or I2C core code!! It just holds private copies of addressing
596 * information and maybe a PEC flag.
597 */
22f76e74 598 client = kzalloc(sizeof(*client), GFP_KERNEL);
1da177e4
LT
599 if (!client) {
600 i2c_put_adapter(adap);
9669f541 601 return -ENOMEM;
1da177e4 602 }
22f76e74 603 snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
1da177e4 604
1da177e4
LT
605 client->adapter = adap;
606 file->private_data = client;
607
9669f541 608 return 0;
1da177e4
LT
609}
610
611static int i2cdev_release(struct inode *inode, struct file *file)
612{
613 struct i2c_client *client = file->private_data;
614
615 i2c_put_adapter(client->adapter);
616 kfree(client);
617 file->private_data = NULL;
618
619 return 0;
620}
621
2b8693c0 622static const struct file_operations i2cdev_fops = {
1da177e4
LT
623 .owner = THIS_MODULE,
624 .llseek = no_llseek,
625 .read = i2cdev_read,
626 .write = i2cdev_write,
77e38bff 627 .unlocked_ioctl = i2cdev_ioctl,
7d5cb456 628 .compat_ioctl = compat_i2cdev_ioctl,
1da177e4
LT
629 .open = i2cdev_open,
630 .release = i2cdev_release,
631};
632
907135aa
DB
633/* ------------------------------------------------------------------------- */
634
79472132 635static struct class *i2c_dev_class;
1da177e4 636
1413ef63
KH
637static void i2cdev_dev_release(struct device *dev)
638{
639 struct i2c_dev *i2c_dev;
640
641 i2c_dev = container_of(dev, struct i2c_dev, dev);
642 kfree(i2c_dev);
643}
644
9ea3e941 645static int i2cdev_attach_adapter(struct device *dev, void *dummy)
1da177e4 646{
9ea3e941 647 struct i2c_adapter *adap;
1da177e4 648 struct i2c_dev *i2c_dev;
defcb46e 649 int res;
1da177e4 650
9ea3e941
JD
651 if (dev->type != &i2c_adapter_type)
652 return 0;
653 adap = to_i2c_adapter(dev);
654
1da177e4
LT
655 i2c_dev = get_free_i2c_dev(adap);
656 if (IS_ERR(i2c_dev))
657 return PTR_ERR(i2c_dev);
658
d6760b14
EN
659 cdev_init(&i2c_dev->cdev, &i2cdev_fops);
660 i2c_dev->cdev.owner = THIS_MODULE;
1413ef63
KH
661
662 device_initialize(&i2c_dev->dev);
663 i2c_dev->dev.devt = MKDEV(I2C_MAJOR, adap->nr);
664 i2c_dev->dev.class = i2c_dev_class;
665 i2c_dev->dev.parent = &adap->dev;
666 i2c_dev->dev.release = i2cdev_dev_release;
667 dev_set_name(&i2c_dev->dev, "i2c-%d", adap->nr);
668
669 res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
670 if (res) {
671 put_i2c_dev(i2c_dev, false);
672 return res;
defcb46e 673 }
b32d20dc 674
295e0e7b 675 pr_debug("adapter [%s] registered as minor %d\n", adap->name, adap->nr);
1da177e4 676 return 0;
1da177e4
LT
677}
678
9ea3e941 679static int i2cdev_detach_adapter(struct device *dev, void *dummy)
1da177e4 680{
9ea3e941 681 struct i2c_adapter *adap;
1da177e4
LT
682 struct i2c_dev *i2c_dev;
683
9ea3e941
JD
684 if (dev->type != &i2c_adapter_type)
685 return 0;
686 adap = to_i2c_adapter(dev);
687
9455e4c9 688 i2c_dev = i2c_dev_get_by_minor(adap->nr);
b32d20dc
JD
689 if (!i2c_dev) /* attach_adapter must have failed */
690 return 0;
1da177e4 691
1413ef63 692 put_i2c_dev(i2c_dev, true);
1da177e4 693
295e0e7b 694 pr_debug("adapter [%s] unregistered\n", adap->name);
1da177e4
LT
695 return 0;
696}
697
eff245c8 698static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
9ea3e941
JD
699 void *data)
700{
701 struct device *dev = data;
702
703 switch (action) {
704 case BUS_NOTIFY_ADD_DEVICE:
705 return i2cdev_attach_adapter(dev, NULL);
706 case BUS_NOTIFY_DEL_DEVICE:
707 return i2cdev_detach_adapter(dev, NULL);
708 }
709
710 return 0;
711}
712
713static struct notifier_block i2cdev_notifier = {
714 .notifier_call = i2cdev_notifier_call,
1da177e4
LT
715};
716
907135aa
DB
717/* ------------------------------------------------------------------------- */
718
719/*
720 * module load/unload record keeping
721 */
722
1da177e4
LT
723static int __init i2c_dev_init(void)
724{
725 int res;
726
295e0e7b 727 pr_info("i2c /dev entries driver\n");
1da177e4 728
d6760b14 729 res = register_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS, "i2c");
1da177e4
LT
730 if (res)
731 goto out;
732
79472132 733 i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
e74783ec
SW
734 if (IS_ERR(i2c_dev_class)) {
735 res = PTR_ERR(i2c_dev_class);
1da177e4 736 goto out_unreg_chrdev;
e74783ec 737 }
45f176ae 738 i2c_dev_class->dev_groups = i2c_groups;
1da177e4 739
9ea3e941
JD
740 /* Keep track of adapters which will be added or removed later */
741 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
1da177e4
LT
742 if (res)
743 goto out_unreg_class;
744
9ea3e941
JD
745 /* Bind to already existing adapters right away */
746 i2c_for_each_dev(NULL, i2cdev_attach_adapter);
747
1da177e4
LT
748 return 0;
749
750out_unreg_class:
79472132 751 class_destroy(i2c_dev_class);
1da177e4 752out_unreg_chrdev:
d6760b14 753 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
1da177e4 754out:
295e0e7b 755 pr_err("Driver Initialisation failed\n");
1da177e4
LT
756 return res;
757}
758
759static void __exit i2c_dev_exit(void)
760{
9ea3e941
JD
761 bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
762 i2c_for_each_dev(NULL, i2cdev_detach_adapter);
79472132 763 class_destroy(i2c_dev_class);
d6760b14 764 unregister_chrdev_region(MKDEV(I2C_MAJOR, 0), I2C_MINORS);
1da177e4
LT
765}
766
f80531c8
JN
767MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
768MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1da177e4
LT
769MODULE_DESCRIPTION("I2C /dev entries driver");
770MODULE_LICENSE("GPL");
771
772module_init(i2c_dev_init);
773module_exit(i2c_dev_exit);