From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61D38C54EE9 for ; Tue, 20 Sep 2022 11:29:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230112AbiITL32 (ORCPT ); Tue, 20 Sep 2022 07:29:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230108AbiITL31 (ORCPT ); Tue, 20 Sep 2022 07:29:27 -0400 Received: from mx0a-00128a01.pphosted.com (mx0a-00128a01.pphosted.com [148.163.135.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D07A06BD7D for ; Tue, 20 Sep 2022 04:29:26 -0700 (PDT) Received: from pps.filterd (m0167088.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 28K89N8X006313; Tue, 20 Sep 2022 07:28:45 -0400 Received: from nwd2mta3.analog.com ([137.71.173.56]) by mx0a-00128a01.pphosted.com (PPS) with ESMTPS id 3jn835muv6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 20 Sep 2022 07:28:45 -0400 Received: from ASHBMBX9.ad.analog.com (ASHBMBX9.ad.analog.com [10.64.17.10]) by nwd2mta3.analog.com (8.14.7/8.14.7) with ESMTP id 28KBSi85062651 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 20 Sep 2022 07:28:44 -0400 Received: from ASHBCASHYB5.ad.analog.com (10.64.17.133) by ASHBMBX9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Tue, 20 Sep 2022 07:28:43 -0400 Received: from ASHBMBX9.ad.analog.com (10.64.17.10) by ASHBCASHYB5.ad.analog.com (10.64.17.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Tue, 20 Sep 2022 07:28:42 -0400 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server id 15.2.986.14 via Frontend Transport; Tue, 20 Sep 2022 07:28:42 -0400 Received: from nsa.ad.analog.com ([10.44.3.57]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 28KBR4UZ021723; Tue, 20 Sep 2022 07:28:33 -0400 From: =?UTF-8?q?Nuno=20S=C3=A1?= To: , , , , CC: Chunyan Zhang , Michael Hennerich , Martin Blumenstingl , Sascha Hauer , Cixi Geng , Kevin Hilman , Vladimir Zapolskiy , Pengutronix Kernel Team , Alexandru Ardelean , "Fabio Estevam" , Andriy Tryshnivskyy , Haibo Chen , "Shawn Guo" , Hans de Goede , Miquel Raynal , Jerome Brunet , "Heiko Stuebner" , Florian Boor , Ciprian Regus , Lars-Peter Clausen , Andy Shevchenko , "Jonathan Cameron" , Neil Armstrong , "Baolin Wang" , Jyoti Bhayana , =?UTF-8?q?Nuno=20S=C3=A1?= , Chen-Yu Tsai , Orson Zhai Subject: [PATCH 13/15] iio: health: max30100: do not use internal iio_dev lock Date: Tue, 20 Sep 2022 13:28:19 +0200 Message-ID: <20220920112821.975359-14-nuno.sa@analog.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220920112821.975359-1-nuno.sa@analog.com> References: <20220920112821.975359-1-nuno.sa@analog.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-ORIG-GUID: bI2aLY9E-BSswkh0PqL80oN4pEXUQtzZ X-Proofpoint-GUID: bI2aLY9E-BSswkh0PqL80oN4pEXUQtzZ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-09-20_03,2022-09-16_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 adultscore=0 impostorscore=0 phishscore=0 priorityscore=1501 suspectscore=0 spamscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2209200068 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The pattern used in this device does not quite fit in the iio_device_claim_direct_mode() typical usage. In this case, iio_buffer_enabled() was being used not to prevent the raw access but to allow it. Hence to get rid of the 'mlock' we need to: 1. Use iio_device_claim_direct_mode() to check if direct mode can be claimed and if we can return -EINVAL (as the original code); 2. Make sure that buffering is not disabled while doing a raw read. For that, we can make use of the local lock that already exists. While at it, fixed a minor coding style complain... Signed-off-by: Nuno Sá --- drivers/iio/health/max30100.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/drivers/iio/health/max30100.c b/drivers/iio/health/max30100.c index ad5717965223..aa494cad5df0 100644 --- a/drivers/iio/health/max30100.c +++ b/drivers/iio/health/max30100.c @@ -185,8 +185,19 @@ static int max30100_buffer_postenable(struct iio_dev *indio_dev) static int max30100_buffer_predisable(struct iio_dev *indio_dev) { struct max30100_data *data = iio_priv(indio_dev); + int ret; + + /* + * As stated in the comment in the read_raw() function, temperature + * can only be acquired if the engine is running. As such the mutex + * is used to make sure we do not power down while doing a temperature + * reading. + */ + mutex_lock(&data->lock); + ret = max30100_set_powermode(data, false); + mutex_unlock(&data->lock); - return max30100_set_powermode(data, false); + return ret; } static const struct iio_buffer_setup_ops max30100_buffer_setup_ops = { @@ -387,18 +398,17 @@ static int max30100_read_raw(struct iio_dev *indio_dev, * Temperature reading can only be acquired while engine * is running */ - mutex_lock(&indio_dev->mlock); - - if (!iio_buffer_enabled(indio_dev)) + if (!iio_device_claim_direct_mode(indio_dev)) { ret = -EAGAIN; - else { + iio_device_release_direct_mode(indio_dev); + } else { + mutex_lock(&data->lock); ret = max30100_get_temp(data, val); if (!ret) ret = IIO_VAL_INT; - + mutex_unlock(&data->lock); } - mutex_unlock(&indio_dev->mlock); break; case IIO_CHAN_INFO_SCALE: *val = 1; /* 0.0625 */ -- 2.37.3