i2c: Drop redundant i2c_adapter.list
[safe/jmp/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 <linux/completion.h>
36 #include <asm/uaccess.h>
37 #include <asm/semaphore.h>
38
39 #include "i2c-core.h"
40
41
42 static LIST_HEAD(drivers);
43 static DEFINE_MUTEX(core_lists);
44 static DEFINE_IDR(i2c_adapter_idr);
45
46 #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
47
48 /* ------------------------------------------------------------------------- */
49
50 static int i2c_device_match(struct device *dev, struct device_driver *drv)
51 {
52         struct i2c_client       *client = to_i2c_client(dev);
53         struct i2c_driver       *driver = to_i2c_driver(drv);
54
55         /* make legacy i2c drivers bypass driver model probing entirely;
56          * such drivers scan each i2c adapter/bus themselves.
57          */
58         if (!is_newstyle_driver(driver))
59                 return 0;
60
61         /* new style drivers use the same kind of driver matching policy
62          * as platform devices or SPI:  compare device and driver IDs.
63          */
64         return strcmp(client->driver_name, drv->name) == 0;
65 }
66
67 #ifdef  CONFIG_HOTPLUG
68
69 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
70 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
71 {
72         struct i2c_client       *client = to_i2c_client(dev);
73
74         /* by definition, legacy drivers can't hotplug */
75         if (dev->driver || !client->driver_name)
76                 return 0;
77
78         if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
79                 return -ENOMEM;
80         dev_dbg(dev, "uevent\n");
81         return 0;
82 }
83
84 #else
85 #define i2c_device_uevent       NULL
86 #endif  /* CONFIG_HOTPLUG */
87
88 static int i2c_device_probe(struct device *dev)
89 {
90         struct i2c_client       *client = to_i2c_client(dev);
91         struct i2c_driver       *driver = to_i2c_driver(dev->driver);
92
93         if (!driver->probe)
94                 return -ENODEV;
95         client->driver = driver;
96         dev_dbg(dev, "probe\n");
97         return driver->probe(client);
98 }
99
100 static int i2c_device_remove(struct device *dev)
101 {
102         struct i2c_client       *client = to_i2c_client(dev);
103         struct i2c_driver       *driver;
104         int                     status;
105
106         if (!dev->driver)
107                 return 0;
108
109         driver = to_i2c_driver(dev->driver);
110         if (driver->remove) {
111                 dev_dbg(dev, "remove\n");
112                 status = driver->remove(client);
113         } else {
114                 dev->driver = NULL;
115                 status = 0;
116         }
117         if (status == 0)
118                 client->driver = NULL;
119         return status;
120 }
121
122 static void i2c_device_shutdown(struct device *dev)
123 {
124         struct i2c_driver *driver;
125
126         if (!dev->driver)
127                 return;
128         driver = to_i2c_driver(dev->driver);
129         if (driver->shutdown)
130                 driver->shutdown(to_i2c_client(dev));
131 }
132
133 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
134 {
135         struct i2c_driver *driver;
136
137         if (!dev->driver)
138                 return 0;
139         driver = to_i2c_driver(dev->driver);
140         if (!driver->suspend)
141                 return 0;
142         return driver->suspend(to_i2c_client(dev), mesg);
143 }
144
145 static int i2c_device_resume(struct device * dev)
146 {
147         struct i2c_driver *driver;
148
149         if (!dev->driver)
150                 return 0;
151         driver = to_i2c_driver(dev->driver);
152         if (!driver->resume)
153                 return 0;
154         return driver->resume(to_i2c_client(dev));
155 }
156
157 static void i2c_client_release(struct device *dev)
158 {
159         struct i2c_client *client = to_i2c_client(dev);
160         complete(&client->released);
161 }
162
163 static void i2c_client_dev_release(struct device *dev)
164 {
165         kfree(to_i2c_client(dev));
166 }
167
168 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
169 {
170         struct i2c_client *client = to_i2c_client(dev);
171         return sprintf(buf, "%s\n", client->name);
172 }
173
174 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
175 {
176         struct i2c_client *client = to_i2c_client(dev);
177         return client->driver_name
178                 ? sprintf(buf, "%s\n", client->driver_name)
179                 : 0;
180 }
181
182 static struct device_attribute i2c_dev_attrs[] = {
183         __ATTR(name, S_IRUGO, show_client_name, NULL),
184         /* modalias helps coldplug:  modprobe $(cat .../modalias) */
185         __ATTR(modalias, S_IRUGO, show_modalias, NULL),
186         { },
187 };
188
189 static struct bus_type i2c_bus_type = {
190         .name           = "i2c",
191         .dev_attrs      = i2c_dev_attrs,
192         .match          = i2c_device_match,
193         .uevent         = i2c_device_uevent,
194         .probe          = i2c_device_probe,
195         .remove         = i2c_device_remove,
196         .shutdown       = i2c_device_shutdown,
197         .suspend        = i2c_device_suspend,
198         .resume         = i2c_device_resume,
199 };
200
201 /**
202  * i2c_new_device - instantiate an i2c device for use with a new style driver
203  * @adap: the adapter managing the device
204  * @info: describes one I2C device; bus_num is ignored
205  * Context: can sleep
206  *
207  * Create a device to work with a new style i2c driver, where binding is
208  * handled through driver model probe()/remove() methods.  This call is not
209  * appropriate for use by mainboad initialization logic, which usually runs
210  * during an arch_initcall() long before any i2c_adapter could exist.
211  *
212  * This returns the new i2c client, which may be saved for later use with
213  * i2c_unregister_device(); or NULL to indicate an error.
214  */
215 struct i2c_client *
216 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
217 {
218         struct i2c_client       *client;
219         int                     status;
220
221         client = kzalloc(sizeof *client, GFP_KERNEL);
222         if (!client)
223                 return NULL;
224
225         client->adapter = adap;
226
227         client->dev.platform_data = info->platform_data;
228         device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
229
230         client->flags = info->flags & ~I2C_CLIENT_WAKE;
231         client->addr = info->addr;
232         client->irq = info->irq;
233
234         strlcpy(client->driver_name, info->driver_name,
235                 sizeof(client->driver_name));
236         strlcpy(client->name, info->type, sizeof(client->name));
237
238         /* a new style driver may be bound to this device when we
239          * return from this function, or any later moment (e.g. maybe
240          * hotplugging will load the driver module).  and the device
241          * refcount model is the standard driver model one.
242          */
243         status = i2c_attach_client(client);
244         if (status < 0) {
245                 kfree(client);
246                 client = NULL;
247         }
248         return client;
249 }
250 EXPORT_SYMBOL_GPL(i2c_new_device);
251
252
253 /**
254  * i2c_unregister_device - reverse effect of i2c_new_device()
255  * @client: value returned from i2c_new_device()
256  * Context: can sleep
257  */
258 void i2c_unregister_device(struct i2c_client *client)
259 {
260         struct i2c_adapter      *adapter = client->adapter;
261         struct i2c_driver       *driver = client->driver;
262
263         if (driver && !is_newstyle_driver(driver)) {
264                 dev_err(&client->dev, "can't unregister devices "
265                         "with legacy drivers\n");
266                 WARN_ON(1);
267                 return;
268         }
269
270         mutex_lock(&adapter->clist_lock);
271         list_del(&client->list);
272         mutex_unlock(&adapter->clist_lock);
273
274         device_unregister(&client->dev);
275 }
276 EXPORT_SYMBOL_GPL(i2c_unregister_device);
277
278
279 /* ------------------------------------------------------------------------- */
280
281 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
282
283 static void i2c_adapter_dev_release(struct device *dev)
284 {
285         struct i2c_adapter *adap = to_i2c_adapter(dev);
286         complete(&adap->dev_released);
287 }
288
289 static ssize_t
290 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
291 {
292         struct i2c_adapter *adap = to_i2c_adapter(dev);
293         return sprintf(buf, "%s\n", adap->name);
294 }
295
296 static struct device_attribute i2c_adapter_attrs[] = {
297         __ATTR(name, S_IRUGO, show_adapter_name, NULL),
298         { },
299 };
300
301 static struct class i2c_adapter_class = {
302         .owner                  = THIS_MODULE,
303         .name                   = "i2c-adapter",
304         .dev_attrs              = i2c_adapter_attrs,
305 };
306
307 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
308 {
309         struct i2c_devinfo      *devinfo;
310
311         mutex_lock(&__i2c_board_lock);
312         list_for_each_entry(devinfo, &__i2c_board_list, list) {
313                 if (devinfo->busnum == adapter->nr
314                                 && !i2c_new_device(adapter,
315                                                 &devinfo->board_info))
316                         printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
317                                 i2c_adapter_id(adapter),
318                                 devinfo->board_info.addr);
319         }
320         mutex_unlock(&__i2c_board_lock);
321 }
322
323 static int i2c_register_adapter(struct i2c_adapter *adap)
324 {
325         int res = 0;
326         struct list_head   *item;
327         struct i2c_driver  *driver;
328
329         mutex_init(&adap->bus_lock);
330         mutex_init(&adap->clist_lock);
331         INIT_LIST_HEAD(&adap->clients);
332
333         mutex_lock(&core_lists);
334
335         /* Add the adapter to the driver core.
336          * If the parent pointer is not set up,
337          * we add this adapter to the host bus.
338          */
339         if (adap->dev.parent == NULL) {
340                 adap->dev.parent = &platform_bus;
341                 pr_debug("I2C adapter driver [%s] forgot to specify "
342                          "physical device\n", adap->name);
343         }
344         sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
345         adap->dev.release = &i2c_adapter_dev_release;
346         adap->dev.class = &i2c_adapter_class;
347         res = device_register(&adap->dev);
348         if (res)
349                 goto out_list;
350
351         dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
352
353         /* create pre-declared device nodes for new-style drivers */
354         if (adap->nr < __i2c_first_dynamic_bus_num)
355                 i2c_scan_static_board_info(adap);
356
357         /* let legacy drivers scan this bus for matching devices */
358         list_for_each(item,&drivers) {
359                 driver = list_entry(item, struct i2c_driver, list);
360                 if (driver->attach_adapter)
361                         /* We ignore the return code; if it fails, too bad */
362                         driver->attach_adapter(adap);
363         }
364
365 out_unlock:
366         mutex_unlock(&core_lists);
367         return res;
368
369 out_list:
370         idr_remove(&i2c_adapter_idr, adap->nr);
371         goto out_unlock;
372 }
373
374 /**
375  * i2c_add_adapter - declare i2c adapter, use dynamic bus number
376  * @adapter: the adapter to add
377  * Context: can sleep
378  *
379  * This routine is used to declare an I2C adapter when its bus number
380  * doesn't matter.  Examples: for I2C adapters dynamically added by
381  * USB links or PCI plugin cards.
382  *
383  * When this returns zero, a new bus number was allocated and stored
384  * in adap->nr, and the specified adapter became available for clients.
385  * Otherwise, a negative errno value is returned.
386  */
387 int i2c_add_adapter(struct i2c_adapter *adapter)
388 {
389         int     id, res = 0;
390
391 retry:
392         if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
393                 return -ENOMEM;
394
395         mutex_lock(&core_lists);
396         /* "above" here means "above or equal to", sigh */
397         res = idr_get_new_above(&i2c_adapter_idr, adapter,
398                                 __i2c_first_dynamic_bus_num, &id);
399         mutex_unlock(&core_lists);
400
401         if (res < 0) {
402                 if (res == -EAGAIN)
403                         goto retry;
404                 return res;
405         }
406
407         adapter->nr = id;
408         return i2c_register_adapter(adapter);
409 }
410 EXPORT_SYMBOL(i2c_add_adapter);
411
412 /**
413  * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
414  * @adap: the adapter to register (with adap->nr initialized)
415  * Context: can sleep
416  *
417  * This routine is used to declare an I2C adapter when its bus number
418  * matters.  Example: for I2C adapters from system-on-chip CPUs, or
419  * otherwise built in to the system's mainboard, and where i2c_board_info
420  * is used to properly configure I2C devices.
421  *
422  * If no devices have pre-been declared for this bus, then be sure to
423  * register the adapter before any dynamically allocated ones.  Otherwise
424  * the required bus ID may not be available.
425  *
426  * When this returns zero, the specified adapter became available for
427  * clients using the bus number provided in adap->nr.  Also, the table
428  * of I2C devices pre-declared using i2c_register_board_info() is scanned,
429  * and the appropriate driver model device nodes are created.  Otherwise, a
430  * negative errno value is returned.
431  */
432 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
433 {
434         int     id;
435         int     status;
436
437         if (adap->nr & ~MAX_ID_MASK)
438                 return -EINVAL;
439
440 retry:
441         if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
442                 return -ENOMEM;
443
444         mutex_lock(&core_lists);
445         /* "above" here means "above or equal to", sigh;
446          * we need the "equal to" result to force the result
447          */
448         status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
449         if (status == 0 && id != adap->nr) {
450                 status = -EBUSY;
451                 idr_remove(&i2c_adapter_idr, id);
452         }
453         mutex_unlock(&core_lists);
454         if (status == -EAGAIN)
455                 goto retry;
456
457         if (status == 0)
458                 status = i2c_register_adapter(adap);
459         return status;
460 }
461 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
462
463 /**
464  * i2c_del_adapter - unregister I2C adapter
465  * @adap: the adapter being unregistered
466  * Context: can sleep
467  *
468  * This unregisters an I2C adapter which was previously registered
469  * by @i2c_add_adapter or @i2c_add_numbered_adapter.
470  */
471 int i2c_del_adapter(struct i2c_adapter *adap)
472 {
473         struct list_head  *item, *_n;
474         struct i2c_driver *driver;
475         struct i2c_client *client;
476         int res = 0;
477
478         mutex_lock(&core_lists);
479
480         /* First make sure that this adapter was ever added */
481         if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
482                 pr_debug("i2c-core: attempting to delete unregistered "
483                          "adapter [%s]\n", adap->name);
484                 res = -EINVAL;
485                 goto out_unlock;
486         }
487
488         list_for_each(item,&drivers) {
489                 driver = list_entry(item, struct i2c_driver, list);
490                 if (driver->detach_adapter)
491                         if ((res = driver->detach_adapter(adap))) {
492                                 dev_err(&adap->dev, "detach_adapter failed "
493                                         "for driver [%s]\n",
494                                         driver->driver.name);
495                                 goto out_unlock;
496                         }
497         }
498
499         /* detach any active clients. This must be done first, because
500          * it can fail; in which case we give up. */
501         list_for_each_safe(item, _n, &adap->clients) {
502                 struct i2c_driver       *driver;
503
504                 client = list_entry(item, struct i2c_client, list);
505                 driver = client->driver;
506
507                 /* new style, follow standard driver model */
508                 if (!driver || is_newstyle_driver(driver)) {
509                         i2c_unregister_device(client);
510                         continue;
511                 }
512
513                 /* legacy drivers create and remove clients themselves */
514                 if ((res = driver->detach_client(client))) {
515                         dev_err(&adap->dev, "detach_client failed for client "
516                                 "[%s] at address 0x%02x\n", client->name,
517                                 client->addr);
518                         goto out_unlock;
519                 }
520         }
521
522         /* clean up the sysfs representation */
523         init_completion(&adap->dev_released);
524         device_unregister(&adap->dev);
525
526         /* wait for sysfs to drop all references */
527         wait_for_completion(&adap->dev_released);
528
529         /* free bus id */
530         idr_remove(&i2c_adapter_idr, adap->nr);
531
532         dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
533
534  out_unlock:
535         mutex_unlock(&core_lists);
536         return res;
537 }
538 EXPORT_SYMBOL(i2c_del_adapter);
539
540
541 /* ------------------------------------------------------------------------- */
542
543 /*
544  * An i2c_driver is used with one or more i2c_client (device) nodes to access
545  * i2c slave chips, on a bus instance associated with some i2c_adapter.  There
546  * are two models for binding the driver to its device:  "new style" drivers
547  * follow the standard Linux driver model and just respond to probe() calls
548  * issued if the driver core sees they match(); "legacy" drivers create device
549  * nodes themselves.
550  */
551
552 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
553 {
554         int res;
555
556         /* new style driver methods can't mix with legacy ones */
557         if (is_newstyle_driver(driver)) {
558                 if (driver->attach_adapter || driver->detach_adapter
559                                 || driver->detach_client) {
560                         printk(KERN_WARNING
561                                         "i2c-core: driver [%s] is confused\n",
562                                         driver->driver.name);
563                         return -EINVAL;
564                 }
565         }
566
567         /* add the driver to the list of i2c drivers in the driver core */
568         driver->driver.owner = owner;
569         driver->driver.bus = &i2c_bus_type;
570
571         /* for new style drivers, when registration returns the driver core
572          * will have called probe() for all matching-but-unbound devices.
573          */
574         res = driver_register(&driver->driver);
575         if (res)
576                 return res;
577
578         mutex_lock(&core_lists);
579
580         list_add_tail(&driver->list,&drivers);
581         pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
582
583         /* legacy drivers scan i2c busses directly */
584         if (driver->attach_adapter) {
585                 struct i2c_adapter *adapter;
586
587                 down(&i2c_adapter_class.sem);
588                 list_for_each_entry(adapter, &i2c_adapter_class.devices,
589                                     dev.node) {
590                         driver->attach_adapter(adapter);
591                 }
592                 up(&i2c_adapter_class.sem);
593         }
594
595         mutex_unlock(&core_lists);
596         return 0;
597 }
598 EXPORT_SYMBOL(i2c_register_driver);
599
600 /**
601  * i2c_del_driver - unregister I2C driver
602  * @driver: the driver being unregistered
603  * Context: can sleep
604  */
605 void i2c_del_driver(struct i2c_driver *driver)
606 {
607         struct list_head   *item2, *_n;
608         struct i2c_client  *client;
609         struct i2c_adapter *adap;
610
611         mutex_lock(&core_lists);
612
613         /* new-style driver? */
614         if (is_newstyle_driver(driver))
615                 goto unregister;
616
617         /* Have a look at each adapter, if clients of this driver are still
618          * attached. If so, detach them to be able to kill the driver
619          * afterwards.
620          */
621         down(&i2c_adapter_class.sem);
622         list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
623                 if (driver->detach_adapter) {
624                         if (driver->detach_adapter(adap)) {
625                                 dev_err(&adap->dev, "detach_adapter failed "
626                                         "for driver [%s]\n",
627                                         driver->driver.name);
628                         }
629                 } else {
630                         list_for_each_safe(item2, _n, &adap->clients) {
631                                 client = list_entry(item2, struct i2c_client, list);
632                                 if (client->driver != driver)
633                                         continue;
634                                 dev_dbg(&adap->dev, "detaching client [%s] "
635                                         "at 0x%02x\n", client->name,
636                                         client->addr);
637                                 if (driver->detach_client(client)) {
638                                         dev_err(&adap->dev, "detach_client "
639                                                 "failed for client [%s] at "
640                                                 "0x%02x\n", client->name,
641                                                 client->addr);
642                                 }
643                         }
644                 }
645         }
646         up(&i2c_adapter_class.sem);
647
648  unregister:
649         driver_unregister(&driver->driver);
650         list_del(&driver->list);
651         pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
652
653         mutex_unlock(&core_lists);
654 }
655 EXPORT_SYMBOL(i2c_del_driver);
656
657 /* ------------------------------------------------------------------------- */
658
659 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
660 {
661         struct list_head   *item;
662         struct i2c_client  *client;
663
664         list_for_each(item,&adapter->clients) {
665                 client = list_entry(item, struct i2c_client, list);
666                 if (client->addr == addr)
667                         return -EBUSY;
668         }
669         return 0;
670 }
671
672 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
673 {
674         int rval;
675
676         mutex_lock(&adapter->clist_lock);
677         rval = __i2c_check_addr(adapter, addr);
678         mutex_unlock(&adapter->clist_lock);
679
680         return rval;
681 }
682
683 int i2c_attach_client(struct i2c_client *client)
684 {
685         struct i2c_adapter *adapter = client->adapter;
686         int res = 0;
687
688         mutex_lock(&adapter->clist_lock);
689         if (__i2c_check_addr(client->adapter, client->addr)) {
690                 res = -EBUSY;
691                 goto out_unlock;
692         }
693         list_add_tail(&client->list,&adapter->clients);
694
695         client->dev.parent = &client->adapter->dev;
696         client->dev.bus = &i2c_bus_type;
697
698         if (client->driver)
699                 client->dev.driver = &client->driver->driver;
700
701         if (client->driver && !is_newstyle_driver(client->driver)) {
702                 client->dev.release = i2c_client_release;
703                 client->dev.uevent_suppress = 1;
704         } else
705                 client->dev.release = i2c_client_dev_release;
706
707         snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
708                 "%d-%04x", i2c_adapter_id(adapter), client->addr);
709         dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
710                 client->name, client->dev.bus_id);
711         res = device_register(&client->dev);
712         if (res)
713                 goto out_list;
714         mutex_unlock(&adapter->clist_lock);
715
716         if (adapter->client_register)  {
717                 if (adapter->client_register(client)) {
718                         dev_dbg(&adapter->dev, "client_register "
719                                 "failed for client [%s] at 0x%02x\n",
720                                 client->name, client->addr);
721                 }
722         }
723
724         return 0;
725
726 out_list:
727         list_del(&client->list);
728         dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
729                 "(%d)\n", client->name, client->addr, res);
730 out_unlock:
731         mutex_unlock(&adapter->clist_lock);
732         return res;
733 }
734 EXPORT_SYMBOL(i2c_attach_client);
735
736 int i2c_detach_client(struct i2c_client *client)
737 {
738         struct i2c_adapter *adapter = client->adapter;
739         int res = 0;
740
741         if (adapter->client_unregister)  {
742                 res = adapter->client_unregister(client);
743                 if (res) {
744                         dev_err(&client->dev,
745                                 "client_unregister [%s] failed, "
746                                 "client not detached\n", client->name);
747                         goto out;
748                 }
749         }
750
751         mutex_lock(&adapter->clist_lock);
752         list_del(&client->list);
753         init_completion(&client->released);
754         device_unregister(&client->dev);
755         mutex_unlock(&adapter->clist_lock);
756         wait_for_completion(&client->released);
757
758  out:
759         return res;
760 }
761 EXPORT_SYMBOL(i2c_detach_client);
762
763 /**
764  * i2c_use_client - increments the reference count of the i2c client structure
765  * @client: the client being referenced
766  *
767  * Each live reference to a client should be refcounted. The driver model does
768  * that automatically as part of driver binding, so that most drivers don't
769  * need to do this explicitly: they hold a reference until they're unbound
770  * from the device.
771  *
772  * A pointer to the client with the incremented reference counter is returned.
773  */
774 struct i2c_client *i2c_use_client(struct i2c_client *client)
775 {
776         get_device(&client->dev);
777         return client;
778 }
779 EXPORT_SYMBOL(i2c_use_client);
780
781 /**
782  * i2c_release_client - release a use of the i2c client structure
783  * @client: the client being no longer referenced
784  *
785  * Must be called when a user of a client is finished with it.
786  */
787 void i2c_release_client(struct i2c_client *client)
788 {
789         put_device(&client->dev);
790 }
791 EXPORT_SYMBOL(i2c_release_client);
792
793 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
794 {
795         struct list_head  *item;
796         struct i2c_client *client;
797
798         mutex_lock(&adap->clist_lock);
799         list_for_each(item,&adap->clients) {
800                 client = list_entry(item, struct i2c_client, list);
801                 if (!try_module_get(client->driver->driver.owner))
802                         continue;
803                 if (NULL != client->driver->command) {
804                         mutex_unlock(&adap->clist_lock);
805                         client->driver->command(client,cmd,arg);
806                         mutex_lock(&adap->clist_lock);
807                 }
808                 module_put(client->driver->driver.owner);
809        }
810        mutex_unlock(&adap->clist_lock);
811 }
812 EXPORT_SYMBOL(i2c_clients_command);
813
814 static int __init i2c_init(void)
815 {
816         int retval;
817
818         retval = bus_register(&i2c_bus_type);
819         if (retval)
820                 return retval;
821         return class_register(&i2c_adapter_class);
822 }
823
824 static void __exit i2c_exit(void)
825 {
826         class_unregister(&i2c_adapter_class);
827         bus_unregister(&i2c_bus_type);
828 }
829
830 subsys_initcall(i2c_init);
831 module_exit(i2c_exit);
832
833 /* ----------------------------------------------------
834  * the functional interface to the i2c busses.
835  * ----------------------------------------------------
836  */
837
838 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
839 {
840         int ret;
841
842         if (adap->algo->master_xfer) {
843 #ifdef DEBUG
844                 for (ret = 0; ret < num; ret++) {
845                         dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
846                                 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
847                                 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
848                                 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
849                 }
850 #endif
851
852                 mutex_lock_nested(&adap->bus_lock, adap->level);
853                 ret = adap->algo->master_xfer(adap,msgs,num);
854                 mutex_unlock(&adap->bus_lock);
855
856                 return ret;
857         } else {
858                 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
859                 return -ENOSYS;
860         }
861 }
862 EXPORT_SYMBOL(i2c_transfer);
863
864 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
865 {
866         int ret;
867         struct i2c_adapter *adap=client->adapter;
868         struct i2c_msg msg;
869
870         msg.addr = client->addr;
871         msg.flags = client->flags & I2C_M_TEN;
872         msg.len = count;
873         msg.buf = (char *)buf;
874
875         ret = i2c_transfer(adap, &msg, 1);
876
877         /* If everything went ok (i.e. 1 msg transmitted), return #bytes
878            transmitted, else error code. */
879         return (ret == 1) ? count : ret;
880 }
881 EXPORT_SYMBOL(i2c_master_send);
882
883 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
884 {
885         struct i2c_adapter *adap=client->adapter;
886         struct i2c_msg msg;
887         int ret;
888
889         msg.addr = client->addr;
890         msg.flags = client->flags & I2C_M_TEN;
891         msg.flags |= I2C_M_RD;
892         msg.len = count;
893         msg.buf = buf;
894
895         ret = i2c_transfer(adap, &msg, 1);
896
897         /* If everything went ok (i.e. 1 msg transmitted), return #bytes
898            transmitted, else error code. */
899         return (ret == 1) ? count : ret;
900 }
901 EXPORT_SYMBOL(i2c_master_recv);
902
903 /* ----------------------------------------------------
904  * the i2c address scanning function
905  * Will not work for 10-bit addresses!
906  * ----------------------------------------------------
907  */
908 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
909                              int (*found_proc) (struct i2c_adapter *, int, int))
910 {
911         int err;
912
913         /* Make sure the address is valid */
914         if (addr < 0x03 || addr > 0x77) {
915                 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
916                          addr);
917                 return -EINVAL;
918         }
919
920         /* Skip if already in use */
921         if (i2c_check_addr(adapter, addr))
922                 return 0;
923
924         /* Make sure there is something at this address, unless forced */
925         if (kind < 0) {
926                 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
927                                    I2C_SMBUS_QUICK, NULL) < 0)
928                         return 0;
929
930                 /* prevent 24RF08 corruption */
931                 if ((addr & ~0x0f) == 0x50)
932                         i2c_smbus_xfer(adapter, addr, 0, 0, 0,
933                                        I2C_SMBUS_QUICK, NULL);
934         }
935
936         /* Finally call the custom detection function */
937         err = found_proc(adapter, addr, kind);
938         /* -ENODEV can be returned if there is a chip at the given address
939            but it isn't supported by this chip driver. We catch it here as
940            this isn't an error. */
941         if (err == -ENODEV)
942                 err = 0;
943
944         if (err)
945                 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
946                          addr, err);
947         return err;
948 }
949
950 int i2c_probe(struct i2c_adapter *adapter,
951               const struct i2c_client_address_data *address_data,
952               int (*found_proc) (struct i2c_adapter *, int, int))
953 {
954         int i, err;
955         int adap_id = i2c_adapter_id(adapter);
956
957         /* Force entries are done first, and are not affected by ignore
958            entries */
959         if (address_data->forces) {
960                 const unsigned short * const *forces = address_data->forces;
961                 int kind;
962
963                 for (kind = 0; forces[kind]; kind++) {
964                         for (i = 0; forces[kind][i] != I2C_CLIENT_END;
965                              i += 2) {
966                                 if (forces[kind][i] == adap_id
967                                  || forces[kind][i] == ANY_I2C_BUS) {
968                                         dev_dbg(&adapter->dev, "found force "
969                                                 "parameter for adapter %d, "
970                                                 "addr 0x%02x, kind %d\n",
971                                                 adap_id, forces[kind][i + 1],
972                                                 kind);
973                                         err = i2c_probe_address(adapter,
974                                                 forces[kind][i + 1],
975                                                 kind, found_proc);
976                                         if (err)
977                                                 return err;
978                                 }
979                         }
980                 }
981         }
982
983         /* Stop here if we can't use SMBUS_QUICK */
984         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
985                 if (address_data->probe[0] == I2C_CLIENT_END
986                  && address_data->normal_i2c[0] == I2C_CLIENT_END)
987                         return 0;
988
989                 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
990                          "can't probe for chips\n");
991                 return -1;
992         }
993
994         /* Probe entries are done second, and are not affected by ignore
995            entries either */
996         for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
997                 if (address_data->probe[i] == adap_id
998                  || address_data->probe[i] == ANY_I2C_BUS) {
999                         dev_dbg(&adapter->dev, "found probe parameter for "
1000                                 "adapter %d, addr 0x%02x\n", adap_id,
1001                                 address_data->probe[i + 1]);
1002                         err = i2c_probe_address(adapter,
1003                                                 address_data->probe[i + 1],
1004                                                 -1, found_proc);
1005                         if (err)
1006                                 return err;
1007                 }
1008         }
1009
1010         /* Normal entries are done last, unless shadowed by an ignore entry */
1011         for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1012                 int j, ignore;
1013
1014                 ignore = 0;
1015                 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1016                      j += 2) {
1017                         if ((address_data->ignore[j] == adap_id ||
1018                              address_data->ignore[j] == ANY_I2C_BUS)
1019                          && address_data->ignore[j + 1]
1020                             == address_data->normal_i2c[i]) {
1021                                 dev_dbg(&adapter->dev, "found ignore "
1022                                         "parameter for adapter %d, "
1023                                         "addr 0x%02x\n", adap_id,
1024                                         address_data->ignore[j + 1]);
1025                                 ignore = 1;
1026                                 break;
1027                         }
1028                 }
1029                 if (ignore)
1030                         continue;
1031
1032                 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1033                         "addr 0x%02x\n", adap_id,
1034                         address_data->normal_i2c[i]);
1035                 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1036                                         -1, found_proc);
1037                 if (err)
1038                         return err;
1039         }
1040
1041         return 0;
1042 }
1043 EXPORT_SYMBOL(i2c_probe);
1044
1045 struct i2c_client *
1046 i2c_new_probed_device(struct i2c_adapter *adap,
1047                       struct i2c_board_info *info,
1048                       unsigned short const *addr_list)
1049 {
1050         int i;
1051
1052         /* Stop here if the bus doesn't support probing */
1053         if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1054                 dev_err(&adap->dev, "Probing not supported\n");
1055                 return NULL;
1056         }
1057
1058         mutex_lock(&adap->clist_lock);
1059         for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1060                 /* Check address validity */
1061                 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1062                         dev_warn(&adap->dev, "Invalid 7-bit address "
1063                                  "0x%02x\n", addr_list[i]);
1064                         continue;
1065                 }
1066
1067                 /* Check address availability */
1068                 if (__i2c_check_addr(adap, addr_list[i])) {
1069                         dev_dbg(&adap->dev, "Address 0x%02x already in "
1070                                 "use, not probing\n", addr_list[i]);
1071                         continue;
1072                 }
1073
1074                 /* Test address responsiveness
1075                    The default probe method is a quick write, but it is known
1076                    to corrupt the 24RF08 EEPROMs due to a state machine bug,
1077                    and could also irreversibly write-protect some EEPROMs, so
1078                    for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1079                    read instead. Also, some bus drivers don't implement
1080                    quick write, so we fallback to a byte read it that case
1081                    too. */
1082                 if ((addr_list[i] & ~0x07) == 0x30
1083                  || (addr_list[i] & ~0x0f) == 0x50
1084                  || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1085                         if (i2c_smbus_xfer(adap, addr_list[i], 0,
1086                                            I2C_SMBUS_READ, 0,
1087                                            I2C_SMBUS_BYTE, NULL) >= 0)
1088                                 break;
1089                 } else {
1090                         if (i2c_smbus_xfer(adap, addr_list[i], 0,
1091                                            I2C_SMBUS_WRITE, 0,
1092                                            I2C_SMBUS_QUICK, NULL) >= 0)
1093                                 break;
1094                 }
1095         }
1096         mutex_unlock(&adap->clist_lock);
1097
1098         if (addr_list[i] == I2C_CLIENT_END) {
1099                 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1100                 return NULL;
1101         }
1102
1103         info->addr = addr_list[i];
1104         return i2c_new_device(adap, info);
1105 }
1106 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1107
1108 struct i2c_adapter* i2c_get_adapter(int id)
1109 {
1110         struct i2c_adapter *adapter;
1111
1112         mutex_lock(&core_lists);
1113         adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1114         if (adapter && !try_module_get(adapter->owner))
1115                 adapter = NULL;
1116
1117         mutex_unlock(&core_lists);
1118         return adapter;
1119 }
1120 EXPORT_SYMBOL(i2c_get_adapter);
1121
1122 void i2c_put_adapter(struct i2c_adapter *adap)
1123 {
1124         module_put(adap->owner);
1125 }
1126 EXPORT_SYMBOL(i2c_put_adapter);
1127
1128 /* The SMBus parts */
1129
1130 #define POLY    (0x1070U << 3)
1131 static u8
1132 crc8(u16 data)
1133 {
1134         int i;
1135
1136         for(i = 0; i < 8; i++) {
1137                 if (data & 0x8000)
1138                         data = data ^ POLY;
1139                 data = data << 1;
1140         }
1141         return (u8)(data >> 8);
1142 }
1143
1144 /* Incremental CRC8 over count bytes in the array pointed to by p */
1145 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1146 {
1147         int i;
1148
1149         for(i = 0; i < count; i++)
1150                 crc = crc8((crc ^ p[i]) << 8);
1151         return crc;
1152 }
1153
1154 /* Assume a 7-bit address, which is reasonable for SMBus */
1155 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1156 {
1157         /* The address will be sent first */
1158         u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1159         pec = i2c_smbus_pec(pec, &addr, 1);
1160
1161         /* The data buffer follows */
1162         return i2c_smbus_pec(pec, msg->buf, msg->len);
1163 }
1164
1165 /* Used for write only transactions */
1166 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1167 {
1168         msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1169         msg->len++;
1170 }
1171
1172 /* Return <0 on CRC error
1173    If there was a write before this read (most cases) we need to take the
1174    partial CRC from the write part into account.
1175    Note that this function does modify the message (we need to decrease the
1176    message length to hide the CRC byte from the caller). */
1177 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1178 {
1179         u8 rpec = msg->buf[--msg->len];
1180         cpec = i2c_smbus_msg_pec(cpec, msg);
1181
1182         if (rpec != cpec) {
1183                 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1184                         rpec, cpec);
1185                 return -1;
1186         }
1187         return 0;
1188 }
1189
1190 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1191 {
1192         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1193                               value,0,I2C_SMBUS_QUICK,NULL);
1194 }
1195 EXPORT_SYMBOL(i2c_smbus_write_quick);
1196
1197 s32 i2c_smbus_read_byte(struct i2c_client *client)
1198 {
1199         union i2c_smbus_data data;
1200         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1201                            I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1202                 return -1;
1203         else
1204                 return data.byte;
1205 }
1206 EXPORT_SYMBOL(i2c_smbus_read_byte);
1207
1208 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1209 {
1210         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1211                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1212 }
1213 EXPORT_SYMBOL(i2c_smbus_write_byte);
1214
1215 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1216 {
1217         union i2c_smbus_data data;
1218         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1219                            I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1220                 return -1;
1221         else
1222                 return data.byte;
1223 }
1224 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1225
1226 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1227 {
1228         union i2c_smbus_data data;
1229         data.byte = value;
1230         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1231                               I2C_SMBUS_WRITE,command,
1232                               I2C_SMBUS_BYTE_DATA,&data);
1233 }
1234 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1235
1236 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1237 {
1238         union i2c_smbus_data data;
1239         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1240                            I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1241                 return -1;
1242         else
1243                 return data.word;
1244 }
1245 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1246
1247 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1248 {
1249         union i2c_smbus_data data;
1250         data.word = value;
1251         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1252                               I2C_SMBUS_WRITE,command,
1253                               I2C_SMBUS_WORD_DATA,&data);
1254 }
1255 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1256
1257 /**
1258  * i2c_smbus_read_block_data - SMBus block read request
1259  * @client: Handle to slave device
1260  * @command: Command byte issued to let the slave know what data should
1261  *      be returned
1262  * @values: Byte array into which data will be read; big enough to hold
1263  *      the data returned by the slave.  SMBus allows at most 32 bytes.
1264  *
1265  * Returns the number of bytes read in the slave's response, else a
1266  * negative number to indicate some kind of error.
1267  *
1268  * Note that using this function requires that the client's adapter support
1269  * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality.  Not all adapter drivers
1270  * support this; its emulation through I2C messaging relies on a specific
1271  * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1272  */
1273 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1274                               u8 *values)
1275 {
1276         union i2c_smbus_data data;
1277
1278         if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1279                            I2C_SMBUS_READ, command,
1280                            I2C_SMBUS_BLOCK_DATA, &data))
1281                 return -1;
1282
1283         memcpy(values, &data.block[1], data.block[0]);
1284         return data.block[0];
1285 }
1286 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1287
1288 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1289                                u8 length, const u8 *values)
1290 {
1291         union i2c_smbus_data data;
1292
1293         if (length > I2C_SMBUS_BLOCK_MAX)
1294                 length = I2C_SMBUS_BLOCK_MAX;
1295         data.block[0] = length;
1296         memcpy(&data.block[1], values, length);
1297         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1298                               I2C_SMBUS_WRITE,command,
1299                               I2C_SMBUS_BLOCK_DATA,&data);
1300 }
1301 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1302
1303 /* Returns the number of read bytes */
1304 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1305                                   u8 length, u8 *values)
1306 {
1307         union i2c_smbus_data data;
1308
1309         if (length > I2C_SMBUS_BLOCK_MAX)
1310                 length = I2C_SMBUS_BLOCK_MAX;
1311         data.block[0] = length;
1312         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1313                               I2C_SMBUS_READ,command,
1314                               I2C_SMBUS_I2C_BLOCK_DATA,&data))
1315                 return -1;
1316
1317         memcpy(values, &data.block[1], data.block[0]);
1318         return data.block[0];
1319 }
1320 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1321
1322 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1323                                    u8 length, const u8 *values)
1324 {
1325         union i2c_smbus_data data;
1326
1327         if (length > I2C_SMBUS_BLOCK_MAX)
1328                 length = I2C_SMBUS_BLOCK_MAX;
1329         data.block[0] = length;
1330         memcpy(data.block + 1, values, length);
1331         return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1332                               I2C_SMBUS_WRITE, command,
1333                               I2C_SMBUS_I2C_BLOCK_DATA, &data);
1334 }
1335 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1336
1337 /* Simulate a SMBus command using the i2c protocol
1338    No checking of parameters is done!  */
1339 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1340                                    unsigned short flags,
1341                                    char read_write, u8 command, int size,
1342                                    union i2c_smbus_data * data)
1343 {
1344         /* So we need to generate a series of msgs. In the case of writing, we
1345           need to use only one message; when reading, we need two. We initialize
1346           most things with sane defaults, to keep the code below somewhat
1347           simpler. */
1348         unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1349         unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1350         int num = read_write == I2C_SMBUS_READ?2:1;
1351         struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1352                                   { addr, flags | I2C_M_RD, 0, msgbuf1 }
1353                                 };
1354         int i;
1355         u8 partial_pec = 0;
1356
1357         msgbuf0[0] = command;
1358         switch(size) {
1359         case I2C_SMBUS_QUICK:
1360                 msg[0].len = 0;
1361                 /* Special case: The read/write field is used as data */
1362                 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1363                 num = 1;
1364                 break;
1365         case I2C_SMBUS_BYTE:
1366                 if (read_write == I2C_SMBUS_READ) {
1367                         /* Special case: only a read! */
1368                         msg[0].flags = I2C_M_RD | flags;
1369                         num = 1;
1370                 }
1371                 break;
1372         case I2C_SMBUS_BYTE_DATA:
1373                 if (read_write == I2C_SMBUS_READ)
1374                         msg[1].len = 1;
1375                 else {
1376                         msg[0].len = 2;
1377                         msgbuf0[1] = data->byte;
1378                 }
1379                 break;
1380         case I2C_SMBUS_WORD_DATA:
1381                 if (read_write == I2C_SMBUS_READ)
1382                         msg[1].len = 2;
1383                 else {
1384                         msg[0].len=3;
1385                         msgbuf0[1] = data->word & 0xff;
1386                         msgbuf0[2] = data->word >> 8;
1387                 }
1388                 break;
1389         case I2C_SMBUS_PROC_CALL:
1390                 num = 2; /* Special case */
1391                 read_write = I2C_SMBUS_READ;
1392                 msg[0].len = 3;
1393                 msg[1].len = 2;
1394                 msgbuf0[1] = data->word & 0xff;
1395                 msgbuf0[2] = data->word >> 8;
1396                 break;
1397         case I2C_SMBUS_BLOCK_DATA:
1398                 if (read_write == I2C_SMBUS_READ) {
1399                         msg[1].flags |= I2C_M_RECV_LEN;
1400                         msg[1].len = 1; /* block length will be added by
1401                                            the underlying bus driver */
1402                 } else {
1403                         msg[0].len = data->block[0] + 2;
1404                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1405                                 dev_err(&adapter->dev, "smbus_access called with "
1406                                        "invalid block write size (%d)\n",
1407                                        data->block[0]);
1408                                 return -1;
1409                         }
1410                         for (i = 1; i < msg[0].len; i++)
1411                                 msgbuf0[i] = data->block[i-1];
1412                 }
1413                 break;
1414         case I2C_SMBUS_BLOCK_PROC_CALL:
1415                 num = 2; /* Another special case */
1416                 read_write = I2C_SMBUS_READ;
1417                 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1418                         dev_err(&adapter->dev, "%s called with invalid "
1419                                 "block proc call size (%d)\n", __FUNCTION__,
1420                                 data->block[0]);
1421                         return -1;
1422                 }
1423                 msg[0].len = data->block[0] + 2;
1424                 for (i = 1; i < msg[0].len; i++)
1425                         msgbuf0[i] = data->block[i-1];
1426                 msg[1].flags |= I2C_M_RECV_LEN;
1427                 msg[1].len = 1; /* block length will be added by
1428                                    the underlying bus driver */
1429                 break;
1430         case I2C_SMBUS_I2C_BLOCK_DATA:
1431                 if (read_write == I2C_SMBUS_READ) {
1432                         msg[1].len = data->block[0];
1433                 } else {
1434                         msg[0].len = data->block[0] + 1;
1435                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1436                                 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1437                                        "invalid block write size (%d)\n",
1438                                        data->block[0]);
1439                                 return -1;
1440                         }
1441                         for (i = 1; i <= data->block[0]; i++)
1442                                 msgbuf0[i] = data->block[i];
1443                 }
1444                 break;
1445         default:
1446                 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1447                        size);
1448                 return -1;
1449         }
1450
1451         i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1452                                       && size != I2C_SMBUS_I2C_BLOCK_DATA);
1453         if (i) {
1454                 /* Compute PEC if first message is a write */
1455                 if (!(msg[0].flags & I2C_M_RD)) {
1456                         if (num == 1) /* Write only */
1457                                 i2c_smbus_add_pec(&msg[0]);
1458                         else /* Write followed by read */
1459                                 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1460                 }
1461                 /* Ask for PEC if last message is a read */
1462                 if (msg[num-1].flags & I2C_M_RD)
1463                         msg[num-1].len++;
1464         }
1465
1466         if (i2c_transfer(adapter, msg, num) < 0)
1467                 return -1;
1468
1469         /* Check PEC if last message is a read */
1470         if (i && (msg[num-1].flags & I2C_M_RD)) {
1471                 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1472                         return -1;
1473         }
1474
1475         if (read_write == I2C_SMBUS_READ)
1476                 switch(size) {
1477                         case I2C_SMBUS_BYTE:
1478                                 data->byte = msgbuf0[0];
1479                                 break;
1480                         case I2C_SMBUS_BYTE_DATA:
1481                                 data->byte = msgbuf1[0];
1482                                 break;
1483                         case I2C_SMBUS_WORD_DATA:
1484                         case I2C_SMBUS_PROC_CALL:
1485                                 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1486                                 break;
1487                         case I2C_SMBUS_I2C_BLOCK_DATA:
1488                                 for (i = 0; i < data->block[0]; i++)
1489                                         data->block[i+1] = msgbuf1[i];
1490                                 break;
1491                         case I2C_SMBUS_BLOCK_DATA:
1492                         case I2C_SMBUS_BLOCK_PROC_CALL:
1493                                 for (i = 0; i < msgbuf1[0] + 1; i++)
1494                                         data->block[i] = msgbuf1[i];
1495                                 break;
1496                 }
1497         return 0;
1498 }
1499
1500
1501 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1502                    char read_write, u8 command, int size,
1503                    union i2c_smbus_data * data)
1504 {
1505         s32 res;
1506
1507         flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1508
1509         if (adapter->algo->smbus_xfer) {
1510                 mutex_lock(&adapter->bus_lock);
1511                 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1512                                                 command,size,data);
1513                 mutex_unlock(&adapter->bus_lock);
1514         } else
1515                 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1516                                               command,size,data);
1517
1518         return res;
1519 }
1520 EXPORT_SYMBOL(i2c_smbus_xfer);
1521
1522 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1523 MODULE_DESCRIPTION("I2C-Bus main module");
1524 MODULE_LICENSE("GPL");