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 /* The devfs code is contributed by Philipp Matthias Hahn
30 <pmhahn@titan.lahn.de> */
32 #include <linux/config.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/smp_lock.h>
38 #include <linux/devfs_fs_kernel.h>
39 #include <linux/init.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-dev.h>
42 #include <asm/uaccess.h>
44 static struct i2c_client i2cdev_client_template
;
48 struct i2c_adapter
*adap
;
49 struct class_device class_dev
;
50 struct completion released
; /* FIXME, we need a class_device_unregister() */
52 #define to_i2c_dev(d) container_of(d, struct i2c_dev, class_dev)
54 #define I2C_MINORS 256
55 static struct i2c_dev
*i2c_dev_array
[I2C_MINORS
];
56 static spinlock_t i2c_dev_array_lock
= SPIN_LOCK_UNLOCKED
;
58 struct i2c_dev
*i2c_dev_get_by_minor(unsigned index
)
60 struct i2c_dev
*i2c_dev
;
62 spin_lock(&i2c_dev_array_lock
);
63 i2c_dev
= i2c_dev_array
[index
];
64 spin_unlock(&i2c_dev_array_lock
);
68 struct i2c_dev
*i2c_dev_get_by_adapter(struct i2c_adapter
*adap
)
70 struct i2c_dev
*i2c_dev
= NULL
;
72 spin_lock(&i2c_dev_array_lock
);
73 if ((i2c_dev_array
[adap
->nr
]) &&
74 (i2c_dev_array
[adap
->nr
]->adap
== adap
))
75 i2c_dev
= i2c_dev_array
[adap
->nr
];
76 spin_unlock(&i2c_dev_array_lock
);
80 static struct i2c_dev
*get_free_i2c_dev(struct i2c_adapter
*adap
)
82 struct i2c_dev
*i2c_dev
;
84 i2c_dev
= kmalloc(sizeof(*i2c_dev
), GFP_KERNEL
);
86 return ERR_PTR(-ENOMEM
);
87 memset(i2c_dev
, 0x00, sizeof(*i2c_dev
));
89 spin_lock(&i2c_dev_array_lock
);
90 if (i2c_dev_array
[adap
->nr
]) {
91 spin_unlock(&i2c_dev_array_lock
);
92 dev_err(&adap
->dev
, "i2c-dev already has a device assigned to this adapter\n");
95 i2c_dev
->minor
= adap
->nr
;
96 i2c_dev_array
[adap
->nr
] = i2c_dev
;
97 spin_unlock(&i2c_dev_array_lock
);
101 return ERR_PTR(-ENODEV
);
104 static void return_i2c_dev(struct i2c_dev
*i2c_dev
)
106 spin_lock(&i2c_dev_array_lock
);
107 i2c_dev_array
[i2c_dev
->minor
] = NULL
;
108 spin_unlock(&i2c_dev_array_lock
);
111 static ssize_t
show_dev(struct class_device
*class_dev
, char *buf
)
113 struct i2c_dev
*i2c_dev
= to_i2c_dev(class_dev
);
114 return print_dev_t(buf
, MKDEV(I2C_MAJOR
, i2c_dev
->minor
));
116 static CLASS_DEVICE_ATTR(dev
, S_IRUGO
, show_dev
, NULL
);
118 static ssize_t
show_adapter_name(struct class_device
*class_dev
, char *buf
)
120 struct i2c_dev
*i2c_dev
= to_i2c_dev(class_dev
);
121 return sprintf(buf
, "%s\n", i2c_dev
->adap
->name
);
123 static CLASS_DEVICE_ATTR(name
, S_IRUGO
, show_adapter_name
, NULL
);
125 static ssize_t
i2cdev_read (struct file
*file
, char __user
*buf
, size_t count
,
131 struct i2c_client
*client
= (struct i2c_client
*)file
->private_data
;
136 tmp
= kmalloc(count
,GFP_KERNEL
);
140 pr_debug("i2c-dev: i2c-%d reading %zd bytes.\n",
141 iminor(file
->f_dentry
->d_inode
), count
);
143 ret
= i2c_master_recv(client
,tmp
,count
);
145 ret
= copy_to_user(buf
,tmp
,count
)?-EFAULT
:ret
;
150 static ssize_t
i2cdev_write (struct file
*file
, const char __user
*buf
, size_t count
,
155 struct i2c_client
*client
= (struct i2c_client
*)file
->private_data
;
160 tmp
= kmalloc(count
,GFP_KERNEL
);
163 if (copy_from_user(tmp
,buf
,count
)) {
168 pr_debug("i2c-dev: i2c-%d writing %zd bytes.\n",
169 iminor(file
->f_dentry
->d_inode
), count
);
171 ret
= i2c_master_send(client
,tmp
,count
);
176 int i2cdev_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
,
179 struct i2c_client
*client
= (struct i2c_client
*)file
->private_data
;
180 struct i2c_rdwr_ioctl_data rdwr_arg
;
181 struct i2c_smbus_ioctl_data data_arg
;
182 union i2c_smbus_data temp
;
183 struct i2c_msg
*rdwr_pa
;
184 u8 __user
**data_ptrs
;
188 dev_dbg(&client
->adapter
->dev
, "i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n",
189 iminor(inode
),cmd
, arg
);
193 case I2C_SLAVE_FORCE
:
195 (((client
->flags
& I2C_M_TEN
) == 0) && arg
> 0x7f))
197 if ((cmd
== I2C_SLAVE
) && i2c_check_addr(client
->adapter
,arg
))
203 client
->flags
|= I2C_M_TEN
;
205 client
->flags
&= ~I2C_M_TEN
;
209 client
->flags
|= I2C_CLIENT_PEC
;
211 client
->flags
&= ~I2C_CLIENT_PEC
;
214 funcs
= i2c_get_functionality(client
->adapter
);
215 return (copy_to_user((unsigned long __user
*)arg
, &funcs
,
216 sizeof(unsigned long)))?-EFAULT
:0;
219 if (copy_from_user(&rdwr_arg
,
220 (struct i2c_rdwr_ioctl_data __user
*)arg
,
224 /* Put an arbritrary limit on the number of messages that can
226 if (rdwr_arg
.nmsgs
> I2C_RDRW_IOCTL_MAX_MSGS
)
229 rdwr_pa
= (struct i2c_msg
*)
230 kmalloc(rdwr_arg
.nmsgs
* sizeof(struct i2c_msg
),
233 if (rdwr_pa
== NULL
) return -ENOMEM
;
235 if (copy_from_user(rdwr_pa
, rdwr_arg
.msgs
,
236 rdwr_arg
.nmsgs
* sizeof(struct i2c_msg
))) {
241 data_ptrs
= kmalloc(rdwr_arg
.nmsgs
* sizeof(u8 __user
*), GFP_KERNEL
);
242 if (data_ptrs
== NULL
) {
248 for( i
=0; i
<rdwr_arg
.nmsgs
; i
++ ) {
249 /* Limit the size of the message to a sane amount */
250 if (rdwr_pa
[i
].len
> 8192) {
254 data_ptrs
[i
] = (u8 __user
*)rdwr_pa
[i
].buf
;
255 rdwr_pa
[i
].buf
= kmalloc(rdwr_pa
[i
].len
, GFP_KERNEL
);
256 if(rdwr_pa
[i
].buf
== NULL
) {
260 if(copy_from_user(rdwr_pa
[i
].buf
,
263 ++i
; /* Needs to be kfreed too */
270 for (j
= 0; j
< i
; ++j
)
271 kfree(rdwr_pa
[j
].buf
);
277 res
= i2c_transfer(client
->adapter
,
281 if( res
>=0 && (rdwr_pa
[i
].flags
& I2C_M_RD
)) {
289 kfree(rdwr_pa
[i
].buf
);
296 if (copy_from_user(&data_arg
,
297 (struct i2c_smbus_ioctl_data __user
*) arg
,
298 sizeof(struct i2c_smbus_ioctl_data
)))
300 if ((data_arg
.size
!= I2C_SMBUS_BYTE
) &&
301 (data_arg
.size
!= I2C_SMBUS_QUICK
) &&
302 (data_arg
.size
!= I2C_SMBUS_BYTE_DATA
) &&
303 (data_arg
.size
!= I2C_SMBUS_WORD_DATA
) &&
304 (data_arg
.size
!= I2C_SMBUS_PROC_CALL
) &&
305 (data_arg
.size
!= I2C_SMBUS_BLOCK_DATA
) &&
306 (data_arg
.size
!= I2C_SMBUS_I2C_BLOCK_DATA
) &&
307 (data_arg
.size
!= I2C_SMBUS_BLOCK_PROC_CALL
)) {
308 dev_dbg(&client
->adapter
->dev
,
309 "size out of range (%x) in ioctl I2C_SMBUS.\n",
313 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
314 so the check is valid if size==I2C_SMBUS_QUICK too. */
315 if ((data_arg
.read_write
!= I2C_SMBUS_READ
) &&
316 (data_arg
.read_write
!= I2C_SMBUS_WRITE
)) {
317 dev_dbg(&client
->adapter
->dev
,
318 "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
319 data_arg
.read_write
);
323 /* Note that command values are always valid! */
325 if ((data_arg
.size
== I2C_SMBUS_QUICK
) ||
326 ((data_arg
.size
== I2C_SMBUS_BYTE
) &&
327 (data_arg
.read_write
== I2C_SMBUS_WRITE
)))
328 /* These are special: we do not use data */
329 return i2c_smbus_xfer(client
->adapter
, client
->addr
,
333 data_arg
.size
, NULL
);
335 if (data_arg
.data
== NULL
) {
336 dev_dbg(&client
->adapter
->dev
,
337 "data is NULL pointer in ioctl I2C_SMBUS.\n");
341 if ((data_arg
.size
== I2C_SMBUS_BYTE_DATA
) ||
342 (data_arg
.size
== I2C_SMBUS_BYTE
))
343 datasize
= sizeof(data_arg
.data
->byte
);
344 else if ((data_arg
.size
== I2C_SMBUS_WORD_DATA
) ||
345 (data_arg
.size
== I2C_SMBUS_PROC_CALL
))
346 datasize
= sizeof(data_arg
.data
->word
);
347 else /* size == smbus block, i2c block, or block proc. call */
348 datasize
= sizeof(data_arg
.data
->block
);
350 if ((data_arg
.size
== I2C_SMBUS_PROC_CALL
) ||
351 (data_arg
.size
== I2C_SMBUS_BLOCK_PROC_CALL
) ||
352 (data_arg
.read_write
== I2C_SMBUS_WRITE
)) {
353 if (copy_from_user(&temp
, data_arg
.data
, datasize
))
356 res
= i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
358 data_arg
.command
,data_arg
.size
,&temp
);
359 if (! res
&& ((data_arg
.size
== I2C_SMBUS_PROC_CALL
) ||
360 (data_arg
.size
== I2C_SMBUS_BLOCK_PROC_CALL
) ||
361 (data_arg
.read_write
== I2C_SMBUS_READ
))) {
362 if (copy_to_user(data_arg
.data
, &temp
, datasize
))
368 return i2c_control(client
,cmd
,arg
);
373 static int i2cdev_open(struct inode
*inode
, struct file
*file
)
375 unsigned int minor
= iminor(inode
);
376 struct i2c_client
*client
;
377 struct i2c_adapter
*adap
;
378 struct i2c_dev
*i2c_dev
;
380 i2c_dev
= i2c_dev_get_by_minor(minor
);
384 adap
= i2c_get_adapter(i2c_dev
->adap
->nr
);
388 client
= kmalloc(sizeof(*client
), GFP_KERNEL
);
390 i2c_put_adapter(adap
);
393 memcpy(client
, &i2cdev_client_template
, sizeof(*client
));
395 /* registered with adapter, passed as client to user */
396 client
->adapter
= adap
;
397 file
->private_data
= client
;
402 static int i2cdev_release(struct inode
*inode
, struct file
*file
)
404 struct i2c_client
*client
= file
->private_data
;
406 i2c_put_adapter(client
->adapter
);
408 file
->private_data
= NULL
;
413 static struct file_operations i2cdev_fops
= {
414 .owner
= THIS_MODULE
,
417 .write
= i2cdev_write
,
418 .ioctl
= i2cdev_ioctl
,
420 .release
= i2cdev_release
,
423 static void release_i2c_dev(struct class_device
*dev
)
425 struct i2c_dev
*i2c_dev
= to_i2c_dev(dev
);
426 complete(&i2c_dev
->released
);
429 static struct class i2c_dev_class
= {
431 .release
= &release_i2c_dev
,
434 static int i2cdev_attach_adapter(struct i2c_adapter
*adap
)
436 struct i2c_dev
*i2c_dev
;
439 i2c_dev
= get_free_i2c_dev(adap
);
441 return PTR_ERR(i2c_dev
);
443 devfs_mk_cdev(MKDEV(I2C_MAJOR
, i2c_dev
->minor
),
444 S_IFCHR
|S_IRUSR
|S_IWUSR
, "i2c/%d", i2c_dev
->minor
);
445 dev_dbg(&adap
->dev
, "Registered as minor %d\n", i2c_dev
->minor
);
447 /* register this i2c device with the driver core */
448 i2c_dev
->adap
= adap
;
449 if (adap
->dev
.parent
== &platform_bus
)
450 i2c_dev
->class_dev
.dev
= &adap
->dev
;
452 i2c_dev
->class_dev
.dev
= adap
->dev
.parent
;
453 i2c_dev
->class_dev
.class = &i2c_dev_class
;
454 snprintf(i2c_dev
->class_dev
.class_id
, BUS_ID_SIZE
, "i2c-%d", i2c_dev
->minor
);
455 retval
= class_device_register(&i2c_dev
->class_dev
);
458 class_device_create_file(&i2c_dev
->class_dev
, &class_device_attr_dev
);
459 class_device_create_file(&i2c_dev
->class_dev
, &class_device_attr_name
);
462 return_i2c_dev(i2c_dev
);
467 static int i2cdev_detach_adapter(struct i2c_adapter
*adap
)
469 struct i2c_dev
*i2c_dev
;
471 i2c_dev
= i2c_dev_get_by_adapter(adap
);
475 init_completion(&i2c_dev
->released
);
476 devfs_remove("i2c/%d", i2c_dev
->minor
);
477 return_i2c_dev(i2c_dev
);
478 class_device_unregister(&i2c_dev
->class_dev
);
479 wait_for_completion(&i2c_dev
->released
);
482 dev_dbg(&adap
->dev
, "Adapter unregistered\n");
486 static int i2cdev_detach_client(struct i2c_client
*client
)
491 static int i2cdev_command(struct i2c_client
*client
, unsigned int cmd
,
497 static struct i2c_driver i2cdev_driver
= {
498 .owner
= THIS_MODULE
,
499 .name
= "dev_driver",
500 .id
= I2C_DRIVERID_I2CDEV
,
501 .flags
= I2C_DF_NOTIFY
,
502 .attach_adapter
= i2cdev_attach_adapter
,
503 .detach_adapter
= i2cdev_detach_adapter
,
504 .detach_client
= i2cdev_detach_client
,
505 .command
= i2cdev_command
,
508 static struct i2c_client i2cdev_client_template
= {
509 .name
= "I2C /dev entry",
512 .driver
= &i2cdev_driver
,
515 static int __init
i2c_dev_init(void)
519 printk(KERN_INFO
"i2c /dev entries driver\n");
521 res
= register_chrdev(I2C_MAJOR
, "i2c", &i2cdev_fops
);
525 res
= class_register(&i2c_dev_class
);
527 goto out_unreg_chrdev
;
529 res
= i2c_add_driver(&i2cdev_driver
);
531 goto out_unreg_class
;
538 class_unregister(&i2c_dev_class
);
540 unregister_chrdev(I2C_MAJOR
, "i2c");
542 printk(KERN_ERR
"%s: Driver Initialisation failed", __FILE__
);
546 static void __exit
i2c_dev_exit(void)
548 i2c_del_driver(&i2cdev_driver
);
549 class_unregister(&i2c_dev_class
);
551 unregister_chrdev(I2C_MAJOR
,"i2c");
554 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
555 "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
556 MODULE_DESCRIPTION("I2C /dev entries driver");
557 MODULE_LICENSE("GPL");
559 module_init(i2c_dev_init
);
560 module_exit(i2c_dev_exit
);