linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 1/3] lib/vsprintf: Add support for generic FourCCs by extending %p4cc
       [not found] <8153cb02-d8f1-4e59-b2d5-0dfdde7a832e@live.com>
@ 2025-04-07 13:36 ` Aditya Garg
  2025-04-07 13:37 ` [PATCH v3 2/3] printf: add tests for generic FourCCs Aditya Garg
  2025-04-07 13:37 ` [PATCH v3 3/3] drm/appletbdrm: use %p4cl instead of %p4cc Aditya Garg
  2 siblings, 0 replies; 7+ messages in thread
From: Aditya Garg @ 2025-04-07 13:36 UTC (permalink / raw)
  To: alyssa, Petr Mladek, Andy Shevchenko, Sven Peter,
	Thomas Zimmermann, Aun-Ali Zaidi, Maxime Ripard, airlied,
	Simona Vetter, Steven Rostedt, Rasmus Villemoes,
	Sergey Senozhatsky, Jonathan Corbet, Andrew Morton, apw, joe,
	dwaipayanray1, lukas.bulwahn, Kees Cook, tamird
  Cc: Linux Kernel Mailing List, dri-devel, linux-doc, Hector Martin,
	Asahi Linux Mailing List

From: Hector Martin <marcan@marcan.st>

%p4cc is designed for DRM/V4L2 FourCCs with their specific quirks, but
it's useful to be able to print generic 4-character codes formatted as
an integer. Extend it to add format specifiers for printing generic
32-bit FourCCs with various endian semantics:

%p4ch	Host byte order
%p4cn	Network byte order
%p4cl	Little-endian
%p4cb	Big-endian

The endianness determines how bytes are interpreted as a u32, and the
FourCC is then always printed MSByte-first (this is the opposite of
V4L/DRM FourCCs). This covers most practical cases, e.g. %p4cn would
allow printing LSByte-first FourCCs stored in host endian order
(other than the hex form being in character order, not the integer
value).

Acked-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Tested-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Aditya Garg <gargaditya08@live.com>
---
 Documentation/core-api/printk-formats.rst | 32 +++++++++++++++++++++
 lib/vsprintf.c                            | 35 +++++++++++++++++++----
 scripts/checkpatch.pl                     |  2 +-
 3 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
index 4bdc394e8..125fd0397 100644
--- a/Documentation/core-api/printk-formats.rst
+++ b/Documentation/core-api/printk-formats.rst
@@ -648,6 +648,38 @@ Examples::
 	%p4cc	Y10  little-endian (0x20303159)
 	%p4cc	NV12 big-endian (0xb231564e)
 
+Generic FourCC code
+-------------------
+
+::
+	%p4c[hnlb]	gP00 (0x67503030)
+
+Print a generic FourCC code, as both ASCII characters and its numerical
+value as hexadecimal.
+
+The generic FourCC code is always printed in the big-endian format,
+the most significant byte first. This is the opposite of V4L/DRM FourCCs.
+
+The additional ``h``, ``n``, ``l``, and ``b`` specifiers define what
+endianness is used to load the stored bytes. The data might be interpreted
+using the host byte order, network byte order, little-endian, or big-endian.
+
+Passed by reference.
+
+Examples for a little-endian machine, given &(u32)0x67503030::
+
+	%p4ch	gP00 (0x67503030)
+	%p4cn	00Pg (0x30305067)
+	%p4cl	gP00 (0x67503030)
+	%p4cb	00Pg (0x30305067)
+
+Examples for a big-endian machine, given &(u32)0x67503030::
+
+	%p4ch	gP00 (0x67503030)
+	%p4cn	00Pg (0x30305067)
+	%p4cl	00Pg (0x30305067)
+	%p4cb	gP00 (0x67503030)
+
 Rust
 ----
 
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 01699852f..6bc64ae52 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1793,27 +1793,50 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
 	char output[sizeof("0123 little-endian (0x01234567)")];
 	char *p = output;
 	unsigned int i;
+	bool pixel_fmt = false;
 	u32 orig, val;
 
-	if (fmt[1] != 'c' || fmt[2] != 'c')
+	if (fmt[1] != 'c')
 		return error_string(buf, end, "(%p4?)", spec);
 
 	if (check_pointer(&buf, end, fourcc, spec))
 		return buf;
 
 	orig = get_unaligned(fourcc);
-	val = orig & ~BIT(31);
+	switch (fmt[2]) {
+	case 'h':
+		break;
+	case 'n':
+		orig = swab32(orig);
+		break;
+	case 'l':
+		orig = (__force u32)cpu_to_le32(orig);
+		break;
+	case 'b':
+		orig = (__force u32)cpu_to_be32(orig);
+		break;
+	case 'c':
+		/* Pixel formats are printed LSB-first */
+		pixel_fmt = true;
+		break;
+	default:
+		return error_string(buf, end, "(%p4?)", spec);
+	}
+
+	val = pixel_fmt ? swab32(orig & ~BIT(31)) : orig;
 
 	for (i = 0; i < sizeof(u32); i++) {
-		unsigned char c = val >> (i * 8);
+		unsigned char c = val >> ((3 - i) * 8);
 
 		/* Print non-control ASCII characters as-is, dot otherwise */
 		*p++ = isascii(c) && isprint(c) ? c : '.';
 	}
 
-	*p++ = ' ';
-	strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
-	p += strlen(p);
+	if (pixel_fmt) {
+		*p++ = ' ';
+		strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
+		p += strlen(p);
+	}
 
 	*p++ = ' ';
 	*p++ = '(';
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 3d22bf863..44e233b6f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6891,7 +6891,7 @@ sub process {
 					    ($extension eq "f" &&
 					     defined $qualifier && $qualifier !~ /^w/) ||
 					    ($extension eq "4" &&
-					     defined $qualifier && $qualifier !~ /^cc/)) {
+					     defined $qualifier && $qualifier !~ /^c[hnlbc]/)) {
 						$bad_specifier = $specifier;
 						last;
 					}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 2/3] printf: add tests for generic FourCCs
       [not found] <8153cb02-d8f1-4e59-b2d5-0dfdde7a832e@live.com>
  2025-04-07 13:36 ` [PATCH v3 1/3] lib/vsprintf: Add support for generic FourCCs by extending %p4cc Aditya Garg
