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