LCOV - code coverage report
Current view: top level - drivers/i2c - i2c-core-smbus.c (source / functions) Hit Total Coverage
Test: Real Lines: 0 211 0.0 %
Date: 2020-10-17 15:46:16 Functions: 0 19 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  * Linux I2C core SMBus and SMBus emulation code
       4                 :            :  *
       5                 :            :  * This file contains the SMBus functions which are always included in the I2C
       6                 :            :  * core because they can be emulated via I2C. SMBus specific extensions
       7                 :            :  * (e.g. smbalert) are handled in a seperate i2c-smbus module.
       8                 :            :  *
       9                 :            :  * All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
      10                 :            :  * SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
      11                 :            :  * Jean Delvare <jdelvare@suse.de>
      12                 :            :  */
      13                 :            : #include <linux/device.h>
      14                 :            : #include <linux/err.h>
      15                 :            : #include <linux/i2c.h>
      16                 :            : #include <linux/i2c-smbus.h>
      17                 :            : #include <linux/slab.h>
      18                 :            : 
      19                 :            : #include "i2c-core.h"
      20                 :            : 
      21                 :            : #define CREATE_TRACE_POINTS
      22                 :            : #include <trace/events/smbus.h>
      23                 :            : 
      24                 :            : 
      25                 :            : /* The SMBus parts */
      26                 :            : 
      27                 :            : #define POLY    (0x1070U << 3)
      28                 :            : static u8 crc8(u16 data)
      29                 :            : {
      30                 :            :         int i;
      31                 :            : 
      32                 :          0 :         for (i = 0; i < 8; i++) {
      33                 :          0 :                 if (data & 0x8000)
      34                 :          0 :                         data = data ^ POLY;
      35                 :          0 :                 data = data << 1;
      36                 :            :         }
      37                 :          0 :         return (u8)(data >> 8);
      38                 :            : }
      39                 :            : 
      40                 :            : /* Incremental CRC8 over count bytes in the array pointed to by p */
      41                 :            : static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
      42                 :            : {
      43                 :            :         int i;
      44                 :            : 
      45                 :          0 :         for (i = 0; i < count; i++)
      46                 :          0 :                 crc = crc8((crc ^ p[i]) << 8);
      47                 :          0 :         return crc;
      48                 :            : }
      49                 :            : 
      50                 :            : /* Assume a 7-bit address, which is reasonable for SMBus */
      51                 :          0 : static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
      52                 :            : {
      53                 :            :         /* The address will be sent first */
      54                 :          0 :         u8 addr = i2c_8bit_addr_from_msg(msg);
      55                 :            :         pec = i2c_smbus_pec(pec, &addr, 1);
      56                 :            : 
      57                 :            :         /* The data buffer follows */
      58                 :          0 :         return i2c_smbus_pec(pec, msg->buf, msg->len);
      59                 :            : }
      60                 :            : 
      61                 :            : /* Used for write only transactions */
      62                 :          0 : static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
      63                 :            : {
      64                 :          0 :         msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
      65                 :          0 :         msg->len++;
      66                 :          0 : }
      67                 :            : 
      68                 :            : /* Return <0 on CRC error
      69                 :            :    If there was a write before this read (most cases) we need to take the
      70                 :            :    partial CRC from the write part into account.
      71                 :            :    Note that this function does modify the message (we need to decrease the
      72                 :            :    message length to hide the CRC byte from the caller). */
      73                 :          0 : static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
      74                 :            : {
      75                 :          0 :         u8 rpec = msg->buf[--msg->len];
      76                 :          0 :         cpec = i2c_smbus_msg_pec(cpec, msg);
      77                 :            : 
      78                 :          0 :         if (rpec != cpec) {
      79                 :            :                 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
      80                 :            :                         rpec, cpec);
      81                 :            :                 return -EBADMSG;
      82                 :            :         }
      83                 :          0 :         return 0;
      84                 :            : }
      85                 :            : 
      86                 :            : /**
      87                 :            :  * i2c_smbus_read_byte - SMBus "receive byte" protocol
      88                 :            :  * @client: Handle to slave device
      89                 :            :  *
      90                 :            :  * This executes the SMBus "receive byte" protocol, returning negative errno
      91                 :            :  * else the byte received from the device.
      92                 :            :  */
      93                 :          0 : s32 i2c_smbus_read_byte(const struct i2c_client *client)
      94                 :            : {
      95                 :            :         union i2c_smbus_data data;
      96                 :            :         int status;
      97                 :            : 
      98                 :          0 :         status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
      99                 :            :                                 I2C_SMBUS_READ, 0,
     100                 :            :                                 I2C_SMBUS_BYTE, &data);
     101                 :          0 :         return (status < 0) ? status : data.byte;
     102                 :            : }
     103                 :            : EXPORT_SYMBOL(i2c_smbus_read_byte);
     104                 :            : 
     105                 :            : /**
     106                 :            :  * i2c_smbus_write_byte - SMBus "send byte" protocol
     107                 :            :  * @client: Handle to slave device
     108                 :            :  * @value: Byte to be sent
     109                 :            :  *
     110                 :            :  * This executes the SMBus "send byte" protocol, returning negative errno
     111                 :            :  * else zero on success.
     112                 :            :  */
     113                 :          0 : s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
     114                 :            : {
     115                 :          0 :         return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
     116                 :            :                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
     117                 :            : }
     118                 :            : EXPORT_SYMBOL(i2c_smbus_write_byte);
     119                 :            : 
     120                 :            : /**
     121                 :            :  * i2c_smbus_read_byte_data - SMBus "read byte" protocol
     122                 :            :  * @client: Handle to slave device
     123                 :            :  * @command: Byte interpreted by slave
     124                 :            :  *
     125                 :            :  * This executes the SMBus "read byte" protocol, returning negative errno
     126                 :            :  * else a data byte received from the device.
     127                 :            :  */
     128                 :          0 : s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
     129                 :            : {
     130                 :            :         union i2c_smbus_data data;
     131                 :            :         int status;
     132                 :            : 
     133                 :          0 :         status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
     134                 :            :                                 I2C_SMBUS_READ, command,
     135                 :            :                                 I2C_SMBUS_BYTE_DATA, &data);
     136                 :          0 :         return (status < 0) ? status : data.byte;
     137                 :            : }
     138                 :            : EXPORT_SYMBOL(i2c_smbus_read_byte_data);
     139                 :            : 
     140                 :            : /**
     141                 :            :  * i2c_smbus_write_byte_data - SMBus "write byte" protocol
     142                 :            :  * @client: Handle to slave device
     143                 :            :  * @command: Byte interpreted by slave
     144                 :            :  * @value: Byte being written
     145                 :            :  *
     146                 :            :  * This executes the SMBus "write byte" protocol, returning negative errno
     147                 :            :  * else zero on success.
     148                 :            :  */
     149                 :          0 : s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
     150                 :            :                               u8 value)
     151                 :            : {
     152                 :            :         union i2c_smbus_data data;
     153                 :          0 :         data.byte = value;
     154                 :          0 :         return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
     155                 :            :                               I2C_SMBUS_WRITE, command,
     156                 :            :                               I2C_SMBUS_BYTE_DATA, &data);
     157                 :            : }
     158                 :            : EXPORT_SYMBOL(i2c_smbus_write_byte_data);
     159                 :            : 
     160                 :            : /**
     161                 :            :  * i2c_smbus_read_word_data - SMBus "read word" protocol
     162                 :            :  * @client: Handle to slave device
     163                 :            :  * @command: Byte interpreted by slave
     164                 :            :  *
     165                 :            :  * This executes the SMBus "read word" protocol, returning negative errno
     166                 :            :  * else a 16-bit unsigned "word" received from the device.
     167                 :            :  */
     168                 :          0 : s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
     169                 :            : {
     170                 :            :         union i2c_smbus_data data;
     171                 :            :         int status;
     172                 :            : 
     173                 :          0 :         status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
     174                 :            :                                 I2C_SMBUS_READ, command,
     175                 :            :                                 I2C_SMBUS_WORD_DATA, &data);
     176                 :          0 :         return (status < 0) ? status : data.word;
     177                 :            : }
     178                 :            : EXPORT_SYMBOL(i2c_smbus_read_word_data);
     179                 :            : 
     180                 :            : /**
     181                 :            :  * i2c_smbus_write_word_data - SMBus "write word" protocol
     182                 :            :  * @client: Handle to slave device
     183                 :            :  * @command: Byte interpreted by slave
     184                 :            :  * @value: 16-bit "word" being written
     185                 :            :  *
     186                 :            :  * This executes the SMBus "write word" protocol, returning negative errno
     187                 :            :  * else zero on success.
     188                 :            :  */
     189                 :          0 : s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
     190                 :            :                               u16 value)
     191                 :            : {
     192                 :            :         union i2c_smbus_data data;
     193                 :          0 :         data.word = value;
     194                 :          0 :         return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
     195                 :            :                               I2C_SMBUS_WRITE, command,
     196                 :            :                               I2C_SMBUS_WORD_DATA, &data);
     197                 :            : }
     198                 :            : EXPORT_SYMBOL(i2c_smbus_write_word_data);
     199                 :            : 
     200                 :            : /**
     201                 :            :  * i2c_smbus_read_block_data - SMBus "block read" protocol
     202                 :            :  * @client: Handle to slave device
     203                 :            :  * @command: Byte interpreted by slave
     204                 :            :  * @values: Byte array into which data will be read; big enough to hold
     205                 :            :  *      the data returned by the slave.  SMBus allows at most 32 bytes.
     206                 :            :  *
     207                 :            :  * This executes the SMBus "block read" protocol, returning negative errno
     208                 :            :  * else the number of data bytes in the slave's response.
     209                 :            :  *
     210                 :            :  * Note that using this function requires that the client's adapter support
     211                 :            :  * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality.  Not all adapter drivers
     212                 :            :  * support this; its emulation through I2C messaging relies on a specific
     213                 :            :  * mechanism (I2C_M_RECV_LEN) which may not be implemented.
     214                 :            :  */
     215                 :          0 : s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
     216                 :            :                               u8 *values)
     217                 :            : {
     218                 :            :         union i2c_smbus_data data;
     219                 :            :         int status;
     220                 :            : 
     221                 :          0 :         status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
     222                 :            :                                 I2C_SMBUS_READ, command,
     223                 :            :                                 I2C_SMBUS_BLOCK_DATA, &data);
     224                 :          0 :         if (status)
     225                 :            :                 return status;
     226                 :            : 
     227                 :          0 :         memcpy(values, &data.block[1], data.block[0]);
     228                 :          0 :         return data.block[0];
     229                 :            : }
     230                 :            : EXPORT_SYMBOL(i2c_smbus_read_block_data);
     231                 :            : 
     232                 :            : /**
     233                 :            :  * i2c_smbus_write_block_data - SMBus "block write" protocol
     234                 :            :  * @client: Handle to slave device
     235                 :            :  * @command: Byte interpreted by slave
     236                 :            :  * @length: Size of data block; SMBus allows at most 32 bytes
     237                 :            :  * @values: Byte array which will be written.
     238                 :            :  *
     239                 :            :  * This executes the SMBus "block write" protocol, returning negative errno
     240                 :            :  * else zero on success.
     241                 :            :  */
     242                 :          0 : s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
     243                 :            :                                u8 length, const u8 *values)
     244                 :            : {
     245                 :            :         union i2c_smbus_data data;
     246                 :            : 
     247                 :          0 :         if (length > I2C_SMBUS_BLOCK_MAX)
     248                 :            :                 length = I2C_SMBUS_BLOCK_MAX;
     249                 :          0 :         data.block[0] = length;
     250                 :          0 :         memcpy(&data.block[1], values, length);
     251                 :          0 :         return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
     252                 :            :                               I2C_SMBUS_WRITE, command,
     253                 :            :                               I2C_SMBUS_BLOCK_DATA, &data);
     254                 :            : }
     255                 :            : EXPORT_SYMBOL(i2c_smbus_write_block_data);
     256                 :            : 
     257                 :            : /* Returns the number of read bytes */
     258                 :          0 : s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
     259                 :            :                                   u8 length, u8 *values)
     260                 :            : {
     261                 :            :         union i2c_smbus_data data;
     262                 :            :         int status;
     263                 :            : 
     264                 :          0 :         if (length > I2C_SMBUS_BLOCK_MAX)
     265                 :            :                 length = I2C_SMBUS_BLOCK_MAX;
     266                 :          0 :         data.block[0] = length;
     267                 :          0 :         status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
     268                 :            :                                 I2C_SMBUS_READ, command,
     269                 :            :                                 I2C_SMBUS_I2C_BLOCK_DATA, &data);
     270                 :          0 :         if (status < 0)
     271                 :            :                 return status;
     272                 :            : 
     273                 :          0 :         memcpy(values, &data.block[1], data.block[0]);
     274                 :          0 :         return data.block[0];
     275                 :            : }
     276                 :            : EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
     277                 :            : 
     278                 :          0 : s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
     279                 :            :                                    u8 length, const u8 *values)
     280                 :            : {
     281                 :            :         union i2c_smbus_data data;
     282                 :            : 
     283                 :          0 :         if (length > I2C_SMBUS_BLOCK_MAX)
     284                 :            :                 length = I2C_SMBUS_BLOCK_MAX;
     285                 :          0 :         data.block[0] = length;
     286                 :          0 :         memcpy(data.block + 1, values, length);
     287                 :          0 :         return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
     288                 :            :                               I2C_SMBUS_WRITE, command,
     289                 :            :                               I2C_SMBUS_I2C_BLOCK_DATA, &data);
     290                 :            : }
     291                 :            : EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
     292                 :            : 
     293                 :          0 : static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val)
     294                 :            : {
     295                 :          0 :         bool is_read = msg->flags & I2C_M_RD;
     296                 :            :         unsigned char *dma_buf;
     297                 :            : 
     298                 :          0 :         dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL);
     299                 :          0 :         if (!dma_buf)
     300                 :          0 :                 return;
     301                 :            : 
     302                 :          0 :         msg->buf = dma_buf;
     303                 :          0 :         msg->flags |= I2C_M_DMA_SAFE;
     304                 :            : 
     305                 :          0 :         if (init_val)
     306                 :          0 :                 msg->buf[0] = init_val;
     307                 :            : }
     308                 :            : 
     309                 :            : /*
     310                 :            :  * Simulate a SMBus command using the I2C protocol.
     311                 :            :  * No checking of parameters is done!
     312                 :            :  */
     313                 :          0 : static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
     314                 :            :                                    unsigned short flags,
     315                 :            :                                    char read_write, u8 command, int size,
     316                 :            :                                    union i2c_smbus_data *data)
     317                 :            : {
     318                 :            :         /*
     319                 :            :          * So we need to generate a series of msgs. In the case of writing, we
     320                 :            :          * need to use only one message; when reading, we need two. We
     321                 :            :          * initialize most things with sane defaults, to keep the code below
     322                 :            :          * somewhat simpler.
     323                 :            :          */
     324                 :            :         unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
     325                 :            :         unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
     326                 :          0 :         int num = read_write == I2C_SMBUS_READ ? 2 : 1;
     327                 :            :         int i;
     328                 :            :         u8 partial_pec = 0;
     329                 :            :         int status;
     330                 :          0 :         struct i2c_msg msg[2] = {
     331                 :            :                 {
     332                 :            :                         .addr = addr,
     333                 :            :                         .flags = flags,
     334                 :            :                         .len = 1,
     335                 :            :                         .buf = msgbuf0,
     336                 :            :                 }, {
     337                 :            :                         .addr = addr,
     338                 :            :                         .flags = flags | I2C_M_RD,
     339                 :            :                         .len = 0,
     340                 :            :                         .buf = msgbuf1,
     341                 :            :                 },
     342                 :            :         };
     343                 :            : 
     344                 :          0 :         msgbuf0[0] = command;
     345                 :          0 :         switch (size) {
     346                 :            :         case I2C_SMBUS_QUICK:
     347                 :          0 :                 msg[0].len = 0;
     348                 :            :                 /* Special case: The read/write field is used as data */
     349                 :          0 :                 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
     350                 :          0 :                                         I2C_M_RD : 0);
     351                 :            :                 num = 1;
     352                 :          0 :                 break;
     353                 :            :         case I2C_SMBUS_BYTE:
     354                 :          0 :                 if (read_write == I2C_SMBUS_READ) {
     355                 :            :                         /* Special case: only a read! */
     356                 :          0 :                         msg[0].flags = I2C_M_RD | flags;
     357                 :            :                         num = 1;
     358                 :            :                 }
     359                 :            :                 break;
     360                 :            :         case I2C_SMBUS_BYTE_DATA:
     361                 :          0 :                 if (read_write == I2C_SMBUS_READ)
     362                 :          0 :                         msg[1].len = 1;
     363                 :            :                 else {
     364                 :          0 :                         msg[0].len = 2;
     365                 :          0 :                         msgbuf0[1] = data->byte;
     366                 :            :                 }
     367                 :            :                 break;
     368                 :            :         case I2C_SMBUS_WORD_DATA:
     369                 :          0 :                 if (read_write == I2C_SMBUS_READ)
     370                 :          0 :                         msg[1].len = 2;
     371                 :            :                 else {
     372                 :          0 :                         msg[0].len = 3;
     373                 :          0 :                         msgbuf0[1] = data->word & 0xff;
     374                 :          0 :                         msgbuf0[2] = data->word >> 8;
     375                 :            :                 }
     376                 :            :                 break;
     377                 :            :         case I2C_SMBUS_PROC_CALL:
     378                 :            :                 num = 2; /* Special case */
     379                 :            :                 read_write = I2C_SMBUS_READ;
     380                 :          0 :                 msg[0].len = 3;
     381                 :          0 :                 msg[1].len = 2;
     382                 :          0 :                 msgbuf0[1] = data->word & 0xff;
     383                 :          0 :                 msgbuf0[2] = data->word >> 8;
     384                 :          0 :                 break;
     385                 :            :         case I2C_SMBUS_BLOCK_DATA:
     386                 :          0 :                 if (read_write == I2C_SMBUS_READ) {
     387                 :          0 :                         msg[1].flags |= I2C_M_RECV_LEN;
     388                 :          0 :                         msg[1].len = 1; /* block length will be added by
     389                 :            :                                            the underlying bus driver */
     390                 :          0 :                         i2c_smbus_try_get_dmabuf(&msg[1], 0);
     391                 :            :                 } else {
     392                 :          0 :                         msg[0].len = data->block[0] + 2;
     393                 :          0 :                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
     394                 :          0 :                                 dev_err(&adapter->dev,
     395                 :            :                                         "Invalid block write size %d\n",
     396                 :            :                                         data->block[0]);
     397                 :          0 :                                 return -EINVAL;
     398                 :            :                         }
     399                 :            : 
     400                 :          0 :                         i2c_smbus_try_get_dmabuf(&msg[0], command);
     401                 :          0 :                         for (i = 1; i < msg[0].len; i++)
     402                 :          0 :                                 msg[0].buf[i] = data->block[i - 1];
     403                 :            :                 }
     404                 :            :                 break;
     405                 :            :         case I2C_SMBUS_BLOCK_PROC_CALL:
     406                 :            :                 num = 2; /* Another special case */
     407                 :            :                 read_write = I2C_SMBUS_READ;
     408                 :          0 :                 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
     409                 :          0 :                         dev_err(&adapter->dev,
     410                 :            :                                 "Invalid block write size %d\n",
     411                 :            :                                 data->block[0]);
     412                 :          0 :                         return -EINVAL;
     413                 :            :                 }
     414                 :            : 
     415                 :          0 :                 msg[0].len = data->block[0] + 2;
     416                 :          0 :                 i2c_smbus_try_get_dmabuf(&msg[0], command);
     417                 :          0 :                 for (i = 1; i < msg[0].len; i++)
     418                 :          0 :                         msg[0].buf[i] = data->block[i - 1];
     419                 :            : 
     420                 :          0 :                 msg[1].flags |= I2C_M_RECV_LEN;
     421                 :          0 :                 msg[1].len = 1; /* block length will be added by
     422                 :            :                                    the underlying bus driver */
     423                 :          0 :                 i2c_smbus_try_get_dmabuf(&msg[1], 0);
     424                 :          0 :                 break;
     425                 :            :         case I2C_SMBUS_I2C_BLOCK_DATA:
     426                 :          0 :                 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
     427                 :          0 :                         dev_err(&adapter->dev, "Invalid block %s size %d\n",
     428                 :            :                                 read_write == I2C_SMBUS_READ ? "read" : "write",
     429                 :            :                                 data->block[0]);
     430                 :          0 :                         return -EINVAL;
     431                 :            :                 }
     432                 :            : 
     433                 :          0 :                 if (read_write == I2C_SMBUS_READ) {
     434                 :          0 :                         msg[1].len = data->block[0];
     435                 :          0 :                         i2c_smbus_try_get_dmabuf(&msg[1], 0);
     436                 :            :                 } else {
     437                 :          0 :                         msg[0].len = data->block[0] + 1;
     438                 :            : 
     439                 :          0 :                         i2c_smbus_try_get_dmabuf(&msg[0], command);
     440                 :          0 :                         for (i = 1; i <= data->block[0]; i++)
     441                 :          0 :                                 msg[0].buf[i] = data->block[i];
     442                 :            :                 }
     443                 :            :                 break;
     444                 :            :         default:
     445                 :          0 :                 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
     446                 :          0 :                 return -EOPNOTSUPP;
     447                 :            :         }
     448                 :            : 
     449                 :          0 :         i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
     450                 :          0 :                                       && size != I2C_SMBUS_I2C_BLOCK_DATA);
     451                 :          0 :         if (i) {
     452                 :            :                 /* Compute PEC if first message is a write */
     453                 :          0 :                 if (!(msg[0].flags & I2C_M_RD)) {
     454                 :          0 :                         if (num == 1) /* Write only */
     455                 :          0 :                                 i2c_smbus_add_pec(&msg[0]);
     456                 :            :                         else /* Write followed by read */
     457                 :          0 :                                 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
     458                 :            :                 }
     459                 :            :                 /* Ask for PEC if last message is a read */
     460                 :          0 :                 if (msg[num-1].flags & I2C_M_RD)
     461                 :          0 :                         msg[num-1].len++;
     462                 :            :         }
     463                 :            : 
     464                 :          0 :         status = __i2c_transfer(adapter, msg, num);
     465                 :          0 :         if (status < 0)
     466                 :            :                 goto cleanup;
     467                 :          0 :         if (status != num) {
     468                 :            :                 status = -EIO;
     469                 :            :                 goto cleanup;
     470                 :            :         }
     471                 :            :         status = 0;
     472                 :            : 
     473                 :            :         /* Check PEC if last message is a read */
     474                 :          0 :         if (i && (msg[num-1].flags & I2C_M_RD)) {
     475                 :          0 :                 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
     476                 :          0 :                 if (status < 0)
     477                 :            :                         goto cleanup;
     478                 :            :         }
     479                 :            : 
     480                 :          0 :         if (read_write == I2C_SMBUS_READ)
     481                 :          0 :                 switch (size) {
     482                 :            :                 case I2C_SMBUS_BYTE:
     483                 :          0 :                         data->byte = msgbuf0[0];
     484                 :          0 :                         break;
     485                 :            :                 case I2C_SMBUS_BYTE_DATA:
     486                 :          0 :                         data->byte = msgbuf1[0];
     487                 :          0 :                         break;
     488                 :            :                 case I2C_SMBUS_WORD_DATA:
     489                 :            :                 case I2C_SMBUS_PROC_CALL:
     490                 :          0 :                         data->word = msgbuf1[0] | (msgbuf1[1] << 8);
     491                 :          0 :                         break;
     492                 :            :                 case I2C_SMBUS_I2C_BLOCK_DATA:
     493                 :          0 :                         for (i = 0; i < data->block[0]; i++)
     494                 :          0 :                                 data->block[i + 1] = msg[1].buf[i];
     495                 :            :                         break;
     496                 :            :                 case I2C_SMBUS_BLOCK_DATA:
     497                 :            :                 case I2C_SMBUS_BLOCK_PROC_CALL:
     498                 :          0 :                         if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
     499                 :          0 :                                 dev_err(&adapter->dev,
     500                 :            :                                         "Invalid block size returned: %d\n",
     501                 :            :                                         msg[1].buf[0]);
     502                 :            :                                 status = -EPROTO;
     503                 :          0 :                                 goto cleanup;
     504                 :            :                         }
     505                 :          0 :                         for (i = 0; i < msg[1].buf[0] + 1; i++)
     506                 :          0 :                                 data->block[i] = msg[1].buf[i];
     507                 :            :                         break;
     508                 :            :                 }
     509                 :            : 
     510                 :            : cleanup:
     511                 :          0 :         if (msg[0].flags & I2C_M_DMA_SAFE)
     512                 :          0 :                 kfree(msg[0].buf);
     513                 :          0 :         if (msg[1].flags & I2C_M_DMA_SAFE)
     514                 :          0 :                 kfree(msg[1].buf);
     515                 :            : 
     516                 :          0 :         return status;
     517                 :            : }
     518                 :            : 
     519                 :            : /**
     520                 :            :  * i2c_smbus_xfer - execute SMBus protocol operations
     521                 :            :  * @adapter: Handle to I2C bus
     522                 :            :  * @addr: Address of SMBus slave on that bus
     523                 :            :  * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
     524                 :            :  * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
     525                 :            :  * @command: Byte interpreted by slave, for protocols which use such bytes
     526                 :            :  * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
     527                 :            :  * @data: Data to be read or written
     528                 :            :  *
     529                 :            :  * This executes an SMBus protocol operation, and returns a negative
     530                 :            :  * errno code else zero on success.
     531                 :            :  */
     532                 :          0 : s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
     533                 :            :                    unsigned short flags, char read_write,
     534                 :            :                    u8 command, int protocol, union i2c_smbus_data *data)
     535                 :            : {
     536                 :            :         s32 res;
     537                 :            : 
     538                 :          0 :         res = __i2c_lock_bus_helper(adapter);
     539                 :          0 :         if (res)
     540                 :            :                 return res;
     541                 :            : 
     542                 :          0 :         res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
     543                 :            :                                command, protocol, data);
     544                 :            :         i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
     545                 :            : 
     546                 :          0 :         return res;
     547                 :            : }
     548                 :            : EXPORT_SYMBOL(i2c_smbus_xfer);
     549                 :            : 
     550                 :          0 : s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
     551                 :            :                      unsigned short flags, char read_write,
     552                 :            :                      u8 command, int protocol, union i2c_smbus_data *data)
     553                 :            : {
     554                 :            :         int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
     555                 :            :                          unsigned short flags, char read_write,
     556                 :            :                          u8 command, int size, union i2c_smbus_data *data);
     557                 :            :         unsigned long orig_jiffies;
     558                 :            :         int try;
     559                 :            :         s32 res;
     560                 :            : 
     561                 :          0 :         res = __i2c_check_suspended(adapter);
     562                 :          0 :         if (res)
     563                 :            :                 return res;
     564                 :            : 
     565                 :            :         /* If enabled, the following two tracepoints are conditional on
     566                 :            :          * read_write and protocol.
     567                 :            :          */
     568                 :          0 :         trace_smbus_write(adapter, addr, flags, read_write,
     569                 :            :                           command, protocol, data);
     570                 :          0 :         trace_smbus_read(adapter, addr, flags, read_write,
     571                 :            :                          command, protocol);
     572                 :            : 
     573                 :          0 :         flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
     574                 :            : 
     575                 :          0 :         xfer_func = adapter->algo->smbus_xfer;
     576                 :          0 :         if (i2c_in_atomic_xfer_mode()) {
     577                 :          0 :                 if (adapter->algo->smbus_xfer_atomic)
     578                 :            :                         xfer_func = adapter->algo->smbus_xfer_atomic;
     579                 :          0 :                 else if (adapter->algo->master_xfer_atomic)
     580                 :            :                         xfer_func = NULL; /* fallback to I2C emulation */
     581                 :            :         }
     582                 :            : 
     583                 :          0 :         if (xfer_func) {
     584                 :            :                 /* Retry automatically on arbitration loss */
     585                 :          0 :                 orig_jiffies = jiffies;
     586                 :          0 :                 for (res = 0, try = 0; try <= adapter->retries; try++) {
     587                 :          0 :                         res = xfer_func(adapter, addr, flags, read_write,
     588                 :            :                                         command, protocol, data);
     589                 :          0 :                         if (res != -EAGAIN)
     590                 :            :                                 break;
     591                 :          0 :                         if (time_after(jiffies,
     592                 :            :                                        orig_jiffies + adapter->timeout))
     593                 :            :                                 break;
     594                 :            :                 }
     595                 :            : 
     596                 :          0 :                 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
     597                 :            :                         goto trace;
     598                 :            :                 /*
     599                 :            :                  * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
     600                 :            :                  * implement native support for the SMBus operation.
     601                 :            :                  */
     602                 :            :         }
     603                 :            : 
     604                 :          0 :         res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
     605                 :            :                                       command, protocol, data);
     606                 :            : 
     607                 :            : trace:
     608                 :            :         /* If enabled, the reply tracepoint is conditional on read_write. */
     609                 :          0 :         trace_smbus_reply(adapter, addr, flags, read_write,
     610                 :            :                           command, protocol, data, res);
     611                 :          0 :         trace_smbus_result(adapter, addr, flags, read_write,
     612                 :            :                            command, protocol, res);
     613                 :            : 
     614                 :          0 :         return res;
     615                 :            : }
     616                 :            : EXPORT_SYMBOL(__i2c_smbus_xfer);
     617                 :            : 
     618                 :            : /**
     619                 :            :  * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
     620                 :            :  * @client: Handle to slave device
     621                 :            :  * @command: Byte interpreted by slave
     622                 :            :  * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
     623                 :            :  * @values: Byte array into which data will be read; big enough to hold
     624                 :            :  *      the data returned by the slave.  SMBus allows at most
     625                 :            :  *      I2C_SMBUS_BLOCK_MAX bytes.
     626                 :            :  *
     627                 :            :  * This executes the SMBus "block read" protocol if supported by the adapter.
     628                 :            :  * If block read is not supported, it emulates it using either word or byte
     629                 :            :  * read protocols depending on availability.
     630                 :            :  *
     631                 :            :  * The addresses of the I2C slave device that are accessed with this function
     632                 :            :  * must be mapped to a linear region, so that a block read will have the same
     633                 :            :  * effect as a byte read. Before using this function you must double-check
     634                 :            :  * if the I2C slave does support exchanging a block transfer with a byte
     635                 :            :  * transfer.
     636                 :            :  */
     637                 :          0 : s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
     638                 :            :                                               u8 command, u8 length, u8 *values)
     639                 :            : {
     640                 :            :         u8 i = 0;
     641                 :            :         int status;
     642                 :            : 
     643                 :          0 :         if (length > I2C_SMBUS_BLOCK_MAX)
     644                 :            :                 length = I2C_SMBUS_BLOCK_MAX;
     645                 :            : 
     646                 :          0 :         if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
     647                 :          0 :                 return i2c_smbus_read_i2c_block_data(client, command, length, values);
     648                 :            : 
     649                 :          0 :         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
     650                 :            :                 return -EOPNOTSUPP;
     651                 :            : 
     652                 :          0 :         if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
     653                 :          0 :                 while ((i + 2) <= length) {
     654                 :          0 :                         status = i2c_smbus_read_word_data(client, command + i);
     655                 :          0 :                         if (status < 0)
     656                 :          0 :                                 return status;
     657                 :          0 :                         values[i] = status & 0xff;
     658                 :          0 :                         values[i + 1] = status >> 8;
     659                 :          0 :                         i += 2;
     660                 :            :                 }
     661                 :            :         }
     662                 :            : 
     663                 :          0 :         while (i < length) {
     664                 :          0 :                 status = i2c_smbus_read_byte_data(client, command + i);
     665                 :          0 :                 if (status < 0)
     666                 :          0 :                         return status;
     667                 :          0 :                 values[i] = status;
     668                 :          0 :                 i++;
     669                 :            :         }
     670                 :            : 
     671                 :          0 :         return i;
     672                 :            : }
     673                 :            : EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
     674                 :            : 
     675                 :            : /**
     676                 :            :  * i2c_setup_smbus_alert - Setup SMBus alert support
     677                 :            :  * @adapter: the target adapter
     678                 :            :  * @setup: setup data for the SMBus alert handler
     679                 :            :  * Context: can sleep
     680                 :            :  *
     681                 :            :  * Setup handling of the SMBus alert protocol on a given I2C bus segment.
     682                 :            :  *
     683                 :            :  * Handling can be done either through our IRQ handler, or by the
     684                 :            :  * adapter (from its handler, periodic polling, or whatever).
     685                 :            :  *
     686                 :            :  * NOTE that if we manage the IRQ, we *MUST* know if it's level or
     687                 :            :  * edge triggered in order to hand it to the workqueue correctly.
     688                 :            :  * If triggering the alert seems to wedge the system, you probably
     689                 :            :  * should have said it's level triggered.
     690                 :            :  *
     691                 :            :  * This returns the ara client, which should be saved for later use with
     692                 :            :  * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL
     693                 :            :  * to indicate an error.
     694                 :            :  */
     695                 :          0 : struct i2c_client *i2c_setup_smbus_alert(struct i2c_adapter *adapter,
     696                 :            :                                          struct i2c_smbus_alert_setup *setup)
     697                 :            : {
     698                 :          0 :         struct i2c_board_info ara_board_info = {
     699                 :            :                 I2C_BOARD_INFO("smbus_alert", 0x0c),
     700                 :            :                 .platform_data = setup,
     701                 :            :         };
     702                 :            : 
     703                 :          0 :         return i2c_new_device(adapter, &ara_board_info);
     704                 :            : }
     705                 :            : EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert);
     706                 :            : 
     707                 :            : #if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_OF)
     708                 :            : int of_i2c_setup_smbus_alert(struct i2c_adapter *adapter)
     709                 :            : {
     710                 :            :         struct i2c_client *client;
     711                 :            :         int irq;
     712                 :            : 
     713                 :            :         irq = of_property_match_string(adapter->dev.of_node, "interrupt-names",
     714                 :            :                                        "smbus_alert");
     715                 :            :         if (irq == -EINVAL || irq == -ENODATA)
     716                 :            :                 return 0;
     717                 :            :         else if (irq < 0)
     718                 :            :                 return irq;
     719                 :            : 
     720                 :            :         client = i2c_setup_smbus_alert(adapter, NULL);
     721                 :            :         if (!client)
     722                 :            :                 return -ENODEV;
     723                 :            : 
     724                 :            :         return 0;
     725                 :            : }
     726                 :            : EXPORT_SYMBOL_GPL(of_i2c_setup_smbus_alert);
     727                 :            : #endif
    

Generated by: LCOV version 1.14