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