]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/i2c/i2c-core.c
i2c: Fix NULL pointer dereference in i2c_new_probed_device
[karo-tx-linux.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 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                         union i2c_smbus_data data;
1200
1201                         if (i2c_smbus_xfer(adap, addr_list[i], 0,
1202                                            I2C_SMBUS_READ, 0,
1203                                            I2C_SMBUS_BYTE, &data) >= 0)
1204                                 break;
1205                 } else {
1206                         if (i2c_smbus_xfer(adap, addr_list[i], 0,
1207                                            I2C_SMBUS_WRITE, 0,
1208                                            I2C_SMBUS_QUICK, NULL) >= 0)
1209                                 break;
1210                 }
1211         }
1212
1213         if (addr_list[i] == I2C_CLIENT_END) {
1214                 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1215                 return NULL;
1216         }
1217
1218         info->addr = addr_list[i];
1219         return i2c_new_device(adap, info);
1220 }
1221 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1222
1223 struct i2c_adapter* i2c_get_adapter(int id)
1224 {
1225         struct i2c_adapter *adapter;
1226
1227         mutex_lock(&core_lock);
1228         adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1229         if (adapter && !try_module_get(adapter->owner))
1230                 adapter = NULL;
1231
1232         mutex_unlock(&core_lock);
1233         return adapter;
1234 }
1235 EXPORT_SYMBOL(i2c_get_adapter);
1236
1237 void i2c_put_adapter(struct i2c_adapter *adap)
1238 {
1239         module_put(adap->owner);
1240 }
1241 EXPORT_SYMBOL(i2c_put_adapter);
1242
1243 /* The SMBus parts */
1244
1245 #define POLY    (0x1070U << 3)
1246 static u8
1247 crc8(u16 data)
1248 {
1249         int i;
1250
1251         for(i = 0; i < 8; i++) {
1252                 if (data & 0x8000)
1253                         data = data ^ POLY;
1254                 data = data << 1;
1255         }
1256         return (u8)(data >> 8);
1257 }
1258
1259 /* Incremental CRC8 over count bytes in the array pointed to by p */
1260 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1261 {
1262         int i;
1263
1264         for(i = 0; i < count; i++)
1265                 crc = crc8((crc ^ p[i]) << 8);
1266         return crc;
1267 }
1268
1269 /* Assume a 7-bit address, which is reasonable for SMBus */
1270 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1271 {
1272         /* The address will be sent first */
1273         u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1274         pec = i2c_smbus_pec(pec, &addr, 1);
1275
1276         /* The data buffer follows */
1277         return i2c_smbus_pec(pec, msg->buf, msg->len);
1278 }
1279
1280 /* Used for write only transactions */
1281 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1282 {
1283         msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1284         msg->len++;
1285 }
1286
1287 /* Return <0 on CRC error
1288    If there was a write before this read (most cases) we need to take the
1289    partial CRC from the write part into account.
1290    Note that this function does modify the message (we need to decrease the
1291    message length to hide the CRC byte from the caller). */
1292 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1293 {
1294         u8 rpec = msg->buf[--msg->len];
1295         cpec = i2c_smbus_msg_pec(cpec, msg);
1296
1297         if (rpec != cpec) {
1298                 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1299                         rpec, cpec);
1300                 return -1;
1301         }
1302         return 0;
1303 }
1304
1305 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1306 {
1307         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1308                               value,0,I2C_SMBUS_QUICK,NULL);
1309 }
1310 EXPORT_SYMBOL(i2c_smbus_write_quick);
1311
1312 s32 i2c_smbus_read_byte(struct i2c_client *client)
1313 {
1314         union i2c_smbus_data data;
1315         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1316                            I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1317                 return -1;
1318         else
1319                 return data.byte;
1320 }
1321 EXPORT_SYMBOL(i2c_smbus_read_byte);
1322
1323 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1324 {
1325         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1326                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1327 }
1328 EXPORT_SYMBOL(i2c_smbus_write_byte);
1329
1330 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1331 {
1332         union i2c_smbus_data data;
1333         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1334                            I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1335                 return -1;
1336         else
1337                 return data.byte;
1338 }
1339 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1340
1341 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1342 {
1343         union i2c_smbus_data data;
1344         data.byte = value;
1345         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1346                               I2C_SMBUS_WRITE,command,
1347                               I2C_SMBUS_BYTE_DATA,&data);
1348 }
1349 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1350
1351 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1352 {
1353         union i2c_smbus_data data;
1354         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1355                            I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1356                 return -1;
1357         else
1358                 return data.word;
1359 }
1360 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1361
1362 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1363 {
1364         union i2c_smbus_data data;
1365         data.word = value;
1366         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1367                               I2C_SMBUS_WRITE,command,
1368                               I2C_SMBUS_WORD_DATA,&data);
1369 }
1370 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1371
1372 /**
1373  * i2c_smbus_read_block_data - SMBus block read request
1374  * @client: Handle to slave device
1375  * @command: Command byte issued to let the slave know what data should
1376  *      be returned
1377  * @values: Byte array into which data will be read; big enough to hold
1378  *      the data returned by the slave.  SMBus allows at most 32 bytes.
1379  *
1380  * Returns the number of bytes read in the slave's response, else a
1381  * negative number to indicate some kind of error.
1382  *
1383  * Note that using this function requires that the client's adapter support
1384  * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality.  Not all adapter drivers
1385  * support this; its emulation through I2C messaging relies on a specific
1386  * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1387  */
1388 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1389                               u8 *values)
1390 {
1391         union i2c_smbus_data data;
1392
1393         if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1394                            I2C_SMBUS_READ, command,
1395                            I2C_SMBUS_BLOCK_DATA, &data))
1396                 return -1;
1397
1398         memcpy(values, &data.block[1], data.block[0]);
1399         return data.block[0];
1400 }
1401 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1402
1403 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1404                                u8 length, const u8 *values)
1405 {
1406         union i2c_smbus_data data;
1407
1408         if (length > I2C_SMBUS_BLOCK_MAX)
1409                 length = I2C_SMBUS_BLOCK_MAX;
1410         data.block[0] = length;
1411         memcpy(&data.block[1], values, length);
1412         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1413                               I2C_SMBUS_WRITE,command,
1414                               I2C_SMBUS_BLOCK_DATA,&data);
1415 }
1416 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1417
1418 /* Returns the number of read bytes */
1419 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1420                                   u8 length, u8 *values)
1421 {
1422         union i2c_smbus_data data;
1423
1424         if (length > I2C_SMBUS_BLOCK_MAX)
1425                 length = I2C_SMBUS_BLOCK_MAX;
1426         data.block[0] = length;
1427         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1428                               I2C_SMBUS_READ,command,
1429                               I2C_SMBUS_I2C_BLOCK_DATA,&data))
1430                 return -1;
1431
1432         memcpy(values, &data.block[1], data.block[0]);
1433         return data.block[0];
1434 }
1435 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1436
1437 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1438                                    u8 length, const u8 *values)
1439 {
1440         union i2c_smbus_data data;
1441
1442         if (length > I2C_SMBUS_BLOCK_MAX)
1443                 length = I2C_SMBUS_BLOCK_MAX;
1444         data.block[0] = length;
1445         memcpy(data.block + 1, values, length);
1446         return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1447                               I2C_SMBUS_WRITE, command,
1448                               I2C_SMBUS_I2C_BLOCK_DATA, &data);
1449 }
1450 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1451
1452 /* Simulate a SMBus command using the i2c protocol
1453    No checking of parameters is done!  */
1454 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1455                                    unsigned short flags,
1456                                    char read_write, u8 command, int size,
1457                                    union i2c_smbus_data * data)
1458 {
1459         /* So we need to generate a series of msgs. In the case of writing, we
1460           need to use only one message; when reading, we need two. We initialize
1461           most things with sane defaults, to keep the code below somewhat
1462           simpler. */
1463         unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1464         unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1465         int num = read_write == I2C_SMBUS_READ?2:1;
1466         struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1467                                   { addr, flags | I2C_M_RD, 0, msgbuf1 }
1468                                 };
1469         int i;
1470         u8 partial_pec = 0;
1471
1472         msgbuf0[0] = command;
1473         switch(size) {
1474         case I2C_SMBUS_QUICK:
1475                 msg[0].len = 0;
1476                 /* Special case: The read/write field is used as data */
1477                 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1478                 num = 1;
1479                 break;
1480         case I2C_SMBUS_BYTE:
1481                 if (read_write == I2C_SMBUS_READ) {
1482                         /* Special case: only a read! */
1483                         msg[0].flags = I2C_M_RD | flags;
1484                         num = 1;
1485                 }
1486                 break;
1487         case I2C_SMBUS_BYTE_DATA:
1488                 if (read_write == I2C_SMBUS_READ)
1489                         msg[1].len = 1;
1490                 else {
1491                         msg[0].len = 2;
1492                         msgbuf0[1] = data->byte;
1493                 }
1494                 break;
1495         case I2C_SMBUS_WORD_DATA:
1496                 if (read_write == I2C_SMBUS_READ)
1497                         msg[1].len = 2;
1498                 else {
1499                         msg[0].len=3;
1500                         msgbuf0[1] = data->word & 0xff;
1501                         msgbuf0[2] = data->word >> 8;
1502                 }
1503                 break;
1504         case I2C_SMBUS_PROC_CALL:
1505                 num = 2; /* Special case */
1506                 read_write = I2C_SMBUS_READ;
1507                 msg[0].len = 3;
1508                 msg[1].len = 2;
1509                 msgbuf0[1] = data->word & 0xff;
1510                 msgbuf0[2] = data->word >> 8;
1511                 break;
1512         case I2C_SMBUS_BLOCK_DATA:
1513                 if (read_write == I2C_SMBUS_READ) {
1514                         msg[1].flags |= I2C_M_RECV_LEN;
1515                         msg[1].len = 1; /* block length will be added by
1516                                            the underlying bus driver */
1517                 } else {
1518                         msg[0].len = data->block[0] + 2;
1519                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1520                                 dev_err(&adapter->dev, "smbus_access called with "
1521                                        "invalid block write size (%d)\n",
1522                                        data->block[0]);
1523                                 return -1;
1524                         }
1525                         for (i = 1; i < msg[0].len; i++)
1526                                 msgbuf0[i] = data->block[i-1];
1527                 }
1528                 break;
1529         case I2C_SMBUS_BLOCK_PROC_CALL:
1530                 num = 2; /* Another special case */
1531                 read_write = I2C_SMBUS_READ;
1532                 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1533                         dev_err(&adapter->dev, "%s called with invalid "
1534                                 "block proc call size (%d)\n", __func__,
1535                                 data->block[0]);
1536                         return -1;
1537                 }
1538                 msg[0].len = data->block[0] + 2;
1539                 for (i = 1; i < msg[0].len; i++)
1540                         msgbuf0[i] = data->block[i-1];
1541                 msg[1].flags |= I2C_M_RECV_LEN;
1542                 msg[1].len = 1; /* block length will be added by
1543                                    the underlying bus driver */
1544                 break;
1545         case I2C_SMBUS_I2C_BLOCK_DATA:
1546                 if (read_write == I2C_SMBUS_READ) {
1547                         msg[1].len = data->block[0];
1548                 } else {
1549                         msg[0].len = data->block[0] + 1;
1550                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1551                                 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1552                                        "invalid block write size (%d)\n",
1553                                        data->block[0]);
1554                                 return -1;
1555                         }
1556                         for (i = 1; i <= data->block[0]; i++)
1557                                 msgbuf0[i] = data->block[i];
1558                 }
1559                 break;
1560         default:
1561                 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1562                        size);
1563                 return -1;
1564         }
1565
1566         i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1567                                       && size != I2C_SMBUS_I2C_BLOCK_DATA);
1568         if (i) {
1569                 /* Compute PEC if first message is a write */
1570                 if (!(msg[0].flags & I2C_M_RD)) {
1571                         if (num == 1) /* Write only */
1572                                 i2c_smbus_add_pec(&msg[0]);
1573                         else /* Write followed by read */
1574                                 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1575                 }
1576                 /* Ask for PEC if last message is a read */
1577                 if (msg[num-1].flags & I2C_M_RD)
1578                         msg[num-1].len++;
1579         }
1580
1581         if (i2c_transfer(adapter, msg, num) < 0)
1582                 return -1;
1583
1584         /* Check PEC if last message is a read */
1585         if (i && (msg[num-1].flags & I2C_M_RD)) {
1586                 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1587                         return -1;
1588         }
1589
1590         if (read_write == I2C_SMBUS_READ)
1591                 switch(size) {
1592                         case I2C_SMBUS_BYTE:
1593                                 data->byte = msgbuf0[0];
1594                                 break;
1595                         case I2C_SMBUS_BYTE_DATA:
1596                                 data->byte = msgbuf1[0];
1597                                 break;
1598                         case I2C_SMBUS_WORD_DATA:
1599                         case I2C_SMBUS_PROC_CALL:
1600                                 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1601                                 break;
1602                         case I2C_SMBUS_I2C_BLOCK_DATA:
1603                                 for (i = 0; i < data->block[0]; i++)
1604                                         data->block[i+1] = msgbuf1[i];
1605                                 break;
1606                         case I2C_SMBUS_BLOCK_DATA:
1607                         case I2C_SMBUS_BLOCK_PROC_CALL:
1608                                 for (i = 0; i < msgbuf1[0] + 1; i++)
1609                                         data->block[i] = msgbuf1[i];
1610                                 break;
1611                 }
1612         return 0;
1613 }
1614
1615
1616 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1617                    char read_write, u8 command, int size,
1618                    union i2c_smbus_data * data)
1619 {
1620         s32 res;
1621
1622         flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1623
1624         if (adapter->algo->smbus_xfer) {
1625                 mutex_lock(&adapter->bus_lock);
1626                 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1627                                                 command,size,data);
1628                 mutex_unlock(&adapter->bus_lock);
1629         } else
1630                 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1631                                               command,size,data);
1632
1633         return res;
1634 }
1635 EXPORT_SYMBOL(i2c_smbus_xfer);
1636
1637 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1638 MODULE_DESCRIPTION("I2C-Bus main module");
1639 MODULE_LICENSE("GPL");