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