drivers/net: Add missing "space"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / i2c-core.c
blob96da22e9a5a44dbe32b247694a2b72bdea20c0d8
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,
204 * i2c_verify_client - return parameter as i2c_client, or NULL
205 * @dev: device, probably from some driver model iterator
207 * When traversing the driver model tree, perhaps using driver model
208 * iterators like @device_for_each_child(), you can't assume very much
209 * about the nodes you find. Use this function to avoid oopses caused
210 * by wrongly treating some non-I2C device as an i2c_client.
212 struct i2c_client *i2c_verify_client(struct device *dev)
214 return (dev->bus == &i2c_bus_type)
215 ? to_i2c_client(dev)
216 : NULL;
218 EXPORT_SYMBOL(i2c_verify_client);
222 * i2c_new_device - instantiate an i2c device for use with a new style driver
223 * @adap: the adapter managing the device
224 * @info: describes one I2C device; bus_num is ignored
225 * Context: can sleep
227 * Create a device to work with a new style i2c driver, where binding is
228 * handled through driver model probe()/remove() methods. This call is not
229 * appropriate for use by mainboad initialization logic, which usually runs
230 * during an arch_initcall() long before any i2c_adapter could exist.
232 * This returns the new i2c client, which may be saved for later use with
233 * i2c_unregister_device(); or NULL to indicate an error.
235 struct i2c_client *
236 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
238 struct i2c_client *client;
239 int status;
241 client = kzalloc(sizeof *client, GFP_KERNEL);
242 if (!client)
243 return NULL;
245 client->adapter = adap;
247 client->dev.platform_data = info->platform_data;
248 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
250 client->flags = info->flags & ~I2C_CLIENT_WAKE;
251 client->addr = info->addr;
252 client->irq = info->irq;
254 strlcpy(client->driver_name, info->driver_name,
255 sizeof(client->driver_name));
256 strlcpy(client->name, info->type, sizeof(client->name));
258 /* a new style driver may be bound to this device when we
259 * return from this function, or any later moment (e.g. maybe
260 * hotplugging will load the driver module). and the device
261 * refcount model is the standard driver model one.
263 status = i2c_attach_client(client);
264 if (status < 0) {
265 kfree(client);
266 client = NULL;
268 return client;
270 EXPORT_SYMBOL_GPL(i2c_new_device);
274 * i2c_unregister_device - reverse effect of i2c_new_device()
275 * @client: value returned from i2c_new_device()
276 * Context: can sleep
278 void i2c_unregister_device(struct i2c_client *client)
280 struct i2c_adapter *adapter = client->adapter;
281 struct i2c_driver *driver = client->driver;
283 if (driver && !is_newstyle_driver(driver)) {
284 dev_err(&client->dev, "can't unregister devices "
285 "with legacy drivers\n");
286 WARN_ON(1);
287 return;
290 mutex_lock(&adapter->clist_lock);
291 list_del(&client->list);
292 mutex_unlock(&adapter->clist_lock);
294 device_unregister(&client->dev);
296 EXPORT_SYMBOL_GPL(i2c_unregister_device);
299 static int dummy_nop(struct i2c_client *client)
301 return 0;
304 static struct i2c_driver dummy_driver = {
305 .driver.name = "dummy",
306 .probe = dummy_nop,
307 .remove = dummy_nop,
311 * i2c_new_dummy - return a new i2c device bound to a dummy driver
312 * @adapter: the adapter managing the device
313 * @address: seven bit address to be used
314 * @type: optional label used for i2c_client.name
315 * Context: can sleep
317 * This returns an I2C client bound to the "dummy" driver, intended for use
318 * with devices that consume multiple addresses. Examples of such chips
319 * include various EEPROMS (like 24c04 and 24c08 models).
321 * These dummy devices have two main uses. First, most I2C and SMBus calls
322 * except i2c_transfer() need a client handle; the dummy will be that handle.
323 * And second, this prevents the specified address from being bound to a
324 * different driver.
326 * This returns the new i2c client, which should be saved for later use with
327 * i2c_unregister_device(); or NULL to indicate an error.
329 struct i2c_client *
330 i2c_new_dummy(struct i2c_adapter *adapter, u16 address, const char *type)
332 struct i2c_board_info info = {
333 .driver_name = "dummy",
334 .addr = address,
337 if (type)
338 strlcpy(info.type, type, sizeof info.type);
339 return i2c_new_device(adapter, &info);
341 EXPORT_SYMBOL_GPL(i2c_new_dummy);
343 /* ------------------------------------------------------------------------- */
345 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
347 static void i2c_adapter_dev_release(struct device *dev)
349 struct i2c_adapter *adap = to_i2c_adapter(dev);
350 complete(&adap->dev_released);
353 static ssize_t
354 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
356 struct i2c_adapter *adap = to_i2c_adapter(dev);
357 return sprintf(buf, "%s\n", adap->name);
360 static struct device_attribute i2c_adapter_attrs[] = {
361 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
362 { },
365 static struct class i2c_adapter_class = {
366 .owner = THIS_MODULE,
367 .name = "i2c-adapter",
368 .dev_attrs = i2c_adapter_attrs,
371 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
373 struct i2c_devinfo *devinfo;
375 mutex_lock(&__i2c_board_lock);
376 list_for_each_entry(devinfo, &__i2c_board_list, list) {
377 if (devinfo->busnum == adapter->nr
378 && !i2c_new_device(adapter,
379 &devinfo->board_info))
380 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
381 i2c_adapter_id(adapter),
382 devinfo->board_info.addr);
384 mutex_unlock(&__i2c_board_lock);
387 static int i2c_do_add_adapter(struct device_driver *d, void *data)
389 struct i2c_driver *driver = to_i2c_driver(d);
390 struct i2c_adapter *adap = data;
392 if (driver->attach_adapter) {
393 /* We ignore the return code; if it fails, too bad */
394 driver->attach_adapter(adap);
396 return 0;
399 static int i2c_register_adapter(struct i2c_adapter *adap)
401 int res = 0, dummy;
403 mutex_init(&adap->bus_lock);
404 mutex_init(&adap->clist_lock);
405 INIT_LIST_HEAD(&adap->clients);
407 mutex_lock(&core_lock);
409 /* Add the adapter to the driver core.
410 * If the parent pointer is not set up,
411 * we add this adapter to the host bus.
413 if (adap->dev.parent == NULL) {
414 adap->dev.parent = &platform_bus;
415 pr_debug("I2C adapter driver [%s] forgot to specify "
416 "physical device\n", adap->name);
418 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
419 adap->dev.release = &i2c_adapter_dev_release;
420 adap->dev.class = &i2c_adapter_class;
421 res = device_register(&adap->dev);
422 if (res)
423 goto out_list;
425 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
427 /* create pre-declared device nodes for new-style drivers */
428 if (adap->nr < __i2c_first_dynamic_bus_num)
429 i2c_scan_static_board_info(adap);
431 /* let legacy drivers scan this bus for matching devices */
432 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
433 i2c_do_add_adapter);
435 out_unlock:
436 mutex_unlock(&core_lock);
437 return res;
439 out_list:
440 idr_remove(&i2c_adapter_idr, adap->nr);
441 goto out_unlock;
445 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
446 * @adapter: the adapter to add
447 * Context: can sleep
449 * This routine is used to declare an I2C adapter when its bus number
450 * doesn't matter. Examples: for I2C adapters dynamically added by
451 * USB links or PCI plugin cards.
453 * When this returns zero, a new bus number was allocated and stored
454 * in adap->nr, and the specified adapter became available for clients.
455 * Otherwise, a negative errno value is returned.
457 int i2c_add_adapter(struct i2c_adapter *adapter)
459 int id, res = 0;
461 retry:
462 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
463 return -ENOMEM;
465 mutex_lock(&core_lock);
466 /* "above" here means "above or equal to", sigh */
467 res = idr_get_new_above(&i2c_adapter_idr, adapter,
468 __i2c_first_dynamic_bus_num, &id);
469 mutex_unlock(&core_lock);
471 if (res < 0) {
472 if (res == -EAGAIN)
473 goto retry;
474 return res;
477 adapter->nr = id;
478 return i2c_register_adapter(adapter);
480 EXPORT_SYMBOL(i2c_add_adapter);
483 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
484 * @adap: the adapter to register (with adap->nr initialized)
485 * Context: can sleep
487 * This routine is used to declare an I2C adapter when its bus number
488 * matters. Example: for I2C adapters from system-on-chip CPUs, or
489 * otherwise built in to the system's mainboard, and where i2c_board_info
490 * is used to properly configure I2C devices.
492 * If no devices have pre-been declared for this bus, then be sure to
493 * register the adapter before any dynamically allocated ones. Otherwise
494 * the required bus ID may not be available.
496 * When this returns zero, the specified adapter became available for
497 * clients using the bus number provided in adap->nr. Also, the table
498 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
499 * and the appropriate driver model device nodes are created. Otherwise, a
500 * negative errno value is returned.
502 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
504 int id;
505 int status;
507 if (adap->nr & ~MAX_ID_MASK)
508 return -EINVAL;
510 retry:
511 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
512 return -ENOMEM;
514 mutex_lock(&core_lock);
515 /* "above" here means "above or equal to", sigh;
516 * we need the "equal to" result to force the result
518 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
519 if (status == 0 && id != adap->nr) {
520 status = -EBUSY;
521 idr_remove(&i2c_adapter_idr, id);
523 mutex_unlock(&core_lock);
524 if (status == -EAGAIN)
525 goto retry;
527 if (status == 0)
528 status = i2c_register_adapter(adap);
529 return status;
531 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
533 static int i2c_do_del_adapter(struct device_driver *d, void *data)
535 struct i2c_driver *driver = to_i2c_driver(d);
536 struct i2c_adapter *adapter = data;
537 int res;
539 if (!driver->detach_adapter)
540 return 0;
541 res = driver->detach_adapter(adapter);
542 if (res)
543 dev_err(&adapter->dev, "detach_adapter failed (%d) "
544 "for driver [%s]\n", res, driver->driver.name);
545 return res;
549 * i2c_del_adapter - unregister I2C adapter
550 * @adap: the adapter being unregistered
551 * Context: can sleep
553 * This unregisters an I2C adapter which was previously registered
554 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
556 int i2c_del_adapter(struct i2c_adapter *adap)
558 struct list_head *item, *_n;
559 struct i2c_client *client;
560 int res = 0;
562 mutex_lock(&core_lock);
564 /* First make sure that this adapter was ever added */
565 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
566 pr_debug("i2c-core: attempting to delete unregistered "
567 "adapter [%s]\n", adap->name);
568 res = -EINVAL;
569 goto out_unlock;
572 /* Tell drivers about this removal */
573 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
574 i2c_do_del_adapter);
575 if (res)
576 goto out_unlock;
578 /* detach any active clients. This must be done first, because
579 * it can fail; in which case we give up. */
580 list_for_each_safe(item, _n, &adap->clients) {
581 struct i2c_driver *driver;
583 client = list_entry(item, struct i2c_client, list);
584 driver = client->driver;
586 /* new style, follow standard driver model */
587 if (!driver || is_newstyle_driver(driver)) {
588 i2c_unregister_device(client);
589 continue;
592 /* legacy drivers create and remove clients themselves */
593 if ((res = driver->detach_client(client))) {
594 dev_err(&adap->dev, "detach_client failed for client "
595 "[%s] at address 0x%02x\n", client->name,
596 client->addr);
597 goto out_unlock;
601 /* clean up the sysfs representation */
602 init_completion(&adap->dev_released);
603 device_unregister(&adap->dev);
605 /* wait for sysfs to drop all references */
606 wait_for_completion(&adap->dev_released);
608 /* free bus id */
609 idr_remove(&i2c_adapter_idr, adap->nr);
611 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
613 out_unlock:
614 mutex_unlock(&core_lock);
615 return res;
617 EXPORT_SYMBOL(i2c_del_adapter);
620 /* ------------------------------------------------------------------------- */
623 * An i2c_driver is used with one or more i2c_client (device) nodes to access
624 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
625 * are two models for binding the driver to its device: "new style" drivers
626 * follow the standard Linux driver model and just respond to probe() calls
627 * issued if the driver core sees they match(); "legacy" drivers create device
628 * nodes themselves.
631 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
633 int res;
635 /* new style driver methods can't mix with legacy ones */
636 if (is_newstyle_driver(driver)) {
637 if (driver->attach_adapter || driver->detach_adapter
638 || driver->detach_client) {
639 printk(KERN_WARNING
640 "i2c-core: driver [%s] is confused\n",
641 driver->driver.name);
642 return -EINVAL;
646 /* add the driver to the list of i2c drivers in the driver core */
647 driver->driver.owner = owner;
648 driver->driver.bus = &i2c_bus_type;
650 /* for new style drivers, when registration returns the driver core
651 * will have called probe() for all matching-but-unbound devices.
653 res = driver_register(&driver->driver);
654 if (res)
655 return res;
657 mutex_lock(&core_lock);
659 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
661 /* legacy drivers scan i2c busses directly */
662 if (driver->attach_adapter) {
663 struct i2c_adapter *adapter;
665 down(&i2c_adapter_class.sem);
666 list_for_each_entry(adapter, &i2c_adapter_class.devices,
667 dev.node) {
668 driver->attach_adapter(adapter);
670 up(&i2c_adapter_class.sem);
673 mutex_unlock(&core_lock);
674 return 0;
676 EXPORT_SYMBOL(i2c_register_driver);
679 * i2c_del_driver - unregister I2C driver
680 * @driver: the driver being unregistered
681 * Context: can sleep
683 void i2c_del_driver(struct i2c_driver *driver)
685 struct list_head *item2, *_n;
686 struct i2c_client *client;
687 struct i2c_adapter *adap;
689 mutex_lock(&core_lock);
691 /* new-style driver? */
692 if (is_newstyle_driver(driver))
693 goto unregister;
695 /* Have a look at each adapter, if clients of this driver are still
696 * attached. If so, detach them to be able to kill the driver
697 * afterwards.
699 down(&i2c_adapter_class.sem);
700 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
701 if (driver->detach_adapter) {
702 if (driver->detach_adapter(adap)) {
703 dev_err(&adap->dev, "detach_adapter failed "
704 "for driver [%s]\n",
705 driver->driver.name);
707 } else {
708 list_for_each_safe(item2, _n, &adap->clients) {
709 client = list_entry(item2, struct i2c_client, list);
710 if (client->driver != driver)
711 continue;
712 dev_dbg(&adap->dev, "detaching client [%s] "
713 "at 0x%02x\n", client->name,
714 client->addr);
715 if (driver->detach_client(client)) {
716 dev_err(&adap->dev, "detach_client "
717 "failed for client [%s] at "
718 "0x%02x\n", client->name,
719 client->addr);
724 up(&i2c_adapter_class.sem);
726 unregister:
727 driver_unregister(&driver->driver);
728 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
730 mutex_unlock(&core_lock);
732 EXPORT_SYMBOL(i2c_del_driver);
734 /* ------------------------------------------------------------------------- */
736 static int __i2c_check_addr(struct device *dev, void *addrp)
738 struct i2c_client *client = i2c_verify_client(dev);
739 int addr = *(int *)addrp;
741 if (client && client->addr == addr)
742 return -EBUSY;
743 return 0;
746 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
748 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
751 int i2c_attach_client(struct i2c_client *client)
753 struct i2c_adapter *adapter = client->adapter;
754 int res = 0;
756 client->dev.parent = &client->adapter->dev;
757 client->dev.bus = &i2c_bus_type;
759 if (client->driver)
760 client->dev.driver = &client->driver->driver;
762 if (client->driver && !is_newstyle_driver(client->driver)) {
763 client->dev.release = i2c_client_release;
764 client->dev.uevent_suppress = 1;
765 } else
766 client->dev.release = i2c_client_dev_release;
768 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
769 "%d-%04x", i2c_adapter_id(adapter), client->addr);
770 res = device_register(&client->dev);
771 if (res)
772 goto out_err;
774 mutex_lock(&adapter->clist_lock);
775 list_add_tail(&client->list, &adapter->clients);
776 mutex_unlock(&adapter->clist_lock);
778 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
779 client->name, client->dev.bus_id);
781 if (adapter->client_register) {
782 if (adapter->client_register(client)) {
783 dev_dbg(&adapter->dev, "client_register "
784 "failed for client [%s] at 0x%02x\n",
785 client->name, client->addr);
789 return 0;
791 out_err:
792 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
793 "(%d)\n", client->name, client->addr, res);
794 return res;
796 EXPORT_SYMBOL(i2c_attach_client);
798 int i2c_detach_client(struct i2c_client *client)
800 struct i2c_adapter *adapter = client->adapter;
801 int res = 0;
803 if (adapter->client_unregister) {
804 res = adapter->client_unregister(client);
805 if (res) {
806 dev_err(&client->dev,
807 "client_unregister [%s] failed, "
808 "client not detached\n", client->name);
809 goto out;
813 mutex_lock(&adapter->clist_lock);
814 list_del(&client->list);
815 mutex_unlock(&adapter->clist_lock);
817 init_completion(&client->released);
818 device_unregister(&client->dev);
819 wait_for_completion(&client->released);
821 out:
822 return res;
824 EXPORT_SYMBOL(i2c_detach_client);
827 * i2c_use_client - increments the reference count of the i2c client structure
828 * @client: the client being referenced
830 * Each live reference to a client should be refcounted. The driver model does
831 * that automatically as part of driver binding, so that most drivers don't
832 * need to do this explicitly: they hold a reference until they're unbound
833 * from the device.
835 * A pointer to the client with the incremented reference counter is returned.
837 struct i2c_client *i2c_use_client(struct i2c_client *client)
839 get_device(&client->dev);
840 return client;
842 EXPORT_SYMBOL(i2c_use_client);
845 * i2c_release_client - release a use of the i2c client structure
846 * @client: the client being no longer referenced
848 * Must be called when a user of a client is finished with it.
850 void i2c_release_client(struct i2c_client *client)
852 put_device(&client->dev);
854 EXPORT_SYMBOL(i2c_release_client);
856 struct i2c_cmd_arg {
857 unsigned cmd;
858 void *arg;
861 static int i2c_cmd(struct device *dev, void *_arg)
863 struct i2c_client *client = i2c_verify_client(dev);
864 struct i2c_cmd_arg *arg = _arg;
866 if (client && client->driver && client->driver->command)
867 client->driver->command(client, arg->cmd, arg->arg);
868 return 0;
871 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
873 struct i2c_cmd_arg cmd_arg;
875 cmd_arg.cmd = cmd;
876 cmd_arg.arg = arg;
877 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
879 EXPORT_SYMBOL(i2c_clients_command);
881 static int __init i2c_init(void)
883 int retval;
885 retval = bus_register(&i2c_bus_type);
886 if (retval)
887 return retval;
888 retval = class_register(&i2c_adapter_class);
889 if (retval)
890 goto bus_err;
891 retval = i2c_add_driver(&dummy_driver);
892 if (retval)
893 goto class_err;
894 return 0;
896 class_err:
897 class_unregister(&i2c_adapter_class);
898 bus_err:
899 bus_unregister(&i2c_bus_type);
900 return retval;
903 static void __exit i2c_exit(void)
905 i2c_del_driver(&dummy_driver);
906 class_unregister(&i2c_adapter_class);
907 bus_unregister(&i2c_bus_type);
910 subsys_initcall(i2c_init);
911 module_exit(i2c_exit);
913 /* ----------------------------------------------------
914 * the functional interface to the i2c busses.
915 * ----------------------------------------------------
918 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
920 int ret;
922 if (adap->algo->master_xfer) {
923 #ifdef DEBUG
924 for (ret = 0; ret < num; ret++) {
925 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
926 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
927 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
928 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
930 #endif
932 if (in_atomic() || irqs_disabled()) {
933 ret = mutex_trylock(&adap->bus_lock);
934 if (!ret)
935 /* I2C activity is ongoing. */
936 return -EAGAIN;
937 } else {
938 mutex_lock_nested(&adap->bus_lock, adap->level);
941 ret = adap->algo->master_xfer(adap,msgs,num);
942 mutex_unlock(&adap->bus_lock);
944 return ret;
945 } else {
946 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
947 return -ENOSYS;
950 EXPORT_SYMBOL(i2c_transfer);
952 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
954 int ret;
955 struct i2c_adapter *adap=client->adapter;
956 struct i2c_msg msg;
958 msg.addr = client->addr;
959 msg.flags = client->flags & I2C_M_TEN;
960 msg.len = count;
961 msg.buf = (char *)buf;
963 ret = i2c_transfer(adap, &msg, 1);
965 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
966 transmitted, else error code. */
967 return (ret == 1) ? count : ret;
969 EXPORT_SYMBOL(i2c_master_send);
971 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
973 struct i2c_adapter *adap=client->adapter;
974 struct i2c_msg msg;
975 int ret;
977 msg.addr = client->addr;
978 msg.flags = client->flags & I2C_M_TEN;
979 msg.flags |= I2C_M_RD;
980 msg.len = count;
981 msg.buf = buf;
983 ret = i2c_transfer(adap, &msg, 1);
985 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
986 transmitted, else error code. */
987 return (ret == 1) ? count : ret;
989 EXPORT_SYMBOL(i2c_master_recv);
991 /* ----------------------------------------------------
992 * the i2c address scanning function
993 * Will not work for 10-bit addresses!
994 * ----------------------------------------------------
996 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
997 int (*found_proc) (struct i2c_adapter *, int, int))
999 int err;
1001 /* Make sure the address is valid */
1002 if (addr < 0x03 || addr > 0x77) {
1003 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1004 addr);
1005 return -EINVAL;
1008 /* Skip if already in use */
1009 if (i2c_check_addr(adapter, addr))
1010 return 0;
1012 /* Make sure there is something at this address, unless forced */
1013 if (kind < 0) {
1014 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1015 I2C_SMBUS_QUICK, NULL) < 0)
1016 return 0;
1018 /* prevent 24RF08 corruption */
1019 if ((addr & ~0x0f) == 0x50)
1020 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1021 I2C_SMBUS_QUICK, NULL);
1024 /* Finally call the custom detection function */
1025 err = found_proc(adapter, addr, kind);
1026 /* -ENODEV can be returned if there is a chip at the given address
1027 but it isn't supported by this chip driver. We catch it here as
1028 this isn't an error. */
1029 if (err == -ENODEV)
1030 err = 0;
1032 if (err)
1033 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1034 addr, err);
1035 return err;
1038 int i2c_probe(struct i2c_adapter *adapter,
1039 const struct i2c_client_address_data *address_data,
1040 int (*found_proc) (struct i2c_adapter *, int, int))
1042 int i, err;
1043 int adap_id = i2c_adapter_id(adapter);
1045 /* Force entries are done first, and are not affected by ignore
1046 entries */
1047 if (address_data->forces) {
1048 const unsigned short * const *forces = address_data->forces;
1049 int kind;
1051 for (kind = 0; forces[kind]; kind++) {
1052 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1053 i += 2) {
1054 if (forces[kind][i] == adap_id
1055 || forces[kind][i] == ANY_I2C_BUS) {
1056 dev_dbg(&adapter->dev, "found force "
1057 "parameter for adapter %d, "
1058 "addr 0x%02x, kind %d\n",
1059 adap_id, forces[kind][i + 1],
1060 kind);
1061 err = i2c_probe_address(adapter,
1062 forces[kind][i + 1],
1063 kind, found_proc);
1064 if (err)
1065 return err;
1071 /* Stop here if we can't use SMBUS_QUICK */
1072 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1073 if (address_data->probe[0] == I2C_CLIENT_END
1074 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1075 return 0;
1077 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1078 "can't probe for chips\n");
1079 return -1;
1082 /* Probe entries are done second, and are not affected by ignore
1083 entries either */
1084 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1085 if (address_data->probe[i] == adap_id
1086 || address_data->probe[i] == ANY_I2C_BUS) {
1087 dev_dbg(&adapter->dev, "found probe parameter for "
1088 "adapter %d, addr 0x%02x\n", adap_id,
1089 address_data->probe[i + 1]);
1090 err = i2c_probe_address(adapter,
1091 address_data->probe[i + 1],
1092 -1, found_proc);
1093 if (err)
1094 return err;
1098 /* Normal entries are done last, unless shadowed by an ignore entry */
1099 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1100 int j, ignore;
1102 ignore = 0;
1103 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1104 j += 2) {
1105 if ((address_data->ignore[j] == adap_id ||
1106 address_data->ignore[j] == ANY_I2C_BUS)
1107 && address_data->ignore[j + 1]
1108 == address_data->normal_i2c[i]) {
1109 dev_dbg(&adapter->dev, "found ignore "
1110 "parameter for adapter %d, "
1111 "addr 0x%02x\n", adap_id,
1112 address_data->ignore[j + 1]);
1113 ignore = 1;
1114 break;
1117 if (ignore)
1118 continue;
1120 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1121 "addr 0x%02x\n", adap_id,
1122 address_data->normal_i2c[i]);
1123 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1124 -1, found_proc);
1125 if (err)
1126 return err;
1129 return 0;
1131 EXPORT_SYMBOL(i2c_probe);
1133 struct i2c_client *
1134 i2c_new_probed_device(struct i2c_adapter *adap,
1135 struct i2c_board_info *info,
1136 unsigned short const *addr_list)
1138 int i;
1140 /* Stop here if the bus doesn't support probing */
1141 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1142 dev_err(&adap->dev, "Probing not supported\n");
1143 return NULL;
1146 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1147 /* Check address validity */
1148 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1149 dev_warn(&adap->dev, "Invalid 7-bit address "
1150 "0x%02x\n", addr_list[i]);
1151 continue;
1154 /* Check address availability */
1155 if (i2c_check_addr(adap, addr_list[i])) {
1156 dev_dbg(&adap->dev, "Address 0x%02x already in "
1157 "use, not probing\n", addr_list[i]);
1158 continue;
1161 /* Test address responsiveness
1162 The default probe method is a quick write, but it is known
1163 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1164 and could also irreversibly write-protect some EEPROMs, so
1165 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1166 read instead. Also, some bus drivers don't implement
1167 quick write, so we fallback to a byte read it that case
1168 too. */
1169 if ((addr_list[i] & ~0x07) == 0x30
1170 || (addr_list[i] & ~0x0f) == 0x50
1171 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1172 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1173 I2C_SMBUS_READ, 0,
1174 I2C_SMBUS_BYTE, NULL) >= 0)
1175 break;
1176 } else {
1177 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1178 I2C_SMBUS_WRITE, 0,
1179 I2C_SMBUS_QUICK, NULL) >= 0)
1180 break;
1184 if (addr_list[i] == I2C_CLIENT_END) {
1185 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1186 return NULL;
1189 info->addr = addr_list[i];
1190 return i2c_new_device(adap, info);
1192 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1194 struct i2c_adapter* i2c_get_adapter(int id)
1196 struct i2c_adapter *adapter;
1198 mutex_lock(&core_lock);
1199 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1200 if (adapter && !try_module_get(adapter->owner))
1201 adapter = NULL;
1203 mutex_unlock(&core_lock);
1204 return adapter;
1206 EXPORT_SYMBOL(i2c_get_adapter);
1208 void i2c_put_adapter(struct i2c_adapter *adap)
1210 module_put(adap->owner);
1212 EXPORT_SYMBOL(i2c_put_adapter);
1214 /* The SMBus parts */
1216 #define POLY (0x1070U << 3)
1217 static u8
1218 crc8(u16 data)
1220 int i;
1222 for(i = 0; i < 8; i++) {
1223 if (data & 0x8000)
1224 data = data ^ POLY;
1225 data = data << 1;
1227 return (u8)(data >> 8);
1230 /* Incremental CRC8 over count bytes in the array pointed to by p */
1231 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1233 int i;
1235 for(i = 0; i < count; i++)
1236 crc = crc8((crc ^ p[i]) << 8);
1237 return crc;
1240 /* Assume a 7-bit address, which is reasonable for SMBus */
1241 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1243 /* The address will be sent first */
1244 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1245 pec = i2c_smbus_pec(pec, &addr, 1);
1247 /* The data buffer follows */
1248 return i2c_smbus_pec(pec, msg->buf, msg->len);
1251 /* Used for write only transactions */
1252 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1254 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1255 msg->len++;
1258 /* Return <0 on CRC error
1259 If there was a write before this read (most cases) we need to take the
1260 partial CRC from the write part into account.
1261 Note that this function does modify the message (we need to decrease the
1262 message length to hide the CRC byte from the caller). */
1263 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1265 u8 rpec = msg->buf[--msg->len];
1266 cpec = i2c_smbus_msg_pec(cpec, msg);
1268 if (rpec != cpec) {
1269 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1270 rpec, cpec);
1271 return -1;
1273 return 0;
1276 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1278 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1279 value,0,I2C_SMBUS_QUICK,NULL);
1281 EXPORT_SYMBOL(i2c_smbus_write_quick);
1283 s32 i2c_smbus_read_byte(struct i2c_client *client)
1285 union i2c_smbus_data data;
1286 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1287 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1288 return -1;
1289 else
1290 return data.byte;
1292 EXPORT_SYMBOL(i2c_smbus_read_byte);
1294 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1296 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1297 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1299 EXPORT_SYMBOL(i2c_smbus_write_byte);
1301 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1303 union i2c_smbus_data data;
1304 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1305 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1306 return -1;
1307 else
1308 return data.byte;
1310 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1312 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1314 union i2c_smbus_data data;
1315 data.byte = value;
1316 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1317 I2C_SMBUS_WRITE,command,
1318 I2C_SMBUS_BYTE_DATA,&data);
1320 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1322 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1324 union i2c_smbus_data data;
1325 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1326 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1327 return -1;
1328 else
1329 return data.word;
1331 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1333 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1335 union i2c_smbus_data data;
1336 data.word = value;
1337 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1338 I2C_SMBUS_WRITE,command,
1339 I2C_SMBUS_WORD_DATA,&data);
1341 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1344 * i2c_smbus_read_block_data - SMBus block read request
1345 * @client: Handle to slave device
1346 * @command: Command byte issued to let the slave know what data should
1347 * be returned
1348 * @values: Byte array into which data will be read; big enough to hold
1349 * the data returned by the slave. SMBus allows at most 32 bytes.
1351 * Returns the number of bytes read in the slave's response, else a
1352 * negative number to indicate some kind of error.
1354 * Note that using this function requires that the client's adapter support
1355 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1356 * support this; its emulation through I2C messaging relies on a specific
1357 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1359 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1360 u8 *values)
1362 union i2c_smbus_data data;
1364 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1365 I2C_SMBUS_READ, command,
1366 I2C_SMBUS_BLOCK_DATA, &data))
1367 return -1;
1369 memcpy(values, &data.block[1], data.block[0]);
1370 return data.block[0];
1372 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1374 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1375 u8 length, const u8 *values)
1377 union i2c_smbus_data data;
1379 if (length > I2C_SMBUS_BLOCK_MAX)
1380 length = I2C_SMBUS_BLOCK_MAX;
1381 data.block[0] = length;
1382 memcpy(&data.block[1], values, length);
1383 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1384 I2C_SMBUS_WRITE,command,
1385 I2C_SMBUS_BLOCK_DATA,&data);
1387 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1389 /* Returns the number of read bytes */
1390 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1391 u8 length, u8 *values)
1393 union i2c_smbus_data data;
1395 if (length > I2C_SMBUS_BLOCK_MAX)
1396 length = I2C_SMBUS_BLOCK_MAX;
1397 data.block[0] = length;
1398 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1399 I2C_SMBUS_READ,command,
1400 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1401 return -1;
1403 memcpy(values, &data.block[1], data.block[0]);
1404 return data.block[0];
1406 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1408 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1409 u8 length, const u8 *values)
1411 union i2c_smbus_data data;
1413 if (length > I2C_SMBUS_BLOCK_MAX)
1414 length = I2C_SMBUS_BLOCK_MAX;
1415 data.block[0] = length;
1416 memcpy(data.block + 1, values, length);
1417 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1418 I2C_SMBUS_WRITE, command,
1419 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1421 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1423 /* Simulate a SMBus command using the i2c protocol
1424 No checking of parameters is done! */
1425 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1426 unsigned short flags,
1427 char read_write, u8 command, int size,
1428 union i2c_smbus_data * data)
1430 /* So we need to generate a series of msgs. In the case of writing, we
1431 need to use only one message; when reading, we need two. We initialize
1432 most things with sane defaults, to keep the code below somewhat
1433 simpler. */
1434 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1435 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1436 int num = read_write == I2C_SMBUS_READ?2:1;
1437 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1438 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1440 int i;
1441 u8 partial_pec = 0;
1443 msgbuf0[0] = command;
1444 switch(size) {
1445 case I2C_SMBUS_QUICK:
1446 msg[0].len = 0;
1447 /* Special case: The read/write field is used as data */
1448 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1449 num = 1;
1450 break;
1451 case I2C_SMBUS_BYTE:
1452 if (read_write == I2C_SMBUS_READ) {
1453 /* Special case: only a read! */
1454 msg[0].flags = I2C_M_RD | flags;
1455 num = 1;
1457 break;
1458 case I2C_SMBUS_BYTE_DATA:
1459 if (read_write == I2C_SMBUS_READ)
1460 msg[1].len = 1;
1461 else {
1462 msg[0].len = 2;
1463 msgbuf0[1] = data->byte;
1465 break;
1466 case I2C_SMBUS_WORD_DATA:
1467 if (read_write == I2C_SMBUS_READ)
1468 msg[1].len = 2;
1469 else {
1470 msg[0].len=3;
1471 msgbuf0[1] = data->word & 0xff;
1472 msgbuf0[2] = data->word >> 8;
1474 break;
1475 case I2C_SMBUS_PROC_CALL:
1476 num = 2; /* Special case */
1477 read_write = I2C_SMBUS_READ;
1478 msg[0].len = 3;
1479 msg[1].len = 2;
1480 msgbuf0[1] = data->word & 0xff;
1481 msgbuf0[2] = data->word >> 8;
1482 break;
1483 case I2C_SMBUS_BLOCK_DATA:
1484 if (read_write == I2C_SMBUS_READ) {
1485 msg[1].flags |= I2C_M_RECV_LEN;
1486 msg[1].len = 1; /* block length will be added by
1487 the underlying bus driver */
1488 } else {
1489 msg[0].len = data->block[0] + 2;
1490 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1491 dev_err(&adapter->dev, "smbus_access called with "
1492 "invalid block write size (%d)\n",
1493 data->block[0]);
1494 return -1;
1496 for (i = 1; i < msg[0].len; i++)
1497 msgbuf0[i] = data->block[i-1];
1499 break;
1500 case I2C_SMBUS_BLOCK_PROC_CALL:
1501 num = 2; /* Another special case */
1502 read_write = I2C_SMBUS_READ;
1503 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1504 dev_err(&adapter->dev, "%s called with invalid "
1505 "block proc call size (%d)\n", __FUNCTION__,
1506 data->block[0]);
1507 return -1;
1509 msg[0].len = data->block[0] + 2;
1510 for (i = 1; i < msg[0].len; i++)
1511 msgbuf0[i] = data->block[i-1];
1512 msg[1].flags |= I2C_M_RECV_LEN;
1513 msg[1].len = 1; /* block length will be added by
1514 the underlying bus driver */
1515 break;
1516 case I2C_SMBUS_I2C_BLOCK_DATA:
1517 if (read_write == I2C_SMBUS_READ) {
1518 msg[1].len = data->block[0];
1519 } else {
1520 msg[0].len = data->block[0] + 1;
1521 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1522 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1523 "invalid block write size (%d)\n",
1524 data->block[0]);
1525 return -1;
1527 for (i = 1; i <= data->block[0]; i++)
1528 msgbuf0[i] = data->block[i];
1530 break;
1531 default:
1532 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1533 size);
1534 return -1;
1537 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1538 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1539 if (i) {
1540 /* Compute PEC if first message is a write */
1541 if (!(msg[0].flags & I2C_M_RD)) {
1542 if (num == 1) /* Write only */
1543 i2c_smbus_add_pec(&msg[0]);
1544 else /* Write followed by read */
1545 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1547 /* Ask for PEC if last message is a read */
1548 if (msg[num-1].flags & I2C_M_RD)
1549 msg[num-1].len++;
1552 if (i2c_transfer(adapter, msg, num) < 0)
1553 return -1;
1555 /* Check PEC if last message is a read */
1556 if (i && (msg[num-1].flags & I2C_M_RD)) {
1557 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1558 return -1;
1561 if (read_write == I2C_SMBUS_READ)
1562 switch(size) {
1563 case I2C_SMBUS_BYTE:
1564 data->byte = msgbuf0[0];
1565 break;
1566 case I2C_SMBUS_BYTE_DATA:
1567 data->byte = msgbuf1[0];
1568 break;
1569 case I2C_SMBUS_WORD_DATA:
1570 case I2C_SMBUS_PROC_CALL:
1571 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1572 break;
1573 case I2C_SMBUS_I2C_BLOCK_DATA:
1574 for (i = 0; i < data->block[0]; i++)
1575 data->block[i+1] = msgbuf1[i];
1576 break;
1577 case I2C_SMBUS_BLOCK_DATA:
1578 case I2C_SMBUS_BLOCK_PROC_CALL:
1579 for (i = 0; i < msgbuf1[0] + 1; i++)
1580 data->block[i] = msgbuf1[i];
1581 break;
1583 return 0;
1587 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1588 char read_write, u8 command, int size,
1589 union i2c_smbus_data * data)
1591 s32 res;
1593 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1595 if (adapter->algo->smbus_xfer) {
1596 mutex_lock(&adapter->bus_lock);
1597 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1598 command,size,data);
1599 mutex_unlock(&adapter->bus_lock);
1600 } else
1601 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1602 command,size,data);
1604 return res;
1606 EXPORT_SYMBOL(i2c_smbus_xfer);
1608 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1609 MODULE_DESCRIPTION("I2C-Bus main module");
1610 MODULE_LICENSE("GPL");