Merge branch 'atmel'
[linux-2.6] / drivers / i2c / i2c-dev.c
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     Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
24    But I have used so much of his original code and ideas that it seems
25    only fair to recognize him as co-author -- Frodo */
26
27 /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/fs.h>
32 #include <linux/slab.h>
33 #include <linux/smp_lock.h>
34 #include <linux/init.h>
35 #include <linux/i2c.h>
36 #include <linux/i2c-dev.h>
37 #include <linux/platform_device.h>
38 #include <asm/uaccess.h>
39
40 static struct i2c_client i2cdev_client_template;
41
42 struct i2c_dev {
43         int minor;
44         struct i2c_adapter *adap;
45         struct class_device class_dev;
46         struct completion released;     /* FIXME, we need a class_device_unregister() */
47 };
48 #define to_i2c_dev(d) container_of(d, struct i2c_dev, class_dev)
49
50 #define I2C_MINORS      256
51 static struct i2c_dev *i2c_dev_array[I2C_MINORS];
52 static DEFINE_SPINLOCK(i2c_dev_array_lock);
53
54 static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
55 {
56         struct i2c_dev *i2c_dev;
57
58         spin_lock(&i2c_dev_array_lock);
59         i2c_dev = i2c_dev_array[index];
60         spin_unlock(&i2c_dev_array_lock);
61         return i2c_dev;
62 }
63
64 static struct i2c_dev *i2c_dev_get_by_adapter(struct i2c_adapter *adap)
65 {
66         struct i2c_dev *i2c_dev = NULL;
67
68         spin_lock(&i2c_dev_array_lock);
69         if ((i2c_dev_array[adap->nr]) &&
70             (i2c_dev_array[adap->nr]->adap == adap))
71                 i2c_dev = i2c_dev_array[adap->nr];
72         spin_unlock(&i2c_dev_array_lock);
73         return i2c_dev;
74 }
75
76 static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
77 {
78         struct i2c_dev *i2c_dev;
79
80         i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
81         if (!i2c_dev)
82                 return ERR_PTR(-ENOMEM);
83
84         spin_lock(&i2c_dev_array_lock);
85         if (i2c_dev_array[adap->nr]) {
86                 spin_unlock(&i2c_dev_array_lock);
87                 dev_err(&adap->dev, "i2c-dev already has a device assigned to this adapter\n");
88                 goto error;
89         }
90         i2c_dev->minor = adap->nr;
91         i2c_dev_array[adap->nr] = i2c_dev;
92         spin_unlock(&i2c_dev_array_lock);
93         return i2c_dev;
94 error:
95         kfree(i2c_dev);
96         return ERR_PTR(-ENODEV);
97 }
98
99 static void return_i2c_dev(struct i2c_dev *i2c_dev)
100 {
101         spin_lock(&i2c_dev_array_lock);
102         i2c_dev_array[i2c_dev->minor] = NULL;
103         spin_unlock(&i2c_dev_array_lock);
104 }
105
106 static ssize_t show_adapter_name(struct class_device *class_dev, char *buf)
107 {
108         struct i2c_dev *i2c_dev = to_i2c_dev(class_dev);
109         return sprintf(buf, "%s\n", i2c_dev->adap->name);
110 }
111 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
112
113 static ssize_t i2cdev_read (struct file *file, char __user *buf, size_t count,
114                             loff_t *offset)
115 {
116         char *tmp;
117         int ret;
118
119         struct i2c_client *client = (struct i2c_client *)file->private_data;
120
121         if (count > 8192)
122                 count = 8192;
123
124         tmp = kmalloc(count,GFP_KERNEL);
125         if (tmp==NULL)
126                 return -ENOMEM;
127
128         pr_debug("i2c-dev: i2c-%d reading %zd bytes.\n",
129                 iminor(file->f_dentry->d_inode), count);
130
131         ret = i2c_master_recv(client,tmp,count);
132         if (ret >= 0)
133                 ret = copy_to_user(buf,tmp,count)?-EFAULT:ret;
134         kfree(tmp);
135         return ret;
136 }
137
138 static ssize_t i2cdev_write (struct file *file, const char __user *buf, size_t count,
139                              loff_t *offset)
140 {
141         int ret;
142         char *tmp;
143         struct i2c_client *client = (struct i2c_client *)file->private_data;
144
145         if (count > 8192)
146                 count = 8192;
147
148         tmp = kmalloc(count,GFP_KERNEL);
149         if (tmp==NULL)
150                 return -ENOMEM;
151         if (copy_from_user(tmp,buf,count)) {
152                 kfree(tmp);
153                 return -EFAULT;
154         }
155
156         pr_debug("i2c-dev: i2c-%d writing %zd bytes.\n",
157                 iminor(file->f_dentry->d_inode), count);
158
159         ret = i2c_master_send(client,tmp,count);
160         kfree(tmp);
161         return ret;
162 }
163
164 static int i2cdev_ioctl(struct inode *inode, struct file *file,
165                 unsigned int cmd, unsigned long arg)
166 {
167         struct i2c_client *client = (struct i2c_client *)file->private_data;
168         struct i2c_rdwr_ioctl_data rdwr_arg;
169         struct i2c_smbus_ioctl_data data_arg;
170         union i2c_smbus_data temp;
171         struct i2c_msg *rdwr_pa;
172         u8 __user **data_ptrs;
173         int i,datasize,res;
174         unsigned long funcs;
175
176         dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
177                 cmd, arg);
178
179         switch ( cmd ) {
180         case I2C_SLAVE:
181         case I2C_SLAVE_FORCE:
182                 if ((arg > 0x3ff) || 
183                     (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
184                         return -EINVAL;
185                 if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))
186                         return -EBUSY;
187                 client->addr = arg;
188                 return 0;
189         case I2C_TENBIT:
190                 if (arg)
191                         client->flags |= I2C_M_TEN;
192                 else
193                         client->flags &= ~I2C_M_TEN;
194                 return 0;
195         case I2C_PEC:
196                 if (arg)
197                         client->flags |= I2C_CLIENT_PEC;
198                 else
199                         client->flags &= ~I2C_CLIENT_PEC;
200                 return 0;
201         case I2C_FUNCS:
202                 funcs = i2c_get_functionality(client->adapter);
203                 return (copy_to_user((unsigned long __user *)arg, &funcs,
204                                      sizeof(unsigned long)))?-EFAULT:0;
205
206         case I2C_RDWR:
207                 if (copy_from_user(&rdwr_arg, 
208                                    (struct i2c_rdwr_ioctl_data __user *)arg, 
209                                    sizeof(rdwr_arg)))
210                         return -EFAULT;
211
212                 /* Put an arbitrary limit on the number of messages that can
213                  * be sent at once */
214                 if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)
215                         return -EINVAL;
216                 
217                 rdwr_pa = (struct i2c_msg *)
218                         kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg), 
219                         GFP_KERNEL);
220
221                 if (rdwr_pa == NULL) return -ENOMEM;
222
223                 if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
224                                    rdwr_arg.nmsgs * sizeof(struct i2c_msg))) {
225                         kfree(rdwr_pa);
226                         return -EFAULT;
227                 }
228
229                 data_ptrs = kmalloc(rdwr_arg.nmsgs * sizeof(u8 __user *), GFP_KERNEL);
230                 if (data_ptrs == NULL) {
231                         kfree(rdwr_pa);
232                         return -ENOMEM;
233                 }
234
235                 res = 0;
236                 for( i=0; i<rdwr_arg.nmsgs; i++ ) {
237                         /* Limit the size of the message to a sane amount */
238                         if (rdwr_pa[i].len > 8192) {
239                                 res = -EINVAL;
240                                 break;
241                         }
242                         data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
243                         rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
244                         if(rdwr_pa[i].buf == NULL) {
245                                 res = -ENOMEM;
246                                 break;
247                         }
248                         if(copy_from_user(rdwr_pa[i].buf,
249                                 data_ptrs[i],
250                                 rdwr_pa[i].len)) {
251                                         ++i; /* Needs to be kfreed too */
252                                         res = -EFAULT;
253                                 break;
254                         }
255                 }
256                 if (res < 0) {
257                         int j;
258                         for (j = 0; j < i; ++j)
259                                 kfree(rdwr_pa[j].buf);
260                         kfree(data_ptrs);
261                         kfree(rdwr_pa);
262                         return res;
263                 }
264
265                 res = i2c_transfer(client->adapter,
266                         rdwr_pa,
267                         rdwr_arg.nmsgs);
268                 while(i-- > 0) {
269                         if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD)) {
270                                 if(copy_to_user(
271                                         data_ptrs[i],
272                                         rdwr_pa[i].buf,
273                                         rdwr_pa[i].len)) {
274                                         res = -EFAULT;
275                                 }
276                         }
277                         kfree(rdwr_pa[i].buf);
278                 }
279                 kfree(data_ptrs);
280                 kfree(rdwr_pa);
281                 return res;
282
283         case I2C_SMBUS:
284                 if (copy_from_user(&data_arg,
285                                    (struct i2c_smbus_ioctl_data __user *) arg,
286                                    sizeof(struct i2c_smbus_ioctl_data)))
287                         return -EFAULT;
288                 if ((data_arg.size != I2C_SMBUS_BYTE) && 
289                     (data_arg.size != I2C_SMBUS_QUICK) &&
290                     (data_arg.size != I2C_SMBUS_BYTE_DATA) && 
291                     (data_arg.size != I2C_SMBUS_WORD_DATA) &&
292                     (data_arg.size != I2C_SMBUS_PROC_CALL) &&
293                     (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
294                     (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) &&
295                     (data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) {
296                         dev_dbg(&client->adapter->dev,
297                                 "size out of range (%x) in ioctl I2C_SMBUS.\n",
298                                 data_arg.size);
299                         return -EINVAL;
300                 }
301                 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1, 
302                    so the check is valid if size==I2C_SMBUS_QUICK too. */
303                 if ((data_arg.read_write != I2C_SMBUS_READ) && 
304                     (data_arg.read_write != I2C_SMBUS_WRITE)) {
305                         dev_dbg(&client->adapter->dev, 
306                                 "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
307                                 data_arg.read_write);
308                         return -EINVAL;
309                 }
310
311                 /* Note that command values are always valid! */
312
313                 if ((data_arg.size == I2C_SMBUS_QUICK) ||
314                     ((data_arg.size == I2C_SMBUS_BYTE) && 
315                     (data_arg.read_write == I2C_SMBUS_WRITE)))
316                         /* These are special: we do not use data */
317                         return i2c_smbus_xfer(client->adapter, client->addr,
318                                               client->flags,
319                                               data_arg.read_write,
320                                               data_arg.command,
321                                               data_arg.size, NULL);
322
323                 if (data_arg.data == NULL) {
324                         dev_dbg(&client->adapter->dev,
325                                 "data is NULL pointer in ioctl I2C_SMBUS.\n");
326                         return -EINVAL;
327                 }
328
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 == smbus block, i2c block, or block proc. call */
336                         datasize = sizeof(data_arg.data->block);
337
338                 if ((data_arg.size == I2C_SMBUS_PROC_CALL) || 
339                     (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) || 
340                     (data_arg.read_write == I2C_SMBUS_WRITE)) {
341                         if (copy_from_user(&temp, data_arg.data, datasize))
342                                 return -EFAULT;
343                 }
344                 res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
345                       data_arg.read_write,
346                       data_arg.command,data_arg.size,&temp);
347                 if (! res && ((data_arg.size == I2C_SMBUS_PROC_CALL) || 
348                               (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) || 
349                               (data_arg.read_write == I2C_SMBUS_READ))) {
350                         if (copy_to_user(data_arg.data, &temp, datasize))
351                                 return -EFAULT;
352                 }
353                 return res;
354
355         default:
356                 return i2c_control(client,cmd,arg);
357         }
358         return 0;
359 }
360
361 static int i2cdev_open(struct inode *inode, struct file *file)
362 {
363         unsigned int minor = iminor(inode);
364         struct i2c_client *client;
365         struct i2c_adapter *adap;
366         struct i2c_dev *i2c_dev;
367
368         i2c_dev = i2c_dev_get_by_minor(minor);
369         if (!i2c_dev)
370                 return -ENODEV;
371
372         adap = i2c_get_adapter(i2c_dev->adap->nr);
373         if (!adap)
374                 return -ENODEV;
375
376         client = kmalloc(sizeof(*client), GFP_KERNEL);
377         if (!client) {
378                 i2c_put_adapter(adap);
379                 return -ENOMEM;
380         }
381         memcpy(client, &i2cdev_client_template, sizeof(*client));
382
383         /* registered with adapter, passed as client to user */
384         client->adapter = adap;
385         file->private_data = client;
386
387         return 0;
388 }
389
390 static int i2cdev_release(struct inode *inode, struct file *file)
391 {
392         struct i2c_client *client = file->private_data;
393
394         i2c_put_adapter(client->adapter);
395         kfree(client);
396         file->private_data = NULL;
397
398         return 0;
399 }
400
401 static struct file_operations i2cdev_fops = {
402         .owner          = THIS_MODULE,
403         .llseek         = no_llseek,
404         .read           = i2cdev_read,
405         .write          = i2cdev_write,
406         .ioctl          = i2cdev_ioctl,
407         .open           = i2cdev_open,
408         .release        = i2cdev_release,
409 };
410
411 static void release_i2c_dev(struct class_device *dev)
412 {
413         struct i2c_dev *i2c_dev = to_i2c_dev(dev);
414         complete(&i2c_dev->released);
415 }
416
417 static struct class i2c_dev_class = {
418         .name           = "i2c-dev",
419         .release        = &release_i2c_dev,
420 };
421
422 static int i2cdev_attach_adapter(struct i2c_adapter *adap)
423 {
424         struct i2c_dev *i2c_dev;
425         int retval;
426
427         i2c_dev = get_free_i2c_dev(adap);
428         if (IS_ERR(i2c_dev))
429                 return PTR_ERR(i2c_dev);
430
431         pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
432                  adap->name, i2c_dev->minor);
433
434         /* register this i2c device with the driver core */
435         i2c_dev->adap = adap;
436         if (adap->dev.parent == &platform_bus)
437                 i2c_dev->class_dev.dev = &adap->dev;
438         else
439                 i2c_dev->class_dev.dev = adap->dev.parent;
440         i2c_dev->class_dev.class = &i2c_dev_class;
441         i2c_dev->class_dev.devt = MKDEV(I2C_MAJOR, i2c_dev->minor);
442         snprintf(i2c_dev->class_dev.class_id, BUS_ID_SIZE, "i2c-%d", i2c_dev->minor);
443         retval = class_device_register(&i2c_dev->class_dev);
444         if (retval)
445                 goto error;
446         class_device_create_file(&i2c_dev->class_dev, &class_device_attr_name);
447         return 0;
448 error:
449         return_i2c_dev(i2c_dev);
450         kfree(i2c_dev);
451         return retval;
452 }
453
454 static int i2cdev_detach_adapter(struct i2c_adapter *adap)
455 {
456         struct i2c_dev *i2c_dev;
457
458         i2c_dev = i2c_dev_get_by_adapter(adap);
459         if (!i2c_dev)
460                 return -ENODEV;
461
462         init_completion(&i2c_dev->released);
463         return_i2c_dev(i2c_dev);
464         class_device_unregister(&i2c_dev->class_dev);
465         wait_for_completion(&i2c_dev->released);
466         kfree(i2c_dev);
467
468         pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
469         return 0;
470 }
471
472 static int i2cdev_detach_client(struct i2c_client *client)
473 {
474         return 0;
475 }
476
477 static int i2cdev_command(struct i2c_client *client, unsigned int cmd,
478                            void *arg)
479 {
480         return -1;
481 }
482
483 static struct i2c_driver i2cdev_driver = {
484         .owner          = THIS_MODULE,
485         .name           = "dev_driver",
486         .id             = I2C_DRIVERID_I2CDEV,
487         .flags          = I2C_DF_NOTIFY,
488         .attach_adapter = i2cdev_attach_adapter,
489         .detach_adapter = i2cdev_detach_adapter,
490         .detach_client  = i2cdev_detach_client,
491         .command        = i2cdev_command,
492 };
493
494 static struct i2c_client i2cdev_client_template = {
495         .name           = "I2C /dev entry",
496         .addr           = -1,
497         .driver         = &i2cdev_driver,
498 };
499
500 static int __init i2c_dev_init(void)
501 {
502         int res;
503
504         printk(KERN_INFO "i2c /dev entries driver\n");
505
506         res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);
507         if (res)
508                 goto out;
509
510         res = class_register(&i2c_dev_class);
511         if (res)
512                 goto out_unreg_chrdev;
513
514         res = i2c_add_driver(&i2cdev_driver);
515         if (res)
516                 goto out_unreg_class;
517
518         return 0;
519
520 out_unreg_class:
521         class_unregister(&i2c_dev_class);
522 out_unreg_chrdev:
523         unregister_chrdev(I2C_MAJOR, "i2c");
524 out:
525         printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
526         return res;
527 }
528
529 static void __exit i2c_dev_exit(void)
530 {
531         i2c_del_driver(&i2cdev_driver);
532         class_unregister(&i2c_dev_class);
533         unregister_chrdev(I2C_MAJOR,"i2c");
534 }
535
536 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
537                 "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
538 MODULE_DESCRIPTION("I2C /dev entries driver");
539 MODULE_LICENSE("GPL");
540
541 module_init(i2c_dev_init);
542 module_exit(i2c_dev_exit);