Skip to content

Commit 95ec3fd

Browse files
Jonathan CameronJonathan Cameron
authored andcommitted
iio: core: Introduce iio_push_to_buffers_with_ts_unaligned()
Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places where leading data needs to be skipped over. For these cases introduce a function that will allocate an appropriate sized and aligned bounce buffer (if not already allocated) and copy the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur. Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Nuno Sá <nuno.sa@analog.com> Link: https://lore.kernel.org/r/20210613151039.569883-2-jic23@kernel.org
1 parent b18831c commit 95ec3fd

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎drivers/iio/industrialio-buffer.c‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,52 @@ int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
17311731
}
17321732
EXPORT_SYMBOL_GPL(iio_push_to_buffers);
17331733

1734+
/**
1735+
* iio_push_to_buffers_with_ts_unaligned() - push to registered buffer,
1736+
* no alignment or space requirements.
1737+
* @indio_dev: iio_dev structure for device.
1738+
* @data: channel data excluding the timestamp.
1739+
* @data_sz: size of data.
1740+
* @timestamp: timestamp for the sample data.
1741+
*
1742+
* This special variant of iio_push_to_buffers_with_timestamp() does
1743+
* not require space for the timestamp, or 8 byte alignment of data.
1744+
* It does however require an allocation on first call and additional
1745+
* copies on all calls, so should be avoided if possible.
1746+
*/
1747+
int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
1748+
const void *data,
1749+
size_t data_sz,
1750+
int64_t timestamp)
1751+
{
1752+
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1753+
1754+
/*
1755+
* Conservative estimate - we can always safely copy the minimum
1756+
* of either the data provided or the length of the destination buffer.
1757+
* This relaxed limit allows the calling drivers to be lax about
1758+
* tracking the size of the data they are pushing, at the cost of
1759+
* unnecessary copying of padding.
1760+
*/
1761+
data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);
1762+
if (iio_dev_opaque->bounce_buffer_size != indio_dev->scan_bytes) {
1763+
void *bb;
1764+
1765+
bb = devm_krealloc(&indio_dev->dev,
1766+
iio_dev_opaque->bounce_buffer,
1767+
indio_dev->scan_bytes, GFP_KERNEL);
1768+
if (!bb)
1769+
return -ENOMEM;
1770+
iio_dev_opaque->bounce_buffer = bb;
1771+
iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;
1772+
}
1773+
memcpy(iio_dev_opaque->bounce_buffer, data, data_sz);
1774+
return iio_push_to_buffers_with_timestamp(indio_dev,
1775+
iio_dev_opaque->bounce_buffer,
1776+
timestamp);
1777+
}
1778+
EXPORT_SYMBOL_GPL(iio_push_to_buffers_with_ts_unaligned);
1779+
17341780
/**
17351781
* iio_buffer_release() - Free a buffer's resources
17361782
* @ref: Pointer to the kref embedded in the iio_buffer struct

‎include/linux/iio/buffer.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
3838
return iio_push_to_buffers(indio_dev, data);
3939
}
4040

41+
int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
42+
const void *data, size_t data_sz,
43+
int64_t timestamp);
44+
4145
bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
4246
const unsigned long *mask);
4347

‎include/linux/iio/iio-opaque.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* @groupcounter: index of next attribute group
2424
* @legacy_scan_el_group: attribute group for legacy scan elements attribute group
2525
* @legacy_buffer_group: attribute group for legacy buffer attributes group
26+
* @bounce_buffer: for devices that call iio_push_to_buffers_with_timestamp_unaligned()
27+
* @bounce_buffer_size: size of currently allocate bounce buffer
2628
* @scan_index_timestamp: cache of the index to the timestamp
2729
* @clock_id: timestamping clock posix identifier
2830
* @chrdev: associated character device
@@ -50,6 +52,8 @@ struct iio_dev_opaque {
5052
int groupcounter;
5153
struct attribute_group legacy_scan_el_group;
5254
struct attribute_group legacy_buffer_group;
55+
void *bounce_buffer;
56+
size_t bounce_buffer_size;
5357

5458
unsigned int scan_index_timestamp;
5559
clockid_t clock_id;

0 commit comments

Comments
 (0)