@ 2025-04-07 13:37 ` Aditya Garg
  2025-04-07 13:42   ` Aditya Garg
  2025-04-07 13:37 ` [PATCH v3 3/3] drm/appletbdrm: use %p4cl instead of %p4cc Aditya Garg
  2 siblings, 1 reply; 7+ messages in thread
From: Aditya Garg @ 2025-04-07 13:37 UTC (permalink / raw)
  To: alyssa, Petr Mladek, Andy Shevchenko, Sven Peter,
	Thomas Zimmermann, Aun-Ali Zaidi, Maxime Ripard, airlied,
	Simona Vetter, Steven Rostedt, Rasmus Villemoes,
	Sergey Senozhatsky, Jonathan Corbet, Andrew Morton, apw, joe,
	dwaipayanray1, lukas.bulwahn, Kees Cook, tamird
  Cc: Linux Kernel Mailing List, dri-devel, linux-doc, Hector Martin,
	Asahi Linux Mailing List

From: Aditya Garg <gargaditya08@live.com>

This patch adds support for kunit tests of generic 32-bit FourCCs added to
vsprintf.

Acked-by: Tamir Duberstein <tamird@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Aditya Garg <gargaditya08@live.com>
---
 lib/tests/printf_kunit.c | 39 ++++++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)

diff --git a/lib/tests/printf_kunit.c b/lib/tests/printf_kunit.c
index 2c9f6170b..b1fa0dcea 100644
--- a/lib/tests/printf_kunit.c
+++ b/lib/tests/printf_kunit.c
@@ -701,21 +701,46 @@ static void fwnode_pointer(struct kunit *kunittest)
 	software_node_unregister_node_group(group);
 }
 
+struct fourcc_struct {
+	u32 code;
+	const char *str;
+};
+
+static void fourcc_pointer_test(struct kunit *kunittest, const struct fourcc_struct *fc,
+				size_t n, const char *fmt)
+{
+	size_t i;
+
+	for (i = 0; i < n; i++)
+		test(fc[i].str, fmt, &fc[i].code);
+}
+
 static void fourcc_pointer(struct kunit *kunittest)
 {
-	struct {
-		u32 code;
-		char *str;
-	} const try[] = {
+	static const struct fourcc_struct try_cc[] = {
 		{ 0x3231564e, "NV12 little-endian (0x3231564e)", },
 		{ 0xb231564e, "NV12 big-endian (0xb231564e)", },
 		{ 0x10111213, ".... little-endian (0x10111213)", },
 		{ 0x20303159, "Y10  little-endian (0x20303159)", },
 	};
-	unsigned int i;
+	static const struct fourcc_struct try_ch[] = {
+		{ 0x41424344, "ABCD (0x41424344)", },
+	};
+	static const struct fourcc_struct try_cn[] = {
+		{ 0x41424344, "DCBA (0x44434241)", },
+	};
+	static const struct fourcc_struct try_cl[] = {
+		{ (__force u32)cpu_to_le32(0x41424344), "ABCD (0x41424344)", },
+	};
+	static const struct fourcc_struct try_cb[] = {
+		{ (__force u32)cpu_to_be32(0x41424344), "ABCD (0x41424344)", },
+	};
 
-	for (i = 0; i < ARRAY_SIZE(try); i++)
-		test(try[i].str, "%p4cc", &try[i].code);
+	fourcc_pointer_test(kunittest, try_cc, ARRAY_SIZE(try_cc), "%p4cc");
+	fourcc_pointer_test(kunittest, try_ch, ARRAY_SIZE(try_ch), "%p4ch");
+	fourcc_pointer_test(kunittest, try_cn, ARRAY_SIZE(try_cn), "%p4cn");
+	fourcc_pointer_test(kunittest, try_cl, ARRAY_SIZE(try_cl), "%p4cl");
+	fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb");
 }
 
 static void
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 3/3] drm/appletbdrm: use %p4cl instead of %p4cc
       [not found] <8153cb02-d8f1-4e59-b2d5-0dfdde7a832e@live.com>
  2025-04-07 13:36 ` [PATCH v3 1/3] lib/vsprintf: Add support for generic FourCCs by extending %p4cc Aditya Garg
  2025-04-07 13:37 ` [PATCH v3 2/3] printf: add tests for generic FourCCs Aditya Garg
@ 2025-04-07 13:37 ` Aditya Garg
  2025-04-07 13:39   ` Alyssa Rosenzweig
  2025-04-07 13:54   ` Andy Shevchenko
  2 siblings, 2 replies; 7+ messages in thread
