- pre2
[davej-history.git] / drivers / i2c / i2c-dev.c
blob0981a96c06f92747bec2b83f56a7491d08330ea5
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 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,
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 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,
69 void*arg);
71 #ifdef MODULE
72 static
73 #else
74 extern
75 #endif
76 int __init i2c_dev_init(void);
77 static inti2cdev_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 inti2cdev_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 return0;
211 case I2C_TENBIT:
212 if(arg)
213 client->flags |= I2C_M_TEN;
214 else
215 client->flags &= ~I2C_M_TEN;
216 return0;
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 if(copy_from_user(&rdwr_arg,
224 (struct i2c_rdwr_ioctl_data *)arg,
225 sizeof(rdwr_arg)))
226 return-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 if(copy_from_user(&data_arg,
284 (struct i2c_smbus_ioctl_data *) arg,
285 sizeof(struct i2c_smbus_ioctl_data)))
286 return-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 returni2c_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 if(copy_from_user(&temp, data_arg.data, datasize))
341 return-EFAULT;
343 res =i2c_smbus_xfer(client->adapter,client->addr,client->flags,
344 data_arg.read_write,
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))
349 return-EFAULT;
351 return res;
353 default:
354 returni2c_control(client,cmd,arg);
356 return0;
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])) {
365 #ifdef DEBUG
366 printk("i2c-dev.o: Trying to open unattached adapter i2c-%d\n",
367 minor);
368 #endif
369 return-ENODEV;
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)))
375 return-ENOMEM;
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]);
383 #ifdef DEBUG
384 printk("i2c-dev.o: opened i2c-%d\n",minor);
385 #endif
386 return0;
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;
394 #ifdef DEBUG
395 printk("i2c-dev.o: Closed: i2c-%d\n", minor);
396 #endif
397 lock_kernel();
398 if(i2cdev_adaps[minor]->dec_use)
399 i2cdev_adaps[minor]->dec_use(i2cdev_adaps[minor]);
400 unlock_kernel();
401 return0;
404 inti2cdev_attach_adapter(struct i2c_adapter *adap)
406 int i;
408 if((i =i2c_adapter_id(adap)) <0) {
409 printk("i2c-dev.o: Unknown adapter ?!?\n");
410 return-ENODEV;
412 if(i >= I2CDEV_ADAPS_MAX) {
413 printk("i2c-dev.o: Adapter number too large?!? (%d)\n",i);
414 return-ENODEV;
417 if(! i2cdev_adaps[i]) {
418 i2cdev_adaps[i] = adap;
419 printk("i2c-dev.o: Registered '%s' as minor %d\n",adap->name,i);
420 }else{
421 /* This is actually a detach_adapter call! */
422 i2cdev_adaps[i] = NULL;
423 #ifdef DEBUG
424 printk("i2c-dev.o: Adapter unregistered: %s\n",adap->name);
425 #endif
428 return0;
431 inti2cdev_detach_client(struct i2c_client *client)
433 return0;
436 static inti2cdev_command(struct i2c_client *client,unsigned int cmd,
437 void*arg)
439 return-1;
442 int __init i2c_dev_init(void)
444 int res;
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",
451 I2C_MAJOR);
452 return-EIO;
454 i2cdev_initialized ++;
456 if((res =i2c_add_driver(&i2cdev_driver))) {
457 printk("i2c-dev.o: Driver registration failed, module not inserted.\n");
458 i2cdev_cleanup();
459 return res;
461 i2cdev_initialized ++;
462 return0;
465 inti2cdev_cleanup(void)
467 int res;
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");
473 return res;
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",
481 I2C_MAJOR);
482 return res;
484 i2cdev_initialized --;
486 return0;
489 EXPORT_NO_SYMBOLS;
491 #ifdef MODULE
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");
496 intinit_module(void)
498 returni2c_dev_init();
501 intcleanup_module(void)
503 returni2cdev_cleanup();
506 #endif/* def MODULE */
close