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