From: Aditya Garg @ 2025-04-07 13:37 UTC (permalink / raw)
  To: alyssa, Petr Mladek, Andy Shevchenko, Sven Peter,
	Thomas Zimmermann, Aun-Ali Zaidi, Maxime Ripard, airlied,
	Simona Vetter, Steven Rostedt, Rasmus Villemoes,
	Sergey Senozhatsky, Jonathan Corbet, Andrew Morton, apw, joe,
	dwaipayanray1, lukas.bulwahn, Kees Cook, tamird
  Cc: Linux Kernel Mailing List, dri-devel, linux-doc, Hector Martin,
	Asahi Linux Mailing List

From: Aditya Garg <gargaditya08@live.com>

Due to lack of a proper printk format, %p4cc was being used instead of
%p4cl for the purpose of printing FourCCs. But the disadvange was that
they were being printed in a reverse order. %p4cl should correct this
issue.

Signed-off-by: Aditya Garg <gargaditya08@live.com>
---
 drivers/gpu/drm/tiny/appletbdrm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/tiny/appletbdrm.c b/drivers/gpu/drm/tiny/appletbdrm.c
index 703b9a41a..751b05753 100644
--- a/drivers/gpu/drm/tiny/appletbdrm.c
+++ b/drivers/gpu/drm/tiny/appletbdrm.c
@@ -212,7 +212,7 @@ static int appletbdrm_read_response(struct appletbdrm_device *adev,
 	}
 
 	if (response->msg != expected_response) {
-		drm_err(drm, "Unexpected response from device (expected %p4cc found %p4cc)\n",
+		drm_err(drm, "Unexpected response from device (expected %p4cl found %p4cl)\n",
 			&expected_response, &response->msg);
 		return -EIO;
 	}
@@ -286,7 +286,7 @@ static int appletbdrm_get_information(struct appletbdrm_device *adev)
 	}
 
 	if (pixel_format != APPLETBDRM_PIXEL_FORMAT) {
-		drm_err(drm, "Encountered unknown pixel format (%p4cc)\n", &pixel_format);
+		drm_err(drm, "Encountered unknown pixel format (%p4cl)\n", &pixel_format);
 		ret = -EINVAL;
 		goto free_info;
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 3/3] drm/appletbdrm: use %p4cl instead of %p4cc
  2025-04-07 13:37 ` [PATCH v3 3/3] drm/appletbdrm: use %p4cl instead of %p4cc Aditya Garg
@ 2025-04-07 13:39   ` Alyssa Rosenzweig
  2025-04-07 13:54   ` Andy Shevchenko
  1 sibling, 0 replies; 7+ messages in thread
From: Alyssa Rosenzweig @ 2025-04-07 13:39 UTC (permalink / raw)
  To: Aditya Garg
  Cc: Petr Mladek, Andy Shevchenko, Sven Peter, Thomas Zimmermann,
	Aun-Ali Zaidi, Maxime Ripard, airlied, Simona Vetter,
	Steven Rostedt, Rasmus Villemoes, Sergey Senozhatsky,
	Jonathan Corbet, Andrew Morton, apw, joe, dwaipayanray1,
	lukas.bulwahn, Kees Cook, tamird, Linux Kernel Mailing List,
	dri-devel, linux-doc, Hector Martin, Asahi Linux Mailing List

Reviewed-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>

Le Mon , Apr 07, 2025 at 07:07:54PM +0530, Aditya Garg a écrit :
> From: Aditya Garg <gargaditya08@live.com>
> 
> Due to lack of a proper printk format, %p4cc was being used instead of
> %p4cl for the purpose of printing FourCCs. But the disadvange was that
> they were being printed in a reverse order. %p4cl should correct this
> issue.
> 
> Signed-off-by: Aditya Garg <gargaditya08@live.com>
> ---
>  drivers/gpu/drm/tiny/appletbdrm.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/tiny/appletbdrm.c b/drivers/gpu/drm/tiny/appletbdrm.c
> index 703b9a41a..751b05753 100644
> --- a/drivers/gpu/drm/tiny/appletbdrm.c
> +++ b/drivers/gpu/drm/tiny/appletbdrm.c
> @@ -212,7 +212,7 @@ static int appletbdrm_read_response(struct appletbdrm_device *adev,
>  	}
>  
>  	if (response->msg != expected_response) {
> -		drm_err(drm, "Unexpected response from device (expected %p4cc found %p4cc)\n",
> +		drm_err(drm, "Unexpected response from device (expected %p4cl found %p4cl)\n",
>  			&expected_response, &response->msg);
>  		return -EIO;
>  	}
> @@ -286,7 +286,7 @@ static int appletbdrm_get_information(struct appletbdrm_device *adev)
>  	}
>  
>  	if (pixel_format != APPLETBDRM_PIXEL_FORMAT) {
> -		drm_err(drm, "Encountered unknown pixel format (%p4cc)\n", &pixel_format);
> +		drm_err(drm, "Encountered unknown pixel format (%p4cl)\n", &pixel_format);
>  		ret = -EINVAL;
>  		goto free_info;
>  	}
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 2/3] printf: add tests for generic FourCCs
  2025-04-07 13:37 ` [PATCH v3 2/3] printf: add tests for generic FourCCs Aditya Garg
