Received: by 10.223.176.5 with SMTP id f5csp2891477wra; Mon, 5 Feb 2018 11:42:46 -0800 (PST) X-Google-Smtp-Source: AH8x2251nEtU7SEEs/i2c0Et8Pi4eRSdE2gEKFBPjCGGAZgY3CbeAK+0aclTHFbUyrtGHw0OuZzy X-Received: by 10.99.148.17 with SMTP id m17mr38286833pge.367.1517859766161; Mon, 05 Feb 2018 11:42:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517859766; cv=none; d=google.com; s=arc-20160816; b=nv6ROE1RTksnemkNO8YbaoW7wou12zMsDCr6xlCV8i+im2iPnfd61F1MJc2cRR2RTn 9gXBZa/JHVNcLoovSbuj06iPdreUsWSd1lEFnb5f1Bj8guUph3o+6Z26r3H/AA6VJVi9 gISAkk7w43KHJWUY8Ftb2w7AXF0de6vf5lAQakkML6WdNM7sKGdwfLouc4qr8Xe7EO+l /XppsFe/tX+H7A3e2XeNPPTuX1AfZ2UHVCQROFv4CjaX475Zw+yVB+pghFzyHGSHDFFJ 5O+G5JmAkfQQc0UsT6WvNEw7mGxAJzrdsFdrfvEe7IUD2ILQ/cUnli73AvRU3SjC2v6o 47Iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=H8+8zvfFxknp7VJXc0tIUfIY0usIb2510IfvCidsBZg=; b=qaFRSaZqsJ+s5JJmgASSPYoskFkaiysJkhlRmfbSPKIBVK3GbueF33Ocnce3lr38v4 F2kg+DgrdMHBnt2D5Q9vj6eOAi71Q9PmmSbvm/ebtNlIRLHm9QTQRBR2tGj43owYemm9 PP2kl/AznGpwCPZe72x0WSeqJpRal9Dk38InfYcauvjAV7r6hlVH/zhoXqdbS65eSu6f qpm020IKZt5DbcCvB8UhlcQJcWzbgNKGkCGqinB+i9vBMfAMLxl1YDFCe6lsEtPD2biQ xDyiz/pfN3lWMq9cuOVSVb/AJPrsWEeGvSLw+jIsJxKVF3M01RW6rK8q+xBxknlselPb cEOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=KIa/7MJx; 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 e16-v6si2846006pli.776.2018.02.05.11.42.31; Mon, 05 Feb 2018 11:42:46 -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=KIa/7MJx; 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 S1751624AbeBETk4 (ORCPT + 99 others); Mon, 5 Feb 2018 14:40:56 -0500 Received: from mail-pg0-f66.google.com ([74.125.83.66]:34029 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750812AbeBETku (ORCPT ); Mon, 5 Feb 2018 14:40:50 -0500 Received: by mail-pg0-f66.google.com with SMTP id s73so5170142pgc.1; Mon, 05 Feb 2018 11:40: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:in-reply-to:references; bh=H8+8zvfFxknp7VJXc0tIUfIY0usIb2510IfvCidsBZg=; b=KIa/7MJxbuCjKGcoBOnWz4sEb4P3b2DJDVfQLR8abZ4sAWaYaCXZc/tZSHub03vp6t NVkppJQmqxT/Y68P57VNf7hMiJFKNhwH+PbEmG5WpQQR+Ml2PdaWcJgTrKQ0+1Eyh869 2K9+hsGsY4nkVy4uiC3r/XtSCQ1C35nzxNL20udiZy3ukU0jm/AQiNahkOpEdSNHM3BJ eGrP1NLwlkX/OJt0s3TkRffpggWJYfMYTO9+ZcY1FWvGqQNaDc5liykuAnPMX+xl3IxO 0IjdWLfTGbTUirJ9bj7z6ifJaqpKb0Gj+wG2zbFXb7Rn7tG5Pqse+lYx+J8LyAEUVPqQ pcGg== 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:in-reply-to :references; bh=H8+8zvfFxknp7VJXc0tIUfIY0usIb2510IfvCidsBZg=; b=cNEhEbP2qhxJWEAbZ0QIQyirbKEk+lAte16uU10uVAIRhM2PlvkfKvJPr94gIRq9z9 H1KVyZt8aRq9upT3Lve/Gvs9diGWNIde6KchAZVRaELWHEAu7AG0sIY7QF2Wm79q+nWM Ohb8VvcqEYRdRtCIv45VcmqgeSEWgXFjUtzHzcNT220zsIp1HjAWPTHKTBD3vDeHpgQp Mnr+E0235ZQ8IMpTFDMXO3Fp5C1/7pqb0Lcrtx2dqZUPz87zBGyoTO6umS10AQB9Iid9 /4Vh2ThARtXfBEOD3xGKMn8jKFb0SB3qLqIz9jAlU4VzXezZfhPcHF8APxCOFMjgBFik MpIg== X-Gm-Message-State: AKwxytch1mQ7gtKnuMhbnItoznoH4IAWcm3ygfDaUNQqBX060HXtl302 T3rtCtyvEtOVd2BbGSUn+C4= X-Received: by 10.98.196.204 with SMTP id h73mr14963033pfk.143.1517859649639; Mon, 05 Feb 2018 11:40:49 -0800 (PST) Received: from shreeya-VirtualBox ([103.212.140.133]) by smtp.gmail.com with ESMTPSA id g15sm14931415pgu.52.2018.02.05.11.40.48 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Feb 2018 11:40:49 -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 v5 2/2] Staging: iio: ade7758: Expand buf_lock to cover both buffer and state protection Date: Tue, 6 Feb 2018 01:10:45 +0530 Message-Id: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: 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. Changes in v5 -Remove goto statement and make the code to return -EINVAL there itself. drivers/staging/iio/meter/ade7758.h | 2 +- drivers/staging/iio/meter/ade7758_core.c | 52 +++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 15 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..dfe8e97 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: @@ -499,20 +520,23 @@ static int ade7758_write_samp_freq(struct device *dev, int val) t = 3; break; default: - ret = -EINVAL; - goto out; + return -EINVAL; } - 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 +569,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