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