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