@ 2025-04-07 13:42   ` Aditya Garg
  2025-04-07 15:09     ` Petr Mladek
  0 siblings, 1 reply; 7+ messages in thread
From: Aditya Garg @ 2025-04-07 13:42 UTC (permalink / raw)
  To: alyssa, Petr Mladek, Andy Shevchenko, Sven Peter,
	Thomas Zimmermann, Aun-Ali Zaidi, Maxime Ripard, airlied,
	Simona Vetter, Steven Rostedt, Rasmus Villemoes,
	Sergey Senozhatsky, Jonathan Corbet, Andrew Morton, apw, joe,
	dwaipayanray1, lukas.bulwahn, Kees Cook, tamird
  Cc: Linux Kernel Mailing List, dri-devel, linux-doc, Hector Martin,
	Asahi Linux Mailing List



On 07/04/25 7:07 pm, Aditya Garg wrote:
> From: Aditya Garg <gargaditya08@live.com>
> 
> This patch adds support for kunit tests of generic 32-bit FourCCs added to
> vsprintf.
> 
> Acked-by: Tamir Duberstein <tamird@gmail.com>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Signed-off-by: Aditya Garg <gargaditya08@live.com>
> ---

Petr are you fine with this here?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 3/3] drm/appletbdrm: use %p4cl instead of %p4cc
  2025-04-07 13:37 ` [PATCH v3 3/3] drm/appletbdrm: use %p4cl instead of %p4cc Aditya Garg
  2025-04-07 13:39   ` Alyssa Rosenzweig
