blob: ed7eed388bae32caec406b4daf335cab07ad9e38 [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>
Russell Kingd052d1b2005-10-29 19:07:23 +010037#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39
40static struct i2c_client i2cdev_client_template;
41
42struct i2c_dev {
43 int minor;
44 struct i2c_adapter *adap;
Greg Kroah-Hartman794721322005-12-06 15:33:15 -080045 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
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{
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800107 struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(class_dev->devt));
108
109 if (!i2c_dev)
110 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return sprintf(buf, "%s\n", i2c_dev->adap->name);
112}
113static CLASS_DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
114
115static ssize_t i2cdev_read (struct file *file, char __user *buf, size_t count,
116 loff_t *offset)
117{
118 char *tmp;
119 int ret;
120
121 struct i2c_client *client = (struct i2c_client *)file->private_data;
122
123 if (count > 8192)
124 count = 8192;
125
126 tmp = kmalloc(count,GFP_KERNEL);
127 if (tmp==NULL)
128 return -ENOMEM;
129
130 pr_debug("i2c-dev: i2c-%d reading %zd bytes.\n",
131 iminor(file->f_dentry->d_inode), count);
132
133 ret = i2c_master_recv(client,tmp,count);
134 if (ret >= 0)
135 ret = copy_to_user(buf,tmp,count)?-EFAULT:ret;
136 kfree(tmp);
137 return ret;
138}
139
140static ssize_t i2cdev_write (struct file *file, const char __user *buf, size_t count,
141 loff_t *offset)
142{
143 int ret;
144 char *tmp;
145 struct i2c_client *client = (struct i2c_client *)file->private_data;
146
147 if (count > 8192)
148 count = 8192;
149
150 tmp = kmalloc(count,GFP_KERNEL);
151 if (tmp==NULL)
152 return -ENOMEM;
153 if (copy_from_user(tmp,buf,count)) {
154 kfree(tmp);
155 return -EFAULT;
156 }
157
158 pr_debug("i2c-dev: i2c-%d writing %zd bytes.\n",
159 iminor(file->f_dentry->d_inode), count);
160
161 ret = i2c_master_send(client,tmp,count);
162 kfree(tmp);
163 return ret;
164}
165
166static int i2cdev_ioctl(struct inode *inode, struct file *file,
167 unsigned int cmd, unsigned long arg)
168{
169 struct i2c_client *client = (struct i2c_client *)file->private_data;
170 struct i2c_rdwr_ioctl_data rdwr_arg;
171 struct i2c_smbus_ioctl_data data_arg;
172 union i2c_smbus_data temp;
173 struct i2c_msg *rdwr_pa;
174 u8 __user **data_ptrs;
175 int i,datasize,res;
176 unsigned long funcs;
177
Jean Delvaree8aafcb2005-10-07 23:06:27 +0200178 dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
179 cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 switch ( cmd ) {
182 case I2C_SLAVE:
183 case I2C_SLAVE_FORCE:
184 if ((arg > 0x3ff) ||
185 (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
186 return -EINVAL;
187 if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))
188 return -EBUSY;
189 client->addr = arg;
190 return 0;
191 case I2C_TENBIT:
192 if (arg)
193 client->flags |= I2C_M_TEN;
194 else
195 client->flags &= ~I2C_M_TEN;
196 return 0;
197 case I2C_PEC:
198 if (arg)
199 client->flags |= I2C_CLIENT_PEC;
200 else
201 client->flags &= ~I2C_CLIENT_PEC;
202 return 0;
203 case I2C_FUNCS:
204 funcs = i2c_get_functionality(client->adapter);
205 return (copy_to_user((unsigned long __user *)arg, &funcs,
206 sizeof(unsigned long)))?-EFAULT:0;
207
208 case I2C_RDWR:
209 if (copy_from_user(&rdwr_arg,
210 (struct i2c_rdwr_ioctl_data __user *)arg,
211 sizeof(rdwr_arg)))
212 return -EFAULT;
213
Tobias Klauserd68a8612005-05-19 21:41:47 +0200214 /* Put an arbitrary limit on the number of messages that can
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * be sent at once */
216 if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)
217 return -EINVAL;
218
219 rdwr_pa = (struct i2c_msg *)
220 kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
221 GFP_KERNEL);
222
223 if (rdwr_pa == NULL) return -ENOMEM;
224
225 if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
226 rdwr_arg.nmsgs * sizeof(struct i2c_msg))) {
227 kfree(rdwr_pa);
228 return -EFAULT;
229 }
230
231 data_ptrs = kmalloc(rdwr_arg.nmsgs * sizeof(u8 __user *), GFP_KERNEL);
232 if (data_ptrs == NULL) {
233 kfree(rdwr_pa);
234 return -ENOMEM;
235 }
236
237 res = 0;
238 for( i=0; i<rdwr_arg.nmsgs; i++ ) {
239 /* Limit the size of the message to a sane amount */
240 if (rdwr_pa[i].len > 8192) {
241 res = -EINVAL;
242 break;
243 }
244 data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
245 rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
246 if(rdwr_pa[i].buf == NULL) {
247 res = -ENOMEM;
248 break;
249 }
250 if(copy_from_user(rdwr_pa[i].buf,
251 data_ptrs[i],
252 rdwr_pa[i].len)) {
253 ++i; /* Needs to be kfreed too */
254 res = -EFAULT;
255 break;
256 }
257 }
258 if (res < 0) {
259 int j;
260 for (j = 0; j < i; ++j)
261 kfree(rdwr_pa[j].buf);
262 kfree(data_ptrs);
263 kfree(rdwr_pa);
264 return res;
265 }
266
267 res = i2c_transfer(client->adapter,
268 rdwr_pa,
269 rdwr_arg.nmsgs);
270 while(i-- > 0) {
271 if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD)) {
272 if(copy_to_user(
273 data_ptrs[i],
274 rdwr_pa[i].buf,
275 rdwr_pa[i].len)) {
276 res = -EFAULT;
277 }
278 }
279 kfree(rdwr_pa[i].buf);
280 }
281 kfree(data_ptrs);
282 kfree(rdwr_pa);
283 return res;
284
285 case I2C_SMBUS:
286 if (copy_from_user(&data_arg,
287 (struct i2c_smbus_ioctl_data __user *) arg,
288 sizeof(struct i2c_smbus_ioctl_data)))
289 return -EFAULT;
290 if ((data_arg.size != I2C_SMBUS_BYTE) &&
291 (data_arg.size != I2C_SMBUS_QUICK) &&
292 (data_arg.size != I2C_SMBUS_BYTE_DATA) &&
293 (data_arg.size != I2C_SMBUS_WORD_DATA) &&
294 (data_arg.size != I2C_SMBUS_PROC_CALL) &&
295 (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
296 (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) &&
297 (data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) {
298 dev_dbg(&client->adapter->dev,
299 "size out of range (%x) in ioctl I2C_SMBUS.\n",
300 data_arg.size);
301 return -EINVAL;
302 }
303 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
304 so the check is valid if size==I2C_SMBUS_QUICK too. */
305 if ((data_arg.read_write != I2C_SMBUS_READ) &&
306 (data_arg.read_write != I2C_SMBUS_WRITE)) {
307 dev_dbg(&client->adapter->dev,
308 "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
309 data_arg.read_write);
310 return -EINVAL;
311 }
312
313 /* Note that command values are always valid! */
314
315 if ((data_arg.size == I2C_SMBUS_QUICK) ||
316 ((data_arg.size == I2C_SMBUS_BYTE) &&
317 (data_arg.read_write == I2C_SMBUS_WRITE)))
318 /* These are special: we do not use data */
319 return i2c_smbus_xfer(client->adapter, client->addr,
320 client->flags,
321 data_arg.read_write,
322 data_arg.command,
323 data_arg.size, NULL);
324
325 if (data_arg.data == NULL) {
326 dev_dbg(&client->adapter->dev,
327 "data is NULL pointer in ioctl I2C_SMBUS.\n");
328 return -EINVAL;
329 }
330
331 if ((data_arg.size == I2C_SMBUS_BYTE_DATA) ||
332 (data_arg.size == I2C_SMBUS_BYTE))
333 datasize = sizeof(data_arg.data->byte);
334 else if ((data_arg.size == I2C_SMBUS_WORD_DATA) ||
335 (data_arg.size == I2C_SMBUS_PROC_CALL))
336 datasize = sizeof(data_arg.data->word);
337 else /* size == smbus block, i2c block, or block proc. call */
338 datasize = sizeof(data_arg.data->block);
339
340 if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
341 (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
342 (data_arg.read_write == I2C_SMBUS_WRITE)) {
343 if (copy_from_user(&temp, data_arg.data, datasize))
344 return -EFAULT;
345 }
346 res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
347 data_arg.read_write,
348 data_arg.command,data_arg.size,&temp);
349 if (! res && ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
350 (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
351 (data_arg.read_write == I2C_SMBUS_READ))) {
352 if (copy_to_user(data_arg.data, &temp, datasize))
353 return -EFAULT;
354 }
355 return res;
356
357 default:
358 return i2c_control(client,cmd,arg);
359 }
360 return 0;
361}
362
363static int i2cdev_open(struct inode *inode, struct file *file)
364{
365 unsigned int minor = iminor(inode);
366 struct i2c_client *client;
367 struct i2c_adapter *adap;
368 struct i2c_dev *i2c_dev;
369
370 i2c_dev = i2c_dev_get_by_minor(minor);
371 if (!i2c_dev)
372 return -ENODEV;
373
374 adap = i2c_get_adapter(i2c_dev->adap->nr);
375 if (!adap)
376 return -ENODEV;
377
378 client = kmalloc(sizeof(*client), GFP_KERNEL);
379 if (!client) {
380 i2c_put_adapter(adap);
381 return -ENOMEM;
382 }
383 memcpy(client, &i2cdev_client_template, sizeof(*client));
384
385 /* registered with adapter, passed as client to user */
386 client->adapter = adap;
387 file->private_data = client;
388
389 return 0;
390}
391
392static int i2cdev_release(struct inode *inode, struct file *file)
393{
394 struct i2c_client *client = file->private_data;
395
396 i2c_put_adapter(client->adapter);
397 kfree(client);
398 file->private_data = NULL;
399
400 return 0;
401}
402
403static struct file_operations i2cdev_fops = {
404 .owner = THIS_MODULE,
405 .llseek = no_llseek,
406 .read = i2cdev_read,
407 .write = i2cdev_write,
408 .ioctl = i2cdev_ioctl,
409 .open = i2cdev_open,
410 .release = i2cdev_release,
411};
412
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800413static struct class *i2c_dev_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415static int i2cdev_attach_adapter(struct i2c_adapter *adap)
416{
417 struct i2c_dev *i2c_dev;
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800418 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 i2c_dev = get_free_i2c_dev(adap);
421 if (IS_ERR(i2c_dev))
422 return PTR_ERR(i2c_dev);
423
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200424 pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
425 adap->name, i2c_dev->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* register this i2c device with the driver core */
428 i2c_dev->adap = adap;
429 if (adap->dev.parent == &platform_bus)
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800430 dev = &adap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 else
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800432 dev = adap->dev.parent;
433 i2c_dev->class_dev = class_device_create(i2c_dev_class, NULL,
434 MKDEV(I2C_MAJOR, i2c_dev->minor),
435 dev, "i2c-%d", i2c_dev->minor);
436 if (!i2c_dev->class_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 goto error;
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800438 class_device_create_file(i2c_dev->class_dev, &class_device_attr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440error:
441 return_i2c_dev(i2c_dev);
442 kfree(i2c_dev);
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800443 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446static int i2cdev_detach_adapter(struct i2c_adapter *adap)
447{
448 struct i2c_dev *i2c_dev;
449
450 i2c_dev = i2c_dev_get_by_adapter(adap);
451 if (!i2c_dev)
452 return -ENODEV;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return_i2c_dev(i2c_dev);
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800455 class_device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, i2c_dev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 kfree(i2c_dev);
457
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200458 pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
460}
461
462static int i2cdev_detach_client(struct i2c_client *client)
463{
464 return 0;
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static struct i2c_driver i2cdev_driver = {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100468 .driver = {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100469 .name = "dev_driver",
470 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 .id = I2C_DRIVERID_I2CDEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 .attach_adapter = i2cdev_attach_adapter,
473 .detach_adapter = i2cdev_detach_adapter,
474 .detach_client = i2cdev_detach_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475};
476
477static struct i2c_client i2cdev_client_template = {
478 .name = "I2C /dev entry",
479 .addr = -1,
480 .driver = &i2cdev_driver,
481};
482
483static int __init i2c_dev_init(void)
484{
485 int res;
486
487 printk(KERN_INFO "i2c /dev entries driver\n");
488
489 res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);
490 if (res)
491 goto out;
492
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800493 i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
494 if (IS_ERR(i2c_dev_class))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto out_unreg_chrdev;
496
497 res = i2c_add_driver(&i2cdev_driver);
498 if (res)
499 goto out_unreg_class;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502
503out_unreg_class:
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800504 class_destroy(i2c_dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505out_unreg_chrdev:
506 unregister_chrdev(I2C_MAJOR, "i2c");
507out:
508 printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
509 return res;
510}
511
512static void __exit i2c_dev_exit(void)
513{
514 i2c_del_driver(&i2cdev_driver);
Greg Kroah-Hartman794721322005-12-06 15:33:15 -0800515 class_destroy(i2c_dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unregister_chrdev(I2C_MAJOR,"i2c");
517}
518
519MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
520 "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
521MODULE_DESCRIPTION("I2C /dev entries driver");
522MODULE_LICENSE("GPL");
523
524module_init(i2c_dev_init);
525module_exit(i2c_dev_exit);