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