@ 2025-04-07 13:54   ` Andy Shevchenko
  1 sibling, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2025-04-07 13:54 UTC (permalink / raw)
  To: Aditya Garg
  Cc: alyssa, Petr Mladek, Sven Peter, Thomas Zimmermann, Aun-Ali Zaidi,
	Maxime Ripard, airlied, Simona Vetter, Steven Rostedt,
	Rasmus Villemoes, Sergey Senozhatsky, Jonathan Corbet,
	Andrew Morton, apw, joe, dwaipayanray1, lukas.bulwahn, Kees Cook,
	tamird, Linux Kernel Mailing List, dri-devel, linux-doc,
	Hector Martin, Asahi Linux Mailing List

On Mon, Apr 07, 2025 at 07:07:54PM +0530, Aditya Garg wrote:
> From: Aditya Garg <gargaditya08@live.com>
> 
> Due to lack of a proper printk format, %p4cc was being used instead of

s/printk format/format specifier/

That's basically the term (`man printf`) everybody knows.

> %p4cl for the purpose of printing FourCCs. But the disadvange was that
> they were being printed in a reverse order. %p4cl should correct this
> issue.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 2/3] printf: add tests for generic FourCCs
  2025-04-07 13:42   ` Aditya Garg
@ 2025-04-07 15:09     ` Petr Mladek
  0 siblings, 0 replies; 7+ messages in thread
From: Petr Mladek @ 2025-04-07 15:09 UTC (permalink / raw)
  To: Aditya Garg
  Cc: alyssa, Andy Shevchenko, Sven Peter, Thomas Zimmermann,
	Aun-Ali Zaidi, Maxime Ripard, airlied, Simona Vetter,
	Steven Rostedt, Rasmus Villemoes, Sergey Senozhatsky,
	Jonathan Corbet, Andrew Morton, apw, joe, dwaipayanray1,
	lukas.bulwahn, Kees Cook, tamird, Linux Kernel Mailing List,
	dri-devel, linux-doc, Hector Martin, Asahi Linux Mailing List

On Mon 2025-04-07 19:12:39, Aditya Garg wrote:
> 
> 
> On 07/04/25 7:07 pm, Aditya Garg wrote:
> > From: Aditya Garg <gargaditya08@live.com>
> > 
> > This patch adds support for kunit tests of generic 32-bit FourCCs added to
> > vsprintf.
> > 
> > Acked-by: Tamir Duberstein <tamird@gmail.com>
> > Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > Signed-off-by: Aditya Garg <gargaditya08@live.com>
> > ---
> 
> Petr are you fine with this here?

Yes, looks good:

Reviewed-by: Petr Mladek <pmladek@suse.com>
Tested-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-04-07 15:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <8153cb02-d8f1-4e59-b2d5-0dfdde7a832e@live.com>
2025-04-07 13:36 ` [PATCH v3 1/3] lib/vsprintf: Add support for generic FourCCs by extending %p4cc Aditya Garg
2025-04-07 13:37 ` [PATCH v3 2/3] printf: add tests for generic FourCCs Aditya Garg
2025-04-07 13:42   ` Aditya Garg
2025-04-07 15:09     ` Petr Mladek
2025-04-07 13:37 ` [PATCH v3 3/3] drm/appletbdrm: use %p4cl instead of %p4cc Aditya Garg
2025-04-07 13:39   ` Alyssa Rosenzweig
2025-04-07 13:54   ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).