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 /* $Id: i2c-dev.c,v 1.32 2000/07/25 23:52:17 frodo Exp $ */ 30 #include <linux/kernel.h> 31 #include <linux/module.h> 33 #include <linux/malloc.h> 34 #include <linux/version.h> 35 #include <linux/smp_lock.h> 37 /* If you want debugging uncomment: */ 40 #include <linux/init.h> 41 #include <asm/uaccess.h> 43 #include <linux/i2c.h> 44 #include <linux/i2c-dev.h> 47 externintinit_module(void); 48 externintcleanup_module(void); 49 #endif/* def MODULE */ 51 /* struct file_operations changed too often in the 2.1 series for nice code */ 53 static loff_t
i2cdev_lseek(struct file
*file
, loff_t offset
,int origin
); 55 static ssize_t
i2cdev_read(struct file
*file
,char*buf
,size_t count
, 57 static ssize_t
i2cdev_write(struct file
*file
,const char*buf
,size_t count
, 60 static inti2cdev_ioctl(struct inode
*inode
,struct file
*file
, 61 unsigned int cmd
,unsigned long arg
); 62 static inti2cdev_open(struct inode
*inode
,struct file
*file
); 64 static inti2cdev_release(struct inode
*inode
,struct file
*file
); 66 static inti2cdev_attach_adapter(struct i2c_adapter
*adap
); 67 static inti2cdev_detach_client(struct i2c_client
*client
); 68 static inti2cdev_command(struct i2c_client
*client
,unsigned int cmd
, 76 int __init
i2c_dev_init(void); 77 static inti2cdev_cleanup(void); 79 static struct file_operations i2cdev_fops
= { 86 release
: i2cdev_release
, 89 #define I2CDEV_ADAPS_MAX I2C_ADAP_MAX 90 static struct i2c_adapter
*i2cdev_adaps
[I2CDEV_ADAPS_MAX
]; 92 static struct i2c_driver i2cdev_driver
= { 93 /* name */"i2c-dev dummy driver", 94 /* id */ I2C_DRIVERID_I2CDEV
, 95 /* flags */ I2C_DF_DUMMY
, 96 /* attach_adapter */ i2cdev_attach_adapter
, 97 /* detach_client */ i2cdev_detach_client
, 98 /* command */ i2cdev_command
, 103 static struct i2c_client i2cdev_client_template
= { 104 /* name */"I2C /dev entry", 109 /* driver */&i2cdev_driver
, 113 static int i2cdev_initialized
; 115 /* Note that the lseek function is called llseek in 2.1 kernels. But things 116 are complicated enough as is. */ 117 loff_t
i2cdev_lseek(struct file
*file
, loff_t offset
,int origin
) 120 struct inode
*inode
= file
->f_dentry
->d_inode
; 121 printk("i2c-dev,o: i2c-%d lseek to %ld bytes relative to %d.\n", 122 MINOR(inode
->i_rdev
),(long) offset
,origin
); 127 static ssize_t
i2cdev_read(struct file
*file
,char*buf
,size_t count
, 134 struct inode
*inode
= file
->f_dentry
->d_inode
; 137 struct i2c_client
*client
= (struct i2c_client
*)file
->private_data
; 139 /* copy user space data to kernel space. */ 140 tmp
=kmalloc(count
,GFP_KERNEL
); 145 printk("i2c-dev,o: i2c-%d reading %d bytes.\n",MINOR(inode
->i_rdev
), 149 ret
=i2c_master_recv(client
,tmp
,count
); 151 ret
=copy_to_user(buf
,tmp
,count
)?-EFAULT
:ret
; 156 static ssize_t
i2cdev_write(struct file
*file
,const char*buf
,size_t count
, 161 struct i2c_client
*client
= (struct i2c_client
*)file
->private_data
; 164 struct inode
*inode
= file
->f_dentry
->d_inode
; 167 /* copy user space data to kernel space. */ 168 tmp
=kmalloc(count
,GFP_KERNEL
); 171 if(copy_from_user(tmp
,buf
,count
)) { 177 printk("i2c-dev,o: i2c-%d writing %d bytes.\n",MINOR(inode
->i_rdev
), 180 ret
=i2c_master_send(client
,tmp
,count
); 185 inti2cdev_ioctl(struct inode
*inode
,struct file
*file
,unsigned int cmd
, 188 struct i2c_client
*client
= (struct i2c_client
*)file
->private_data
; 189 struct i2c_rdwr_ioctl_data rdwr_arg
; 190 struct i2c_smbus_ioctl_data data_arg
; 191 union i2c_smbus_data temp
; 192 struct i2c_msg
*rdwr_pa
; 197 printk("i2c-dev.o: i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n", 198 MINOR(inode
->i_rdev
),cmd
, arg
); 203 case I2C_SLAVE_FORCE
: 205 (((client
->flags
& I2C_M_TEN
) ==0) && arg
>0x7f)) 207 if((cmd
== I2C_SLAVE
) &&i2c_check_addr(client
->adapter
,arg
)) 213 client
->flags
|= I2C_M_TEN
; 215 client
->flags
&= ~I2C_M_TEN
; 218 funcs
=i2c_get_functionality(client
->adapter
); 219 return(copy_to_user((unsigned long*)arg
,&funcs
, 220 sizeof(unsigned long)))?-EFAULT
:0; 223 if(copy_from_user(&rdwr_arg
, 224 (struct i2c_rdwr_ioctl_data
*)arg
, 228 rdwr_pa
= (struct i2c_msg
*) 229 kmalloc(rdwr_arg
.nmsgs
*sizeof(struct i2c_msg
), 232 if(rdwr_pa
== NULL
)return-ENOMEM
; 235 for( i
=0; i
<rdwr_arg
.nmsgs
; i
++ ) 237 if(copy_from_user(&(rdwr_pa
[i
]), 244 rdwr_pa
[i
].buf
=kmalloc(rdwr_pa
[i
].len
, GFP_KERNEL
); 245 if(rdwr_pa
[i
].buf
== NULL
) 250 if(copy_from_user(rdwr_pa
[i
].buf
, 251 rdwr_arg
.msgs
[i
].buf
, 254 kfree(rdwr_pa
[i
].buf
); 261 res
=i2c_transfer(client
->adapter
, 267 if( res
>=0&& (rdwr_pa
[i
].flags
& I2C_M_RD
)) 270 rdwr_arg
.msgs
[i
].buf
, 277 kfree(rdwr_pa
[i
].buf
); 283 if(copy_from_user(&data_arg
, 284 (struct i2c_smbus_ioctl_data
*) arg
, 285 sizeof(struct i2c_smbus_ioctl_data
))) 287 if((data_arg
.size
!= I2C_SMBUS_BYTE
) && 288 (data_arg
.size
!= I2C_SMBUS_QUICK
) && 289 (data_arg
.size
!= I2C_SMBUS_BYTE_DATA
) && 290 (data_arg
.size
!= I2C_SMBUS_WORD_DATA
) && 291 (data_arg
.size
!= I2C_SMBUS_PROC_CALL
) && 292 (data_arg
.size
!= I2C_SMBUS_BLOCK_DATA
)) { 294 printk("i2c-dev.o: size out of range (%x) in ioctl I2C_SMBUS.\n", 299 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1, 300 so the check is valid if size==I2C_SMBUS_QUICK too. */ 301 if((data_arg
.read_write
!= I2C_SMBUS_READ
) && 302 (data_arg
.read_write
!= I2C_SMBUS_WRITE
)) { 304 printk("i2c-dev.o: read_write out of range (%x) in ioctl I2C_SMBUS.\n", 305 data_arg
.read_write
); 310 /* Note that command values are always valid! */ 312 if((data_arg
.size
== I2C_SMBUS_QUICK
) || 313 ((data_arg
.size
== I2C_SMBUS_BYTE
) && 314 (data_arg
.read_write
== I2C_SMBUS_WRITE
))) 315 /* These are special: we do not use data */ 316 returni2c_smbus_xfer(client
->adapter
, client
->addr
, 320 data_arg
.size
, NULL
); 322 if(data_arg
.data
== NULL
) { 324 printk("i2c-dev.o: 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 == I2C_SMBUS_BLOCK_DATA */ 336 datasize
=sizeof(data_arg
.data
->block
); 338 if((data_arg
.size
== I2C_SMBUS_PROC_CALL
) || 339 (data_arg
.read_write
== I2C_SMBUS_WRITE
)) { 340 if(copy_from_user(&temp
, data_arg
.data
, datasize
)) 343 res
=i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
, 345 data_arg
.command
,data_arg
.size
,&temp
); 346 if(! res
&& ((data_arg
.size
== I2C_SMBUS_PROC_CALL
) || 347 (data_arg
.read_write
== I2C_SMBUS_READ
))) { 348 if(copy_to_user(data_arg
.data
, &temp
, datasize
)) 354 returni2c_control(client
,cmd
,arg
); 359 inti2cdev_open(struct inode
*inode
,struct file
*file
) 361 unsigned int minor
=MINOR(inode
->i_rdev
); 362 struct i2c_client
*client
; 364 if((minor
>= I2CDEV_ADAPS_MAX
) || ! (i2cdev_adaps
[minor
])) { 366 printk("i2c-dev.o: Trying to open unattached adapter i2c-%d\n", 372 /* Note that we here allocate a client for later use, but we will *not* 373 register this client! Yes, this is safe. No, it is not very clean. */ 374 if(! (client
=kmalloc(sizeof(struct i2c_client
),GFP_KERNEL
))) 376 memcpy(client
,&i2cdev_client_template
,sizeof(struct i2c_client
)); 377 client
->adapter
= i2cdev_adaps
[minor
]; 378 file
->private_data
= client
; 380 if(i2cdev_adaps
[minor
]->inc_use
) 381 i2cdev_adaps
[minor
]->inc_use(i2cdev_adaps
[minor
]); 384 printk("i2c-dev.o: opened i2c-%d\n",minor
); 389 static inti2cdev_release(struct inode
*inode
,struct file
*file
) 391 unsigned int minor
=MINOR(inode
->i_rdev
); 392 kfree(file
->private_data
); 393 file
->private_data
=NULL
; 395 printk("i2c-dev.o: Closed: i2c-%d\n", minor
); 398 if(i2cdev_adaps
[minor
]->dec_use
) 399 i2cdev_adaps
[minor
]->dec_use(i2cdev_adaps
[minor
]); 404 inti2cdev_attach_adapter(struct i2c_adapter
*adap
) 408 if((i
=i2c_adapter_id(adap
)) <0) { 409 printk("i2c-dev.o: Unknown adapter ?!?\n"); 412 if(i
>= I2CDEV_ADAPS_MAX
) { 413 printk("i2c-dev.o: Adapter number too large?!? (%d)\n",i
); 417 if(! i2cdev_adaps
[i
]) { 418 i2cdev_adaps
[i
] = adap
; 419 printk("i2c-dev.o: Registered '%s' as minor %d\n",adap
->name
,i
); 421 /* This is actually a detach_adapter call! */ 422 i2cdev_adaps
[i
] = NULL
; 424 printk("i2c-dev.o: Adapter unregistered: %s\n",adap
->name
); 431 inti2cdev_detach_client(struct i2c_client
*client
) 436 static inti2cdev_command(struct i2c_client
*client
,unsigned int cmd
, 442 int __init
i2c_dev_init(void) 446 printk("i2c-dev.o: i2c /dev entries driver module\n"); 448 i2cdev_initialized
=0; 449 if(register_chrdev(I2C_MAJOR
,"i2c",&i2cdev_fops
)) { 450 printk("i2c-dev.o: unable to get major %d for i2c bus\n", 454 i2cdev_initialized
++; 456 if((res
=i2c_add_driver(&i2cdev_driver
))) { 457 printk("i2c-dev.o: Driver registration failed, module not inserted.\n"); 461 i2cdev_initialized
++; 465 inti2cdev_cleanup(void) 469 if(i2cdev_initialized
>=2) { 470 if((res
=i2c_del_driver(&i2cdev_driver
))) { 471 printk("i2c-dev.o: Driver deregistration failed, " 472 "module not removed.\n"); 475 i2cdev_initialized
++; 478 if(i2cdev_initialized
>=1) { 479 if((res
=unregister_chrdev(I2C_MAJOR
,"i2c"))) { 480 printk("i2c-dev.o: unable to release major %d for i2c bus\n", 484 i2cdev_initialized
--; 493 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and Simon G. Vogl <simon@tk.uni-linz.ac.at>"); 494 MODULE_DESCRIPTION("I2C /dev entries driver"); 498 returni2c_dev_init(); 501 intcleanup_module(void) 503 returni2cdev_cleanup(); 506 #endif/* def MODULE */