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/mutex.h>
33 #include <linux/completion.h>
34 #include <linux/hardirq.h>
35 #include <linux/irqflags.h>
36 #include <asm/uaccess.h>
41 static DEFINE_MUTEX(core_lock);
42 static DEFINE_IDR(i2c_adapter_idr);
44 #define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
46 static int i2c_attach_client(struct i2c_client *client);
47 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
49 /* ------------------------------------------------------------------------- */
51 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
52 const struct i2c_client *client)
55 if (strcmp(client->name, id->name) == 0)
62 static int i2c_device_match(struct device *dev, struct device_driver *drv)
64 struct i2c_client *client = to_i2c_client(dev);
65 struct i2c_driver *driver = to_i2c_driver(drv);
67 /* make legacy i2c drivers bypass driver model probing entirely;
68 * such drivers scan each i2c adapter/bus themselves.
70 if (!is_newstyle_driver(driver))
73 /* match on an id table if there is one */
75 return i2c_match_id(driver->id_table, client) != NULL;
82 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
83 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
85 struct i2c_client *client = to_i2c_client(dev);
87 if (add_uevent_var(env, "MODALIAS=%s%s",
88 I2C_MODULE_PREFIX, client->name))
90 dev_dbg(dev, "uevent\n");
95 #define i2c_device_uevent NULL
96 #endif /* CONFIG_HOTPLUG */
98 static int i2c_device_probe(struct device *dev)
100 struct i2c_client *client = to_i2c_client(dev);
101 struct i2c_driver *driver = to_i2c_driver(dev->driver);
104 if (!driver->probe || !driver->id_table)
106 client->driver = driver;
107 if (!device_can_wakeup(&client->dev))
108 device_init_wakeup(&client->dev,
109 client->flags & I2C_CLIENT_WAKE);
110 dev_dbg(dev, "probe\n");
112 status = driver->probe(client, i2c_match_id(driver->id_table, client));
114 client->driver = NULL;
118 static int i2c_device_remove(struct device *dev)
120 struct i2c_client *client = to_i2c_client(dev);
121 struct i2c_driver *driver;
127 driver = to_i2c_driver(dev->driver);
128 if (driver->remove) {
129 dev_dbg(dev, "remove\n");
130 status = driver->remove(client);
136 client->driver = NULL;
140 static void i2c_device_shutdown(struct device *dev)
142 struct i2c_driver *driver;
146 driver = to_i2c_driver(dev->driver);
147 if (driver->shutdown)
148 driver->shutdown(to_i2c_client(dev));
151 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
153 struct i2c_driver *driver;
157 driver = to_i2c_driver(dev->driver);
158 if (!driver->suspend)
160 return driver->suspend(to_i2c_client(dev), mesg);
163 static int i2c_device_resume(struct device *dev)
165 struct i2c_driver *driver;
169 driver = to_i2c_driver(dev->driver);
172 return driver->resume(to_i2c_client(dev));
175 static void i2c_client_release(struct device *dev)
177 struct i2c_client *client = to_i2c_client(dev);
178 complete(&client->released);
181 static void i2c_client_dev_release(struct device *dev)
183 kfree(to_i2c_client(dev));
187 show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
189 struct i2c_client *client = to_i2c_client(dev);
190 return sprintf(buf, "%s\n", client->name);
194 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
196 struct i2c_client *client = to_i2c_client(dev);
197 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
200 static struct device_attribute i2c_dev_attrs[] = {
201 __ATTR(name, S_IRUGO, show_client_name, NULL),
202 /* modalias helps coldplug: modprobe $(cat .../modalias) */
203 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
207 struct bus_type i2c_bus_type = {
209 .dev_attrs = i2c_dev_attrs,
210 .match = i2c_device_match,
211 .uevent = i2c_device_uevent,
212 .probe = i2c_device_probe,
213 .remove = i2c_device_remove,
214 .shutdown = i2c_device_shutdown,
215 .suspend = i2c_device_suspend,
216 .resume = i2c_device_resume,
218 EXPORT_SYMBOL_GPL(i2c_bus_type);
222 * i2c_verify_client - return parameter as i2c_client, or NULL
223 * @dev: device, probably from some driver model iterator
225 * When traversing the driver model tree, perhaps using driver model
226 * iterators like @device_for_each_child(), you can't assume very much
227 * about the nodes you find. Use this function to avoid oopses caused
228 * by wrongly treating some non-I2C device as an i2c_client.
230 struct i2c_client *i2c_verify_client(struct device *dev)
232 return (dev->bus == &i2c_bus_type)
236 EXPORT_SYMBOL(i2c_verify_client);
240 * i2c_new_device - instantiate an i2c device for use with a new style driver
241 * @adap: the adapter managing the device
242 * @info: describes one I2C device; bus_num is ignored
245 * Create a device to work with a new style i2c driver, where binding is
246 * handled through driver model probe()/remove() methods. This call is not
247 * appropriate for use by mainboad initialization logic, which usually runs
248 * during an arch_initcall() long before any i2c_adapter could exist.
250 * This returns the new i2c client, which may be saved for later use with
251 * i2c_unregister_device(); or NULL to indicate an error.
254 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
256 struct i2c_client *client;
259 client = kzalloc(sizeof *client, GFP_KERNEL);
263 client->adapter = adap;
265 client->dev.platform_data = info->platform_data;
268 client->dev.archdata = *info->archdata;
270 client->flags = info->flags;
271 client->addr = info->addr;
272 client->irq = info->irq;
274 strlcpy(client->name, info->type, sizeof(client->name));
276 /* a new style driver may be bound to this device when we
277 * return from this function, or any later moment (e.g. maybe
278 * hotplugging will load the driver module). and the device
279 * refcount model is the standard driver model one.
281 status = i2c_attach_client(client);
288 EXPORT_SYMBOL_GPL(i2c_new_device);
292 * i2c_unregister_device - reverse effect of i2c_new_device()
293 * @client: value returned from i2c_new_device()
296 void i2c_unregister_device(struct i2c_client *client)
298 struct i2c_adapter *adapter = client->adapter;
299 struct i2c_driver *driver = client->driver;
301 if (driver && !is_newstyle_driver(driver)) {
302 dev_err(&client->dev, "can't unregister devices "
303 "with legacy drivers\n");
308 mutex_lock(&adapter->clist_lock);
309 list_del(&client->list);
310 mutex_unlock(&adapter->clist_lock);
312 device_unregister(&client->dev);
314 EXPORT_SYMBOL_GPL(i2c_unregister_device);
317 static const struct i2c_device_id dummy_id[] = {
322 static int dummy_probe(struct i2c_client *client,
323 const struct i2c_device_id *id)
328 static int dummy_remove(struct i2c_client *client)
333 static struct i2c_driver dummy_driver = {
334 .driver.name = "dummy",
335 .probe = dummy_probe,
336 .remove = dummy_remove,
337 .id_table = dummy_id,
341 * i2c_new_dummy - return a new i2c device bound to a dummy driver
342 * @adapter: the adapter managing the device
343 * @address: seven bit address to be used
346 * This returns an I2C client bound to the "dummy" driver, intended for use
347 * with devices that consume multiple addresses. Examples of such chips
348 * include various EEPROMS (like 24c04 and 24c08 models).
350 * These dummy devices have two main uses. First, most I2C and SMBus calls
351 * except i2c_transfer() need a client handle; the dummy will be that handle.
352 * And second, this prevents the specified address from being bound to a
355 * This returns the new i2c client, which should be saved for later use with
356 * i2c_unregister_device(); or NULL to indicate an error.
358 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
360 struct i2c_board_info info = {
361 I2C_BOARD_INFO("dummy", address),
364 return i2c_new_device(adapter, &info);
366 EXPORT_SYMBOL_GPL(i2c_new_dummy);
368 /* ------------------------------------------------------------------------- */
370 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
372 static void i2c_adapter_dev_release(struct device *dev)
374 struct i2c_adapter *adap = to_i2c_adapter(dev);
375 complete(&adap->dev_released);
379 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
381 struct i2c_adapter *adap = to_i2c_adapter(dev);
382 return sprintf(buf, "%s\n", adap->name);
385 static struct device_attribute i2c_adapter_attrs[] = {
386 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
390 static struct class i2c_adapter_class = {
391 .owner = THIS_MODULE,
392 .name = "i2c-adapter",
393 .dev_attrs = i2c_adapter_attrs,
396 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
398 struct i2c_devinfo *devinfo;
400 mutex_lock(&__i2c_board_lock);
401 list_for_each_entry(devinfo, &__i2c_board_list, list) {
402 if (devinfo->busnum == adapter->nr
403 && !i2c_new_device(adapter,
404 &devinfo->board_info))
405 dev_err(&adapter->dev,
406 "Can't create device at 0x%02x\n",
407 devinfo->board_info.addr);
409 mutex_unlock(&__i2c_board_lock);
412 static int i2c_do_add_adapter(struct device_driver *d, void *data)
414 struct i2c_driver *driver = to_i2c_driver(d);
415 struct i2c_adapter *adap = data;
417 /* Detect supported devices on that bus, and instantiate them */
418 i2c_detect(adap, driver);
420 /* Let legacy drivers scan this bus for matching devices */
421 if (driver->attach_adapter) {
422 /* We ignore the return code; if it fails, too bad */
423 driver->attach_adapter(adap);
428 static int i2c_register_adapter(struct i2c_adapter *adap)
432 /* Can't register until after driver model init */
433 if (unlikely(WARN_ON(!i2c_bus_type.p)))
436 mutex_init(&adap->bus_lock);
437 mutex_init(&adap->clist_lock);
438 INIT_LIST_HEAD(&adap->clients);
440 mutex_lock(&core_lock);
442 /* Set default timeout to 1 second if not already set */
443 if (adap->timeout == 0)
446 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
447 adap->dev.release = &i2c_adapter_dev_release;
448 adap->dev.class = &i2c_adapter_class;
449 res = device_register(&adap->dev);
453 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
455 /* create pre-declared device nodes */
456 if (adap->nr < __i2c_first_dynamic_bus_num)
457 i2c_scan_static_board_info(adap);
460 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
464 mutex_unlock(&core_lock);
468 idr_remove(&i2c_adapter_idr, adap->nr);
473 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
474 * @adapter: the adapter to add
477 * This routine is used to declare an I2C adapter when its bus number
478 * doesn't matter. Examples: for I2C adapters dynamically added by
479 * USB links or PCI plugin cards.
481 * When this returns zero, a new bus number was allocated and stored
482 * in adap->nr, and the specified adapter became available for clients.
483 * Otherwise, a negative errno value is returned.
485 int i2c_add_adapter(struct i2c_adapter *adapter)
490 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
493 mutex_lock(&core_lock);
494 /* "above" here means "above or equal to", sigh */
495 res = idr_get_new_above(&i2c_adapter_idr, adapter,
496 __i2c_first_dynamic_bus_num, &id);
497 mutex_unlock(&core_lock);
506 return i2c_register_adapter(adapter);
508 EXPORT_SYMBOL(i2c_add_adapter);
511 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
512 * @adap: the adapter to register (with adap->nr initialized)
515 * This routine is used to declare an I2C adapter when its bus number
516 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
517 * or otherwise built in to the system's mainboard, and where i2c_board_info
518 * is used to properly configure I2C devices.
520 * If no devices have pre-been declared for this bus, then be sure to
521 * register the adapter before any dynamically allocated ones. Otherwise
522 * the required bus ID may not be available.
524 * When this returns zero, the specified adapter became available for
525 * clients using the bus number provided in adap->nr. Also, the table
526 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
527 * and the appropriate driver model device nodes are created. Otherwise, a
528 * negative errno value is returned.
530 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
535 if (adap->nr & ~MAX_ID_MASK)
539 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
542 mutex_lock(&core_lock);
543 /* "above" here means "above or equal to", sigh;
544 * we need the "equal to" result to force the result
546 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
547 if (status == 0 && id != adap->nr) {
549 idr_remove(&i2c_adapter_idr, id);
551 mutex_unlock(&core_lock);
552 if (status == -EAGAIN)
556 status = i2c_register_adapter(adap);
559 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
561 static int i2c_do_del_adapter(struct device_driver *d, void *data)
563 struct i2c_driver *driver = to_i2c_driver(d);
564 struct i2c_adapter *adapter = data;
565 struct i2c_client *client, *_n;
568 /* Remove the devices we created ourselves as the result of hardware
569 * probing (using a driver's detect method) */
570 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
571 if (client->adapter == adapter) {
572 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
573 client->name, client->addr);
574 list_del(&client->detected);
575 i2c_unregister_device(client);
579 if (!driver->detach_adapter)
581 res = driver->detach_adapter(adapter);
583 dev_err(&adapter->dev, "detach_adapter failed (%d) "
584 "for driver [%s]\n", res, driver->driver.name);
589 * i2c_del_adapter - unregister I2C adapter
590 * @adap: the adapter being unregistered
593 * This unregisters an I2C adapter which was previously registered
594 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
596 int i2c_del_adapter(struct i2c_adapter *adap)
598 struct i2c_client *client, *_n;
601 mutex_lock(&core_lock);
603 /* First make sure that this adapter was ever added */
604 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
605 pr_debug("i2c-core: attempting to delete unregistered "
606 "adapter [%s]\n", adap->name);
611 /* Tell drivers about this removal */
612 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
617 /* Detach any active clients */
618 list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
619 i2c_unregister_device(client);
622 /* clean up the sysfs representation */
623 init_completion(&adap->dev_released);
624 device_unregister(&adap->dev);
626 /* wait for sysfs to drop all references */
627 wait_for_completion(&adap->dev_released);
630 idr_remove(&i2c_adapter_idr, adap->nr);
632 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
634 /* Clear the device structure in case this adapter is ever going to be
636 memset(&adap->dev, 0, sizeof(adap->dev));
639 mutex_unlock(&core_lock);
642 EXPORT_SYMBOL(i2c_del_adapter);
645 /* ------------------------------------------------------------------------- */
647 static int __attach_adapter(struct device *dev, void *data)
649 struct i2c_adapter *adapter = to_i2c_adapter(dev);
650 struct i2c_driver *driver = data;
652 i2c_detect(adapter, driver);
654 /* Legacy drivers scan i2c busses directly */
655 if (driver->attach_adapter)
656 driver->attach_adapter(adapter);
662 * An i2c_driver is used with one or more i2c_client (device) nodes to access
663 * i2c slave chips, on a bus instance associated with some i2c_adapter.
666 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
670 /* Can't register until after driver model init */
671 if (unlikely(WARN_ON(!i2c_bus_type.p)))
674 /* add the driver to the list of i2c drivers in the driver core */
675 driver->driver.owner = owner;
676 driver->driver.bus = &i2c_bus_type;
678 /* When registration returns, the driver core
679 * will have called probe() for all matching-but-unbound devices.
681 res = driver_register(&driver->driver);
685 mutex_lock(&core_lock);
687 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
689 INIT_LIST_HEAD(&driver->clients);
690 /* Walk the adapters that are already present */
691 class_for_each_device(&i2c_adapter_class, NULL, driver,
694 mutex_unlock(&core_lock);
697 EXPORT_SYMBOL(i2c_register_driver);
699 static int __detach_adapter(struct device *dev, void *data)
701 struct i2c_adapter *adapter = to_i2c_adapter(dev);
702 struct i2c_driver *driver = data;
703 struct i2c_client *client, *_n;
705 /* Remove the devices we created ourselves as the result of hardware
706 * probing (using a driver's detect method) */
707 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
708 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
709 client->name, client->addr);
710 list_del(&client->detected);
711 i2c_unregister_device(client);
714 if (is_newstyle_driver(driver))
717 if (driver->detach_adapter) {
718 if (driver->detach_adapter(adapter))
719 dev_err(&adapter->dev,
720 "detach_adapter failed for driver [%s]\n",
721 driver->driver.name);
728 * i2c_del_driver - unregister I2C driver
729 * @driver: the driver being unregistered
732 void i2c_del_driver(struct i2c_driver *driver)
734 mutex_lock(&core_lock);
736 class_for_each_device(&i2c_adapter_class, NULL, driver,
739 driver_unregister(&driver->driver);
740 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
742 mutex_unlock(&core_lock);
744 EXPORT_SYMBOL(i2c_del_driver);
746 /* ------------------------------------------------------------------------- */
748 static int __i2c_check_addr(struct device *dev, void *addrp)
750 struct i2c_client *client = i2c_verify_client(dev);
751 int addr = *(int *)addrp;
753 if (client && client->addr == addr)
758 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
760 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
763 static int i2c_attach_client(struct i2c_client *client)
765 struct i2c_adapter *adapter = client->adapter;
768 /* Check for address business */
769 res = i2c_check_addr(adapter, client->addr);
773 client->dev.parent = &client->adapter->dev;
774 client->dev.bus = &i2c_bus_type;
777 client->dev.driver = &client->driver->driver;
779 if (client->driver && !is_newstyle_driver(client->driver)) {
780 client->dev.release = i2c_client_release;
781 dev_set_uevent_suppress(&client->dev, 1);
783 client->dev.release = i2c_client_dev_release;
785 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter),
787 res = device_register(&client->dev);
791 mutex_lock(&adapter->clist_lock);
792 list_add_tail(&client->list, &adapter->clients);
793 mutex_unlock(&adapter->clist_lock);
795 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
796 client->name, dev_name(&client->dev));
801 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
802 "(%d)\n", client->name, client->addr, res);
807 * i2c_use_client - increments the reference count of the i2c client structure
808 * @client: the client being referenced
810 * Each live reference to a client should be refcounted. The driver model does
811 * that automatically as part of driver binding, so that most drivers don't
812 * need to do this explicitly: they hold a reference until they're unbound
815 * A pointer to the client with the incremented reference counter is returned.
817 struct i2c_client *i2c_use_client(struct i2c_client *client)
819 if (client && get_device(&client->dev))
823 EXPORT_SYMBOL(i2c_use_client);
826 * i2c_release_client - release a use of the i2c client structure
827 * @client: the client being no longer referenced
829 * Must be called when a user of a client is finished with it.
831 void i2c_release_client(struct i2c_client *client)
834 put_device(&client->dev);
836 EXPORT_SYMBOL(i2c_release_client);
843 static int i2c_cmd(struct device *dev, void *_arg)
845 struct i2c_client *client = i2c_verify_client(dev);
846 struct i2c_cmd_arg *arg = _arg;
848 if (client && client->driver && client->driver->command)
849 client->driver->command(client, arg->cmd, arg->arg);
853 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
855 struct i2c_cmd_arg cmd_arg;
859 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
861 EXPORT_SYMBOL(i2c_clients_command);
863 static int __init i2c_init(void)
867 retval = bus_register(&i2c_bus_type);
870 retval = class_register(&i2c_adapter_class);
873 retval = i2c_add_driver(&dummy_driver);
879 class_unregister(&i2c_adapter_class);
881 bus_unregister(&i2c_bus_type);
885 static void __exit i2c_exit(void)
887 i2c_del_driver(&dummy_driver);
888 class_unregister(&i2c_adapter_class);
889 bus_unregister(&i2c_bus_type);
892 /* We must initialize early, because some subsystems register i2c drivers
893 * in subsys_initcall() code, but are linked (and initialized) before i2c.
895 postcore_initcall(i2c_init);
896 module_exit(i2c_exit);
898 /* ----------------------------------------------------
899 * the functional interface to the i2c busses.
900 * ----------------------------------------------------
904 * i2c_transfer - execute a single or combined I2C message
905 * @adap: Handle to I2C bus
906 * @msgs: One or more messages to execute before STOP is issued to
907 * terminate the operation; each message begins with a START.
908 * @num: Number of messages to be executed.
910 * Returns negative errno, else the number of messages executed.
912 * Note that there is no requirement that each message be sent to
913 * the same slave address, although that is the most common model.
915 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
917 unsigned long orig_jiffies;
920 /* REVISIT the fault reporting model here is weak:
922 * - When we get an error after receiving N bytes from a slave,
923 * there is no way to report "N".
925 * - When we get a NAK after transmitting N bytes to a slave,
926 * there is no way to report "N" ... or to let the master
927 * continue executing the rest of this combined message, if
928 * that's the appropriate response.
930 * - When for example "num" is two and we successfully complete
931 * the first message but get an error part way through the
932 * second, it's unclear whether that should be reported as
933 * one (discarding status on the second message) or errno
934 * (discarding status on the first one).
937 if (adap->algo->master_xfer) {
939 for (ret = 0; ret < num; ret++) {
940 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
941 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
942 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
943 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
947 if (in_atomic() || irqs_disabled()) {
948 ret = mutex_trylock(&adap->bus_lock);
950 /* I2C activity is ongoing. */
953 mutex_lock_nested(&adap->bus_lock, adap->level);
956 /* Retry automatically on arbitration loss */
957 orig_jiffies = jiffies;
958 for (ret = 0, try = 0; try <= adap->retries; try++) {
959 ret = adap->algo->master_xfer(adap, msgs, num);
962 if (time_after(jiffies, orig_jiffies + adap->timeout))
965 mutex_unlock(&adap->bus_lock);
969 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
973 EXPORT_SYMBOL(i2c_transfer);
976 * i2c_master_send - issue a single I2C message in master transmit mode
977 * @client: Handle to slave device
978 * @buf: Data that will be written to the slave
979 * @count: How many bytes to write
981 * Returns negative errno, or else the number of bytes written.
983 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
986 struct i2c_adapter *adap=client->adapter;
989 msg.addr = client->addr;
990 msg.flags = client->flags & I2C_M_TEN;
992 msg.buf = (char *)buf;
994 ret = i2c_transfer(adap, &msg, 1);
996 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
997 transmitted, else error code. */
998 return (ret == 1) ? count : ret;
1000 EXPORT_SYMBOL(i2c_master_send);
1003 * i2c_master_recv - issue a single I2C message in master receive mode
1004 * @client: Handle to slave device
1005 * @buf: Where to store data read from slave
1006 * @count: How many bytes to read
1008 * Returns negative errno, or else the number of bytes read.
1010 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1012 struct i2c_adapter *adap=client->adapter;
1016 msg.addr = client->addr;
1017 msg.flags = client->flags & I2C_M_TEN;
1018 msg.flags |= I2C_M_RD;
1022 ret = i2c_transfer(adap, &msg, 1);
1024 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1025 transmitted, else error code. */
1026 return (ret == 1) ? count : ret;
1028 EXPORT_SYMBOL(i2c_master_recv);
1030 /* ----------------------------------------------------
1031 * the i2c address scanning function
1032 * Will not work for 10-bit addresses!
1033 * ----------------------------------------------------
1036 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1037 struct i2c_driver *driver)
1039 struct i2c_board_info info;
1040 struct i2c_adapter *adapter = temp_client->adapter;
1041 int addr = temp_client->addr;
1044 /* Make sure the address is valid */
1045 if (addr < 0x03 || addr > 0x77) {
1046 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1051 /* Skip if already in use */
1052 if (i2c_check_addr(adapter, addr))
1055 /* Make sure there is something at this address, unless forced */
1057 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1058 I2C_SMBUS_QUICK, NULL) < 0)
1061 /* prevent 24RF08 corruption */
1062 if ((addr & ~0x0f) == 0x50)
1063 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1064 I2C_SMBUS_QUICK, NULL);
1067 /* Finally call the custom detection function */
1068 memset(&info, 0, sizeof(struct i2c_board_info));
1070 err = driver->detect(temp_client, kind, &info);
1072 /* -ENODEV is returned if the detection fails. We catch it
1073 here as this isn't an error. */
1074 return err == -ENODEV ? 0 : err;
1077 /* Consistency check */
1078 if (info.type[0] == '\0') {
1079 dev_err(&adapter->dev, "%s detection function provided "
1080 "no name for 0x%x\n", driver->driver.name,
1083 struct i2c_client *client;
1085 /* Detection succeeded, instantiate the device */
1086 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1087 info.type, info.addr);
1088 client = i2c_new_device(adapter, &info);
1090 list_add_tail(&client->detected, &driver->clients);
1092 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1093 info.type, info.addr);
1098 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1100 const struct i2c_client_address_data *address_data;
1101 struct i2c_client *temp_client;
1103 int adap_id = i2c_adapter_id(adapter);
1105 address_data = driver->address_data;
1106 if (!driver->detect || !address_data)
1109 /* Set up a temporary client to help detect callback */
1110 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1113 temp_client->adapter = adapter;
1115 /* Force entries are done first, and are not affected by ignore
1117 if (address_data->forces) {
1118 const unsigned short * const *forces = address_data->forces;
1121 for (kind = 0; forces[kind]; kind++) {
1122 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1124 if (forces[kind][i] == adap_id
1125 || forces[kind][i] == ANY_I2C_BUS) {
1126 dev_dbg(&adapter->dev, "found force "
1127 "parameter for adapter %d, "
1128 "addr 0x%02x, kind %d\n",
1129 adap_id, forces[kind][i + 1],
1131 temp_client->addr = forces[kind][i + 1];
1132 err = i2c_detect_address(temp_client,
1141 /* Stop here if the classes do not match */
1142 if (!(adapter->class & driver->class))
1145 /* Stop here if we can't use SMBUS_QUICK */
1146 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1147 if (address_data->probe[0] == I2C_CLIENT_END
1148 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1151 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1152 "can't probe for chips\n");
1157 /* Probe entries are done second, and are not affected by ignore
1159 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1160 if (address_data->probe[i] == adap_id
1161 || address_data->probe[i] == ANY_I2C_BUS) {
1162 dev_dbg(&adapter->dev, "found probe parameter for "
1163 "adapter %d, addr 0x%02x\n", adap_id,
1164 address_data->probe[i + 1]);
1165 temp_client->addr = address_data->probe[i + 1];
1166 err = i2c_detect_address(temp_client, -1, driver);
1172 /* Normal entries are done last, unless shadowed by an ignore entry */
1173 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1177 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1179 if ((address_data->ignore[j] == adap_id ||
1180 address_data->ignore[j] == ANY_I2C_BUS)
1181 && address_data->ignore[j + 1]
1182 == address_data->normal_i2c[i]) {
1183 dev_dbg(&adapter->dev, "found ignore "
1184 "parameter for adapter %d, "
1185 "addr 0x%02x\n", adap_id,
1186 address_data->ignore[j + 1]);
1194 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1195 "addr 0x%02x\n", adap_id,
1196 address_data->normal_i2c[i]);
1197 temp_client->addr = address_data->normal_i2c[i];
1198 err = i2c_detect_address(temp_client, -1, driver);
1209 i2c_new_probed_device(struct i2c_adapter *adap,
1210 struct i2c_board_info *info,
1211 unsigned short const *addr_list)
1215 /* Stop here if the bus doesn't support probing */
1216 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1217 dev_err(&adap->dev, "Probing not supported\n");
1221 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1222 /* Check address validity */
1223 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1224 dev_warn(&adap->dev, "Invalid 7-bit address "
1225 "0x%02x\n", addr_list[i]);
1229 /* Check address availability */
1230 if (i2c_check_addr(adap, addr_list[i])) {
1231 dev_dbg(&adap->dev, "Address 0x%02x already in "
1232 "use, not probing\n", addr_list[i]);
1236 /* Test address responsiveness
1237 The default probe method is a quick write, but it is known
1238 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1239 and could also irreversibly write-protect some EEPROMs, so
1240 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1241 read instead. Also, some bus drivers don't implement
1242 quick write, so we fallback to a byte read it that case
1244 if ((addr_list[i] & ~0x07) == 0x30
1245 || (addr_list[i] & ~0x0f) == 0x50
1246 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1247 union i2c_smbus_data data;
1249 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1251 I2C_SMBUS_BYTE, &data) >= 0)
1254 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1256 I2C_SMBUS_QUICK, NULL) >= 0)
1261 if (addr_list[i] == I2C_CLIENT_END) {
1262 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1266 info->addr = addr_list[i];
1267 return i2c_new_device(adap, info);
1269 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1271 struct i2c_adapter* i2c_get_adapter(int id)
1273 struct i2c_adapter *adapter;
1275 mutex_lock(&core_lock);
1276 adapter = idr_find(&i2c_adapter_idr, id);
1277 if (adapter && !try_module_get(adapter->owner))
1280 mutex_unlock(&core_lock);
1283 EXPORT_SYMBOL(i2c_get_adapter);
1285 void i2c_put_adapter(struct i2c_adapter *adap)
1287 module_put(adap->owner);
1289 EXPORT_SYMBOL(i2c_put_adapter);
1291 /* The SMBus parts */
1293 #define POLY (0x1070U << 3)
1294 static u8 crc8(u16 data)
1298 for(i = 0; i < 8; i++) {
1303 return (u8)(data >> 8);
1306 /* Incremental CRC8 over count bytes in the array pointed to by p */
1307 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1311 for(i = 0; i < count; i++)
1312 crc = crc8((crc ^ p[i]) << 8);
1316 /* Assume a 7-bit address, which is reasonable for SMBus */
1317 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1319 /* The address will be sent first */
1320 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1321 pec = i2c_smbus_pec(pec, &addr, 1);
1323 /* The data buffer follows */
1324 return i2c_smbus_pec(pec, msg->buf, msg->len);
1327 /* Used for write only transactions */
1328 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1330 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1334 /* Return <0 on CRC error
1335 If there was a write before this read (most cases) we need to take the
1336 partial CRC from the write part into account.
1337 Note that this function does modify the message (we need to decrease the
1338 message length to hide the CRC byte from the caller). */
1339 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1341 u8 rpec = msg->buf[--msg->len];
1342 cpec = i2c_smbus_msg_pec(cpec, msg);
1345 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1353 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1354 * @client: Handle to slave device
1356 * This executes the SMBus "receive byte" protocol, returning negative errno
1357 * else the byte received from the device.
1359 s32 i2c_smbus_read_byte(struct i2c_client *client)
1361 union i2c_smbus_data data;
1364 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1366 I2C_SMBUS_BYTE, &data);
1367 return (status < 0) ? status : data.byte;
1369 EXPORT_SYMBOL(i2c_smbus_read_byte);
1372 * i2c_smbus_write_byte - SMBus "send byte" protocol
1373 * @client: Handle to slave device
1374 * @value: Byte to be sent
1376 * This executes the SMBus "send byte" protocol, returning negative errno
1377 * else zero on success.
1379 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1381 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1382 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1384 EXPORT_SYMBOL(i2c_smbus_write_byte);
1387 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1388 * @client: Handle to slave device
1389 * @command: Byte interpreted by slave
1391 * This executes the SMBus "read byte" protocol, returning negative errno
1392 * else a data byte received from the device.
1394 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1396 union i2c_smbus_data data;
1399 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1400 I2C_SMBUS_READ, command,
1401 I2C_SMBUS_BYTE_DATA, &data);
1402 return (status < 0) ? status : data.byte;
1404 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1407 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1408 * @client: Handle to slave device
1409 * @command: Byte interpreted by slave
1410 * @value: Byte being written
1412 * This executes the SMBus "write byte" protocol, returning negative errno
1413 * else zero on success.
1415 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1417 union i2c_smbus_data data;
1419 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1420 I2C_SMBUS_WRITE,command,
1421 I2C_SMBUS_BYTE_DATA,&data);
1423 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1426 * i2c_smbus_read_word_data - SMBus "read word" protocol
1427 * @client: Handle to slave device
1428 * @command: Byte interpreted by slave
1430 * This executes the SMBus "read word" protocol, returning negative errno
1431 * else a 16-bit unsigned "word" received from the device.
1433 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1435 union i2c_smbus_data data;
1438 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1439 I2C_SMBUS_READ, command,
1440 I2C_SMBUS_WORD_DATA, &data);
1441 return (status < 0) ? status : data.word;
1443 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1446 * i2c_smbus_write_word_data - SMBus "write word" protocol
1447 * @client: Handle to slave device
1448 * @command: Byte interpreted by slave
1449 * @value: 16-bit "word" being written
1451 * This executes the SMBus "write word" protocol, returning negative errno
1452 * else zero on success.
1454 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1456 union i2c_smbus_data data;
1458 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1459 I2C_SMBUS_WRITE,command,
1460 I2C_SMBUS_WORD_DATA,&data);
1462 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1465 * i2c_smbus_process_call - SMBus "process call" protocol
1466 * @client: Handle to slave device
1467 * @command: Byte interpreted by slave
1468 * @value: 16-bit "word" being written
1470 * This executes the SMBus "process call" protocol, returning negative errno
1471 * else a 16-bit unsigned "word" received from the device.
1473 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1475 union i2c_smbus_data data;
1479 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1480 I2C_SMBUS_WRITE, command,
1481 I2C_SMBUS_PROC_CALL, &data);
1482 return (status < 0) ? status : data.word;
1484 EXPORT_SYMBOL(i2c_smbus_process_call);
1487 * i2c_smbus_read_block_data - SMBus "block read" protocol
1488 * @client: Handle to slave device
1489 * @command: Byte interpreted by slave
1490 * @values: Byte array into which data will be read; big enough to hold
1491 * the data returned by the slave. SMBus allows at most 32 bytes.
1493 * This executes the SMBus "block read" protocol, returning negative errno
1494 * else the number of data bytes in the slave's response.
1496 * Note that using this function requires that the client's adapter support
1497 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1498 * support this; its emulation through I2C messaging relies on a specific
1499 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1501 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1504 union i2c_smbus_data data;
1507 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1508 I2C_SMBUS_READ, command,
1509 I2C_SMBUS_BLOCK_DATA, &data);
1513 memcpy(values, &data.block[1], data.block[0]);
1514 return data.block[0];
1516 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1519 * i2c_smbus_write_block_data - SMBus "block write" protocol
1520 * @client: Handle to slave device
1521 * @command: Byte interpreted by slave
1522 * @length: Size of data block; SMBus allows at most 32 bytes
1523 * @values: Byte array which will be written.
1525 * This executes the SMBus "block write" protocol, returning negative errno
1526 * else zero on success.
1528 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1529 u8 length, const u8 *values)
1531 union i2c_smbus_data data;
1533 if (length > I2C_SMBUS_BLOCK_MAX)
1534 length = I2C_SMBUS_BLOCK_MAX;
1535 data.block[0] = length;
1536 memcpy(&data.block[1], values, length);
1537 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1538 I2C_SMBUS_WRITE,command,
1539 I2C_SMBUS_BLOCK_DATA,&data);
1541 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1543 /* Returns the number of read bytes */
1544 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1545 u8 length, u8 *values)
1547 union i2c_smbus_data data;
1550 if (length > I2C_SMBUS_BLOCK_MAX)
1551 length = I2C_SMBUS_BLOCK_MAX;
1552 data.block[0] = length;
1553 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1554 I2C_SMBUS_READ, command,
1555 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1559 memcpy(values, &data.block[1], data.block[0]);
1560 return data.block[0];
1562 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1564 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1565 u8 length, const u8 *values)
1567 union i2c_smbus_data data;
1569 if (length > I2C_SMBUS_BLOCK_MAX)
1570 length = I2C_SMBUS_BLOCK_MAX;
1571 data.block[0] = length;
1572 memcpy(data.block + 1, values, length);
1573 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1574 I2C_SMBUS_WRITE, command,
1575 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1577 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1579 /* Simulate a SMBus command using the i2c protocol
1580 No checking of parameters is done! */
1581 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1582 unsigned short flags,
1583 char read_write, u8 command, int size,
1584 union i2c_smbus_data * data)
1586 /* So we need to generate a series of msgs. In the case of writing, we
1587 need to use only one message; when reading, we need two. We initialize
1588 most things with sane defaults, to keep the code below somewhat
1590 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1591 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1592 int num = read_write == I2C_SMBUS_READ?2:1;
1593 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1594 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1600 msgbuf0[0] = command;
1602 case I2C_SMBUS_QUICK:
1604 /* Special case: The read/write field is used as data */
1605 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1609 case I2C_SMBUS_BYTE:
1610 if (read_write == I2C_SMBUS_READ) {
1611 /* Special case: only a read! */
1612 msg[0].flags = I2C_M_RD | flags;
1616 case I2C_SMBUS_BYTE_DATA:
1617 if (read_write == I2C_SMBUS_READ)
1621 msgbuf0[1] = data->byte;
1624 case I2C_SMBUS_WORD_DATA:
1625 if (read_write == I2C_SMBUS_READ)
1629 msgbuf0[1] = data->word & 0xff;
1630 msgbuf0[2] = data->word >> 8;
1633 case I2C_SMBUS_PROC_CALL:
1634 num = 2; /* Special case */
1635 read_write = I2C_SMBUS_READ;
1638 msgbuf0[1] = data->word & 0xff;
1639 msgbuf0[2] = data->word >> 8;
1641 case I2C_SMBUS_BLOCK_DATA:
1642 if (read_write == I2C_SMBUS_READ) {
1643 msg[1].flags |= I2C_M_RECV_LEN;
1644 msg[1].len = 1; /* block length will be added by
1645 the underlying bus driver */
1647 msg[0].len = data->block[0] + 2;
1648 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1649 dev_err(&adapter->dev,
1650 "Invalid block write size %d\n",
1654 for (i = 1; i < msg[0].len; i++)
1655 msgbuf0[i] = data->block[i-1];
1658 case I2C_SMBUS_BLOCK_PROC_CALL:
1659 num = 2; /* Another special case */
1660 read_write = I2C_SMBUS_READ;
1661 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1662 dev_err(&adapter->dev,
1663 "Invalid block write size %d\n",
1667 msg[0].len = data->block[0] + 2;
1668 for (i = 1; i < msg[0].len; i++)
1669 msgbuf0[i] = data->block[i-1];
1670 msg[1].flags |= I2C_M_RECV_LEN;
1671 msg[1].len = 1; /* block length will be added by
1672 the underlying bus driver */
1674 case I2C_SMBUS_I2C_BLOCK_DATA:
1675 if (read_write == I2C_SMBUS_READ) {
1676 msg[1].len = data->block[0];
1678 msg[0].len = data->block[0] + 1;
1679 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1680 dev_err(&adapter->dev,
1681 "Invalid block write size %d\n",
1685 for (i = 1; i <= data->block[0]; i++)
1686 msgbuf0[i] = data->block[i];
1690 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1694 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1695 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1697 /* Compute PEC if first message is a write */
1698 if (!(msg[0].flags & I2C_M_RD)) {
1699 if (num == 1) /* Write only */
1700 i2c_smbus_add_pec(&msg[0]);
1701 else /* Write followed by read */
1702 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1704 /* Ask for PEC if last message is a read */
1705 if (msg[num-1].flags & I2C_M_RD)
1709 status = i2c_transfer(adapter, msg, num);
1713 /* Check PEC if last message is a read */
1714 if (i && (msg[num-1].flags & I2C_M_RD)) {
1715 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1720 if (read_write == I2C_SMBUS_READ)
1722 case I2C_SMBUS_BYTE:
1723 data->byte = msgbuf0[0];
1725 case I2C_SMBUS_BYTE_DATA:
1726 data->byte = msgbuf1[0];
1728 case I2C_SMBUS_WORD_DATA:
1729 case I2C_SMBUS_PROC_CALL:
1730 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1732 case I2C_SMBUS_I2C_BLOCK_DATA:
1733 for (i = 0; i < data->block[0]; i++)
1734 data->block[i+1] = msgbuf1[i];
1736 case I2C_SMBUS_BLOCK_DATA:
1737 case I2C_SMBUS_BLOCK_PROC_CALL:
1738 for (i = 0; i < msgbuf1[0] + 1; i++)
1739 data->block[i] = msgbuf1[i];
1746 * i2c_smbus_xfer - execute SMBus protocol operations
1747 * @adapter: Handle to I2C bus
1748 * @addr: Address of SMBus slave on that bus
1749 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1750 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1751 * @command: Byte interpreted by slave, for protocols which use such bytes
1752 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1753 * @data: Data to be read or written
1755 * This executes an SMBus protocol operation, and returns a negative
1756 * errno code else zero on success.
1758 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1759 char read_write, u8 command, int protocol,
1760 union i2c_smbus_data *data)
1762 unsigned long orig_jiffies;
1766 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1768 if (adapter->algo->smbus_xfer) {
1769 mutex_lock(&adapter->bus_lock);
1771 /* Retry automatically on arbitration loss */
1772 orig_jiffies = jiffies;
1773 for (res = 0, try = 0; try <= adapter->retries; try++) {
1774 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1775 read_write, command,
1779 if (time_after(jiffies,
1780 orig_jiffies + adapter->timeout))
1783 mutex_unlock(&adapter->bus_lock);
1785 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1786 command, protocol, data);
1790 EXPORT_SYMBOL(i2c_smbus_xfer);
1792 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1793 MODULE_DESCRIPTION("I2C-Bus main module");
1794 MODULE_LICENSE("GPL");