Skip to content

Commit 82fe39a

Browse files
Wolfram SangWolfram Sang
authored andcommitted
i2c: refactor function to release a DMA safe buffer
a) rename to 'put' instead of 'release' to match 'get' when obtaining the buffer b) change the argument order to have the buffer as first argument c) add a new argument telling the function if the message was transferred. This allows the function to be used also in cases where setting up DMA failed, so the buffer needs to be freed without syncing to the message buffer. Also convert the only user. Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com> Reviewed-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se> Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
1 parent 1204d12 commit 82fe39a

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

Documentation/i2c/DMA-considerations

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ bounce buffer. But you don't need to care about that detail, just use the
5050
returned buffer. If NULL is returned, the threshold was not met or a bounce
5151
buffer could not be allocated. Fall back to PIO in that case.
5252

53-
In any case, a buffer obtained from above needs to be released. It ensures data
54-
is copied back to the message and a potentially used bounce buffer is freed::
53+
In any case, a buffer obtained from above needs to be released. Another helper
54+
function ensures a potentially used bounce buffer is freed::
5555

56-
i2c_release_dma_safe_msg_buf(msg, dma_buf);
56+
i2c_put_dma_safe_msg_buf(dma_buf, msg, xferred);
57+
58+
The last argument 'xferred' controls if the buffer is synced back to the
59+
message or not. No syncing is needed in cases setting up DMA had an error and
60+
there was no data transferred.
5761

5862
The bounce buffer handling from the core is generic and simple. It will always
5963
allocate a new bounce buffer. If you want a more sophisticated handling (e.g.

drivers/i2c/busses/i2c-sh_mobile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ static void sh_mobile_i2c_dma_callback(void *data)
507507
pd->pos = pd->msg->len;
508508
pd->stop_after_dma = true;
509509

510-
i2c_release_dma_safe_msg_buf(pd->msg, pd->dma_buf);
510+
i2c_put_dma_safe_msg_buf(pd->dma_buf, pd->msg, true);
511511

512512
iic_set_clr(pd, ICIC, 0, ICIC_TDMAE | ICIC_RDMAE);
513513
}

drivers/i2c/i2c-core-base.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,21 +2293,22 @@ u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
22932293
EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
22942294

22952295
/**
2296-
* i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
2297-
* @msg: the message to be synced with
2296+
* i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
22982297
* @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
2298+
* @msg: the message which the buffer corresponds to
2299+
* @xferred: bool saying if the message was transferred
22992300
*/
2300-
void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)
2301+
void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred)
23012302
{
23022303
if (!buf || buf == msg->buf)
23032304
return;
23042305

2305-
if (msg->flags & I2C_M_RD)
2306+
if (xferred && msg->flags & I2C_M_RD)
23062307
memcpy(msg->buf, buf, msg->len);
23072308

23082309
kfree(buf);
23092310
}
2310-
EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);
2311+
EXPORT_SYMBOL_GPL(i2c_put_dma_safe_msg_buf);
23112312

23122313
MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
23132314
MODULE_DESCRIPTION("I2C-Bus main module");

include/linux/i2c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
855855
}
856856

857857
u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold);
858-
void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf);
858+
void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred);
859859

860860
int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
861861
/**

0 commit comments

Comments
 (0)