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.1.1.1 2005/04/11 02:50:21 jack Exp $ */
33 #include <linux/config.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/version.h>
39 #if LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0)
40 #include <linux/smp_lock.h>
41 #endif /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */
42 #ifdef CONFIG_DEVFS_FS
43 #include <linux/devfs_fs_kernel.h>
47 /* If you want debugging uncomment: */
50 #include <linux/init.h>
51 #include <asm/uaccess.h>
53 #include <linux/i2c.h>
54 #include <linux/i2c-dev.h>
57 extern int init_module(void);
58 extern int cleanup_module(void);
59 #endif /* def MODULE */
61 /* struct file_operations changed too often in the 2.1 series for nice code */
63 #if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,9)
64 static loff_t i2cdev_lseek (struct file *file, loff_t offset, int origin);
66 static ssize_t i2cdev_read (struct file *file, char *buf, size_t count,
68 static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count,
71 static int i2cdev_ioctl (struct inode *inode, struct file *file,
72 unsigned int cmd, unsigned long arg);
73 static int i2cdev_open (struct inode *inode, struct file *file);
75 static int i2cdev_release (struct inode *inode, struct file *file);
77 static int i2cdev_attach_adapter(struct i2c_adapter *adap);
78 static int i2cdev_detach_client(struct i2c_client *client);
79 static int i2cdev_command(struct i2c_client *client, unsigned int cmd,
87 int __init i2c_dev_init(void);
88 static int i2cdev_cleanup(void);
90 static struct file_operations i2cdev_fops = {
91 #if LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0)
93 #endif /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */
94 #if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,9)
103 release: i2cdev_release,
106 #define I2CDEV_ADAPS_MAX I2C_ADAP_MAX
107 static struct i2c_adapter *i2cdev_adaps[I2CDEV_ADAPS_MAX];
108 #ifdef CONFIG_DEVFS_FS
109 static devfs_handle_t devfs_i2c[I2CDEV_ADAPS_MAX];
110 static devfs_handle_t devfs_handle = NULL;
113 static struct i2c_driver i2cdev_driver = {
114 name: "i2c-dev dummy driver",
115 id: I2C_DRIVERID_I2CDEV,
117 attach_adapter: i2cdev_attach_adapter,
118 detach_client: i2cdev_detach_client,
119 command: i2cdev_command,
124 static struct i2c_client i2cdev_client_template = {
125 name: "I2C /dev entry",
130 driver: &i2cdev_driver,
134 static int i2cdev_initialized;
136 #if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,9)
137 /* Note that the lseek function is called llseek in 2.1 kernels. But things
138 are complicated enough as is. */
139 loff_t i2cdev_lseek (struct file *file, loff_t offset, int origin)
142 struct inode *inode = file->f_dentry->d_inode;
143 printk("i2c-dev.o: i2c-%d lseek to %ld bytes relative to %d.\n",
144 MINOR(inode->i_rdev),(long) offset,origin);
150 static ssize_t i2cdev_read (struct file *file, char *buf, size_t count,
157 struct inode *inode = file->f_dentry->d_inode;
160 struct i2c_client *client = (struct i2c_client *)file->private_data;
165 /* copy user space data to kernel space. */
166 tmp = kmalloc(count,GFP_KERNEL);
171 printk("i2c-dev.o: i2c-%d reading %d bytes.\n",MINOR(inode->i_rdev),
175 ret = i2c_master_recv(client,tmp,count);
177 ret = copy_to_user(buf,tmp,count)?-EFAULT:ret;
182 static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count,
187 struct i2c_client *client = (struct i2c_client *)file->private_data;
190 struct inode *inode = file->f_dentry->d_inode;
196 /* copy user space data to kernel space. */
197 tmp = kmalloc(count,GFP_KERNEL);
200 if (copy_from_user(tmp,buf,count)) {
206 printk("i2c-dev.o: i2c-%d writing %d bytes.\n",MINOR(inode->i_rdev),
209 ret = i2c_master_send(client,tmp,count);
214 int i2cdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
217 struct i2c_client *client = (struct i2c_client *)file->private_data;
218 struct i2c_rdwr_ioctl_data rdwr_arg;
219 struct i2c_smbus_ioctl_data data_arg;
220 union i2c_smbus_data temp;
221 struct i2c_msg *rdwr_pa;
226 printk("i2c-dev.o: i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n",
227 MINOR(inode->i_rdev),cmd, arg);
232 case I2C_SLAVE_FORCE:
234 (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
236 if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))
242 client->flags |= I2C_M_TEN;
244 client->flags &= ~I2C_M_TEN;
247 funcs = i2c_get_functionality(client->adapter);
248 return (copy_to_user((unsigned long *)arg,&funcs,
249 sizeof(unsigned long)))?-EFAULT:0;
252 if (copy_from_user(&rdwr_arg,
253 (struct i2c_rdwr_ioctl_data *)arg,
257 rdwr_pa = (struct i2c_msg *)
258 kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
261 if (rdwr_pa == NULL) return -ENOMEM;
264 for( i=0; i<rdwr_arg.nmsgs; i++ )
266 if(copy_from_user(&(rdwr_pa[i]),
273 rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
274 if(rdwr_pa[i].buf == NULL)
279 if(copy_from_user(rdwr_pa[i].buf,
280 rdwr_arg.msgs[i].buf,
283 kfree(rdwr_pa[i].buf);
290 res = i2c_transfer(client->adapter,
296 if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD))
299 rdwr_arg.msgs[i].buf,
306 kfree(rdwr_pa[i].buf);
312 if (copy_from_user(&data_arg,
313 (struct i2c_smbus_ioctl_data *) arg,
314 sizeof(struct i2c_smbus_ioctl_data)))
316 if ((data_arg.size != I2C_SMBUS_BYTE) &&
317 (data_arg.size != I2C_SMBUS_QUICK) &&
318 (data_arg.size != I2C_SMBUS_BYTE_DATA) &&
319 (data_arg.size != I2C_SMBUS_WORD_DATA) &&
320 (data_arg.size != I2C_SMBUS_PROC_CALL) &&
321 (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
322 (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA)) {
324 printk("i2c-dev.o: size out of range (%x) in ioctl I2C_SMBUS.\n",
329 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
330 so the check is valid if size==I2C_SMBUS_QUICK too. */
331 if ((data_arg.read_write != I2C_SMBUS_READ) &&
332 (data_arg.read_write != I2C_SMBUS_WRITE)) {
334 printk("i2c-dev.o: read_write out of range (%x) in ioctl I2C_SMBUS.\n",
335 data_arg.read_write);
340 /* Note that command values are always valid! */
342 if ((data_arg.size == I2C_SMBUS_QUICK) ||
343 ((data_arg.size == I2C_SMBUS_BYTE) &&
344 (data_arg.read_write == I2C_SMBUS_WRITE)))
345 /* These are special: we do not use data */
346 return i2c_smbus_xfer(client->adapter, client->addr,
350 data_arg.size, NULL);
352 if (data_arg.data == NULL) {
354 printk("i2c-dev.o: data is NULL pointer in ioctl I2C_SMBUS.\n");
359 if ((data_arg.size == I2C_SMBUS_BYTE_DATA) ||
360 (data_arg.size == I2C_SMBUS_BYTE))
361 datasize = sizeof(data_arg.data->byte);
362 else if ((data_arg.size == I2C_SMBUS_WORD_DATA) ||
363 (data_arg.size == I2C_SMBUS_PROC_CALL))
364 datasize = sizeof(data_arg.data->word);
365 else /* size == I2C_SMBUS_BLOCK_DATA */
366 datasize = sizeof(data_arg.data->block);
368 if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
369 (data_arg.read_write == I2C_SMBUS_WRITE)) {
370 if (copy_from_user(&temp, data_arg.data, datasize))
373 res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
375 data_arg.command,data_arg.size,&temp);
376 if (! res && ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
377 (data_arg.read_write == I2C_SMBUS_READ))) {
378 if (copy_to_user(data_arg.data, &temp, datasize))
384 return i2c_control(client,cmd,arg);
389 int i2cdev_open (struct inode *inode, struct file *file)
391 unsigned int minor = MINOR(inode->i_rdev);
392 struct i2c_client *client;
394 if ((minor >= I2CDEV_ADAPS_MAX) || ! (i2cdev_adaps[minor])) {
396 printk("i2c-dev.o: Trying to open unattached adapter i2c-%d\n",
402 /* Note that we here allocate a client for later use, but we will *not*
403 register this client! Yes, this is safe. No, it is not very clean. */
404 if(! (client = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
406 memcpy(client,&i2cdev_client_template,sizeof(struct i2c_client));
407 client->adapter = i2cdev_adaps[minor];
408 file->private_data = client;
410 if (i2cdev_adaps[minor]->inc_use)
411 i2cdev_adaps[minor]->inc_use(i2cdev_adaps[minor]);
412 #if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,0)
414 #endif /* LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,0) */
417 printk("i2c-dev.o: opened i2c-%d\n",minor);
422 static int i2cdev_release (struct inode *inode, struct file *file)
424 unsigned int minor = MINOR(inode->i_rdev);
425 kfree(file->private_data);
426 file->private_data=NULL;
428 printk("i2c-dev.o: Closed: i2c-%d\n", minor);
430 #if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,0)
432 #else /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */
434 #endif /* LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,0) */
435 if (i2cdev_adaps[minor]->dec_use)
436 i2cdev_adaps[minor]->dec_use(i2cdev_adaps[minor]);
437 #if LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0)
439 #endif /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */
443 int i2cdev_attach_adapter(struct i2c_adapter *adap)
448 if ((i = i2c_adapter_id(adap)) < 0) {
449 printk("i2c-dev.o: Unknown adapter ?!?\n");
452 if (i >= I2CDEV_ADAPS_MAX) {
453 printk("i2c-dev.o: Adapter number too large?!? (%d)\n",i);
457 sprintf (name, "%d", i);
458 if (! i2cdev_adaps[i]) {
459 i2cdev_adaps[i] = adap;
460 #ifdef CONFIG_DEVFS_FS
461 devfs_i2c[i] = devfs_register (devfs_handle, name,
462 DEVFS_FL_DEFAULT, I2C_MAJOR, i,
463 S_IFCHR | S_IRUSR | S_IWUSR,
466 printk("i2c-dev.o: Registered '%s' as minor %d\n",adap->name,i);
468 /* This is actually a detach_adapter call! */
469 #ifdef CONFIG_DEVFS_FS
470 devfs_unregister(devfs_i2c[i]);
472 i2cdev_adaps[i] = NULL;
474 printk("i2c-dev.o: Adapter unregistered: %s\n",adap->name);
481 int i2cdev_detach_client(struct i2c_client *client)
486 static int i2cdev_command(struct i2c_client *client, unsigned int cmd,
492 int __init i2c_dev_init(void)
496 printk("i2c-dev.o: i2c /dev entries driver module\n");
498 i2cdev_initialized = 0;
499 #ifdef CONFIG_DEVFS_FS
500 if (devfs_register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops)) {
502 if (register_chrdev(I2C_MAJOR,"i2c",&i2cdev_fops)) {
504 printk("i2c-dev.o: unable to get major %d for i2c bus\n",
508 #ifdef CONFIG_DEVFS_FS
509 devfs_handle = devfs_mk_dir(NULL, "i2c", NULL);
511 i2cdev_initialized ++;
513 if ((res = i2c_add_driver(&i2cdev_driver))) {
514 printk("i2c-dev.o: Driver registration failed, module not inserted.\n");
518 i2cdev_initialized ++;
522 int i2cdev_cleanup(void)
526 if (i2cdev_initialized >= 2) {
527 if ((res = i2c_del_driver(&i2cdev_driver))) {
528 printk("i2c-dev.o: Driver deregistration failed, "
529 "module not removed.\n");
532 i2cdev_initialized --;
535 if (i2cdev_initialized >= 1) {
536 #ifdef CONFIG_DEVFS_FS
537 devfs_unregister(devfs_handle);
538 if ((res = devfs_unregister_chrdev(I2C_MAJOR, "i2c"))) {
540 if ((res = unregister_chrdev(I2C_MAJOR,"i2c"))) {
542 printk("i2c-dev.o: unable to release major %d for i2c bus\n",
546 i2cdev_initialized --;
555 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and Simon G. Vogl <simon@tk.uni-linz.ac.at>");
556 MODULE_DESCRIPTION("I2C /dev entries driver");
557 MODULE_LICENSE("GPL");
559 int init_module(void)
561 return i2c_dev_init();
564 int cleanup_module(void)
566 return i2cdev_cleanup();
569 #endif /* def MODULE */