i2c: Support i2c_transfer in atomic contexts
[linux-2.6/x86.git] / drivers / i2c / i2c-core.c
blobddd1b83f44d45ef4913eae28141daa8daeef60cb
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 <linux/hardirq.h>
37 #include <linux/irqflags.h>
38 #include <asm/uaccess.h>
39 #include <asm/semaphore.h>
41 #include "i2c-core.h"
44 static DEFINE_MUTEX(core_lock);
45 static DEFINE_IDR(i2c_adapter_idr);
47 #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
49 /* ------------------------------------------------------------------------- */
51 static int i2c_device_match(struct device *dev, struct device_driver *drv)
53 struct i2c_client *client = to_i2c_client(dev);
54 struct i2c_driver *driver = to_i2c_driver(drv);
56 /* make legacy i2c drivers bypass driver model probing entirely;
57 * such drivers scan each i2c adapter/bus themselves.
59 if (!is_newstyle_driver(driver))
60 return 0;
62 /* new style drivers use the same kind of driver matching policy
63 * as platform devices or SPI: compare device and driver IDs.
65 return strcmp(client->driver_name, drv->name) == 0;
68 #ifdef CONFIG_HOTPLUG
70 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
71 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
73 struct i2c_client *client = to_i2c_client(dev);
75 /* by definition, legacy drivers can't hotplug */
76 if (dev->driver || !client->driver_name)
77 return 0;
79 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
80 return -ENOMEM;
81 dev_dbg(dev, "uevent\n");
82 return 0;
85 #else
86 #define i2c_device_uevent NULL
87 #endif /* CONFIG_HOTPLUG */
89 static int i2c_device_probe(struct device *dev)
91 struct i2c_client *client = to_i2c_client(dev);
92 struct i2c_driver *driver = to_i2c_driver(dev->driver);
94 if (!driver->probe)
95 return -ENODEV;
96 client->driver = driver;
97 dev_dbg(dev, "probe\n");
98 return driver->probe(client);
101 static int i2c_device_remove(struct device *dev)
103 struct i2c_client *client = to_i2c_client(dev);
104 struct i2c_driver *driver;
105 int status;
107 if (!dev->driver)
108 return 0;
110 driver = to_i2c_driver(dev->driver);
111 if (driver->remove) {
112 dev_dbg(dev, "remove\n");
113 status = driver->remove(client);
114 } else {
115 dev->driver = NULL;
116 status = 0;
118 if (status == 0)
119 client->driver = NULL;
120 return status;
123 static void i2c_device_shutdown(struct device *dev)
125 struct i2c_driver *driver;
127 if (!dev->driver)
128 return;
129 driver = to_i2c_driver(dev->driver);
130 if (driver->shutdown)
131 driver->shutdown(to_i2c_client(dev));
134 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
136 struct i2c_driver *driver;
138 if (!dev->driver)
139 return 0;
140 driver = to_i2c_driver(dev->driver);
141 if (!driver->suspend)
142 return 0;
143 return driver->suspend(to_i2c_client(dev), mesg);
146 static int i2c_device_resume(struct device * dev)
148 struct i2c_driver *driver;
150 if (!dev->driver)
151 return 0;
152 driver = to_i2c_driver(dev->driver);
153 if (!driver->resume)
154 return 0;
155 return driver->resume(to_i2c_client(dev));
158 static void i2c_client_release(struct device *dev)
160 struct i2c_client *client = to_i2c_client(dev);
161 complete(&client->released);
164 static void i2c_client_dev_release(struct device *dev)
166 kfree(to_i2c_client(dev));
169 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
171 struct i2c_client *client = to_i2c_client(dev);
172 return sprintf(buf, "%s\n", client->name);
175 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
177 struct i2c_client *client = to_i2c_client(dev);
178 return client->driver_name
179 ? sprintf(buf, "%s\n", client->driver_name)
180 : 0;
183 static struct device_attribute i2c_dev_attrs[] = {
184 __ATTR(name, S_IRUGO, show_client_name, NULL),
185 /* modalias helps coldplug: modprobe $(cat .../modalias) */
186 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
187 { },
190 static struct bus_type i2c_bus_type = {
191 .name = "i2c",
192 .dev_attrs = i2c_dev_attrs,
193 .match = i2c_device_match,
194 .uevent = i2c_device_uevent,
195 .probe = i2c_device_probe,
196 .remove = i2c_device_remove,
197 .shutdown = i2c_device_shutdown,
198 .suspend = i2c_device_suspend,
199 .resume = i2c_device_resume,
203 * i2c_new_device - instantiate an i2c device for use with a new style driver
204 * @adap: the adapter managing the device
205 * @info: describes one I2C device; bus_num is ignored
206 * Context: can sleep
208 * Create a device to work with a new style i2c driver, where binding is
209 * handled through driver model probe()/remove() methods. This call is not
210 * appropriate for use by mainboad initialization logic, which usually runs
211 * during an arch_initcall() long before any i2c_adapter could exist.
213 * This returns the new i2c client, which may be saved for later use with
214 * i2c_unregister_device(); or NULL to indicate an error.
216 struct i2c_client *
217 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
219 struct i2c_client *client;
220 int status;
222 client = kzalloc(sizeof *client, GFP_KERNEL);
223 if (!client)
224 return NULL;
226 client->adapter = adap;
228 client->dev.platform_data = info->platform_data;
229 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
231 client->flags = info->flags & ~I2C_CLIENT_WAKE;
232 client->addr = info->addr;
233 client->irq = info->irq;
235 strlcpy(client->driver_name, info->driver_name,
236 sizeof(client->driver_name));
237 strlcpy(client->name, info->type, sizeof(client->name));
239 /* a new style driver may be bound to this device when we
240 * return from this function, or any later moment (e.g. maybe
241 * hotplugging will load the driver module). and the device
242 * refcount model is the standard driver model one.
244 status = i2c_attach_client(client);
245 if (status < 0) {
246 kfree(client);
247 client = NULL;
249 return client;
251 EXPORT_SYMBOL_GPL(i2c_new_device);
255 * i2c_unregister_device - reverse effect of i2c_new_device()
256 * @client: value returned from i2c_new_device()
257 * Context: can sleep
259 void i2c_unregister_device(struct i2c_client *client)
261 struct i2c_adapter *adapter = client->adapter;
262 struct i2c_driver *driver = client->driver;
264 if (driver && !is_newstyle_driver(driver)) {
265 dev_err(&client->dev, "can't unregister devices "
266 "with legacy drivers\n");
267 WARN_ON(1);
268 return;
271 mutex_lock(&adapter->clist_lock);
272 list_del(&client->list);
273 mutex_unlock(&adapter->clist_lock);
275 device_unregister(&client->dev);
277 EXPORT_SYMBOL_GPL(i2c_unregister_device);
280 /* ------------------------------------------------------------------------- */
282 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
284 static void i2c_adapter_dev_release(struct device *dev)
286 struct i2c_adapter *adap = to_i2c_adapter(dev);
287 complete(&adap->dev_released);
290 static ssize_t
291 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
293 struct i2c_adapter *adap = to_i2c_adapter(dev);
294 return sprintf(buf, "%s\n", adap->name);
297 static struct device_attribute i2c_adapter_attrs[] = {
298 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
299 { },
302 static struct class i2c_adapter_class = {
303 .owner = THIS_MODULE,
304 .name = "i2c-adapter",
305 .dev_attrs = i2c_adapter_attrs,
308 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
310 struct i2c_devinfo *devinfo;
312 mutex_lock(&__i2c_board_lock);
313 list_for_each_entry(devinfo, &__i2c_board_list, list) {
314 if (devinfo->busnum == adapter->nr
315 && !i2c_new_device(adapter,
316 &devinfo->board_info))
317 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
318 i2c_adapter_id(adapter),
319 devinfo->board_info.addr);
321 mutex_unlock(&__i2c_board_lock);
324 static int i2c_do_add_adapter(struct device_driver *d, void *data)
326 struct i2c_driver *driver = to_i2c_driver(d);
327 struct i2c_adapter *adap = data;
329 if (driver->attach_adapter) {
330 /* We ignore the return code; if it fails, too bad */
331 driver->attach_adapter(adap);
333 return 0;
336 static int i2c_register_adapter(struct i2c_adapter *adap)
338 int res = 0, dummy;
340 mutex_init(&adap->bus_lock);
341 mutex_init(&adap->clist_lock);
342 INIT_LIST_HEAD(&adap->clients);
344 mutex_lock(&core_lock);
346 /* Add the adapter to the driver core.
347 * If the parent pointer is not set up,
348 * we add this adapter to the host bus.
350 if (adap->dev.parent == NULL) {
351 adap->dev.parent = &platform_bus;
352 pr_debug("I2C adapter driver [%s] forgot to specify "
353 "physical device\n", adap->name);
355 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
356 adap->dev.release = &i2c_adapter_dev_release;
357 adap->dev.class = &i2c_adapter_class;
358 res = device_register(&adap->dev);
359 if (res)
360 goto out_list;
362 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
364 /* create pre-declared device nodes for new-style drivers */
365 if (adap->nr < __i2c_first_dynamic_bus_num)
366 i2c_scan_static_board_info(adap);
368 /* let legacy drivers scan this bus for matching devices */
369 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
370 i2c_do_add_adapter);
372 out_unlock:
373 mutex_unlock(&core_lock);
374 return res;
376 out_list:
377 idr_remove(&i2c_adapter_idr, adap->nr);
378 goto out_unlock;
382 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
383 * @adapter: the adapter to add
384 * Context: can sleep
386 * This routine is used to declare an I2C adapter when its bus number
387 * doesn't matter. Examples: for I2C adapters dynamically added by
388 * USB links or PCI plugin cards.
390 * When this returns zero, a new bus number was allocated and stored
391 * in adap->nr, and the specified adapter became available for clients.
392 * Otherwise, a negative errno value is returned.
394 int i2c_add_adapter(struct i2c_adapter *adapter)
396 int id, res = 0;
398 retry:
399 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
400 return -ENOMEM;
402 mutex_lock(&core_lock);
403 /* "above" here means "above or equal to", sigh */
404 res = idr_get_new_above(&i2c_adapter_idr, adapter,
405 __i2c_first_dynamic_bus_num, &id);
406 mutex_unlock(&core_lock);
408 if (res < 0) {
409 if (res == -EAGAIN)
410 goto retry;
411 return res;
414 adapter->nr = id;
415 return i2c_register_adapter(adapter);
417 EXPORT_SYMBOL(i2c_add_adapter);
420 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
421 * @adap: the adapter to register (with adap->nr initialized)
422 * Context: can sleep
424 * This routine is used to declare an I2C adapter when its bus number
425 * matters. Example: for I2C adapters from system-on-chip CPUs, or
426 * otherwise built in to the system's mainboard, and where i2c_board_info
427 * is used to properly configure I2C devices.
429 * If no devices have pre-been declared for this bus, then be sure to
430 * register the adapter before any dynamically allocated ones. Otherwise
431 * the required bus ID may not be available.
433 * When this returns zero, the specified adapter became available for
434 * clients using the bus number provided in adap->nr. Also, the table
435 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
436 * and the appropriate driver model device nodes are created. Otherwise, a
437 * negative errno value is returned.
439 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
441 int id;
442 int status;
444 if (adap->nr & ~MAX_ID_MASK)
445 return -EINVAL;
447 retry:
448 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
449 return -ENOMEM;
451 mutex_lock(&core_lock);
452 /* "above" here means "above or equal to", sigh;
453 * we need the "equal to" result to force the result
455 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
456 if (status == 0 && id != adap->nr) {
457 status = -EBUSY;
458 idr_remove(&i2c_adapter_idr, id);
460 mutex_unlock(&core_lock);
461 if (status == -EAGAIN)
462 goto retry;
464 if (status == 0)
465 status = i2c_register_adapter(adap);
466 return status;
468 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
470 static int i2c_do_del_adapter(struct device_driver *d, void *data)
472 struct i2c_driver *driver = to_i2c_driver(d);
473 struct i2c_adapter *adapter = data;
474 int res;
476 if (!driver->detach_adapter)
477 return 0;
478 res = driver->detach_adapter(adapter);
479 if (res)
480 dev_err(&adapter->dev, "detach_adapter failed (%d) "
481 "for driver [%s]\n", res, driver->driver.name);
482 return res;
486 * i2c_del_adapter - unregister I2C adapter
487 * @adap: the adapter being unregistered
488 * Context: can sleep
490 * This unregisters an I2C adapter which was previously registered
491 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
493 int i2c_del_adapter(struct i2c_adapter *adap)
495 struct list_head *item, *_n;
496 struct i2c_client *client;
497 int res = 0;
499 mutex_lock(&core_lock);
501 /* First make sure that this adapter was ever added */
502 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
503 pr_debug("i2c-core: attempting to delete unregistered "
504 "adapter [%s]\n", adap->name);
505 res = -EINVAL;
506 goto out_unlock;
509 /* Tell drivers about this removal */
510 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
511 i2c_do_del_adapter);
512 if (res)
513 goto out_unlock;
515 /* detach any active clients. This must be done first, because
516 * it can fail; in which case we give up. */
517 list_for_each_safe(item, _n, &adap->clients) {
518 struct i2c_driver *driver;
520 client = list_entry(item, struct i2c_client, list);
521 driver = client->driver;
523 /* new style, follow standard driver model */
524 if (!driver || is_newstyle_driver(driver)) {
525 i2c_unregister_device(client);
526 continue;
529 /* legacy drivers create and remove clients themselves */
530 if ((res = driver->detach_client(client))) {
531 dev_err(&adap->dev, "detach_client failed for client "
532 "[%s] at address 0x%02x\n", client->name,
533 client->addr);
534 goto out_unlock;
538 /* clean up the sysfs representation */
539 init_completion(&adap->dev_released);
540 device_unregister(&adap->dev);
542 /* wait for sysfs to drop all references */
543 wait_for_completion(&adap->dev_released);
545 /* free bus id */
546 idr_remove(&i2c_adapter_idr, adap->nr);
548 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
550 out_unlock:
551 mutex_unlock(&core_lock);
552 return res;
554 EXPORT_SYMBOL(i2c_del_adapter);
557 /* ------------------------------------------------------------------------- */
560 * An i2c_driver is used with one or more i2c_client (device) nodes to access
561 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
562 * are two models for binding the driver to its device: "new style" drivers
563 * follow the standard Linux driver model and just respond to probe() calls
564 * issued if the driver core sees they match(); "legacy" drivers create device
565 * nodes themselves.
568 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
570 int res;
572 /* new style driver methods can't mix with legacy ones */
573 if (is_newstyle_driver(driver)) {
574 if (driver->attach_adapter || driver->detach_adapter
575 || driver->detach_client) {
576 printk(KERN_WARNING
577 "i2c-core: driver [%s] is confused\n",
578 driver->driver.name);
579 return -EINVAL;
583 /* add the driver to the list of i2c drivers in the driver core */
584 driver->driver.owner = owner;
585 driver->driver.bus = &i2c_bus_type;
587 /* for new style drivers, when registration returns the driver core
588 * will have called probe() for all matching-but-unbound devices.
590 res = driver_register(&driver->driver);
591 if (res)
592 return res;
594 mutex_lock(&core_lock);
596 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
598 /* legacy drivers scan i2c busses directly */
599 if (driver->attach_adapter) {
600 struct i2c_adapter *adapter;
602 down(&i2c_adapter_class.sem);
603 list_for_each_entry(adapter, &i2c_adapter_class.devices,
604 dev.node) {
605 driver->attach_adapter(adapter);
607 up(&i2c_adapter_class.sem);
610 mutex_unlock(&core_lock);
611 return 0;
613 EXPORT_SYMBOL(i2c_register_driver);
616 * i2c_del_driver - unregister I2C driver
617 * @driver: the driver being unregistered
618 * Context: can sleep
620 void i2c_del_driver(struct i2c_driver *driver)
622 struct list_head *item2, *_n;
623 struct i2c_client *client;
624 struct i2c_adapter *adap;
626 mutex_lock(&core_lock);
628 /* new-style driver? */
629 if (is_newstyle_driver(driver))
630 goto unregister;
632 /* Have a look at each adapter, if clients of this driver are still
633 * attached. If so, detach them to be able to kill the driver
634 * afterwards.
636 down(&i2c_adapter_class.sem);
637 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
638 if (driver->detach_adapter) {
639 if (driver->detach_adapter(adap)) {
640 dev_err(&adap->dev, "detach_adapter failed "
641 "for driver [%s]\n",
642 driver->driver.name);
644 } else {
645 list_for_each_safe(item2, _n, &adap->clients) {
646 client = list_entry(item2, struct i2c_client, list);
647 if (client->driver != driver)
648 continue;
649 dev_dbg(&adap->dev, "detaching client [%s] "
650 "at 0x%02x\n", client->name,
651 client->addr);
652 if (driver->detach_client(client)) {
653 dev_err(&adap->dev, "detach_client "
654 "failed for client [%s] at "
655 "0x%02x\n", client->name,
656 client->addr);
661 up(&i2c_adapter_class.sem);
663 unregister:
664 driver_unregister(&driver->driver);
665 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
667 mutex_unlock(&core_lock);
669 EXPORT_SYMBOL(i2c_del_driver);
671 /* ------------------------------------------------------------------------- */
673 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
675 struct list_head *item;
676 struct i2c_client *client;
678 list_for_each(item,&adapter->clients) {
679 client = list_entry(item, struct i2c_client, list);
680 if (client->addr == addr)
681 return -EBUSY;
683 return 0;
686 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
688 int rval;
690 mutex_lock(&adapter->clist_lock);
691 rval = __i2c_check_addr(adapter, addr);
692 mutex_unlock(&adapter->clist_lock);
694 return rval;
697 int i2c_attach_client(struct i2c_client *client)
699 struct i2c_adapter *adapter = client->adapter;
700 int res = 0;
702 mutex_lock(&adapter->clist_lock);
703 if (__i2c_check_addr(client->adapter, client->addr)) {
704 res = -EBUSY;
705 goto out_unlock;
707 list_add_tail(&client->list,&adapter->clients);
709 client->dev.parent = &client->adapter->dev;
710 client->dev.bus = &i2c_bus_type;
712 if (client->driver)
713 client->dev.driver = &client->driver->driver;
715 if (client->driver && !is_newstyle_driver(client->driver)) {
716 client->dev.release = i2c_client_release;
717 client->dev.uevent_suppress = 1;
718 } else
719 client->dev.release = i2c_client_dev_release;
721 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
722 "%d-%04x", i2c_adapter_id(adapter), client->addr);
723 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
724 client->name, client->dev.bus_id);
725 res = device_register(&client->dev);
726 if (res)
727 goto out_list;
728 mutex_unlock(&adapter->clist_lock);
730 if (adapter->client_register) {
731 if (adapter->client_register(client)) {
732 dev_dbg(&adapter->dev, "client_register "
733 "failed for client [%s] at 0x%02x\n",
734 client->name, client->addr);
738 return 0;
740 out_list:
741 list_del(&client->list);
742 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
743 "(%d)\n", client->name, client->addr, res);
744 out_unlock:
745 mutex_unlock(&adapter->clist_lock);
746 return res;
748 EXPORT_SYMBOL(i2c_attach_client);
750 int i2c_detach_client(struct i2c_client *client)
752 struct i2c_adapter *adapter = client->adapter;
753 int res = 0;
755 if (adapter->client_unregister) {
756 res = adapter->client_unregister(client);
757 if (res) {
758 dev_err(&client->dev,
759 "client_unregister [%s] failed, "
760 "client not detached\n", client->name);
761 goto out;
765 mutex_lock(&adapter->clist_lock);
766 list_del(&client->list);
767 init_completion(&client->released);
768 device_unregister(&client->dev);
769 mutex_unlock(&adapter->clist_lock);
770 wait_for_completion(&client->released);
772 out:
773 return res;
775 EXPORT_SYMBOL(i2c_detach_client);
778 * i2c_use_client - increments the reference count of the i2c client structure
779 * @client: the client being referenced
781 * Each live reference to a client should be refcounted. The driver model does
782 * that automatically as part of driver binding, so that most drivers don't
783 * need to do this explicitly: they hold a reference until they're unbound
784 * from the device.
786 * A pointer to the client with the incremented reference counter is returned.
788 struct i2c_client *i2c_use_client(struct i2c_client *client)
790 get_device(&client->dev);
791 return client;
793 EXPORT_SYMBOL(i2c_use_client);
796 * i2c_release_client - release a use of the i2c client structure
797 * @client: the client being no longer referenced
799 * Must be called when a user of a client is finished with it.
801 void i2c_release_client(struct i2c_client *client)
803 put_device(&client->dev);
805 EXPORT_SYMBOL(i2c_release_client);
807 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
809 struct list_head *item;
810 struct i2c_client *client;
812 mutex_lock(&adap->clist_lock);
813 list_for_each(item,&adap->clients) {
814 client = list_entry(item, struct i2c_client, list);
815 if (!try_module_get(client->driver->driver.owner))
816 continue;
817 if (NULL != client->driver->command) {
818 mutex_unlock(&adap->clist_lock);
819 client->driver->command(client,cmd,arg);
820 mutex_lock(&adap->clist_lock);
822 module_put(client->driver->driver.owner);
824 mutex_unlock(&adap->clist_lock);
826 EXPORT_SYMBOL(i2c_clients_command);
828 static int __init i2c_init(void)
830 int retval;
832 retval = bus_register(&i2c_bus_type);
833 if (retval)
834 return retval;
835 return class_register(&i2c_adapter_class);
838 static void __exit i2c_exit(void)
840 class_unregister(&i2c_adapter_class);
841 bus_unregister(&i2c_bus_type);
844 subsys_initcall(i2c_init);
845 module_exit(i2c_exit);
847 /* ----------------------------------------------------
848 * the functional interface to the i2c busses.
849 * ----------------------------------------------------
852 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
854 int ret;
856 if (adap->algo->master_xfer) {
857 #ifdef DEBUG
858 for (ret = 0; ret < num; ret++) {
859 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
860 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
861 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
862 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
864 #endif
866 if (in_atomic() || irqs_disabled()) {
867 ret = mutex_trylock(&adap->bus_lock);
868 if (!ret)
869 /* I2C activity is ongoing. */
870 return -EAGAIN;
871 } else {
872 mutex_lock_nested(&adap->bus_lock, adap->level);
875 ret = adap->algo->master_xfer(adap,msgs,num);
876 mutex_unlock(&adap->bus_lock);
878 return ret;
879 } else {
880 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
881 return -ENOSYS;
884 EXPORT_SYMBOL(i2c_transfer);
886 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
888 int ret;
889 struct i2c_adapter *adap=client->adapter;
890 struct i2c_msg msg;
892 msg.addr = client->addr;
893 msg.flags = client->flags & I2C_M_TEN;
894 msg.len = count;
895 msg.buf = (char *)buf;
897 ret = i2c_transfer(adap, &msg, 1);
899 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
900 transmitted, else error code. */
901 return (ret == 1) ? count : ret;
903 EXPORT_SYMBOL(i2c_master_send);
905 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
907 struct i2c_adapter *adap=client->adapter;
908 struct i2c_msg msg;
909 int ret;
911 msg.addr = client->addr;
912 msg.flags = client->flags & I2C_M_TEN;
913 msg.flags |= I2C_M_RD;
914 msg.len = count;
915 msg.buf = buf;
917 ret = i2c_transfer(adap, &msg, 1);
919 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
920 transmitted, else error code. */
921 return (ret == 1) ? count : ret;
923 EXPORT_SYMBOL(i2c_master_recv);
925 /* ----------------------------------------------------
926 * the i2c address scanning function
927 * Will not work for 10-bit addresses!
928 * ----------------------------------------------------
930 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
931 int (*found_proc) (struct i2c_adapter *, int, int))
933 int err;
935 /* Make sure the address is valid */
936 if (addr < 0x03 || addr > 0x77) {
937 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
938 addr);
939 return -EINVAL;
942 /* Skip if already in use */
943 if (i2c_check_addr(adapter, addr))
944 return 0;
946 /* Make sure there is something at this address, unless forced */
947 if (kind < 0) {
948 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
949 I2C_SMBUS_QUICK, NULL) < 0)
950 return 0;
952 /* prevent 24RF08 corruption */
953 if ((addr & ~0x0f) == 0x50)
954 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
955 I2C_SMBUS_QUICK, NULL);
958 /* Finally call the custom detection function */
959 err = found_proc(adapter, addr, kind);
960 /* -ENODEV can be returned if there is a chip at the given address
961 but it isn't supported by this chip driver. We catch it here as
962 this isn't an error. */
963 if (err == -ENODEV)
964 err = 0;
966 if (err)
967 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
968 addr, err);
969 return err;
972 int i2c_probe(struct i2c_adapter *adapter,
973 const struct i2c_client_address_data *address_data,
974 int (*found_proc) (struct i2c_adapter *, int, int))
976 int i, err;
977 int adap_id = i2c_adapter_id(adapter);
979 /* Force entries are done first, and are not affected by ignore
980 entries */
981 if (address_data->forces) {
982 const unsigned short * const *forces = address_data->forces;
983 int kind;
985 for (kind = 0; forces[kind]; kind++) {
986 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
987 i += 2) {
988 if (forces[kind][i] == adap_id
989 || forces[kind][i] == ANY_I2C_BUS) {
990 dev_dbg(&adapter->dev, "found force "
991 "parameter for adapter %d, "
992 "addr 0x%02x, kind %d\n",
993 adap_id, forces[kind][i + 1],
994 kind);
995 err = i2c_probe_address(adapter,
996 forces[kind][i + 1],
997 kind, found_proc);
998 if (err)
999 return err;
1005 /* Stop here if we can't use SMBUS_QUICK */
1006 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1007 if (address_data->probe[0] == I2C_CLIENT_END
1008 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1009 return 0;
1011 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1012 "can't probe for chips\n");
1013 return -1;
1016 /* Probe entries are done second, and are not affected by ignore
1017 entries either */
1018 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1019 if (address_data->probe[i] == adap_id
1020 || address_data->probe[i] == ANY_I2C_BUS) {
1021 dev_dbg(&adapter->dev, "found probe parameter for "
1022 "adapter %d, addr 0x%02x\n", adap_id,
1023 address_data->probe[i + 1]);
1024 err = i2c_probe_address(adapter,
1025 address_data->probe[i + 1],
1026 -1, found_proc);
1027 if (err)
1028 return err;
1032 /* Normal entries are done last, unless shadowed by an ignore entry */
1033 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1034 int j, ignore;
1036 ignore = 0;
1037 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1038 j += 2) {
1039 if ((address_data->ignore[j] == adap_id ||
1040 address_data->ignore[j] == ANY_I2C_BUS)
1041 && address_data->ignore[j + 1]
1042 == address_data->normal_i2c[i]) {
1043 dev_dbg(&adapter->dev, "found ignore "
1044 "parameter for adapter %d, "
1045 "addr 0x%02x\n", adap_id,
1046 address_data->ignore[j + 1]);
1047 ignore = 1;
1048 break;
1051 if (ignore)
1052 continue;
1054 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1055 "addr 0x%02x\n", adap_id,
1056 address_data->normal_i2c[i]);
1057 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1058 -1, found_proc);
1059 if (err)
1060 return err;
1063 return 0;
1065 EXPORT_SYMBOL(i2c_probe);
1067 struct i2c_client *
1068 i2c_new_probed_device(struct i2c_adapter *adap,
1069 struct i2c_board_info *info,
1070 unsigned short const *addr_list)
1072 int i;
1074 /* Stop here if the bus doesn't support probing */
1075 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1076 dev_err(&adap->dev, "Probing not supported\n");
1077 return NULL;
1080 mutex_lock(&adap->clist_lock);
1081 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1082 /* Check address validity */
1083 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1084 dev_warn(&adap->dev, "Invalid 7-bit address "
1085 "0x%02x\n", addr_list[i]);
1086 continue;
1089 /* Check address availability */
1090 if (__i2c_check_addr(adap, addr_list[i])) {
1091 dev_dbg(&adap->dev, "Address 0x%02x already in "
1092 "use, not probing\n", addr_list[i]);
1093 continue;
1096 /* Test address responsiveness
1097 The default probe method is a quick write, but it is known
1098 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1099 and could also irreversibly write-protect some EEPROMs, so
1100 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1101 read instead. Also, some bus drivers don't implement
1102 quick write, so we fallback to a byte read it that case
1103 too. */
1104 if ((addr_list[i] & ~0x07) == 0x30
1105 || (addr_list[i] & ~0x0f) == 0x50
1106 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1107 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1108 I2C_SMBUS_READ, 0,
1109 I2C_SMBUS_BYTE, NULL) >= 0)
1110 break;
1111 } else {
1112 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1113 I2C_SMBUS_WRITE, 0,
1114 I2C_SMBUS_QUICK, NULL) >= 0)
1115 break;
1118 mutex_unlock(&adap->clist_lock);
1120 if (addr_list[i] == I2C_CLIENT_END) {
1121 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1122 return NULL;
1125 info->addr = addr_list[i];
1126 return i2c_new_device(adap, info);
1128 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1130 struct i2c_adapter* i2c_get_adapter(int id)
1132 struct i2c_adapter *adapter;
1134 mutex_lock(&core_lock);
1135 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1136 if (adapter && !try_module_get(adapter->owner))
1137 adapter = NULL;
1139 mutex_unlock(&core_lock);
1140 return adapter;
1142 EXPORT_SYMBOL(i2c_get_adapter);
1144 void i2c_put_adapter(struct i2c_adapter *adap)
1146 module_put(adap->owner);
1148 EXPORT_SYMBOL(i2c_put_adapter);
1150 /* The SMBus parts */
1152 #define POLY (0x1070U << 3)
1153 static u8
1154 crc8(u16 data)
1156 int i;
1158 for(i = 0; i < 8; i++) {
1159 if (data & 0x8000)
1160 data = data ^ POLY;
1161 data = data << 1;
1163 return (u8)(data >> 8);
1166 /* Incremental CRC8 over count bytes in the array pointed to by p */
1167 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1169 int i;
1171 for(i = 0; i < count; i++)
1172 crc = crc8((crc ^ p[i]) << 8);
1173 return crc;
1176 /* Assume a 7-bit address, which is reasonable for SMBus */
1177 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1179 /* The address will be sent first */
1180 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1181 pec = i2c_smbus_pec(pec, &addr, 1);
1183 /* The data buffer follows */
1184 return i2c_smbus_pec(pec, msg->buf, msg->len);
1187 /* Used for write only transactions */
1188 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1190 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1191 msg->len++;
1194 /* Return <0 on CRC error
1195 If there was a write before this read (most cases) we need to take the
1196 partial CRC from the write part into account.
1197 Note that this function does modify the message (we need to decrease the
1198 message length to hide the CRC byte from the caller). */
1199 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1201 u8 rpec = msg->buf[--msg->len];
1202 cpec = i2c_smbus_msg_pec(cpec, msg);
1204 if (rpec != cpec) {
1205 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1206 rpec, cpec);
1207 return -1;
1209 return 0;
1212 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1214 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1215 value,0,I2C_SMBUS_QUICK,NULL);
1217 EXPORT_SYMBOL(i2c_smbus_write_quick);
1219 s32 i2c_smbus_read_byte(struct i2c_client *client)
1221 union i2c_smbus_data data;
1222 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1223 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1224 return -1;
1225 else
1226 return data.byte;
1228 EXPORT_SYMBOL(i2c_smbus_read_byte);
1230 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1232 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1233 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1235 EXPORT_SYMBOL(i2c_smbus_write_byte);
1237 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1239 union i2c_smbus_data data;
1240 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1241 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1242 return -1;
1243 else
1244 return data.byte;
1246 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1248 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1250 union i2c_smbus_data data;
1251 data.byte = value;
1252 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1253 I2C_SMBUS_WRITE,command,
1254 I2C_SMBUS_BYTE_DATA,&data);
1256 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1258 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1260 union i2c_smbus_data data;
1261 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1262 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1263 return -1;
1264 else
1265 return data.word;
1267 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1269 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1271 union i2c_smbus_data data;
1272 data.word = value;
1273 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1274 I2C_SMBUS_WRITE,command,
1275 I2C_SMBUS_WORD_DATA,&data);
1277 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1280 * i2c_smbus_read_block_data - SMBus block read request
1281 * @client: Handle to slave device
1282 * @command: Command byte issued to let the slave know what data should
1283 * be returned
1284 * @values: Byte array into which data will be read; big enough to hold
1285 * the data returned by the slave. SMBus allows at most 32 bytes.
1287 * Returns the number of bytes read in the slave's response, else a
1288 * negative number to indicate some kind of error.
1290 * Note that using this function requires that the client's adapter support
1291 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1292 * support this; its emulation through I2C messaging relies on a specific
1293 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1295 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1296 u8 *values)
1298 union i2c_smbus_data data;
1300 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1301 I2C_SMBUS_READ, command,
1302 I2C_SMBUS_BLOCK_DATA, &data))
1303 return -1;
1305 memcpy(values, &data.block[1], data.block[0]);
1306 return data.block[0];
1308 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1310 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1311 u8 length, const u8 *values)
1313 union i2c_smbus_data data;
1315 if (length > I2C_SMBUS_BLOCK_MAX)
1316 length = I2C_SMBUS_BLOCK_MAX;
1317 data.block[0] = length;
1318 memcpy(&data.block[1], values, length);
1319 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1320 I2C_SMBUS_WRITE,command,
1321 I2C_SMBUS_BLOCK_DATA,&data);
1323 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1325 /* Returns the number of read bytes */
1326 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1327 u8 length, u8 *values)
1329 union i2c_smbus_data data;
1331 if (length > I2C_SMBUS_BLOCK_MAX)
1332 length = I2C_SMBUS_BLOCK_MAX;
1333 data.block[0] = length;
1334 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1335 I2C_SMBUS_READ,command,
1336 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1337 return -1;
1339 memcpy(values, &data.block[1], data.block[0]);
1340 return data.block[0];
1342 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1344 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1345 u8 length, const u8 *values)
1347 union i2c_smbus_data data;
1349 if (length > I2C_SMBUS_BLOCK_MAX)
1350 length = I2C_SMBUS_BLOCK_MAX;
1351 data.block[0] = length;
1352 memcpy(data.block + 1, values, length);
1353 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1354 I2C_SMBUS_WRITE, command,
1355 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1357 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1359 /* Simulate a SMBus command using the i2c protocol
1360 No checking of parameters is done! */
1361 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1362 unsigned short flags,
1363 char read_write, u8 command, int size,
1364 union i2c_smbus_data * data)
1366 /* So we need to generate a series of msgs. In the case of writing, we
1367 need to use only one message; when reading, we need two. We initialize
1368 most things with sane defaults, to keep the code below somewhat
1369 simpler. */
1370 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1371 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1372 int num = read_write == I2C_SMBUS_READ?2:1;
1373 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1374 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1376 int i;
1377 u8 partial_pec = 0;
1379 msgbuf0[0] = command;
1380 switch(size) {
1381 case I2C_SMBUS_QUICK:
1382 msg[0].len = 0;
1383 /* Special case: The read/write field is used as data */
1384 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1385 num = 1;
1386 break;
1387 case I2C_SMBUS_BYTE:
1388 if (read_write == I2C_SMBUS_READ) {
1389 /* Special case: only a read! */
1390 msg[0].flags = I2C_M_RD | flags;
1391 num = 1;
1393 break;
1394 case I2C_SMBUS_BYTE_DATA:
1395 if (read_write == I2C_SMBUS_READ)
1396 msg[1].len = 1;
1397 else {
1398 msg[0].len = 2;
1399 msgbuf0[1] = data->byte;
1401 break;
1402 case I2C_SMBUS_WORD_DATA:
1403 if (read_write == I2C_SMBUS_READ)
1404 msg[1].len = 2;
1405 else {
1406 msg[0].len=3;
1407 msgbuf0[1] = data->word & 0xff;
1408 msgbuf0[2] = data->word >> 8;
1410 break;
1411 case I2C_SMBUS_PROC_CALL:
1412 num = 2; /* Special case */
1413 read_write = I2C_SMBUS_READ;
1414 msg[0].len = 3;
1415 msg[1].len = 2;
1416 msgbuf0[1] = data->word & 0xff;
1417 msgbuf0[2] = data->word >> 8;
1418 break;
1419 case I2C_SMBUS_BLOCK_DATA:
1420 if (read_write == I2C_SMBUS_READ) {
1421 msg[1].flags |= I2C_M_RECV_LEN;
1422 msg[1].len = 1; /* block length will be added by
1423 the underlying bus driver */
1424 } else {
1425 msg[0].len = data->block[0] + 2;
1426 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1427 dev_err(&adapter->dev, "smbus_access called with "
1428 "invalid block write size (%d)\n",
1429 data->block[0]);
1430 return -1;
1432 for (i = 1; i < msg[0].len; i++)
1433 msgbuf0[i] = data->block[i-1];
1435 break;
1436 case I2C_SMBUS_BLOCK_PROC_CALL:
1437 num = 2; /* Another special case */
1438 read_write = I2C_SMBUS_READ;
1439 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1440 dev_err(&adapter->dev, "%s called with invalid "
1441 "block proc call size (%d)\n", __FUNCTION__,
1442 data->block[0]);
1443 return -1;
1445 msg[0].len = data->block[0] + 2;
1446 for (i = 1; i < msg[0].len; i++)
1447 msgbuf0[i] = data->block[i-1];
1448 msg[1].flags |= I2C_M_RECV_LEN;
1449 msg[1].len = 1; /* block length will be added by
1450 the underlying bus driver */
1451 break;
1452 case I2C_SMBUS_I2C_BLOCK_DATA:
1453 if (read_write == I2C_SMBUS_READ) {
1454 msg[1].len = data->block[0];
1455 } else {
1456 msg[0].len = data->block[0] + 1;
1457 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1458 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1459 "invalid block write size (%d)\n",
1460 data->block[0]);
1461 return -1;
1463 for (i = 1; i <= data->block[0]; i++)
1464 msgbuf0[i] = data->block[i];
1466 break;
1467 default:
1468 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1469 size);
1470 return -1;
1473 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1474 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1475 if (i) {
1476 /* Compute PEC if first message is a write */
1477 if (!(msg[0].flags & I2C_M_RD)) {
1478 if (num == 1) /* Write only */
1479 i2c_smbus_add_pec(&msg[0]);
1480 else /* Write followed by read */
1481 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1483 /* Ask for PEC if last message is a read */
1484 if (msg[num-1].flags & I2C_M_RD)
1485 msg[num-1].len++;
1488 if (i2c_transfer(adapter, msg, num) < 0)
1489 return -1;
1491 /* Check PEC if last message is a read */
1492 if (i && (msg[num-1].flags & I2C_M_RD)) {
1493 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1494 return -1;
1497 if (read_write == I2C_SMBUS_READ)
1498 switch(size) {
1499 case I2C_SMBUS_BYTE:
1500 data->byte = msgbuf0[0];
1501 break;
1502 case I2C_SMBUS_BYTE_DATA:
1503 data->byte = msgbuf1[0];
1504 break;
1505 case I2C_SMBUS_WORD_DATA:
1506 case I2C_SMBUS_PROC_CALL:
1507 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1508 break;
1509 case I2C_SMBUS_I2C_BLOCK_DATA:
1510 for (i = 0; i < data->block[0]; i++)
1511 data->block[i+1] = msgbuf1[i];
1512 break;
1513 case I2C_SMBUS_BLOCK_DATA:
1514 case I2C_SMBUS_BLOCK_PROC_CALL:
1515 for (i = 0; i < msgbuf1[0] + 1; i++)
1516 data->block[i] = msgbuf1[i];
1517 break;
1519 return 0;
1523 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1524 char read_write, u8 command, int size,
1525 union i2c_smbus_data * data)
1527 s32 res;
1529 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1531 if (adapter->algo->smbus_xfer) {
1532 mutex_lock(&adapter->bus_lock);
1533 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1534 command,size,data);
1535 mutex_unlock(&adapter->bus_lock);
1536 } else
1537 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1538 command,size,data);
1540 return res;
1542 EXPORT_SYMBOL(i2c_smbus_xfer);
1544 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1545 MODULE_DESCRIPTION("I2C-Bus main module");
1546 MODULE_LICENSE("GPL");