i2c: completion header cleanups
[pandora-kernel.git] / drivers / i2c / i2c-core.c
1 /* i2c-core.c - a device driver for the iic-bus interface                    */
2 /* ------------------------------------------------------------------------- */
3 /*   Copyright (C) 1995-99 Simon G. Vogl
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
18 /* ------------------------------------------------------------------------- */
19
20 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21    All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
22    SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23    Jean Delvare <khali@linux-fr.org> */
24
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/init.h>
31 #include <linux/idr.h>
32 #include <linux/seq_file.h>
33 #include <linux/platform_device.h>
34 #include <linux/mutex.h>
35 #include <linux/completion.h>
36 #include <asm/uaccess.h>
37
38
39 static LIST_HEAD(adapters);
40 static LIST_HEAD(drivers);
41 static DEFINE_MUTEX(core_lists);
42 static DEFINE_IDR(i2c_adapter_idr);
43
44 /* match always succeeds, as we want the probe() to tell if we really accept this match */
45 static int i2c_device_match(struct device *dev, struct device_driver *drv)
46 {
47         return 1;
48 }
49
50 static int i2c_bus_suspend(struct device * dev, pm_message_t state)
51 {
52         int rc = 0;
53
54         if (dev->driver && dev->driver->suspend)
55                 rc = dev->driver->suspend(dev, state);
56         return rc;
57 }
58
59 static int i2c_bus_resume(struct device * dev)
60 {
61         int rc = 0;
62         
63         if (dev->driver && dev->driver->resume)
64                 rc = dev->driver->resume(dev);
65         return rc;
66 }
67
68 static int i2c_device_probe(struct device *dev)
69 {
70         return -ENODEV;
71 }
72
73 static int i2c_device_remove(struct device *dev)
74 {
75         return 0;
76 }
77
78 struct bus_type i2c_bus_type = {
79         .name =         "i2c",
80         .match =        i2c_device_match,
81         .probe =        i2c_device_probe,
82         .remove =       i2c_device_remove,
83         .suspend =      i2c_bus_suspend,
84         .resume =       i2c_bus_resume,
85 };
86
87 void i2c_adapter_dev_release(struct device *dev)
88 {
89         struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
90         complete(&adap->dev_released);
91 }
92
93 struct device_driver i2c_adapter_driver = {
94         .owner = THIS_MODULE,
95         .name = "i2c_adapter",
96         .bus = &i2c_bus_type,
97 };
98
99 /* ------------------------------------------------------------------------- */
100
101 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
102
103 static void i2c_adapter_class_dev_release(struct class_device *dev)
104 {
105         struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
106         complete(&adap->class_dev_released);
107 }
108
109 static ssize_t i2c_adapter_show_name(struct class_device *cdev, char *buf)
110 {
111         struct i2c_adapter *adap = class_dev_to_i2c_adapter(cdev);
112         return sprintf(buf, "%s\n", adap->name);
113 }
114
115 static struct class_device_attribute i2c_adapter_attrs[] = {
116         __ATTR(name, S_IRUGO, i2c_adapter_show_name, NULL),
117         { },
118 };
119
120 struct class i2c_adapter_class = {
121         .owner                  = THIS_MODULE,
122         .name                   = "i2c-adapter",
123         .class_dev_attrs        = i2c_adapter_attrs,
124         .release                = &i2c_adapter_class_dev_release,
125 };
126
127 static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
128 {
129         struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
130         return sprintf(buf, "%s\n", adap->name);
131 }
132 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
133
134
135 static void i2c_client_release(struct device *dev)
136 {
137         struct i2c_client *client = to_i2c_client(dev);
138         complete(&client->released);
139 }
140
141 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
142 {
143         struct i2c_client *client = to_i2c_client(dev);
144         return sprintf(buf, "%s\n", client->name);
145 }
146
147 /*
148  * We can't use the DEVICE_ATTR() macro here, as we used the same name for
149  * an i2c adapter attribute (above).
150  */
151 static struct device_attribute dev_attr_client_name =
152         __ATTR(name, S_IRUGO, &show_client_name, NULL);
153
154
155 /* ---------------------------------------------------
156  * registering functions
157  * ---------------------------------------------------
158  */
159
160 /* -----
161  * i2c_add_adapter is called from within the algorithm layer,
162  * when a new hw adapter registers. A new device is register to be
163  * available for clients.
164  */
165 int i2c_add_adapter(struct i2c_adapter *adap)
166 {
167         int id, res = 0;
168         struct list_head   *item;
169         struct i2c_driver  *driver;
170
171         mutex_lock(&core_lists);
172
173         if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
174                 res = -ENOMEM;
175                 goto out_unlock;
176         }
177
178         res = idr_get_new(&i2c_adapter_idr, adap, &id);
179         if (res < 0) {
180                 if (res == -EAGAIN)
181                         res = -ENOMEM;
182                 goto out_unlock;
183         }
184
185         adap->nr =  id & MAX_ID_MASK;
186         mutex_init(&adap->bus_lock);
187         mutex_init(&adap->clist_lock);
188         list_add_tail(&adap->list,&adapters);
189         INIT_LIST_HEAD(&adap->clients);
190
191         /* Add the adapter to the driver core.
192          * If the parent pointer is not set up,
193          * we add this adapter to the host bus.
194          */
195         if (adap->dev.parent == NULL) {
196                 adap->dev.parent = &platform_bus;
197                 printk(KERN_WARNING "**WARNING** I2C adapter driver [%s] "
198                        "forgot to specify physical device; fix it!\n",
199                        adap->name);
200         }
201         sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
202         adap->dev.driver = &i2c_adapter_driver;
203         adap->dev.release = &i2c_adapter_dev_release;
204         res = device_register(&adap->dev);
205         if (res)
206                 goto out_list;
207         res = device_create_file(&adap->dev, &dev_attr_name);
208         if (res)
209                 goto out_unregister;
210
211         /* Add this adapter to the i2c_adapter class */
212         memset(&adap->class_dev, 0x00, sizeof(struct class_device));
213         adap->class_dev.dev = &adap->dev;
214         adap->class_dev.class = &i2c_adapter_class;
215         strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
216         res = class_device_register(&adap->class_dev);
217         if (res)
218                 goto out_remove_name;
219
220         dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
221
222         /* inform drivers of new adapters */
223         list_for_each(item,&drivers) {
224                 driver = list_entry(item, struct i2c_driver, list);
225                 if (driver->attach_adapter)
226                         /* We ignore the return code; if it fails, too bad */
227                         driver->attach_adapter(adap);
228         }
229
230 out_unlock:
231         mutex_unlock(&core_lists);
232         return res;
233
234 out_remove_name:
235         device_remove_file(&adap->dev, &dev_attr_name);
236 out_unregister:
237         init_completion(&adap->dev_released); /* Needed? */
238         device_unregister(&adap->dev);
239         wait_for_completion(&adap->dev_released);
240 out_list:
241         list_del(&adap->list);
242         idr_remove(&i2c_adapter_idr, adap->nr);
243         goto out_unlock;
244 }
245
246
247 int i2c_del_adapter(struct i2c_adapter *adap)
248 {
249         struct list_head  *item, *_n;
250         struct i2c_adapter *adap_from_list;
251         struct i2c_driver *driver;
252         struct i2c_client *client;
253         int res = 0;
254
255         mutex_lock(&core_lists);
256
257         /* First make sure that this adapter was ever added */
258         list_for_each_entry(adap_from_list, &adapters, list) {
259                 if (adap_from_list == adap)
260                         break;
261         }
262         if (adap_from_list != adap) {
263                 pr_debug("i2c-core: attempting to delete unregistered "
264                          "adapter [%s]\n", adap->name);
265                 res = -EINVAL;
266                 goto out_unlock;
267         }
268
269         list_for_each(item,&drivers) {
270                 driver = list_entry(item, struct i2c_driver, list);
271                 if (driver->detach_adapter)
272                         if ((res = driver->detach_adapter(adap))) {
273                                 dev_err(&adap->dev, "detach_adapter failed "
274                                         "for driver [%s]\n",
275                                         driver->driver.name);
276                                 goto out_unlock;
277                         }
278         }
279
280         /* detach any active clients. This must be done first, because
281          * it can fail; in which case we give up. */
282         list_for_each_safe(item, _n, &adap->clients) {
283                 client = list_entry(item, struct i2c_client, list);
284
285                 if ((res=client->driver->detach_client(client))) {
286                         dev_err(&adap->dev, "detach_client failed for client "
287                                 "[%s] at address 0x%02x\n", client->name,
288                                 client->addr);
289                         goto out_unlock;
290                 }
291         }
292
293         /* clean up the sysfs representation */
294         init_completion(&adap->dev_released);
295         init_completion(&adap->class_dev_released);
296         class_device_unregister(&adap->class_dev);
297         device_remove_file(&adap->dev, &dev_attr_name);
298         device_unregister(&adap->dev);
299         list_del(&adap->list);
300
301         /* wait for sysfs to drop all references */
302         wait_for_completion(&adap->dev_released);
303         wait_for_completion(&adap->class_dev_released);
304
305         /* free dynamically allocated bus id */
306         idr_remove(&i2c_adapter_idr, adap->nr);
307
308         dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
309
310  out_unlock:
311         mutex_unlock(&core_lists);
312         return res;
313 }
314
315
316 /* -----
317  * What follows is the "upwards" interface: commands for talking to clients,
318  * which implement the functions to access the physical information of the
319  * chips.
320  */
321
322 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
323 {
324         struct list_head   *item;
325         struct i2c_adapter *adapter;
326         int res;
327
328         /* add the driver to the list of i2c drivers in the driver core */
329         driver->driver.owner = owner;
330         driver->driver.bus = &i2c_bus_type;
331
332         res = driver_register(&driver->driver);
333         if (res)
334                 return res;
335
336         mutex_lock(&core_lists);
337
338         list_add_tail(&driver->list,&drivers);
339         pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
340
341         /* now look for instances of driver on our adapters */
342         if (driver->attach_adapter) {
343                 list_for_each(item,&adapters) {
344                         adapter = list_entry(item, struct i2c_adapter, list);
345                         driver->attach_adapter(adapter);
346                 }
347         }
348
349         mutex_unlock(&core_lists);
350         return 0;
351 }
352 EXPORT_SYMBOL(i2c_register_driver);
353
354 int i2c_del_driver(struct i2c_driver *driver)
355 {
356         struct list_head   *item1, *item2, *_n;
357         struct i2c_client  *client;
358         struct i2c_adapter *adap;
359
360         int res = 0;
361
362         mutex_lock(&core_lists);
363
364         /* Have a look at each adapter, if clients of this driver are still
365          * attached. If so, detach them to be able to kill the driver
366          * afterwards.
367          */
368         list_for_each(item1,&adapters) {
369                 adap = list_entry(item1, struct i2c_adapter, list);
370                 if (driver->detach_adapter) {
371                         if ((res = driver->detach_adapter(adap))) {
372                                 dev_err(&adap->dev, "detach_adapter failed "
373                                         "for driver [%s]\n",
374                                         driver->driver.name);
375                                 goto out_unlock;
376                         }
377                 } else {
378                         list_for_each_safe(item2, _n, &adap->clients) {
379                                 client = list_entry(item2, struct i2c_client, list);
380                                 if (client->driver != driver)
381                                         continue;
382                                 dev_dbg(&adap->dev, "detaching client [%s] "
383                                         "at 0x%02x\n", client->name,
384                                         client->addr);
385                                 if ((res = driver->detach_client(client))) {
386                                         dev_err(&adap->dev, "detach_client "
387                                                 "failed for client [%s] at "
388                                                 "0x%02x\n", client->name,
389                                                 client->addr);
390                                         goto out_unlock;
391                                 }
392                         }
393                 }
394         }
395
396         driver_unregister(&driver->driver);
397         list_del(&driver->list);
398         pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
399
400  out_unlock:
401         mutex_unlock(&core_lists);
402         return 0;
403 }
404
405 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
406 {
407         struct list_head   *item;
408         struct i2c_client  *client;
409
410         list_for_each(item,&adapter->clients) {
411                 client = list_entry(item, struct i2c_client, list);
412                 if (client->addr == addr)
413                         return -EBUSY;
414         }
415         return 0;
416 }
417
418 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
419 {
420         int rval;
421
422         mutex_lock(&adapter->clist_lock);
423         rval = __i2c_check_addr(adapter, addr);
424         mutex_unlock(&adapter->clist_lock);
425
426         return rval;
427 }
428
429 int i2c_attach_client(struct i2c_client *client)
430 {
431         struct i2c_adapter *adapter = client->adapter;
432         int res = 0;
433
434         mutex_lock(&adapter->clist_lock);
435         if (__i2c_check_addr(client->adapter, client->addr)) {
436                 res = -EBUSY;
437                 goto out_unlock;
438         }
439         list_add_tail(&client->list,&adapter->clients);
440
441         client->usage_count = 0;
442
443         client->dev.parent = &client->adapter->dev;
444         client->dev.driver = &client->driver->driver;
445         client->dev.bus = &i2c_bus_type;
446         client->dev.release = &i2c_client_release;
447
448         snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
449                 "%d-%04x", i2c_adapter_id(adapter), client->addr);
450         dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
451                 client->name, client->dev.bus_id);
452         res = device_register(&client->dev);
453         if (res)
454                 goto out_list;
455         res = device_create_file(&client->dev, &dev_attr_client_name);
456         if (res)
457                 goto out_unregister;
458         mutex_unlock(&adapter->clist_lock);
459
460         if (adapter->client_register)  {
461                 if (adapter->client_register(client)) {
462                         dev_dbg(&adapter->dev, "client_register "
463                                 "failed for client [%s] at 0x%02x\n",
464                                 client->name, client->addr);
465                 }
466         }
467
468         return 0;
469
470 out_unregister:
471         init_completion(&client->released); /* Needed? */
472         device_unregister(&client->dev);
473         wait_for_completion(&client->released);
474 out_list:
475         list_del(&client->list);
476         dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
477                 "(%d)\n", client->name, client->addr, res);
478 out_unlock:
479         mutex_unlock(&adapter->clist_lock);
480         return res;
481 }
482
483
484 int i2c_detach_client(struct i2c_client *client)
485 {
486         struct i2c_adapter *adapter = client->adapter;
487         int res = 0;
488
489         if (client->usage_count > 0) {
490                 dev_warn(&client->dev, "Client [%s] still busy, "
491                          "can't detach\n", client->name);
492                 return -EBUSY;
493         }
494
495         if (adapter->client_unregister)  {
496                 res = adapter->client_unregister(client);
497                 if (res) {
498                         dev_err(&client->dev,
499                                 "client_unregister [%s] failed, "
500                                 "client not detached\n", client->name);
501                         goto out;
502                 }
503         }
504
505         mutex_lock(&adapter->clist_lock);
506         list_del(&client->list);
507         init_completion(&client->released);
508         device_remove_file(&client->dev, &dev_attr_client_name);
509         device_unregister(&client->dev);
510         mutex_unlock(&adapter->clist_lock);
511         wait_for_completion(&client->released);
512
513  out:
514         return res;
515 }
516
517 static int i2c_inc_use_client(struct i2c_client *client)
518 {
519
520         if (!try_module_get(client->driver->driver.owner))
521                 return -ENODEV;
522         if (!try_module_get(client->adapter->owner)) {
523                 module_put(client->driver->driver.owner);
524                 return -ENODEV;
525         }
526
527         return 0;
528 }
529
530 static void i2c_dec_use_client(struct i2c_client *client)
531 {
532         module_put(client->driver->driver.owner);
533         module_put(client->adapter->owner);
534 }
535
536 int i2c_use_client(struct i2c_client *client)
537 {
538         int ret;
539
540         ret = i2c_inc_use_client(client);
541         if (ret)
542                 return ret;
543
544         client->usage_count++;
545
546         return 0;
547 }
548
549 int i2c_release_client(struct i2c_client *client)
550 {
551         if (!client->usage_count) {
552                 pr_debug("i2c-core: %s used one too many times\n",
553                          __FUNCTION__);
554                 return -EPERM;
555         }
556
557         client->usage_count--;
558         i2c_dec_use_client(client);
559
560         return 0;
561 }
562
563 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
564 {
565         struct list_head  *item;
566         struct i2c_client *client;
567
568         mutex_lock(&adap->clist_lock);
569         list_for_each(item,&adap->clients) {
570                 client = list_entry(item, struct i2c_client, list);
571                 if (!try_module_get(client->driver->driver.owner))
572                         continue;
573                 if (NULL != client->driver->command) {
574                         mutex_unlock(&adap->clist_lock);
575                         client->driver->command(client,cmd,arg);
576                         mutex_lock(&adap->clist_lock);
577                 }
578                 module_put(client->driver->driver.owner);
579        }
580        mutex_unlock(&adap->clist_lock);
581 }
582
583 static int __init i2c_init(void)
584 {
585         int retval;
586
587         retval = bus_register(&i2c_bus_type);
588         if (retval)
589                 return retval;
590         retval = driver_register(&i2c_adapter_driver);
591         if (retval)
592                 return retval;
593         return class_register(&i2c_adapter_class);
594 }
595
596 static void __exit i2c_exit(void)
597 {
598         class_unregister(&i2c_adapter_class);
599         driver_unregister(&i2c_adapter_driver);
600         bus_unregister(&i2c_bus_type);
601 }
602
603 subsys_initcall(i2c_init);
604 module_exit(i2c_exit);
605
606 /* ----------------------------------------------------
607  * the functional interface to the i2c busses.
608  * ----------------------------------------------------
609  */
610
611 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
612 {
613         int ret;
614
615         if (adap->algo->master_xfer) {
616 #ifdef DEBUG
617                 for (ret = 0; ret < num; ret++) {
618                         dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
619                                 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
620                                 'R' : 'W', msgs[ret].addr, msgs[ret].len);
621                 }
622 #endif
623
624                 mutex_lock_nested(&adap->bus_lock, adap->level);
625                 ret = adap->algo->master_xfer(adap,msgs,num);
626                 mutex_unlock(&adap->bus_lock);
627
628                 return ret;
629         } else {
630                 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
631                 return -ENOSYS;
632         }
633 }
634
635 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
636 {
637         int ret;
638         struct i2c_adapter *adap=client->adapter;
639         struct i2c_msg msg;
640
641         msg.addr = client->addr;
642         msg.flags = client->flags & I2C_M_TEN;
643         msg.len = count;
644         msg.buf = (char *)buf;
645
646         ret = i2c_transfer(adap, &msg, 1);
647
648         /* If everything went ok (i.e. 1 msg transmitted), return #bytes
649            transmitted, else error code. */
650         return (ret == 1) ? count : ret;
651 }
652
653 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
654 {
655         struct i2c_adapter *adap=client->adapter;
656         struct i2c_msg msg;
657         int ret;
658
659         msg.addr = client->addr;
660         msg.flags = client->flags & I2C_M_TEN;
661         msg.flags |= I2C_M_RD;
662         msg.len = count;
663         msg.buf = buf;
664
665         ret = i2c_transfer(adap, &msg, 1);
666
667         /* If everything went ok (i.e. 1 msg transmitted), return #bytes
668            transmitted, else error code. */
669         return (ret == 1) ? count : ret;
670 }
671
672
673 int i2c_control(struct i2c_client *client,
674         unsigned int cmd, unsigned long arg)
675 {
676         int ret = 0;
677         struct i2c_adapter *adap = client->adapter;
678
679         dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
680         switch (cmd) {
681                 case I2C_RETRIES:
682                         adap->retries = arg;
683                         break;
684                 case I2C_TIMEOUT:
685                         adap->timeout = arg;
686                         break;
687                 default:
688                         if (adap->algo->algo_control!=NULL)
689                                 ret = adap->algo->algo_control(adap,cmd,arg);
690         }
691         return ret;
692 }
693
694 /* ----------------------------------------------------
695  * the i2c address scanning function
696  * Will not work for 10-bit addresses!
697  * ----------------------------------------------------
698  */
699 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
700                              int (*found_proc) (struct i2c_adapter *, int, int))
701 {
702         int err;
703
704         /* Make sure the address is valid */
705         if (addr < 0x03 || addr > 0x77) {
706                 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
707                          addr);
708                 return -EINVAL;
709         }
710
711         /* Skip if already in use */
712         if (i2c_check_addr(adapter, addr))
713                 return 0;
714
715         /* Make sure there is something at this address, unless forced */
716         if (kind < 0) {
717                 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
718                                    I2C_SMBUS_QUICK, NULL) < 0)
719                         return 0;
720
721                 /* prevent 24RF08 corruption */
722                 if ((addr & ~0x0f) == 0x50)
723                         i2c_smbus_xfer(adapter, addr, 0, 0, 0,
724                                        I2C_SMBUS_QUICK, NULL);
725         }
726
727         /* Finally call the custom detection function */
728         err = found_proc(adapter, addr, kind);
729         /* -ENODEV can be returned if there is a chip at the given address
730            but it isn't supported by this chip driver. We catch it here as
731            this isn't an error. */
732         if (err == -ENODEV)
733                 err = 0;
734
735         if (err)
736                 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
737                          addr, err);
738         return err;
739 }
740
741 int i2c_probe(struct i2c_adapter *adapter,
742               struct i2c_client_address_data *address_data,
743               int (*found_proc) (struct i2c_adapter *, int, int))
744 {
745         int i, err;
746         int adap_id = i2c_adapter_id(adapter);
747
748         /* Force entries are done first, and are not affected by ignore
749            entries */
750         if (address_data->forces) {
751                 unsigned short **forces = address_data->forces;
752                 int kind;
753
754                 for (kind = 0; forces[kind]; kind++) {
755                         for (i = 0; forces[kind][i] != I2C_CLIENT_END;
756                              i += 2) {
757                                 if (forces[kind][i] == adap_id
758                                  || forces[kind][i] == ANY_I2C_BUS) {
759                                         dev_dbg(&adapter->dev, "found force "
760                                                 "parameter for adapter %d, "
761                                                 "addr 0x%02x, kind %d\n",
762                                                 adap_id, forces[kind][i + 1],
763                                                 kind);
764                                         err = i2c_probe_address(adapter,
765                                                 forces[kind][i + 1],
766                                                 kind, found_proc);
767                                         if (err)
768                                                 return err;
769                                 }
770                         }
771                 }
772         }
773
774         /* Stop here if we can't use SMBUS_QUICK */
775         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
776                 if (address_data->probe[0] == I2C_CLIENT_END
777                  && address_data->normal_i2c[0] == I2C_CLIENT_END)
778                         return 0;
779
780                 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
781                          "can't probe for chips\n");
782                 return -1;
783         }
784
785         /* Probe entries are done second, and are not affected by ignore
786            entries either */
787         for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
788                 if (address_data->probe[i] == adap_id
789                  || address_data->probe[i] == ANY_I2C_BUS) {
790                         dev_dbg(&adapter->dev, "found probe parameter for "
791                                 "adapter %d, addr 0x%02x\n", adap_id,
792                                 address_data->probe[i + 1]);
793                         err = i2c_probe_address(adapter,
794                                                 address_data->probe[i + 1],
795                                                 -1, found_proc);
796                         if (err)
797                                 return err;
798                 }
799         }
800
801         /* Normal entries are done last, unless shadowed by an ignore entry */
802         for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
803                 int j, ignore;
804
805                 ignore = 0;
806                 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
807                      j += 2) {
808                         if ((address_data->ignore[j] == adap_id ||
809                              address_data->ignore[j] == ANY_I2C_BUS)
810                          && address_data->ignore[j + 1]
811                             == address_data->normal_i2c[i]) {
812                                 dev_dbg(&adapter->dev, "found ignore "
813                                         "parameter for adapter %d, "
814                                         "addr 0x%02x\n", adap_id,
815                                         address_data->ignore[j + 1]);
816                                 ignore = 1;
817                                 break;
818                         }
819                 }
820                 if (ignore)
821                         continue;
822
823                 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
824                         "addr 0x%02x\n", adap_id,
825                         address_data->normal_i2c[i]);
826                 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
827                                         -1, found_proc);
828                 if (err)
829                         return err;
830         }
831
832         return 0;
833 }
834
835 struct i2c_adapter* i2c_get_adapter(int id)
836 {
837         struct i2c_adapter *adapter;
838
839         mutex_lock(&core_lists);
840         adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
841         if (adapter && !try_module_get(adapter->owner))
842                 adapter = NULL;
843
844         mutex_unlock(&core_lists);
845         return adapter;
846 }
847
848 void i2c_put_adapter(struct i2c_adapter *adap)
849 {
850         module_put(adap->owner);
851 }
852
853 /* The SMBus parts */
854
855 #define POLY    (0x1070U << 3)
856 static u8
857 crc8(u16 data)
858 {
859         int i;
860
861         for(i = 0; i < 8; i++) {
862                 if (data & 0x8000)
863                         data = data ^ POLY;
864                 data = data << 1;
865         }
866         return (u8)(data >> 8);
867 }
868
869 /* Incremental CRC8 over count bytes in the array pointed to by p */
870 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
871 {
872         int i;
873
874         for(i = 0; i < count; i++)
875                 crc = crc8((crc ^ p[i]) << 8);
876         return crc;
877 }
878
879 /* Assume a 7-bit address, which is reasonable for SMBus */
880 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
881 {
882         /* The address will be sent first */
883         u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
884         pec = i2c_smbus_pec(pec, &addr, 1);
885
886         /* The data buffer follows */
887         return i2c_smbus_pec(pec, msg->buf, msg->len);
888 }
889
890 /* Used for write only transactions */
891 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
892 {
893         msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
894         msg->len++;
895 }
896
897 /* Return <0 on CRC error
898    If there was a write before this read (most cases) we need to take the
899    partial CRC from the write part into account.
900    Note that this function does modify the message (we need to decrease the
901    message length to hide the CRC byte from the caller). */
902 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
903 {
904         u8 rpec = msg->buf[--msg->len];
905         cpec = i2c_smbus_msg_pec(cpec, msg);
906
907         if (rpec != cpec) {
908                 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
909                         rpec, cpec);
910                 return -1;
911         }
912         return 0;
913 }
914
915 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
916 {
917         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
918                               value,0,I2C_SMBUS_QUICK,NULL);
919 }
920
921 s32 i2c_smbus_read_byte(struct i2c_client *client)
922 {
923         union i2c_smbus_data data;
924         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
925                            I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
926                 return -1;
927         else
928                 return data.byte;
929 }
930
931 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
932 {
933         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
934                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
935 }
936
937 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
938 {
939         union i2c_smbus_data data;
940         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
941                            I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
942                 return -1;
943         else
944                 return data.byte;
945 }
946
947 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
948 {
949         union i2c_smbus_data data;
950         data.byte = value;
951         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
952                               I2C_SMBUS_WRITE,command,
953                               I2C_SMBUS_BYTE_DATA,&data);
954 }
955
956 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
957 {
958         union i2c_smbus_data data;
959         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
960                            I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
961                 return -1;
962         else
963                 return data.word;
964 }
965
966 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
967 {
968         union i2c_smbus_data data;
969         data.word = value;
970         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
971                               I2C_SMBUS_WRITE,command,
972                               I2C_SMBUS_WORD_DATA,&data);
973 }
974
975 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
976                                u8 length, const u8 *values)
977 {
978         union i2c_smbus_data data;
979
980         if (length > I2C_SMBUS_BLOCK_MAX)
981                 length = I2C_SMBUS_BLOCK_MAX;
982         data.block[0] = length;
983         memcpy(&data.block[1], values, length);
984         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
985                               I2C_SMBUS_WRITE,command,
986                               I2C_SMBUS_BLOCK_DATA,&data);
987 }
988
989 /* Returns the number of read bytes */
990 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
991 {
992         union i2c_smbus_data data;
993
994         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
995                               I2C_SMBUS_READ,command,
996                               I2C_SMBUS_I2C_BLOCK_DATA,&data))
997                 return -1;
998
999         memcpy(values, &data.block[1], data.block[0]);
1000         return data.block[0];
1001 }
1002
1003 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1004                                    u8 length, const u8 *values)
1005 {
1006         union i2c_smbus_data data;
1007
1008         if (length > I2C_SMBUS_BLOCK_MAX)
1009                 length = I2C_SMBUS_BLOCK_MAX;
1010         data.block[0] = length;
1011         memcpy(data.block + 1, values, length);
1012         return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1013                               I2C_SMBUS_WRITE, command,
1014                               I2C_SMBUS_I2C_BLOCK_DATA, &data);
1015 }
1016
1017 /* Simulate a SMBus command using the i2c protocol
1018    No checking of parameters is done!  */
1019 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1020                                    unsigned short flags,
1021                                    char read_write, u8 command, int size,
1022                                    union i2c_smbus_data * data)
1023 {
1024         /* So we need to generate a series of msgs. In the case of writing, we
1025           need to use only one message; when reading, we need two. We initialize
1026           most things with sane defaults, to keep the code below somewhat
1027           simpler. */
1028         unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1029         unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1030         int num = read_write == I2C_SMBUS_READ?2:1;
1031         struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1032                                   { addr, flags | I2C_M_RD, 0, msgbuf1 }
1033                                 };
1034         int i;
1035         u8 partial_pec = 0;
1036
1037         msgbuf0[0] = command;
1038         switch(size) {
1039         case I2C_SMBUS_QUICK:
1040                 msg[0].len = 0;
1041                 /* Special case: The read/write field is used as data */
1042                 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1043                 num = 1;
1044                 break;
1045         case I2C_SMBUS_BYTE:
1046                 if (read_write == I2C_SMBUS_READ) {
1047                         /* Special case: only a read! */
1048                         msg[0].flags = I2C_M_RD | flags;
1049                         num = 1;
1050                 }
1051                 break;
1052         case I2C_SMBUS_BYTE_DATA:
1053                 if (read_write == I2C_SMBUS_READ)
1054                         msg[1].len = 1;
1055                 else {
1056                         msg[0].len = 2;
1057                         msgbuf0[1] = data->byte;
1058                 }
1059                 break;
1060         case I2C_SMBUS_WORD_DATA:
1061                 if (read_write == I2C_SMBUS_READ)
1062                         msg[1].len = 2;
1063                 else {
1064                         msg[0].len=3;
1065                         msgbuf0[1] = data->word & 0xff;
1066                         msgbuf0[2] = data->word >> 8;
1067                 }
1068                 break;
1069         case I2C_SMBUS_PROC_CALL:
1070                 num = 2; /* Special case */
1071                 read_write = I2C_SMBUS_READ;
1072                 msg[0].len = 3;
1073                 msg[1].len = 2;
1074                 msgbuf0[1] = data->word & 0xff;
1075                 msgbuf0[2] = data->word >> 8;
1076                 break;
1077         case I2C_SMBUS_BLOCK_DATA:
1078                 if (read_write == I2C_SMBUS_READ) {
1079                         dev_err(&adapter->dev, "Block read not supported "
1080                                "under I2C emulation!\n");
1081                         return -1;
1082                 } else {
1083                         msg[0].len = data->block[0] + 2;
1084                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1085                                 dev_err(&adapter->dev, "smbus_access called with "
1086                                        "invalid block write size (%d)\n",
1087                                        data->block[0]);
1088                                 return -1;
1089                         }
1090                         for (i = 1; i < msg[0].len; i++)
1091                                 msgbuf0[i] = data->block[i-1];
1092                 }
1093                 break;
1094         case I2C_SMBUS_BLOCK_PROC_CALL:
1095                 dev_dbg(&adapter->dev, "Block process call not supported "
1096                        "under I2C emulation!\n");
1097                 return -1;
1098         case I2C_SMBUS_I2C_BLOCK_DATA:
1099                 if (read_write == I2C_SMBUS_READ) {
1100                         msg[1].len = I2C_SMBUS_BLOCK_MAX;
1101                 } else {
1102                         msg[0].len = data->block[0] + 1;
1103                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1104                                 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1105                                        "invalid block write size (%d)\n",
1106                                        data->block[0]);
1107                                 return -1;
1108                         }
1109                         for (i = 1; i <= data->block[0]; i++)
1110                                 msgbuf0[i] = data->block[i];
1111                 }
1112                 break;
1113         default:
1114                 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1115                        size);
1116                 return -1;
1117         }
1118
1119         i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1120                                       && size != I2C_SMBUS_I2C_BLOCK_DATA);
1121         if (i) {
1122                 /* Compute PEC if first message is a write */
1123                 if (!(msg[0].flags & I2C_M_RD)) {
1124                         if (num == 1) /* Write only */
1125                                 i2c_smbus_add_pec(&msg[0]);
1126                         else /* Write followed by read */
1127                                 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1128                 }
1129                 /* Ask for PEC if last message is a read */
1130                 if (msg[num-1].flags & I2C_M_RD)
1131                         msg[num-1].len++;
1132         }
1133
1134         if (i2c_transfer(adapter, msg, num) < 0)
1135                 return -1;
1136
1137         /* Check PEC if last message is a read */
1138         if (i && (msg[num-1].flags & I2C_M_RD)) {
1139                 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1140                         return -1;
1141         }
1142
1143         if (read_write == I2C_SMBUS_READ)
1144                 switch(size) {
1145                         case I2C_SMBUS_BYTE:
1146                                 data->byte = msgbuf0[0];
1147                                 break;
1148                         case I2C_SMBUS_BYTE_DATA:
1149                                 data->byte = msgbuf1[0];
1150                                 break;
1151                         case I2C_SMBUS_WORD_DATA:
1152                         case I2C_SMBUS_PROC_CALL:
1153                                 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1154                                 break;
1155                         case I2C_SMBUS_I2C_BLOCK_DATA:
1156                                 /* fixed at 32 for now */
1157                                 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1158                                 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1159                                         data->block[i+1] = msgbuf1[i];
1160                                 break;
1161                 }
1162         return 0;
1163 }
1164
1165
1166 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1167                    char read_write, u8 command, int size,
1168                    union i2c_smbus_data * data)
1169 {
1170         s32 res;
1171
1172         flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1173
1174         if (adapter->algo->smbus_xfer) {
1175                 mutex_lock(&adapter->bus_lock);
1176                 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1177                                                 command,size,data);
1178                 mutex_unlock(&adapter->bus_lock);
1179         } else
1180                 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1181                                               command,size,data);
1182
1183         return res;
1184 }
1185
1186
1187 /* Next four are needed by i2c-isa */
1188 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1189 EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1190 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1191 EXPORT_SYMBOL_GPL(i2c_bus_type);
1192
1193 EXPORT_SYMBOL(i2c_add_adapter);
1194 EXPORT_SYMBOL(i2c_del_adapter);
1195 EXPORT_SYMBOL(i2c_del_driver);
1196 EXPORT_SYMBOL(i2c_attach_client);
1197 EXPORT_SYMBOL(i2c_detach_client);
1198 EXPORT_SYMBOL(i2c_use_client);
1199 EXPORT_SYMBOL(i2c_release_client);
1200 EXPORT_SYMBOL(i2c_clients_command);
1201 EXPORT_SYMBOL(i2c_check_addr);
1202
1203 EXPORT_SYMBOL(i2c_master_send);
1204 EXPORT_SYMBOL(i2c_master_recv);
1205 EXPORT_SYMBOL(i2c_control);
1206 EXPORT_SYMBOL(i2c_transfer);
1207 EXPORT_SYMBOL(i2c_get_adapter);
1208 EXPORT_SYMBOL(i2c_put_adapter);
1209 EXPORT_SYMBOL(i2c_probe);
1210
1211 EXPORT_SYMBOL(i2c_smbus_xfer);
1212 EXPORT_SYMBOL(i2c_smbus_write_quick);
1213 EXPORT_SYMBOL(i2c_smbus_read_byte);
1214 EXPORT_SYMBOL(i2c_smbus_write_byte);
1215 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1216 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1217 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1218 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1219 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1220 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1221 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1222
1223 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1224 MODULE_DESCRIPTION("I2C-Bus main module");
1225 MODULE_LICENSE("GPL");