Commit c98bdf37 authored by Linus Walleij's avatar Linus Walleij Committed by Thadeu Lima de Souza Cascardo

iio: st_sensors: simplify buffer address handling

BugLink: https://bugs.launchpad.net/bugs/1676356

The driver goes to some length to dynamically allocate an array
to hold the channel addresses. However no ST sensor has more than
three channels (x, y, z at most). Instead of kmalloc():ing and
kfree():in the address array, just use a fixed array of three
elements.

Cc: Giuseppe Barba <giuseppe.barba@st.com>
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
Acked-by: default avatarDenis Ciocca <denis.ciocca@st.com>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
(cherry picked from commit e6e45420)
Signed-off-by: default avatarShrirang Bagul <shrirang.bagul@canonical.com>
Acked-by: default avatarStefan Bader <stefan.bader@canonical.com>
Acked-by: default avatarTim Gardner <tim.gardner@canonical.com>
Signed-off-by: default avatarThadeu Lima de Souza Cascardo <cascardo@canonical.com>
parent 275a231c
......@@ -24,7 +24,7 @@
int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
{
u8 *addr;
u8 addr[3]; /* no ST sensor has more than 3 channels */
int i, n = 0, len;
struct st_sensor_data *sdata = iio_priv(indio_dev);
unsigned int num_data_channels = sdata->num_data_channels;
......@@ -32,12 +32,6 @@ int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
DIV_ROUND_UP(indio_dev->channels[0].scan_type.realbits +
indio_dev->channels[0].scan_type.shift, 8);
addr = kmalloc(num_data_channels, GFP_KERNEL);
if (!addr) {
len = -ENOMEM;
goto st_sensors_get_buffer_element_error;
}
for (i = 0; i < num_data_channels; i++) {
if (test_bit(i, indio_dev->active_scan_mask)) {
addr[n] = indio_dev->channels[i].address;
......@@ -58,10 +52,8 @@ int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
u8 *rx_array;
rx_array = kmalloc(byte_for_channel * num_data_channels,
GFP_KERNEL);
if (!rx_array) {
len = -ENOMEM;
goto st_sensors_free_memory;
}
if (!rx_array)
return -ENOMEM;
len = sdata->tf->read_multiple_byte(&sdata->tb,
sdata->dev, addr[0],
......@@ -69,7 +61,7 @@ int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
rx_array, sdata->multiread_bit);
if (len < 0) {
kfree(rx_array);
goto st_sensors_free_memory;
return len;
}
for (i = 0; i < n * byte_for_channel; i++) {
......@@ -88,17 +80,11 @@ int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
buf, sdata->multiread_bit);
break;
default:
len = -EINVAL;
goto st_sensors_free_memory;
}
if (len != byte_for_channel * n) {
len = -EIO;
goto st_sensors_free_memory;
return -EINVAL;
}
if (len != byte_for_channel * n)
return -EIO;
st_sensors_free_memory:
kfree(addr);
st_sensors_get_buffer_element_error:
return len;
}
EXPORT_SYMBOL(st_sensors_get_buffer_element);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment