i2c: Rename main mutex
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / i2c-core.c
blob7161f913de1498ff8ba857b89ecdc184013392be
1 /* i2c-core.c - a device driver for the iic-bus interface */
2 /* ------------------------------------------------------------------------- */
3 /* Copyright (C) 1995-99 Simon G. Vogl
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.
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.
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 /* ------------------------------------------------------------------------- */
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> */
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 #include <asm/semaphore.h>
39 #include "i2c-core.h"
42 static DEFINE_MUTEX(core_lock);
43 static DEFINE_IDR(i2c_adapter_idr);
45 #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
47 /* ------------------------------------------------------------------------- */
49 static int i2c_device_match(struct device *dev, struct device_driver *drv)
51 struct i2c_client *client = to_i2c_client(dev);
52 struct i2c_driver *driver = to_i2c_driver(drv);
54 /* make legacy i2c drivers bypass driver model probing entirely;
55 * such drivers scan each i2c adapter/bus themselves.
57 if (!is_newstyle_driver(driver))
58 return 0;
60 /* new style drivers use the same kind of driver matching policy
61 * as platform devices or SPI: compare device and driver IDs.
63 return strcmp(client->driver_name, drv->name) == 0;
66 #ifdef CONFIG_HOTPLUG
68 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
69 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
71 struct i2c_client *client = to_i2c_client(dev);
73 /* by definition, legacy drivers can't hotplug */
74 if (dev->driver || !client->driver_name)
75 return 0;
77 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
78 return -ENOMEM;
79 dev_dbg(dev, "uevent\n");
80 return 0;
83 #else
84 #define i2c_device_uevent NULL
85 #endif /* CONFIG_HOTPLUG */
87 static int i2c_device_probe(struct device *dev)
89 struct i2c_client *client = to_i2c_client(dev);
90 struct i2c_driver *driver = to_i2c_driver(dev->driver);
92 if (!driver->probe)
93 return -ENODEV;
94 client->driver = driver;
95 dev_dbg(dev, "probe\n");
96 return driver->probe(client);
99 static int i2c_device_remove(struct device *dev)
101 struct i2c_client *client = to_i2c_client(dev);
102 struct i2c_driver *driver;
103 int status;
105 if (!dev->driver)
106 return 0;
108 driver = to_i2c_driver(dev->driver);
109 if (driver->remove) {
110 dev_dbg(dev, "remove\n");
111 status = driver->remove(client);
112 } else {
113 dev->driver = NULL;
114 status = 0;
116 if (status == 0)
117 client->driver = NULL;
118 return status;
121 static void i2c_device_shutdown(struct device *dev)
123 struct i2c_driver *driver;
125 if (!dev->driver)
126 return;
127 driver = to_i2c_driver(dev->driver);
128 if (driver->shutdown)
129 driver->shutdown(to_i2c_client(dev));
132 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
134 struct i2c_driver *driver;
136 if (!dev->driver)
137 return 0;
138 driver = to_i2c_driver(dev->driver);
139 if (!driver->suspend)
140 return 0;
141 return driver->suspend(to_i2c_client(dev), mesg);
144 static int i2c_device_resume(struct device * dev)
146 struct i2c_driver *driver;
148 if (!dev->driver)
149 return 0;
150 driver = to_i2c_driver(dev->driver);
151 if (!driver->resume)
152 return 0;
153 return driver->resume(to_i2c_client(dev));
156 static void i2c_client_release(struct device *dev)
158 struct i2c_client *client = to_i2c_client(dev);
159 complete(&client->released);
162 static void i2c_client_dev_release(struct device *dev)
164 kfree(to_i2c_client(dev));
167 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
169 struct i2c_client *client = to_i2c_client(dev);
170 return sprintf(buf, "%s\n", client->name);
173 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
175 struct i2c_client *client = to_i2c_client(dev);
176 return client->driver_name
177 ? sprintf(buf, "%s\n", client->driver_name)
178 : 0;
181 static struct device_attribute i2c_dev_attrs[] = {
182 __ATTR(name, S_IRUGO, show_client_name, NULL),
183 /* modalias helps coldplug: modprobe $(cat .../modalias) */
184 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
185 { },
188 static struct bus_type i2c_bus_type = {
189 .name = "i2c",
190 .dev_attrs = i2c_dev_attrs,
191 .match = i2c_device_match,
192 .uevent = i2c_device_uevent,
193 .probe = i2c_device_probe,
194 .remove = i2c_device_remove,
195 .shutdown = i2c_device_shutdown,
196 .suspend = i2c_device_suspend,
197 .resume = i2c_device_resume,
201 * i2c_new_device - instantiate an i2c device for use with a new style driver
202 * @adap: the adapter managing the device
203 * @info: describes one I2C device; bus_num is ignored
204 * Context: can sleep
206 * Create a device to work with a new style i2c driver, where binding is
207 * handled through driver model probe()/remove() methods. This call is not
208 * appropriate for use by mainboad initialization logic, which usually runs
209 * during an arch_initcall() long before any i2c_adapter could exist.
211 * This returns the new i2c client, which may be saved for later use with
212 * i2c_unregister_device(); or NULL to indicate an error.
214 struct i2c_client *
215 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
217 struct i2c_client *client;
218 int status;
220 client = kzalloc(sizeof *client, GFP_KERNEL);
221 if (!client)
222 return NULL;
224 client->adapter = adap;
226 client->dev.platform_data = info->platform_data;
227 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
229 client->flags = info->flags & ~I2C_CLIENT_WAKE;
230 client->addr = info->addr;
231 client->irq = info->irq;
233 strlcpy(client->driver_name, info->driver_name,
234 sizeof(client->driver_name));
235 strlcpy(client->name, info->type, sizeof(client->name));
237 /* a new style driver may be bound to this device when we
238 * return from this function, or any later moment (e.g. maybe
239 * hotplugging will load the driver module). and the device
240 * refcount model is the standard driver model one.
242 status = i2c_attach_client(client);
243 if (status < 0) {
244 kfree(client);
245 client = NULL;
247 return client;
249 EXPORT_SYMBOL_GPL(i2c_new_device);
253 * i2c_unregister_device - reverse effect of i2c_new_device()
254 * @client: value returned from i2c_new_device()
255 * Context: can sleep
257 void i2c_unregister_device(struct i2c_client *client)
259 struct i2c_adapter *adapter = client->adapter;
260 struct i2c_driver *driver = client->driver;
262 if (driver && !is_newstyle_driver(driver)) {
263 dev_err(&client->dev, "can't unregister devices "
264 "with legacy drivers\n");
265 WARN_ON(1);
266 return;
269 mutex_lock(&adapter->clist_lock);
270 list_del(&client->list);
271 mutex_unlock(&adapter->clist_lock);
273 device_unregister(&client->dev);
275 EXPORT_SYMBOL_GPL(i2c_unregister_device);
278 /* ------------------------------------------------------------------------- */
280 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
282 static void i2c_adapter_dev_release(struct device *dev)
284 struct i2c_adapter *adap = to_i2c_adapter(dev);
285 complete(&adap->dev_released);
288 static ssize_t
289 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
291 struct i2c_adapter *adap = to_i2c_adapter(dev);
292 return sprintf(buf, "%s\n", adap->name);
295 static struct device_attribute i2c_adapter_attrs[] = {
296 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
297 { },
300 static struct class i2c_adapter_class = {
301 .owner = THIS_MODULE,
302 .name = "i2c-adapter",
303 .dev_attrs = i2c_adapter_attrs,
306 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
308 struct i2c_devinfo *devinfo;
310 mutex_lock(&__i2c_board_lock);
311 list_for_each_entry(devinfo, &__i2c_board_list, list) {
312 if (devinfo->busnum == adapter->nr
313 && !i2c_new_device(adapter,
314 &devinfo->board_info))
315 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
316 i2c_adapter_id(adapter),
317 devinfo->board_info.addr);
319 mutex_unlock(&__i2c_board_lock);
322 static int i2c_do_add_adapter(struct device_driver *d, void *data)
324 struct i2c_driver *driver = to_i2c_driver(d);
325 struct i2c_adapter *adap = data;
327 if (driver->attach_adapter) {
328 /* We ignore the return code; if it fails, too bad */
329 driver->attach_adapter(adap);
331 return 0;
334 static int i2c_register_adapter(struct i2c_adapter *adap)
336 int res = 0, dummy;
338 mutex_init(&adap->bus_lock);
339 mutex_init(&adap->clist_lock);
340 INIT_LIST_HEAD(&adap->clients);
342 mutex_lock(&core_lock);
344 /* Add the adapter to the driver core.
345 * If the parent pointer is not set up,
346 * we add this adapter to the host bus.
348 if (adap->dev.parent == NULL) {
349 adap->dev.parent = &platform_bus;
350 pr_debug("I2C adapter driver [%s] forgot to specify "
351 "physical device\n", adap->name);
353 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
354 adap->dev.release = &i2c_adapter_dev_release;
355 adap->dev.class = &i2c_adapter_class;
356 res = device_register(&adap->dev);
357 if (res)
358 goto out_list;
360 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
362 /* create pre-declared device nodes for new-style drivers */
363 if (adap->nr < __i2c_first_dynamic_bus_num)
364 i2c_scan_static_board_info(adap);
366 /* let legacy drivers scan this bus for matching devices */
367 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
368 i2c_do_add_adapter);
370 out_unlock:
371 mutex_unlock(&core_lock);
372 return res;
374 out_list:
375 idr_remove(&i2c_adapter_idr, adap->nr);
376 goto out_unlock;
380 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
381 * @adapter: the adapter to add
382 * Context: can sleep
384 * This routine is used to declare an I2C adapter when its bus number
385 * doesn't matter. Examples: for I2C adapters dynamically added by
386 * USB links or PCI plugin cards.
388 * When this returns zero, a new bus number was allocated and stored
389 * in adap->nr, and the specified adapter became available for clients.
390 * Otherwise, a negative errno value is returned.
392 int i2c_add_adapter(struct i2c_adapter *adapter)
394 int id, res = 0;
396 retry:
397 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
398 return -ENOMEM;
400 mutex_lock(&core_lock);
401 /* "above" here means "above or equal to", sigh */
402 res = idr_get_new_above(&i2c_adapter_idr, adapter,
403 __i2c_first_dynamic_bus_num, &id);
404 mutex_unlock(&core_lock);
406 if (res < 0) {
407 if (res == -EAGAIN)
408 goto retry;
409 return res;
412 adapter->nr = id;
413 return i2c_register_adapter(adapter);
415 EXPORT_SYMBOL(i2c_add_adapter);
418 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
419 * @adap: the adapter to register (with adap->nr initialized)
420 * Context: can sleep
422 * This routine is used to declare an I2C adapter when its bus number
423 * matters. Example: for I2C adapters from system-on-chip CPUs, or
424 * otherwise built in to the system's mainboard, and where i2c_board_info
425 * is used to properly configure I2C devices.
427 * If no devices have pre-been declared for this bus, then be sure to
428 * register the adapter before any dynamically allocated ones. Otherwise
429 * the required bus ID may not be available.
431 * When this returns zero, the specified adapter became available for
432 * clients using the bus number provided in adap->nr. Also, the table
433 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
434 * and the appropriate driver model device nodes are created. Otherwise, a
435 * negative errno value is returned.
437 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
439 int id;
440 int status;
442 if (adap->nr & ~MAX_ID_MASK)
443 return -EINVAL;
445 retry:
446 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
447 return -ENOMEM;
449 mutex_lock(&core_lock);
450 /* "above" here means "above or equal to", sigh;
451 * we need the "equal to" result to force the result
453 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
454 if (status == 0 && id != adap->nr) {
455 status = -EBUSY;
456 idr_remove(&i2c_adapter_idr, id);
458 mutex_unlock(&core_lock);
459 if (status == -EAGAIN)
460 goto retry;
462 if (status == 0)
463 status = i2c_register_adapter(adap);
464 return status;
466 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
468 static int i2c_do_del_adapter(struct device_driver *d, void *data)
470 struct i2c_driver *driver = to_i2c_driver(d);
471 struct i2c_adapter *adapter = data;
472 int res;
474 if (!driver->detach_adapter)
475 return 0;
476 res = driver->detach_adapter(adapter);
477 if (res)
478 dev_err(&adapter->dev, "detach_adapter failed (%d) "
479 "for driver [%s]\n", res, driver->driver.name);
480 return res;
484 * i2c_del_adapter - unregister I2C adapter
485 * @adap: the adapter being unregistered
486 * Context: can sleep
488 * This unregisters an I2C adapter which was previously registered
489 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
491 int i2c_del_adapter(struct i2c_adapter *adap)
493 struct list_head *item, *_n;
494 struct i2c_client *client;
495 int res = 0;
497 mutex_lock(&core_lock);
499 /* First make sure that this adapter was ever added */
500 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
501 pr_debug("i2c-core: attempting to delete unregistered "
502 "adapter [%s]\n", adap->name);
503 res = -EINVAL;
504 goto out_unlock;
507 /* Tell drivers about this removal */
508 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
509 i2c_do_del_adapter);
510 if (res)
511 goto out_unlock;
513 /* detach any active clients. This must be done first, because
514 * it can fail; in which case we give up. */
515 list_for_each_safe(item, _n, &adap->clients) {
516 struct i2c_driver *driver;
518 client = list_entry(item, struct i2c_client, list);
519 driver = client->driver;
521 /* new style, follow standard driver model */
522 if (!driver || is_newstyle_driver(driver)) {
523 i2c_unregister_device(client);
524 continue;
527 /* legacy drivers create and remove clients themselves */
528 if ((res = driver->detach_client(client))) {
529 dev_err(&adap->dev, "detach_client failed for client "
530 "[%s] at address 0x%02x\n", client->name,
531 client->addr);
532 goto out_unlock;
536 /* clean up the sysfs representation */
537 init_completion(&adap->dev_released);
538 device_unregister(&adap->dev);
540 /* wait for sysfs to drop all references */
541 wait_for_completion(&adap->dev_released);
543 /* free bus id */
544 idr_remove(&i2c_adapter_idr, adap->nr);
546 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
548 out_unlock:
549 mutex_unlock(&core_lock);
550 return res;
552 EXPORT_SYMBOL(i2c_del_adapter);
555 /* ------------------------------------------------------------------------- */
558 * An i2c_driver is used with one or more i2c_client (device) nodes to access
559 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
560 * are two models for binding the driver to its device: "new style" drivers
561 * follow the standard Linux driver model and just respond to probe() calls
562 * issued if the driver core sees they match(); "legacy" drivers create device
563 * nodes themselves.
566 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
568 int res;
570 /* new style driver methods can't mix with legacy ones */
571 if (is_newstyle_driver(driver)) {
572 if (driver->attach_adapter || driver->detach_adapter
573 || driver->detach_client) {
574 printk(KERN_WARNING
575 "i2c-core: driver [%s] is confused\n",
576 driver->driver.name);
577 return -EINVAL;
581 /* add the driver to the list of i2c drivers in the driver core */
582 driver->driver.owner = owner;
583 driver->driver.bus = &i2c_bus_type;
585 /* for new style drivers, when registration returns the driver core
586 * will have called probe() for all matching-but-unbound devices.
588 res = driver_register(&driver->driver);
589 if (res)
590 return res;
592 mutex_lock(&core_lock);
594 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
596 /* legacy drivers scan i2c busses directly */
597 if (driver->attach_adapter) {
598 struct i2c_adapter *adapter;
600 down(&i2c_adapter_class.sem);
601 list_for_each_entry(adapter, &i2c_adapter_class.devices,
602 dev.node) {
603 driver->attach_adapter(adapter);
605 up(&i2c_adapter_class.sem);
608 mutex_unlock(&core_lock);
609 return 0;
611 EXPORT_SYMBOL(i2c_register_driver);
614 * i2c_del_driver - unregister I2C driver
615 * @driver: the driver being unregistered
616 * Context: can sleep
618 void i2c_del_driver(struct i2c_driver *driver)
620 struct list_head *item2, *_n;
621 struct i2c_client *client;
622 struct i2c_adapter *adap;
624 mutex_lock(&core_lock);
626 /* new-style driver? */
627 if (is_newstyle_driver(driver))
628 goto unregister;
630 /* Have a look at each adapter, if clients of this driver are still
631 * attached. If so, detach them to be able to kill the driver
632 * afterwards.
634 down(&i2c_adapter_class.sem);
635 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
636 if (driver->detach_adapter) {
637 if (driver->detach_adapter(adap)) {
638 dev_err(&adap->dev, "detach_adapter failed "
639 "for driver [%s]\n",
640 driver->driver.name);
642 } else {
643 list_for_each_safe(item2, _n, &adap->clients) {
644 client = list_entry(item2, struct i2c_client, list);
645 if (client->driver != driver)
646 continue;
647 dev_dbg(&adap->dev, "detaching client [%s] "
648 "at 0x%02x\n", client->name,
649 client->addr);
650 if (driver->detach_client(client)) {
651 dev_err(&adap->dev, "detach_client "
652 "failed for client [%s] at "
653 "0x%02x\n", client->name,
654 client->addr);
659 up(&i2c_adapter_class.sem);
661 unregister:
662 driver_unregister(&driver->driver);
663 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
665 mutex_unlock(&core_lock);
667 EXPORT_SYMBOL(i2c_del_driver);
669 /* ------------------------------------------------------------------------- */
671 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
673 struct list_head *item;
674 struct i2c_client *client;
676 list_for_each(item,&adapter->clients) {
677 client = list_entry(item, struct i2c_client, list);
678 if (client->addr == addr)
679 return -EBUSY;
681 return 0;
684 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
686 int rval;
688 mutex_lock(&adapter->clist_lock);
689 rval = __i2c_check_addr(adapter, addr);
690 mutex_unlock(&adapter->clist_lock);
692 return rval;
695 int i2c_attach_client(struct i2c_client *client)
697 struct i2c_adapter *adapter = client->adapter;
698 int res = 0;
700 mutex_lock(&adapter->clist_lock);
701 if (__i2c_check_addr(client->adapter, client->addr)) {
702 res = -EBUSY;
703 goto out_unlock;
705 list_add_tail(&client->list,&adapter->clients);
707 client->dev.parent = &client->adapter->dev;
708 client->dev.bus = &i2c_bus_type;
710 if (client->driver)
711 client->dev.driver = &client->driver->driver;
713 if (client->driver && !is_newstyle_driver(client->driver)) {
714 client->dev.release = i2c_client_release;
715 client->dev.uevent_suppress = 1;
716 } else
717 client->dev.release = i2c_client_dev_release;
719 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
720 "%d-%04x", i2c_adapter_id(adapter), client->addr);
721 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
722 client->name, client->dev.bus_id);
723 res = device_register(&client->dev);
724 if (res)
725 goto out_list;
726 mutex_unlock(&adapter->clist_lock);
728 if (adapter->client_register) {
729 if (adapter->client_register(client)) {
730 dev_dbg(&adapter->dev, "client_register "
731 "failed for client [%s] at 0x%02x\n",
732 client->name, client->addr);
736 return 0;
738 out_list:
739 list_del(&client->list);
740 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
741 "(%d)\n", client->name, client->addr, res);
742 out_unlock:
743 mutex_unlock(&adapter->clist_lock);
744 return res;
746 EXPORT_SYMBOL(i2c_attach_client);
748 int i2c_detach_client(struct i2c_client *client)
750 struct i2c_adapter *adapter = client->adapter;
751 int res = 0;
753 if (adapter->client_unregister) {
754 res = adapter->client_unregister(client);
755 if (res) {
756 dev_err(&client->dev,
757 "client_unregister [%s] failed, "
758 "client not detached\n", client->name);
759 goto out;
763 mutex_lock(&adapter->clist_lock);
764 list_del(&client->list);
765 init_completion(&client->released);
766 device_unregister(&client->dev);
767 mutex_unlock(&adapter->clist_lock);
768 wait_for_completion(&client->released);
770 out:
771 return res;
773 EXPORT_SYMBOL(i2c_detach_client);
776 * i2c_use_client - increments the reference count of the i2c client structure
777 * @client: the client being referenced
779 * Each live reference to a client should be refcounted. The driver model does
780 * that automatically as part of driver binding, so that most drivers don't
781 * need to do this explicitly: they hold a reference until they're unbound
782 * from the device.
784 * A pointer to the client with the incremented reference counter is returned.
786 struct i2c_client *i2c_use_client(struct i2c_client *client)
788 get_device(&client->dev);
789 return client;
791 EXPORT_SYMBOL(i2c_use_client);
794 * i2c_release_client - release a use of the i2c client structure
795 * @client: the client being no longer referenced
797 * Must be called when a user of a client is finished with it.
799 void i2c_release_client(struct i2c_client *client)
801 put_device(&client->dev);
803 EXPORT_SYMBOL(i2c_release_client);
805 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
807 struct list_head *item;
808 struct i2c_client *client;
810 mutex_lock(&adap->clist_lock);
811 list_for_each(item,&adap->clients) {
812 client = list_entry(item, struct i2c_client, list);
813 if (!try_module_get(client->driver->driver.owner))
814 continue;
815 if (NULL != client->driver->command) {
816 mutex_unlock(&adap->clist_lock);
817 client->driver->command(client,cmd,arg);
818 mutex_lock(&adap->clist_lock);
820 module_put(client->driver->driver.owner);
822 mutex_unlock(&adap->clist_lock);
824 EXPORT_SYMBOL(i2c_clients_command);
826 static int __init i2c_init(void)
828 int retval;
830 retval = bus_register(&i2c_bus_type);
831 if (retval)
832 return retval;
833 return class_register(&i2c_adapter_class);
836 static void __exit i2c_exit(void)
838 class_unregister(&i2c_adapter_class);
839 bus_unregister(&i2c_bus_type);
842 subsys_initcall(i2c_init);
843 module_exit(i2c_exit);
845 /* ----------------------------------------------------
846 * the functional interface to the i2c busses.
847 * ----------------------------------------------------
850 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
852 int ret;
854 if (adap->algo->master_xfer) {
855 #ifdef DEBUG
856 for (ret = 0; ret < num; ret++) {
857 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
858 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
859 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
860 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
862 #endif
864 mutex_lock_nested(&adap->bus_lock, adap->level);
865 ret = adap->algo->master_xfer(adap,msgs,num);
866 mutex_unlock(&adap->bus_lock);
868 return ret;
869 } else {
870 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
871 return -ENOSYS;
874 EXPORT_SYMBOL(i2c_transfer);
876 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
878 int ret;
879 struct i2c_adapter *adap=client->adapter;
880 struct i2c_msg msg;
882 msg.addr = client->addr;
883 msg.flags = client->flags & I2C_M_TEN;
884 msg.len = count;
885 msg.buf = (char *)buf;
887 ret = i2c_transfer(adap, &msg, 1);
889 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
890 transmitted, else error code. */
891 return (ret == 1) ? count : ret;
893 EXPORT_SYMBOL(i2c_master_send);
895 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
897 struct i2c_adapter *adap=client->adapter;
898 struct i2c_msg msg;
899 int ret;
901 msg.addr = client->addr;
902 msg.flags = client->flags & I2C_M_TEN;
903 msg.flags |= I2C_M_RD;
904 msg.len = count;
905 msg.buf = buf;
907 ret = i2c_transfer(adap, &msg, 1);
909 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
910 transmitted, else error code. */
911 return (ret == 1) ? count : ret;
913 EXPORT_SYMBOL(i2c_master_recv);
915 /* ----------------------------------------------------
916 * the i2c address scanning function
917 * Will not work for 10-bit addresses!
918 * ----------------------------------------------------
920 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
921 int (*found_proc) (struct i2c_adapter *, int, int))
923 int err;
925 /* Make sure the address is valid */
926 if (addr < 0x03 || addr > 0x77) {
927 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
928 addr);
929 return -EINVAL;
932 /* Skip if already in use */
933 if (i2c_check_addr(adapter, addr))
934 return 0;
936 /* Make sure there is something at this address, unless forced */
937 if (kind < 0) {
938 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
939 I2C_SMBUS_QUICK, NULL) < 0)
940 return 0;
942 /* prevent 24RF08 corruption */
943 if ((addr & ~0x0f) == 0x50)
944 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
945 I2C_SMBUS_QUICK, NULL);
948 /* Finally call the custom detection function */
949 err = found_proc(adapter, addr, kind);
950 /* -ENODEV can be returned if there is a chip at the given address
951 but it isn't supported by this chip driver. We catch it here as
952 this isn't an error. */
953 if (err == -ENODEV)
954 err = 0;
956 if (err)
957 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
958 addr, err);
959 return err;
962 int i2c_probe(struct i2c_adapter *adapter,
963 const struct i2c_client_address_data *address_data,
964 int (*found_proc) (struct i2c_adapter *, int, int))
966 int i, err;
967 int adap_id = i2c_adapter_id(adapter);
969 /* Force entries are done first, and are not affected by ignore
970 entries */
971 if (address_data->forces) {
972 const unsigned short * const *forces = address_data->forces;
973 int kind;
975 for (kind = 0; forces[kind]; kind++) {
976 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
977 i += 2) {
978 if (forces[kind][i] == adap_id
979 || forces[kind][i] == ANY_I2C_BUS) {
980 dev_dbg(&adapter->dev, "found force "
981 "parameter for adapter %d, "
982 "addr 0x%02x, kind %d\n",
983 adap_id, forces[kind][i + 1],
984 kind);
985 err = i2c_probe_address(adapter,
986 forces[kind][i + 1],
987 kind, found_proc);
988 if (err)
989 return err;
995 /* Stop here if we can't use SMBUS_QUICK */
996 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
997 if (address_data->probe[0] == I2C_CLIENT_END
998 && address_data->normal_i2c[0] == I2C_CLIENT_END)
999 return 0;
1001 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1002 "can't probe for chips\n");
1003 return -1;
1006 /* Probe entries are done second, and are not affected by ignore
1007 entries either */
1008 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1009 if (address_data->probe[i] == adap_id
1010 || address_data->probe[i] == ANY_I2C_BUS) {
1011 dev_dbg(&adapter->dev, "found probe parameter for "
1012 "adapter %d, addr 0x%02x\n", adap_id,
1013 address_data->probe[i + 1]);
1014 err = i2c_probe_address(adapter,
1015 address_data->probe[i + 1],
1016 -1, found_proc);
1017 if (err)
1018 return err;
1022 /* Normal entries are done last, unless shadowed by an ignore entry */
1023 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1024 int j, ignore;
1026 ignore = 0;
1027 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1028 j += 2) {
1029 if ((address_data->ignore[j] == adap_id ||
1030 address_data->ignore[j] == ANY_I2C_BUS)
1031 && address_data->ignore[j + 1]
1032 == address_data->normal_i2c[i]) {
1033 dev_dbg(&adapter->dev, "found ignore "
1034 "parameter for adapter %d, "
1035 "addr 0x%02x\n", adap_id,
1036 address_data->ignore[j + 1]);
1037 ignore = 1;
1038 break;
1041 if (ignore)
1042 continue;
1044 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1045 "addr 0x%02x\n", adap_id,
1046 address_data->normal_i2c[i]);
1047 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1048 -1, found_proc);
1049 if (err)
1050 return err;
1053 return 0;
1055 EXPORT_SYMBOL(i2c_probe);
1057 struct i2c_client *
1058 i2c_new_probed_device(struct i2c_adapter *adap,
1059 struct i2c_board_info *info,
1060 unsigned short const *addr_list)
1062 int i;
1064 /* Stop here if the bus doesn't support probing */
1065 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1066 dev_err(&adap->dev, "Probing not supported\n");
1067 return NULL;
1070 mutex_lock(&adap->clist_lock);
1071 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1072 /* Check address validity */
1073 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1074 dev_warn(&adap->dev, "Invalid 7-bit address "
1075 "0x%02x\n", addr_list[i]);
1076 continue;
1079 /* Check address availability */
1080 if (__i2c_check_addr(adap, addr_list[i])) {
1081 dev_dbg(&adap->dev, "Address 0x%02x already in "
1082 "use, not probing\n", addr_list[i]);
1083 continue;
1086 /* Test address responsiveness
1087 The default probe method is a quick write, but it is known
1088 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1089 and could also irreversibly write-protect some EEPROMs, so
1090 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1091 read instead. Also, some bus drivers don't implement
1092 quick write, so we fallback to a byte read it that case
1093 too. */
1094 if ((addr_list[i] & ~0x07) == 0x30
1095 || (addr_list[i] & ~0x0f) == 0x50
1096 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1097 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1098 I2C_SMBUS_READ, 0,
1099 I2C_SMBUS_BYTE, NULL) >= 0)
1100 break;
1101 } else {
1102 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1103 I2C_SMBUS_WRITE, 0,
1104 I2C_SMBUS_QUICK, NULL) >= 0)
1105 break;
1108 mutex_unlock(&adap->clist_lock);
1110 if (addr_list[i] == I2C_CLIENT_END) {
1111 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1112 return NULL;
1115 info->addr = addr_list[i];
1116 return i2c_new_device(adap, info);
1118 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1120 struct i2c_adapter* i2c_get_adapter(int id)
1122 struct i2c_adapter *adapter;
1124 mutex_lock(&core_lock);
1125 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1126 if (adapter && !try_module_get(adapter->owner))
1127 adapter = NULL;
1129 mutex_unlock(&core_lock);
1130 return adapter;
1132 EXPORT_SYMBOL(i2c_get_adapter);
1134 void i2c_put_adapter(struct i2c_adapter *adap)
1136 module_put(adap->owner);
1138 EXPORT_SYMBOL(i2c_put_adapter);
1140 /* The SMBus parts */
1142 #define POLY (0x1070U << 3)
1143 static u8
1144 crc8(u16 data)
1146 int i;
1148 for(i = 0; i < 8; i++) {
1149 if (data & 0x8000)
1150 data = data ^ POLY;
1151 data = data << 1;
1153 return (u8)(data >> 8);
1156 /* Incremental CRC8 over count bytes in the array pointed to by p */
1157 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1159 int i;
1161 for(i = 0; i < count; i++)
1162 crc = crc8((crc ^ p[i]) << 8);
1163 return crc;
1166 /* Assume a 7-bit address, which is reasonable for SMBus */
1167 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1169 /* The address will be sent first */
1170 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1171 pec = i2c_smbus_pec(pec, &addr, 1);
1173 /* The data buffer follows */
1174 return i2c_smbus_pec(pec, msg->buf, msg->len);
1177 /* Used for write only transactions */
1178 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1180 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1181 msg->len++;
1184 /* Return <0 on CRC error
1185 If there was a write before this read (most cases) we need to take the
1186 partial CRC from the write part into account.
1187 Note that this function does modify the message (we need to decrease the
1188 message length to hide the CRC byte from the caller). */
1189 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1191 u8 rpec = msg->buf[--msg->len];
1192 cpec = i2c_smbus_msg_pec(cpec, msg);
1194 if (rpec != cpec) {
1195 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1196 rpec, cpec);
1197 return -1;
1199 return 0;
1202 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1204 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1205 value,0,I2C_SMBUS_QUICK,NULL);
1207 EXPORT_SYMBOL(i2c_smbus_write_quick);
1209 s32 i2c_smbus_read_byte(struct i2c_client *client)
1211 union i2c_smbus_data data;
1212 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1213 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1214 return -1;
1215 else
1216 return data.byte;
1218 EXPORT_SYMBOL(i2c_smbus_read_byte);
1220 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1222 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1223 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1225 EXPORT_SYMBOL(i2c_smbus_write_byte);
1227 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1229 union i2c_smbus_data data;
1230 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1231 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1232 return -1;
1233 else
1234 return data.byte;
1236 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1238 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1240 union i2c_smbus_data data;
1241 data.byte = value;
1242 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1243 I2C_SMBUS_WRITE,command,
1244 I2C_SMBUS_BYTE_DATA,&data);
1246 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1248 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1250 union i2c_smbus_data data;
1251 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1252 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1253 return -1;
1254 else
1255 return data.word;
1257 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1259 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1261 union i2c_smbus_data data;
1262 data.word = value;
1263 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1264 I2C_SMBUS_WRITE,command,
1265 I2C_SMBUS_WORD_DATA,&data);
1267 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1270 * i2c_smbus_read_block_data - SMBus block read request
1271 * @client: Handle to slave device
1272 * @command: Command byte issued to let the slave know what data should
1273 * be returned
1274 * @values: Byte array into which data will be read; big enough to hold
1275 * the data returned by the slave. SMBus allows at most 32 bytes.
1277 * Returns the number of bytes read in the slave's response, else a
1278 * negative number to indicate some kind of error.
1280 * Note that using this function requires that the client's adapter support
1281 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1282 * support this; its emulation through I2C messaging relies on a specific
1283 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1285 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1286 u8 *values)
1288 union i2c_smbus_data data;
1290 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1291 I2C_SMBUS_READ, command,
1292 I2C_SMBUS_BLOCK_DATA, &data))
1293 return -1;
1295 memcpy(values, &data.block[1], data.block[0]);
1296 return data.block[0];
1298 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1300 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1301 u8 length, const u8 *values)
1303 union i2c_smbus_data data;
1305 if (length > I2C_SMBUS_BLOCK_MAX)
1306 length = I2C_SMBUS_BLOCK_MAX;
1307 data.block[0] = length;
1308 memcpy(&data.block[1], values, length);
1309 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1310 I2C_SMBUS_WRITE,command,
1311 I2C_SMBUS_BLOCK_DATA,&data);
1313 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1315 /* Returns the number of read bytes */
1316 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1317 u8 length, u8 *values)
1319 union i2c_smbus_data data;
1321 if (length > I2C_SMBUS_BLOCK_MAX)
1322 length = I2C_SMBUS_BLOCK_MAX;
1323 data.block[0] = length;
1324 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1325 I2C_SMBUS_READ,command,
1326 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1327 return -1;
1329 memcpy(values, &data.block[1], data.block[0]);
1330 return data.block[0];
1332 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1334 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1335 u8 length, const u8 *values)
1337 union i2c_smbus_data data;
1339 if (length > I2C_SMBUS_BLOCK_MAX)
1340 length = I2C_SMBUS_BLOCK_MAX;
1341 data.block[0] = length;
1342 memcpy(data.block + 1, values, length);
1343 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1344 I2C_SMBUS_WRITE, command,
1345 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1347 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1349 /* Simulate a SMBus command using the i2c protocol
1350 No checking of parameters is done! */
1351 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1352 unsigned short flags,
1353 char read_write, u8 command, int size,
1354 union i2c_smbus_data * data)
1356 /* So we need to generate a series of msgs. In the case of writing, we
1357 need to use only one message; when reading, we need two. We initialize
1358 most things with sane defaults, to keep the code below somewhat
1359 simpler. */
1360 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1361 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1362 int num = read_write == I2C_SMBUS_READ?2:1;
1363 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1364 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1366 int i;
1367 u8 partial_pec = 0;
1369 msgbuf0[0] = command;
1370 switch(size) {
1371 case I2C_SMBUS_QUICK:
1372 msg[0].len = 0;
1373 /* Special case: The read/write field is used as data */
1374 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1375 num = 1;
1376 break;
1377 case I2C_SMBUS_BYTE:
1378 if (read_write == I2C_SMBUS_READ) {
1379 /* Special case: only a read! */
1380 msg[0].flags = I2C_M_RD | flags;
1381 num = 1;
1383 break;
1384 case I2C_SMBUS_BYTE_DATA:
1385 if (read_write == I2C_SMBUS_READ)
1386 msg[1].len = 1;
1387 else {
1388 msg[0].len = 2;
1389 msgbuf0[1] = data->byte;
1391 break;
1392 case I2C_SMBUS_WORD_DATA:
1393 if (read_write == I2C_SMBUS_READ)
1394 msg[1].len = 2;
1395 else {
1396 msg[0].len=3;
1397 msgbuf0[1] = data->word & 0xff;
1398 msgbuf0[2] = data->word >> 8;
1400 break;
1401 case I2C_SMBUS_PROC_CALL:
1402 num = 2; /* Special case */
1403 read_write = I2C_SMBUS_READ;
1404 msg[0].len = 3;
1405 msg[1].len = 2;
1406 msgbuf0[1] = data->word & 0xff;
1407 msgbuf0[2] = data->word >> 8;
1408 break;
1409 case I2C_SMBUS_BLOCK_DATA:
1410 if (read_write == I2C_SMBUS_READ) {
1411 msg[1].flags |= I2C_M_RECV_LEN;
1412 msg[1].len = 1; /* block length will be added by
1413 the underlying bus driver */
1414 } else {
1415 msg[0].len = data->block[0] + 2;
1416 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1417 dev_err(&adapter->dev, "smbus_access called with "
1418 "invalid block write size (%d)\n",
1419 data->block[0]);
1420 return -1;
1422 for (i = 1; i < msg[0].len; i++)
1423 msgbuf0[i] = data->block[i-1];
1425 break;
1426 case I2C_SMBUS_BLOCK_PROC_CALL:
1427 num = 2; /* Another special case */
1428 read_write = I2C_SMBUS_READ;
1429 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1430 dev_err(&adapter->dev, "%s called with invalid "
1431 "block proc call size (%d)\n", __FUNCTION__,
1432 data->block[0]);
1433 return -1;
1435 msg[0].len = data->block[0] + 2;
1436 for (i = 1; i < msg[0].len; i++)
1437 msgbuf0[i] = data->block[i-1];
1438 msg[1].flags |= I2C_M_RECV_LEN;
1439 msg[1].len = 1; /* block length will be added by
1440 the underlying bus driver */
1441 break;
1442 case I2C_SMBUS_I2C_BLOCK_DATA:
1443 if (read_write == I2C_SMBUS_READ) {
1444 msg[1].len = data->block[0];
1445 } else {
1446 msg[0].len = data->block[0] + 1;
1447 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1448 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1449 "invalid block write size (%d)\n",
1450 data->block[0]);
1451 return -1;
1453 for (i = 1; i <= data->block[0]; i++)
1454 msgbuf0[i] = data->block[i];
1456 break;
1457 default:
1458 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1459 size);
1460 return -1;
1463 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1464 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1465 if (i) {
1466 /* Compute PEC if first message is a write */
1467 if (!(msg[0].flags & I2C_M_RD)) {
1468 if (num == 1) /* Write only */
1469 i2c_smbus_add_pec(&msg[0]);
1470 else /* Write followed by read */
1471 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1473 /* Ask for PEC if last message is a read */
1474 if (msg[num-1].flags & I2C_M_RD)
1475 msg[num-1].len++;
1478 if (i2c_transfer(adapter, msg, num) < 0)
1479 return -1;
1481 /* Check PEC if last message is a read */
1482 if (i && (msg[num-1].flags & I2C_M_RD)) {
1483 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1484 return -1;
1487 if (read_write == I2C_SMBUS_READ)
1488 switch(size) {
1489 case I2C_SMBUS_BYTE:
1490 data->byte = msgbuf0[0];
1491 break;
1492 case I2C_SMBUS_BYTE_DATA:
1493 data->byte = msgbuf1[0];
1494 break;
1495 case I2C_SMBUS_WORD_DATA:
1496 case I2C_SMBUS_PROC_CALL:
1497 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1498 break;
1499 case I2C_SMBUS_I2C_BLOCK_DATA:
1500 for (i = 0; i < data->block[0]; i++)
1501 data->block[i+1] = msgbuf1[i];
1502 break;
1503 case I2C_SMBUS_BLOCK_DATA:
1504 case I2C_SMBUS_BLOCK_PROC_CALL:
1505 for (i = 0; i < msgbuf1[0] + 1; i++)
1506 data->block[i] = msgbuf1[i];
1507 break;
1509 return 0;
1513 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1514 char read_write, u8 command, int size,
1515 union i2c_smbus_data * data)
1517 s32 res;
1519 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1521 if (adapter->algo->smbus_xfer) {
1522 mutex_lock(&adapter->bus_lock);
1523 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1524 command,size,data);
1525 mutex_unlock(&adapter->bus_lock);
1526 } else
1527 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1528 command,size,data);
1530 return res;
1532 EXPORT_SYMBOL(i2c_smbus_xfer);
1534 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1535 MODULE_DESCRIPTION("I2C-Bus main module");
1536 MODULE_LICENSE("GPL");