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 extern int init_module(void);
48 extern int cleanup_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 int i2cdev_ioctl (struct inode
*inode
, struct file
*file
,
61 unsigned int cmd
, unsigned long arg
);
62 static int i2cdev_open (struct inode
*inode
, struct file
*file
);
64 static int i2cdev_release (struct inode
*inode
, struct file
*file
);
66 static int i2cdev_attach_adapter(struct i2c_adapter
*adap
);
67 static int i2cdev_detach_client(struct i2c_client
*client
);
68 static int i2cdev_command(struct i2c_client
*client
, unsigned int cmd
,
76 int __init
i2c_dev_init(void);
77 static int i2cdev_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 int i2cdev_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 copy_from_user_ret(&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 copy_from_user_ret(&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 return i2c_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 copy_from_user_ret(&temp
,data_arg
.data
,datasize
,
342 res
= i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
344 data_arg
.command
,data_arg
.size
,&temp
);
345 if (! res
&& ((data_arg
.size
== I2C_SMBUS_PROC_CALL
) ||
346 (data_arg
.read_write
== I2C_SMBUS_READ
)))
347 copy_to_user_ret(data_arg
.data
,&temp
,datasize
,-EFAULT
);
351 return i2c_control(client
,cmd
,arg
);
356 int i2cdev_open (struct inode
*inode
, struct file
*file
)
358 unsigned int minor
= MINOR(inode
->i_rdev
);
359 struct i2c_client
*client
;
361 if ((minor
>= I2CDEV_ADAPS_MAX
) || ! (i2cdev_adaps
[minor
])) {
363 printk("i2c-dev.o: Trying to open unattached adapter i2c-%d\n",
369 /* Note that we here allocate a client for later use, but we will *not*
370 register this client! Yes, this is safe. No, it is not very clean. */
371 if(! (client
= kmalloc(sizeof(struct i2c_client
),GFP_KERNEL
)))
373 memcpy(client
,&i2cdev_client_template
,sizeof(struct i2c_client
));
374 client
->adapter
= i2cdev_adaps
[minor
];
375 file
->private_data
= client
;
377 if (i2cdev_adaps
[minor
]->inc_use
)
378 i2cdev_adaps
[minor
]->inc_use(i2cdev_adaps
[minor
]);
381 printk("i2c-dev.o: opened i2c-%d\n",minor
);
386 static int i2cdev_release (struct inode
*inode
, struct file
*file
)
388 unsigned int minor
= MINOR(inode
->i_rdev
);
389 kfree(file
->private_data
);
390 file
->private_data
=NULL
;
392 printk("i2c-dev.o: Closed: i2c-%d\n", minor
);
395 if (i2cdev_adaps
[minor
]->dec_use
)
396 i2cdev_adaps
[minor
]->dec_use(i2cdev_adaps
[minor
]);
401 int i2cdev_attach_adapter(struct i2c_adapter
*adap
)
405 if ((i
= i2c_adapter_id(adap
)) < 0) {
406 printk("i2c-dev.o: Unknown adapter ?!?\n");
409 if (i
>= I2CDEV_ADAPS_MAX
) {
410 printk("i2c-dev.o: Adapter number too large?!? (%d)\n",i
);
414 if (! i2cdev_adaps
[i
]) {
415 i2cdev_adaps
[i
] = adap
;
416 printk("i2c-dev.o: Registered '%s' as minor %d\n",adap
->name
,i
);
418 /* This is actually a detach_adapter call! */
419 i2cdev_adaps
[i
] = NULL
;
421 printk("i2c-dev.o: Adapter unregistered: %s\n",adap
->name
);
428 int i2cdev_detach_client(struct i2c_client
*client
)
433 static int i2cdev_command(struct i2c_client
*client
, unsigned int cmd
,
439 int __init
i2c_dev_init(void)
443 printk("i2c-dev.o: i2c /dev entries driver module\n");
445 i2cdev_initialized
= 0;
446 if (register_chrdev(I2C_MAJOR
,"i2c",&i2cdev_fops
)) {
447 printk("i2c-dev.o: unable to get major %d for i2c bus\n",
451 i2cdev_initialized
++;
453 if ((res
= i2c_add_driver(&i2cdev_driver
))) {
454 printk("i2c-dev.o: Driver registration failed, module not inserted.\n");
458 i2cdev_initialized
++;
462 int i2cdev_cleanup(void)
466 if (i2cdev_initialized
>= 2) {
467 if ((res
= i2c_del_driver(&i2cdev_driver
))) {
468 printk("i2c-dev.o: Driver deregistration failed, "
469 "module not removed.\n");
472 i2cdev_initialized
++;
475 if (i2cdev_initialized
>= 1) {
476 if ((res
= unregister_chrdev(I2C_MAJOR
,"i2c"))) {
477 printk("i2c-dev.o: unable to release major %d for i2c bus\n",
481 i2cdev_initialized
--;
490 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and Simon G. Vogl <simon@tk.uni-linz.ac.at>");
491 MODULE_DESCRIPTION("I2C /dev entries driver");
493 int init_module(void)
495 return i2c_dev_init();
498 int cleanup_module(void)
500 return i2cdev_cleanup();
503 #endif /* def MODULE */