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