From nobody Sat Nov 23 23:57:43 2024 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 66B0815B102 for ; Mon, 18 Nov 2024 11:20:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731928813; cv=none; b=QlbkxpSlvkUpj6d5yqWGCSEGCP6lage5Ejmej+Fdaa3oGUk9R2d6VkQ13r/UdLfZ5kb4PDYp5wHybiffvCGTqL6xyUB7R87tIYS8hACChoWjWd8drtmv89wPJ/5+WHEaiw2xJfmXc5l9Z8JldNNrmiW+FDQmgullIrdtkmB+1k4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731928813; c=relaxed/simple; bh=AwoRhhLqq1WWulNpltBkzcfiIf2PFCJos6WNxq0hVrQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=sDkNOgk6ncds/eQvuivFp1fF4dHlBVFmqZesjsI/28xQjkeYVhOjvQ8Nxlkbz5A51BQH1Qdi0ccyrR4CQIJNpkxIsfA2a1ajyozVxw76mWDNnKtk3M6p3mph4vQHPKl44kfcujLxA43zQfycOpt/D2U2SqVorAmVhcKRdNhtZrA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=JOYBhm1J; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="JOYBhm1J" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4315eeb2601so27964815e9.2 for ; Mon, 18 Nov 2024 03:20:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1731928810; x=1732533610; 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=X3clBHGKkxA0zoXbebM9YhLM0sVigriy0PiPrWq36rQ=; b=JOYBhm1JwrO6kj33x3PMmbsvhbWbpMVrybbedZKqPHoT8Ues4KvfMRCv2+HfHsQG9C 65im3kGl4N2PLCb8gZQ8r08XcSPSs+0aZBX7Fgj0o01Qtzuxdiv0dO0PganjiZnKFPp+ KI8SbCru0URDdwUtezbORHFbI+fKGuHLDjZOvbBWYqDasCuB6wFkfzFKnQiUGoeI0w+C i43NcUGsnw6/w6jCV1dSPgs/ENHlDxNR/OMPU7XgymskXmQAtN9SkaWKa49xP8ylifix oG+V/0Eiya6DZpDnL8yo3maghvF7yP8E521tg6gn1xq+CUn5DUT2P9CFW5+qftpxH1pW sEXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731928810; x=1732533610; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=X3clBHGKkxA0zoXbebM9YhLM0sVigriy0PiPrWq36rQ=; b=uEs12c+iJ6qMPP3dY3bE1v9utjhkWzY6LuGiFalIHVOhYwRBiR8T3cjLDukDeSboA5 CmQy6pWkNBK/BW6+QXKS4SKjwRMHL1YImam0OwhqTZmBCuv3uFiSwQatEnnpROcBIZT7 +xycsvlWXYd8Zd897EoU2XN+hlPsl2gj6KAgiLmBOpS7ZC/yKMCbSrDkS+HHGWcSlXp5 SUif2e4vsOdlxf9o78W0UYEx0xtlV4BxTwRW89X4gb8GljjeHXkrncxYyaXggCcTADak AUfSdqR6wY9H3fMsncVFNyKxPZeeZAmU+0CP9HYOtEmI54xp6VMQd7bd01rLpM7EqI01 tSlQ== X-Forwarded-Encrypted: i=1; AJvYcCXYtvhvI37TIkZT43L6oUgJwfk1oCE4VaFT6FFLuVC4NlllAK+D+hqkSC63f+91CA8dx8hPqqG88+q5leU=@vger.kernel.org X-Gm-Message-State: AOJu0YxTTbbtRRsHeeGHTqM5GmmSe3HBGlQ7u9PI6nkhgKgXNWsk/aSa gYeciqh5ORWnt8YRWDc3AxZ5dOVPRy4TS9DQVNrp3UoY+HH8BtkdlvkY7Qc3ynpHP/jsEz95TeZ F X-Google-Smtp-Source: AGHT+IGXx6oSXXKahYVS7gkCoNCEnTgspzDxDZpDMaegzj57Hq2dkpWqqADyAoNPoFCIkz4gnMfCgA== X-Received: by 2002:a05:600c:4f02:b0:42c:ba83:3f00 with SMTP id 5b1f17b1804b1-432df722e64mr105030275e9.1.1731928809838; Mon, 18 Nov 2024 03:20:09 -0800 (PST) Received: from localhost (p509159f1.dip0.t-ipconnect.de. [80.145.89.241]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432da2946a3sm153746835e9.35.2024.11.18.03.20.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2024 03:20:09 -0800 (PST) From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= To: Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] iio: adc: ad7124: Don't create more channels than the driver can handle Date: Mon, 18 Nov 2024 12:19:55 +0100 Message-ID: <26b8c3f2f204ca62520f0f3fe1e5173bc9ba3d30.1731404695.git.u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Developer-Signature: v=1; a=openpgp-sha256; l=1454; i=u.kleine-koenig@baylibre.com; h=from:subject:message-id; bh=AwoRhhLqq1WWulNpltBkzcfiIf2PFCJos6WNxq0hVrQ=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnOyLdLLX1bgBehhSBqSo1RG/wt0fy1Xa0OnO3t 5P0yZ3BdJ6JATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZzsi3QAKCRCPgPtYfRL+ TuQCB/9J5DoSs+zCfIygvDXx/x724zk6SLAdonurWUO3DFybWhFWJ78tYa02HiyRR46yy5x2e1B YOdyqu/UCfAZ2vfxU3jqfOvApBq26/7aqJvLL9ke/h1m2dini8wzqJv23I/JCSVdNOt3jK8cpUd u66Xf6y2CLIuR/IwLDNQiQvZTj66ObOIvAx9oCjhCL1sGtS/vfai7Qb2HYazZoS5vcrKXjf/3IN x9sw1hwiqGGNggSBGm8BoBx/FyXeg89wYgsCJQvml0Zl0otFEC6emOgFlZEHia7KBOlt/EMORpP tZ8zfihVaEk0AGvz2joOdcRIh9NB9dyYPzcixaC0iSnSqfqu X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 Content-Transfer-Encoding: quoted-printable The ad7124-4 and ad7124-8 both support 16 channel registers and assigns each channel defined in dt statically such a register. While the driver could be a bit more clever about this, it currently isn't and specifying more than 16 channels yields broken behaviour. So just refuse to bind in this situation. Fixes: b3af341bbd96 ("iio: adc: Add ad7124 support") Signed-off-by: Uwe Kleine-K=C3=B6nig --- drivers/iio/adc/ad7124.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index 749304d38415..4de69bb8653a 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -821,6 +821,16 @@ static int ad7124_parse_channel_config(struct iio_dev = *indio_dev, if (!st->num_channels) return dev_err_probe(dev, -ENODEV, "no channel children\n"); =20 + /* + * The driver assigns each logical channel defined in the device tree + * statically one channel register. So only accept 16 such logical + * channels to not treat CONFIG_0 (i.e. the register following + * CHANNEL_15) as an additional channel register. The driver could be + * improved to lift this limitation. + */ + if (st->num_channels > AD7124_MAX_CHANNELS) + return dev_err_probe(dev, -EINVAL, "Too many channels defined\n"); + chan =3D devm_kcalloc(indio_dev->dev.parent, st->num_channels, sizeof(*chan), GFP_KERNEL); if (!chan) --=20 2.45.2 From nobody Sat Nov 23 23:57:43 2024 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 D491A196C6C for ; Mon, 18 Nov 2024 11:20:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731928814; cv=none; b=jtnULkITPn+zoUg5qseE+E6Yjpe2/+s9zmFfkYX1BzDf8KCllyy6P71vRjZCTMewuLvsWTSO24Okb5siLqZPgE4mGTlNw2rEAs7Oro9mWPgQgqu/0DYY5edeNIAQQ/tnIABwt+9cKwk8T29i0OWIY9YYRDYoKCgcujVHoc3e3AQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731928814; c=relaxed/simple; bh=gkebMvifaHHqYyg3wApsDhnNPWqEu8s40kTTe7mYLOw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QAJt4flP9S3XlU/LzkdIINHHsExnR7PCjqhXbmjINB+PycWCXRmfxISn0xxYwis+i+oW2whcLaPO9skB8586X9KSDybejWwU7tUmeTahcvuXLE70c5bZoKQ+/XseeGXDWqgyTmwKvaLALRjeVYhhZs8n+fUA7BTyVQFOjh85+YA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=aOveGGGv; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="aOveGGGv" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-4315e62afe0so37242595e9.1 for ; Mon, 18 Nov 2024 03:20:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1731928811; x=1732533611; 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=Asv+1zcPSdtmvj/jsSINOJXF2sibgY7PXKflhCuFup4=; b=aOveGGGvoNZ0O1eTy9ZV8x1nd207pxAH+TQTN6Zr7InPAIMK5jFI4ZOPj9Oq6VxefQ 3ZXXz+MKbFbdEHSXyIDW56SfYa/yl4SDfca+i33F0jwmTX77XAihv+jJAOvsLNKP0XSR Y4NKk2KnavF0JeO2b8q69UmOY8ITyzTUGSQA1SjVWhSc3eWC9hQUP1u5ANXMPgzI4lDM 99hgdWeejbOD6oPxKMXk0EL5EL7O4LeCiIA1LKkUO84Yw3sI4Bv/O1aEg9Gx8uPpaXTC /LdjOy6/OnKO0Kq2+FaDhSTvnXd1/klOg26wAeh++dkpOplJ7PNsmzHB9VS+puucCxsR ON0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731928811; x=1732533611; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Asv+1zcPSdtmvj/jsSINOJXF2sibgY7PXKflhCuFup4=; b=lFid7vH06VasdvCxtqMXbcGY6Ne2JOOeG4M0GVsGvw6eQ2bEU5uzaDEt4DszAvjT2q sryEgPEGoHPjamBK57zQewpnKI03YISL74NNC1wOMwuKUUVQxAZX3ON7yLPszdn5469j LGw+6UZSGjrcdtGYxJVUpPpEyixQ/rk1LfpBD6WCQtopHb+TxusEAKUi2KHpE1Wj+hkY fwbR5ZY2dAIsi9vABfTsNWLJkwI+kepm/zVpeprOux8ixNQNBtB16WwkDhtqVJtXyBJF m9opKyTwxuGnw4i+sRR0CeGZr07kTlvV0z3CTSmg+yaNxAhH5kUqEV+sipxQtopUBkSX IooQ== X-Forwarded-Encrypted: i=1; AJvYcCVEojEmuIdVQ7r2jAsZZh4upb9nj+BRqTADiYcXv9CbGyi8oqWv4159YjPnStt1HAofFDoKXpIrf/KYQkg=@vger.kernel.org X-Gm-Message-State: AOJu0YyucwDjCX9y/EQvt0gc1EOrPQsMu29uZiTA91ttFc8squJgvw/i Q7ME9TkgV/z/vMZQiFNrcfsFHpBGOYLb02AHwLo559mHF1BYrW7gzNZlPywzg58= X-Google-Smtp-Source: AGHT+IG8WB54uuUl8Kw673I1UaAQsV/m9AUVgJQSJZ7e5WATQjmgWaIOnj9DxIM7Tr7wJGPWfVKJVg== X-Received: by 2002:a05:6000:1544:b0:37d:393a:97c5 with SMTP id ffacd0b85a97d-38225a059a1mr9276421f8f.28.1731928811238; Mon, 18 Nov 2024 03:20:11 -0800 (PST) Received: from localhost (p509159f1.dip0.t-ipconnect.de. [80.145.89.241]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3824af06e02sm517377f8f.32.2024.11.18.03.20.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2024 03:20:10 -0800 (PST) From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= To: Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] iio: adc: ad7124: Refuse invalid input specifiers Date: Mon, 18 Nov 2024 12:19:56 +0100 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Developer-Signature: v=1; a=openpgp-sha256; l=2144; i=u.kleine-koenig@baylibre.com; h=from:subject:message-id; bh=gkebMvifaHHqYyg3wApsDhnNPWqEu8s40kTTe7mYLOw=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBnOyLfGsCIByaVZGarVHFl3mkS+B0/j1LFfwxB1 8fYuZZ1IT+JATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZzsi3wAKCRCPgPtYfRL+ Ts+bCACIz+mk+hqNkhtGHLZ6hP8yd0qzFgyJFsUKKkzO536lI8s6cM4YJbIpKlRBwMrlDULO0Nk W6FItS/1LvFpl35M6Ea3q8CiZhf4W7CRKrQqxGzpGYis/jBD79JQXQAIUm5211E7yRJz1adqSwA +sKLNn1aZKg0uCJmQY25DtAnXcm1L8oVeXxLYp/cYHJ7bec5ea8Kn/2aL8RhTyccE7K07BF+x2k Tw2lXhP0I7yR12eLck5B7bCtA+aOjzkGhQ4Z/3vejJNjkOyq9NM9+tgTMzEnU7yq5DRCgN8g7Hm wL9JCgOcR39Zp5/qVMyWBn7ODUZyjNXbpS7LEuo1p/VOZ2+B X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 Content-Transfer-Encoding: quoted-printable The ad7124-4 has 8 analog inputs; the input select values 8 to 15 are reserved and not to be used. These are fine for ad7124-8. For both ad7124-4 and ad7124-8 values bigger than 15 are internal channels that might appear as inputs in the channels specified in the device description according to the description of commit f1794fd7bdf7 ("iio: adc: ad7124: Remove input number limitation"), values bigger than 31 don't fit into the respective register bit field and the driver masked them to smaller values. Check for these invalid input specifiers and fail to probe if one is found. Fixes: f1794fd7bdf7 ("iio: adc: ad7124: Remove input number limitation") Signed-off-by: Uwe Kleine-K=C3=B6nig --- drivers/iio/adc/ad7124.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index 4de69bb8653a..bfeec59e33ba 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -807,6 +807,19 @@ static int ad7124_check_chip_id(struct ad7124_state *s= t) return 0; } =20 +/* + * Input specifiers 8 - 15 are explicitly reserved for ad7124-4 + * while they are fine for ad7124-8. Values above 31 don't fit + * into the register field and so are invalid for sure. + */ +static bool ad7124_valid_input_select(unsigned int ain, const struct ad712= 4_chip_info *info) +{ + if (ain >=3D info->num_inputs && ain < 16) + return false; + + return ain <=3D FIELD_MAX(AD7124_CHANNEL_AINM_MSK); +} + static int ad7124_parse_channel_config(struct iio_dev *indio_dev, struct device *dev) { @@ -859,6 +872,11 @@ static int ad7124_parse_channel_config(struct iio_dev = *indio_dev, if (ret) return ret; =20 + if (!ad7124_valid_input_select(ain[0], st->chip_info) || + !ad7124_valid_input_select(ain[1], st->chip_info)) + return dev_err_probe(dev, -EINVAL, + "diff-channels property of %pfwP contains invalid data\n", child= ); + st->channels[channel].nr =3D channel; st->channels[channel].ain =3D AD7124_CHANNEL_AINP(ain[0]) | AD7124_CHANNEL_AINM(ain[1]); --=20 2.45.2