From nobody Thu Sep 24 23:34:04 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 420BC481FCA; Fri, 18 Sep 2026 12:53:35 +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=1789736018; cv=none; b=QdrdFwfyllR1oFnhZVolmZuJmL0ptNopuWxpPg914Iu108NH0lDbYTJZLZRu2UhbYTik1O+L4u037aWEYveWTPH6UGTGZD6Cxx1OeCCZ8Jw32nf/Ob2LuxeHg7XRrNnre0537F+ey6JjJUrNU19WVrUraQ/83W0quR6phbx3QWk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789736018; c=relaxed/simple; bh=XzJjoVhnb/s7cdDvuM4wM0NdbOkb23/JUb5yHGelLtw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AFi4K3fw46C7u3nVhOIFyj81Onnh8UcSYxsY0wQV7Ll328Urv0Rm4FCy+X24sIhpf2d7FOgN1ZpfZa/AkdCfZ0dTYPUE4Nkfm4YkhIq+bX+4wsCs9rvh/wesZOdYmEoVLcM9WXwN0dO+O7gnGdcc+xqkYazHmZVF8tHbm/1liH8= 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 35E1F200591; Fri, 18 Sep 2026 14:45:12 +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 1x7Xxo-001Zaf-0M; Fri, 18 Sep 2026 14:45:12 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 14:44:55 +0200 Subject: [PATCH v3 1/8] 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-v3-1-d3ca218d9f76@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@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=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaaxSieMOjTeFWXgfH3QN/z8/8eV7n9eo9mvYxF0IsZ LRcwo/Jd5SyMIhxMciKKbLEqskpCBn7XzertIuFmcPKBDKEgYtTACYyrY+R4Vta4PyHgp+vcv9t XutbIpn/bYduXuLH3fZVN51UDffHuDD8s23K26CRVv2mc8GFo3alLnlSkoH7Gc/sWJic3MKnZH6 XGwA= 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:34:04 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 5977D495AC6; Fri, 18 Sep 2026 12:53:35 +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=1789736019; cv=none; b=TMTiz9yyoYfUJagi3fbPUXd7bigoHuP9wwjNgJS14jodIfbCNhtagxKh2DK1KmOeLA9CdVlPUXhtYZe3n+Qg06JUqRkwjecAHXwSGHGQPfbEQWYI+UceMVyASERw7zgfMdT5DaIupr05EtB3Myf7U3dDnc92lH+PmTQEcCZI+9U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789736019; c=relaxed/simple; bh=j+7T7rMs9DnoInDomemxGeAGPNydZFvy6gAW82aLMzo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cfO3aO4T3kUhANpFtzItjiSZ3DefY0+G5ss2yCtc59UdLO9eYGmQCE3QZCL5uFNXXvldR/pXcMqOGyAefD1j1WrCchFQCIKN4oJWa8bL0ibd08eX5HxdxiXQHl9D9qtmnJz/fDgUn35cMeD/1iA6Qftc4LZPDDVGTWAt+MLrErg= 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 3FE1E20075E; Fri, 18 Sep 2026 14:45:12 +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 1x7Xxo-001Zaf-0W; Fri, 18 Sep 2026 14:45:12 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 14:44:56 +0200 Subject: [PATCH v3 2/8] 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-v3-2-d3ca218d9f76@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@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=3138; i=jre@pengutronix.de; h=from:subject:message-id; bh=8y0UZbJne6KJzGZjVt710HS/0SIaPx+4Wjrrv9ACrdc=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaaxQikPQh2c7wlnbThPgjm2rEPUTEfdI2PVrgyvH/b gobl+f9jlIWBjEuBlkxRZZYNTkFIWP/62aVdrEwc1iZQIYwcHEKwETO5jL8r567y3it9YIG+UyX b+1a7NtWXWWf1vZBM+xKxLNLDDuCNjIytL+Knf3lp5dQUfyTzAjRF76fVjCmHHxekyUdWrOp1UG IAwA= 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 | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index bd5ee7a6ac82..457dba763822 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) @@ -268,6 +271,10 @@ static void parse_opts(int argc, char *argv[]) break; case 'l': mode |=3D SPI_LOOP; + compare =3D 1; + break; + case 'c': + compare =3D 1; break; case 'H': mode |=3D SPI_CPHA; @@ -425,7 +432,7 @@ static void transfer_buf(int fd, int len) _write_count +=3D len; _read_count +=3D len; =20 - if (mode & SPI_LOOP) { + if (compare) { if (memcmp(tx, rx, len)) { fprintf(stderr, "transfer error !\n"); hex_dump(tx, len, 32, "TX"); @@ -449,6 +456,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:34:04 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 5939F491585; Fri, 18 Sep 2026 12:53:35 +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=1789736018; cv=none; b=GARnmPLDMoGS22VfwM7vi6LfPMVkQJQOUahAIjQZrVa/d0XyMWQQZNhUovUcCwVTuP3hNeKfPk3rhZboeDwkYGAYhvhQZ78bAbiw+qg0QmB++ypUWGZMrn5zYC9Zp8Vh85xnlLUDEJUW58sjBnmxvqMvDbmYXokrp9EDEb2pcIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789736018; c=relaxed/simple; bh=VOnLxIeETDbJd1a+D31iVSxHN5zL0s/3Afet3w0OY1M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oEwettJCgHbqXV2ni9gKmj9+6SVDoAKUvMFpUUlm8PXOP0183m29ESn6VfSeJPD6BE6xrB55lQa9FhIywf2yPRlyGTh0Oj7vrTiLhis1444+1eCdC4t8hdIpEqbl5s3Zsf51KDzRr1esi/TEaUaa8aRfF0lTKjT0p8RyCEgs6WU= 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 5092920098A; Fri, 18 Sep 2026 14:45:12 +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 1x7Xxo-001Zaf-0g; Fri, 18 Sep 2026 14:45:12 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 14:44:57 +0200 Subject: [PATCH v3 3/8] 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-v3-3-d3ca218d9f76@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@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=5064; i=jre@pengutronix.de; h=from:subject:message-id; bh=z1pY00EymWbmg08iHAl5y9XflPIqFXfjPinOn4J+Bx8=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaaxRaesa0JUPZ/fjrD5OS7NdfvbNsgsL1+9einryZf 79xpynD9Y5SFgYxLgZZMUWWWDU5BSFj/+tmlXaxMHNYmUCGMHBxCsBEgj4yMhycrGyWwHNow/lS h5ppJswyki49F3uL3Kdd2Vj281NikQLD/1zP6P1bXi1xT9R9wc/xo3slU9m803t8H9o+EVEPUEw sYgEA 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 | 66 ++++++++++++++++++++++++++++++++++-----------= ---- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 457dba763822..dce7583971c7 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; @@ -276,6 +283,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; @@ -311,6 +325,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; @@ -413,26 +428,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) { + if (tx && rx && compare) { if (memcmp(tx, rx, len)) { fprintf(stderr, "transfer error !\n"); hex_dump(tx, len, 32, "TX"); @@ -453,8 +473,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 -p, -i (--input), -t (--no-tx) may be selected"); + + if (compare && (!do_tx || !do_rx)) + pabort("-l/-c (--loop/--compare) conflict 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)"); --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:34:04 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 5924248D890; Fri, 18 Sep 2026 12:53:35 +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=1789736019; cv=none; b=ulLjTbbyIsXpnIB+Sba66V9uToi35uqqOElAEN/Ib8c2eMLKLqgbG3SVEnwiHKGjf6j3JgPOQOqh8J/jZrSidmAs8EV5Kc0D17QsJ0UnadY3J6xKWRu2a5YchWIt92UCMsm6bKuoEWnIewCiB/I+SIPt5W8frww9rDXMnMNKDqk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789736019; c=relaxed/simple; bh=4JW+YvKXA5jdPrcDK6dFf7gDOuyqcwx/9aHDQ0RE974=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EbIPe5Hl9vMyjPM564EVptRd8nLFNvUC4Ljzti2Y91a8W/03/NqY+B7O+uIuZL1lXw8MPWREPhWrxpnwUvVlyyJ9HPfSe5bUawlbt4Qg7GSgYiEfbu3fG+ldGnWMpM+KvyDkdPLfe09y4QZ3e23Mxp6ADwQdv5oaSOP4ch8UotE= 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 58AF4200B4C; Fri, 18 Sep 2026 14:45:12 +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 1x7Xxo-001Zaf-0r; Fri, 18 Sep 2026 14:45:12 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 14:44:58 +0200 Subject: [PATCH v3 4/8] 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-v3-4-d3ca218d9f76@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@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=3446; i=jre@pengutronix.de; h=from:subject:message-id; bh=v3umAC9d76AWxUkUnDovEjxRqiyGKD0e5TDKo1DfXwA=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaaxT6VZZP4pb0xIzMqZ+yjKs1Wv9UZa5Oem1nmT7v9 afFD9mlO0pZGMS4GGTFFFli1eQUhIz9r5tV2sXCzGFlAhnCwMUpABNZE8rwh9dCYHtTFpvima6V u8+vWPPUKdzj4dmakr87c44IZhooazIyTO2q+m1aoS0fESusml69mis0wH2WieO2iOmK9wq8Jiq wAgA= 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, 15 insertions(+), 5 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index dce7583971c7..fcef8cdbefd2 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 size\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,10 @@ static void parse_opts(int argc, char *argv[]) case 'S': transfer_size =3D atoi(optarg); break; + case 'z': + nonzero =3D 1; + input_choices++; + break; case 'I': iterations =3D atoi(optarg); break; @@ -436,8 +443,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) { @@ -474,7 +484,7 @@ int main(int argc, char *argv[]) parse_opts(argc, argv); =20 if (input_choices > 1) - pabort("only one of -p, -i (--input), -t (--no-tx) may be selected"); + pabort("only one of -p, -i (--input), -t (--no-tx), -z (--nonzero) may b= e selected"); =20 if (compare && (!do_tx || !do_rx)) pabort("-l/-c (--loop/--compare) conflict with -t (--no-tx) or -r (--no-= rx)"); --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:34:04 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 AD3A134D4DE; Fri, 18 Sep 2026 12:45:17 +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=1789735523; cv=none; b=VQ9sg+Oxyl/F5ORq0w8mnyhdNQ/NiCdu6LE6OljUndRTLjvMzuGXQQHaIP+CASOZHxW9dWvt/rhQxXGW0p9aQkDbiRH7pb79WmjS13wOwp8NB5iuQtXAD+kEGPZfeFOU+ZibsCVp5mBP/tYus4JfI63azxI6/335EVF+Kszexpo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789735523; c=relaxed/simple; bh=ym/o/NcL7ESv6D9+WfJITX3QAIkhTYSmwxgB4J0NN7M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WC8Kld7H4HhmjhMmXgv3o18QUOIXHtGUKMxh2vAxdfJ0gBj+W5N0PGXRq2dw34OVTL90qYSb/3Hm1neHf1TuJfUq7KBY2M6HUF7Mcr6ppVOJh4pgBVbI2C+i604OsVxXZ+8t+goJQ//VEo7ct7q3wdINmOYJOCjZHJA6Grr604w= 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 63AC5200B67; Fri, 18 Sep 2026 14:45:12 +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 1x7Xxo-001Zaf-11; Fri, 18 Sep 2026 14:45:12 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 14:44:59 +0200 Subject: [PATCH v3 5/8] 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-v3-5-d3ca218d9f76@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@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=3373; i=jre@pengutronix.de; h=from:subject:message-id; bh=YkRoTi7buq9hQM791dyaH9KNzd4yNxZXuKkrp3bzoRU=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaaxS2RvNEkx3DgUmq+XvvMW4yc1ybqsFw8ckrgY/P5 xW6qP1q6ihlYRDjYpAVU2SJVZNTEDL2v25WaRcLM4eVCWQIAxenAEwk4ysjw2q3rdd33Miwe/rx 1auIoDVKy+8e+3BQPtv/mZzmVNFZ8uUM/2zfrMlL25V6OTLOU4TbqVKVq0f7tF79T7nQfckqc33 38wIA 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 fcef8cdbefd2..011ac0593736 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 size\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) @@ -330,6 +333,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; @@ -444,9 +451,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 @@ -484,7 +495,7 @@ int main(int argc, char *argv[]) parse_opts(argc, argv); =20 if (input_choices > 1) - pabort("only one of -p, -i (--input), -t (--no-tx), -z (--nonzero) may b= e selected"); + pabort("only one of -p, -i (--input), -t (--no-tx), -z (--nonzero), -P (= --predictable) may be selected"); =20 if (compare && (!do_tx || !do_rx)) pabort("-l/-c (--loop/--compare) conflict with -t (--no-tx) or -r (--no-= rx)"); --=20 2.56.0.rc0.108.gf0ef1b96a0 From nobody Thu Sep 24 23:34:04 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 81E8131ED68; Fri, 18 Sep 2026 12:45:17 +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=1789735525; cv=none; b=YXX8H+funiJM/qpNgN4QayIFQCE/PxCAQVjfExeaKmxaJjRGjHQUJ6Vx+HqWdbk0gS46WwpeyjLoWHx+FaU2SqG0TsVR/cI95vH9me39HR2Jro36sIOsszXCp5UGm2DOsfBUjKsPno4AWElgLly5bFXSdtREjfeRsiwm8fmQATY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789735525; c=relaxed/simple; bh=lL2jZIYpO1vPss0VZ4p9UtwigPPNIA3ViXaTdD7+kvY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VITUgfsyZKzn/PCOdg/1RCkgwbinBFeUQIs5Uay+S6m0HU66TLJZ2HPQoOhnNCzPnbKbFkZUN2y4JKOiVtbEOpCFVNCz02vMPJ4snH84V9mLu8YQSKUJpEKdHNKVmJEgwr4YttpB/HUNy0G61IZEAd4B8lyLiFHaIS1QEpg1yb4= 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 70014200F8D; Fri, 18 Sep 2026 14:45:12 +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 1x7Xxo-001Zaf-1A; Fri, 18 Sep 2026 14:45:12 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 14:45:00 +0200 Subject: [PATCH v3 6/8] 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-v3-6-d3ca218d9f76@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@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=5463; i=jre@pengutronix.de; h=from:subject:message-id; bh=AVd0wt8oBnIsqup0WQuJyg/UhmMqYW9BJzq+5osopo8=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaaxR29e/DXSZP+tv51973cl/WMJ3B+0JLRPvyjJKfK bPSDt5M7yhlYRDjYpAVU2SJVZNTEDL2v25WaRcLM4eVCWQIAxenAExkxiOGf7riv9uLVR0LLle8 3pPurPXrII81y7O7wY5ThZnNZ67VcWD4K7Mpce3RxT+teaZWPpyVskSlT7bvRPDtz0570i283PM MmAA= 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 | 95 +++++++++++++++++++++++++++++++++------------= ---- 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 011ac0593736..6fb67021ae14 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,61 @@ 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; + + 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 +198,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 +233,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 +271,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) @@ -320,6 +344,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; @@ -506,6 +533,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:34:04 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 AD2D8340260; Fri, 18 Sep 2026 12:45:17 +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=1789735525; cv=none; b=l1CjpIEcw3Cf3GBR7pHcv40iOQzQNvLDhe2G3ygcMiRvEwe9YQDxf0tg91Q3hmRqNwk4YyZjJeaLYjQc/ENk2wZ6evrp3vUfUfoEBlLbxOvhg+3PJnrpyAbHWie2kcduR84Vtb2WA91wyNemhYv9GUU587u4CFhxrdLMT917Ac8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789735525; c=relaxed/simple; bh=EqMPi4ZVzC7cGNVME5Mq0PuQ5uT/iihcHbpfsjrz1Js=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Sh64yCENroQNTq68KOzDq9tbodJ2PhYM3Ps8EtdsHJt12fqouf9pPnN2LdZNl+STr+Jl3msaJ5tGoTCJQToh80diTcf3hoCbWbhjSTyt/VPQ7/UY8PUFG/aFmldQUyBd/3w5X7dPUcfYhM+2b7WXzlP7wRCKMkOoooElFYUam8E= 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 78F5320102D; Fri, 18 Sep 2026 14:45:12 +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 1x7Xxo-001Zaf-1K; Fri, 18 Sep 2026 14:45:12 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 14:45:01 +0200 Subject: [PATCH v3 7/8] 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-v3-7-d3ca218d9f76@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@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=1060; i=jre@pengutronix.de; h=from:subject:message-id; bh=Z7jsU2ZPr2fg0qvB9saNI9I/cjK4cAO1XrpxoxrZUyE=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaaxQ+eYvVv8Nb7x82vfOveYHIbc6q992ZJ6qTiq6+f DUrgTXhXUcpC4MYF4OsmCJLrJqcgpCx/3WzSrtYmDmsTCBDGLg4BWAivBIM/yturDu5Mqfp+rmF Di+SEzOuRl57W6Y6T/9d4hSdNrFb2zwZGTZpH87bmFJ25P5WPbmWXOFXlyy65398USUqfN6o8lr YR1YA 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 6fb67021ae14..9dfd33678d89 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -174,8 +174,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:34:04 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 ACE9633E34B; Fri, 18 Sep 2026 12:45:17 +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=1789735524; cv=none; b=lmXREGlV9qx9IHPGTfkAt4PirNqaYiJtEMoRtxGw1oqsoIuizi7GR245aAh6Xx+McZ4lFAirUVlqXVnQPq11EGHujX1BtFWMUqSgICjDj1nnoxVfTAJ5EtIWyRgr7df0yhB+1F0GVlZ2ifKdgy7dDD2nGqC+HtGVFDPdaKaMngk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789735524; c=relaxed/simple; bh=5EtD8OFJRrdpdMESFfhXTrr7YH9Ho9/1DFRXsxh2MPY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Qk2kenFNnH6Xe3dHYc9GfnnJP5XvgVZMQdlftVUXbg10sLIhvJHCn8rLnuKPM1UjBDu5EYfG3Sn0y0hj40lrHID80ndV/B6a/xnxDZvYLenGKI00F0ibp+4DCCD6Tt8Ug8WPTxbbAw+qBv+ZKb3ZOJY1eY27Ruw60n5dE18p+Uw= 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 837E120127E; Fri, 18 Sep 2026 14:45:12 +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 1x7Xxo-001Zaf-1S; Fri, 18 Sep 2026 14:45:12 +0200 From: Jonas Rebmann Date: Fri, 18 Sep 2026 14:45:02 +0200 Subject: [PATCH v3 8/8] 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-v3-8-d3ca218d9f76@pengutronix.de> References: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@pengutronix.de> In-Reply-To: <20260918-spi-sun4i-spidev_test-v3-0-d3ca218d9f76@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=2406; i=jre@pengutronix.de; h=from:subject:message-id; bh=5EtD8OFJRrdpdMESFfhXTrr7YH9Ho9/1DFRXsxh2MPY=; b=owGbwMvMwCV2ZcYT3onnbjcwnlZLYshaaxTOZzBptcj9HDmNbvuKm2Iepia/xSYudhOTKmxaz CaotG9TRykLgxgXg6yYIkusmpyCkLH/dbNKu1iYOaxMIEMYuDgFYCIvdjH84VrXanYg2nQF5+eV DpkeG6KW2v/sSE851J4beKXPbWKcJCPDBcbj5id+JivdzV255vLG/4ZVXFbf28781xZbvLSO/W0 RJwA= 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 9dfd33678d89..5cbf72353052 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) @@ -421,7 +425,7 @@ static void transfer_escaped_string(int fd, char *str) if (!rx) 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