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