From nobody Thu Sep 24 23:33:50 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79C6A50276E; Fri, 18 Sep 2026 15:23:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744991; cv=none; b=Uo/pSGjj/SUZEExSPUBrlozj4oBc4ZyPMfwYR8B194ZzWVEtgo6n9F8R/GPs+tXC2WJNAGa+rxve25Niot8VlFGu3LhijHuEeH4bFWZ1T+c6BInlnrq04M19lzfaGDBAsdbGDKcLGIsTzKbL84xVJ8UxFgpDd+eX6PPAqoKw8z8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744991; c=relaxed/simple; bh=XzJjoVhnb/s7cdDvuM4wM0NdbOkb23/JUb5yHGelLtw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=klZ+2AOgonzAvm0OPNPhR4VB8c9VPIpouySv5vr7iNneLgIOFefUJxUu4f8+y9V2axMYHWcMCSSIT4S0Dtvya7jlb9h++L5m1KhAOeazCSGjprAyIF4kmhzWoYWped1j7YiZktxqlfAT8ucPufqJb3du5f9DJYgtVIJ3KmQh5Yw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 2DE58200B28; Fri, 18 Sep 2026 17:23:01 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x7aQX-001awp-0G; Fri, 18 Sep 2026 17:23:01 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 17:22:47 +0200 Subject: [PATCH v4 1/9] spi: spidev_test: include tools/include 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20260918-spi-sun4i-spidev_test-v4-1-5ffacb1a4ebd@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Kleine-Budde , kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1720; i=jre@pengutronix.de; h=from:subject:message-id; bh=XzJjoVhnb/s7cdDvuM4wM0NdbOkb23/JUb5yHGelLtw=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaGx6w68u3rc1F6Slqkd63L7r3bSlccLjc2or/0L6se f7mBdfudpSyMIhxMciKKbLEqskpCBn7XzertIuFmcPKBDKEgYtTACayp47hn66Vw50TEw+yqBjG b1sg6jPBuOe43soJ9RMsti1brcgoPYuRYWe88f6jlZ8zfC4E6glfzdJUDzp0/vLEKYlzn9gHtun JMAAA X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 Add tools/include to include paths to allow using macro helpers. Use ARRAY_SIZE() from tools/include/linux/kernel.h instead of maintaining a copy. Remove the obsolete comment with cross-compile instructions from the header since spidev_test is integrated with the tools buildsystem since commit f325b73dc4db ("spi: tools: move to tools buildsystem"). Signed-off-by: Jonas Rebmann --- tools/spi/Makefile | 2 +- tools/spi/spidev_test.c | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/tools/spi/Makefile b/tools/spi/Makefile index 7fccd245a535..8cf21ff8cd03 100644 --- a/tools/spi/Makefile +++ b/tools/spi/Makefile @@ -12,7 +12,7 @@ endif # (this improves performance and avoids hard-to-debug behaviour); MAKEFLAGS +=3D -r =20 -CFLAGS +=3D -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include +CFLAGS +=3D -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include -I$(srctree)/too= ls/include =20 ALL_TARGETS :=3D spidev_test spidev_fdx ALL_PROGRAMS :=3D $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS)) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index f2135d619a0b..bd5ee7a6ac82 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -4,8 +4,6 @@ * * Copyright (c) 2007 MontaVista Software, Inc. * Copyright (c) 2007 Anton Vorontsov - * - * Cross-compile with cross-gcc -I/path/to/cross-kernel/include */ =20 #include @@ -22,8 +20,7 @@ #include #include #include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#include =20 static void pabort(const char *s) { --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:33:50 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8B2C502776; Fri, 18 Sep 2026 15:23:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744990; cv=none; b=UlykrqNy3rFJkI32YjE855LGJiOF7NoPw12gBVnTQFmktYkAirXpnGK6iTwka14SoajnVG5abDKIPUXdA5Ks616DuNg8Yf/vYx9W0A8WIEN5wZBr+WWJAwfpp9Fe+kVEFhMonKu3bkutsx2bTQM3pkEJzjF8D87y+gt7li5icU8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744990; c=relaxed/simple; bh=fL7MQlNOKmTkLIyBArEuxwuAjlxrPp4JGa+0SDpk/P8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=n8kdxzhD9DNtPkJ0ta2WowBWc2LstirJafN3ghoMvvzg5Ao7Ub9aibtQpOemPCvXAiGI5xitZ5CjNoyF7l7RaufscMivJtLQJS8MnVHG0eZvjES5UzTmwHOJ+uHlocqHCalG/DEunrbW913g+8is4SF2TOKJq8l4nZU46H4dhZA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 3AA822018B6; Fri, 18 Sep 2026 17:23:01 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x7aQX-001awp-0Q; Fri, 18 Sep 2026 17:23:01 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 17:22:48 +0200 Subject: [PATCH v4 2/9] spi: spidev_test: clarify help text for --size 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20260918-spi-sun4i-spidev_test-v4-2-5ffacb1a4ebd@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Kleine-Budde , kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1190; i=jre@pengutronix.de; h=from:subject:message-id; bh=fL7MQlNOKmTkLIyBArEuxwuAjlxrPp4JGa+0SDpk/P8=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaGx6o8seuJ/TcRvMj5+TEvJ/pf7ZQ7fBg7DnnL3Cp+ JO0/y/hjlIWBjEuBlkxRZZYNTkFIWP/62aVdrEwc1iZQIYwcHEKwEQ2tzIy/J/YH+3K9bHZfqpq xe2dU7Zc7s2WWmi7pzZaMVOEaabVXkaGR7GPnk28Gn9yndsR3gNZT851CgRvXd8oIL71OKOfbo4 5EwA= X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 It is slightly confusing that spidev_test by default transmits the predetermined message default_tx but random bytes given the --size parameter. Clarify this in the help text. While at it, make the -p help text consistently lowercase. Signed-off-by: Jonas Rebmann --- tools/spi/spidev_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index bd5ee7a6ac82..f5ab67236fac 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -191,8 +191,8 @@ static void print_usage(const char *prog) "data:\n" " -i --input input data from a file (e.g. \"test.bin\")\n" " -o --output output data to a file (e.g. \"results.bin\")\n" - " -p Send data (e.g. \"1234\\xde\\xad\")\n" - " -S --size transfer size\n" + " -p send data (e.g. \"1234\\xde\\xad\")\n" + " -S --size transfer the given number of random bytes\n" " -I --iter iterations\n" "additional parameters:\n" " -b --bpw bits per word\n" --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:33:50 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79B8250276C; Fri, 18 Sep 2026 15:23:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744991; cv=none; b=Grpma04CpfgIvj4IhXJgpJerpzJoH2t0RNL/So0XLOKkaSCuuOCOik9IKsEZAe11yGvnaPDFydQSD9ohsyBlERGVX13zu3qHNUXW/gzXRANYAuEsltKsNcYLdNyV4HDdAXdac6PNg6ilIOPQp7KROBpjk02d1CAPJCPAkIgYOMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744991; c=relaxed/simple; bh=7YVXq8vXFh9JGK2OgAlbDJkcSwXJMi81LEDdKZRlYUY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rcHkRolyfaLWAawYy3qGjvSuYV44+wywz+LSMaegff/Mij9MCxao/krzkoAn59fhwTUDpN/IgOtsxsTrP78zXXNL5BTbLxF+KPV1f8ULt7srDIRVybG9h7M77cYq850P2a5Ojg6zz1/P7cIGBD3Bl8xyY7dzNKUu+8Zru3n5K5Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 4451D201C37; Fri, 18 Sep 2026 17:23:01 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x7aQX-001awp-0Y; Fri, 18 Sep 2026 17:23:01 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 17:22:49 +0200 Subject: [PATCH v4 3/9] spi: spidev_test: add compare mode 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20260918-spi-sun4i-spidev_test-v4-3-5ffacb1a4ebd@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Kleine-Budde , kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3137; i=jre@pengutronix.de; h=from:subject:message-id; bh=n8XhVZLSFhOpD3ZnWIq+soYNGBUAkwZyn6fB56crf98=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaGx7oW8m8fe4RT7USv2OZdmdUJp9b8qejR/DD4vP/L 01c9IO7oaOUhUGMi0FWTJElVk1OQcjY/7pZpV0szBxWJpAhDFycAjCRtImMDLO3L5re7qO7J+hV lHjfrcdHWc4GvLM9ub9IVDBq1xW5jtUM/wwezyqb+WzpohlKXF8XSmv0z9A03v5gVv/WguXnpgZ /yWUGAA== X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 From: Marc Kleine-Budde Check whether the received data correspond to the transferred data but without enabling loopback mode in the controller. This is for testing with TX physically bridged to RX which is useful if a loopback mode is unavailable or insufficient. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann --- tools/spi/spidev_test.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index f5ab67236fac..7993eae88c44 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -44,6 +44,7 @@ static int verbose; static int transfer_size; static int iterations; static int interval =3D 5; /* interval in seconds for showing transfer rat= e */ +static int compare; =20 static uint8_t default_tx[] =3D { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, @@ -171,13 +172,14 @@ static void transfer(int fd, uint8_t const *tx, uint8= _t const *rx, size_t len) =20 static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNORSZbdilopsvw]\n", prog); + printf("Usage: %s [-2348CDFHILMNORSZbcdilopsvw]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" " -d --delay delay (usec)\n" " -w --word-delay word delay (usec)\n" " -l --loop loopback\n" + " -c --compare compare RX'ed and TX'ed data\n" "spi mode:\n" " -H --cpha clock phase\n" " -O --cpol clock polarity\n" @@ -215,6 +217,7 @@ static void parse_opts(int argc, char *argv[]) { "delay", 1, 0, 'd' }, { "word-delay", 1, 0, 'w' }, { "loop", 0, 0, 'l' }, + { "compare", 0, 0, 'c' }, { "cpha", 0, 0, 'H' }, { "cpol", 0, 0, 'O' }, { "rx-cpha-flip", 0, 0, 'F' }, @@ -238,7 +241,7 @@ static void parse_opts(int argc, char *argv[]) }; int c; =20 - c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lHOLC3ZFMNR248p:vS:I:", + c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lcHOLC3ZFMNR248p:vS:I:", lopts, NULL); =20 if (c =3D=3D -1) @@ -269,6 +272,9 @@ static void parse_opts(int argc, char *argv[]) case 'l': mode |=3D SPI_LOOP; break; + case 'c': + compare =3D 1; + break; case 'H': mode |=3D SPI_CPHA; break; @@ -425,7 +431,7 @@ static void transfer_buf(int fd, int len) _write_count +=3D len; _read_count +=3D len; =20 - if (mode & SPI_LOOP) { + if (compare || mode & SPI_LOOP) { if (memcmp(tx, rx, len)) { fprintf(stderr, "transfer error !\n"); hex_dump(tx, len, 32, "TX"); @@ -449,6 +455,9 @@ int main(int argc, char *argv[]) if (input_tx && input_file) pabort("only one of -p and --input may be selected"); =20 + if (compare && mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) + pabort("-c (--compare) conflicts with -2 (--dual), -4 (--quad) or -8 (--= octal)"); + fd =3D open(device, O_RDWR); if (fd < 0) pabort("can't open device"); --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:33:50 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 602A9502D58; Fri, 18 Sep 2026 15:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744994; cv=none; b=hR/AUz4KXcqNR5rG3H4jabllLpXvw/vPSjcRcdlSPEVrEuiiHdn56K58CMyr/IpguEhhLriIzPWihYz/R5BNtv9d1yU5rZwwGXl2t3jte0fhEwkkPvpVpWjjHbK0PBRS179oIMRU3zx+zT4HLd5duvLlDZUswJ6wJCkbFu+gUxk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744994; c=relaxed/simple; bh=r7/PvjZhUjg7QIW1LcYyaezUQI54Fsi3GwiYNF6HJlI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=g801WV45f720+PX8mdS7UDmSenmSCG2U/83rscenRGUjmEaJKaO7o6256uWSWre4P7nFOdS3YpueDrlBrSKHYmKjF1ap2jmHegqvcJg69WlSYw1IPNgW4HJrJ2pgKyEQ/6bbnGub5WRAMz1/8apJM1D/jGbcRqTJa/n8KzoE/gg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 5275E201D7D; Fri, 18 Sep 2026 17:23:01 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x7aQX-001awp-0i; Fri, 18 Sep 2026 17:23:01 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 17:22:50 +0200 Subject: [PATCH v4 4/9] spi: spidev_test: allow disabling rx or tx buffers 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20260918-spi-sun4i-spidev_test-v4-4-5ffacb1a4ebd@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Kleine-Budde , kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=6766; i=jre@pengutronix.de; h=from:subject:message-id; bh=4II9rXw8bFinhNlsnofkBsyj52J+XvYRnLbK1yMTRyk=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaGx507KZPxJSYBYuEdJmPGO/UNn+V3mOS9NJOmt/nx 3zH4PcOHaUsDGJcDLJiiiyxanIKQsb+180q7WJh5rAygQxh4OIUgIn0HmNkuPP6jePqGrOwiP09 1a/mrnq3w+qpgIUuz9ctm/tSkwUZbBj+J969fyB49d72m+9FX0XNbQ9xPXVnU41i4Hvfbx2vX77 ZxgAA X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 From: Marc Kleine-Budde Allow not providing rx or tx buffers. This is useful to check if drivers that don't use SPI_CONTROLLER_MUST_RX (or -TX respectively) handle their operations correctly without a buffer. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann --- tools/spi/spidev_test.c | 90 +++++++++++++++++++++++++++++++++------------= ---- 1 file changed, 61 insertions(+), 29 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 7993eae88c44..906f9997b10a 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -45,6 +45,8 @@ static int transfer_size; static int iterations; static int interval =3D 5; /* interval in seconds for showing transfer rat= e */ static int compare; +static int do_tx =3D 1, do_rx =3D 1; +static int input_choices; =20 static uint8_t default_tx[] =3D { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, @@ -151,7 +153,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t= const *rx, size_t len) if (ret < 1) pabort("can't send spi message"); =20 - if (verbose) + if (verbose && tx) hex_dump(tx, len, 32, "TX"); =20 if (output_file) { @@ -166,13 +168,13 @@ static void transfer(int fd, uint8_t const *tx, uint8= _t const *rx, size_t len) close(out_fd); } =20 - if (verbose) + if (verbose && rx) hex_dump(rx, len, 32, "RX"); } =20 static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNORSZbcdilopsvw]\n", prog); + printf("Usage: %s [-2348CDFHILMNORSZbcdiloprstvw]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" @@ -180,6 +182,8 @@ static void print_usage(const char *prog) " -w --word-delay word delay (usec)\n" " -l --loop loopback\n" " -c --compare compare RX'ed and TX'ed data\n" + " -t --no-tx don't send data\n" + " -r --no-rx don't receive data\n" "spi mode:\n" " -H --cpha clock phase\n" " -O --cpol clock polarity\n" @@ -218,6 +222,8 @@ static void parse_opts(int argc, char *argv[]) { "word-delay", 1, 0, 'w' }, { "loop", 0, 0, 'l' }, { "compare", 0, 0, 'c' }, + { "no-tx", 0, 0, 't' }, + { "no-rx", 0, 0, 'r' }, { "cpha", 0, 0, 'H' }, { "cpol", 0, 0, 'O' }, { "rx-cpha-flip", 0, 0, 'F' }, @@ -241,7 +247,7 @@ static void parse_opts(int argc, char *argv[]) }; int c; =20 - c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lcHOLC3ZFMNR248p:vS:I:", + c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:vS:I:", lopts, NULL); =20 if (c =3D=3D -1) @@ -265,6 +271,7 @@ static void parse_opts(int argc, char *argv[]) break; case 'i': input_file =3D optarg; + input_choices++; break; case 'o': output_file =3D optarg; @@ -275,6 +282,13 @@ static void parse_opts(int argc, char *argv[]) case 'c': compare =3D 1; break; + case 't': + do_tx =3D 0; + input_choices++; + break; + case 'r': + do_rx =3D 0; + break; case 'H': mode |=3D SPI_CPHA; break; @@ -310,6 +324,7 @@ static void parse_opts(int argc, char *argv[]) break; case 'p': input_tx =3D optarg; + input_choices++; break; case '2': mode |=3D SPI_TX_DUAL; @@ -322,6 +337,7 @@ static void parse_opts(int argc, char *argv[]) break; case 'S': transfer_size =3D atoi(optarg); + input_choices++; break; case 'I': iterations =3D atoi(optarg); @@ -344,15 +360,17 @@ static void transfer_escaped_string(int fd, char *str) { size_t size =3D strlen(str); uint8_t *tx; - uint8_t *rx; + uint8_t *rx =3D NULL; =20 tx =3D malloc(size); if (!tx) pabort("can't allocate tx buffer"); =20 - rx =3D malloc(size); - if (!rx) - pabort("can't allocate rx buffer"); + if (do_rx) { + rx =3D malloc(size); + if (!rx) + pabort("can't allocate rx buffer"); + } =20 size =3D unescape((char *)tx, str, size); transfer(fd, tx, rx, size); @@ -366,7 +384,7 @@ static void transfer_file(int fd, char *filename) struct stat sb; int tx_fd; uint8_t *tx; - uint8_t *rx; + uint8_t *rx =3D NULL; =20 if (stat(filename, &sb) =3D=3D -1) pabort("can't stat input file"); @@ -379,9 +397,12 @@ static void transfer_file(int fd, char *filename) if (!tx) pabort("can't allocate tx buffer"); =20 - rx =3D malloc(sb.st_size); - if (!rx) - pabort("can't allocate rx buffer"); + + if (do_rx) { + rx =3D malloc(sb.st_size); + if (!rx) + pabort("can't allocate rx buffer"); + } =20 bytes =3D read(tx_fd, tx, sb.st_size); if (bytes !=3D sb.st_size) @@ -412,26 +433,31 @@ static void show_transfer_rate(void) =20 static void transfer_buf(int fd, int len) { - uint8_t *tx; - uint8_t *rx; + uint8_t *tx =3D NULL; + uint8_t *rx =3D NULL; int i; =20 - tx =3D malloc(len); - if (!tx) - pabort("can't allocate tx buffer"); - for (i =3D 0; i < len; i++) - tx[i] =3D random(); + if (do_tx) { + tx =3D malloc(len); + if (!tx) + pabort("can't allocate tx buffer"); + for (i =3D 0; i < len; i++) + tx[i] =3D random(); + } =20 - rx =3D malloc(len); - if (!rx) - pabort("can't allocate rx buffer"); + if (do_rx) { + rx =3D malloc(len); + if (!rx) + pabort("can't allocate rx buffer"); + } =20 transfer(fd, tx, rx, len); + if (do_tx) + _write_count +=3D len; + if (do_rx) + _read_count +=3D len; =20 - _write_count +=3D len; - _read_count +=3D len; - - if (compare || mode & SPI_LOOP) { + if (tx && rx && (compare || mode & SPI_LOOP)) { if (memcmp(tx, rx, len)) { fprintf(stderr, "transfer error !\n"); hex_dump(tx, len, 32, "TX"); @@ -452,8 +478,14 @@ int main(int argc, char *argv[]) =20 parse_opts(argc, argv); =20 - if (input_tx && input_file) - pabort("only one of -p and --input may be selected"); + if (input_choices > 1) + pabort("only one of -S (--size), -p, -i (--input), -t (--no-tx) may be s= elected"); + + if (compare && (!do_tx || !do_rx)) + pabort("-c (--compare) conflicts with -t (--no-tx) or -r (--no-rx)"); + + if (!do_rx && output_file) + pabort("-r (--no-rx) conflicts with -o (--output)"); =20 if (compare && mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) pabort("-c (--compare) conflicts with -2 (--dual), -4 (--quad) or -8 (--= octal)"); @@ -532,7 +564,7 @@ int main(int argc, char *argv[]) printf("total: tx %.1fKB, rx %.1fKB\n", _write_count/1024.0, _read_count/1024.0); } else - transfer(fd, default_tx, default_rx, sizeof(default_tx)); + transfer(fd, do_tx ? default_tx : NULL, do_rx ? default_rx : NULL, sizeo= f(default_tx)); =20 close(fd); =20 --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:33:50 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79AC650276B; Fri, 18 Sep 2026 15:23:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744992; cv=none; b=Y32vOUHJyouSGuh/HVgrTHA7A252rIrfShHgbWZqyjdtnwH/gdr4rdlZib34VpzNPFeDjkgB8Q6a4rfN92GpfVytbTAnewPX1rqix/09ae6sTwjto6xyG536J2pI0WJD5bRs5zTVyNShy9CAOk7CBqs2N93WNTr2gSUQu8LFA5I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744992; c=relaxed/simple; bh=8xDm8HIc3T8dnoSW91nqDhioHpyQRv2vW2Srh62u+Mo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OjXeFTCYaqsGX88yIMej29FImHy6VyaMQm/Cvb67C+PQn9P8b5yR/ueWacpezN1bztoqKRFGlggvQ8gpSMqeFyZ6P3vcvVnNPn1Vb+wfu2yJfXw3FUgOlRH9HUOaEs6VFJ8wwcQGM0a8+0zBGylgSIBevViHoQ42jXJY0QE5yEA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 5B9C8201DA2; Fri, 18 Sep 2026 17:23:01 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x7aQX-001awp-0s; Fri, 18 Sep 2026 17:23:01 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 17:22:51 +0200 Subject: [PATCH v4 5/9] spi: spidev_test: don't send 0x0 or 0xff 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20260918-spi-sun4i-spidev_test-v4-5-5ffacb1a4ebd@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Kleine-Budde , kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3449; i=jre@pengutronix.de; h=from:subject:message-id; bh=Gp0oEcidUjVPV8uP6NxV4x1D1YLZkYqm5Y471bag4y4=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaGx6U4c4RkLrq5tMUx3f5Yk+q97mLRouyXV14zXRy+ 4TQU+yMHaUsDGJcDLJiiiyxanIKQsb+180q7WJh5rAygQxh4OIUgIkEyzAy3Puhe7L+1JX5p1hi CpVdjTiSmVz/JE3N3Hnrpvqn+59lkxl+MkaUf/59Xutemcw9NgamvHM3ds38n6ZxmaOw+9TSdK5 fvAA= X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 From: Marc Kleine-Budde Particularly when using compare mode, if the controller fails to transfer any data, asserting on a read of 0x00 or 0xff may lead a false negative test, indicating a byte was successfully transferred when the values simply originate from the pull-up or pull-down of RX. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann --- tools/spi/spidev_test.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 906f9997b10a..c34601558646 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -45,6 +45,7 @@ static int transfer_size; static int iterations; static int interval =3D 5; /* interval in seconds for showing transfer rat= e */ static int compare; +static int nonzero; static int do_tx =3D 1, do_rx =3D 1; static int input_choices; =20 @@ -174,7 +175,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t= const *rx, size_t len) =20 static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNORSZbcdiloprstvw]\n", prog); + printf("Usage: %s [-2348CDFHILMNORSZbcdiloprstvwz]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" @@ -198,6 +199,7 @@ static void print_usage(const char *prog) " -i --input input data from a file (e.g. \"test.bin\")\n" " -o --output output data to a file (e.g. \"results.bin\")\n" " -p send data (e.g. \"1234\\xde\\xad\")\n" + " -z --nonzero don't send 0x00 or 0xff bytes\n" " -S --size transfer the given number of random bytes\n" " -I --iter iterations\n" "additional parameters:\n" @@ -235,6 +237,7 @@ static void parse_opts(int argc, char *argv[]) { "input", 1, 0, 'i' }, { "output", 1, 0, 'o' }, { "size", 1, 0, 'S' }, + { "nonzero", 0, 0, 'z' }, { "iter", 1, 0, 'I' }, { "bpw", 1, 0, 'b' }, { "lsb", 0, 0, 'L' }, @@ -247,7 +250,7 @@ static void parse_opts(int argc, char *argv[]) }; int c; =20 - c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:vS:I:", + c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:vS:zI:", lopts, NULL); =20 if (c =3D=3D -1) @@ -339,6 +342,9 @@ static void parse_opts(int argc, char *argv[]) transfer_size =3D atoi(optarg); input_choices++; break; + case 'z': + nonzero =3D 1; + break; case 'I': iterations =3D atoi(optarg); break; @@ -441,8 +447,11 @@ static void transfer_buf(int fd, int len) tx =3D malloc(len); if (!tx) pabort("can't allocate tx buffer"); - for (i =3D 0; i < len; i++) - tx[i] =3D random(); + for (i =3D 0; i < len; i++) { + do + tx[i] =3D random(); + while (nonzero && (tx[i] =3D=3D 0x0 || tx[i] =3D=3D 0xff)); + } } =20 if (do_rx) { @@ -481,6 +490,9 @@ int main(int argc, char *argv[]) if (input_choices > 1) pabort("only one of -S (--size), -p, -i (--input), -t (--no-tx) may be s= elected"); =20 + if (nonzero && !transfer_size) + pabort("-z (--nonzero) is only implemented for -S (--size)"); + if (compare && (!do_tx || !do_rx)) pabort("-c (--compare) conflicts with -t (--no-tx) or -r (--no-rx)"); =20 --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:33:50 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 61106502D59; Fri, 18 Sep 2026 15:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744994; cv=none; b=tnLDlEA3jQd+j5kq0mLgmq0trNrd8dYNzeDyhwhOkseEKaPQzedeZk94+8+PnQr8FZPgGGeUhW8fjq4qObYdA3kgBrMKF+QcQXXfHP3XxxuPcPiPv/Twx+2CSCKjGsw3e0PLgzbnONIlT59xUrC5mjeMXGgDqzkUW4l8vUNooxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744994; c=relaxed/simple; bh=teDU+jsKaLqxcV9T2d/W44RsJpnkOzRGmLjayhbf0Fc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=navYhOsNgNtGfQioSI8brrAB/fWho5hw/EK+okVpARsjrnsMwvOpFy2XgUU/7s9YrS/N2HolPE/Bp+P6OrZFnK2gAcWaKHZCgO7VX185OCxq33kbnuzeNFLmp5kfBFeJYht3AScDQGP3rDEisxNmRtWtU///g3T85P9x0LJwOGk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 6B2D3201DF6; Fri, 18 Sep 2026 17:23:01 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x7aQX-001awp-11; Fri, 18 Sep 2026 17:23:01 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 17:22:52 +0200 Subject: [PATCH v4 6/9] spi: spidev_test: send predictable data 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20260918-spi-sun4i-spidev_test-v4-6-5ffacb1a4ebd@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Kleine-Budde , kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3374; i=jre@pengutronix.de; h=from:subject:message-id; bh=AlWK5cCS//6rBkEsTOGeF1HnmyMz5zeZy/fD+EBk87o=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaGx4skpyg23wgp7150YOoy08UDm6u8zA6Xp693eTzv +rji+vcOkpZGMS4GGTFFFli1eQUhIz9r5tV2sXCzGFlAhnCwMUpABP5NIPhnwqDrXFg6vXm1hnT vp5y+1W5RC81/cPDxt4HOwpE3j9cb8zI8Jbtne6+yHunXp/Lfy9o+S7g6Y2ab+oTT/hy9ATd9Ph 8mQUA X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 From: Marc Kleine-Budde Introduce a flag to test on a predictable byte sequence instead of random bytes. This is useful when comparing multiple runs with the same option set e.g. with an oscilloscope. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann --- tools/spi/spidev_test.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index c34601558646..21a631d346a4 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -47,6 +47,7 @@ static int interval =3D 5; /* interval in seconds for sho= wing transfer rate */ static int compare; static int nonzero; static int do_tx =3D 1, do_rx =3D 1; +static int predictable; static int input_choices; =20 static uint8_t default_tx[] =3D { @@ -175,7 +176,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t= const *rx, size_t len) =20 static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNORSZbcdiloprstvwz]\n", prog); + printf("Usage: %s [-2348CDFHILMNOPRSZbcdiloprstvwz]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" @@ -200,6 +201,7 @@ static void print_usage(const char *prog) " -o --output output data to a file (e.g. \"results.bin\")\n" " -p send data (e.g. \"1234\\xde\\xad\")\n" " -z --nonzero don't send 0x00 or 0xff bytes\n" + " -P --predictable send a predictable sequence instead of random nu= mbers\n" " -S --size transfer the given number of random bytes\n" " -I --iter iterations\n" "additional parameters:\n" @@ -245,12 +247,13 @@ static void parse_opts(int argc, char *argv[]) { "no-cs", 0, 0, 'N' }, { "ready", 0, 0, 'R' }, { "mosi-idle-low", 0, 0, 'M' }, + { "predictable", 0, 0, 'P' }, { "verbose", 0, 0, 'v' }, { NULL, 0, 0, 0 }, }; int c; =20 - c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:vS:zI:", + c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:PvS:zI:", lopts, NULL); =20 if (c =3D=3D -1) @@ -329,6 +332,10 @@ static void parse_opts(int argc, char *argv[]) input_tx =3D optarg; input_choices++; break; + case 'P': + predictable =3D 1; + input_choices++; + break; case '2': mode |=3D SPI_TX_DUAL; break; @@ -448,9 +455,13 @@ static void transfer_buf(int fd, int len) if (!tx) pabort("can't allocate tx buffer"); for (i =3D 0; i < len; i++) { - do - tx[i] =3D random(); - while (nonzero && (tx[i] =3D=3D 0x0 || tx[i] =3D=3D 0xff)); + if (predictable) { + tx[i] =3D i - iterations; + } else { + do { + tx[i] =3D random(); + } while (nonzero && (tx[i] =3D=3D 0x0 || tx[i] =3D=3D 0xff)); + } } } =20 @@ -488,7 +499,7 @@ int main(int argc, char *argv[]) parse_opts(argc, argv); =20 if (input_choices > 1) - pabort("only one of -S (--size), -p, -i (--input), -t (--no-tx) may be s= elected"); + pabort("only one of -S (--size), -p, -i (--input), -t (--no-tx), -P (--p= redictable) may be selected"); =20 if (nonzero && !transfer_size) pabort("-z (--nonzero) is only implemented for -S (--size)"); --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:33:50 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC04D502D6A; Fri, 18 Sep 2026 15:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744995; cv=none; b=fXiYqS9l9OIERMT4868MDaxALCd++jQ1owjQRPnBMBByd5qE3DHmnUrJyx1aJ5z+tEgajjpTmVlIYDh81jzGxeQQwH4sVGEIMwa148A9F4Pe+KpV+tDTN+adrBmfj7up49VYMHxwtJWFrXxmKyaT7l0ORgZWyg7ZTQggES42Yo4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744995; c=relaxed/simple; bh=uEJAKvL0GW1th3LNcNyHmvUSa9x53KnETWcSbXA1Di4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=duHD2/JyFaTLboisM4J2DRKFlCDUpf5caAc9AqdZMYe30pPlS1yFiBu9EXfLhyB/wS/GrulQfd2d6itqgYb6iDMHqEUUGsWulIZN1dJGhFhxaYxkAfXPcNRF+reNEd7RmMFv73SzlnOBzvrhTAwfSwgLE7VHmbzlZ6E39X79UHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 7644D201DFA; Fri, 18 Sep 2026 17:23:01 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x7aQX-001awp-19; Fri, 18 Sep 2026 17:23:01 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 17:22:53 +0200 Subject: [PATCH v4 7/9] spi: spidev_test: add option to split message into multiple transfers 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20260918-spi-sun4i-spidev_test-v4-7-5ffacb1a4ebd@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Kleine-Budde , kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=5522; i=jre@pengutronix.de; h=from:subject:message-id; bh=+kk2ClcDiRy4fa10f+0S1SciTBoZzS7nkDuapft4hzo=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaGx5s475k4tSc9rVHFSu47irrMdSu/zLnmN4yWZbJU wq2XhXp6ChlYRDjYpAVU2SJVZNTEDL2v25WaRcLM4eVCWQIAxenAEwkYg7D/9J1DjExer80Z5xw DVvzcNP3HYdXHipIkPKKcc5t/JyouoaRocOnYtOs33d5G3xuT9NuPNHXYPgx4XPlhpYa2RUGOxb 3cwIA X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 From: Marc Kleine-Budde SPI_IOC_MESSAGE() submits a single SPI message containing one or more transfers. Introduce an option to allow splitting the message into multiple transfers. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann --- tools/spi/spidev_test.c | 98 +++++++++++++++++++++++++++++++++------------= ---- 1 file changed, 67 insertions(+), 31 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 21a631d346a4..9aa1e0849a88 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -42,6 +42,7 @@ static uint16_t delay; static uint16_t word_delay; static int verbose; static int transfer_size; +static int transfers =3D 1; static int iterations; static int interval =3D 5; /* interval in seconds for showing transfer rat= e */ static int compare; @@ -118,40 +119,64 @@ static int unescape(char *_dst, char *_src, size_t le= n) return ret; } =20 -static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t = len) +static void transfer(int fd, uint8_t const * const tx, uint8_t const * con= st rx, size_t len) { int ret; int out_fd; - struct spi_ioc_transfer tr =3D { - .tx_buf =3D (unsigned long)tx, - .rx_buf =3D (unsigned long)rx, - .len =3D len, - .delay_usecs =3D delay, - .word_delay_usecs =3D word_delay, - .speed_hz =3D speed, - .bits_per_word =3D bits, - }; - - if (mode & SPI_TX_OCTAL) - tr.tx_nbits =3D 8; - else if (mode & SPI_TX_QUAD) - tr.tx_nbits =3D 4; - else if (mode & SPI_TX_DUAL) - tr.tx_nbits =3D 2; - if (mode & SPI_RX_OCTAL) - tr.rx_nbits =3D 8; - else if (mode & SPI_RX_QUAD) - tr.rx_nbits =3D 4; - else if (mode & SPI_RX_DUAL) - tr.rx_nbits =3D 2; - if (!(mode & SPI_LOOP)) { - if (mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) - tr.rx_buf =3D 0; - else if (mode & (SPI_RX_OCTAL | SPI_RX_QUAD | SPI_RX_DUAL)) - tr.tx_buf =3D 0; + size_t bytes_per_word =3D DIV_ROUND_UP(bits, 8); + int effective_transfers =3D min_t(int, transfers, DIV_ROUND_UP(len, bytes= _per_word)); + struct spi_ioc_transfer *tr =3D calloc(effective_transfers, sizeof(*tr)); + size_t len_per_transfer =3D roundup(DIV_ROUND_UP(len, effective_transfers= ), bytes_per_word); + const uint8_t *tx_buf =3D tx; + const uint8_t *rx_buf =3D rx; + size_t rem =3D len; + + if (!tr) + pabort("can't allocate transfer array"); + + for (int i =3D 0; i < effective_transfers; i++) { + size_t n =3D min(rem, len_per_transfer); + + tr[i] =3D (struct spi_ioc_transfer) { + .tx_buf =3D (unsigned long)tx_buf, + .rx_buf =3D (unsigned long)rx_buf, + .len =3D n, + .delay_usecs =3D delay, + .word_delay_usecs =3D word_delay, + .speed_hz =3D speed, + .bits_per_word =3D bits, + }; + + if (tx_buf) + tx_buf +=3D n; + + if (rx_buf) + rx_buf +=3D n; + + rem -=3D n; + + if (mode & SPI_TX_OCTAL) + tr[i].tx_nbits =3D 8; + else if (mode & SPI_TX_QUAD) + tr[i].tx_nbits =3D 4; + else if (mode & SPI_TX_DUAL) + tr[i].tx_nbits =3D 2; + if (mode & SPI_RX_OCTAL) + tr[i].rx_nbits =3D 8; + else if (mode & SPI_RX_QUAD) + tr[i].rx_nbits =3D 4; + else if (mode & SPI_RX_DUAL) + tr[i].rx_nbits =3D 2; + if (!(mode & SPI_LOOP)) { + if (mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) + tr[i].rx_buf =3D 0; + else if (mode & (SPI_RX_OCTAL | SPI_RX_QUAD | SPI_RX_DUAL)) + tr[i].tx_buf =3D 0; + } } =20 - ret =3D ioctl(fd, SPI_IOC_MESSAGE(1), &tr); + ret =3D ioctl(fd, SPI_IOC_MESSAGE(effective_transfers), tr); + free(tr); if (ret < 1) pabort("can't send spi message"); =20 @@ -176,7 +201,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t= const *rx, size_t len) =20 static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNOPRSZbcdiloprstvwz]\n", prog); + printf("Usage: %s [-2348CDFHILMNOPRSTZbcdiloprstvwz]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" @@ -211,6 +236,7 @@ static void print_usage(const char *prog) " -N --no-cs no chip select\n" " -R --ready slave pulls low to pause\n" " -M --mosi-idle-low leave mosi line low when idle\n" + " -T --transfers number of transfers\n" "misc:\n" " -v --verbose Verbose (show tx buffer)\n"); exit(1); @@ -248,12 +274,13 @@ static void parse_opts(int argc, char *argv[]) { "ready", 0, 0, 'R' }, { "mosi-idle-low", 0, 0, 'M' }, { "predictable", 0, 0, 'P' }, + { "transfers", 1, 0, 'T' }, { "verbose", 0, 0, 'v' }, { NULL, 0, 0, 0 }, }; int c; =20 - c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:PvS:zI:", + c =3D getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:PT:vS:zI= :", lopts, NULL); =20 if (c =3D=3D -1) @@ -319,6 +346,9 @@ static void parse_opts(int argc, char *argv[]) case 'M': mode |=3D SPI_MOSI_IDLE_LOW; break; + case 'T': + transfers =3D atoi(optarg); + break; case 'N': mode |=3D SPI_NO_CS; break; @@ -513,6 +543,12 @@ int main(int argc, char *argv[]) if (compare && mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) pabort("-c (--compare) conflicts with -2 (--dual), -4 (--quad) or -8 (--= octal)"); =20 + if (transfers < 1) + pabort("-T (--transfers) must be 1 or above"); + + if (bits < 1) + pabort("-b (--bpw) must be 1 or above"); + fd =3D open(device, O_RDWR); if (fd < 0) pabort("can't open device"); --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:33:50 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC9D9502D6D; Fri, 18 Sep 2026 15:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744994; cv=none; b=RmYQWESTiGCuqTs7e/DYec2mUMljWv1cDL5XJCwsePknyDZ4wmVEgxO52AISwLle8amrmfviHc+ayPFEX2trv/8SHNvRyK3KGrxOXQVDkzNX2XfYQwPMQj44GdIV/23v+jvMVcxZ2O2O1s0gKSDJGvI56hIuRFxMF/rcV8GeXwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744994; c=relaxed/simple; bh=mJaZs4FVyMEvTLT3VRG2o4F8L2qwm8a64IN9N+d+tyY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Je4K6vglSzVNGKbLxrbXHym9Fv8LrwelqhPbYXJZnZZABMaZoMj04e8Yh4zuXCFBuxBDu75vnAgwot/4pN3rEylYd8stUOHKi9ka6rr+qyL/zUoFEmIl41G2q3mZhBGMxhnfH1LO55FOFsgHocB+EsJjp7DjwPaADF273nRAOPs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 80437201DFF; Fri, 18 Sep 2026 17:23:01 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x7aQX-001awp-1J; Fri, 18 Sep 2026 17:23:01 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 17:22:54 +0200 Subject: [PATCH v4 8/9] spi: spidev_test: print TX on error 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20260918-spi-sun4i-spidev_test-v4-8-5ffacb1a4ebd@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Kleine-Budde , kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1059; i=jre@pengutronix.de; h=from:subject:message-id; bh=hHETDu9uonW3JsowDVUX9ZM5NSWu+HwgSoTAX14MMNs=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaGx6ypvxu0JKKowqND9uXFC5JtHwnmnLm/Dyd6e3Si lW7+HiCO0pZGMS4GGTFFFli1eQUhIz9r5tV2sXCzGFlAhnCwMUpABM5cZSR4bFqkq/bGk5bnSd3 eDKuVEm1qfxk3dX+YevWcxKKKx3/9TAyrH6sdOyEj5i0x8SoHVFGh1+6TVhyz/t/1OmlzcWGJ6e 94AEA X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 From: Marc Kleine-Budde If sending an spi message fails, dump its first up to 256 bytes to the console. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann --- tools/spi/spidev_test.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 9aa1e0849a88..1910facdd3da 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -177,8 +177,18 @@ static void transfer(int fd, uint8_t const * const tx,= uint8_t const * const rx, =20 ret =3D ioctl(fd, SPI_IOC_MESSAGE(effective_transfers), tr); free(tr); - if (ret < 1) + if (ret < 1) { + const size_t dump_len =3D min_t(size_t, len, 256); + int saved_errno =3D errno; + + if (tx) { + hex_dump(tx, dump_len, 32, "TX"); + if (len > dump_len) + printf("... (%zu more bytes)\n", len - dump_len); + } + errno =3D saved_errno; pabort("can't send spi message"); + } =20 if (verbose && tx) hex_dump(tx, len, 32, "TX"); --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:33:50 2026 Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57A225038E1; Fri, 18 Sep 2026 15:23:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.200.13 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744995; cv=none; b=Ea5bfdPYvfUbxWRah6ZhrraDrm+MJw+LtpXlhUwxm0EnHjSQCt3YS7z4QNM1u31KYXWDnBScySXUDf/Cud5TTFGxtSahUGiTrVq7D9jRVK+FPtk1VAjZ6rscurPv/u1z6a0WzDnTEGQOjF2maeZb4chhinVCmTv3Z3O1Dgu19p4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789744995; c=relaxed/simple; bh=wnXKMJ7G5RW5vLHYwJMCyFceR++MFwHGc1EJvqp7jJE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NCqzAEFDNlcmP5g57w0Ysny/tYFxM6HOV6jhunU1rAlEGCaQgmNwFO5PzYGG3WLId3YqvmYXLnC0f7bOVY7M70vjKLL1Gwj6Y+fwX8NJguIyWsRd/CJOM23BsbykAS6Y3JQrZhTs7OD1HQ21RCWrr/Ep5j6KA7ZEFfE2zMRd7hQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.200.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de (drehscheibe.grey.stw.pengutronix.de [IPv6:2a0a:edc0:0:c01:1d::a2]) (Authenticated sender: relay-from-drehscheibe.grey.stw.pengutronix.de) by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 8C27C201E03; Fri, 18 Sep 2026 17:23:01 +0200 (CEST) Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1x7aQX-001awp-1S; Fri, 18 Sep 2026 17:23:01 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 17:22:55 +0200 Subject: [PATCH v4 9/9] spi: spidev_test: rewrite unescape() to stay in bounds 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20260918-spi-sun4i-spidev_test-v4-9-5ffacb1a4ebd@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v4-0-5ffacb1a4ebd@pengutronix.de> To: Mark Brown Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, Marc Kleine-Budde , kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2400; i=jre@pengutronix.de; h=from:subject:message-id; bh=wnXKMJ7G5RW5vLHYwJMCyFceR++MFwHGc1EJvqp7jJE=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaGx7yT3XZCWH3HeHsStxB/pPvRbzZIMUU5xlZfJB5r dKeJXMzOkpZGMS4GGTFFFli1eQUhIz9r5tV2sXCzGFlAhnCwMUpABMxS2JkeBS+uS1F83SN206j PTKLtSc/bxJUcSvx9/436fdRN+06KYb/qQyrPyz8+9Z1WZNjf82fBgmtZ0s5914vXH5i8W7pLPY UZgA= X-Developer-Key: i=jre@pengutronix.de; a=openpgp; fpr=0B7B750D5D3CD21B3B130DE8B61515E135CD49B5 unescape() could read the source buffer out of bounds for inputs like "\\x" and could write the destination buffer out of bounds due to lack of checking against the len parameter. For better readability and consistency, rewrite it as a simplified version of unescape_string() in string_helpers.h. This includes rearranging src and dst parameters. Signed-off-by: Jonas Rebmann --- tools/spi/spidev_test.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 1910facdd3da..95d5d37788ba 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -95,28 +96,31 @@ static void hex_dump(const void *src, size_t length, si= ze_t line_size, * Unescape - process hexadecimal escape character * converts shell input "\x23" -> 0x23 */ -static int unescape(char *_dst, char *_src, size_t len) +static int unescape(char *src, char *dst, size_t size) { - int ret =3D 0; - int match; - char *src =3D _src; - char *dst =3D _dst; + char *out =3D dst; unsigned int ch; =20 - while (*src) { - if (*src =3D=3D '\\' && *(src+1) =3D=3D 'x') { - match =3D sscanf(src + 2, "%2x", &ch); - if (!match) - pabort("malformed input string"); + while (*src && size--) { + if (src[0] =3D=3D '\\' && src[1] !=3D '\0') { + src++; + + if (src[0] =3D=3D 'x' && + isxdigit((unsigned char)src[1]) && + isxdigit((unsigned char)src[2]) && + sscanf(&src[1], "%2x", &ch)) { + *out++ =3D (unsigned char)ch; + src +=3D 3; + continue; + } =20 - src +=3D 4; - *dst++ =3D (unsigned char)ch; - } else { - *dst++ =3D *src++; + *out++ =3D '\\'; + if (!size--) + break; } - ret++; + *out++ =3D *src++; } - return ret; + return out - dst; } =20 static void transfer(int fd, uint8_t const * const tx, uint8_t const * con= st rx, size_t len) @@ -425,7 +429,7 @@ static void transfer_escaped_string(int fd, char *str) pabort("can't allocate rx buffer"); } =20 - size =3D unescape((char *)tx, str, size); + size =3D unescape(str, (char *)tx, size); transfer(fd, tx, rx, size); free(rx); free(tx); --=20 2.56.0.rc0.108.gf0ef1b96a0