Received: by 10.223.176.46 with SMTP id f43csp2360302wra; Thu, 25 Jan 2018 08:41:45 -0800 (PST) X-Google-Smtp-Source: AH8x226U4UQMX4d0NqGK+rvWKKMMwC8oDtw67+/gv8cUsl3tRKRsqWDwEkQKzAQYuWZ0ISxUBLc0 X-Received: by 10.99.131.74 with SMTP id h71mr13420391pge.373.1516898505739; Thu, 25 Jan 2018 08:41:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516898505; cv=none; d=google.com; s=arc-20160816; b=mPaWMtMFXqUOU1bu0fLnA0NlS9MllaIKLOVj780RV3E9scwXb50YWixgHzJHS70XND Gnq4hEHMJO0wLHqy5OdmDU9ZDyTX+pz/VNHgBBiNR5vLmnOf9PrUWW6lEfD0z0/s0oii 3t8N79ri4Tp6UKcCFEAipYO2Okk6l9tErbpvA17LDz6ZyjxO5ernGXyxQblumYV1SbEF 87/5g4lWVvxnPosVtck2OLKS80Hu2cVQ7xUZUsVOXU9VzzoBTpvmZumfL3msiYsk9SZ/ 3J4bRHl95QeSScfxW+g/sJjU95AR72sOx7Mg6JGdTFzj7DinNJRA1R4vOyv0Pz8VFZfe uunA== 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=cObNIuwDBTc+3AYmVUAfxV/HuB+rQ3UIOXg1siqcP3k=; b=RAxk+3BDm7AczWA+aeY4ZTPL5POztSex5Utmwmwtu8yOXCPvoShKto9SF4u1r6oAYb hPxKYicsSGhOGcS/VgFg9NtfXqkHo362K4WQutjWTFBMRtqxMu5l0r9a/eTVngKNNqYL puUWwvatT4RnCqll7vodKrGvBM0olsOQ/Bcg2vaKCV55VNoRkCt4tVT67r5FNenzbF0Q 7idAkARNX2QHC7BXviK5MUFAl2imkk/DWY35l54xG9CWAXT9yrvF3oMYrDpqSuzm2fgE uvkVKUoI0Nqt++lHAHYRvl4R1nfMpOe7/2D6LQaKVvLdgbUFVVdxMv7XWX2CYkcTlvpI oMSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=s4vsCjce; 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 o7si1759246pgr.390.2018.01.25.08.41.30; Thu, 25 Jan 2018 08:41:45 -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=s4vsCjce; 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 S1751267AbeAYQkP (ORCPT + 99 others); Thu, 25 Jan 2018 11:40:15 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:35397 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750980AbeAYQkN (ORCPT ); Thu, 25 Jan 2018 11:40:13 -0500 Received: by mail-pg0-f65.google.com with SMTP id o13so5394781pgs.2; Thu, 25 Jan 2018 08:40:13 -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=cObNIuwDBTc+3AYmVUAfxV/HuB+rQ3UIOXg1siqcP3k=; b=s4vsCjce+/iIrxoX86UhZJgOLJKqdS5CCJgXQz0dxukEZp7buxo1V+5+38PR2xE/21 PvXGOaA8+RA5X/3g2PKMC1K+0Ryayp5qQUN+rtYxF77jv8Z/j78viK6/lnjqpUjnzXlz Ac3FG/ty5pQaW+nmbch4QsLAout+NYJu+U9eCXeV5/pctJbbDXWXwi3n8qFUOOwE0Lft yQi1u68h64UfmclQdqI2q91bc6zp9kLDXhJapcsDGdlx5DmYjl6y2Dv6BvYj/TdlA+FK cgZQrwaJLcInE6HIfTUFNfu6NfniL33fFaMnRkiMVoezgMz+V5fRvKTDtqSOGQLXEXOt 5mzg== 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=cObNIuwDBTc+3AYmVUAfxV/HuB+rQ3UIOXg1siqcP3k=; b=RmyYPh3NSAXCGqPUHhdroz80MloGE1j+be57T0PqogSPW3laUroQzyjLMTfqddGpx1 6ILt780U0UKXs/MG7uYc7dUbutTOCjWePjEHQ7Sm/28TcAdbiq21zYcIjA8Tri+6IOAb BOBSH4uDEBCG2DHo2KdeKEXTb3C7lU4eDNpFAroapdAEvYxl1zI/6nXLR+mHYzW5IyrR SwAIb8CB7w2szgN/OYom4uxwB195EzsO8d2Xrr1S5XOM6+OAkYoF3UqpqdDlAYosBV0W 3cnRjLQ5kdlb30tK7XolfgoVvm20LOt7SPoFTzwyE5klLuR7X6TeYzq/g1p2ec3be55y LPBw== X-Gm-Message-State: AKwxytfQBTEImGVnLGPQLM2bQnN9Ne6Yxmd8FjgPgcKJpsKeEvPsjgZF 9GlTghIBwtZtGw4aZtnegJM= X-Received: by 10.98.233.10 with SMTP id j10mr15948948pfh.123.1516898412639; Thu, 25 Jan 2018 08:40:12 -0800 (PST) Received: from shreeya-VirtualBox ([103.212.140.157]) by smtp.gmail.com with ESMTPSA id u63sm15511560pfi.63.2018.01.25.08.40.11 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 25 Jan 2018 08:40:11 -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 v2] Staging: iio: ade7758: expand buf_lock to cover both buffer and state protection Date: Thu, 25 Jan 2018 22:10:08 +0530 Message-Id: <1516898408-8129-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' __ade7758_spi_write_reg_8 and __ade7758_spi_read_reg_8 functions to be used by ade7758_write_samp_freq and ade7758_read_samp_freq which avoids nested locks and maintains atomicity between bus and device frequency changes. Signed-off-by: Shreeya Patel --- Changes in v2 -Add static keyword to newly introduced functions and remove some added comments which are not required. drivers/staging/iio/meter/ade7758.h | 2 +- drivers/staging/iio/meter/ade7758_core.c | 47 +++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 14 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 7b7ffe5..fed4684 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; } @@ -470,7 +489,7 @@ static int ade7758_read_samp_freq(struct device *dev, int *val) int ret; u8 t; - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &t); + ret = __ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &t); if (ret) return ret; @@ -503,14 +522,14 @@ static int ade7758_write_samp_freq(struct device *dev, int val) goto out; } - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, ®); + 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: return ret; @@ -523,12 +542,13 @@ static int ade7758_read_raw(struct iio_dev *indio_dev, long mask) { int ret; + struct ade7758_state *st = iio_priv(indio_dev); switch (mask) { case IIO_CHAN_INFO_SAMP_FREQ: - mutex_lock(&indio_dev->mlock); + mutex_lock(&st->buf_lock); ret = ade7758_read_samp_freq(&indio_dev->dev, val); - mutex_unlock(&indio_dev->mlock); + mutex_unlock(&st->buf_lock); return ret; default: return -EINVAL; @@ -542,14 +562,15 @@ static int ade7758_write_raw(struct iio_dev *indio_dev, int val, int val2, long mask) { int ret; + struct ade7758_state *st = iio_priv(indio_dev); switch (mask) { case IIO_CHAN_INFO_SAMP_FREQ: if (val2) return -EINVAL; - mutex_lock(&indio_dev->mlock); + mutex_lock(&st->buf_lock); ret = ade7758_write_samp_freq(&indio_dev->dev, val); - mutex_unlock(&indio_dev->mlock); + mutex_unlock(&st->buf_lock); return ret; default: return -EINVAL; -- 2.7.4