Import 2.4.0-test5pre5
[davej-history.git] / drivers / i2c / i2c-dev.c
blobcec43ae51c3bb3ed7c976c3f1ee8b9bfc500a59d
1 /*
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>
32 #include <linux/fs.h>
33 #include <linux/malloc.h>
34 #include <linux/version.h>
35 #include <linux/smp_lock.h>
37 /* If you want debugging uncomment: */
38 /* #define DEBUG */
40 #include <linux/init.h>
41 #include <asm/uaccess.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-dev.h>
46 #ifdef MODULE
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,
56 loff_t *offset);
57 static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count,
58 loff_t *offset);
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,
69 void *arg);
71 #ifdef MODULE
72 static
73 #else
74 extern
75 #endif
76 int __init i2c_dev_init(void);
77 static int i2cdev_cleanup(void);
79 static struct file_operations i2cdev_fops = {
80 owner: THIS_MODULE,
81 llseek: i2cdev_lseek,
82 read: i2cdev_read,
83 write: i2cdev_write,
84 ioctl: i2cdev_ioctl,
85 open: i2cdev_open,
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,
99 /* inc_use */ NULL,
100 /* dec_use */ NULL,
103 static struct i2c_client i2cdev_client_template = {
104 /* name */ "I2C /dev entry",
105 /* id */ 1,
106 /* flags */ 0,
107 /* addr */ -1,
108 /* adapter */ NULL,
109 /* driver */ &i2cdev_driver,
110 /* data */ NULL
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)
119 #ifdef DEBUG
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);
123 #endif /* DEBUG */
124 return -ESPIPE;
127 static ssize_t i2cdev_read (struct file *file, char *buf, size_t count,
128 loff_t *offset)
130 char *tmp;
131 int ret;
133 #ifdef DEBUG
134 struct inode *inode = file->f_dentry->d_inode;
135 #endif /* DEBUG */
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);
141 if (tmp==NULL)
142 return -ENOMEM;
144 #ifdef DEBUG
145 printk("i2c-dev,o: i2c-%d reading %d bytes.\n",MINOR(inode->i_rdev),
146 count);
147 #endif
149 ret = i2c_master_recv(client,tmp,count);
150 if (ret >= 0)
151 ret = copy_to_user(buf,tmp,count)?-EFAULT:ret;
152 kfree(tmp);
153 return ret;
156 static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count,
157 loff_t *offset)
159 int ret;
160 char *tmp;
161 struct i2c_client *client = (struct i2c_client *)file->private_data;
163 #ifdef DEBUG
164 struct inode *inode = file->f_dentry->d_inode;
165 #endif /* DEBUG */
167 /* copy user space data to kernel space. */
168 tmp = kmalloc(count,GFP_KERNEL);
169 if (tmp==NULL)
170 return -ENOMEM;
171 if (copy_from_user(tmp,buf,count)) {
172 kfree(tmp);
173 return -EFAULT;
176 #ifdef DEBUG
177 printk("i2c-dev,o: i2c-%d writing %d bytes.\n",MINOR(inode->i_rdev),
178 count);
179 #endif
180 ret = i2c_master_send(client,tmp,count);
181 kfree(tmp);
182 return ret;
185 int i2cdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
186 unsigned long arg)
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;
193 int i,datasize,res;
194 unsigned long funcs;
196 #ifdef DEBUG
197 printk("i2c-dev.o: i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n",
198 MINOR(inode->i_rdev),cmd, arg);
199 #endif /* DEBUG */
201 switch ( cmd ) {
202 case I2C_SLAVE:
203 case I2C_SLAVE_FORCE:
204 if ((arg > 0x3ff) ||
205 (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
206 return -EINVAL;
207 if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))
208 return -EBUSY;
209 client->addr = arg;
210 return 0;
211 case I2C_TENBIT:
212 if (arg)
213 client->flags |= I2C_M_TEN;
214 else
215 client->flags &= ~I2C_M_TEN;
216 return 0;
217 case I2C_FUNCS:
218 funcs = i2c_get_functionality(client->adapter);
219 return (copy_to_user((unsigned long *)arg,&funcs,
220 sizeof(unsigned long)))?-EFAULT:0;
222 case I2C_RDWR:
223 copy_from_user_ret(&rdwr_arg,
224 (struct i2c_rdwr_ioctl_data *)arg,
225 sizeof(rdwr_arg),
226 -EFAULT);
228 rdwr_pa = (struct i2c_msg *)
229 kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
230 GFP_KERNEL);
232 if (rdwr_pa == NULL) return -ENOMEM;
234 res = 0;
235 for( i=0; i<rdwr_arg.nmsgs; i++ )
237 if(copy_from_user(&(rdwr_pa[i]),
238 &(rdwr_arg.msgs[i]),
239 sizeof(rdwr_pa[i])))
241 res = -EFAULT;
242 break;
244 rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
245 if(rdwr_pa[i].buf == NULL)
247 res = -ENOMEM;
248 break;
250 if(copy_from_user(rdwr_pa[i].buf,
251 rdwr_arg.msgs[i].buf,
252 rdwr_pa[i].len))
254 kfree(rdwr_pa[i].buf);
255 res = -EFAULT;
256 break;
259 if (!res)
261 res = i2c_transfer(client->adapter,
262 rdwr_pa,
263 rdwr_arg.nmsgs);
265 while(i-- > 0)
267 if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD))
269 if(copy_to_user(
270 rdwr_arg.msgs[i].buf,
271 rdwr_pa[i].buf,
272 rdwr_pa[i].len))
274 res = -EFAULT;
277 kfree(rdwr_pa[i].buf);
279 kfree(rdwr_pa);
280 return res;
282 case I2C_SMBUS:
283 copy_from_user_ret(&data_arg,
284 (struct i2c_smbus_ioctl_data *) arg,
285 sizeof(struct i2c_smbus_ioctl_data),
286 -EFAULT);
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)) {
293 #ifdef DEBUG
294 printk("i2c-dev.o: size out of range (%x) in ioctl I2C_SMBUS.\n",
295 data_arg.size);
296 #endif
297 return -EINVAL;
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)) {
303 #ifdef DEBUG
304 printk("i2c-dev.o: read_write out of range (%x) in ioctl I2C_SMBUS.\n",
305 data_arg.read_write);
306 #endif
307 return -EINVAL;
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,
317 client->flags,
318 data_arg.read_write,
319 data_arg.command,
320 data_arg.size, NULL);
322 if (data_arg.data == NULL) {
323 #ifdef DEBUG
324 printk("i2c-dev.o: data is NULL pointer in ioctl I2C_SMBUS.\n");
325 #endif
326 return -EINVAL;
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,
341 -EFAULT);
342 res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
343 data_arg.read_write,
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);
348 return res;
350 default:
351 return i2c_control(client,cmd,arg);
353 return 0;
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])) {
362 #ifdef DEBUG
363 printk("i2c-dev.o: Trying to open unattached adapter i2c-%d\n",
364 minor);
365 #endif
366 return -ENODEV;
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)))
372 return -ENOMEM;
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]);
380 #ifdef DEBUG
381 printk("i2c-dev.o: opened i2c-%d\n",minor);
382 #endif
383 return 0;
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;
391 #ifdef DEBUG
392 printk("i2c-dev.o: Closed: i2c-%d\n", minor);
393 #endif
394 lock_kernel();
395 if (i2cdev_adaps[minor]->dec_use)
396 i2cdev_adaps[minor]->dec_use(i2cdev_adaps[minor]);
397 unlock_kernel();
398 return 0;
401 int i2cdev_attach_adapter(struct i2c_adapter *adap)
403 int i;
405 if ((i = i2c_adapter_id(adap)) < 0) {
406 printk("i2c-dev.o: Unknown adapter ?!?\n");
407 return -ENODEV;
409 if (i >= I2CDEV_ADAPS_MAX) {
410 printk("i2c-dev.o: Adapter number too large?!? (%d)\n",i);
411 return -ENODEV;
414 if (! i2cdev_adaps[i]) {
415 i2cdev_adaps[i] = adap;
416 printk("i2c-dev.o: Registered '%s' as minor %d\n",adap->name,i);
417 } else {
418 /* This is actually a detach_adapter call! */
419 i2cdev_adaps[i] = NULL;
420 #ifdef DEBUG
421 printk("i2c-dev.o: Adapter unregistered: %s\n",adap->name);
422 #endif
425 return 0;
428 int i2cdev_detach_client(struct i2c_client *client)
430 return 0;
433 static int i2cdev_command(struct i2c_client *client, unsigned int cmd,
434 void *arg)
436 return -1;
439 int __init i2c_dev_init(void)
441 int res;
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",
448 I2C_MAJOR);
449 return -EIO;
451 i2cdev_initialized ++;
453 if ((res = i2c_add_driver(&i2cdev_driver))) {
454 printk("i2c-dev.o: Driver registration failed, module not inserted.\n");
455 i2cdev_cleanup();
456 return res;
458 i2cdev_initialized ++;
459 return 0;
462 int i2cdev_cleanup(void)
464 int res;
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");
470 return res;
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",
478 I2C_MAJOR);
479 return res;
481 i2cdev_initialized --;
483 return 0;
486 EXPORT_NO_SYMBOLS;
488 #ifdef MODULE
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 */