Received: by 10.223.176.5 with SMTP id f5csp659904wra; Sat, 3 Feb 2018 07:33:21 -0800 (PST) X-Google-Smtp-Source: AH8x225H6yq6L5Ri7oIqp1MlSMCc24LoZv9xOfkJnFQYjovihoW1GpkGmcu9JJTCG/pVUjvwVtIs X-Received: by 2002:a17:902:52f:: with SMTP id 44-v6mr37650422plf.65.1517672001362; Sat, 03 Feb 2018 07:33:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517672001; cv=none; d=google.com; s=arc-20160816; b=ISwLOt1O4M/vapljLmxTF1B+3pW1Y/p57wmes8zu9+RyjQ/KvIobaldy3ZGbyCWJuT Kx1Akeq/W8ZBHpeHFx1D3dsLgnrF79+0JoElpUkKYoi01Oa7BZ++I5VU+GXZ1YKbZNIw E3jM+go7E3HmV5bbZkUn++mkbu1h0cmHmW3G52VFS2T23G4lhPeMRAT0d+l8VJLSZJuF R7qtbgJ3iZCDO9kwna+u+mcPASvbqAy9eYRC2OUWQUMZiN5KhpvonwkLJG6MgyK+XfVW UlBKq6N8s2K110xCgvresAYSkrdqkwYvHk1QG/Wy/alQPTSp7hCtEyyxSRwHZ1eTrwdD A0nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=ZBQZDUCLkGIn1aRQtLRzspouqCRstadMobLMhTAjR50=; b=vfAjtzk5JWP5oGW4bgNWDDqQBZQzzFHyCeKSIsQg5h7mihWMuTuj6E1eKYtMN6ZqLS gqYV8a5WelRIo4T7jCp18mvbdiuNbxJ4ddt039CYmhM03HCqd9r0z8C3kIKi2vpo3eiD JdRD6+TpAYW8oBssfnY0d6BBa4PT1l2clZEOBjH6K30OdkMgrV118WOxE+g/3GZvUFZC 6YlpCcNkGmAaMKdLyWnIQdNiN7y50uN/Re7aG3DVf3Hb53s4kyGRrG+p1LKN8HXLlgkq bJNdMDMptLuFyKbzHavOggwUCm9FkHUEBivM0jOZtaPQmpy551gDunV0Hb3nESiNbgkc 6B4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=nC9g4tPL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o32-v6si3828421pld.52.2018.02.03.07.33.05; Sat, 03 Feb 2018 07:33:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=nC9g4tPL; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752535AbeBCPb5 (ORCPT + 99 others); Sat, 3 Feb 2018 10:31:57 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:38096 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752159AbeBCPbu (ORCPT ); Sat, 3 Feb 2018 10:31:50 -0500 Received: by mail-pf0-f194.google.com with SMTP id q79so2363224pfl.5; Sat, 03 Feb 2018 07:31:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=ZBQZDUCLkGIn1aRQtLRzspouqCRstadMobLMhTAjR50=; b=nC9g4tPLqoSN+WbzpmboMCriRRaiO1EHTa2pknnmWfN+qIPXzKyL/mhmZP+GZhW4r8 LBXYQsoYiCXIa/xh6o5aBgKq3+yXbTbwQaLdhZo0nLVUzPuvo32zns+y26Eb7DtJwJb3 zqxXnkujVhxvx1gvJYqY6ULOYN+tZtX6Im5rrD0n5YR9QNNxVsIH8DnInCwT8K03f6A+ uyQOCA7wTd2nTMawTYlk9DcHWDF83hZvNTI/LiFwc0CEROB8K3VLA7BGFmFjIlk7Ca2x GfokbqRfTaIOA9G/yZoxqDyU287HK7TB6wYJzwjCw90G62MBG08Q/mQ+/EqrrBeGTfpV GAjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ZBQZDUCLkGIn1aRQtLRzspouqCRstadMobLMhTAjR50=; b=Yb30tLaD5F7GvLSZWtRtEztFZjieGKjs15amp4NrGjTdUipL7Z9Vg/MfOi9ty3ymlu mZbscGwHSqgG0kkVsr0VwfhF3s+xCm8rjZr04K/IFXRzZ11J41wPX/KZbOofPhwIXM2O vWj7D5zZLxf/jJqzXYlFdEPat8TOVmXEcwZroZiYiZRcnB50fT/dyFKxA3NNgcfMl6s3 MxErsC80Ezw81aC7ohkgN8IfUsJgjG1GiphHOOU/SlZrS4YOnvbjuAsfzf35okniCN1x lJv5BIKD9jT0y/wSurmv7VbKMFAkOhYJnLdeK5hxNmhEST4tRzoA1wT/1XzcKCgGROrh NDHA== X-Gm-Message-State: AKwxytc35fvzmE+4xAsiTA1m/SlMqaVw5bXxgb9TvcDi2RE0XjZye5GO IShUkuzk2qI9l6ofrGAvdYQ= X-Received: by 10.99.54.76 with SMTP id d73mr2477356pga.202.1517671909717; Sat, 03 Feb 2018 07:31:49 -0800 (PST) Received: from shreeya-VirtualBox ([103.212.140.159]) by smtp.gmail.com with ESMTPSA id b9sm7057329pgq.35.2018.02.03.07.31.47 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 03 Feb 2018 07:31:48 -0800 (PST) From: Shreeya Patel To: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org, knaack.h@gmx.de, pmeerw@pmeerw.net, gregkh@linuxfoundation.org, linux-iio@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Cc: Shreeya Patel Subject: [PATCH v4] Staging: iio: ade7758: Expand buf_lock to cover both buffer and state protection Date: Sat, 3 Feb 2018 21:01:32 +0530 Message-Id: <1517671892-4045-1-git-send-email-shreeya.patel23498@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org iio_dev->mlock is to be used only by the IIO core for protecting device mode changes between INDIO_DIRECT and INDIO_BUFFER. This patch replaces the use of mlock with the already established buf_lock mutex. Introducing 'unlocked' forms of read and write registers. The read/write frequency functions now require buf_lock to be held. That's not obvious so avoid this but moving the locking inside the functions where it is then clear that they are taking the unlocked forms of the register read/write. It isn't readily apparent that write frequency function requires the locks to be taken, so move it inside the function to where it is required to protect. Signed-off-by: Shreeya Patel --- Changes in v2 -Add static keyword to newly introduced functions and remove some added comments which are not required. Changes in v3 -Remove some useless mlocks and send it as another patch. Also make the necessary change in the current patch associated with the new patch with commit id 88eba33. Make commit message more appropriate. Changes in v4 -Write frequency function do not require lock so move it inside the function to where it is required to protect. drivers/staging/iio/meter/ade7758.h | 2 +- drivers/staging/iio/meter/ade7758_core.c | 49 ++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/drivers/staging/iio/meter/ade7758.h b/drivers/staging/iio/meter/ade7758.h index 6ae78d8..2de81b5 100644 --- a/drivers/staging/iio/meter/ade7758.h +++ b/drivers/staging/iio/meter/ade7758.h @@ -111,7 +111,7 @@ * @trig: data ready trigger registered with iio * @tx: transmit buffer * @rx: receive buffer - * @buf_lock: mutex to protect tx and rx + * @buf_lock: mutex to protect tx, rx, read and write frequency **/ struct ade7758_state { struct spi_device *us; diff --git a/drivers/staging/iio/meter/ade7758_core.c b/drivers/staging/iio/meter/ade7758_core.c index 227dbfc..ff19d46 100644 --- a/drivers/staging/iio/meter/ade7758_core.c +++ b/drivers/staging/iio/meter/ade7758_core.c @@ -24,17 +24,25 @@ #include "meter.h" #include "ade7758.h" -int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) +static int __ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) { - int ret; struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ade7758_state *st = iio_priv(indio_dev); - mutex_lock(&st->buf_lock); st->tx[0] = ADE7758_WRITE_REG(reg_address); st->tx[1] = val; - ret = spi_write(st->us, st->tx, 2); + return spi_write(st->us, st->tx, 2); +} + +int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) +{ + int ret; + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct ade7758_state *st = iio_priv(indio_dev); + + mutex_lock(&st->buf_lock); + ret = __ade7758_spi_write_reg_8(dev, reg_address, val); mutex_unlock(&st->buf_lock); return ret; @@ -91,7 +99,7 @@ static int ade7758_spi_write_reg_24(struct device *dev, u8 reg_address, return ret; } -int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) +static int __ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) { struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ade7758_state *st = iio_priv(indio_dev); @@ -111,7 +119,6 @@ int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) }, }; - mutex_lock(&st->buf_lock); st->tx[0] = ADE7758_READ_REG(reg_address); st->tx[1] = 0; @@ -124,7 +131,19 @@ int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) *val = st->rx[0]; error_ret: + return ret; +} + +int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) +{ + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct ade7758_state *st = iio_priv(indio_dev); + int ret; + + mutex_lock(&st->buf_lock); + ret = __ade7758_spi_read_reg_8(dev, reg_address, val); mutex_unlock(&st->buf_lock); + return ret; } @@ -480,10 +499,12 @@ static int ade7758_read_samp_freq(struct device *dev, int *val) return 0; } -static int ade7758_write_samp_freq(struct device *dev, int val) +static int ade7758_write_samp_freq(struct iio_dev *indio_dev, int val) { int ret; u8 reg, t; + struct ade7758_state *st = iio_priv(indio_dev); + struct device *dev = &indio_dev->dev; switch (val) { case 26040: @@ -503,16 +524,20 @@ static int ade7758_write_samp_freq(struct device *dev, int val) goto out; } - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, ®); + mutex_lock(&st->buf_lock); + + ret = __ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, ®); if (ret) goto out; reg &= ~(5 << 3); reg |= t << 5; - ret = ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, reg); + ret = __ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, reg); out: + mutex_unlock(&st->buf_lock); + return ret; } @@ -545,9 +570,9 @@ static int ade7758_write_raw(struct iio_dev *indio_dev, case IIO_CHAN_INFO_SAMP_FREQ: if (val2) return -EINVAL; - mutex_lock(&indio_dev->mlock); - ret = ade7758_write_samp_freq(&indio_dev->dev, val); - mutex_unlock(&indio_dev->mlock); + + ret = ade7758_write_samp_freq(indio_dev, val); + return ret; default: return -EINVAL; -- 2.7.4