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