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 static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
45 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
47 /* ------------------------------------------------------------------------- */
49 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
50 const struct i2c_client *client)
53 if (strcmp(client->name, id->name) == 0)
60 static int i2c_device_match(struct device *dev, struct device_driver *drv)
62 struct i2c_client *client = to_i2c_client(dev);
63 struct i2c_driver *driver = to_i2c_driver(drv);
65 /* match on an id table if there is one */
67 return i2c_match_id(driver->id_table, client) != NULL;
74 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
75 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
77 struct i2c_client *client = to_i2c_client(dev);
79 if (add_uevent_var(env, "MODALIAS=%s%s",
80 I2C_MODULE_PREFIX, client->name))
82 dev_dbg(dev, "uevent\n");
87 #define i2c_device_uevent NULL
88 #endif /* CONFIG_HOTPLUG */
90 static int i2c_device_probe(struct device *dev)
92 struct i2c_client *client = to_i2c_client(dev);
93 struct i2c_driver *driver = to_i2c_driver(dev->driver);
96 if (!driver->probe || !driver->id_table)
98 client->driver = driver;
99 if (!device_can_wakeup(&client->dev))
100 device_init_wakeup(&client->dev,
101 client->flags & I2C_CLIENT_WAKE);
102 dev_dbg(dev, "probe\n");
104 status = driver->probe(client, i2c_match_id(driver->id_table, client));
106 client->driver = NULL;
110 static int i2c_device_remove(struct device *dev)
112 struct i2c_client *client = to_i2c_client(dev);
113 struct i2c_driver *driver;
119 driver = to_i2c_driver(dev->driver);
120 if (driver->remove) {
121 dev_dbg(dev, "remove\n");
122 status = driver->remove(client);
128 client->driver = NULL;
132 static void i2c_device_shutdown(struct device *dev)
134 struct i2c_driver *driver;
138 driver = to_i2c_driver(dev->driver);
139 if (driver->shutdown)
140 driver->shutdown(to_i2c_client(dev));
143 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
145 struct i2c_driver *driver;
149 driver = to_i2c_driver(dev->driver);
150 if (!driver->suspend)
152 return driver->suspend(to_i2c_client(dev), mesg);
155 static int i2c_device_resume(struct device *dev)
157 struct i2c_driver *driver;
161 driver = to_i2c_driver(dev->driver);
164 return driver->resume(to_i2c_client(dev));
167 static void i2c_client_dev_release(struct device *dev)
169 kfree(to_i2c_client(dev));
173 show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
175 struct i2c_client *client = to_i2c_client(dev);
176 return sprintf(buf, "%s\n", client->name);
180 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
182 struct i2c_client *client = to_i2c_client(dev);
183 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
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),
193 struct bus_type i2c_bus_type = {
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,
204 EXPORT_SYMBOL_GPL(i2c_bus_type);
208 * i2c_verify_client - return parameter as i2c_client, or NULL
209 * @dev: device, probably from some driver model iterator
211 * When traversing the driver model tree, perhaps using driver model
212 * iterators like @device_for_each_child(), you can't assume very much
213 * about the nodes you find. Use this function to avoid oopses caused
214 * by wrongly treating some non-I2C device as an i2c_client.
216 struct i2c_client *i2c_verify_client(struct device *dev)
218 return (dev->bus == &i2c_bus_type)
222 EXPORT_SYMBOL(i2c_verify_client);
226 * i2c_new_device - instantiate an i2c device
227 * @adap: the adapter managing the device
228 * @info: describes one I2C device; bus_num is ignored
231 * Create an i2c device. Binding is handled through driver model
232 * probe()/remove() methods. A driver may be bound to this device when we
233 * return from this function, or any later moment (e.g. maybe hotplugging will
234 * load the driver module). This call is not appropriate for use by mainboard
235 * initialization logic, which usually runs during an arch_initcall() long
236 * before any i2c_adapter could exist.
238 * This returns the new i2c client, which may be saved for later use with
239 * i2c_unregister_device(); or NULL to indicate an error.
242 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
244 struct i2c_client *client;
247 client = kzalloc(sizeof *client, GFP_KERNEL);
251 client->adapter = adap;
253 client->dev.platform_data = info->platform_data;
256 client->dev.archdata = *info->archdata;
258 client->flags = info->flags;
259 client->addr = info->addr;
260 client->irq = info->irq;
262 strlcpy(client->name, info->type, sizeof(client->name));
264 /* Check for address business */
265 status = i2c_check_addr(adap, client->addr);
269 client->dev.parent = &client->adapter->dev;
270 client->dev.bus = &i2c_bus_type;
271 client->dev.release = i2c_client_dev_release;
273 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
275 status = device_register(&client->dev);
279 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
280 client->name, dev_name(&client->dev));
285 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
286 "(%d)\n", client->name, client->addr, status);
290 EXPORT_SYMBOL_GPL(i2c_new_device);
294 * i2c_unregister_device - reverse effect of i2c_new_device()
295 * @client: value returned from i2c_new_device()
298 void i2c_unregister_device(struct i2c_client *client)
300 device_unregister(&client->dev);
302 EXPORT_SYMBOL_GPL(i2c_unregister_device);
305 static const struct i2c_device_id dummy_id[] = {
310 static int dummy_probe(struct i2c_client *client,
311 const struct i2c_device_id *id)
316 static int dummy_remove(struct i2c_client *client)
321 static struct i2c_driver dummy_driver = {
322 .driver.name = "dummy",
323 .probe = dummy_probe,
324 .remove = dummy_remove,
325 .id_table = dummy_id,
329 * i2c_new_dummy - return a new i2c device bound to a dummy driver
330 * @adapter: the adapter managing the device
331 * @address: seven bit address to be used
334 * This returns an I2C client bound to the "dummy" driver, intended for use
335 * with devices that consume multiple addresses. Examples of such chips
336 * include various EEPROMS (like 24c04 and 24c08 models).
338 * These dummy devices have two main uses. First, most I2C and SMBus calls
339 * except i2c_transfer() need a client handle; the dummy will be that handle.
340 * And second, this prevents the specified address from being bound to a
343 * This returns the new i2c client, which should be saved for later use with
344 * i2c_unregister_device(); or NULL to indicate an error.
346 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
348 struct i2c_board_info info = {
349 I2C_BOARD_INFO("dummy", address),
352 return i2c_new_device(adapter, &info);
354 EXPORT_SYMBOL_GPL(i2c_new_dummy);
356 /* ------------------------------------------------------------------------- */
358 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
360 static void i2c_adapter_dev_release(struct device *dev)
362 struct i2c_adapter *adap = to_i2c_adapter(dev);
363 complete(&adap->dev_released);
367 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
369 struct i2c_adapter *adap = to_i2c_adapter(dev);
370 return sprintf(buf, "%s\n", adap->name);
373 static struct device_attribute i2c_adapter_attrs[] = {
374 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
378 static struct class i2c_adapter_class = {
379 .owner = THIS_MODULE,
380 .name = "i2c-adapter",
381 .dev_attrs = i2c_adapter_attrs,
384 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
386 struct i2c_devinfo *devinfo;
388 mutex_lock(&__i2c_board_lock);
389 list_for_each_entry(devinfo, &__i2c_board_list, list) {
390 if (devinfo->busnum == adapter->nr
391 && !i2c_new_device(adapter,
392 &devinfo->board_info))
393 dev_err(&adapter->dev,
394 "Can't create device at 0x%02x\n",
395 devinfo->board_info.addr);
397 mutex_unlock(&__i2c_board_lock);
400 static int i2c_do_add_adapter(struct device_driver *d, void *data)
402 struct i2c_driver *driver = to_i2c_driver(d);
403 struct i2c_adapter *adap = data;
405 /* Detect supported devices on that bus, and instantiate them */
406 i2c_detect(adap, driver);
408 /* Let legacy drivers scan this bus for matching devices */
409 if (driver->attach_adapter) {
410 /* We ignore the return code; if it fails, too bad */
411 driver->attach_adapter(adap);
416 static int i2c_register_adapter(struct i2c_adapter *adap)
420 /* Can't register until after driver model init */
421 if (unlikely(WARN_ON(!i2c_bus_type.p)))
424 mutex_init(&adap->bus_lock);
426 mutex_lock(&core_lock);
428 /* Set default timeout to 1 second if not already set */
429 if (adap->timeout == 0)
432 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
433 adap->dev.release = &i2c_adapter_dev_release;
434 adap->dev.class = &i2c_adapter_class;
435 res = device_register(&adap->dev);
439 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
441 /* create pre-declared device nodes */
442 if (adap->nr < __i2c_first_dynamic_bus_num)
443 i2c_scan_static_board_info(adap);
446 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
450 mutex_unlock(&core_lock);
454 idr_remove(&i2c_adapter_idr, adap->nr);
459 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
460 * @adapter: the adapter to add
463 * This routine is used to declare an I2C adapter when its bus number
464 * doesn't matter. Examples: for I2C adapters dynamically added by
465 * USB links or PCI plugin cards.
467 * When this returns zero, a new bus number was allocated and stored
468 * in adap->nr, and the specified adapter became available for clients.
469 * Otherwise, a negative errno value is returned.
471 int i2c_add_adapter(struct i2c_adapter *adapter)
476 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
479 mutex_lock(&core_lock);
480 /* "above" here means "above or equal to", sigh */
481 res = idr_get_new_above(&i2c_adapter_idr, adapter,
482 __i2c_first_dynamic_bus_num, &id);
483 mutex_unlock(&core_lock);
492 return i2c_register_adapter(adapter);
494 EXPORT_SYMBOL(i2c_add_adapter);
497 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
498 * @adap: the adapter to register (with adap->nr initialized)
501 * This routine is used to declare an I2C adapter when its bus number
502 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
503 * or otherwise built in to the system's mainboard, and where i2c_board_info
504 * is used to properly configure I2C devices.
506 * If no devices have pre-been declared for this bus, then be sure to
507 * register the adapter before any dynamically allocated ones. Otherwise
508 * the required bus ID may not be available.
510 * When this returns zero, the specified adapter became available for
511 * clients using the bus number provided in adap->nr. Also, the table
512 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
513 * and the appropriate driver model device nodes are created. Otherwise, a
514 * negative errno value is returned.
516 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
521 if (adap->nr & ~MAX_ID_MASK)
525 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
528 mutex_lock(&core_lock);
529 /* "above" here means "above or equal to", sigh;
530 * we need the "equal to" result to force the result
532 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
533 if (status == 0 && id != adap->nr) {
535 idr_remove(&i2c_adapter_idr, id);
537 mutex_unlock(&core_lock);
538 if (status == -EAGAIN)
542 status = i2c_register_adapter(adap);
545 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
547 static int i2c_do_del_adapter(struct device_driver *d, void *data)
549 struct i2c_driver *driver = to_i2c_driver(d);
550 struct i2c_adapter *adapter = data;
551 struct i2c_client *client, *_n;
554 /* Remove the devices we created ourselves as the result of hardware
555 * probing (using a driver's detect method) */
556 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
557 if (client->adapter == adapter) {
558 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
559 client->name, client->addr);
560 list_del(&client->detected);
561 i2c_unregister_device(client);
565 if (!driver->detach_adapter)
567 res = driver->detach_adapter(adapter);
569 dev_err(&adapter->dev, "detach_adapter failed (%d) "
570 "for driver [%s]\n", res, driver->driver.name);
574 static int __unregister_client(struct device *dev, void *dummy)
576 struct i2c_client *client = i2c_verify_client(dev);
578 i2c_unregister_device(client);
583 * i2c_del_adapter - unregister I2C adapter
584 * @adap: the adapter being unregistered
587 * This unregisters an I2C adapter which was previously registered
588 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
590 int i2c_del_adapter(struct i2c_adapter *adap)
594 mutex_lock(&core_lock);
596 /* First make sure that this adapter was ever added */
597 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
598 pr_debug("i2c-core: attempting to delete unregistered "
599 "adapter [%s]\n", adap->name);
604 /* Tell drivers about this removal */
605 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
610 /* Detach any active clients. This can't fail, thus we do not
611 checking the returned value. */
612 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
614 /* clean up the sysfs representation */
615 init_completion(&adap->dev_released);
616 device_unregister(&adap->dev);
618 /* wait for sysfs to drop all references */
619 wait_for_completion(&adap->dev_released);
622 idr_remove(&i2c_adapter_idr, adap->nr);
624 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
626 /* Clear the device structure in case this adapter is ever going to be
628 memset(&adap->dev, 0, sizeof(adap->dev));
631 mutex_unlock(&core_lock);
634 EXPORT_SYMBOL(i2c_del_adapter);
637 /* ------------------------------------------------------------------------- */
639 static int __attach_adapter(struct device *dev, void *data)
641 struct i2c_adapter *adapter = to_i2c_adapter(dev);
642 struct i2c_driver *driver = data;
644 i2c_detect(adapter, driver);
646 /* Legacy drivers scan i2c busses directly */
647 if (driver->attach_adapter)
648 driver->attach_adapter(adapter);
654 * An i2c_driver is used with one or more i2c_client (device) nodes to access
655 * i2c slave chips, on a bus instance associated with some i2c_adapter.
658 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
662 /* Can't register until after driver model init */
663 if (unlikely(WARN_ON(!i2c_bus_type.p)))
666 /* add the driver to the list of i2c drivers in the driver core */
667 driver->driver.owner = owner;
668 driver->driver.bus = &i2c_bus_type;
670 /* When registration returns, the driver core
671 * will have called probe() for all matching-but-unbound devices.
673 res = driver_register(&driver->driver);
677 mutex_lock(&core_lock);
679 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
681 INIT_LIST_HEAD(&driver->clients);
682 /* Walk the adapters that are already present */
683 class_for_each_device(&i2c_adapter_class, NULL, driver,
686 mutex_unlock(&core_lock);
689 EXPORT_SYMBOL(i2c_register_driver);
691 static int __detach_adapter(struct device *dev, void *data)
693 struct i2c_adapter *adapter = to_i2c_adapter(dev);
694 struct i2c_driver *driver = data;
695 struct i2c_client *client, *_n;
697 /* Remove the devices we created ourselves as the result of hardware
698 * probing (using a driver's detect method) */
699 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
700 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
701 client->name, client->addr);
702 list_del(&client->detected);
703 i2c_unregister_device(client);
706 if (driver->detach_adapter) {
707 if (driver->detach_adapter(adapter))
708 dev_err(&adapter->dev,
709 "detach_adapter failed for driver [%s]\n",
710 driver->driver.name);
717 * i2c_del_driver - unregister I2C driver
718 * @driver: the driver being unregistered
721 void i2c_del_driver(struct i2c_driver *driver)
723 mutex_lock(&core_lock);
725 class_for_each_device(&i2c_adapter_class, NULL, driver,
728 driver_unregister(&driver->driver);
729 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
731 mutex_unlock(&core_lock);
733 EXPORT_SYMBOL(i2c_del_driver);
735 /* ------------------------------------------------------------------------- */
737 static int __i2c_check_addr(struct device *dev, void *addrp)
739 struct i2c_client *client = i2c_verify_client(dev);
740 int addr = *(int *)addrp;
742 if (client && client->addr == addr)
747 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
749 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
753 * i2c_use_client - increments the reference count of the i2c client structure
754 * @client: the client being referenced
756 * Each live reference to a client should be refcounted. The driver model does
757 * that automatically as part of driver binding, so that most drivers don't
758 * need to do this explicitly: they hold a reference until they're unbound
761 * A pointer to the client with the incremented reference counter is returned.
763 struct i2c_client *i2c_use_client(struct i2c_client *client)
765 if (client && get_device(&client->dev))
769 EXPORT_SYMBOL(i2c_use_client);
772 * i2c_release_client - release a use of the i2c client structure
773 * @client: the client being no longer referenced
775 * Must be called when a user of a client is finished with it.
777 void i2c_release_client(struct i2c_client *client)
780 put_device(&client->dev);
782 EXPORT_SYMBOL(i2c_release_client);
789 static int i2c_cmd(struct device *dev, void *_arg)
791 struct i2c_client *client = i2c_verify_client(dev);
792 struct i2c_cmd_arg *arg = _arg;
794 if (client && client->driver && client->driver->command)
795 client->driver->command(client, arg->cmd, arg->arg);
799 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
801 struct i2c_cmd_arg cmd_arg;
805 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
807 EXPORT_SYMBOL(i2c_clients_command);
809 static int __init i2c_init(void)
813 retval = bus_register(&i2c_bus_type);
816 retval = class_register(&i2c_adapter_class);
819 retval = i2c_add_driver(&dummy_driver);
825 class_unregister(&i2c_adapter_class);
827 bus_unregister(&i2c_bus_type);
831 static void __exit i2c_exit(void)
833 i2c_del_driver(&dummy_driver);
834 class_unregister(&i2c_adapter_class);
835 bus_unregister(&i2c_bus_type);
838 /* We must initialize early, because some subsystems register i2c drivers
839 * in subsys_initcall() code, but are linked (and initialized) before i2c.
841 postcore_initcall(i2c_init);
842 module_exit(i2c_exit);
844 /* ----------------------------------------------------
845 * the functional interface to the i2c busses.
846 * ----------------------------------------------------
850 * i2c_transfer - execute a single or combined I2C message
851 * @adap: Handle to I2C bus
852 * @msgs: One or more messages to execute before STOP is issued to
853 * terminate the operation; each message begins with a START.
854 * @num: Number of messages to be executed.
856 * Returns negative errno, else the number of messages executed.
858 * Note that there is no requirement that each message be sent to
859 * the same slave address, although that is the most common model.
861 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
863 unsigned long orig_jiffies;
866 /* REVISIT the fault reporting model here is weak:
868 * - When we get an error after receiving N bytes from a slave,
869 * there is no way to report "N".
871 * - When we get a NAK after transmitting N bytes to a slave,
872 * there is no way to report "N" ... or to let the master
873 * continue executing the rest of this combined message, if
874 * that's the appropriate response.
876 * - When for example "num" is two and we successfully complete
877 * the first message but get an error part way through the
878 * second, it's unclear whether that should be reported as
879 * one (discarding status on the second message) or errno
880 * (discarding status on the first one).
883 if (adap->algo->master_xfer) {
885 for (ret = 0; ret < num; ret++) {
886 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
887 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
888 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
889 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
893 if (in_atomic() || irqs_disabled()) {
894 ret = mutex_trylock(&adap->bus_lock);
896 /* I2C activity is ongoing. */
899 mutex_lock_nested(&adap->bus_lock, adap->level);
902 /* Retry automatically on arbitration loss */
903 orig_jiffies = jiffies;
904 for (ret = 0, try = 0; try <= adap->retries; try++) {
905 ret = adap->algo->master_xfer(adap, msgs, num);
908 if (time_after(jiffies, orig_jiffies + adap->timeout))
911 mutex_unlock(&adap->bus_lock);
915 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
919 EXPORT_SYMBOL(i2c_transfer);
922 * i2c_master_send - issue a single I2C message in master transmit mode
923 * @client: Handle to slave device
924 * @buf: Data that will be written to the slave
925 * @count: How many bytes to write
927 * Returns negative errno, or else the number of bytes written.
929 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
932 struct i2c_adapter *adap=client->adapter;
935 msg.addr = client->addr;
936 msg.flags = client->flags & I2C_M_TEN;
938 msg.buf = (char *)buf;
940 ret = i2c_transfer(adap, &msg, 1);
942 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
943 transmitted, else error code. */
944 return (ret == 1) ? count : ret;
946 EXPORT_SYMBOL(i2c_master_send);
949 * i2c_master_recv - issue a single I2C message in master receive mode
950 * @client: Handle to slave device
951 * @buf: Where to store data read from slave
952 * @count: How many bytes to read
954 * Returns negative errno, or else the number of bytes read.
956 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
958 struct i2c_adapter *adap=client->adapter;
962 msg.addr = client->addr;
963 msg.flags = client->flags & I2C_M_TEN;
964 msg.flags |= I2C_M_RD;
968 ret = i2c_transfer(adap, &msg, 1);
970 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
971 transmitted, else error code. */
972 return (ret == 1) ? count : ret;
974 EXPORT_SYMBOL(i2c_master_recv);
976 /* ----------------------------------------------------
977 * the i2c address scanning function
978 * Will not work for 10-bit addresses!
979 * ----------------------------------------------------
982 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
983 struct i2c_driver *driver)
985 struct i2c_board_info info;
986 struct i2c_adapter *adapter = temp_client->adapter;
987 int addr = temp_client->addr;
990 /* Make sure the address is valid */
991 if (addr < 0x03 || addr > 0x77) {
992 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
997 /* Skip if already in use */
998 if (i2c_check_addr(adapter, addr))
1001 /* Make sure there is something at this address, unless forced */
1003 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1004 I2C_SMBUS_QUICK, NULL) < 0)
1007 /* prevent 24RF08 corruption */
1008 if ((addr & ~0x0f) == 0x50)
1009 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1010 I2C_SMBUS_QUICK, NULL);
1013 /* Finally call the custom detection function */
1014 memset(&info, 0, sizeof(struct i2c_board_info));
1016 err = driver->detect(temp_client, kind, &info);
1018 /* -ENODEV is returned if the detection fails. We catch it
1019 here as this isn't an error. */
1020 return err == -ENODEV ? 0 : err;
1023 /* Consistency check */
1024 if (info.type[0] == '\0') {
1025 dev_err(&adapter->dev, "%s detection function provided "
1026 "no name for 0x%x\n", driver->driver.name,
1029 struct i2c_client *client;
1031 /* Detection succeeded, instantiate the device */
1032 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1033 info.type, info.addr);
1034 client = i2c_new_device(adapter, &info);
1036 list_add_tail(&client->detected, &driver->clients);
1038 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1039 info.type, info.addr);
1044 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1046 const struct i2c_client_address_data *address_data;
1047 struct i2c_client *temp_client;
1049 int adap_id = i2c_adapter_id(adapter);
1051 address_data = driver->address_data;
1052 if (!driver->detect || !address_data)
1055 /* Set up a temporary client to help detect callback */
1056 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1059 temp_client->adapter = adapter;
1061 /* Force entries are done first, and are not affected by ignore
1063 if (address_data->forces) {
1064 const unsigned short * const *forces = address_data->forces;
1067 for (kind = 0; forces[kind]; kind++) {
1068 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1070 if (forces[kind][i] == adap_id
1071 || forces[kind][i] == ANY_I2C_BUS) {
1072 dev_dbg(&adapter->dev, "found force "
1073 "parameter for adapter %d, "
1074 "addr 0x%02x, kind %d\n",
1075 adap_id, forces[kind][i + 1],
1077 temp_client->addr = forces[kind][i + 1];
1078 err = i2c_detect_address(temp_client,
1087 /* Stop here if the classes do not match */
1088 if (!(adapter->class & driver->class))
1091 /* Stop here if we can't use SMBUS_QUICK */
1092 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1093 if (address_data->probe[0] == I2C_CLIENT_END
1094 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1097 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1098 "can't probe for chips\n");
1103 /* Probe entries are done second, and are not affected by ignore
1105 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1106 if (address_data->probe[i] == adap_id
1107 || address_data->probe[i] == ANY_I2C_BUS) {
1108 dev_dbg(&adapter->dev, "found probe parameter for "
1109 "adapter %d, addr 0x%02x\n", adap_id,
1110 address_data->probe[i + 1]);
1111 temp_client->addr = address_data->probe[i + 1];
1112 err = i2c_detect_address(temp_client, -1, driver);
1118 /* Normal entries are done last, unless shadowed by an ignore entry */
1119 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1123 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1125 if ((address_data->ignore[j] == adap_id ||
1126 address_data->ignore[j] == ANY_I2C_BUS)
1127 && address_data->ignore[j + 1]
1128 == address_data->normal_i2c[i]) {
1129 dev_dbg(&adapter->dev, "found ignore "
1130 "parameter for adapter %d, "
1131 "addr 0x%02x\n", adap_id,
1132 address_data->ignore[j + 1]);
1140 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1141 "addr 0x%02x\n", adap_id,
1142 address_data->normal_i2c[i]);
1143 temp_client->addr = address_data->normal_i2c[i];
1144 err = i2c_detect_address(temp_client, -1, driver);
1155 i2c_new_probed_device(struct i2c_adapter *adap,
1156 struct i2c_board_info *info,
1157 unsigned short const *addr_list)
1161 /* Stop here if the bus doesn't support probing */
1162 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1163 dev_err(&adap->dev, "Probing not supported\n");
1167 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1168 /* Check address validity */
1169 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1170 dev_warn(&adap->dev, "Invalid 7-bit address "
1171 "0x%02x\n", addr_list[i]);
1175 /* Check address availability */
1176 if (i2c_check_addr(adap, addr_list[i])) {
1177 dev_dbg(&adap->dev, "Address 0x%02x already in "
1178 "use, not probing\n", addr_list[i]);
1182 /* Test address responsiveness
1183 The default probe method is a quick write, but it is known
1184 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1185 and could also irreversibly write-protect some EEPROMs, so
1186 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1187 read instead. Also, some bus drivers don't implement
1188 quick write, so we fallback to a byte read it that case
1190 if ((addr_list[i] & ~0x07) == 0x30
1191 || (addr_list[i] & ~0x0f) == 0x50
1192 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1193 union i2c_smbus_data data;
1195 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1197 I2C_SMBUS_BYTE, &data) >= 0)
1200 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1202 I2C_SMBUS_QUICK, NULL) >= 0)
1207 if (addr_list[i] == I2C_CLIENT_END) {
1208 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1212 info->addr = addr_list[i];
1213 return i2c_new_device(adap, info);
1215 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1217 struct i2c_adapter* i2c_get_adapter(int id)
1219 struct i2c_adapter *adapter;
1221 mutex_lock(&core_lock);
1222 adapter = idr_find(&i2c_adapter_idr, id);
1223 if (adapter && !try_module_get(adapter->owner))
1226 mutex_unlock(&core_lock);
1229 EXPORT_SYMBOL(i2c_get_adapter);
1231 void i2c_put_adapter(struct i2c_adapter *adap)
1233 module_put(adap->owner);
1235 EXPORT_SYMBOL(i2c_put_adapter);
1237 /* The SMBus parts */
1239 #define POLY (0x1070U << 3)
1240 static u8 crc8(u16 data)
1244 for(i = 0; i < 8; i++) {
1249 return (u8)(data >> 8);
1252 /* Incremental CRC8 over count bytes in the array pointed to by p */
1253 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1257 for(i = 0; i < count; i++)
1258 crc = crc8((crc ^ p[i]) << 8);
1262 /* Assume a 7-bit address, which is reasonable for SMBus */
1263 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1265 /* The address will be sent first */
1266 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1267 pec = i2c_smbus_pec(pec, &addr, 1);
1269 /* The data buffer follows */
1270 return i2c_smbus_pec(pec, msg->buf, msg->len);
1273 /* Used for write only transactions */
1274 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1276 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1280 /* Return <0 on CRC error
1281 If there was a write before this read (most cases) we need to take the
1282 partial CRC from the write part into account.
1283 Note that this function does modify the message (we need to decrease the
1284 message length to hide the CRC byte from the caller). */
1285 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1287 u8 rpec = msg->buf[--msg->len];
1288 cpec = i2c_smbus_msg_pec(cpec, msg);
1291 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1299 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1300 * @client: Handle to slave device
1302 * This executes the SMBus "receive byte" protocol, returning negative errno
1303 * else the byte received from the device.
1305 s32 i2c_smbus_read_byte(struct i2c_client *client)
1307 union i2c_smbus_data data;
1310 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1312 I2C_SMBUS_BYTE, &data);
1313 return (status < 0) ? status : data.byte;
1315 EXPORT_SYMBOL(i2c_smbus_read_byte);
1318 * i2c_smbus_write_byte - SMBus "send byte" protocol
1319 * @client: Handle to slave device
1320 * @value: Byte to be sent
1322 * This executes the SMBus "send byte" protocol, returning negative errno
1323 * else zero on success.
1325 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1327 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1328 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1330 EXPORT_SYMBOL(i2c_smbus_write_byte);
1333 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1334 * @client: Handle to slave device
1335 * @command: Byte interpreted by slave
1337 * This executes the SMBus "read byte" protocol, returning negative errno
1338 * else a data byte received from the device.
1340 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1342 union i2c_smbus_data data;
1345 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1346 I2C_SMBUS_READ, command,
1347 I2C_SMBUS_BYTE_DATA, &data);
1348 return (status < 0) ? status : data.byte;
1350 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1353 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1354 * @client: Handle to slave device
1355 * @command: Byte interpreted by slave
1356 * @value: Byte being written
1358 * This executes the SMBus "write byte" protocol, returning negative errno
1359 * else zero on success.
1361 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1363 union i2c_smbus_data data;
1365 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1366 I2C_SMBUS_WRITE,command,
1367 I2C_SMBUS_BYTE_DATA,&data);
1369 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1372 * i2c_smbus_read_word_data - SMBus "read word" protocol
1373 * @client: Handle to slave device
1374 * @command: Byte interpreted by slave
1376 * This executes the SMBus "read word" protocol, returning negative errno
1377 * else a 16-bit unsigned "word" received from the device.
1379 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1381 union i2c_smbus_data data;
1384 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1385 I2C_SMBUS_READ, command,
1386 I2C_SMBUS_WORD_DATA, &data);
1387 return (status < 0) ? status : data.word;
1389 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1392 * i2c_smbus_write_word_data - SMBus "write word" protocol
1393 * @client: Handle to slave device
1394 * @command: Byte interpreted by slave
1395 * @value: 16-bit "word" being written
1397 * This executes the SMBus "write word" protocol, returning negative errno
1398 * else zero on success.
1400 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1402 union i2c_smbus_data data;
1404 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1405 I2C_SMBUS_WRITE,command,
1406 I2C_SMBUS_WORD_DATA,&data);
1408 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1411 * i2c_smbus_process_call - SMBus "process call" protocol
1412 * @client: Handle to slave device
1413 * @command: Byte interpreted by slave
1414 * @value: 16-bit "word" being written
1416 * This executes the SMBus "process call" protocol, returning negative errno
1417 * else a 16-bit unsigned "word" received from the device.
1419 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1421 union i2c_smbus_data data;
1425 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1426 I2C_SMBUS_WRITE, command,
1427 I2C_SMBUS_PROC_CALL, &data);
1428 return (status < 0) ? status : data.word;
1430 EXPORT_SYMBOL(i2c_smbus_process_call);
1433 * i2c_smbus_read_block_data - SMBus "block read" protocol
1434 * @client: Handle to slave device
1435 * @command: Byte interpreted by slave
1436 * @values: Byte array into which data will be read; big enough to hold
1437 * the data returned by the slave. SMBus allows at most 32 bytes.
1439 * This executes the SMBus "block read" protocol, returning negative errno
1440 * else the number of data bytes in the slave's response.
1442 * Note that using this function requires that the client's adapter support
1443 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1444 * support this; its emulation through I2C messaging relies on a specific
1445 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1447 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1450 union i2c_smbus_data data;
1453 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1454 I2C_SMBUS_READ, command,
1455 I2C_SMBUS_BLOCK_DATA, &data);
1459 memcpy(values, &data.block[1], data.block[0]);
1460 return data.block[0];
1462 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1465 * i2c_smbus_write_block_data - SMBus "block write" protocol
1466 * @client: Handle to slave device
1467 * @command: Byte interpreted by slave
1468 * @length: Size of data block; SMBus allows at most 32 bytes
1469 * @values: Byte array which will be written.
1471 * This executes the SMBus "block write" protocol, returning negative errno
1472 * else zero on success.
1474 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1475 u8 length, const u8 *values)
1477 union i2c_smbus_data data;
1479 if (length > I2C_SMBUS_BLOCK_MAX)
1480 length = I2C_SMBUS_BLOCK_MAX;
1481 data.block[0] = length;
1482 memcpy(&data.block[1], values, length);
1483 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1484 I2C_SMBUS_WRITE,command,
1485 I2C_SMBUS_BLOCK_DATA,&data);
1487 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1489 /* Returns the number of read bytes */
1490 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1491 u8 length, u8 *values)
1493 union i2c_smbus_data data;
1496 if (length > I2C_SMBUS_BLOCK_MAX)
1497 length = I2C_SMBUS_BLOCK_MAX;
1498 data.block[0] = length;
1499 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1500 I2C_SMBUS_READ, command,
1501 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1505 memcpy(values, &data.block[1], data.block[0]);
1506 return data.block[0];
1508 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1510 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1511 u8 length, const u8 *values)
1513 union i2c_smbus_data data;
1515 if (length > I2C_SMBUS_BLOCK_MAX)
1516 length = I2C_SMBUS_BLOCK_MAX;
1517 data.block[0] = length;
1518 memcpy(data.block + 1, values, length);
1519 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1520 I2C_SMBUS_WRITE, command,
1521 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1523 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1525 /* Simulate a SMBus command using the i2c protocol
1526 No checking of parameters is done! */
1527 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1528 unsigned short flags,
1529 char read_write, u8 command, int size,
1530 union i2c_smbus_data * data)
1532 /* So we need to generate a series of msgs. In the case of writing, we
1533 need to use only one message; when reading, we need two. We initialize
1534 most things with sane defaults, to keep the code below somewhat
1536 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1537 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1538 int num = read_write == I2C_SMBUS_READ?2:1;
1539 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1540 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1546 msgbuf0[0] = command;
1548 case I2C_SMBUS_QUICK:
1550 /* Special case: The read/write field is used as data */
1551 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1555 case I2C_SMBUS_BYTE:
1556 if (read_write == I2C_SMBUS_READ) {
1557 /* Special case: only a read! */
1558 msg[0].flags = I2C_M_RD | flags;
1562 case I2C_SMBUS_BYTE_DATA:
1563 if (read_write == I2C_SMBUS_READ)
1567 msgbuf0[1] = data->byte;
1570 case I2C_SMBUS_WORD_DATA:
1571 if (read_write == I2C_SMBUS_READ)
1575 msgbuf0[1] = data->word & 0xff;
1576 msgbuf0[2] = data->word >> 8;
1579 case I2C_SMBUS_PROC_CALL:
1580 num = 2; /* Special case */
1581 read_write = I2C_SMBUS_READ;
1584 msgbuf0[1] = data->word & 0xff;
1585 msgbuf0[2] = data->word >> 8;
1587 case I2C_SMBUS_BLOCK_DATA:
1588 if (read_write == I2C_SMBUS_READ) {
1589 msg[1].flags |= I2C_M_RECV_LEN;
1590 msg[1].len = 1; /* block length will be added by
1591 the underlying bus driver */
1593 msg[0].len = data->block[0] + 2;
1594 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1595 dev_err(&adapter->dev,
1596 "Invalid block write size %d\n",
1600 for (i = 1; i < msg[0].len; i++)
1601 msgbuf0[i] = data->block[i-1];
1604 case I2C_SMBUS_BLOCK_PROC_CALL:
1605 num = 2; /* Another special case */
1606 read_write = I2C_SMBUS_READ;
1607 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1608 dev_err(&adapter->dev,
1609 "Invalid block write size %d\n",
1613 msg[0].len = data->block[0] + 2;
1614 for (i = 1; i < msg[0].len; i++)
1615 msgbuf0[i] = data->block[i-1];
1616 msg[1].flags |= I2C_M_RECV_LEN;
1617 msg[1].len = 1; /* block length will be added by
1618 the underlying bus driver */
1620 case I2C_SMBUS_I2C_BLOCK_DATA:
1621 if (read_write == I2C_SMBUS_READ) {
1622 msg[1].len = data->block[0];
1624 msg[0].len = data->block[0] + 1;
1625 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1626 dev_err(&adapter->dev,
1627 "Invalid block write size %d\n",
1631 for (i = 1; i <= data->block[0]; i++)
1632 msgbuf0[i] = data->block[i];
1636 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1640 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1641 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1643 /* Compute PEC if first message is a write */
1644 if (!(msg[0].flags & I2C_M_RD)) {
1645 if (num == 1) /* Write only */
1646 i2c_smbus_add_pec(&msg[0]);
1647 else /* Write followed by read */
1648 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1650 /* Ask for PEC if last message is a read */
1651 if (msg[num-1].flags & I2C_M_RD)
1655 status = i2c_transfer(adapter, msg, num);
1659 /* Check PEC if last message is a read */
1660 if (i && (msg[num-1].flags & I2C_M_RD)) {
1661 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1666 if (read_write == I2C_SMBUS_READ)
1668 case I2C_SMBUS_BYTE:
1669 data->byte = msgbuf0[0];
1671 case I2C_SMBUS_BYTE_DATA:
1672 data->byte = msgbuf1[0];
1674 case I2C_SMBUS_WORD_DATA:
1675 case I2C_SMBUS_PROC_CALL:
1676 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1678 case I2C_SMBUS_I2C_BLOCK_DATA:
1679 for (i = 0; i < data->block[0]; i++)
1680 data->block[i+1] = msgbuf1[i];
1682 case I2C_SMBUS_BLOCK_DATA:
1683 case I2C_SMBUS_BLOCK_PROC_CALL:
1684 for (i = 0; i < msgbuf1[0] + 1; i++)
1685 data->block[i] = msgbuf1[i];
1692 * i2c_smbus_xfer - execute SMBus protocol operations
1693 * @adapter: Handle to I2C bus
1694 * @addr: Address of SMBus slave on that bus
1695 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1696 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1697 * @command: Byte interpreted by slave, for protocols which use such bytes
1698 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1699 * @data: Data to be read or written
1701 * This executes an SMBus protocol operation, and returns a negative
1702 * errno code else zero on success.
1704 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1705 char read_write, u8 command, int protocol,
1706 union i2c_smbus_data *data)
1708 unsigned long orig_jiffies;
1712 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1714 if (adapter->algo->smbus_xfer) {
1715 mutex_lock(&adapter->bus_lock);
1717 /* Retry automatically on arbitration loss */
1718 orig_jiffies = jiffies;
1719 for (res = 0, try = 0; try <= adapter->retries; try++) {
1720 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1721 read_write, command,
1725 if (time_after(jiffies,
1726 orig_jiffies + adapter->timeout))
1729 mutex_unlock(&adapter->bus_lock);
1731 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1732 command, protocol, data);
1736 EXPORT_SYMBOL(i2c_smbus_xfer);
1738 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1739 MODULE_DESCRIPTION("I2C-Bus main module");
1740 MODULE_LICENSE("GPL");