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