From: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Marcin Niestroj <m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org>
Cc: Peter Meerwald-Stadler
<pmeerw-jW+XmwGofnusTnJN9+BGXg@public.gmane.org>,
Hartmut Knaack <knaack.h-Mmb7MZpHnFY@public.gmane.org>,
Lars-Peter Clausen <lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>,
Daniel Baluta
<daniel.baluta-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>,
Gregor Boirie
<gregor.boirie-ITF29qwbsa/QT0dZR+AlfA@public.gmane.org>,
Sanchayan Maity
<maitysanchayan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
Rob Herring <robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
Mark Rutland <mark.rutland-5wv7dgnIgG8@public.gmane.org>,
linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH v2 4/6] iio: bmi160: Protect data transmission with mutex
Date: Fri, 30 Dec 2016 10:47:31 +0000 [thread overview]
Message-ID: <38885189-27b9-4420-1c28-dfd33eb5ac1f@kernel.org> (raw)
In-Reply-To: <20161208142259.26230-5-m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org>
On 08/12/16 14:22, Marcin Niestroj wrote:
> There are currently two possible cases for data transmission with the
> sensor: read/write by iio sysfs and read in trigger interrupt
> handler. Hence we need to protect data transmission using regmap_*
> functions with mutex.
Except that the regmap functions themselves have build in protection against
any such clashes. Hence I don't 'think' this is necessary as we don't
have any open coded read / update loops in here that I can see.
Jonathan
>
> Signed-off-by: Marcin Niestroj <m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org>
> ---
> Patch introduced in v2
>
> drivers/iio/imu/bmi160/bmi160_core.c | 39 +++++++++++++++++++++++++++++-------
> 1 file changed, 32 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c
> index e0251b8..095533c 100644
> --- a/drivers/iio/imu/bmi160/bmi160_core.c
> +++ b/drivers/iio/imu/bmi160/bmi160_core.c
> @@ -2,6 +2,7 @@
> * BMI160 - Bosch IMU (accel, gyro plus external magnetometer)
> *
> * Copyright (c) 2016, Intel Corporation.
> + * Copyright (c) 2016, Grinn
> *
> * This file is subject to the terms and conditions of version 2 of
> * the GNU General Public License. See the file COPYING in the main
> @@ -112,6 +113,7 @@ enum bmi160_sensor_type {
>
> struct bmi160_data {
> struct regmap *regmap;
> + struct mutex mutex;
> };
>
> const struct regmap_config bmi160_regmap_config = {
> @@ -285,7 +287,9 @@ int bmi160_set_mode(struct bmi160_data *data, enum bmi160_sensor_type t,
> else
> cmd = bmi160_regs[t].pmu_cmd_suspend;
>
> + mutex_lock(&data->mutex);
> ret = regmap_write(data->regmap, BMI160_REG_CMD, cmd);
> + mutex_unlock(&data->mutex);
> if (ret < 0)
> return ret;
>
> @@ -298,6 +302,7 @@ static
> int bmi160_set_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
> int uscale)
> {
> + int ret;
> int i;
>
> for (i = 0; i < bmi160_scale_table[t].num; i++)
> @@ -307,8 +312,12 @@ int bmi160_set_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
> if (i == bmi160_scale_table[t].num)
> return -EINVAL;
>
> - return regmap_write(data->regmap, bmi160_regs[t].range,
> - bmi160_scale_table[t].tbl[i].bits);
> + mutex_lock(&data->mutex);
> + ret = regmap_write(data->regmap, bmi160_regs[t].range,
> + bmi160_scale_table[t].tbl[i].bits);
> + mutex_unlock(&data->mutex);
> +
> + return ret;
> }
>
> static
> @@ -317,7 +326,9 @@ int bmi160_get_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
> {
> int i, ret, val;
>
> + mutex_lock(&data->mutex);
> ret = regmap_read(data->regmap, bmi160_regs[t].range, &val);
> + mutex_unlock(&data->mutex);
> if (ret < 0)
> return ret;
>
> @@ -340,7 +351,9 @@ static int bmi160_get_data(struct bmi160_data *data, int chan_type,
>
> reg = bmi160_regs[t].data + (axis - IIO_MOD_X) * sizeof(__le16);
>
> + mutex_lock(&data->mutex);
> ret = regmap_bulk_read(data->regmap, reg, &sample, sizeof(__le16));
> + mutex_unlock(&data->mutex);
> if (ret < 0)
> return ret;
>
> @@ -353,6 +366,7 @@ static
> int bmi160_set_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
> int odr, int uodr)
> {
> + int ret;
> int i;
>
> for (i = 0; i < bmi160_odr_table[t].num; i++)
> @@ -363,10 +377,14 @@ int bmi160_set_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
> if (i >= bmi160_odr_table[t].num)
> return -EINVAL;
>
> - return regmap_update_bits(data->regmap,
> - bmi160_regs[t].config,
> - bmi160_regs[t].config_odr_mask,
> - bmi160_odr_table[t].tbl[i].bits);
> + mutex_lock(&data->mutex);
> + ret = regmap_update_bits(data->regmap,
> + bmi160_regs[t].config,
> + bmi160_regs[t].config_odr_mask,
> + bmi160_odr_table[t].tbl[i].bits);
> + mutex_unlock(&data->mutex);
> +
> + return ret;
> }
>
> static int bmi160_get_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
> @@ -374,7 +392,9 @@ static int bmi160_get_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
> {
> int i, val, ret;
>
> + mutex_lock(&data->mutex);
> ret = regmap_read(data->regmap, bmi160_regs[t].config, &val);
> + mutex_unlock(&data->mutex);
> if (ret < 0)
> return ret;
>
> @@ -402,14 +422,18 @@ static irqreturn_t bmi160_trigger_handler(int irq, void *p)
> int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L;
> __le16 sample;
>
> + mutex_lock(&data->mutex);
> for_each_set_bit(i, indio_dev->active_scan_mask,
> indio_dev->masklength) {
> ret = regmap_bulk_read(data->regmap, base + i * sizeof(__le16),
> &sample, sizeof(__le16));
> - if (ret < 0)
> + if (ret < 0) {
> + mutex_unlock(&data->mutex);
> goto done;
> + }
> buf[j++] = sample;
> }
> + mutex_unlock(&data->mutex);
>
> iio_push_to_buffers_with_timestamp(indio_dev, buf,
> iio_get_time_ns(indio_dev));
> @@ -575,6 +599,7 @@ int bmi160_core_probe(struct device *dev, struct regmap *regmap,
> data = iio_priv(indio_dev);
> dev_set_drvdata(dev, indio_dev);
> data->regmap = regmap;
> + mutex_init(&data->mutex);
>
> ret = bmi160_chip_init(data, use_spi);
> if (ret < 0)
>
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2016-12-30 10:47 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-08 14:22 [PATCH v2 0/6] iio: bmi160: cleanups and hardware fifo support Marcin Niestroj
[not found] ` <20161208142259.26230-1-m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org>
2016-12-08 14:22 ` [PATCH v2 1/6] iio: bmi160: Add of device table for i2c Marcin Niestroj
[not found] ` <20161208142259.26230-2-m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org>
2016-12-30 10:47 ` Jonathan Cameron
2016-12-08 14:22 ` [PATCH v2 2/6] iio: bmi160: Add of device table for spi Marcin Niestroj
2016-12-08 14:22 ` [PATCH v2 3/6] Documentation: DT: Add bmi160 imu binding Marcin Niestroj
[not found] ` <20161208142259.26230-4-m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org>
2016-12-12 17:15 ` Rob Herring
2016-12-30 10:40 ` Jonathan Cameron
2016-12-08 14:22 ` [PATCH v2 4/6] iio: bmi160: Protect data transmission with mutex Marcin Niestroj
[not found] ` <20161208142259.26230-5-m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org>
2016-12-30 10:47 ` Jonathan Cameron [this message]
2016-12-08 14:22 ` [PATCH v2 5/6] iio: bmi160: Fix time needed to sleep after command execution Marcin Niestroj
[not found] ` <20161208142259.26230-6-m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org>
2016-12-30 10:49 ` Jonathan Cameron
[not found] ` <7668f191-dafd-e616-171a-cafc52791292-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2016-12-30 10:51 ` Jonathan Cameron
2016-12-08 14:22 ` [PATCH v2 6/6] iio: bmi160: Support hardware fifo Marcin Niestroj
[not found] ` <20161208142259.26230-7-m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org>
2016-12-30 11:29 ` Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=38885189-27b9-4420-1c28-dfd33eb5ac1f@kernel.org \
--to=jic23-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
--cc=daniel.baluta-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org \
--cc=devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=gregor.boirie-ITF29qwbsa/QT0dZR+AlfA@public.gmane.org \
--cc=knaack.h-Mmb7MZpHnFY@public.gmane.org \
--cc=lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org \
--cc=linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=m.niestroj-z3quKL4iOrmQ6ZAhV5LmOA@public.gmane.org \
--cc=maitysanchayan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
--cc=mark.rutland-5wv7dgnIgG8@public.gmane.org \
--cc=pmeerw-jW+XmwGofnusTnJN9+BGXg@public.gmane.org \
--cc=robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).