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>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
23 But I have used so much of his original code and ideas that it seems
24 only fair to recognize him as co-author -- Frodo */
26 /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
28 /* The devfs code is contributed by Philipp Matthias Hahn
29 <pmhahn@titan.lahn.de> */
31 /* $Id: i2c-dev.c,v 1.40 2001/08/25 01:28:01 mds Exp $ */
33 #include <linux/config.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/smp_lock.h>
39 #ifdef CONFIG_DEVFS_FS
40 #include <linux/devfs_fs_kernel.h>
42 #include <linux/init.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-dev.h>
45 #include <asm/uaccess.h>
47 /* If you want debugging uncomment: */
52 extern int init_module(void);
53 extern int cleanup_module(void);
54 #endif /* def MODULE */
56 /* struct file_operations changed too often in the 2.1 series for nice code */
58 static ssize_t i2cdev_read (struct file *file, char *buf, size_t count,
60 static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count,
63 static int i2cdev_ioctl (struct inode *inode, struct file *file,
64 unsigned int cmd, unsigned long arg);
65 static int i2cdev_open (struct inode *inode, struct file *file);
67 static int i2cdev_release (struct inode *inode, struct file *file);
69 static int i2cdev_attach_adapter(struct i2c_adapter *adap);
70 static int i2cdev_detach_client(struct i2c_client *client);
71 static int i2cdev_command(struct i2c_client *client, unsigned int cmd,
79 int __init i2c_dev_init(void);
80 static int i2cdev_cleanup(void);
82 static struct file_operations i2cdev_fops = {
86 .write = i2cdev_write,
87 .ioctl = i2cdev_ioctl,
89 .release = i2cdev_release,
92 #define I2CDEV_ADAPS_MAX I2C_ADAP_MAX
93 static struct i2c_adapter *i2cdev_adaps[I2CDEV_ADAPS_MAX];
94 #ifdef CONFIG_DEVFS_FS
95 static devfs_handle_t devfs_i2c[I2CDEV_ADAPS_MAX];
96 static devfs_handle_t devfs_handle = NULL;
99 static struct i2c_driver i2cdev_driver = {
100 .name = "i2c-dev dummy driver",
101 .id = I2C_DRIVERID_I2CDEV,
102 .flags = I2C_DF_DUMMY,
103 .attach_adapter = i2cdev_attach_adapter,
104 .detach_client = i2cdev_detach_client,
105 .command = i2cdev_command,
108 static struct i2c_client i2cdev_client_template = {
109 .name = "I2C /dev entry",
113 .driver = &i2cdev_driver,
116 static int i2cdev_initialized;
118 static ssize_t i2cdev_read (struct file *file, char *buf, size_t count,
125 struct inode *inode = file->f_dentry->d_inode;
128 struct i2c_client *client = (struct i2c_client *)file->private_data;
133 /* copy user space data to kernel space. */
134 tmp = kmalloc(count,GFP_KERNEL);
139 printk(KERN_DEBUG "i2c-dev.o: i2c-%d reading %d bytes.\n",MINOR(inode->i_rdev),
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 *buf, size_t count,
155 struct i2c_client *client = (struct i2c_client *)file->private_data;
158 struct inode *inode = file->f_dentry->d_inode;
164 /* copy user space data to kernel space. */
165 tmp = kmalloc(count,GFP_KERNEL);
168 if (copy_from_user(tmp,buf,count)) {
174 printk(KERN_DEBUG "i2c-dev.o: i2c-%d writing %d bytes.\n",MINOR(inode->i_rdev),
177 ret = i2c_master_send(client,tmp,count);
182 int i2cdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
185 struct i2c_client *client = (struct i2c_client *)file->private_data;
186 struct i2c_rdwr_ioctl_data rdwr_arg;
187 struct i2c_smbus_ioctl_data data_arg;
188 union i2c_smbus_data temp;
189 struct i2c_msg *rdwr_pa;
195 printk(KERN_DEBUG "i2c-dev.o: i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n",
196 MINOR(inode->i_rdev),cmd, arg);
201 case I2C_SLAVE_FORCE:
203 (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
205 if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))
211 client->flags |= I2C_M_TEN;
213 client->flags &= ~I2C_M_TEN;
216 funcs = i2c_get_functionality(client->adapter);
217 return (copy_to_user((unsigned long *)arg,&funcs,
218 sizeof(unsigned long)))?-EFAULT:0;
221 if (copy_from_user(&rdwr_arg,
222 (struct i2c_rdwr_ioctl_data *)arg,
226 /* Put an arbitrary limit on the number of messages that can
228 if (rdwr_arg.nmsgs > 42)
231 rdwr_pa = (struct i2c_msg *)
232 kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
235 if (rdwr_pa == NULL) return -ENOMEM;
237 if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
238 rdwr_arg.nmsgs * sizeof(struct i2c_msg))) {
243 data_ptrs = (u8 **) kmalloc(rdwr_arg.nmsgs * sizeof(u8 *),
245 if (data_ptrs == NULL) {
251 for( i=0; i<rdwr_arg.nmsgs; i++ )
253 /* Limit the size of the message to a sane amount */
254 if (rdwr_pa[i].len > 8192) {
258 data_ptrs[i] = rdwr_pa[i].buf;
259 rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
260 if(rdwr_pa[i].buf == NULL)
265 if(copy_from_user(rdwr_pa[i].buf,
269 ++i; /* Needs to be kfreed too */
276 for (j = 0; j < i; ++j)
277 kfree(rdwr_pa[j].buf);
283 res = i2c_transfer(client->adapter,
288 if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD))
298 kfree(rdwr_pa[i].buf);
305 if (copy_from_user(&data_arg,
306 (struct i2c_smbus_ioctl_data *) arg,
307 sizeof(struct i2c_smbus_ioctl_data)))
309 if ((data_arg.size != I2C_SMBUS_BYTE) &&
310 (data_arg.size != I2C_SMBUS_QUICK) &&
311 (data_arg.size != I2C_SMBUS_BYTE_DATA) &&
312 (data_arg.size != I2C_SMBUS_WORD_DATA) &&
313 (data_arg.size != I2C_SMBUS_PROC_CALL) &&
314 (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
315 (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA)) {
317 printk(KERN_DEBUG "i2c-dev.o: size out of range (%x) in ioctl I2C_SMBUS.\n",
322 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
323 so the check is valid if size==I2C_SMBUS_QUICK too. */
324 if ((data_arg.read_write != I2C_SMBUS_READ) &&
325 (data_arg.read_write != I2C_SMBUS_WRITE)) {
327 printk(KERN_DEBUG "i2c-dev.o: read_write out of range (%x) in ioctl I2C_SMBUS.\n",
328 data_arg.read_write);
333 /* Note that command values are always valid! */
335 if ((data_arg.size == I2C_SMBUS_QUICK) ||
336 ((data_arg.size == I2C_SMBUS_BYTE) &&
337 (data_arg.read_write == I2C_SMBUS_WRITE)))
338 /* These are special: we do not use data */
339 return i2c_smbus_xfer(client->adapter, client->addr,
343 data_arg.size, NULL);
345 if (data_arg.data == NULL) {
347 printk(KERN_DEBUG "i2c-dev.o: data is NULL pointer in ioctl I2C_SMBUS.\n");
352 if ((data_arg.size == I2C_SMBUS_BYTE_DATA) ||
353 (data_arg.size == I2C_SMBUS_BYTE))
354 datasize = sizeof(data_arg.data->byte);
355 else if ((data_arg.size == I2C_SMBUS_WORD_DATA) ||
356 (data_arg.size == I2C_SMBUS_PROC_CALL))
357 datasize = sizeof(data_arg.data->word);
358 else /* size == I2C_SMBUS_BLOCK_DATA */
359 datasize = sizeof(data_arg.data->block);
361 if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
362 (data_arg.read_write == I2C_SMBUS_WRITE)) {
363 if (copy_from_user(&temp, data_arg.data, datasize))
366 res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
368 data_arg.command,data_arg.size,&temp);
369 if (! res && ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
370 (data_arg.read_write == I2C_SMBUS_READ))) {
371 if (copy_to_user(data_arg.data, &temp, datasize))
377 return i2c_control(client,cmd,arg);
382 int i2cdev_open (struct inode *inode, struct file *file)
384 unsigned int minor = MINOR(inode->i_rdev);
385 struct i2c_client *client;
387 if ((minor >= I2CDEV_ADAPS_MAX) || ! (i2cdev_adaps[minor])) {
389 printk(KERN_DEBUG "i2c-dev.o: Trying to open unattached adapter i2c-%d\n",
395 /* Note that we here allocate a client for later use, but we will *not*
396 register this client! Yes, this is safe. No, it is not very clean. */
397 if(! (client = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
399 memcpy(client,&i2cdev_client_template,sizeof(struct i2c_client));
400 client->adapter = i2cdev_adaps[minor];
401 file->private_data = client;
403 if (i2cdev_adaps[minor]->inc_use)
404 i2cdev_adaps[minor]->inc_use(i2cdev_adaps[minor]);
407 printk(KERN_DEBUG "i2c-dev.o: opened i2c-%d\n",minor);
412 static int i2cdev_release (struct inode *inode, struct file *file)
414 unsigned int minor = MINOR(inode->i_rdev);
415 kfree(file->private_data);
416 file->private_data=NULL;
418 printk(KERN_DEBUG "i2c-dev.o: Closed: i2c-%d\n", minor);
421 if (i2cdev_adaps[minor]->dec_use)
422 i2cdev_adaps[minor]->dec_use(i2cdev_adaps[minor]);
427 int i2cdev_attach_adapter(struct i2c_adapter *adap)
432 if ((i = i2c_adapter_id(adap)) < 0) {
433 printk(KERN_DEBUG "i2c-dev.o: Unknown adapter ?!?\n");
436 if (i >= I2CDEV_ADAPS_MAX) {
437 printk(KERN_DEBUG "i2c-dev.o: Adapter number too large?!? (%d)\n",i);
441 sprintf (name, "%d", i);
442 if (! i2cdev_adaps[i]) {
443 i2cdev_adaps[i] = adap;
444 #ifdef CONFIG_DEVFS_FS
445 devfs_i2c[i] = devfs_register (devfs_handle, name,
446 DEVFS_FL_DEFAULT, I2C_MAJOR, i,
447 S_IFCHR | S_IRUSR | S_IWUSR,
450 printk(KERN_DEBUG "i2c-dev.o: Registered '%s' as minor %d\n",adap->name,i);
452 /* This is actually a detach_adapter call! */
453 #ifdef CONFIG_DEVFS_FS
454 devfs_unregister(devfs_i2c[i]);
456 i2cdev_adaps[i] = NULL;
458 printk(KERN_DEBUG "i2c-dev.o: Adapter unregistered: %s\n",adap->name);
465 int i2cdev_detach_client(struct i2c_client *client)
470 static int i2cdev_command(struct i2c_client *client, unsigned int cmd,
476 int __init i2c_dev_init(void)
480 printk(KERN_INFO "i2c-dev.o: i2c /dev entries driver module version %s (%s)\n", I2C_VERSION, I2C_DATE);
482 i2cdev_initialized = 0;
483 #ifdef CONFIG_DEVFS_FS
484 if (devfs_register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops)) {
486 if (register_chrdev(I2C_MAJOR,"i2c",&i2cdev_fops)) {
488 printk(KERN_ERR "i2c-dev.o: unable to get major %d for i2c bus\n",
492 #ifdef CONFIG_DEVFS_FS
493 devfs_handle = devfs_mk_dir(NULL, "i2c", NULL);
495 i2cdev_initialized ++;
497 if ((res = i2c_add_driver(&i2cdev_driver))) {
498 printk(KERN_ERR "i2c-dev.o: Driver registration failed, module not inserted.\n");
502 i2cdev_initialized ++;
506 int i2cdev_cleanup(void)
510 if (i2cdev_initialized >= 2) {
511 if ((res = i2c_del_driver(&i2cdev_driver))) {
512 printk("i2c-dev.o: Driver deregistration failed, "
513 "module not removed.\n");
516 i2cdev_initialized --;
519 if (i2cdev_initialized >= 1) {
520 #ifdef CONFIG_DEVFS_FS
521 devfs_unregister(devfs_handle);
522 if ((res = devfs_unregister_chrdev(I2C_MAJOR, "i2c"))) {
524 if ((res = unregister_chrdev(I2C_MAJOR,"i2c"))) {
526 printk("i2c-dev.o: unable to release major %d for i2c bus\n",
530 i2cdev_initialized --;
539 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and Simon G. Vogl <simon@tk.uni-linz.ac.at>");
540 MODULE_DESCRIPTION("I2C /dev entries driver");
541 MODULE_LICENSE("GPL");
543 int init_module(void)
545 return i2c_dev_init();
548 int cleanup_module(void)
550 return i2cdev_cleanup();
553 #endif /* def MODULE */