From nobody Mon Jun 15 22:05:38 2026 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0112F38F248 for ; Tue, 14 Apr 2026 22:43:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776206603; cv=none; b=Cih1/hrJLx+LgbpffTz6p1gjXGKgdfNadv4eLfT10NJpi8aQ9Cyiltq+D/4ESK2QfneF+2/J7nxl0GqVeO9ykeZzpb8nSwHCDAg/9AyROT0/GcB75Lu05Fq6JbPGvrAgSbsjAIp/TZyYjDX5tNCwsYRhsFZuORtmoH1+mwyR1mw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776206603; c=relaxed/simple; bh=vNRA6J2+Nh5kk+8wVzqvkREK5Nenp0Br7khYY03UGDc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gjoYqtR07Y7njNNEBdt+/FEJNpD9PKHWGcJG/7EapmxL6QCX1MWqbfWDQZFg2k4wfL0sJu3GZJla7LOj8DESVRAvSEm6CrTbL3T4MAfL8lvjKNtsBYik6fmBR6HASSbws/8UmdFJ6SeBKcVt1ZQX1FjYZzem/e/XFoNC5oyY9BA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=usp.br; spf=pass smtp.mailfrom=usp.br; dkim=pass (2048-bit key) header.d=usp.br header.i=@usp.br header.b=SjccCrv4; arc=none smtp.client-ip=74.125.82.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=usp.br Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=usp.br Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=usp.br header.i=@usp.br header.b="SjccCrv4" Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-2bdcf5970cdso3850608eec.0 for ; Tue, 14 Apr 2026 15:43:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=usp.br; s=usp-google; t=1776206599; x=1776811399; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8xVaqx2bA0uX9Kc1Awl/FAoEiMPC51tiJH6XAaQB8+g=; b=SjccCrv4PlnV3GHaggNSfArQxDA/2uwZjjrobAMxHFvYkVDwxFkdJ0hAwmEPWwopVA JG4o2w09JoEUAslelG1d8gYXwiUPAD1XpQDUh1B/ydCES8jmpnY0bBg3BITVeSdUAxob AxgR3rsMgIXEzncgDoLUEB8xvQjhOO3yFiUoZRsSL9JGA7W3+Zlne29LHpL6WuPokOy1 tE2LWGdWN8zvP8iw/X3wR/jIDh/CwaOON7D0L1/tCsdQ/W1pm+dGyAaFPPi+QCmieMru 6uLpUFMVDkUPfEV5H9cH9p2TvOJsSK+H9sJKhtd2ZUxKTFwdmjea18gMKspom0tZst1v bOYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776206599; x=1776811399; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=8xVaqx2bA0uX9Kc1Awl/FAoEiMPC51tiJH6XAaQB8+g=; b=OZkSOiht+OcfFS/2sqhfceVzZ/J/YtV9AjPTEGOvMJHdFzRLRgoIvk6ep3kZWz0ghB MlF+A9I3Y2SQf0so9ZhD1GWrvmCF9IpyVuKNbKdJJHMXRG4DIk/iZRIqGk8mV+XM0uOA gMIIlZLOPm5utvl50MPmjW6geXGe4hnbK1XBXHM9yPlsauxssfKwfT6Pbkp97hdLqvjw XKlT/220SXz4AR6VVMWq72zXjXiieFfi7bP6g2vKe+YYTFeD8ntRf7LncDS8doMkRCZg ksjtONIIBmamDglWPTY9foT4WGUBQXTfTmGM40g36ELfFVMFRbhL59LjuHFeiEJ9DHYx H2Fw== X-Forwarded-Encrypted: i=1; AFNElJ9UaqyZayXAAbz83RO9DvT3f8JKyBkhkcdLRFQ8lpgZ+BDiYXeS8ktHSSE2x/UWpW39FO+ax2cPsOQSIRE=@vger.kernel.org X-Gm-Message-State: AOJu0YzKC4zrpUKFPtdJbGatmTyxbzYIu9uoVfRW5TcmWqb81NjAF7GI /FEicXxNZjLaBjGPXE/YGdDYzqRm4OnIPmnx9Mqf1e1CqsYTW0L5uUNZv6A1GIzIo2E= X-Gm-Gg: AeBDiesL2wdvocqAj578mUQxKaZvwfOPdps4XBEqtam4Ol1r2xJkYWWajr/rAO8HWi+ 4BwltM2F0QZQUF4Gsa85K7EfzX+ZYVw69mGwCnpGGxyeamDnJMEgsuqwPlpZf2B1cP4W9J380yR oJa/3d7rDoSlu+EROHcpErsmGmffq2Ejn4Vr/SeAP7u2TwNqRoR70EQxNjrYMHPg/tIx/FAbink WJLrAKhlB+ATj9TWbbVi8YaiijJu0gCxdEDSglL0IJ2/ysKXO871qavVIx61MaTDePLTN14crxG W2q+wWNrqiIYk2Jw/UNVD+R4uRYsFNKy463ld+BM1oGvM4VTmBDQkTK2HTtiQK2sbHtUag+dz8C u6eeSHQxDoHIXo6XmDL0KsSepuVsa44egobU1yiHrTTW2SiFN4YnfDyVvAhWErKCtuZn+e5dQtH UjqdeoHbTMrJLoZ2tvaulOjkhDySNuxpQdwqZmTXv+y3xumPdWNvj3wDYkVQTXZ6VtcMA0590wF 0bRos53dfU+AGG2NBJ4fQUtP0Qvo9IWZWMvglbuqpwTOOgpSA== X-Received: by 2002:a05:7301:2a27:b0:2be:acac:af7f with SMTP id 5a478bee46e88-2d40e3e6960mr10640030eec.7.1776206598921; Tue, 14 Apr 2026 15:43:18 -0700 (PDT) Received: from gui-host.Dlink ([177.33.104.190]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2dd32dc2827sm4650361eec.23.2026.04.14.15.43.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2026 15:43:18 -0700 (PDT) From: Guilherme Ivo Bozi To: Salih Erim , Conall O'Griofa , Jonathan Cameron , Michal Simek Cc: David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Guilherme Ivo Bozi Subject: [PATCH v3 1/3] iio: adc: xilinx-ams: fix out-of-bounds channel lookup in event handling Date: Tue, 14 Apr 2026 19:40:33 -0300 Message-ID: <20260414224245.8493-2-guilherme.bozi@usp.br> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260414224245.8493-1-guilherme.bozi@usp.br> References: <20260414224245.8493-1-guilherme.bozi@usp.br> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ams_event_to_channel() may return a pointer past the end of dev->channels when no matching scan_index is found. This can lead to invalid memory access in ams_handle_event(). Add a bounds check in ams_event_to_channel() and return NULL when no channel is found. Also guard the caller to safely handle this case. Fixes: d5c70627a794 ("iio: adc: Add Xilinx AMS driver") Signed-off-by: Guilherme Ivo Bozi --- drivers/iio/adc/xilinx-ams.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/iio/adc/xilinx-ams.c b/drivers/iio/adc/xilinx-ams.c index 124470c92529..6191cd1b29a5 100644 --- a/drivers/iio/adc/xilinx-ams.c +++ b/drivers/iio/adc/xilinx-ams.c @@ -871,6 +871,9 @@ static const struct iio_chan_spec *ams_event_to_channel= (struct iio_dev *dev, if (dev->channels[i].scan_index =3D=3D scan_index) break; =20 + if (i =3D=3D dev->num_channels) + return NULL; + return &dev->channels[i]; } =20 @@ -1012,6 +1015,8 @@ static void ams_handle_event(struct iio_dev *indio_de= v, u32 event) const struct iio_chan_spec *chan; =20 chan =3D ams_event_to_channel(indio_dev, event); + if (!chan) + return; =20 if (chan->type =3D=3D IIO_TEMP) { /* --=20 2.47.3 From nobody Mon Jun 15 22:05:38 2026 Received: from mail-dy1-f182.google.com (mail-dy1-f182.google.com [74.125.82.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93EE838E5D6 for ; Tue, 14 Apr 2026 22:43:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776206615; cv=none; b=RUqV4mSEqE+UWo3Kn/GhPTZeEMEjD+8FkOv/u802XFdq68Wd5qpQemdBL+kGhJFm84maLRTTgsILn+UyMs5SJoKPKf+F8n0Mw1YxVuydx+Btsv4LSal0UoXVe33olTRM4zgWCvmZ925QHqVmPsdmzmxwdSyTo2mNVnMLBlhomNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776206615; c=relaxed/simple; bh=GV5Du5irr67MCF6Y5SivJf7NN5YLZm0xYjcNv8fUz4Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XX07dWIOTyRnDwmxyuPdN7l/Zu/lBSvVzp/Zw+tvV3tRogI/N3rnYNfXT9+61gP9Vn970T5O2lTz9/85t/Gto6yseCZXz8sqBHDCIggkcyWK2kGmTkeh3VhUdwpDH7EO5aAPC0AOaepvAaaVxlyimA/lcK2x4chmh+daG/F3Dwc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=usp.br; spf=pass smtp.mailfrom=usp.br; dkim=pass (2048-bit key) header.d=usp.br header.i=@usp.br header.b=hjMSAFeo; arc=none smtp.client-ip=74.125.82.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=usp.br Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=usp.br Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=usp.br header.i=@usp.br header.b="hjMSAFeo" Received: by mail-dy1-f182.google.com with SMTP id 5a478bee46e88-2bdd40d3c61so5488114eec.1 for ; Tue, 14 Apr 2026 15:43:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=usp.br; s=usp-google; t=1776206608; x=1776811408; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Rj8k2pIm7AGjb+3hd5/bWIIgB52x4pA/wcxJmlNMSMU=; b=hjMSAFeolBJGTl8Obsb0Lbfgm0ymIPRBJN0DWOXweKtqaNfgqPiwarOJFuzT1aQJir 95Zf2WWqiFjsLOShpjtk2ZXMh71rxlUPkH6qe4og+IUA3F00mz4j6+HTk00fpTtMtYEn 3DYhr6xn3mbwEjPcLsj8SoDohrNP0ZpD6lrvAJQWlPe3Zry0za7CsND9G8jwvTLabMHz bHo8wqu9qL0X1NmG7yUUwzEaPAft49F224IO6gCZUTRlrji5PGAZKSWaZjVVh+Qta6Ca K0G1ZWiyzPG9aX3GSEQaSxJDLqlCLgOn+esnoFhFB87qVB0L+Rw05VnUa25EIyFkb0E9 qwvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776206608; x=1776811408; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Rj8k2pIm7AGjb+3hd5/bWIIgB52x4pA/wcxJmlNMSMU=; b=ESNEHhK5jngzUBm7wka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QZq0I+PMmLyH6nObcYw1CNugylW1pmmQeKC90vh0l VaWprJV1Hf+obX73CKzsDx+XUwIs1Ge/slhAGI+1nJaBESRoQ== X-Received: by 2002:a05:693c:80c1:b0:2d9:6f2f:9f6f with SMTP id 5a478bee46e88-2d96f2fd124mr3002666eec.24.1776206608051; Tue, 14 Apr 2026 15:43:28 -0700 (PDT) Received: from gui-host.Dlink ([177.33.104.190]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2dd32dc2827sm4650361eec.23.2026.04.14.15.43.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2026 15:43:27 -0700 (PDT) From: Guilherme Ivo Bozi To: Salih Erim , Conall O'Griofa , Jonathan Cameron , Michal Simek Cc: David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Guilherme Ivo Bozi , Andy Shevchenko Subject: [PATCH v3 2/3] iio: adc: xilinx-ams: use guard(mutex) for automatic locking Date: Tue, 14 Apr 2026 19:40:34 -0300 Message-ID: <20260414224245.8493-3-guilherme.bozi@usp.br> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260414224245.8493-1-guilherme.bozi@usp.br> References: <20260414224245.8493-1-guilherme.bozi@usp.br> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace open-coded mutex_lock()/mutex_unlock() pairs with guard(mutex) to simplify locking and ensure proper unlock on all control flow paths. This removes explicit unlock handling, reduces boilerplate, and avoids potential mistakes in error paths while keeping the behavior unchanged. Signed-off-by: Guilherme Ivo Bozi Reviewed-by: Andy Shevchenko --- drivers/iio/adc/xilinx-ams.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/drivers/iio/adc/xilinx-ams.c b/drivers/iio/adc/xilinx-ams.c index 6191cd1b29a5..70a1f97f6dad 100644 --- a/drivers/iio/adc/xilinx-ams.c +++ b/drivers/iio/adc/xilinx-ams.c @@ -720,22 +720,20 @@ static int ams_read_raw(struct iio_dev *indio_dev, int ret; =20 switch (mask) { - case IIO_CHAN_INFO_RAW: - mutex_lock(&ams->lock); + case IIO_CHAN_INFO_RAW: { + guard(mutex)(&ams->lock); if (chan->scan_index >=3D AMS_CTRL_SEQ_BASE) { ret =3D ams_read_vcc_reg(ams, chan->address, val); if (ret) - goto unlock_mutex; + return ret; ams_enable_channel_sequence(indio_dev); } else if (chan->scan_index >=3D AMS_PS_SEQ_MAX) *val =3D readl(ams->pl_base + chan->address); else *val =3D readl(ams->ps_base + chan->address); =20 - ret =3D IIO_VAL_INT; -unlock_mutex: - mutex_unlock(&ams->lock); - return ret; + return IIO_VAL_INT; + } case IIO_CHAN_INFO_SCALE: switch (chan->type) { case IIO_VOLTAGE: @@ -939,7 +937,7 @@ static int ams_write_event_config(struct iio_dev *indio= _dev, =20 alarm =3D ams_get_alarm_mask(chan->scan_index); =20 - mutex_lock(&ams->lock); + guard(mutex)(&ams->lock); =20 if (state) ams->alarm_mask |=3D alarm; @@ -948,8 +946,6 @@ static int ams_write_event_config(struct iio_dev *indio= _dev, =20 ams_update_alarm(ams, ams->alarm_mask); =20 - mutex_unlock(&ams->lock); - return 0; } =20 @@ -962,15 +958,13 @@ static int ams_read_event_value(struct iio_dev *indio= _dev, struct ams *ams =3D iio_priv(indio_dev); unsigned int offset =3D ams_get_alarm_offset(chan->scan_index, dir); =20 - mutex_lock(&ams->lock); + guard(mutex)(&ams->lock); =20 if (chan->scan_index >=3D AMS_PS_SEQ_MAX) *val =3D readl(ams->pl_base + offset); else *val =3D readl(ams->ps_base + offset); =20 - mutex_unlock(&ams->lock); - return IIO_VAL_INT; } =20 @@ -983,7 +977,7 @@ static int ams_write_event_value(struct iio_dev *indio_= dev, struct ams *ams =3D iio_priv(indio_dev); unsigned int offset; =20 - mutex_lock(&ams->lock); + guard(mutex)(&ams->lock); =20 /* Set temperature channel threshold to direct threshold */ if (chan->type =3D=3D IIO_TEMP) { @@ -1005,8 +999,6 @@ static int ams_write_event_value(struct iio_dev *indio= _dev, else writel(val, ams->ps_base + offset); =20 - mutex_unlock(&ams->lock); - return 0; } =20 --=20 2.47.3 From nobody Mon Jun 15 22:05:38 2026 Received: from mail-dl1-f45.google.com (mail-dl1-f45.google.com [74.125.82.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B70CB38C412 for ; Tue, 14 Apr 2026 22:43:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776206623; cv=none; b=qGny+G4uXhVfEg6JdzxMFRktFBzD6r3JMHxDopLpasYuha8cShA9nChPLaIZMwWiqU5ihS5YvDY0bxi6ejHCN5Vxe5+criff84A224BJ1mdgscXZO+nNBD9vq6ti3xVdqDeR8LBs0hJcgrVN4uy9zHQMzjA/YC/rA5yHB2kyB8o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776206623; c=relaxed/simple; bh=1qYBzUhFsUGJZx2QfFBfNFPOg+2WtmALd9u5R0MyRP0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B20VYaCL+QHEfc9YQ2AwgGhb0PxeiiDVLDOTIEgRfa/gyoHD82x7xVt7CbkcP7cSGjB+xaqAA6/H88PZbbaD5Phsh0j3M/3+nC0l4cVH/SsTrSw8GOwlb3fG1Apk/6evwSTdlfRQo5bqRXrVDvUDyPYb0hQJQMII1y0Y+nciHWo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=usp.br; spf=pass smtp.mailfrom=usp.br; dkim=pass (2048-bit key) header.d=usp.br header.i=@usp.br header.b=gbKahJL5; arc=none smtp.client-ip=74.125.82.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=usp.br Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=usp.br Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=usp.br header.i=@usp.br header.b="gbKahJL5" Received: by mail-dl1-f45.google.com with SMTP id a92af1059eb24-12c287eb77fso9341755c88.1 for ; Tue, 14 Apr 2026 15:43:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=usp.br; s=usp-google; t=1776206617; x=1776811417; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Atp4lMX+KQb/XdR4N3FQnBkv+Ii6UBEhE3TAnqHAWWo=; b=gbKahJL5UrrX4bExkoOmPSu5QOCb3oxz4pC6cOKeCsXzRgykUqpeZkSFugs3Us2rEr pJwqUtsnb41K8Rnu3D4NE+vwB7S9aDtB4FJ0rpV0QP1d4tujLoCmWWhzxgPFm24VUatN sryEyyoBm31dwgEcLD/va2H2p1sUOiVHax+5ZJ9ksluv2/LxtqG6z09Og/GQg9EzhSzR lmBkvI5O0ABveyZs7C6QVwvTcLXIK/ulF2ul2j4BHevfAG8u14MUMlz1edOxlZuMjgQi k9r9BbEOLAGWBk/pigpsBAK4XpE3GK+5ULbaYo2yL8TTYNgGQGwESuE1kkz15yUSv7Aa aMYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776206617; x=1776811417; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Atp4lMX+KQb/XdR4N3FQnBkv+Ii6UBEhE3TAnqHAWWo=; b=cQoE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BiBASgA2sfrZTzFsAIFG4jEEvbIt4AQVmCbucXLCAMl22mTI6tse04 9pXghokXpOmOSjYqcTkL7ENYYNbb3nMZtef/kfDdumKZkvkzw== X-Received: by 2002:a05:7022:e1b:b0:128:d107:da0f with SMTP id a92af1059eb24-12c34ea9471mr11412638c88.10.1776206617403; Tue, 14 Apr 2026 15:43:37 -0700 (PDT) Received: from gui-host.Dlink ([177.33.104.190]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2dd32dc2827sm4650361eec.23.2026.04.14.15.43.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2026 15:43:36 -0700 (PDT) From: Guilherme Ivo Bozi To: Salih Erim , Conall O'Griofa , Jonathan Cameron , Michal Simek Cc: David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Guilherme Ivo Bozi Subject: [PATCH v3 3/3] iio: adc: xilinx-ams: refactor alarm mapping to table-driven approach Date: Tue, 14 Apr 2026 19:40:35 -0300 Message-ID: <20260414224245.8493-4-guilherme.bozi@usp.br> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260414224245.8493-1-guilherme.bozi@usp.br> References: <20260414224245.8493-1-guilherme.bozi@usp.br> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace multiple open-coded switch statements that map between scan_index, alarm bits, and register offsets with a centralized table-driven approach. Introduce a struct-based alarm_map to describe the relationship between scan indices and alarm offsets, and add a helper to translate scan_index to event IDs. This removes duplicated logic across ams_get_alarm_offset(), ams_event_to_channel(), and ams_get_alarm_mask(). The new approach improves maintainability, reduces code size, and makes it easier to extend or modify alarm mappings in the future, while preserving existing behavior. Signed-off-by: Guilherme Ivo Bozi --- drivers/iio/adc/xilinx-ams.c | 161 +++++++++++++---------------------- 1 file changed, 58 insertions(+), 103 deletions(-) diff --git a/drivers/iio/adc/xilinx-ams.c b/drivers/iio/adc/xilinx-ams.c index 70a1f97f6dad..2ce78883cead 100644 --- a/drivers/iio/adc/xilinx-ams.c +++ b/drivers/iio/adc/xilinx-ams.c @@ -102,6 +102,7 @@ #define AMS_PS_SEQ_MASK GENMASK(21, 0) #define AMS_PL_SEQ_MASK GENMASK_ULL(59, 22) =20 +#define AMS_ALARM_NONE 0x000 /* not a real offset */ #define AMS_ALARM_TEMP 0x140 #define AMS_ALARM_SUPPLY1 0x144 #define AMS_ALARM_SUPPLY2 0x148 @@ -763,9 +764,49 @@ static int ams_read_raw(struct iio_dev *indio_dev, } } =20 +struct ams_alarm_map { + enum ams_ps_pl_seq scan_index; + unsigned int base_offset; +}; + +/* + * Array index matches enum ams_alarm_bit. + * Entries with base_offset =3D=3D AMS_ALARM_NONE are unused/invalid + * (e.g. RESERVED) and must be skipped. + */ +static const struct ams_alarm_map alarm_map[] =3D { + [AMS_ALARM_BIT_TEMP] =3D { AMS_SEQ_TEMP, AMS_ALARM_TEMP }, + [AMS_ALARM_BIT_SUPPLY1] =3D { AMS_SEQ_SUPPLY1, AMS_ALARM_SUPPLY1 }, + [AMS_ALARM_BIT_SUPPLY2] =3D { AMS_SEQ_SUPPLY2, AMS_ALARM_SUPPLY2 }, + [AMS_ALARM_BIT_SUPPLY3] =3D { AMS_SEQ_SUPPLY3, AMS_ALARM_SUPPLY3 }, + [AMS_ALARM_BIT_SUPPLY4] =3D { AMS_SEQ_SUPPLY4, AMS_ALARM_SUPPLY4 }, + [AMS_ALARM_BIT_SUPPLY5] =3D { AMS_SEQ_SUPPLY5, AMS_ALARM_SUPPLY5 }, + [AMS_ALARM_BIT_SUPPLY6] =3D { AMS_SEQ_SUPPLY6, AMS_ALARM_SUPPLY6 }, + [AMS_ALARM_BIT_RESERVED] =3D { 0, AMS_ALARM_NONE }, + [AMS_ALARM_BIT_SUPPLY7] =3D { AMS_SEQ_SUPPLY7, AMS_ALARM_SUPPLY7 }, + [AMS_ALARM_BIT_SUPPLY8] =3D { AMS_SEQ_SUPPLY8, AMS_ALARM_SUPPLY8 }, + [AMS_ALARM_BIT_SUPPLY9] =3D { AMS_SEQ_SUPPLY9, AMS_ALARM_SUPPLY9 }, + [AMS_ALARM_BIT_SUPPLY10] =3D { AMS_SEQ_SUPPLY10, AMS_ALARM_SUPPLY10 }, + [AMS_ALARM_BIT_VCCAMS] =3D { AMS_SEQ_VCCAMS, AMS_ALARM_VCCAMS }, + [AMS_ALARM_BIT_TEMP_REMOTE] =3D { AMS_SEQ_TEMP_REMOTE, AMS_ALARM_TEMP_REM= OTE }, +}; + +static int ams_scan_index_to_event(int scan_index) +{ + for (unsigned int i =3D 0; i < ARRAY_SIZE(alarm_map); i++) { + if (alarm_map[i].base_offset =3D=3D AMS_ALARM_NONE) + continue; + + if (alarm_map[i].scan_index =3D=3D scan_index) + return i; + } + + return -EINVAL; +} + static int ams_get_alarm_offset(int scan_index, enum iio_event_direction d= ir) { - int offset; + int offset, event; =20 if (scan_index >=3D AMS_PS_SEQ_MAX) scan_index -=3D AMS_PS_SEQ_MAX; @@ -779,36 +820,11 @@ static int ams_get_alarm_offset(int scan_index, enum = iio_event_direction dir) offset =3D 0; } =20 - switch (scan_index) { - case AMS_SEQ_TEMP: - return AMS_ALARM_TEMP + offset; - case AMS_SEQ_SUPPLY1: - return AMS_ALARM_SUPPLY1 + offset; - case AMS_SEQ_SUPPLY2: - return AMS_ALARM_SUPPLY2 + offset; - case AMS_SEQ_SUPPLY3: - return AMS_ALARM_SUPPLY3 + offset; - case AMS_SEQ_SUPPLY4: - return AMS_ALARM_SUPPLY4 + offset; - case AMS_SEQ_SUPPLY5: - return AMS_ALARM_SUPPLY5 + offset; - case AMS_SEQ_SUPPLY6: - return AMS_ALARM_SUPPLY6 + offset; - case AMS_SEQ_SUPPLY7: - return AMS_ALARM_SUPPLY7 + offset; - case AMS_SEQ_SUPPLY8: - return AMS_ALARM_SUPPLY8 + offset; - case AMS_SEQ_SUPPLY9: - return AMS_ALARM_SUPPLY9 + offset; - case AMS_SEQ_SUPPLY10: - return AMS_ALARM_SUPPLY10 + offset; - case AMS_SEQ_VCCAMS: - return AMS_ALARM_VCCAMS + offset; - case AMS_SEQ_TEMP_REMOTE: - return AMS_ALARM_TEMP_REMOTE + offset; - default: + event =3D ams_scan_index_to_event(scan_index); + if (event < 0 || alarm_map[event].base_offset =3D=3D AMS_ALARM_NONE) return 0; - } + + return alarm_map[event].base_offset + offset; } =20 static const struct iio_chan_spec *ams_event_to_channel(struct iio_dev *de= v, @@ -821,49 +837,13 @@ static const struct iio_chan_spec *ams_event_to_chann= el(struct iio_dev *dev, scan_index =3D AMS_PS_SEQ_MAX; } =20 - switch (event) { - case AMS_ALARM_BIT_TEMP: - scan_index +=3D AMS_SEQ_TEMP; - break; - case AMS_ALARM_BIT_SUPPLY1: - scan_index +=3D AMS_SEQ_SUPPLY1; - break; - case AMS_ALARM_BIT_SUPPLY2: - scan_index +=3D AMS_SEQ_SUPPLY2; - break; - case AMS_ALARM_BIT_SUPPLY3: - scan_index +=3D AMS_SEQ_SUPPLY3; - break; - case AMS_ALARM_BIT_SUPPLY4: - scan_index +=3D AMS_SEQ_SUPPLY4; - break; - case AMS_ALARM_BIT_SUPPLY5: - scan_index +=3D AMS_SEQ_SUPPLY5; - break; - case AMS_ALARM_BIT_SUPPLY6: - scan_index +=3D AMS_SEQ_SUPPLY6; - break; - case AMS_ALARM_BIT_SUPPLY7: - scan_index +=3D AMS_SEQ_SUPPLY7; - break; - case AMS_ALARM_BIT_SUPPLY8: - scan_index +=3D AMS_SEQ_SUPPLY8; - break; - case AMS_ALARM_BIT_SUPPLY9: - scan_index +=3D AMS_SEQ_SUPPLY9; - break; - case AMS_ALARM_BIT_SUPPLY10: - scan_index +=3D AMS_SEQ_SUPPLY10; - break; - case AMS_ALARM_BIT_VCCAMS: - scan_index +=3D AMS_SEQ_VCCAMS; - break; - case AMS_ALARM_BIT_TEMP_REMOTE: - scan_index +=3D AMS_SEQ_TEMP_REMOTE; - break; - default: - break; - } + if (event < 0 || event >=3D ARRAY_SIZE(alarm_map)) + return NULL; + + if (alarm_map[event].base_offset =3D=3D AMS_ALARM_NONE) + return NULL; + + scan_index +=3D alarm_map[event].scan_index; =20 for (i =3D 0; i < dev->num_channels; i++) if (dev->channels[i].scan_index =3D=3D scan_index) @@ -877,43 +857,18 @@ static const struct iio_chan_spec *ams_event_to_chann= el(struct iio_dev *dev, =20 static int ams_get_alarm_mask(int scan_index) { - int bit =3D 0; + int bit =3D 0, event; =20 if (scan_index >=3D AMS_PS_SEQ_MAX) { bit =3D AMS_PL_ALARM_START; scan_index -=3D AMS_PS_SEQ_MAX; } =20 - switch (scan_index) { - case AMS_SEQ_TEMP: - return BIT(AMS_ALARM_BIT_TEMP + bit); - case AMS_SEQ_SUPPLY1: - return BIT(AMS_ALARM_BIT_SUPPLY1 + bit); - case AMS_SEQ_SUPPLY2: - return BIT(AMS_ALARM_BIT_SUPPLY2 + bit); - case AMS_SEQ_SUPPLY3: - return BIT(AMS_ALARM_BIT_SUPPLY3 + bit); - case AMS_SEQ_SUPPLY4: - return BIT(AMS_ALARM_BIT_SUPPLY4 + bit); - case AMS_SEQ_SUPPLY5: - return BIT(AMS_ALARM_BIT_SUPPLY5 + bit); - case AMS_SEQ_SUPPLY6: - return BIT(AMS_ALARM_BIT_SUPPLY6 + bit); - case AMS_SEQ_SUPPLY7: - return BIT(AMS_ALARM_BIT_SUPPLY7 + bit); - case AMS_SEQ_SUPPLY8: - return BIT(AMS_ALARM_BIT_SUPPLY8 + bit); - case AMS_SEQ_SUPPLY9: - return BIT(AMS_ALARM_BIT_SUPPLY9 + bit); - case AMS_SEQ_SUPPLY10: - return BIT(AMS_ALARM_BIT_SUPPLY10 + bit); - case AMS_SEQ_VCCAMS: - return BIT(AMS_ALARM_BIT_VCCAMS + bit); - case AMS_SEQ_TEMP_REMOTE: - return BIT(AMS_ALARM_BIT_TEMP_REMOTE + bit); - default: + event =3D ams_scan_index_to_event(scan_index); + if (event < 0) return 0; - } + + return BIT(event + bit); } =20 static int ams_read_event_config(struct iio_dev *indio_dev, --=20 2.47.3