linux/drivers/i2c/i2c-core.c
<<
>>
Prefs
   1/* i2c-core.c - a device driver for the iic-bus interface                    */
   2/* ------------------------------------------------------------------------- */
   3/*   Copyright (C) 1995-99 Simon G. Vogl
   4
   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.
   9
  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.
  14
  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/* ------------------------------------------------------------------------- */
  19
  20/* With some changes from Ky\xC3\xB6sti M\xC3\xA4lkki <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> */
  24
  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/platform_device.h>
  33#include <linux/mutex.h>
  34#include <linux/completion.h>
  35#include <linux/hardirq.h>
  36#include <linux/irqflags.h>
  37#include <asm/uaccess.h>
  38
  39#include "i2c-core.h"
  40
  41
  42static DEFINE_MUTEX(core_lock);
  43static DEFINE_IDR(i2c_adapter_idr);
  44
  45#define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
  46
  47static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
  48
  49/* ------------------------------------------------------------------------- */
  50
  51static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  52                                                const struct i2c_client *client)
  53{
  54        while (id->name[0]) {
  55                if (strcmp(client->name, id->name) == 0)
  56                        return id;
  57                id++;
  58        }
  59        return NULL;
  60}
  61
  62static int i2c_device_match(struct device *dev, struct device_driver *drv)
  63{
  64        struct i2c_client       *client = to_i2c_client(dev);
  65        struct i2c_driver       *driver = to_i2c_driver(drv);
  66
  67        /* make legacy i2c drivers bypass driver model probing entirely;
  68         * such drivers scan each i2c adapter/bus themselves.
  69         */
  70        if (!is_newstyle_driver(driver))
  71                return 0;
  72
  73        /* match on an id table if there is one */
  74        if (driver->id_table)
  75                return i2c_match_id(driver->id_table, client) != NULL;
  76
  77        return 0;
  78}
  79
  80#ifdef  CONFIG_HOTPLUG
  81
  82/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
  83static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  84{
  85        struct i2c_client       *client = to_i2c_client(dev);
  86
  87        /* by definition, legacy drivers can't hotplug */
  88        if (dev->driver)
  89                return 0;
  90
  91        if (add_uevent_var(env, "MODALIAS=%s%s",
  92                           I2C_MODULE_PREFIX, client->name))
  93                return -ENOMEM;
  94        dev_dbg(dev, "uevent\n");
  95        return 0;
  96}
  97
  98#else
  99#define i2c_device_uevent       NULL
 100#endif  /* CONFIG_HOTPLUG */
 101
 102static int i2c_device_probe(struct device *dev)
 103{
 104        struct i2c_client       *client = to_i2c_client(dev);
 105        struct i2c_driver       *driver = to_i2c_driver(dev->driver);
 106        int status;
 107
 108        if (!driver->probe || !driver->id_table)
 109                return -ENODEV;
 110        client->driver = driver;
 111        if (!device_can_wakeup(&client->dev))
 112                device_init_wakeup(&client->dev,
 113                                        client->flags & I2C_CLIENT_WAKE);
 114        dev_dbg(dev, "probe\n");
 115
 116        status = driver->probe(client, i2c_match_id(driver->id_table, client));
 117        if (status)
 118                client->driver = NULL;
 119        return status;
 120}
 121
 122static int i2c_device_remove(struct device *dev)
 123{
 124        struct i2c_client       *client = to_i2c_client(dev);
 125        struct i2c_driver       *driver;
 126        int                     status;
 127
 128        if (!dev->driver)
 129                return 0;
 130
 131        driver = to_i2c_driver(dev->driver);
 132        if (driver->remove) {
 133                dev_dbg(dev, "remove\n");
 134                status = driver->remove(client);
 135        } else {
 136                dev->driver = NULL;
 137                status = 0;
 138        }
 139        if (status == 0)
 140                client->driver = NULL;
 141        return status;
 142}
 143
 144static void i2c_device_shutdown(struct device *dev)
 145{
 146        struct i2c_driver *driver;
 147
 148        if (!dev->driver)
 149                return;
 150        driver = to_i2c_driver(dev->driver);
 151        if (driver->shutdown)
 152                driver->shutdown(to_i2c_client(dev));
 153}
 154
 155static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
 156{
 157        struct i2c_driver *driver;
 158
 159        if (!dev->driver)
 160                return 0;
 161        driver = to_i2c_driver(dev->driver);
 162        if (!driver->suspend)
 163                return 0;
 164        return driver->suspend(to_i2c_client(dev), mesg);
 165}
 166
 167static int i2c_device_resume(struct device * dev)
 168{
 169        struct i2c_driver *driver;
 170
 171        if (!dev->driver)
 172                return 0;
 173        driver = to_i2c_driver(dev->driver);
 174        if (!driver->resume)
 175                return 0;
 176        return driver->resume(to_i2c_client(dev));
 177}
 178
 179static void i2c_client_release(struct device *dev)
 180{
 181        struct i2c_client *client = to_i2c_client(dev);
 182        complete(&client->released);
 183}
 184
 185static void i2c_client_dev_release(struct device *dev)
 186{
 187        kfree(to_i2c_client(dev));
 188}
 189
 190static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
 191{
 192        struct i2c_client *client = to_i2c_client(dev);
 193        return sprintf(buf, "%s\n", client->name);
 194}
 195
 196static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
 197{
 198        struct i2c_client *client = to_i2c_client(dev);
 199        return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
 200}
 201
 202static struct device_attribute i2c_dev_attrs[] = {
 203        __ATTR(name, S_IRUGO, show_client_name, NULL),
 204        /* modalias helps coldplug:  modprobe $(cat .../modalias) */
 205        __ATTR(modalias, S_IRUGO, show_modalias, NULL),
 206        { },
 207};
 208
 209struct bus_type i2c_bus_type = {
 210        .name           = "i2c",
 211        .dev_attrs      = i2c_dev_attrs,
 212        .match          = i2c_device_match,
 213        .uevent         = i2c_device_uevent,
 214        .probe          = i2c_device_probe,
 215        .remove         = i2c_device_remove,
 216        .shutdown       = i2c_device_shutdown,
 217        .suspend        = i2c_device_suspend,
 218        .resume         = i2c_device_resume,
 219};
 220EXPORT_SYMBOL_GPL(i2c_bus_type);
 221
 222
 223/**
 224 * i2c_verify_client - return parameter as i2c_client, or NULL
 225 * @dev: device, probably from some driver model iterator
 226 *
 227 * When traversing the driver model tree, perhaps using driver model
 228 * iterators like @device_for_each_child(), you can't assume very much
 229 * about the nodes you find.  Use this function to avoid oopses caused
 230 * by wrongly treating some non-I2C device as an i2c_client.
 231 */
 232struct i2c_client *i2c_verify_client(struct device *dev)
 233{
 234        return (dev->bus == &i2c_bus_type)
 235                        ? to_i2c_client(dev)
 236                        : NULL;
 237}
 238EXPORT_SYMBOL(i2c_verify_client);
 239
 240
 241/**
 242 * i2c_new_device - instantiate an i2c device for use with a new style driver
 243 * @adap: the adapter managing the device
 244 * @info: describes one I2C device; bus_num is ignored
 245 * Context: can sleep
 246 *
 247 * Create a device to work with a new style i2c driver, where binding is
 248 * handled through driver model probe()/remove() methods.  This call is not
 249 * appropriate for use by mainboad initialization logic, which usually runs
 250 * during an arch_initcall() long before any i2c_adapter could exist.
 251 *
 252 * This returns the new i2c client, which may be saved for later use with
 253 * i2c_unregister_device(); or NULL to indicate an error.
 254 */
 255struct i2c_client *
 256i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
 257{
 258        struct i2c_client       *client;
 259        int                     status;
 260
 261        client = kzalloc(sizeof *client, GFP_KERNEL);
 262        if (!client)
 263                return NULL;
 264
 265        client->adapter = adap;
 266
 267        client->dev.platform_data = info->platform_data;
 268
 269        if (info->archdata)
 270                client->dev.archdata = *info->archdata;
 271
 272        client->flags = info->flags;
 273        client->addr = info->addr;
 274        client->irq = info->irq;
 275
 276        strlcpy(client->name, info->type, sizeof(client->name));
 277
 278        /* a new style driver may be bound to this device when we
 279         * return from this function, or any later moment (e.g. maybe
 280         * hotplugging will load the driver module).  and the device
 281         * refcount model is the standard driver model one.
 282         */
 283        status = i2c_attach_client(client);
 284        if (status < 0) {
 285                kfree(client);
 286                client = NULL;
 287        }
 288        return client;
 289}
 290EXPORT_SYMBOL_GPL(i2c_new_device);
 291
 292
 293/**
 294 * i2c_unregister_device - reverse effect of i2c_new_device()
 295 * @client: value returned from i2c_new_device()
 296 * Context: can sleep
 297 */
 298void i2c_unregister_device(struct i2c_client *client)
 299{
 300        struct i2c_adapter      *adapter = client->adapter;
 301        struct i2c_driver       *driver = client->driver;
 302
 303        if (driver && !is_newstyle_driver(driver)) {
 304                dev_err(&client->dev, "can't unregister devices "
 305                        "with legacy drivers\n");
 306                WARN_ON(1);
 307                return;
 308        }
 309
 310        if (adapter->client_unregister) {
 311                if (adapter->client_unregister(client)) {
 312                        dev_warn(&client->dev,
 313                                 "client_unregister [%s] failed\n",
 314                                 client->name);
 315                }
 316        }
 317
 318        mutex_lock(&adapter->clist_lock);
 319        list_del(&client->list);
 320        mutex_unlock(&adapter->clist_lock);
 321
 322        device_unregister(&client->dev);
 323}
 324EXPORT_SYMBOL_GPL(i2c_unregister_device);
 325
 326
 327static const struct i2c_device_id dummy_id[] = {
 328        { "dummy", 0 },
 329        { },
 330};
 331
 332static int dummy_probe(struct i2c_client *client,
 333                       const struct i2c_device_id *id)
 334{
 335        return 0;
 336}
 337
 338static int dummy_remove(struct i2c_client *client)
 339{
 340        return 0;
 341}
 342
 343static struct i2c_driver dummy_driver = {
 344        .driver.name    = "dummy",
 345        .probe          = dummy_probe,
 346        .remove         = dummy_remove,
 347        .id_table       = dummy_id,
 348};
 349
 350/**
 351 * i2c_new_dummy - return a new i2c device bound to a dummy driver
 352 * @adapter: the adapter managing the device
 353 * @address: seven bit address to be used
 354 * Context: can sleep
 355 *
 356 * This returns an I2C client bound to the "dummy" driver, intended for use
 357 * with devices that consume multiple addresses.  Examples of such chips
 358 * include various EEPROMS (like 24c04 and 24c08 models).
 359 *
 360 * These dummy devices have two main uses.  First, most I2C and SMBus calls
 361 * except i2c_transfer() need a client handle; the dummy will be that handle.
 362 * And second, this prevents the specified address from being bound to a
 363 * different driver.
 364 *
 365 * This returns the new i2c client, which should be saved for later use with
 366 * i2c_unregister_device(); or NULL to indicate an error.
 367 */
 368struct i2c_client *
 369i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
 370{
 371        struct i2c_board_info info = {
 372                I2C_BOARD_INFO("dummy", address),
 373        };
 374
 375        return i2c_new_device(adapter, &info);
 376}
 377EXPORT_SYMBOL_GPL(i2c_new_dummy);
 378
 379/* ------------------------------------------------------------------------- */
 380
 381/* I2C bus adapters -- one roots each I2C or SMBUS segment */
 382
 383static void i2c_adapter_dev_release(struct device *dev)
 384{
 385        struct i2c_adapter *adap = to_i2c_adapter(dev);
 386        complete(&adap->dev_released);
 387}
 388
 389static ssize_t
 390show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
 391{
 392        struct i2c_adapter *adap = to_i2c_adapter(dev);
 393        return sprintf(buf, "%s\n", adap->name);
 394}
 395
 396static struct device_attribute i2c_adapter_attrs[] = {
 397        __ATTR(name, S_IRUGO, show_adapter_name, NULL),
 398        { },
 399};
 400
 401static struct class i2c_adapter_class = {
 402        .owner                  = THIS_MODULE,
 403        .name                   = "i2c-adapter",
 404        .dev_attrs              = i2c_adapter_attrs,
 405};
 406
 407static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
 408{
 409        struct i2c_devinfo      *devinfo;
 410
 411        mutex_lock(&__i2c_board_lock);
 412        list_for_each_entry(devinfo, &__i2c_board_list, list) {
 413                if (devinfo->busnum == adapter->nr
 414                                && !i2c_new_device(adapter,
 415                                                &devinfo->board_info))
 416                        printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
 417                                i2c_adapter_id(adapter),
 418                                devinfo->board_info.addr);
 419        }
 420        mutex_unlock(&__i2c_board_lock);
 421}
 422
 423static int i2c_do_add_adapter(struct device_driver *d, void *data)
 424{
 425        struct i2c_driver *driver = to_i2c_driver(d);
 426        struct i2c_adapter *adap = data;
 427
 428        /* Detect supported devices on that bus, and instantiate them */
 429        i2c_detect(adap, driver);
 430
 431        /* Let legacy drivers scan this bus for matching devices */
 432        if (driver->attach_adapter) {
 433                /* We ignore the return code; if it fails, too bad */
 434                driver->attach_adapter(adap);
 435        }
 436        return 0;
 437}
 438
 439static int i2c_register_adapter(struct i2c_adapter *adap)
 440{
 441        int res = 0, dummy;
 442
 443        /* Can't register until after driver model init */
 444        if (unlikely(WARN_ON(!i2c_bus_type.p)))
 445                return -EAGAIN;
 446
 447        mutex_init(&adap->bus_lock);
 448        mutex_init(&adap->clist_lock);
 449        INIT_LIST_HEAD(&adap->clients);
 450
 451        mutex_lock(&core_lock);
 452
 453        /* Add the adapter to the driver core.
 454         * If the parent pointer is not set up,
 455         * we add this adapter to the host bus.
 456         */
 457        if (adap->dev.parent == NULL) {
 458                adap->dev.parent = &platform_bus;
 459                pr_debug("I2C adapter driver [%s] forgot to specify "
 460                         "physical device\n", adap->name);
 461        }
 462        sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
 463        adap->dev.release = &i2c_adapter_dev_release;
 464        adap->dev.class = &i2c_adapter_class;
 465        res = device_register(&adap->dev);
 466        if (res)
 467                goto out_list;
 468
 469        dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
 470
 471        /* create pre-declared device nodes for new-style drivers */
 472        if (adap->nr < __i2c_first_dynamic_bus_num)
 473                i2c_scan_static_board_info(adap);
 474
 475        /* Notify drivers */
 476        dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
 477                                 i2c_do_add_adapter);
 478
 479out_unlock:
 480        mutex_unlock(&core_lock);
 481        return res;
 482
 483out_list:
 484        idr_remove(&i2c_adapter_idr, adap->nr);
 485        goto out_unlock;
 486}
 487
 488/**
 489 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
 490 * @adapter: the adapter to add
 491 * Context: can sleep
 492 *
 493 * This routine is used to declare an I2C adapter when its bus number
 494 * doesn't matter.  Examples: for I2C adapters dynamically added by
 495 * USB links or PCI plugin cards.
 496 *
 497 * When this returns zero, a new bus number was allocated and stored
 498 * in adap->nr, and the specified adapter became available for clients.
 499 * Otherwise, a negative errno value is returned.
 500 */
 501int i2c_add_adapter(struct i2c_adapter *adapter)
 502{
 503        int     id, res = 0;
 504
 505retry:
 506        if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
 507                return -ENOMEM;
 508
 509        mutex_lock(&core_lock);
 510        /* "above" here means "above or equal to", sigh */
 511        res = idr_get_new_above(&i2c_adapter_idr, adapter,
 512                                __i2c_first_dynamic_bus_num, &id);
 513        mutex_unlock(&core_lock);
 514
 515        if (res < 0) {
 516                if (res == -EAGAIN)
 517                        goto retry;
 518                return res;
 519        }
 520
 521        adapter->nr = id;
 522        return i2c_register_adapter(adapter);
 523}
 524EXPORT_SYMBOL(i2c_add_adapter);
 525
 526/**
 527 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
 528 * @adap: the adapter to register (with adap->nr initialized)
 529 * Context: can sleep
 530 *
 531 * This routine is used to declare an I2C adapter when its bus number
 532 * matters.  For example, use it for I2C adapters from system-on-chip CPUs,
 533 * or otherwise built in to the system's mainboard, and where i2c_board_info
 534 * is used to properly configure I2C devices.
 535 *
 536 * If no devices have pre-been declared for this bus, then be sure to
 537 * register the adapter before any dynamically allocated ones.  Otherwise
 538 * the required bus ID may not be available.
 539 *
 540 * When this returns zero, the specified adapter became available for
 541 * clients using the bus number provided in adap->nr.  Also, the table
 542 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
 543 * and the appropriate driver model device nodes are created.  Otherwise, a
 544 * negative errno value is returned.
 545 */
 546int i2c_add_numbered_adapter(struct i2c_adapter *adap)
 547{
 548        int     id;
 549        int     status;
 550
 551        if (adap->nr & ~MAX_ID_MASK)
 552                return -EINVAL;
 553
 554retry:
 555        if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
 556                return -ENOMEM;
 557
 558        mutex_lock(&core_lock);
 559        /* "above" here means "above or equal to", sigh;
 560         * we need the "equal to" result to force the result
 561         */
 562        status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
 563        if (status == 0 && id != adap->nr) {
 564                status = -EBUSY;
 565                idr_remove(&i2c_adapter_idr, id);
 566        }
 567        mutex_unlock(&core_lock);
 568        if (status == -EAGAIN)
 569                goto retry;
 570
 571        if (status == 0)
 572                status = i2c_register_adapter(adap);
 573        return status;
 574}
 575EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
 576
 577static int i2c_do_del_adapter(struct device_driver *d, void *data)
 578{
 579        struct i2c_driver *driver = to_i2c_driver(d);
 580        struct i2c_adapter *adapter = data;
 581        struct i2c_client *client, *_n;
 582        int res;
 583
 584        /* Remove the devices we created ourselves */
 585        list_for_each_entry_safe(client, _n, &driver->clients, detected) {
 586                if (client->adapter == adapter) {
 587                        dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
 588                                client->name, client->addr);
 589                        list_del(&client->detected);
 590                        i2c_unregister_device(client);
 591                }
 592        }
 593
 594        if (!driver->detach_adapter)
 595                return 0;
 596        res = driver->detach_adapter(adapter);
 597        if (res)
 598                dev_err(&adapter->dev, "detach_adapter failed (%d) "
 599                        "for driver [%s]\n", res, driver->driver.name);
 600        return res;
 601}
 602
 603/**
 604 * i2c_del_adapter - unregister I2C adapter
 605 * @adap: the adapter being unregistered
 606 * Context: can sleep
 607 *
 608 * This unregisters an I2C adapter which was previously registered
 609 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
 610 */
 611int i2c_del_adapter(struct i2c_adapter *adap)
 612{
 613        struct i2c_client *client, *_n;
 614        int res = 0;
 615
 616        mutex_lock(&core_lock);
 617
 618        /* First make sure that this adapter was ever added */
 619        if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
 620                pr_debug("i2c-core: attempting to delete unregistered "
 621                         "adapter [%s]\n", adap->name);
 622                res = -EINVAL;
 623                goto out_unlock;
 624        }
 625
 626        /* Tell drivers about this removal */
 627        res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
 628                               i2c_do_del_adapter);
 629        if (res)
 630                goto out_unlock;
 631
 632        /* detach any active clients. This must be done first, because
 633         * it can fail; in which case we give up. */
 634        list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
 635                struct i2c_driver       *driver;
 636
 637                driver = client->driver;
 638
 639                /* new style, follow standard driver model */
 640                if (!driver || is_newstyle_driver(driver)) {
 641                        i2c_unregister_device(client);
 642                        continue;
 643                }
 644
 645                /* legacy drivers create and remove clients themselves */
 646                if ((res = driver->detach_client(client))) {
 647                        dev_err(&adap->dev, "detach_client failed for client "
 648                                "[%s] at address 0x%02x\n", client->name,
 649                                client->addr);
 650                        goto out_unlock;
 651                }
 652        }
 653
 654        /* clean up the sysfs representation */
 655        init_completion(&adap->dev_released);
 656        device_unregister(&adap->dev);
 657
 658        /* wait for sysfs to drop all references */
 659        wait_for_completion(&adap->dev_released);
 660
 661        /* free bus id */
 662        idr_remove(&i2c_adapter_idr, adap->nr);
 663
 664        dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
 665
 666        /* Clear the device structure in case this adapter is ever going to be
 667           added again */
 668        memset(&adap->dev, 0, sizeof(adap->dev));
 669
 670 out_unlock:
 671        mutex_unlock(&core_lock);
 672        return res;
 673}
 674EXPORT_SYMBOL(i2c_del_adapter);
 675
 676
 677/* ------------------------------------------------------------------------- */
 678
 679static int __attach_adapter(struct device *dev, void *data)
 680{
 681        struct i2c_adapter *adapter = to_i2c_adapter(dev);
 682        struct i2c_driver *driver = data;
 683
 684        i2c_detect(adapter, driver);
 685
 686        /* Legacy drivers scan i2c busses directly */
 687        if (driver->attach_adapter)
 688                driver->attach_adapter(adapter);
 689
 690        return 0;
 691}
 692
 693/*
 694 * An i2c_driver is used with one or more i2c_client (device) nodes to access
 695 * i2c slave chips, on a bus instance associated with some i2c_adapter.  There
 696 * are two models for binding the driver to its device:  "new style" drivers
 697 * follow the standard Linux driver model and just respond to probe() calls
 698 * issued if the driver core sees they match(); "legacy" drivers create device
 699 * nodes themselves.
 700 */
 701
 702int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
 703{
 704        int res;
 705
 706        /* Can't register until after driver model init */
 707        if (unlikely(WARN_ON(!i2c_bus_type.p)))
 708                return -EAGAIN;
 709
 710        /* new style driver methods can't mix with legacy ones */
 711        if (is_newstyle_driver(driver)) {
 712                if (driver->attach_adapter || driver->detach_adapter
 713                                || driver->detach_client) {
 714                        printk(KERN_WARNING
 715                                        "i2c-core: driver [%s] is confused\n",
 716                                        driver->driver.name);
 717                        return -EINVAL;
 718                }
 719        }
 720
 721        /* add the driver to the list of i2c drivers in the driver core */
 722        driver->driver.owner = owner;
 723        driver->driver.bus = &i2c_bus_type;
 724
 725        /* for new style drivers, when registration returns the driver core
 726         * will have called probe() for all matching-but-unbound devices.
 727         */
 728        res = driver_register(&driver->driver);
 729        if (res)
 730                return res;
 731
 732        mutex_lock(&core_lock);
 733
 734        pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
 735
 736        INIT_LIST_HEAD(&driver->clients);
 737        /* Walk the adapters that are already present */
 738        class_for_each_device(&i2c_adapter_class, NULL, driver,
 739                              __attach_adapter);
 740
 741        mutex_unlock(&core_lock);
 742        return 0;
 743}
 744EXPORT_SYMBOL(i2c_register_driver);
 745
 746static int __detach_adapter(struct device *dev, void *data)
 747{
 748        struct i2c_adapter *adapter = to_i2c_adapter(dev);
 749        struct i2c_driver *driver = data;
 750        struct i2c_client *client, *_n;
 751
 752        list_for_each_entry_safe(client, _n, &driver->clients, detected) {
 753                dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
 754                        client->name, client->addr);
 755                list_del(&client->detected);
 756                i2c_unregister_device(client);
 757        }
 758
 759        if (is_newstyle_driver(driver))
 760                return 0;
 761
 762        /* Have a look at each adapter, if clients of this driver are still
 763         * attached. If so, detach them to be able to kill the driver
 764         * afterwards.
 765         */
 766        if (driver->detach_adapter) {
 767                if (driver->detach_adapter(adapter))
 768                        dev_err(&adapter->dev,
 769                                "detach_adapter failed for driver [%s]\n",
 770                                driver->driver.name);
 771        } else {
 772                struct i2c_client *client, *_n;
 773
 774                list_for_each_entry_safe(client, _n, &adapter->clients, list) {
 775                        if (client->driver != driver)
 776                                continue;
 777                        dev_dbg(&adapter->dev,
 778                                "detaching client [%s] at 0x%02x\n",
 779                                client->name, client->addr);
 780                        if (driver->detach_client(client))
 781                                dev_err(&adapter->dev, "detach_client "
 782                                        "failed for client [%s] at 0x%02x\n",
 783                                        client->name, client->addr);
 784                }
 785        }
 786
 787        return 0;
 788}
 789
 790/**
 791 * i2c_del_driver - unregister I2C driver
 792 * @driver: the driver being unregistered
 793 * Context: can sleep
 794 */
 795void i2c_del_driver(struct i2c_driver *driver)
 796{
 797        mutex_lock(&core_lock);
 798
 799        class_for_each_device(&i2c_adapter_class, NULL, driver,
 800                              __detach_adapter);
 801
 802        driver_unregister(&driver->driver);
 803        pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
 804
 805        mutex_unlock(&core_lock);
 806}
 807EXPORT_SYMBOL(i2c_del_driver);
 808
 809/* ------------------------------------------------------------------------- */
 810
 811static int __i2c_check_addr(struct device *dev, void *addrp)
 812{
 813        struct i2c_client       *client = i2c_verify_client(dev);
 814        int                     addr = *(int *)addrp;
 815
 816        if (client && client->addr == addr)
 817                return -EBUSY;
 818        return 0;
 819}
 820
 821static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
 822{
 823        return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
 824}
 825
 826int i2c_attach_client(struct i2c_client *client)
 827{
 828        struct i2c_adapter *adapter = client->adapter;
 829        int res;
 830
 831        /* Check for address business */
 832        res = i2c_check_addr(adapter, client->addr);
 833        if (res)
 834                return res;
 835
 836        client->dev.parent = &client->adapter->dev;
 837        client->dev.bus = &i2c_bus_type;
 838
 839        if (client->driver)
 840                client->dev.driver = &client->driver->driver;
 841
 842        if (client->driver && !is_newstyle_driver(client->driver)) {
 843                client->dev.release = i2c_client_release;
 844                client->dev.uevent_suppress = 1;
 845        } else
 846                client->dev.release = i2c_client_dev_release;
 847
 848        snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
 849                "%d-%04x", i2c_adapter_id(adapter), client->addr);
 850        res = device_register(&client->dev);
 851        if (res)
 852                goto out_err;
 853
 854        mutex_lock(&adapter->clist_lock);
 855        list_add_tail(&client->list, &adapter->clients);
 856        mutex_unlock(&adapter->clist_lock);
 857
 858        dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
 859                client->name, client->dev.bus_id);
 860
 861        if (adapter->client_register)  {
 862                if (adapter->client_register(client)) {
 863                        dev_dbg(&adapter->dev, "client_register "
 864                                "failed for client [%s] at 0x%02x\n",
 865                                client->name, client->addr);
 866                }
 867        }
 868
 869        return 0;
 870
 871out_err:
 872        dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
 873                "(%d)\n", client->name, client->addr, res);
 874        return res;
 875}
 876EXPORT_SYMBOL(i2c_attach_client);
 877
 878int i2c_detach_client(struct i2c_client *client)
 879{
 880        struct i2c_adapter *adapter = client->adapter;
 881        int res = 0;
 882
 883        if (adapter->client_unregister)  {
 884                res = adapter->client_unregister(client);
 885                if (res) {
 886                        dev_err(&client->dev,
 887                                "client_unregister [%s] failed, "
 888                                "client not detached\n", client->name);
 889                        goto out;
 890                }
 891        }
 892
 893        mutex_lock(&adapter->clist_lock);
 894        list_del(&client->list);
 895        mutex_unlock(&adapter->clist_lock);
 896
 897        init_completion(&client->released);
 898        device_unregister(&client->dev);
 899        wait_for_completion(&client->released);
 900
 901 out:
 902        return res;
 903}
 904EXPORT_SYMBOL(i2c_detach_client);
 905
 906/**
 907 * i2c_use_client - increments the reference count of the i2c client structure
 908 * @client: the client being referenced
 909 *
 910 * Each live reference to a client should be refcounted. The driver model does
 911 * that automatically as part of driver binding, so that most drivers don't
 912 * need to do this explicitly: they hold a reference until they're unbound
 913 * from the device.
 914 *
 915 * A pointer to the client with the incremented reference counter is returned.
 916 */
 917struct i2c_client *i2c_use_client(struct i2c_client *client)
 918{
 919        if (client && get_device(&client->dev))
 920                return client;
 921        return NULL;
 922}
 923EXPORT_SYMBOL(i2c_use_client);
 924
 925/**
 926 * i2c_release_client - release a use of the i2c client structure
 927 * @client: the client being no longer referenced
 928 *
 929 * Must be called when a user of a client is finished with it.
 930 */
 931void i2c_release_client(struct i2c_client *client)
 932{
 933        if (client)
 934                put_device(&client->dev);
 935}
 936EXPORT_SYMBOL(i2c_release_client);
 937
 938struct i2c_cmd_arg {
 939        unsigned        cmd;
 940        void            *arg;
 941};
 942
 943static int i2c_cmd(struct device *dev, void *_arg)
 944{
 945        struct i2c_client       *client = i2c_verify_client(dev);
 946        struct i2c_cmd_arg      *arg = _arg;
 947
 948        if (client && client->driver && client->driver->command)
 949                client->driver->command(client, arg->cmd, arg->arg);
 950        return 0;
 951}
 952
 953void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
 954{
 955        struct i2c_cmd_arg      cmd_arg;
 956
 957        cmd_arg.cmd = cmd;
 958        cmd_arg.arg = arg;
 959        device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
 960}
 961EXPORT_SYMBOL(i2c_clients_command);
 962
 963static int __init i2c_init(void)
 964{
 965        int retval;
 966
 967        retval = bus_register(&i2c_bus_type);
 968        if (retval)
 969                return retval;
 970        retval = class_register(&i2c_adapter_class);
 971        if (retval)
 972                goto bus_err;
 973        retval = i2c_add_driver(&dummy_driver);
 974        if (retval)
 975                goto class_err;
 976        return 0;
 977
 978class_err:
 979        class_unregister(&i2c_adapter_class);
 980bus_err:
 981        bus_unregister(&i2c_bus_type);
 982        return retval;
 983}
 984
 985static void __exit i2c_exit(void)
 986{
 987        i2c_del_driver(&dummy_driver);
 988        class_unregister(&i2c_adapter_class);
 989        bus_unregister(&i2c_bus_type);
 990}
 991
 992/* We must initialize early, because some subsystems register i2c drivers
 993 * in subsys_initcall() code, but are linked (and initialized) before i2c.
 994 */
 995postcore_initcall(i2c_init);
 996module_exit(i2c_exit);
 997
 998/* ----------------------------------------------------
 999 * the functional interface to the i2c busses.
1000 * ----------------------------------------------------
1001 */
1002
1003/**
1004 * i2c_transfer - execute a single or combined I2C message
1005 * @adap: Handle to I2C bus
1006 * @msgs: One or more messages to execute before STOP is issued to
1007 *      terminate the operation; each message begins with a START.
1008 * @num: Number of messages to be executed.
1009 *
1010 * Returns negative errno, else the number of messages executed.
1011 *
1012 * Note that there is no requirement that each message be sent to
1013 * the same slave address, although that is the most common model.
1014 */
1015int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
1016{
1017        int ret;
1018
1019        /* REVISIT the fault reporting model here is weak:
1020         *
1021         *  - When we get an error after receiving N bytes from a slave,
1022         *    there is no way to report "N".
1023         *
1024         *  - When we get a NAK after transmitting N bytes to a slave,
1025         *    there is no way to report "N" ... or to let the master
1026         *    continue executing the rest of this combined message, if
1027         *    that's the appropriate response.
1028         *
1029         *  - When for example "num" is two and we successfully complete
1030         *    the first message but get an error part way through the
1031         *    second, it's unclear whether that should be reported as
1032         *    one (discarding status on the second message) or errno
1033         *    (discarding status on the first one).
1034         */
1035
1036        if (adap->algo->master_xfer) {
1037#ifdef DEBUG
1038                for (ret = 0; ret < num; ret++) {
1039                        dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1040                                "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1041                                ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1042                                (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1043                }
1044#endif
1045
1046                if (in_atomic() || irqs_disabled()) {
1047                        ret = mutex_trylock(&adap->bus_lock);
1048                        if (!ret)
1049                                /* I2C activity is ongoing. */
1050                                return -EAGAIN;
1051                } else {
1052                        mutex_lock_nested(&adap->bus_lock, adap->level);
1053                }
1054
1055                ret = adap->algo->master_xfer(adap,msgs,num);
1056                mutex_unlock(&adap->bus_lock);
1057
1058                return ret;
1059        } else {
1060                dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1061                return -EOPNOTSUPP;
1062        }
1063}
1064EXPORT_SYMBOL(i2c_transfer);
1065
1066/**
1067 * i2c_master_send - issue a single I2C message in master transmit mode
1068 * @client: Handle to slave device
1069 * @buf: Data that will be written to the slave
1070 * @count: How many bytes to write
1071 *
1072 * Returns negative errno, or else the number of bytes written.
1073 */
1074int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1075{
1076        int ret;
1077        struct i2c_adapter *adap=client->adapter;
1078        struct i2c_msg msg;
1079
1080        msg.addr = client->addr;
1081        msg.flags = client->flags & I2C_M_TEN;
1082        msg.len = count;
1083        msg.buf = (char *)buf;
1084
1085        ret = i2c_transfer(adap, &msg, 1);
1086
1087        /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1088           transmitted, else error code. */
1089        return (ret == 1) ? count : ret;
1090}
1091EXPORT_SYMBOL(i2c_master_send);
1092
1093/**
1094 * i2c_master_recv - issue a single I2C message in master receive mode
1095 * @client: Handle to slave device
1096 * @buf: Where to store data read from slave
1097 * @count: How many bytes to read
1098 *
1099 * Returns negative errno, or else the number of bytes read.
1100 */
1101int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1102{
1103        struct i2c_adapter *adap=client->adapter;
1104        struct i2c_msg msg;
1105        int ret;
1106
1107        msg.addr = client->addr;
1108        msg.flags = client->flags & I2C_M_TEN;
1109        msg.flags |= I2C_M_RD;
1110        msg.len = count;
1111        msg.buf = buf;
1112
1113        ret = i2c_transfer(adap, &msg, 1);
1114
1115        /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1116           transmitted, else error code. */
1117        return (ret == 1) ? count : ret;
1118}
1119EXPORT_SYMBOL(i2c_master_recv);
1120
1121/* ----------------------------------------------------
1122 * the i2c address scanning function
1123 * Will not work for 10-bit addresses!
1124 * ----------------------------------------------------
1125 */
1126static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
1127                             int (*found_proc) (struct i2c_adapter *, int, int))
1128{
1129        int err;
1130
1131        /* Make sure the address is valid */
1132        if (addr < 0x03 || addr > 0x77) {
1133                dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1134                         addr);
1135                return -EINVAL;
1136        }
1137
1138        /* Skip if already in use */
1139        if (i2c_check_addr(adapter, addr))
1140                return 0;
1141
1142        /* Make sure there is something at this address, unless forced */
1143        if (kind < 0) {
1144                if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1145                                   I2C_SMBUS_QUICK, NULL) < 0)
1146                        return 0;
1147
1148                /* prevent 24RF08 corruption */
1149                if ((addr & ~0x0f) == 0x50)
1150                        i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1151                                       I2C_SMBUS_QUICK, NULL);
1152        }
1153
1154        /* Finally call the custom detection function */
1155        err = found_proc(adapter, addr, kind);
1156        /* -ENODEV can be returned if there is a chip at the given address
1157           but it isn't supported by this chip driver. We catch it here as
1158           this isn't an error. */
1159        if (err == -ENODEV)
1160                err = 0;
1161
1162        if (err)
1163                dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1164                         addr, err);
1165        return err;
1166}
1167
1168int i2c_probe(struct i2c_adapter *adapter,
1169              const struct i2c_client_address_data *address_data,
1170              int (*found_proc) (struct i2c_adapter *, int, int))
1171{
1172        int i, err;
1173        int adap_id = i2c_adapter_id(adapter);
1174
1175        /* Force entries are done first, and are not affected by ignore
1176           entries */
1177        if (address_data->forces) {
1178                const unsigned short * const *forces = address_data->forces;
1179                int kind;
1180
1181                for (kind = 0; forces[kind]; kind++) {
1182                        for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1183                             i += 2) {
1184                                if (forces[kind][i] == adap_id
1185                                 || forces[kind][i] == ANY_I2C_BUS) {
1186                                        dev_dbg(&adapter->dev, "found force "
1187                                                "parameter for adapter %d, "
1188                                                "addr 0x%02x, kind %d\n",
1189                                                adap_id, forces[kind][i + 1],
1190                                                kind);
1191                                        err = i2c_probe_address(adapter,
1192                                                forces[kind][i + 1],
1193                                                kind, found_proc);
1194                                        if (err)
1195                                                return err;
1196                                }
1197                        }
1198                }
1199        }
1200
1201        /* Stop here if we can't use SMBUS_QUICK */
1202        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1203                if (address_data->probe[0] == I2C_CLIENT_END
1204                 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1205                        return 0;
1206
1207                dev_dbg(&adapter->dev, "SMBus Quick command not supported, "
1208                        "can't probe for chips\n");
1209                return -EOPNOTSUPP;
1210        }
1211
1212        /* Probe entries are done second, and are not affected by ignore
1213           entries either */
1214        for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1215                if (address_data->probe[i] == adap_id
1216                 || address_data->probe[i] == ANY_I2C_BUS) {
1217                        dev_dbg(&adapter->dev, "found probe parameter for "
1218                                "adapter %d, addr 0x%02x\n", adap_id,
1219                                address_data->probe[i + 1]);
1220                        err = i2c_probe_address(adapter,
1221                                                address_data->probe[i + 1],
1222                                                -1, found_proc);
1223                        if (err)
1224                                return err;
1225                }
1226        }
1227
1228        /* Normal entries are done last, unless shadowed by an ignore entry */
1229        for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1230                int j, ignore;
1231
1232                ignore = 0;
1233                for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1234                     j += 2) {
1235                        if ((address_data->ignore[j] == adap_id ||
1236                             address_data->ignore[j] == ANY_I2C_BUS)
1237                         && address_data->ignore[j + 1]
1238                            == address_data->normal_i2c[i]) {
1239                                dev_dbg(&adapter->dev, "found ignore "
1240                                        "parameter for adapter %d, "
1241                                        "addr 0x%02x\n", adap_id,
1242                                        address_data->ignore[j + 1]);
1243                                ignore = 1;
1244                                break;
1245                        }
1246                }
1247                if (ignore)
1248                        continue;
1249
1250                dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1251                        "addr 0x%02x\n", adap_id,
1252                        address_data->normal_i2c[i]);
1253                err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1254                                        -1, found_proc);
1255                if (err)
1256                        return err;
1257        }
1258
1259        return 0;
1260}
1261EXPORT_SYMBOL(i2c_probe);
1262
1263/* Separate detection function for new-style drivers */
1264static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1265                              struct i2c_driver *driver)
1266{
1267        struct i2c_board_info info;
1268        struct i2c_adapter *adapter = temp_client->adapter;
1269        int addr = temp_client->addr;
1270        int err;
1271
1272        /* Make sure the address is valid */
1273        if (addr < 0x03 || addr > 0x77) {
1274                dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1275                         addr);
1276                return -EINVAL;
1277        }
1278
1279        /* Skip if already in use */
1280        if (i2c_check_addr(adapter, addr))
1281                return 0;
1282
1283        /* Make sure there is something at this address, unless forced */
1284        if (kind < 0) {
1285                if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1286                                   I2C_SMBUS_QUICK, NULL) < 0)
1287                        return 0;
1288
1289                /* prevent 24RF08 corruption */
1290                if ((addr & ~0x0f) == 0x50)
1291                        i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1292                                       I2C_SMBUS_QUICK, NULL);
1293        }
1294
1295        /* Finally call the custom detection function */
1296        memset(&info, 0, sizeof(struct i2c_board_info));
1297        info.addr = addr;
1298        err = driver->detect(temp_client, kind, &info);
1299        if (err) {
1300                /* -ENODEV is returned if the detection fails. We catch it
1301                   here as this isn't an error. */
1302                return err == -ENODEV ? 0 : err;
1303        }
1304
1305        /* Consistency check */
1306        if (info.type[0] == '\0') {
1307                dev_err(&adapter->dev, "%s detection function provided "
1308                        "no name for 0x%x\n", driver->driver.name,
1309                        addr);
1310        } else {
1311                struct i2c_client *client;
1312
1313                /* Detection succeeded, instantiate the device */
1314                dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1315                        info.type, info.addr);
1316                client = i2c_new_device(adapter, &info);
1317                if (client)
1318                        list_add_tail(&client->detected, &driver->clients);
1319                else
1320                        dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1321                                info.type, info.addr);
1322        }
1323        return 0;
1324}
1325
1326static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1327{
1328        const struct i2c_client_address_data *address_data;
1329        struct i2c_client *temp_client;
1330        int i, err = 0;
1331        int adap_id = i2c_adapter_id(adapter);
1332
1333        address_data = driver->address_data;
1334        if (!driver->detect || !address_data)
1335                return 0;
1336
1337        /* Set up a temporary client to help detect callback */
1338        temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1339        if (!temp_client)
1340                return -ENOMEM;
1341        temp_client->adapter = adapter;
1342
1343        /* Force entries are done first, and are not affected by ignore
1344           entries */
1345        if (address_data->forces) {
1346                const unsigned short * const *forces = address_data->forces;
1347                int kind;
1348
1349                for (kind = 0; forces[kind]; kind++) {
1350                        for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1351                             i += 2) {
1352                                if (forces[kind][i] == adap_id
1353                                 || forces[kind][i] == ANY_I2C_BUS) {
1354                                        dev_dbg(&adapter->dev, "found force "
1355                                                "parameter for adapter %d, "
1356                                                "addr 0x%02x, kind %d\n",
1357                                                adap_id, forces[kind][i + 1],
1358                                                kind);
1359                                        temp_client->addr = forces[kind][i + 1];
1360                                        err = i2c_detect_address(temp_client,
1361                                                kind, driver);
1362                                        if (err)
1363                                                goto exit_free;
1364                                }
1365                        }
1366                }
1367        }
1368
1369        /* Stop here if the classes do not match */
1370        if (!(adapter->class & driver->class))
1371                goto exit_free;
1372
1373        /* Stop here if we can't use SMBUS_QUICK */
1374        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1375                if (address_data->probe[0] == I2C_CLIENT_END
1376                 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1377                        goto exit_free;
1378
1379                dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1380                         "can't probe for chips\n");
1381                err = -EOPNOTSUPP;
1382                goto exit_free;
1383        }
1384
1385        /* Probe entries are done second, and are not affected by ignore
1386           entries either */
1387        for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1388                if (address_data->probe[i] == adap_id
1389                 || address_data->probe[i] == ANY_I2C_BUS) {
1390                        dev_dbg(&adapter->dev, "found probe parameter for "
1391                                "adapter %d, addr 0x%02x\n", adap_id,
1392                                address_data->probe[i + 1]);
1393                        temp_client->addr = address_data->probe[i + 1];
1394                        err = i2c_detect_address(temp_client, -1, driver);
1395                        if (err)
1396                                goto exit_free;
1397                }
1398        }
1399
1400        /* Normal entries are done last, unless shadowed by an ignore entry */
1401        for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1402                int j, ignore;
1403
1404                ignore = 0;
1405                for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1406                     j += 2) {
1407                        if ((address_data->ignore[j] == adap_id ||
1408                             address_data->ignore[j] == ANY_I2C_BUS)
1409                         && address_data->ignore[j + 1]
1410                            == address_data->normal_i2c[i]) {
1411                                dev_dbg(&adapter->dev, "found ignore "
1412                                        "parameter for adapter %d, "
1413                                        "addr 0x%02x\n", adap_id,
1414                                        address_data->ignore[j + 1]);
1415                                ignore = 1;
1416                                break;
1417                        }
1418                }
1419                if (ignore)
1420                        continue;
1421
1422                dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1423                        "addr 0x%02x\n", adap_id,
1424                        address_data->normal_i2c[i]);
1425                temp_client->addr = address_data->normal_i2c[i];
1426                err = i2c_detect_address(temp_client, -1, driver);
1427                if (err)
1428                        goto exit_free;
1429        }
1430
1431 exit_free:
1432        kfree(temp_client);
1433        return err;
1434}
1435
1436struct i2c_client *
1437i2c_new_probed_device(struct i2c_adapter *adap,
1438                      struct i2c_board_info *info,
1439                      unsigned short const *addr_list)
1440{
1441        int i;
1442
1443        /* Stop here if the bus doesn't support probing */
1444        if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1445                dev_err(&adap->dev, "Probing not supported\n");
1446                return NULL;
1447        }
1448
1449        for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1450                /* Check address validity */
1451                if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1452                        dev_warn(&adap->dev, "Invalid 7-bit address "
1453                                 "0x%02x\n", addr_list[i]);
1454                        continue;
1455                }
1456
1457                /* Check address availability */
1458                if (i2c_check_addr(adap, addr_list[i])) {
1459                        dev_dbg(&adap->dev, "Address 0x%02x already in "
1460                                "use, not probing\n", addr_list[i]);
1461                        continue;
1462                }
1463
1464                /* Test address responsiveness
1465                   The default probe method is a quick write, but it is known
1466                   to corrupt the 24RF08 EEPROMs due to a state machine bug,
1467                   and could also irreversibly write-protect some EEPROMs, so
1468                   for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1469                   read instead. Also, some bus drivers don't implement
1470                   quick write, so we fallback to a byte read it that case
1471                   too. */
1472                if ((addr_list[i] & ~0x07) == 0x30
1473                 || (addr_list[i] & ~0x0f) == 0x50
1474                 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1475                        union i2c_smbus_data data;
1476
1477                        if (i2c_smbus_xfer(adap, addr_list[i], 0,
1478                                           I2C_SMBUS_READ, 0,
1479                                           I2C_SMBUS_BYTE, &data) >= 0)
1480                                break;
1481                } else {
1482                        if (i2c_smbus_xfer(adap, addr_list[i], 0,
1483                                           I2C_SMBUS_WRITE, 0,
1484                                           I2C_SMBUS_QUICK, NULL) >= 0)
1485                                break;
1486                }
1487        }
1488
1489        if (addr_list[i] == I2C_CLIENT_END) {
1490                dev_dbg(&adap->dev, "Probing failed, no device found\n");
1491                return NULL;
1492        }
1493
1494        info->addr = addr_list[i];
1495        return i2c_new_device(adap, info);
1496}
1497EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1498
1499struct i2c_adapter* i2c_get_adapter(int id)
1500{
1501        struct i2c_adapter *adapter;
1502
1503        mutex_lock(&core_lock);
1504        adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1505        if (adapter && !try_module_get(adapter->owner))
1506                adapter = NULL;
1507
1508        mutex_unlock(&core_lock);
1509        return adapter;
1510}
1511EXPORT_SYMBOL(i2c_get_adapter);
1512
1513void i2c_put_adapter(struct i2c_adapter *adap)
1514{
1515        module_put(adap->owner);
1516}
1517EXPORT_SYMBOL(i2c_put_adapter);
1518
1519/* The SMBus parts */
1520
1521#define POLY    (0x1070U << 3)
1522static u8
1523crc8(u16 data)
1524{
1525        int i;
1526
1527        for(i = 0; i < 8; i++) {
1528                if (data & 0x8000)
1529                        data = data ^ POLY;
1530                data = data << 1;
1531        }
1532        return (u8)(data >> 8);
1533}
1534
1535/* Incremental CRC8 over count bytes in the array pointed to by p */
1536static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1537{
1538        int i;
1539
1540        for(i = 0; i < count; i++)
1541                crc = crc8((crc ^ p[i]) << 8);
1542        return crc;
1543}
1544
1545/* Assume a 7-bit address, which is reasonable for SMBus */
1546static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1547{
1548        /* The address will be sent first */
1549        u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1550        pec = i2c_smbus_pec(pec, &addr, 1);
1551
1552        /* The data buffer follows */
1553        return i2c_smbus_pec(pec, msg->buf, msg->len);
1554}
1555
1556/* Used for write only transactions */
1557static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1558{
1559        msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1560        msg->len++;
1561}
1562
1563/* Return <0 on CRC error
1564   If there was a write before this read (most cases) we need to take the
1565   partial CRC from the write part into account.
1566   Note that this function does modify the message (we need to decrease the
1567   message length to hide the CRC byte from the caller). */
1568static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1569{
1570        u8 rpec = msg->buf[--msg->len];
1571        cpec = i2c_smbus_msg_pec(cpec, msg);
1572
1573        if (rpec != cpec) {
1574                pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1575                        rpec, cpec);
1576                return -EBADMSG;
1577        }
1578        return 0;
1579}
1580
1581/**
1582 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1583 * @client: Handle to slave device
1584 *
1585 * This executes the SMBus "receive byte" protocol, returning negative errno
1586 * else the byte received from the device.
1587 */
1588s32 i2c_smbus_read_byte(struct i2c_client *client)
1589{
1590        union i2c_smbus_data data;
1591        int status;
1592
1593        status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1594                                I2C_SMBUS_READ, 0,
1595                                I2C_SMBUS_BYTE, &data);
1596        return (status < 0) ? status : data.byte;
1597}
1598EXPORT_SYMBOL(i2c_smbus_read_byte);
1599
1600/**
1601 * i2c_smbus_write_byte - SMBus "send byte" protocol
1602 * @client: Handle to slave device
1603 * @value: Byte to be sent
1604 *
1605 * This executes the SMBus "send byte" protocol, returning negative errno
1606 * else zero on success.
1607 */
1608s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1609{
1610        return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1611                              I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1612}
1613EXPORT_SYMBOL(i2c_smbus_write_byte);
1614
1615/**
1616 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1617 * @client: Handle to slave device
1618 * @command: Byte interpreted by slave
1619 *
1620 * This executes the SMBus "read byte" protocol, returning negative errno
1621 * else a data byte received from the device.
1622 */
1623s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1624{
1625        union i2c_smbus_data data;
1626        int status;
1627
1628        status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1629                                I2C_SMBUS_READ, command,
1630                                I2C_SMBUS_BYTE_DATA, &data);
1631        return (status < 0) ? status : data.byte;
1632}
1633EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1634
1635/**
1636 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1637 * @client: Handle to slave device
1638 * @command: Byte interpreted by slave
1639 * @value: Byte being written
1640 *
1641 * This executes the SMBus "write byte" protocol, returning negative errno
1642 * else zero on success.
1643 */
1644s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1645{
1646        union i2c_smbus_data data;
1647        data.byte = value;
1648        return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1649                              I2C_SMBUS_WRITE,command,
1650                              I2C_SMBUS_BYTE_DATA,&data);
1651}
1652EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1653
1654/**
1655 * i2c_smbus_read_word_data - SMBus "read word" protocol
1656 * @client: Handle to slave device
1657 * @command: Byte interpreted by slave
1658 *
1659 * This executes the SMBus "read word" protocol, returning negative errno
1660 * else a 16-bit unsigned "word" received from the device.
1661 */
1662s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1663{
1664        union i2c_smbus_data data;
1665        int status;
1666
1667        status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1668                                I2C_SMBUS_READ, command,
1669                                I2C_SMBUS_WORD_DATA, &data);
1670        return (status < 0) ? status : data.word;
1671}
1672EXPORT_SYMBOL(i2c_smbus_read_word_data);
1673
1674/**
1675 * i2c_smbus_write_word_data - SMBus "write word" protocol
1676 * @client: Handle to slave device
1677 * @command: Byte interpreted by slave
1678 * @value: 16-bit "word" being written
1679 *
1680 * This executes the SMBus "write word" protocol, returning negative errno
1681 * else zero on success.
1682 */
1683s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1684{
1685        union i2c_smbus_data data;
1686        data.word = value;
1687        return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1688                              I2C_SMBUS_WRITE,command,
1689                              I2C_SMBUS_WORD_DATA,&data);
1690}
1691EXPORT_SYMBOL(i2c_smbus_write_word_data);
1692
1693/**
1694 * i2c_smbus_process_call - SMBus "process call" protocol
1695 * @client: Handle to slave device
1696 * @command: Byte interpreted by slave
1697 * @value: 16-bit "word" being written
1698 *
1699 * This executes the SMBus "process call" protocol, returning negative errno
1700 * else a 16-bit unsigned "word" received from the device.
1701 */
1702s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1703{
1704        union i2c_smbus_data data;
1705        int status;
1706        data.word = value;
1707
1708        status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1709                                I2C_SMBUS_WRITE, command,
1710                                I2C_SMBUS_PROC_CALL, &data);
1711        return (status < 0) ? status : data.word;
1712}
1713EXPORT_SYMBOL(i2c_smbus_process_call);
1714
1715/**
1716 * i2c_smbus_read_block_data - SMBus "block read" protocol
1717 * @client: Handle to slave device
1718 * @command: Byte interpreted by slave
1719 * @values: Byte array into which data will be read; big enough to hold
1720 *      the data returned by the slave.  SMBus allows at most 32 bytes.
1721 *
1722 * This executes the SMBus "block read" protocol, returning negative errno
1723 * else the number of data bytes in the slave's response.
1724 *
1725 * Note that using this function requires that the client's adapter support
1726 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality.  Not all adapter drivers
1727 * support this; its emulation through I2C messaging relies on a specific
1728 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1729 */
1730s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1731                              u8 *values)
1732{
1733        union i2c_smbus_data data;
1734        int status;
1735
1736        status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1737                                I2C_SMBUS_READ, command,
1738                                I2C_SMBUS_BLOCK_DATA, &data);
1739        if (status)
1740                return status;
1741
1742        memcpy(values, &data.block[1], data.block[0]);
1743        return data.block[0];
1744}
1745EXPORT_SYMBOL(i2c_smbus_read_block_data);
1746
1747/**
1748 * i2c_smbus_write_block_data - SMBus "block write" protocol
1749 * @client: Handle to slave device
1750 * @command: Byte interpreted by slave
1751 * @length: Size of data block; SMBus allows at most 32 bytes
1752 * @values: Byte array which will be written.
1753 *
1754 * This executes the SMBus "block write" protocol, returning negative errno
1755 * else zero on success.
1756 */
1757s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1758                               u8 length, const u8 *values)
1759{
1760        union i2c_smbus_data data;
1761
1762        if (length > I2C_SMBUS_BLOCK_MAX)
1763                length = I2C_SMBUS_BLOCK_MAX;
1764        data.block[0] = length;
1765        memcpy(&data.block[1], values, length);
1766        return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1767                              I2C_SMBUS_WRITE,command,
1768                              I2C_SMBUS_BLOCK_DATA,&data);
1769}
1770EXPORT_SYMBOL(i2c_smbus_write_block_data);
1771
1772/* Returns the number of read bytes */
1773s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1774                                  u8 length, u8 *values)
1775{
1776        union i2c_smbus_data data;
1777        int status;
1778
1779        if (length > I2C_SMBUS_BLOCK_MAX)
1780                length = I2C_SMBUS_BLOCK_MAX;
1781        data.block[0] = length;
1782        status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1783                                I2C_SMBUS_READ, command,
1784                                I2C_SMBUS_I2C_BLOCK_DATA, &data);
1785        if (status < 0)
1786                return status;
1787
1788        memcpy(values, &data.block[1], data.block[0]);
1789        return data.block[0];
1790}
1791EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1792
1793s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1794                                   u8 length, const u8 *values)
1795{
1796        union i2c_smbus_data data;
1797
1798        if (length > I2C_SMBUS_BLOCK_MAX)
1799                length = I2C_SMBUS_BLOCK_MAX;
1800        data.block[0] = length;
1801        memcpy(data.block + 1, values, length);
1802        return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1803                              I2C_SMBUS_WRITE, command,
1804                              I2C_SMBUS_I2C_BLOCK_DATA, &data);
1805}
1806EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1807
1808/* Simulate a SMBus command using the i2c protocol
1809   No checking of parameters is done!  */
1810static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1811                                   unsigned short flags,
1812                                   char read_write, u8 command, int size,
1813                                   union i2c_smbus_data * data)
1814{
1815        /* So we need to generate a series of msgs. In the case of writing, we
1816          need to use only one message; when reading, we need two. We initialize
1817          most things with sane defaults, to keep the code below somewhat
1818          simpler. */
1819        unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1820        unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1821        int num = read_write == I2C_SMBUS_READ?2:1;
1822        struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1823                                  { addr, flags | I2C_M_RD, 0, msgbuf1 }
1824                                };
1825        int i;
1826        u8 partial_pec = 0;
1827        int status;
1828
1829        msgbuf0[0] = command;
1830        switch(size) {
1831        case I2C_SMBUS_QUICK:
1832                msg[0].len = 0;
1833                /* Special case: The read/write field is used as data */
1834                msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1835                num = 1;
1836                break;
1837        case I2C_SMBUS_BYTE:
1838                if (read_write == I2C_SMBUS_READ) {
1839                        /* Special case: only a read! */
1840                        msg[0].flags = I2C_M_RD | flags;
1841                        num = 1;
1842                }
1843                break;
1844        case I2C_SMBUS_BYTE_DATA:
1845                if (read_write == I2C_SMBUS_READ)
1846                        msg[1].len = 1;
1847                else {
1848                        msg[0].len = 2;
1849                        msgbuf0[1] = data->byte;
1850                }
1851                break;
1852        case I2C_SMBUS_WORD_DATA:
1853                if (read_write == I2C_SMBUS_READ)
1854                        msg[1].len = 2;
1855                else {
1856                        msg[0].len=3;
1857                        msgbuf0[1] = data->word & 0xff;
1858                        msgbuf0[2] = data->word >> 8;
1859                }
1860                break;
1861        case I2C_SMBUS_PROC_CALL:
1862                num = 2; /* Special case */
1863                read_write = I2C_SMBUS_READ;
1864                msg[0].len = 3;
1865                msg[1].len = 2;
1866                msgbuf0[1] = data->word & 0xff;
1867                msgbuf0[2] = data->word >> 8;
1868                break;
1869        case I2C_SMBUS_BLOCK_DATA:
1870                if (read_write == I2C_SMBUS_READ) {
1871                        msg[1].flags |= I2C_M_RECV_LEN;
1872                        msg[1].len = 1; /* block length will be added by
1873                                           the underlying bus driver */
1874                } else {
1875                        msg[0].len = data->block[0] + 2;
1876                        if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1877                                dev_err(&adapter->dev,
1878                                        "Invalid block write size %d\n",
1879                                        data->block[0]);
1880                                return -EINVAL;
1881                        }
1882                        for (i = 1; i < msg[0].len; i++)
1883                                msgbuf0[i] = data->block[i-1];
1884                }
1885                break;
1886        case I2C_SMBUS_BLOCK_PROC_CALL:
1887                num = 2; /* Another special case */
1888                read_write = I2C_SMBUS_READ;
1889                if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1890                        dev_err(&adapter->dev,
1891                                "Invalid block write size %d\n",
1892                                data->block[0]);
1893                        return -EINVAL;
1894                }
1895                msg[0].len = data->block[0] + 2;
1896                for (i = 1; i < msg[0].len; i++)
1897                        msgbuf0[i] = data->block[i-1];
1898                msg[1].flags |= I2C_M_RECV_LEN;
1899                msg[1].len = 1; /* block length will be added by
1900                                   the underlying bus driver */
1901                break;
1902        case I2C_SMBUS_I2C_BLOCK_DATA:
1903                if (read_write == I2C_SMBUS_READ) {
1904                        msg[1].len = data->block[0];
1905                } else {
1906                        msg[0].len = data->block[0] + 1;
1907                        if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1908                                dev_err(&adapter->dev,
1909                                        "Invalid block write size %d\n",
1910                                        data->block[0]);
1911                                return -EINVAL;
1912                        }
1913                        for (i = 1; i <= data->block[0]; i++)
1914                                msgbuf0[i] = data->block[i];
1915                }
1916                break;
1917        default:
1918                dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1919                return -EOPNOTSUPP;
1920        }
1921
1922        i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1923                                      && size != I2C_SMBUS_I2C_BLOCK_DATA);
1924        if (i) {
1925                /* Compute PEC if first message is a write */
1926                if (!(msg[0].flags & I2C_M_RD)) {
1927                        if (num == 1) /* Write only */
1928                                i2c_smbus_add_pec(&msg[0]);
1929                        else /* Write followed by read */
1930                                partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1931                }
1932                /* Ask for PEC if last message is a read */
1933                if (msg[num-1].flags & I2C_M_RD)
1934                        msg[num-1].len++;
1935        }
1936
1937        status = i2c_transfer(adapter, msg, num);
1938        if (status < 0)
1939                return status;
1940
1941        /* Check PEC if last message is a read */
1942        if (i && (msg[num-1].flags & I2C_M_RD)) {
1943                status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1944                if (status < 0)
1945                        return status;
1946        }
1947
1948        if (read_write == I2C_SMBUS_READ)
1949                switch(size) {
1950                        case I2C_SMBUS_BYTE:
1951                                data->byte = msgbuf0[0];
1952                                break;
1953                        case I2C_SMBUS_BYTE_DATA:
1954                                data->byte = msgbuf1[0];
1955                                break;
1956                        case I2C_SMBUS_WORD_DATA:
1957                        case I2C_SMBUS_PROC_CALL:
1958                                data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1959                                break;
1960                        case I2C_SMBUS_I2C_BLOCK_DATA:
1961                                for (i = 0; i < data->block[0]; i++)
1962                                        data->block[i+1] = msgbuf1[i];
1963                                break;
1964                        case I2C_SMBUS_BLOCK_DATA:
1965                        case I2C_SMBUS_BLOCK_PROC_CALL:
1966                                for (i = 0; i < msgbuf1[0] + 1; i++)
1967                                        data->block[i] = msgbuf1[i];
1968                                break;
1969                }
1970        return 0;
1971}
1972
1973/**
1974 * i2c_smbus_xfer - execute SMBus protocol operations
1975 * @adapter: Handle to I2C bus
1976 * @addr: Address of SMBus slave on that bus
1977 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1978 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1979 * @command: Byte interpreted by slave, for protocols which use such bytes
1980 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1981 * @data: Data to be read or written
1982 *
1983 * This executes an SMBus protocol operation, and returns a negative
1984 * errno code else zero on success.
1985 */
1986s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1987                   char read_write, u8 command, int protocol,
1988                   union i2c_smbus_data * data)
1989{
1990        s32 res;
1991
1992        flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1993
1994        if (adapter->algo->smbus_xfer) {
1995                mutex_lock(&adapter->bus_lock);
1996                res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1997                                                command, protocol, data);
1998                mutex_unlock(&adapter->bus_lock);
1999        } else
2000                res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
2001                                              command, protocol, data);
2002
2003        return res;
2004}
2005EXPORT_SYMBOL(i2c_smbus_xfer);
2006
2007MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2008MODULE_DESCRIPTION("I2C-Bus main module");
2009MODULE_LICENSE("GPL");
2010