LCOV - code coverage report
Current view: top level - drivers/i2c - i2c-core.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 184 775 23.7 %
Date: 2017-01-25 Functions: 17 68 25.0 %

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

Generated by: LCOV version 1.10