1 /*
2     i2c-dev.c - i2c-bus driver, char device interface
3 
4     Copyright (C) 1995-97 Simon G. Vogl
5     Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
6 
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.
11 
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.
16 
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.
20 */
21 
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 */
25 
26 /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
27 
28 /* The devfs code is contributed by Philipp Matthias Hahn
29    <pmhahn@titan.lahn.de> */
30 
31 /* $Id: i2c-dev.c,v 1.40 2001/08/25 01:28:01 mds Exp $ */
32 
33 #include <linux/config.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <linux/smp_lock.h>
39 #ifdef CONFIG_DEVFS_FS
40 #include <linux/devfs_fs_kernel.h>
41 #endif
42 #include <linux/init.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-dev.h>
45 #include <asm/uaccess.h>
46 
47 /* If you want debugging uncomment: */
48 /* #define DEBUG */
49 
50 
51 #ifdef MODULE
52 extern int init_module(void);
53 extern int cleanup_module(void);
54 #endif /* def MODULE */
55 
56 /* struct file_operations changed too often in the 2.1 series for nice code */
57 
58 static ssize_t i2cdev_read (struct file *file, char *buf, size_t count,
59                             loff_t *offset);
60 static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count,
61                              loff_t *offset);
62 
63 static int i2cdev_ioctl (struct inode *inode, struct file *file,
64                          unsigned int cmd, unsigned long arg);
65 static int i2cdev_open (struct inode *inode, struct file *file);
66 
67 static int i2cdev_release (struct inode *inode, struct file *file);
68 
69 static int i2cdev_attach_adapter(struct i2c_adapter *adap);
70 static int i2cdev_detach_client(struct i2c_client *client);
71 static int i2cdev_command(struct i2c_client *client, unsigned int cmd,
72                            void *arg);
73 
74 #ifdef MODULE
75 static
76 #else
77 extern
78 #endif
79        int __init i2c_dev_init(void);
80 static int i2cdev_cleanup(void);
81 
82 static struct file_operations i2cdev_fops = {
83 	.owner		= THIS_MODULE,
84 	.llseek		= no_llseek,
85 	.read		= i2cdev_read,
86 	.write		= i2cdev_write,
87 	.ioctl		= i2cdev_ioctl,
88 	.open		= i2cdev_open,
89 	.release	= i2cdev_release,
90 };
91 
92 #define I2CDEV_ADAPS_MAX I2C_ADAP_MAX
93 static struct i2c_adapter *i2cdev_adaps[I2CDEV_ADAPS_MAX];
94 #ifdef CONFIG_DEVFS_FS
95 static devfs_handle_t devfs_i2c[I2CDEV_ADAPS_MAX];
96 static devfs_handle_t devfs_handle = NULL;
97 #endif
98 
99 static struct i2c_driver i2cdev_driver = {
100 	.name		= "i2c-dev dummy driver",
101 	.id		= I2C_DRIVERID_I2CDEV,
102 	.flags		= I2C_DF_DUMMY,
103 	.attach_adapter	= i2cdev_attach_adapter,
104 	.detach_client	= i2cdev_detach_client,
105 	.command	= i2cdev_command,
106 };
107 
108 static struct i2c_client i2cdev_client_template = {
109 	.name		= "I2C /dev entry",
110 	.id		= 1,
111 	.flags		= 0,
112 	.addr		= -1,
113 	.driver		= &i2cdev_driver,
114 };
115 
116 static int i2cdev_initialized;
117 
i2cdev_read(struct file * file,char * buf,size_t count,loff_t * offset)118 static ssize_t i2cdev_read (struct file *file, char *buf, size_t count,
119                             loff_t *offset)
120 {
121 	char *tmp;
122 	int ret;
123 
124 #ifdef DEBUG
125 	struct inode *inode = file->f_dentry->d_inode;
126 #endif /* DEBUG */
127 
128 	struct i2c_client *client = (struct i2c_client *)file->private_data;
129 
130 	if (count > 8192)
131 		count = 8192;
132 
133 	/* copy user space data to kernel space. */
134 	tmp = kmalloc(count,GFP_KERNEL);
135 	if (tmp==NULL)
136 		return -ENOMEM;
137 
138 #ifdef DEBUG
139 	printk(KERN_DEBUG "i2c-dev.o: i2c-%d reading %d bytes.\n",MINOR(inode->i_rdev),
140 	       count);
141 #endif
142 
143 	ret = i2c_master_recv(client,tmp,count);
144 	if (ret >= 0)
145 		ret = copy_to_user(buf,tmp,count)?-EFAULT:ret;
146 	kfree(tmp);
147 	return ret;
148 }
149 
i2cdev_write(struct file * file,const char * buf,size_t count,loff_t * offset)150 static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count,
151                              loff_t *offset)
152 {
153 	int ret;
154 	char *tmp;
155 	struct i2c_client *client = (struct i2c_client *)file->private_data;
156 
157 #ifdef DEBUG
158 	struct inode *inode = file->f_dentry->d_inode;
159 #endif /* DEBUG */
160 
161 	if (count > 8192)
162 		count = 8192;
163 
164 	/* copy user space data to kernel space. */
165 	tmp = kmalloc(count,GFP_KERNEL);
166 	if (tmp==NULL)
167 		return -ENOMEM;
168 	if (copy_from_user(tmp,buf,count)) {
169 		kfree(tmp);
170 		return -EFAULT;
171 	}
172 
173 #ifdef DEBUG
174 	printk(KERN_DEBUG "i2c-dev.o: i2c-%d writing %d bytes.\n",MINOR(inode->i_rdev),
175 	       count);
176 #endif
177 	ret = i2c_master_send(client,tmp,count);
178 	kfree(tmp);
179 	return ret;
180 }
181 
i2cdev_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)182 int i2cdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
183                   unsigned long arg)
184 {
185 	struct i2c_client *client = (struct i2c_client *)file->private_data;
186 	struct i2c_rdwr_ioctl_data rdwr_arg;
187 	struct i2c_smbus_ioctl_data data_arg;
188 	union i2c_smbus_data temp;
189 	struct i2c_msg *rdwr_pa;
190 	u8 **data_ptrs;
191 	int i,datasize,res;
192 	unsigned long funcs;
193 
194 #ifdef DEBUG
195 	printk(KERN_DEBUG "i2c-dev.o: i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n",
196 	       MINOR(inode->i_rdev),cmd, arg);
197 #endif /* DEBUG */
198 
199 	switch ( cmd ) {
200 	case I2C_SLAVE:
201 	case I2C_SLAVE_FORCE:
202 		if ((arg > 0x3ff) ||
203 		    (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
204 			return -EINVAL;
205 		if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))
206 			return -EBUSY;
207 		client->addr = arg;
208 		return 0;
209 	case I2C_TENBIT:
210 		if (arg)
211 			client->flags |= I2C_M_TEN;
212 		else
213 			client->flags &= ~I2C_M_TEN;
214 		return 0;
215 	case I2C_FUNCS:
216 		funcs = i2c_get_functionality(client->adapter);
217 		return (copy_to_user((unsigned long *)arg,&funcs,
218 		                     sizeof(unsigned long)))?-EFAULT:0;
219 
220 	case I2C_RDWR:
221 		if (copy_from_user(&rdwr_arg,
222 				   (struct i2c_rdwr_ioctl_data *)arg,
223 				   sizeof(rdwr_arg)))
224 			return -EFAULT;
225 
226 		/* Put an arbitrary limit on the number of messages that can
227 		 * be sent at once */
228 		if (rdwr_arg.nmsgs > 42)
229 			return -EINVAL;
230 
231 		rdwr_pa = (struct i2c_msg *)
232 			kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
233 			GFP_KERNEL);
234 
235 		if (rdwr_pa == NULL) return -ENOMEM;
236 
237 		if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
238 				   rdwr_arg.nmsgs * sizeof(struct i2c_msg))) {
239 			kfree(rdwr_pa);
240 			return -EFAULT;
241 		}
242 
243 		data_ptrs = (u8 **) kmalloc(rdwr_arg.nmsgs * sizeof(u8 *),
244 					    GFP_KERNEL);
245 		if (data_ptrs == NULL) {
246 			kfree(rdwr_pa);
247 			return -ENOMEM;
248 		}
249 
250 		res = 0;
251 		for( i=0; i<rdwr_arg.nmsgs; i++ )
252 		{
253 			/* Limit the size of the message to a sane amount */
254 			if (rdwr_pa[i].len > 8192) {
255 				res = -EINVAL;
256 				break;
257 			}
258 			data_ptrs[i] = rdwr_pa[i].buf;
259 			rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
260 			if(rdwr_pa[i].buf == NULL)
261 			{
262 				res = -ENOMEM;
263 				break;
264 			}
265 			if(copy_from_user(rdwr_pa[i].buf,
266 				data_ptrs[i],
267 				rdwr_pa[i].len))
268 			{
269 				++i; /* Needs to be kfreed too */
270 				res = -EFAULT;
271 				break;
272 			}
273 		}
274 		if (res < 0) {
275 			int j;
276 			for (j = 0; j < i; ++j)
277 				kfree(rdwr_pa[j].buf);
278 			kfree(data_ptrs);
279 			kfree(rdwr_pa);
280 			return res;
281 		}
282 
283 		res = i2c_transfer(client->adapter,
284 			rdwr_pa,
285 			rdwr_arg.nmsgs);
286 		while(i-- > 0)
287 		{
288 			if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD))
289 			{
290 				if(copy_to_user(
291 					data_ptrs[i],
292 					rdwr_pa[i].buf,
293 					rdwr_pa[i].len))
294 				{
295 					res = -EFAULT;
296 				}
297 			}
298 			kfree(rdwr_pa[i].buf);
299 		}
300 		kfree(data_ptrs);
301 		kfree(rdwr_pa);
302 		return res;
303 
304 	case I2C_SMBUS:
305 		if (copy_from_user(&data_arg,
306 		                   (struct i2c_smbus_ioctl_data *) arg,
307 		                   sizeof(struct i2c_smbus_ioctl_data)))
308 			return -EFAULT;
309 		if ((data_arg.size != I2C_SMBUS_BYTE) &&
310 		    (data_arg.size != I2C_SMBUS_QUICK) &&
311 		    (data_arg.size != I2C_SMBUS_BYTE_DATA) &&
312 		    (data_arg.size != I2C_SMBUS_WORD_DATA) &&
313 		    (data_arg.size != I2C_SMBUS_PROC_CALL) &&
314 		    (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
315 		    (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA)) {
316 #ifdef DEBUG
317 			printk(KERN_DEBUG "i2c-dev.o: size out of range (%x) in ioctl I2C_SMBUS.\n",
318 			       data_arg.size);
319 #endif
320 			return -EINVAL;
321 		}
322 		/* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
323 		   so the check is valid if size==I2C_SMBUS_QUICK too. */
324 		if ((data_arg.read_write != I2C_SMBUS_READ) &&
325 		    (data_arg.read_write != I2C_SMBUS_WRITE)) {
326 #ifdef DEBUG
327 			printk(KERN_DEBUG "i2c-dev.o: read_write out of range (%x) in ioctl I2C_SMBUS.\n",
328 			       data_arg.read_write);
329 #endif
330 			return -EINVAL;
331 		}
332 
333 		/* Note that command values are always valid! */
334 
335 		if ((data_arg.size == I2C_SMBUS_QUICK) ||
336 		    ((data_arg.size == I2C_SMBUS_BYTE) &&
337 		    (data_arg.read_write == I2C_SMBUS_WRITE)))
338 			/* These are special: we do not use data */
339 			return i2c_smbus_xfer(client->adapter, client->addr,
340 			                      client->flags,
341 			                      data_arg.read_write,
342 			                      data_arg.command,
343 			                      data_arg.size, NULL);
344 
345 		if (data_arg.data == NULL) {
346 #ifdef DEBUG
347 			printk(KERN_DEBUG "i2c-dev.o: data is NULL pointer in ioctl I2C_SMBUS.\n");
348 #endif
349 			return -EINVAL;
350 		}
351 
352 		if ((data_arg.size == I2C_SMBUS_BYTE_DATA) ||
353 		    (data_arg.size == I2C_SMBUS_BYTE))
354 			datasize = sizeof(data_arg.data->byte);
355 		else if ((data_arg.size == I2C_SMBUS_WORD_DATA) ||
356 		         (data_arg.size == I2C_SMBUS_PROC_CALL))
357 			datasize = sizeof(data_arg.data->word);
358 		else /* size == I2C_SMBUS_BLOCK_DATA */
359 			datasize = sizeof(data_arg.data->block);
360 
361 		if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
362 		    (data_arg.read_write == I2C_SMBUS_WRITE)) {
363 			if (copy_from_user(&temp, data_arg.data, datasize))
364 				return -EFAULT;
365 		}
366 		res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
367 		      data_arg.read_write,
368 		      data_arg.command,data_arg.size,&temp);
369 		if (! res && ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
370 			      (data_arg.read_write == I2C_SMBUS_READ))) {
371 			if (copy_to_user(data_arg.data, &temp, datasize))
372 				return -EFAULT;
373 		}
374 		return res;
375 
376 	default:
377 		return i2c_control(client,cmd,arg);
378 	}
379 	return 0;
380 }
381 
i2cdev_open(struct inode * inode,struct file * file)382 int i2cdev_open (struct inode *inode, struct file *file)
383 {
384 	unsigned int minor = MINOR(inode->i_rdev);
385 	struct i2c_client *client;
386 
387 	if ((minor >= I2CDEV_ADAPS_MAX) || ! (i2cdev_adaps[minor])) {
388 #ifdef DEBUG
389 		printk(KERN_DEBUG "i2c-dev.o: Trying to open unattached adapter i2c-%d\n",
390 		       minor);
391 #endif
392 		return -ENODEV;
393 	}
394 
395 	/* Note that we here allocate a client for later use, but we will *not*
396 	   register this client! Yes, this is safe. No, it is not very clean. */
397 	if(! (client = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
398 		return -ENOMEM;
399 	memcpy(client,&i2cdev_client_template,sizeof(struct i2c_client));
400 	client->adapter = i2cdev_adaps[minor];
401 	file->private_data = client;
402 
403 	if (i2cdev_adaps[minor]->inc_use)
404 		i2cdev_adaps[minor]->inc_use(i2cdev_adaps[minor]);
405 
406 #ifdef DEBUG
407 	printk(KERN_DEBUG "i2c-dev.o: opened i2c-%d\n",minor);
408 #endif
409 	return 0;
410 }
411 
i2cdev_release(struct inode * inode,struct file * file)412 static int i2cdev_release (struct inode *inode, struct file *file)
413 {
414 	unsigned int minor = MINOR(inode->i_rdev);
415 	kfree(file->private_data);
416 	file->private_data=NULL;
417 #ifdef DEBUG
418 	printk(KERN_DEBUG "i2c-dev.o: Closed: i2c-%d\n", minor);
419 #endif
420 	lock_kernel();
421 	if (i2cdev_adaps[minor]->dec_use)
422 		i2cdev_adaps[minor]->dec_use(i2cdev_adaps[minor]);
423 	unlock_kernel();
424 	return 0;
425 }
426 
i2cdev_attach_adapter(struct i2c_adapter * adap)427 int i2cdev_attach_adapter(struct i2c_adapter *adap)
428 {
429 	int i;
430 	char name[8];
431 
432 	if ((i = i2c_adapter_id(adap)) < 0) {
433 		printk(KERN_DEBUG "i2c-dev.o: Unknown adapter ?!?\n");
434 		return -ENODEV;
435 	}
436 	if (i >= I2CDEV_ADAPS_MAX) {
437 		printk(KERN_DEBUG "i2c-dev.o: Adapter number too large?!? (%d)\n",i);
438 		return -ENODEV;
439 	}
440 
441 	sprintf (name, "%d", i);
442 	if (! i2cdev_adaps[i]) {
443 		i2cdev_adaps[i] = adap;
444 #ifdef CONFIG_DEVFS_FS
445 		devfs_i2c[i] = devfs_register (devfs_handle, name,
446 			DEVFS_FL_DEFAULT, I2C_MAJOR, i,
447 			S_IFCHR | S_IRUSR | S_IWUSR,
448 			&i2cdev_fops, NULL);
449 #endif
450 		printk(KERN_DEBUG "i2c-dev.o: Registered '%s' as minor %d\n",adap->name,i);
451 	} else {
452 		/* This is actually a detach_adapter call! */
453 #ifdef CONFIG_DEVFS_FS
454 		devfs_unregister(devfs_i2c[i]);
455 #endif
456 		i2cdev_adaps[i] = NULL;
457 #ifdef DEBUG
458 		printk(KERN_DEBUG "i2c-dev.o: Adapter unregistered: %s\n",adap->name);
459 #endif
460 	}
461 
462 	return 0;
463 }
464 
i2cdev_detach_client(struct i2c_client * client)465 int i2cdev_detach_client(struct i2c_client *client)
466 {
467 	return 0;
468 }
469 
i2cdev_command(struct i2c_client * client,unsigned int cmd,void * arg)470 static int i2cdev_command(struct i2c_client *client, unsigned int cmd,
471                            void *arg)
472 {
473 	return -1;
474 }
475 
i2c_dev_init(void)476 int __init i2c_dev_init(void)
477 {
478 	int res;
479 
480 	printk(KERN_INFO "i2c-dev.o: i2c /dev entries driver module version %s (%s)\n", I2C_VERSION, I2C_DATE);
481 
482 	i2cdev_initialized = 0;
483 #ifdef CONFIG_DEVFS_FS
484 	if (devfs_register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops)) {
485 #else
486 	if (register_chrdev(I2C_MAJOR,"i2c",&i2cdev_fops)) {
487 #endif
488 		printk(KERN_ERR "i2c-dev.o: unable to get major %d for i2c bus\n",
489 		       I2C_MAJOR);
490 		return -EIO;
491 	}
492 #ifdef CONFIG_DEVFS_FS
493 	devfs_handle = devfs_mk_dir(NULL, "i2c", NULL);
494 #endif
495 	i2cdev_initialized ++;
496 
497 	if ((res = i2c_add_driver(&i2cdev_driver))) {
498 		printk(KERN_ERR "i2c-dev.o: Driver registration failed, module not inserted.\n");
499 		i2cdev_cleanup();
500 		return res;
501 	}
502 	i2cdev_initialized ++;
503 	return 0;
504 }
505 
506 int i2cdev_cleanup(void)
507 {
508 	int res;
509 
510 	if (i2cdev_initialized >= 2) {
511 		if ((res = i2c_del_driver(&i2cdev_driver))) {
512 			printk("i2c-dev.o: Driver deregistration failed, "
513 			       "module not removed.\n");
514 			return res;
515 		}
516 	i2cdev_initialized --;
517 	}
518 
519 	if (i2cdev_initialized >= 1) {
520 #ifdef CONFIG_DEVFS_FS
521 		devfs_unregister(devfs_handle);
522 		if ((res = devfs_unregister_chrdev(I2C_MAJOR, "i2c"))) {
523 #else
524 		if ((res = unregister_chrdev(I2C_MAJOR,"i2c"))) {
525 #endif
526 			printk("i2c-dev.o: unable to release major %d for i2c bus\n",
527 			       I2C_MAJOR);
528 			return res;
529 		}
530 		i2cdev_initialized --;
531 	}
532 	return 0;
533 }
534 
535 EXPORT_NO_SYMBOLS;
536 
537 #ifdef MODULE
538 
539 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and Simon G. Vogl <simon@tk.uni-linz.ac.at>");
540 MODULE_DESCRIPTION("I2C /dev entries driver");
541 MODULE_LICENSE("GPL");
542 
543 int init_module(void)
544 {
545 	return i2c_dev_init();
546 }
547 
548 int cleanup_module(void)
549 {
550 	return i2cdev_cleanup();
551 }
552 
553 #endif /* def MODULE */
554 
555