Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Linux I2C core SMBus and SMBus emulation code
0004  *
0005  * This file contains the SMBus functions which are always included in the I2C
0006  * core because they can be emulated via I2C. SMBus specific extensions
0007  * (e.g. smbalert) are handled in a separate i2c-smbus module.
0008  *
0009  * All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
0010  * SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
0011  * Jean Delvare <jdelvare@suse.de>
0012  */
0013 #include <linux/device.h>
0014 #include <linux/err.h>
0015 #include <linux/i2c.h>
0016 #include <linux/i2c-smbus.h>
0017 #include <linux/property.h>
0018 #include <linux/slab.h>
0019 
0020 #include "i2c-core.h"
0021 
0022 #define CREATE_TRACE_POINTS
0023 #include <trace/events/smbus.h>
0024 
0025 
0026 /* The SMBus parts */
0027 
0028 #define POLY    (0x1070U << 3)
0029 static u8 crc8(u16 data)
0030 {
0031     int i;
0032 
0033     for (i = 0; i < 8; i++) {
0034         if (data & 0x8000)
0035             data = data ^ POLY;
0036         data = data << 1;
0037     }
0038     return (u8)(data >> 8);
0039 }
0040 
0041 /**
0042  * i2c_smbus_pec - Incremental CRC8 over the given input data array
0043  * @crc: previous return crc8 value
0044  * @p: pointer to data buffer.
0045  * @count: number of bytes in data buffer.
0046  *
0047  * Incremental CRC8 over count bytes in the array pointed to by p
0048  */
0049 u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
0050 {
0051     int i;
0052 
0053     for (i = 0; i < count; i++)
0054         crc = crc8((crc ^ p[i]) << 8);
0055     return crc;
0056 }
0057 EXPORT_SYMBOL(i2c_smbus_pec);
0058 
0059 /* Assume a 7-bit address, which is reasonable for SMBus */
0060 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
0061 {
0062     /* The address will be sent first */
0063     u8 addr = i2c_8bit_addr_from_msg(msg);
0064     pec = i2c_smbus_pec(pec, &addr, 1);
0065 
0066     /* The data buffer follows */
0067     return i2c_smbus_pec(pec, msg->buf, msg->len);
0068 }
0069 
0070 /* Used for write only transactions */
0071 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
0072 {
0073     msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
0074     msg->len++;
0075 }
0076 
0077 /* Return <0 on CRC error
0078    If there was a write before this read (most cases) we need to take the
0079    partial CRC from the write part into account.
0080    Note that this function does modify the message (we need to decrease the
0081    message length to hide the CRC byte from the caller). */
0082 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
0083 {
0084     u8 rpec = msg->buf[--msg->len];
0085     cpec = i2c_smbus_msg_pec(cpec, msg);
0086 
0087     if (rpec != cpec) {
0088         pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
0089             rpec, cpec);
0090         return -EBADMSG;
0091     }
0092     return 0;
0093 }
0094 
0095 /**
0096  * i2c_smbus_read_byte - SMBus "receive byte" protocol
0097  * @client: Handle to slave device
0098  *
0099  * This executes the SMBus "receive byte" protocol, returning negative errno
0100  * else the byte received from the device.
0101  */
0102 s32 i2c_smbus_read_byte(const struct i2c_client *client)
0103 {
0104     union i2c_smbus_data data;
0105     int status;
0106 
0107     status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0108                 I2C_SMBUS_READ, 0,
0109                 I2C_SMBUS_BYTE, &data);
0110     return (status < 0) ? status : data.byte;
0111 }
0112 EXPORT_SYMBOL(i2c_smbus_read_byte);
0113 
0114 /**
0115  * i2c_smbus_write_byte - SMBus "send byte" protocol
0116  * @client: Handle to slave device
0117  * @value: Byte to be sent
0118  *
0119  * This executes the SMBus "send byte" protocol, returning negative errno
0120  * else zero on success.
0121  */
0122 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
0123 {
0124     return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0125                           I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
0126 }
0127 EXPORT_SYMBOL(i2c_smbus_write_byte);
0128 
0129 /**
0130  * i2c_smbus_read_byte_data - SMBus "read byte" protocol
0131  * @client: Handle to slave device
0132  * @command: Byte interpreted by slave
0133  *
0134  * This executes the SMBus "read byte" protocol, returning negative errno
0135  * else a data byte received from the device.
0136  */
0137 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
0138 {
0139     union i2c_smbus_data data;
0140     int status;
0141 
0142     status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0143                 I2C_SMBUS_READ, command,
0144                 I2C_SMBUS_BYTE_DATA, &data);
0145     return (status < 0) ? status : data.byte;
0146 }
0147 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
0148 
0149 /**
0150  * i2c_smbus_write_byte_data - SMBus "write byte" protocol
0151  * @client: Handle to slave device
0152  * @command: Byte interpreted by slave
0153  * @value: Byte being written
0154  *
0155  * This executes the SMBus "write byte" protocol, returning negative errno
0156  * else zero on success.
0157  */
0158 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
0159                   u8 value)
0160 {
0161     union i2c_smbus_data data;
0162     data.byte = value;
0163     return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0164                   I2C_SMBUS_WRITE, command,
0165                   I2C_SMBUS_BYTE_DATA, &data);
0166 }
0167 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
0168 
0169 /**
0170  * i2c_smbus_read_word_data - SMBus "read word" protocol
0171  * @client: Handle to slave device
0172  * @command: Byte interpreted by slave
0173  *
0174  * This executes the SMBus "read word" protocol, returning negative errno
0175  * else a 16-bit unsigned "word" received from the device.
0176  */
0177 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
0178 {
0179     union i2c_smbus_data data;
0180     int status;
0181 
0182     status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0183                 I2C_SMBUS_READ, command,
0184                 I2C_SMBUS_WORD_DATA, &data);
0185     return (status < 0) ? status : data.word;
0186 }
0187 EXPORT_SYMBOL(i2c_smbus_read_word_data);
0188 
0189 /**
0190  * i2c_smbus_write_word_data - SMBus "write word" protocol
0191  * @client: Handle to slave device
0192  * @command: Byte interpreted by slave
0193  * @value: 16-bit "word" being written
0194  *
0195  * This executes the SMBus "write word" protocol, returning negative errno
0196  * else zero on success.
0197  */
0198 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
0199                   u16 value)
0200 {
0201     union i2c_smbus_data data;
0202     data.word = value;
0203     return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0204                   I2C_SMBUS_WRITE, command,
0205                   I2C_SMBUS_WORD_DATA, &data);
0206 }
0207 EXPORT_SYMBOL(i2c_smbus_write_word_data);
0208 
0209 /**
0210  * i2c_smbus_read_block_data - SMBus "block read" protocol
0211  * @client: Handle to slave device
0212  * @command: Byte interpreted by slave
0213  * @values: Byte array into which data will be read; big enough to hold
0214  *  the data returned by the slave.  SMBus allows at most 32 bytes.
0215  *
0216  * This executes the SMBus "block read" protocol, returning negative errno
0217  * else the number of data bytes in the slave's response.
0218  *
0219  * Note that using this function requires that the client's adapter support
0220  * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality.  Not all adapter drivers
0221  * support this; its emulation through I2C messaging relies on a specific
0222  * mechanism (I2C_M_RECV_LEN) which may not be implemented.
0223  */
0224 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
0225                   u8 *values)
0226 {
0227     union i2c_smbus_data data;
0228     int status;
0229 
0230     status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0231                 I2C_SMBUS_READ, command,
0232                 I2C_SMBUS_BLOCK_DATA, &data);
0233     if (status)
0234         return status;
0235 
0236     memcpy(values, &data.block[1], data.block[0]);
0237     return data.block[0];
0238 }
0239 EXPORT_SYMBOL(i2c_smbus_read_block_data);
0240 
0241 /**
0242  * i2c_smbus_write_block_data - SMBus "block write" protocol
0243  * @client: Handle to slave device
0244  * @command: Byte interpreted by slave
0245  * @length: Size of data block; SMBus allows at most 32 bytes
0246  * @values: Byte array which will be written.
0247  *
0248  * This executes the SMBus "block write" protocol, returning negative errno
0249  * else zero on success.
0250  */
0251 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
0252                    u8 length, const u8 *values)
0253 {
0254     union i2c_smbus_data data;
0255 
0256     if (length > I2C_SMBUS_BLOCK_MAX)
0257         length = I2C_SMBUS_BLOCK_MAX;
0258     data.block[0] = length;
0259     memcpy(&data.block[1], values, length);
0260     return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0261                   I2C_SMBUS_WRITE, command,
0262                   I2C_SMBUS_BLOCK_DATA, &data);
0263 }
0264 EXPORT_SYMBOL(i2c_smbus_write_block_data);
0265 
0266 /* Returns the number of read bytes */
0267 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
0268                   u8 length, u8 *values)
0269 {
0270     union i2c_smbus_data data;
0271     int status;
0272 
0273     if (length > I2C_SMBUS_BLOCK_MAX)
0274         length = I2C_SMBUS_BLOCK_MAX;
0275     data.block[0] = length;
0276     status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0277                 I2C_SMBUS_READ, command,
0278                 I2C_SMBUS_I2C_BLOCK_DATA, &data);
0279     if (status < 0)
0280         return status;
0281 
0282     memcpy(values, &data.block[1], data.block[0]);
0283     return data.block[0];
0284 }
0285 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
0286 
0287 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
0288                    u8 length, const u8 *values)
0289 {
0290     union i2c_smbus_data data;
0291 
0292     if (length > I2C_SMBUS_BLOCK_MAX)
0293         length = I2C_SMBUS_BLOCK_MAX;
0294     data.block[0] = length;
0295     memcpy(data.block + 1, values, length);
0296     return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0297                   I2C_SMBUS_WRITE, command,
0298                   I2C_SMBUS_I2C_BLOCK_DATA, &data);
0299 }
0300 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
0301 
0302 static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val)
0303 {
0304     bool is_read = msg->flags & I2C_M_RD;
0305     unsigned char *dma_buf;
0306 
0307     dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL);
0308     if (!dma_buf)
0309         return;
0310 
0311     msg->buf = dma_buf;
0312     msg->flags |= I2C_M_DMA_SAFE;
0313 
0314     if (init_val)
0315         msg->buf[0] = init_val;
0316 }
0317 
0318 /*
0319  * Simulate a SMBus command using the I2C protocol.
0320  * No checking of parameters is done!
0321  */
0322 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
0323                    unsigned short flags,
0324                    char read_write, u8 command, int size,
0325                    union i2c_smbus_data *data)
0326 {
0327     /*
0328      * So we need to generate a series of msgs. In the case of writing, we
0329      * need to use only one message; when reading, we need two. We
0330      * initialize most things with sane defaults, to keep the code below
0331      * somewhat simpler.
0332      */
0333     unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
0334     unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
0335     int nmsgs = read_write == I2C_SMBUS_READ ? 2 : 1;
0336     u8 partial_pec = 0;
0337     int status;
0338     struct i2c_msg msg[2] = {
0339         {
0340             .addr = addr,
0341             .flags = flags,
0342             .len = 1,
0343             .buf = msgbuf0,
0344         }, {
0345             .addr = addr,
0346             .flags = flags | I2C_M_RD,
0347             .len = 0,
0348             .buf = msgbuf1,
0349         },
0350     };
0351     bool wants_pec = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
0352               && size != I2C_SMBUS_I2C_BLOCK_DATA);
0353 
0354     msgbuf0[0] = command;
0355     switch (size) {
0356     case I2C_SMBUS_QUICK:
0357         msg[0].len = 0;
0358         /* Special case: The read/write field is used as data */
0359         msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
0360                     I2C_M_RD : 0);
0361         nmsgs = 1;
0362         break;
0363     case I2C_SMBUS_BYTE:
0364         if (read_write == I2C_SMBUS_READ) {
0365             /* Special case: only a read! */
0366             msg[0].flags = I2C_M_RD | flags;
0367             nmsgs = 1;
0368         }
0369         break;
0370     case I2C_SMBUS_BYTE_DATA:
0371         if (read_write == I2C_SMBUS_READ)
0372             msg[1].len = 1;
0373         else {
0374             msg[0].len = 2;
0375             msgbuf0[1] = data->byte;
0376         }
0377         break;
0378     case I2C_SMBUS_WORD_DATA:
0379         if (read_write == I2C_SMBUS_READ)
0380             msg[1].len = 2;
0381         else {
0382             msg[0].len = 3;
0383             msgbuf0[1] = data->word & 0xff;
0384             msgbuf0[2] = data->word >> 8;
0385         }
0386         break;
0387     case I2C_SMBUS_PROC_CALL:
0388         nmsgs = 2; /* Special case */
0389         read_write = I2C_SMBUS_READ;
0390         msg[0].len = 3;
0391         msg[1].len = 2;
0392         msgbuf0[1] = data->word & 0xff;
0393         msgbuf0[2] = data->word >> 8;
0394         break;
0395     case I2C_SMBUS_BLOCK_DATA:
0396         if (read_write == I2C_SMBUS_READ) {
0397             msg[1].flags |= I2C_M_RECV_LEN;
0398             msg[1].len = 1; /* block length will be added by
0399                        the underlying bus driver */
0400             i2c_smbus_try_get_dmabuf(&msg[1], 0);
0401         } else {
0402             msg[0].len = data->block[0] + 2;
0403             if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
0404                 dev_err(&adapter->dev,
0405                     "Invalid block write size %d\n",
0406                     data->block[0]);
0407                 return -EINVAL;
0408             }
0409 
0410             i2c_smbus_try_get_dmabuf(&msg[0], command);
0411             memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
0412         }
0413         break;
0414     case I2C_SMBUS_BLOCK_PROC_CALL:
0415         nmsgs = 2; /* Another special case */
0416         read_write = I2C_SMBUS_READ;
0417         if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
0418             dev_err(&adapter->dev,
0419                 "Invalid block write size %d\n",
0420                 data->block[0]);
0421             return -EINVAL;
0422         }
0423 
0424         msg[0].len = data->block[0] + 2;
0425         i2c_smbus_try_get_dmabuf(&msg[0], command);
0426         memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
0427 
0428         msg[1].flags |= I2C_M_RECV_LEN;
0429         msg[1].len = 1; /* block length will be added by
0430                    the underlying bus driver */
0431         i2c_smbus_try_get_dmabuf(&msg[1], 0);
0432         break;
0433     case I2C_SMBUS_I2C_BLOCK_DATA:
0434         if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
0435             dev_err(&adapter->dev, "Invalid block %s size %d\n",
0436                 read_write == I2C_SMBUS_READ ? "read" : "write",
0437                 data->block[0]);
0438             return -EINVAL;
0439         }
0440 
0441         if (read_write == I2C_SMBUS_READ) {
0442             msg[1].len = data->block[0];
0443             i2c_smbus_try_get_dmabuf(&msg[1], 0);
0444         } else {
0445             msg[0].len = data->block[0] + 1;
0446 
0447             i2c_smbus_try_get_dmabuf(&msg[0], command);
0448             memcpy(msg[0].buf + 1, data->block + 1, data->block[0]);
0449         }
0450         break;
0451     default:
0452         dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
0453         return -EOPNOTSUPP;
0454     }
0455 
0456     if (wants_pec) {
0457         /* Compute PEC if first message is a write */
0458         if (!(msg[0].flags & I2C_M_RD)) {
0459             if (nmsgs == 1) /* Write only */
0460                 i2c_smbus_add_pec(&msg[0]);
0461             else /* Write followed by read */
0462                 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
0463         }
0464         /* Ask for PEC if last message is a read */
0465         if (msg[nmsgs - 1].flags & I2C_M_RD)
0466             msg[nmsgs - 1].len++;
0467     }
0468 
0469     status = __i2c_transfer(adapter, msg, nmsgs);
0470     if (status < 0)
0471         goto cleanup;
0472     if (status != nmsgs) {
0473         status = -EIO;
0474         goto cleanup;
0475     }
0476     status = 0;
0477 
0478     /* Check PEC if last message is a read */
0479     if (wants_pec && (msg[nmsgs - 1].flags & I2C_M_RD)) {
0480         status = i2c_smbus_check_pec(partial_pec, &msg[nmsgs - 1]);
0481         if (status < 0)
0482             goto cleanup;
0483     }
0484 
0485     if (read_write == I2C_SMBUS_READ)
0486         switch (size) {
0487         case I2C_SMBUS_BYTE:
0488             data->byte = msgbuf0[0];
0489             break;
0490         case I2C_SMBUS_BYTE_DATA:
0491             data->byte = msgbuf1[0];
0492             break;
0493         case I2C_SMBUS_WORD_DATA:
0494         case I2C_SMBUS_PROC_CALL:
0495             data->word = msgbuf1[0] | (msgbuf1[1] << 8);
0496             break;
0497         case I2C_SMBUS_I2C_BLOCK_DATA:
0498             memcpy(data->block + 1, msg[1].buf, data->block[0]);
0499             break;
0500         case I2C_SMBUS_BLOCK_DATA:
0501         case I2C_SMBUS_BLOCK_PROC_CALL:
0502             if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
0503                 dev_err(&adapter->dev,
0504                     "Invalid block size returned: %d\n",
0505                     msg[1].buf[0]);
0506                 status = -EPROTO;
0507                 goto cleanup;
0508             }
0509             memcpy(data->block, msg[1].buf, msg[1].buf[0] + 1);
0510             break;
0511         }
0512 
0513 cleanup:
0514     if (msg[0].flags & I2C_M_DMA_SAFE)
0515         kfree(msg[0].buf);
0516     if (msg[1].flags & I2C_M_DMA_SAFE)
0517         kfree(msg[1].buf);
0518 
0519     return status;
0520 }
0521 
0522 /**
0523  * i2c_smbus_xfer - execute SMBus protocol operations
0524  * @adapter: Handle to I2C bus
0525  * @addr: Address of SMBus slave on that bus
0526  * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
0527  * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
0528  * @command: Byte interpreted by slave, for protocols which use such bytes
0529  * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
0530  * @data: Data to be read or written
0531  *
0532  * This executes an SMBus protocol operation, and returns a negative
0533  * errno code else zero on success.
0534  */
0535 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
0536            unsigned short flags, char read_write,
0537            u8 command, int protocol, union i2c_smbus_data *data)
0538 {
0539     s32 res;
0540 
0541     res = __i2c_lock_bus_helper(adapter);
0542     if (res)
0543         return res;
0544 
0545     res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
0546                    command, protocol, data);
0547     i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
0548 
0549     return res;
0550 }
0551 EXPORT_SYMBOL(i2c_smbus_xfer);
0552 
0553 s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
0554              unsigned short flags, char read_write,
0555              u8 command, int protocol, union i2c_smbus_data *data)
0556 {
0557     int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
0558              unsigned short flags, char read_write,
0559              u8 command, int size, union i2c_smbus_data *data);
0560     unsigned long orig_jiffies;
0561     int try;
0562     s32 res;
0563 
0564     res = __i2c_check_suspended(adapter);
0565     if (res)
0566         return res;
0567 
0568     /* If enabled, the following two tracepoints are conditional on
0569      * read_write and protocol.
0570      */
0571     trace_smbus_write(adapter, addr, flags, read_write,
0572               command, protocol, data);
0573     trace_smbus_read(adapter, addr, flags, read_write,
0574              command, protocol);
0575 
0576     flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
0577 
0578     xfer_func = adapter->algo->smbus_xfer;
0579     if (i2c_in_atomic_xfer_mode()) {
0580         if (adapter->algo->smbus_xfer_atomic)
0581             xfer_func = adapter->algo->smbus_xfer_atomic;
0582         else if (adapter->algo->master_xfer_atomic)
0583             xfer_func = NULL; /* fallback to I2C emulation */
0584     }
0585 
0586     if (xfer_func) {
0587         /* Retry automatically on arbitration loss */
0588         orig_jiffies = jiffies;
0589         for (res = 0, try = 0; try <= adapter->retries; try++) {
0590             res = xfer_func(adapter, addr, flags, read_write,
0591                     command, protocol, data);
0592             if (res != -EAGAIN)
0593                 break;
0594             if (time_after(jiffies,
0595                        orig_jiffies + adapter->timeout))
0596                 break;
0597         }
0598 
0599         if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
0600             goto trace;
0601         /*
0602          * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
0603          * implement native support for the SMBus operation.
0604          */
0605     }
0606 
0607     res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
0608                       command, protocol, data);
0609 
0610 trace:
0611     /* If enabled, the reply tracepoint is conditional on read_write. */
0612     trace_smbus_reply(adapter, addr, flags, read_write,
0613               command, protocol, data, res);
0614     trace_smbus_result(adapter, addr, flags, read_write,
0615                command, protocol, res);
0616 
0617     return res;
0618 }
0619 EXPORT_SYMBOL(__i2c_smbus_xfer);
0620 
0621 /**
0622  * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
0623  * @client: Handle to slave device
0624  * @command: Byte interpreted by slave
0625  * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
0626  * @values: Byte array into which data will be read; big enough to hold
0627  *  the data returned by the slave.  SMBus allows at most
0628  *  I2C_SMBUS_BLOCK_MAX bytes.
0629  *
0630  * This executes the SMBus "block read" protocol if supported by the adapter.
0631  * If block read is not supported, it emulates it using either word or byte
0632  * read protocols depending on availability.
0633  *
0634  * The addresses of the I2C slave device that are accessed with this function
0635  * must be mapped to a linear region, so that a block read will have the same
0636  * effect as a byte read. Before using this function you must double-check
0637  * if the I2C slave does support exchanging a block transfer with a byte
0638  * transfer.
0639  */
0640 s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
0641                           u8 command, u8 length, u8 *values)
0642 {
0643     u8 i = 0;
0644     int status;
0645 
0646     if (length > I2C_SMBUS_BLOCK_MAX)
0647         length = I2C_SMBUS_BLOCK_MAX;
0648 
0649     if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
0650         return i2c_smbus_read_i2c_block_data(client, command, length, values);
0651 
0652     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
0653         return -EOPNOTSUPP;
0654 
0655     if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
0656         while ((i + 2) <= length) {
0657             status = i2c_smbus_read_word_data(client, command + i);
0658             if (status < 0)
0659                 return status;
0660             values[i] = status & 0xff;
0661             values[i + 1] = status >> 8;
0662             i += 2;
0663         }
0664     }
0665 
0666     while (i < length) {
0667         status = i2c_smbus_read_byte_data(client, command + i);
0668         if (status < 0)
0669             return status;
0670         values[i] = status;
0671         i++;
0672     }
0673 
0674     return i;
0675 }
0676 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
0677 
0678 /**
0679  * i2c_new_smbus_alert_device - get ara client for SMBus alert support
0680  * @adapter: the target adapter
0681  * @setup: setup data for the SMBus alert handler
0682  * Context: can sleep
0683  *
0684  * Setup handling of the SMBus alert protocol on a given I2C bus segment.
0685  *
0686  * Handling can be done either through our IRQ handler, or by the
0687  * adapter (from its handler, periodic polling, or whatever).
0688  *
0689  * This returns the ara client, which should be saved for later use with
0690  * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or an
0691  * ERRPTR to indicate an error.
0692  */
0693 struct i2c_client *i2c_new_smbus_alert_device(struct i2c_adapter *adapter,
0694                           struct i2c_smbus_alert_setup *setup)
0695 {
0696     struct i2c_board_info ara_board_info = {
0697         I2C_BOARD_INFO("smbus_alert", 0x0c),
0698         .platform_data = setup,
0699     };
0700 
0701     return i2c_new_client_device(adapter, &ara_board_info);
0702 }
0703 EXPORT_SYMBOL_GPL(i2c_new_smbus_alert_device);
0704 
0705 #if IS_ENABLED(CONFIG_I2C_SMBUS)
0706 int i2c_setup_smbus_alert(struct i2c_adapter *adapter)
0707 {
0708     struct device *parent = adapter->dev.parent;
0709     int irq;
0710 
0711     /* Adapter instantiated without parent, skip the SMBus alert setup */
0712     if (!parent)
0713         return 0;
0714 
0715     irq = device_property_match_string(parent, "interrupt-names", "smbus_alert");
0716     if (irq == -EINVAL || irq == -ENODATA)
0717         return 0;
0718     else if (irq < 0)
0719         return irq;
0720 
0721     return PTR_ERR_OR_ZERO(i2c_new_smbus_alert_device(adapter, NULL));
0722 }
0723 #endif