2 i2c-dev.c - i2c-bus driver, char device interface
4 Copyright (C) 1995-97 Simon G. Vogl
5 Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
6 Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
24 But I have used so much of his original code and ideas that it seems
25 only fair to recognize him as co-author -- Frodo */
27 /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
29 #include <linux/kernel.h>
30 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/smp_lock.h>
34 #include <linux/init.h>
35 #include <linux/i2c.h>
36 #include <linux/i2c-dev.h>
37 #include <linux/platform_device.h>
38 #include <asm/uaccess.h>
40 static struct i2c_client i2cdev_client_template
;
44 struct i2c_adapter
*adap
;
45 struct class_device class_dev
;
46 struct completion released
; /* FIXME, we need a class_device_unregister() */
48 #define to_i2c_dev(d) container_of(d, struct i2c_dev, class_dev)
50 #define I2C_MINORS 256
51 static struct i2c_dev
*i2c_dev_array
[I2C_MINORS
];
52 static DEFINE_SPINLOCK(i2c_dev_array_lock
);
54 static struct i2c_dev
*i2c_dev_get_by_minor(unsigned index
)
56 struct i2c_dev
*i2c_dev
;
58 spin_lock(&i2c_dev_array_lock
);
59 i2c_dev
= i2c_dev_array
[index
];
60 spin_unlock(&i2c_dev_array_lock
);
64 static struct i2c_dev
*i2c_dev_get_by_adapter(struct i2c_adapter
*adap
)
66 struct i2c_dev
*i2c_dev
= NULL
;
68 spin_lock(&i2c_dev_array_lock
);
69 if ((i2c_dev_array
[adap
->nr
]) &&
70 (i2c_dev_array
[adap
->nr
]->adap
== adap
))
71 i2c_dev
= i2c_dev_array
[adap
->nr
];
72 spin_unlock(&i2c_dev_array_lock
);
76 static struct i2c_dev
*get_free_i2c_dev(struct i2c_adapter
*adap
)
78 struct i2c_dev
*i2c_dev
;
80 i2c_dev
= kzalloc(sizeof(*i2c_dev
), GFP_KERNEL
);
82 return ERR_PTR(-ENOMEM
);
84 spin_lock(&i2c_dev_array_lock
);
85 if (i2c_dev_array
[adap
->nr
]) {
86 spin_unlock(&i2c_dev_array_lock
);
87 dev_err(&adap
->dev
, "i2c-dev already has a device assigned to this adapter\n");
90 i2c_dev
->minor
= adap
->nr
;
91 i2c_dev_array
[adap
->nr
] = i2c_dev
;
92 spin_unlock(&i2c_dev_array_lock
);
96 return ERR_PTR(-ENODEV
);
99 static void return_i2c_dev(struct i2c_dev
*i2c_dev
)
101 spin_lock(&i2c_dev_array_lock
);
102 i2c_dev_array
[i2c_dev
->minor
] = NULL
;
103 spin_unlock(&i2c_dev_array_lock
);
106 static ssize_t
show_adapter_name(struct class_device
*class_dev
, char *buf
)
108 struct i2c_dev
*i2c_dev
= to_i2c_dev(class_dev
);
109 return sprintf(buf
, "%s\n", i2c_dev
->adap
->name
);
111 static CLASS_DEVICE_ATTR(name
, S_IRUGO
, show_adapter_name
, NULL
);
113 static ssize_t
i2cdev_read (struct file
*file
, char __user
*buf
, size_t count
,
119 struct i2c_client
*client
= (struct i2c_client
*)file
->private_data
;
124 tmp
= kmalloc(count
,GFP_KERNEL
);
128 pr_debug("i2c-dev: i2c-%d reading %zd bytes.\n",
129 iminor(file
->f_dentry
->d_inode
), count
);
131 ret
= i2c_master_recv(client
,tmp
,count
);
133 ret
= copy_to_user(buf
,tmp
,count
)?-EFAULT
:ret
;
138 static ssize_t
i2cdev_write (struct file
*file
, const char __user
*buf
, size_t count
,
143 struct i2c_client
*client
= (struct i2c_client
*)file
->private_data
;
148 tmp
= kmalloc(count
,GFP_KERNEL
);
151 if (copy_from_user(tmp
,buf
,count
)) {
156 pr_debug("i2c-dev: i2c-%d writing %zd bytes.\n",
157 iminor(file
->f_dentry
->d_inode
), count
);
159 ret
= i2c_master_send(client
,tmp
,count
);
164 static int i2cdev_ioctl(struct inode
*inode
, struct file
*file
,
165 unsigned int cmd
, unsigned long arg
)
167 struct i2c_client
*client
= (struct i2c_client
*)file
->private_data
;
168 struct i2c_rdwr_ioctl_data rdwr_arg
;
169 struct i2c_smbus_ioctl_data data_arg
;
170 union i2c_smbus_data temp
;
171 struct i2c_msg
*rdwr_pa
;
172 u8 __user
**data_ptrs
;
176 dev_dbg(&client
->adapter
->dev
, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
181 case I2C_SLAVE_FORCE
:
183 (((client
->flags
& I2C_M_TEN
) == 0) && arg
> 0x7f))
185 if ((cmd
== I2C_SLAVE
) && i2c_check_addr(client
->adapter
,arg
))
191 client
->flags
|= I2C_M_TEN
;
193 client
->flags
&= ~I2C_M_TEN
;
197 client
->flags
|= I2C_CLIENT_PEC
;
199 client
->flags
&= ~I2C_CLIENT_PEC
;
202 funcs
= i2c_get_functionality(client
->adapter
);
203 return (copy_to_user((unsigned long __user
*)arg
, &funcs
,
204 sizeof(unsigned long)))?-EFAULT
:0;
207 if (copy_from_user(&rdwr_arg
,
208 (struct i2c_rdwr_ioctl_data __user
*)arg
,
212 /* Put an arbitrary limit on the number of messages that can
214 if (rdwr_arg
.nmsgs
> I2C_RDRW_IOCTL_MAX_MSGS
)
217 rdwr_pa
= (struct i2c_msg
*)
218 kmalloc(rdwr_arg
.nmsgs
* sizeof(struct i2c_msg
),
221 if (rdwr_pa
== NULL
) return -ENOMEM
;
223 if (copy_from_user(rdwr_pa
, rdwr_arg
.msgs
,
224 rdwr_arg
.nmsgs
* sizeof(struct i2c_msg
))) {
229 data_ptrs
= kmalloc(rdwr_arg
.nmsgs
* sizeof(u8 __user
*), GFP_KERNEL
);
230 if (data_ptrs
== NULL
) {
236 for( i
=0; i
<rdwr_arg
.nmsgs
; i
++ ) {
237 /* Limit the size of the message to a sane amount */
238 if (rdwr_pa
[i
].len
> 8192) {
242 data_ptrs
[i
] = (u8 __user
*)rdwr_pa
[i
].buf
;
243 rdwr_pa
[i
].buf
= kmalloc(rdwr_pa
[i
].len
, GFP_KERNEL
);
244 if(rdwr_pa
[i
].buf
== NULL
) {
248 if(copy_from_user(rdwr_pa
[i
].buf
,
251 ++i
; /* Needs to be kfreed too */
258 for (j
= 0; j
< i
; ++j
)
259 kfree(rdwr_pa
[j
].buf
);
265 res
= i2c_transfer(client
->adapter
,
269 if( res
>=0 && (rdwr_pa
[i
].flags
& I2C_M_RD
)) {
277 kfree(rdwr_pa
[i
].buf
);
284 if (copy_from_user(&data_arg
,
285 (struct i2c_smbus_ioctl_data __user
*) arg
,
286 sizeof(struct i2c_smbus_ioctl_data
)))
288 if ((data_arg
.size
!= I2C_SMBUS_BYTE
) &&
289 (data_arg
.size
!= I2C_SMBUS_QUICK
) &&
290 (data_arg
.size
!= I2C_SMBUS_BYTE_DATA
) &&
291 (data_arg
.size
!= I2C_SMBUS_WORD_DATA
) &&
292 (data_arg
.size
!= I2C_SMBUS_PROC_CALL
) &&
293 (data_arg
.size
!= I2C_SMBUS_BLOCK_DATA
) &&
294 (data_arg
.size
!= I2C_SMBUS_I2C_BLOCK_DATA
) &&
295 (data_arg
.size
!= I2C_SMBUS_BLOCK_PROC_CALL
)) {
296 dev_dbg(&client
->adapter
->dev
,
297 "size out of range (%x) in ioctl I2C_SMBUS.\n",
301 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
302 so the check is valid if size==I2C_SMBUS_QUICK too. */
303 if ((data_arg
.read_write
!= I2C_SMBUS_READ
) &&
304 (data_arg
.read_write
!= I2C_SMBUS_WRITE
)) {
305 dev_dbg(&client
->adapter
->dev
,
306 "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
307 data_arg
.read_write
);
311 /* Note that command values are always valid! */
313 if ((data_arg
.size
== I2C_SMBUS_QUICK
) ||
314 ((data_arg
.size
== I2C_SMBUS_BYTE
) &&
315 (data_arg
.read_write
== I2C_SMBUS_WRITE
)))
316 /* These are special: we do not use data */
317 return i2c_smbus_xfer(client
->adapter
, client
->addr
,
321 data_arg
.size
, NULL
);
323 if (data_arg
.data
== NULL
) {
324 dev_dbg(&client
->adapter
->dev
,
325 "data is NULL pointer in ioctl I2C_SMBUS.\n");
329 if ((data_arg
.size
== I2C_SMBUS_BYTE_DATA
) ||
330 (data_arg
.size
== I2C_SMBUS_BYTE
))
331 datasize
= sizeof(data_arg
.data
->byte
);
332 else if ((data_arg
.size
== I2C_SMBUS_WORD_DATA
) ||
333 (data_arg
.size
== I2C_SMBUS_PROC_CALL
))
334 datasize
= sizeof(data_arg
.data
->word
);
335 else /* size == smbus block, i2c block, or block proc. call */
336 datasize
= sizeof(data_arg
.data
->block
);
338 if ((data_arg
.size
== I2C_SMBUS_PROC_CALL
) ||
339 (data_arg
.size
== I2C_SMBUS_BLOCK_PROC_CALL
) ||
340 (data_arg
.read_write
== I2C_SMBUS_WRITE
)) {
341 if (copy_from_user(&temp
, data_arg
.data
, datasize
))
344 res
= i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
346 data_arg
.command
,data_arg
.size
,&temp
);
347 if (! res
&& ((data_arg
.size
== I2C_SMBUS_PROC_CALL
) ||
348 (data_arg
.size
== I2C_SMBUS_BLOCK_PROC_CALL
) ||
349 (data_arg
.read_write
== I2C_SMBUS_READ
))) {
350 if (copy_to_user(data_arg
.data
, &temp
, datasize
))
356 return i2c_control(client
,cmd
,arg
);
361 static int i2cdev_open(struct inode
*inode
, struct file
*file
)
363 unsigned int minor
= iminor(inode
);
364 struct i2c_client
*client
;
365 struct i2c_adapter
*adap
;
366 struct i2c_dev
*i2c_dev
;
368 i2c_dev
= i2c_dev_get_by_minor(minor
);
372 adap
= i2c_get_adapter(i2c_dev
->adap
->nr
);
376 client
= kmalloc(sizeof(*client
), GFP_KERNEL
);
378 i2c_put_adapter(adap
);
381 memcpy(client
, &i2cdev_client_template
, sizeof(*client
));
383 /* registered with adapter, passed as client to user */
384 client
->adapter
= adap
;
385 file
->private_data
= client
;
390 static int i2cdev_release(struct inode
*inode
, struct file
*file
)
392 struct i2c_client
*client
= file
->private_data
;
394 i2c_put_adapter(client
->adapter
);
396 file
->private_data
= NULL
;
401 static struct file_operations i2cdev_fops
= {
402 .owner
= THIS_MODULE
,
405 .write
= i2cdev_write
,
406 .ioctl
= i2cdev_ioctl
,
408 .release
= i2cdev_release
,
411 static void release_i2c_dev(struct class_device
*dev
)
413 struct i2c_dev
*i2c_dev
= to_i2c_dev(dev
);
414 complete(&i2c_dev
->released
);
417 static struct class i2c_dev_class
= {
419 .release
= &release_i2c_dev
,
422 static int i2cdev_attach_adapter(struct i2c_adapter
*adap
)
424 struct i2c_dev
*i2c_dev
;
427 i2c_dev
= get_free_i2c_dev(adap
);
429 return PTR_ERR(i2c_dev
);
431 pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
432 adap
->name
, i2c_dev
->minor
);
434 /* register this i2c device with the driver core */
435 i2c_dev
->adap
= adap
;
436 if (adap
->dev
.parent
== &platform_bus
)
437 i2c_dev
->class_dev
.dev
= &adap
->dev
;
439 i2c_dev
->class_dev
.dev
= adap
->dev
.parent
;
440 i2c_dev
->class_dev
.class = &i2c_dev_class
;
441 i2c_dev
->class_dev
.devt
= MKDEV(I2C_MAJOR
, i2c_dev
->minor
);
442 snprintf(i2c_dev
->class_dev
.class_id
, BUS_ID_SIZE
, "i2c-%d", i2c_dev
->minor
);
443 retval
= class_device_register(&i2c_dev
->class_dev
);
446 class_device_create_file(&i2c_dev
->class_dev
, &class_device_attr_name
);
449 return_i2c_dev(i2c_dev
);
454 static int i2cdev_detach_adapter(struct i2c_adapter
*adap
)
456 struct i2c_dev
*i2c_dev
;
458 i2c_dev
= i2c_dev_get_by_adapter(adap
);
462 init_completion(&i2c_dev
->released
);
463 return_i2c_dev(i2c_dev
);
464 class_device_unregister(&i2c_dev
->class_dev
);
465 wait_for_completion(&i2c_dev
->released
);
468 pr_debug("i2c-dev: adapter [%s] unregistered\n", adap
->name
);
472 static int i2cdev_detach_client(struct i2c_client
*client
)
477 static int i2cdev_command(struct i2c_client
*client
, unsigned int cmd
,
483 static struct i2c_driver i2cdev_driver
= {
484 .owner
= THIS_MODULE
,
485 .name
= "dev_driver",
486 .id
= I2C_DRIVERID_I2CDEV
,
487 .flags
= I2C_DF_NOTIFY
,
488 .attach_adapter
= i2cdev_attach_adapter
,
489 .detach_adapter
= i2cdev_detach_adapter
,
490 .detach_client
= i2cdev_detach_client
,
491 .command
= i2cdev_command
,
494 static struct i2c_client i2cdev_client_template
= {
495 .name
= "I2C /dev entry",
497 .driver
= &i2cdev_driver
,
500 static int __init
i2c_dev_init(void)
504 printk(KERN_INFO
"i2c /dev entries driver\n");
506 res
= register_chrdev(I2C_MAJOR
, "i2c", &i2cdev_fops
);
510 res
= class_register(&i2c_dev_class
);
512 goto out_unreg_chrdev
;
514 res
= i2c_add_driver(&i2cdev_driver
);
516 goto out_unreg_class
;
521 class_unregister(&i2c_dev_class
);
523 unregister_chrdev(I2C_MAJOR
, "i2c");
525 printk(KERN_ERR
"%s: Driver Initialisation failed\n", __FILE__
);
529 static void __exit
i2c_dev_exit(void)
531 i2c_del_driver(&i2cdev_driver
);
532 class_unregister(&i2c_dev_class
);
533 unregister_chrdev(I2C_MAJOR
,"i2c");
536 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
537 "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
538 MODULE_DESCRIPTION("I2C /dev entries driver");
539 MODULE_LICENSE("GPL");
541 module_init(i2c_dev_init
);
542 module_exit(i2c_dev_exit
);