Skip to contentSkip to conversation

GUD USB Display driver

PATCH Superseded Noralf Trønnes 2 participants 15 messages Feb 19, 2021 – Mar 1, 2021 1 message not in the archive

Subsystems (4)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover GUD USB Display driver

Hi,

A while back I had the idea to turn a Raspberry Pi Zero into a $5 USB to HDMI/SDTV/DPI display adapter.

The protocol is open so people are free to make displays implementing it and use this driver, all that's needed is to add a USB vid:pid to the driver for the display.

See the wiki[1] for more information and images for the Raspberry Pi Zero/4.

Changes in this version:
- Use obj-y in Makefile (Peter)
- Fix missing le32_to_cpu() when using GUD_DISPLAY_MAGIC (Peter)
- Set initial brightness on backlight device

Noralf.

[1] https://github.com/notro/gud/wiki

Noralf Trønnes (3):
drm/uapi: Add USB connector type
drm/probe-helper: Check epoch counter in output_poll_execute()
drm: Add GUD USB Display driver

 MAINTAINERS                         |   8 +
 drivers/gpu/drm/Kconfig             |   2 +
 drivers/gpu/drm/Makefile            |   1 +
 drivers/gpu/drm/drm_connector.c     |   1 +
 drivers/gpu/drm/drm_probe_helper.c  |   7 +-
 drivers/gpu/drm/gud/Kconfig         |  14 +
 drivers/gpu/drm/gud/Makefile        |   4 +
 drivers/gpu/drm/gud/gud_connector.c | 738 ++++++++++++++++++++++++++++
 drivers/gpu/drm/gud/gud_drv.c       | 625 +++++++++++++++++++++++
 drivers/gpu/drm/gud/gud_internal.h  | 149 ++++++
 drivers/gpu/drm/gud/gud_pipe.c      | 475 ++++++++++++++++++
 include/drm/gud.h                   | 356 ++++++++++++++
 include/uapi/drm/drm_mode.h         |   1 +
 13 files changed, 2380 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/gud/Kconfig
 create mode 100644 drivers/gpu/drm/gud/Makefile
 create mode 100644 drivers/gpu/drm/gud/gud_connector.c
 create mode 100644 drivers/gpu/drm/gud/gud_drv.c
 create mode 100644 drivers/gpu/drm/gud/gud_internal.h
 create mode 100644 drivers/gpu/drm/gud/gud_pipe.c
 create mode 100644 include/drm/gud.h
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH v6 1/3 drm/uapi: Add USB connector type

Add a connector type for USB connected display panels.

Some examples of what current userspace will name the connector:
- Weston: "UNNAMED-%d"
- Mutter: "Unknown20-%d"
- X: "Unknown20-%d"

v2:
- Update drm_connector_enum_list
- Add examples to commit message

---
 drivers/gpu/drm/drm_connector.c | 1 +
 include/uapi/drm/drm_mode.h     | 1 +
 2 files changed, 2 insertions(+)
drivers/gpu/drm/drm_connector.c +1 -0
index 717c4e7271b0..52712daed3bc 100644
@@ -94,6 +94,7 @@ static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
 	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
 	{ DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
 	{ DRM_MODE_CONNECTOR_SPI, "SPI" },
+	{ DRM_MODE_CONNECTOR_USB, "USB" },
 };
 
 void drm_connector_ida_init(void)
include/uapi/drm/drm_mode.h +1 -0
index fed66a03c7ae..33024cc5d26e 100644
@@ -367,6 +367,7 @@ enum drm_mode_subconnector {
 #define DRM_MODE_CONNECTOR_DPI		17
 #define DRM_MODE_CONNECTOR_WRITEBACK	18
 #define DRM_MODE_CONNECTOR_SPI		19
+#define DRM_MODE_CONNECTOR_USB		20
 
 /**
  * struct drm_mode_get_connector - Get connector metadata.
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

PATCH v6 2/3 drm/probe-helper: Check epoch counter in output_poll_execute()

In reply to Noralf Trønnes

drm_helper_hpd_irq_event() checks the epoch counter to determine connector status change. This was introduced in
commit 5186421cbfe2 ("drm: Introduce epoch counter to drm_connector"). Do the same for output_poll_execute() so it can detect other changes beside connection status value changes.

v2:
- Add Fixes tag (Daniel)

Fixes 5186421cbfe2 ("drm: Introduce epoch counter to drm_connector")
---
 drivers/gpu/drm/drm_probe_helper.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_probe_helper.c +6 -1
index d6017726cc2a..e5432dcf6999 100644
@@ -623,6 +623,7 @@ static void output_poll_execute(struct work_struct *work)
 	struct drm_connector_list_iter conn_iter;
 	enum drm_connector_status old_status;
 	bool repoll = false, changed;
+	u64 old_epoch_counter;
 
 	if (!dev->mode_config.poll_enabled)
 		return;
@@ -659,8 +660,9 @@ static void output_poll_execute(struct work_struct *work)
 
 		repoll = true;
 
+		old_epoch_counter = connector->epoch_counter;
 		connector->status = drm_helper_probe_detect(connector, NULL, false);
-		if (old_status != connector->status) {
+		if (old_epoch_counter != connector->epoch_counter) {
 			const char *old, *new;
 
 			/*
@@ -689,6 +691,9 @@ static void output_poll_execute(struct work_struct *work)
 				      connector->base.id,
 				      connector->name,
 				      old, new);
+			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] epoch counter %llu -> %llu\n",
+				      connector->base.id, connector->name,
+				      old_epoch_counter, connector->epoch_counter);
 
 			changed = true;
 		}
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw

PATCH v6 3/3 drm: Add GUD USB Display driver

In reply to Noralf Trønnes

This adds a USB display driver with the intention that it can be used with future USB interfaced low end displays/adapters. The Linux gadget device driver will serve as the canonical device implementation.

The following DRM properties are supported:
- Plane rotation
- Connector TV properties

There is also support for backlight brightness exposed as a backlight device.

Display modes can be made available to the host driver either as DRM display modes or through EDID. If both are present, EDID is just passed on to userspace.

Performance is preferred over color depth, so if the device supports RGB565, DRM_CAP_DUMB_PREFERRED_DEPTH will return 16.

If the device transfer buffer can't fit an uncompressed framebuffer update, the update is split up into parts that do fit.

Optimal user experience is achieved by providing damage reports either by setting FB_DAMAGE_CLIPS on pageflips or calling DRM_IOCTL_MODE_DIRTYFB.

LZ4 compression is used if the device supports it.

The driver supports a one bit monochrome transfer format: R1. This is not implemented in the gadget driver. It is added in preparation for future monochrome e-ink displays.

The driver is MIT licensed to smooth the path for any BSD port of the driver.

v2:
- Use devm_drm_dev_alloc() and drmm_mode_config_init()
- drm_fbdev_generic_setup: Use preferred_bpp=0, 16 was a copy paste error
- The drm_backlight_helper is dropped, copy in the code
- Support protocol version backwards compatibility for device

v3:
- Use donated Openmoko USB pid
- Use direct compression from framebuffer when pitch matches, not only on full frames, so split updates can benefit
- Use __le16 in struct gud_drm_req_get_connector_status
- Set edid property when the device only provides edid
- Clear compression fields in struct gud_drm_req_set_buffer
- Fix protocol version negotiation
- Remove mode->vrefresh, it's calculated

v4:
- Drop the status req polling which was a workaround for something that turned out to be a dwc2 udc driver problem
- Add a flag for the Linux gadget to require a status request on SET operations. Other devices will only get status req on STALL errors
- Use protocol specific error codes (Peter)
- Add a flag for devices that want to receive the entire framebuffer on each flush (Lubomir)
- Retry a failed framebuffer flush
- If mode has changed wait for worker and clear pending damage before queuing up new damage, fb width/height might have changed
- Increase error counter on bulk transfer failures
- Use DRM_MODE_CONNECTOR_USB
- Handle R1 kmalloc error (Peter)
- Don't try and replicate the USB get descriptor request standard for the display descriptor (Peter)
- Make max_buffer_size optional (Peter), drop the pow2 requirement since it's not necessary anymore.
- Don't pre-alloc a control request buffer, it was only 4k
- Let gud.h describe the whole protocol explicitly and don't let DRM leak into it (Peter)
- Drop display mode .hskew and .vscan from the protocol
- Shorten names: s/GUD_DRM_/GUD_/ s/gud_drm_/gud_/ (Peter)
- Fix gud_pipe_check() connector picking when switching connector
- Drop gud_drm_driver_gem_create_object() cached is default now
- Retrieve USB device from struct drm_device.dev instead of keeping a pointer
- Honour fb->offsets[0]
- Fix mode fetching when connector status is forced
- Check EDID length reported by the device
- Use drm_do_get_edid() so userspace can overrride EDID
- Set epoch counter to signal connector status change
- gud_drm_driver can be const now

v5:
- GUD_DRM_FORMAT_R1: Use non-human ascii values (Daniel)
- Change name to: GUD USB Display (Thomas, Simon)
- Change one __u32 -> __le32 in protocol header
- Always log fb flush errors, unless the previous one failed
- Run backlight update in a worker to avoid upsetting lockdep (Daniel)
- Drop backlight_ops.get_brightness, there's no readback from the device so it doesn't really add anything.
- Set dma mask, needed by dma-buf importers

v6:
- Use obj-y in Makefile (Peter)
- Fix missing le32_to_cpu() when using GUD_DISPLAY_MAGIC (Peter)
- Set initial brightness on backlight device

---
 MAINTAINERS                         |   8 +
 drivers/gpu/drm/Kconfig             |   2 +
 drivers/gpu/drm/Makefile            |   1 +
 drivers/gpu/drm/gud/Kconfig         |  14 +
 drivers/gpu/drm/gud/Makefile        |   4 +
 drivers/gpu/drm/gud/gud_connector.c | 738 ++++++++++++++++++++++++++++
 drivers/gpu/drm/gud/gud_drv.c       | 625 +++++++++++++++++++++++
 drivers/gpu/drm/gud/gud_internal.h  | 149 ++++++
 drivers/gpu/drm/gud/gud_pipe.c      | 475 ++++++++++++++++++
 include/drm/gud.h                   | 356 ++++++++++++++
 10 files changed, 2372 insertions(+)
 create mode 100644 drivers/gpu/drm/gud/Kconfig
 create mode 100644 drivers/gpu/drm/gud/Makefile
 create mode 100644 drivers/gpu/drm/gud/gud_connector.c
 create mode 100644 drivers/gpu/drm/gud/gud_drv.c
 create mode 100644 drivers/gpu/drm/gud/gud_internal.h
 create mode 100644 drivers/gpu/drm/gud/gud_pipe.c
 create mode 100644 include/drm/gud.h
MAINTAINERS +8 -0
index 00112c044608..e7f71ac55f08 100644
@@ -5525,6 +5525,14 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/display/panel/feiyang,fy07024di26a30d.yaml
 F:	drivers/gpu/drm/panel/panel-feiyang-fy07024di26a30d.c
 
+DRM DRIVER FOR GENERIC USB DISPLAY
+M:	Noralf Trønnes <[email protected]>
+S:	Maintained
+W:	https://github.com/notro/gud/wiki
+T:	git git://anongit.freedesktop.org/drm/drm-misc
+F:	drivers/gpu/drm/gud/
+F:	include/drm/gud.h
+
 DRM DRIVER FOR GRAIN MEDIA GM12U320 PROJECTORS
 M:	Hans de Goede <[email protected]>
 S:	Maintained
drivers/gpu/drm/Kconfig +2 -0
index 147d61b9674e..6d3f47933e51 100644
@@ -388,6 +388,8 @@ source "drivers/gpu/drm/tidss/Kconfig"
 
 source "drivers/gpu/drm/xlnx/Kconfig"
 
+source "drivers/gpu/drm/gud/Kconfig"
+
 # Keep legacy drivers last
 
 menuconfig DRM_LEGACY
drivers/gpu/drm/Makefile +1 -0
index 81569009f884..78dd8e12525d 100644
@@ -124,3 +124,4 @@ obj-$(CONFIG_DRM_ASPEED_GFX) += aspeed/
 obj-$(CONFIG_DRM_MCDE) += mcde/
 obj-$(CONFIG_DRM_TIDSS) += tidss/
 obj-y			+= xlnx/
+obj-y			+= gud/
drivers/gpu/drm/gud/Kconfig new file +14 -0
new file mode 100644
index 000000000000..1c8601bf4d91
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config DRM_GUD
+	tristate "GUD USB Display"
+	depends on DRM && USB
+	select LZ4_COMPRESS
+	select DRM_KMS_HELPER
+	select DRM_GEM_SHMEM_HELPER
+	select BACKLIGHT_CLASS_DEVICE
+	help
+	  This is a DRM display driver for GUD USB Displays or display
+	  adapters.
+
+	  If M is selected the module will be called gud.
drivers/gpu/drm/gud/Makefile new file +4 -0
new file mode 100644
index 000000000000..68a1c622cf33
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+
+gud-y				:= gud_drv.o gud_pipe.o gud_connector.o
+obj-$(CONFIG_DRM_GUD)		+= gud.o
drivers/gpu/drm/gud/gud_connector.c new file +738 -0

738 lines not on this page · raw message

drivers/gpu/drm/gud/gud_drv.c new file +625 -0

625 lines not on this page · raw message

drivers/gpu/drm/gud/gud_internal.h new file +149 -0

149 lines not on this page · raw message

drivers/gpu/drm/gud/gud_pipe.c new file +475 -0

475 lines not on this page · raw message

include/drm/gud.h new file +356 -0

356 lines not on this page · raw message

Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#4 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Hi Noralf,

Quoted 1 line · Noralf Trønnes

+++ b/drivers/gpu/drm/gud/gud_connector.c

..

Quoted 1 line

+static int gud_connector_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)

..

Quoted 18 lines
drivers/gpu/drm/gud/gud_connector.c new file +18 -0
+	struct gud_connector *gconn = ctx->gconn;
+	size_t start = block * EDID_LENGTH;
+
+	if (start + len > gconn->edid_len)
+		return -1;
+
+	if (!block) {
+		struct gud_device *gdrm = to_gud_device(gconn->connector.dev);
+		int ret;
+
+		/* Check because drm_do_get_edid() will retry on failure */
+		if (!ctx->buf)
+			ctx->buf = kmalloc(gconn->edid_len, GFP_KERNEL);
+		if (!ctx->buf)
+			return -1;
+
+		ret = gud_usb_get(gdrm, GUD_REQ_GET_CONNECTOR_EDID, gconn->connector.index,
+				  ctx->buf, gconn->edid_len);

..

drivers/gpu/drm/gud/gud_connector.c line 252

Quoted 1 line
drivers/gpu/drm/gud/gud_connector.c new file +1 -0
+	memcpy(buf, ctx->buf + start, len);

Danger, danger?

gconn->edid_len in this call to gud_usb_get() comes from the device in gud_connector_status_request() where the only validation is that edid_len % EDID_LENGTH == 0, so a device could write past the buffer if drm_do_get_edid() passes a buffer smaller than edid_len.

I guess the buffer passed is just 128, EDID_LENGTH, so a malicious or buggy device could overwrite 64k-128 kernel memory? Ouch!

More generally it's not very typical in USB to report the data size separately from the data itself, if reporting size explicitly at all.

Sizes can be part of the data structure itself (like in descriptors) but on the application layer (like here) it's convenient to just decide a sensible fixed maximum size and let the host try to always transfer that size while accepting short transfers. Unlike read() a short transfer only ever happens if and when a device intends for it, so that's like an in-band handshake but "for free".

Oh, and does/should the GUD EDID change if the panel "behind" the device CPU on a hotpluggable connector changes? It wouldn't be great to require GUD driver reprobe in that case. But maybe DRM requires that anyway?

I'm sorry I didn't spot this pattern earlier, I understand that it's late in the game and that changing it needs the gadget to change as well, but I do really think this is a worthwhile change throughout the protocol.

And I think it applies to more than EDID, e.g. both GUD and connector properties, maybe formats, something else?

Unfortunately, the gud_usb_control_msg() check (ret != len) creates a requirement to know in advance how much data will be transfered.

That could be revised at least for the general case, even if not used everywhere; maybe something like adding a size_t required_min_len parameter to gud_usb_control_msg()?

drivers/gpu/drm/gud/gud_connector.c line 265

Quoted 9 lines
drivers/gpu/drm/gud/gud_connector.c new file +9 -0
+static int gud_connector_get_modes(struct drm_connector *connector)
+{
+	struct gud_connector *gconn = to_gud_connector(connector);
+	struct gud_device *gdrm = to_gud_device(connector->dev);
+	struct gud_connector_get_edid_ctx edid_ctx = {
+		.gconn = gconn,
+	};
+	struct gud_display_mode_req *reqmodes = NULL;
+	unsigned int i, num_modes = 0;

The error path of this function executes "return num_modes" with num_modes unmodified; ie. 0. Is that intentional?

Quoted 1 line

+static int gud_connector_add_tv_mode(struct gud_device *gdrm,

..

drivers/gpu/drm/gud/gud_connector.c line 423

Quoted 3 lines
drivers/gpu/drm/gud/gud_connector.c new file +3 -0
+	buf_len = num_modes * GUD_CONNECTOR_TV_MODE_NAME_LEN;
+	modes = kmalloc_array(num_modes, sizeof(*modes), GFP_KERNEL);
+	buf = kmalloc(buf_len, GFP_KERNEL);

Maybe moving the buf assignment immediately following the buf_len assignment would help readability? This is quite minor.

Quoted 6 lines
drivers/gpu/drm/gud/gud_connector.c new file +6 -0
+static int gud_connector_add_properties(struct gud_device *gdrm, struct gud_connector *gconn,
+					unsigned int num_properties)
+{
+	struct drm_device *drm = &gdrm->drm;
+	struct drm_connector *connector = &gconn->connector;
+	struct gud_property_req *properties;

drivers/gpu/drm/gud/gud_connector.c line 523

Quoted 10 lines
drivers/gpu/drm/gud/gud_connector.c new file +10 -0
+	unsigned int i;
+	int ret;
+
+	gconn->properties = kcalloc(num_properties, sizeof(*gconn->properties), GFP_KERNEL);
+	if (!gconn->properties)
+		return -ENOMEM;
+
+	properties = kcalloc(num_properties, sizeof(*properties), GFP_KERNEL);
+	if (!properties)
+		return -ENOMEM;

I think this error path leaks gconn->properties?

Quoted 1 line

+int gud_connector_create(struct gud_device *gdrm, unsigned int index)

Most error paths in this function seem to leak both gconn and connector?

Quoted 1 line

+++ b/drivers/gpu/drm/gud/gud_drv.c

..

Quoted 1 line

+static int gud_usb_get_status(struct usb_device *usb, u8 ifnum, u8 *status)

..

drivers/gpu/drm/gud/gud_drv.c line 114

Quoted 2 lines
drivers/gpu/drm/gud/gud_drv.c new file +2 -0
+	ret = gud_usb_control_msg(usb, ifnum, true, GUD_REQ_GET_STATUS, 0, buf, sizeof(*buf));
+	*status = *buf;

Maybe make this conditional on 0 == ret.

Quoted 1 line

+static int gud_set_version(struct usb_device *usb, u8 ifnum, u32 flags, u8 version)

..

drivers/gpu/drm/gud/gud_drv.c line 227

Quoted 2 lines
drivers/gpu/drm/gud/gud_drv.c new file +2 -0
+	if (ret == -EPIPE)
+		return -EPROTONOSUPPORT;

So yeah, this isn't typical, devices usually describe optional things that the driver may need to know about, unless it's something that can change during operation.

Arguably mildly contradictory to the short transfer pattern, but one is capability and the other is "runtime" data.

Quoted 7 lines
drivers/gpu/drm/gud/gud_drv.c new file +7 -0
+static int gud_get_properties(struct gud_device *gdrm, unsigned int num_properties)
+{
+	struct gud_property_req *properties;
+	unsigned int i;
+	int ret;
+
+	if (!num_properties)

drivers/gpu/drm/gud/gud_drv.c line 258

Quoted 10 lines
drivers/gpu/drm/gud/gud_drv.c new file +10 -0
+		return 0;
+
+	gdrm->properties = drmm_kcalloc(&gdrm->drm, num_properties, sizeof(*gdrm->properties),
+					GFP_KERNEL);
+	if (!gdrm->properties)
+		return -ENOMEM;
+
+	properties = kcalloc(num_properties, sizeof(*properties), GFP_KERNEL);
+	if (!properties)
+		return -ENOMEM;

It looks like this function leaks gdrm->properties in all error paths?

drivers/gpu/drm/gud/gud_drv.c line 281

Quoted 3 lines
drivers/gpu/drm/gud/gud_drv.c new file +3 -0
+		default:
+			/* New ones might show up in future devices, skip those we don't know. */
+			drm_dbg(&gdrm->drm, "Unknown property: %u\n", prop);

Maybe "Ignoring unknown property: %u\n" would be a little more clear?

Quoted 1 line

+static int gud_stats_debugfs(struct seq_file *m, void *data)

..

drivers/gpu/drm/gud/gud_drv.c line 309

Quoted 4 lines
drivers/gpu/drm/gud/gud_drv.c new file +4 -0
+	seq_puts(m, "Compression:      ");
+	if (gdrm->compression & GUD_COMPRESSION_LZ4)
+		seq_puts(m, " lz4");
+	seq_puts(m, "\n");

Maybe an explicit seq_puts(m, " none") if there are none?

drivers/gpu/drm/gud/gud_drv.c line 318

Quoted 8 lines
drivers/gpu/drm/gud/gud_drv.c new file +8 -0
+	if (gdrm->compression) {
+		u64 remainder;
+		u64 ratio = div64_u64_rem(gdrm->stats_length, gdrm->stats_actual_length,
+					  &remainder);
+		u64 ratio_frac = div64_u64(remainder * 10, gdrm->stats_actual_length);
+
+		seq_printf(m, "Compression ratio: %llu.%llu\n", ratio, ratio_frac);
+	}

Will the fraction ever need zero padding?

Quoted 1 line

+static int gud_probe(struct usb_interface *interface, const struct usb_device_id *id)

I appreciate very much that GUD works on interface level, so that it can also be used in composite devices at some point. Thanks a lot! \o/

Quoted 1 line

+++ b/drivers/gpu/drm/gud/gud_pipe.c

..

Quoted 1 line

+int gud_pipe_check(struct drm_simple_display_pipe *pipe,

..

drivers/gpu/drm/gud/gud_pipe.c line 392

Quoted 9 lines
drivers/gpu/drm/gud/gud_pipe.c new file +9 -0
+	req = kzalloc(len, GFP_KERNEL);
+	if (!req)
+		return -ENOMEM;
+
+	gud_from_display_mode(&req->mode, mode);
+
+	req->format = gud_from_fourcc(format->format);
+	if (WARN_ON_ONCE(!req->format))
+		return -EINVAL;

req leaks?

drivers/gpu/drm/gud/gud_pipe.c line 398

Quoted 5 lines
drivers/gpu/drm/gud/gud_pipe.c new file +5 -0
+	req->connector = drm_connector_index(connector);
+	req->num_properties = num_properties;
+
+	num_properties = gud_connector_fill_properties(connector, connector_state,
+						       req->properties);

Following this new assignment to num_properties the new value is used to (hopefully!) append at the first req->properties[] index after the old value was used as index, that doesn't feel great..

I mean, it's harmless as long as gud_connector_fill_properties() is sure to return the same value, but, well, maybe sometime later it doesn't, or is that guaranteed if there is no error? Then maybe at least document that requirement by the function. What do you think?

Thanks a lot

//Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Den 19.02.2021 22.42, skrev Peter Stuge:

Quoted 30 lines

Hi Noralf,

Quoted 1 line · Noralf Trønnes

+++ b/drivers/gpu/drm/gud/gud_connector.c

..

Quoted 1 line

+static int gud_connector_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)

..

Quoted 18 lines
drivers/gpu/drm/gud/gud_connector.c new file +18 -0
+	struct gud_connector *gconn = ctx->gconn;
+	size_t start = block * EDID_LENGTH;
+
+	if (start + len > gconn->edid_len)
+		return -1;
+
+	if (!block) {
+		struct gud_device *gdrm = to_gud_device(gconn->connector.dev);
+		int ret;
+
+		/* Check because drm_do_get_edid() will retry on failure */
+		if (!ctx->buf)
+			ctx->buf = kmalloc(gconn->edid_len, GFP_KERNEL);
+		if (!ctx->buf)
+			return -1;
+
+		ret = gud_usb_get(gdrm, GUD_REQ_GET_CONNECTOR_EDID, gconn->connector.index,
+				  ctx->buf, gconn->edid_len);

..

drivers/gpu/drm/gud/gud_connector.c line 252

Quoted 1 line
drivers/gpu/drm/gud/gud_connector.c new file +1 -0
+	memcpy(buf, ctx->buf + start, len);

Danger, danger?

Quoted 8 lines

gconn->edid_len in this call to gud_usb_get() comes from the device in gud_connector_status_request() where the only validation is that edid_len % EDID_LENGTH == 0, so a device could write past the buffer if drm_do_get_edid() passes a buffer smaller than edid_len.

I guess the buffer passed is just 128, EDID_LENGTH, so a malicious or buggy device could overwrite 64k-128 kernel memory? Ouch!

The result goes into ctx->buf which is big enough. Then each edid block is copied from that buffer as the parser runs the callback.

Maybe I should add a cap on ->edid_len, but I don't know how big it should be. There's no danger as such not having a cap, the host will either fail to allocate memory (max 4MB usually) or do a very big transfer and error out in the edid parser.

Quoted 11 lines

More generally it's not very typical in USB to report the data size separately from the data itself, if reporting size explicitly at all.

Sizes can be part of the data structure itself (like in descriptors) but on the application layer (like here) it's convenient to just decide a sensible fixed maximum size and let the host try to always transfer that size while accepting short transfers. Unlike read() a short transfer only ever happens if and when a device intends for it, so that's like an in-band handshake but "for free".

Quoted 4 lines

Oh, and does/should the GUD EDID change if the panel "behind" the device CPU on a hotpluggable connector changes? It wouldn't be great to require GUD driver reprobe in that case. But maybe DRM requires that anyway?

If gud_connector_status_req.status has changed since last poll or GUD_CONNECTOR_STATUS_CHANGED is set, DRM will notify userspace which will reprobe the connector. connector->epoch_counter++ in gud_connector_status_request() triggers that.

Quoted 5 lines

I'm sorry I didn't spot this pattern earlier, I understand that it's late in the game and that changing it needs the gadget to change as well, but I do really think this is a worthwhile change throughout the protocol.

I see what you mean, I'll give it a try.

Quoted 12 lines

And I think it applies to more than EDID, e.g. both GUD and connector properties, maybe formats, something else?

Unfortunately, the gud_usb_control_msg() check (ret != len) creates a requirement to know in advance how much data will be transfered.

That could be revised at least for the general case, even if not used everywhere; maybe something like adding a size_t required_min_len parameter to gud_usb_control_msg()?

Quoted 13 lines

drivers/gpu/drm/gud/gud_connector.c line 265

Quoted 9 lines
drivers/gpu/drm/gud/gud_connector.c new file +9 -0
+static int gud_connector_get_modes(struct drm_connector *connector)
+{
+	struct gud_connector *gconn = to_gud_connector(connector);
+	struct gud_device *gdrm = to_gud_device(connector->dev);
+	struct gud_connector_get_edid_ctx edid_ctx = {
+		.gconn = gconn,
+	};
+	struct gud_display_mode_req *reqmodes = NULL;
+	unsigned int i, num_modes = 0;

The error path of this function executes "return num_modes" with num_modes unmodified; ie. 0. Is that intentional?

It's not allowed to return an error code so 0 is the only option. The caller drm_helper_probe_single_connector_modes() adds a fallback 1024x768 mode in that case which probably won't fit for a display panel, but the device should reject an illegal mode on GUD_REQ_SET_STATE_CHECK so we're fine.

Quoted 21 lines
Quoted 2 lines

+static int gud_connector_add_tv_mode(struct gud_device *gdrm,

..

drivers/gpu/drm/gud/gud_connector.c line 423

Quoted 3 lines
drivers/gpu/drm/gud/gud_connector.c new file +3 -0
+	buf_len = num_modes * GUD_CONNECTOR_TV_MODE_NAME_LEN;
+	modes = kmalloc_array(num_modes, sizeof(*modes), GFP_KERNEL);
+	buf = kmalloc(buf_len, GFP_KERNEL);

Maybe moving the buf assignment immediately following the buf_len assignment would help readability? This is quite minor.

Quoted 10 lines
drivers/gpu/drm/gud/gud_connector.c new file +10 -0
+static int gud_connector_add_properties(struct gud_device *gdrm, struct gud_connector *gconn,
+					unsigned int num_properties)
+{
+	struct drm_device *drm = &gdrm->drm;
+	struct drm_connector *connector = &gconn->connector;
+	struct gud_property_req *properties;
+	unsigned int i;
+	int ret;
+
+	gconn->properties = kcalloc(num_properties, sizeof(*gconn->properties), GFP_KERNEL);
Quoted 9 lines

drivers/gpu/drm/gud/gud_connector.c line 523

Quoted 6 lines
drivers/gpu/drm/gud/gud_connector.c new file +6 -0
+	if (!gconn->properties)
+		return -ENOMEM;
+
+	properties = kcalloc(num_properties, sizeof(*properties), GFP_KERNEL);
+	if (!properties)
+		return -ENOMEM;

I think this error path leaks gconn->properties?

It's freed in gud_connector_destroy() which will be called on error at this stage.

Quoted 5 lines
Quoted 2 lines

+int gud_connector_create(struct gud_device *gdrm, unsigned int index)

Most error paths in this function seem to leak both gconn and connector?

Everything that happens after the drm_connector_init() call is cleaned up automatically when the DRM device is torn down. devm_drm_dev_alloc() and drmm_mode_config_init() sets this up.

Quoted 35 lines
Quoted 2 lines

+++ b/drivers/gpu/drm/gud/gud_drv.c

..

Quoted 1 line

+static int gud_usb_get_status(struct usb_device *usb, u8 ifnum, u8 *status)

..

drivers/gpu/drm/gud/gud_drv.c line 114

Quoted 2 lines
drivers/gpu/drm/gud/gud_drv.c new file +2 -0
+	ret = gud_usb_control_msg(usb, ifnum, true, GUD_REQ_GET_STATUS, 0, buf, sizeof(*buf));
+	*status = *buf;

Maybe make this conditional on 0 == ret.

Quoted 1 line

+static int gud_set_version(struct usb_device *usb, u8 ifnum, u32 flags, u8 version)

..

drivers/gpu/drm/gud/gud_drv.c line 227

Quoted 2 lines
drivers/gpu/drm/gud/gud_drv.c new file +2 -0
+	if (ret == -EPIPE)
+		return -EPROTONOSUPPORT;

So yeah, this isn't typical, devices usually describe optional things that the driver may need to know about, unless it's something that can change during operation.

Arguably mildly contradictory to the short transfer pattern, but one is capability and the other is "runtime" data.

Quoted 11 lines
drivers/gpu/drm/gud/gud_drv.c new file +11 -0
+static int gud_get_properties(struct gud_device *gdrm, unsigned int num_properties)
+{
+	struct gud_property_req *properties;
+	unsigned int i;
+	int ret;
+
+	if (!num_properties)
+		return 0;
+
+	gdrm->properties = drmm_kcalloc(&gdrm->drm, num_properties, sizeof(*gdrm->properties),
+					GFP_KERNEL);
Quoted 9 lines

drivers/gpu/drm/gud/gud_drv.c line 258

Quoted 6 lines
drivers/gpu/drm/gud/gud_drv.c new file +6 -0
+	if (!gdrm->properties)
+		return -ENOMEM;
+
+	properties = kcalloc(num_properties, sizeof(*properties), GFP_KERNEL);
+	if (!properties)
+		return -ENOMEM;

It looks like this function leaks gdrm->properties in all error paths?

drmm_kcalloc() is the DRM version of devm_kcalloc() which frees the memory when the last DRM fd is closed. There might be open fd's when the device goes away so the devm_ versions can't be used.

Quoted 7 lines

drivers/gpu/drm/gud/gud_drv.c line 281

Quoted 4 lines
drivers/gpu/drm/gud/gud_drv.c new file +3 -0
+		default:
+			/* New ones might show up in future devices, skip those we don't know. */
+			drm_dbg(&gdrm->drm, "Unknown property: %u\n", prop);

Maybe "Ignoring unknown property: %u\n" would be a little more clear?

Sure.

Quoted 10 lines
Quoted 2 lines

+static int gud_stats_debugfs(struct seq_file *m, void *data)

..

drivers/gpu/drm/gud/gud_drv.c line 309

Quoted 4 lines
drivers/gpu/drm/gud/gud_drv.c new file +4 -0
+	seq_puts(m, "Compression:      ");
+	if (gdrm->compression & GUD_COMPRESSION_LZ4)
+		seq_puts(m, " lz4");
+	seq_puts(m, "\n");

Maybe an explicit seq_puts(m, " none") if there are none?

That makes sense.

Quoted 12 lines

drivers/gpu/drm/gud/gud_drv.c line 318

Quoted 9 lines
drivers/gpu/drm/gud/gud_drv.c new file +8 -0
+	if (gdrm->compression) {
+		u64 remainder;
+		u64 ratio = div64_u64_rem(gdrm->stats_length, gdrm->stats_actual_length,
+					  &remainder);
+		u64 ratio_frac = div64_u64(remainder * 10, gdrm->stats_actual_length);
+
+		seq_printf(m, "Compression ratio: %llu.%llu\n", ratio, ratio_frac);
+	}

Will the fraction ever need zero padding?

No, I don't see why.

Quoted 6 lines
Quoted 2 lines

+static int gud_probe(struct usb_interface *interface, const struct usb_device_id *id)

I appreciate very much that GUD works on interface level, so that it can also be used in composite devices at some point. Thanks a lot! \o/

This was a design requirement since I wanted to make room for at least HID touch and maybe audio. It will also tolerate other vendor class interfaces and just silently ignore them.

The Pi images on the wiki has the ability to add a serial console USB interface for debugging.

Quoted 5 lines
Quoted 2 lines

+++ b/drivers/gpu/drm/gud/gud_pipe.c

..

Quoted 1 line

+int gud_pipe_check(struct drm_simple_display_pipe *pipe,

..

Quoted 12 lines

drivers/gpu/drm/gud/gud_pipe.c line 392

Quoted 9 lines
drivers/gpu/drm/gud/gud_pipe.c new file +9 -0
+	req = kzalloc(len, GFP_KERNEL);
+	if (!req)
+		return -ENOMEM;
+
+	gud_from_display_mode(&req->mode, mode);
+
+	req->format = gud_from_fourcc(format->format);
+	if (WARN_ON_ONCE(!req->format))
+		return -EINVAL;

req leaks?

Yep, you're right.

Quoted 11 lines

drivers/gpu/drm/gud/gud_pipe.c line 398

Quoted 6 lines
drivers/gpu/drm/gud/gud_pipe.c new file +5 -0
+	req->connector = drm_connector_index(connector);
+	req->num_properties = num_properties;
+
+	num_properties = gud_connector_fill_properties(connector, connector_state,
+						       req->properties);

Following this new assignment to num_properties the new value is used to (hopefully!) append at the first req->properties[] index after the old value was used as index, that doesn't feel great..

Quoted 5 lines

I mean, it's harmless as long as gud_connector_fill_properties() is sure to return the same value, but, well, maybe sometime later it doesn't, or is that guaranteed if there is no error? Then maybe at least document that requirement by the function. What do you think?

The number of properties doesn't change, it is returned by GUD_REQ_GET_CONNECTOR during probe.
There is a small comment in gud_connector_fill_properties() but I can expand on that and put it before the function definition.

Thanks for your scrutiny.

Noralf.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Den 20.02.2021 18.27, skrev Noralf Trønnes:

Quoted 26 lines

Den 19.02.2021 22.42, skrev Peter Stuge:

Quoted 11 lines

More generally it's not very typical in USB to report the data size separately from the data itself, if reporting size explicitly at all.

Sizes can be part of the data structure itself (like in descriptors) but on the application layer (like here) it's convenient to just decide a sensible fixed maximum size and let the host try to always transfer that size while accepting short transfers. Unlike read() a short transfer only ever happens if and when a device intends for it, so that's like an in-band handshake but "for free".

Quoted 4 lines

Oh, and does/should the GUD EDID change if the panel "behind" the device CPU on a hotpluggable connector changes? It wouldn't be great to require GUD driver reprobe in that case. But maybe DRM requires that anyway?

If gud_connector_status_req.status has changed since last poll or GUD_CONNECTOR_STATUS_CHANGED is set, DRM will notify userspace which will reprobe the connector. connector->epoch_counter++ in gud_connector_status_request() triggers that.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

I'm sorry I didn't spot this pattern earlier, I understand that it's late in the game and that changing it needs the gadget to change as well, but I do really think this is a worthwhile change throughout the protocol.

I see what you mean, I'll give it a try.

Peter, please have a look at this diff and see if I'm on the right track
here: https://gist.github.com/notro/a43a93a3aa0cc75d930890b7b254fc0a

I want to avoid waisting a patch version cycle by being way off.

Noralf.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Quoted 2 lines · Noralf Trønnes

Peter, please have a look at this diff and see if I'm on the right track
here: https://gist.github.com/notro/a43a93a3aa0cc75d930890b7b254fc0a

Yes that's exactly what I meant; this way the possibility for contradicting sizes is eliminated by protocol and not just by implementation - very nice!

Some more comments, sorry if this is just because of ongoing work:

Perhaps the functions taking usb_device + ifnum could take usb_interface instead - but I don't know if that would simplify or complicate things. Alan mentioned this idea in similar circumstances in another thread. I don't feel strongly, but perhaps it's cleaner.

gud_usb_control_msg() now seems almost redundant, maybe it could be removed.

In gud_usb_set() if NULL == buf then that's passed to usb_control_msg() along with len, which likely crashes if len > 0, so it may be good to check or enforce that, maybe with else len=0; before the gud_usb_transfer() call.

Finally a small style note that I'd personally change a few if (ret > 0) { blocks to have one indent level less and do each check right away, e.g. in gud_connector_get_modes():

ret = gud_usb_get()

if (ret % EDID_LENGTH) {
	drm_err();
} else if (ret > 0) {
	edid_ctx.len = ret;
	edid = drm_do_get_edid();
}

and later on in the function by the display modes one indent level could be saved with a goto:

if (ret <= 0)

	goto out;

but obviously no huge deal.

In general it's really helpful for device development to see error messages when the device behaves incorrectly, the "Invalid .. size" errors are great examples of this, but e.g. gud_get_display_descriptor() returns -EIO without a message. Maybe there are opportunities for further helpful error messages?

Thanks a lot and kind regards

//Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#8 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Den 28.02.2021 02.52, skrev Peter Stuge:

Quoted 7 lines
Quoted 2 lines · Noralf Trønnes

Peter, please have a look at this diff and see if I'm on the right track
here: https://gist.github.com/notro/a43a93a3aa0cc75d930890b7b254fc0a

Yes that's exactly what I meant; this way the possibility for contradicting sizes is eliminated by protocol and not just by implementation - very nice!

Quoted 7 lines

Some more comments, sorry if this is just because of ongoing work:

Perhaps the functions taking usb_device + ifnum could take usb_interface instead - but I don't know if that would simplify or complicate things. Alan mentioned this idea in similar circumstances in another thread. I don't feel strongly, but perhaps it's cleaner.

I agree it's cleaner, this way I don't have to store the interface number in gdrm.

Quoted 2 lines

gud_usb_control_msg() now seems almost redundant, maybe it could be removed.

There are 4 callers so I think it makes sense still.

Quoted 5 lines

In gud_usb_set() if NULL == buf then that's passed to usb_control_msg() along with len, which likely crashes if len > 0, so it may be good to check or enforce that, maybe with else len=0; before the gud_usb_transfer() call.

Ok.

Quoted 12 lines

Finally a small style note that I'd personally change a few if (ret > 0) { blocks to have one indent level less and do each check right away, e.g. in gud_connector_get_modes():

ret = gud_usb_get()

if (ret % EDID_LENGTH) {
	drm_err();
} else if (ret > 0) {
	edid_ctx.len = ret;
	edid = drm_do_get_edid();
}
Quoted 8 lines

and later on in the function by the display modes one indent level could be saved with a goto:

if (ret <= 0)

	goto out;

but obviously no huge deal.

It makes for a better read so I'll do that.

Quoted 6 lines

In general it's really helpful for device development to see error messages when the device behaves incorrectly, the "Invalid .. size" errors are great examples of this, but e.g. gud_get_display_descriptor() returns -EIO without a message. Maybe there are opportunities for further helpful error messages?

The message is printed by the caller:

	ret = gud_get_display_descriptor(intf, &desc);
	if (ret) {
		DRM_DEV_DEBUG_DRIVER(dev, "Not a display interface: ret=%d\n", ret);
		return -ENODEV;
	}

It's a debug message enabled by writing to /sys/module/drm/parameters/debug. The reason for not making it an error message, is that I want the driver to just ignore non-display vendor class interfaces so they can co-exist on the device. Someone might make an open protocol gpio (vendor class) interface driver some day, or adc, i2c, spi, rtc, or...

Thanks,
Noralf.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#9 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

In reply to Noralf Trønnes

Hi Noralf,

Quoted 3 lines · Noralf Trønnes

The driver supports a one bit monochrome transfer format: R1. This is not implemented in the gadget driver. It is added in preparation for future monochrome e-ink displays.

I forgot, but I have a two-tone (black/red) e-ink display here, and I also have a 3-bpp RGB TFT display.

Should we add maybe R2 and R3? (or R3/R8 for number of colours?)

I'm particularly considering the 3-bpp RGB panel for GUD use now, and while it will surely work with say a 16-bit RGB mode many bits will be wasted in the process.

What are your thoughts? Would you take a patch for that now, later, never?

//Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#10 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Den 25.02.2021 10.58, skrev Peter Stuge:

Quoted 10 lines

Hi Noralf,

Quoted 3 lines · Noralf Trønnes

The driver supports a one bit monochrome transfer format: R1. This is not implemented in the gadget driver. It is added in preparation for future monochrome e-ink displays.

I forgot, but I have a two-tone (black/red) e-ink display here, and I also have a 3-bpp RGB TFT display.

Quoted 8 lines

Should we add maybe R2 and R3? (or R3/R8 for number of colours?)

I'm particularly considering the 3-bpp RGB panel for GUD use now, and while it will surely work with say a 16-bit RGB mode many bits will be wasted in the process.

What are your thoughts? Would you take a patch for that now, later, never?

I've been anticipating the need for more formats, but I didn't want to add them without having a user. Otherwise I could end up adding stuff that would never be used. If you can test, there's no problem adding support for more formats now.

The R1 name is derived from DRM_FORMAT_R8 which is a 8 bit monochrome (or one color channel) format.

Linux has these one byte color pixel formats currently defined:

/* color index */
#define DRM_FORMAT_C8 fourcc_code('C', '8', ' ', ' ') /* [7:0] C */

/* 8 bpp Red */
#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */

/* 8 bpp RGB */
#define DRM_FORMAT_RGB332 fourcc_code('R', 'G', 'B', '8') /* [7:0] R:G:B
3:3:2 */
#define DRM_FORMAT_BGR233 fourcc_code('B', 'G', 'R', '8') /* [7:0] B:G:R
2:3:3 */

And these two color formats:

/* 16 bpp RG */
#define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G
8:8 little endian */
#define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R
8:8 little endian */

/* 32 bpp RG */
#define DRM_FORMAT_RG1616 fourcc_code('R', 'G', '3', '2') /* [31:0] R:G
16:16 little endian */
#define DRM_FORMAT_GR1616 fourcc_code('G', 'R', '3', '2') /* [31:0] G:R
16:16 little endian */

Building on that I would define a 2 bpp RG format like this in the driver:

static const struct drm_format_info gud_drm_format_rg11 = {
	.format = GUD_DRM_FORMAT_RG11,
	.num_planes = 1,
	.char_per_block = { 1, 0, 0 },
	.block_w = { 4, 0, 0 }, /* 4 pixels per block/byte */
	.block_h = { 1, 0, 0 },
	.hsub = 1,
	.vsub = 1,
};

And a 3 bpp RGB format like this:

static const struct drm_format_info gud_drm_format_rgb111 = {
	.format = GUD_DRM_FORMAT_RGB111,
	.num_planes = 1,
	.char_per_block = { 1, 0, 0 },
	.block_w = { 2, 0, 0 }, /* 2 pixels per block/byte */
	.block_h = { 1, 0, 0 },
	.hsub = 1,
	.vsub = 1,
};

The MIPI DBI standard defines 2 ways to transmit 2x 3-bpp pixels in one byte (X=pad bit):
- Option 1: X X R1 G1 B1 R2 G2 B2
- Option 2: X R1 G1 B1 X R2 G2 B2

So maybe we should have GUD_DRM_FORMAT_RGB111_OPTION1 and GUD_DRM_FORMAT_RGB111_OPTION2?
Or just use option 2 and let the display fix it up if needed?

What format does your 3 bpp display use?

And then something like this for the conversion function:

static size_t gud_xrgb8888_to_color(u8 *dst, const struct drm_format_info *format,
u32 *src, struct drm_framebuffer *fb, struct drm_rect *rect)

{
	unsigned int block_width = drm_format_info_block_width(format, 0);
	unsigned int x, y, width, height;
	u8 r, g, b, *block = dst; /* Assign to silence compiler warning */
	size_t len;

	WARN_ON_ONCE(format->char_per_block[0] != 1);

	/* Start on a byte boundary */
	rect->x1 = ALIGN_DOWN(rect->x1, block_width);
	width = drm_rect_width(rect);
	height = drm_rect_height(rect);
	len = drm_format_info_min_pitch(format, 0, width) * height;

	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++) {
			if (!(x % block_width)) {
				block = dst++;
				*block = 0;
			}

			/* r,g,b are bytes so no need to mask out anything explicitly */
			r = *src >> 16;
			g = *src >> 8;
			b = *src++;

			switch (format->format) {
			case GUD_DRM_FORMAT_RG11:
				*block <<= 2;
				*block |= ((r >> 7) << 1) | (g >> 7);
				break;
			case GUD_DRM_FORMAT_RGB111_OPTION1:
				*block <<= 3;
				*block |= ((r >> 7) << 2) | ((g >> 7) << 1) | (b >> 7);
				break;
			case GUD_DRM_FORMAT_RGB111_OPTION2:
				*block <<= 4;
				*block |= ((r >> 7) << 2) | ((g >> 7) << 1) | (b >> 7);
				break;
			default:
				WARN_ON_ONCE(1);
				return len;
			};
		}
	}

	return len;
}

Noralf.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#11 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Quoted 7 lines · Noralf Trønnes
Quoted 2 lines

I forgot, but I have a two-tone (black/red) e-ink display here, and I also have a 3-bpp RGB TFT display.

I've been anticipating the need for more formats, but I didn't want to add them without having a user. Otherwise I could end up adding stuff that would never be used. If you can test, there's no problem adding support for more formats now.

That's great! I can't promise to to test the three-color (b/w/r) e-ink but I'll prepare a test setup for the RGB-TFT on the weekend.

Quoted 14 lines

Building on that I would define a 2 bpp RG format like this in the driver:

static const struct drm_format_info gud_drm_format_rg11 = {
	.format = GUD_DRM_FORMAT_RG11,
	.num_planes = 1,
	.char_per_block = { 1, 0, 0 },
	.block_w = { 4, 0, 0 }, /* 4 pixels per block/byte */
	.block_h = { 1, 0, 0 },
	.hsub = 1,
	.vsub = 1,
};

And a 3 bpp RGB format like this:

Quoted 9 lines
static const struct drm_format_info gud_drm_format_rgb111 = {
	.format = GUD_DRM_FORMAT_RGB111,
	.num_planes = 1,
	.char_per_block = { 1, 0, 0 },
	.block_w = { 2, 0, 0 }, /* 2 pixels per block/byte */
	.block_h = { 1, 0, 0 },
	.hsub = 1,
	.vsub = 1,
};

I can't really comment; I know next to nothing about the drm subsystem. :)

Quoted 8 lines

The MIPI DBI standard defines 2 ways to transmit 2x 3-bpp pixels in one byte (X=pad bit):
- Option 1: X X R1 G1 B1 R2 G2 B2
- Option 2: X R1 G1 B1 X R2 G2 B2

So maybe we should have GUD_DRM_FORMAT_RGB111_OPTION1 and GUD_DRM_FORMAT_RGB111_OPTION2?
Or just use option 2 and let the display fix it up if needed?

It would of course be lovely to be able to set up an automated DMA from a USB endpoint to the panel in the device and not have to touch the data, but that would require the DRM driver to support all the combinations, which quickly becomes complicated.

Quoted 1 line

What format does your 3 bpp display use?

It supports three formats:

- R1 G1 B1 R2 G2 B2
- R1 G1 B1 X R2 G2 B2 X (your option 2)
- R1 R2 R3 (simulated monochrome, same data bit to all three subpixels)

Quoted 3 lines

And then something like this for the conversion function:

static size_t gud_xrgb8888_to_color(u8 *dst, const struct

All right!

//Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#12 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Den 25.02.2021 22.37, skrev Peter Stuge:

Quoted 4 lines
Quoted 3 lines · Noralf Trønnes
Quoted 2 lines

I forgot, but I have a two-tone (black/red) e-ink display here, and I also have a 3-bpp RGB TFT display.

Quoted 8 lines
Quoted 4 lines

I've been anticipating the need for more formats, but I didn't want to add them without having a user. Otherwise I could end up adding stuff that would never be used. If you can test, there's no problem adding support for more formats now.

That's great! I can't promise to to test the three-color (b/w/r) e-ink but I'll prepare a test setup for the RGB-TFT on the weekend.

Ok, I'll add GUD_DRM_FORMAT_RGB111 in the next patch version.

Noralf.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Hi Noralf,

Quoted 1 line · Peter Stuge

I'll prepare a test setup for the RGB-TFT on the weekend.

So implementing a GUD and looking at the protocol from yet another angle gives more new insights - surprise. :)

Here are some thoughts so far:

* GUD_REQ_SET_VERSION does still rub me wrong; it seems potentially quite complex to maintain compatibility in two places; both for host and device. I don't want to insist on removing it, but at a minimum it's quite unusual.
Part of the idea in USB is that host software updates are easy if not fully automated but device firmware updates less so, thus complexity is rather placed in the host.

* It's unclear to me from reading the header files in this v6 patch set which GUD_REQ:s and which properties are actually mandatory in devices. I'm getting hints from your STM32 device and reading the driver code in detail, but what do you think is a good way to document what's required vs. optional?

* GUD_REQ_SET_BUFFER my old nemesis. :) It's great that it's optional! But do you see any way to turn it into a bulk message, in order to remove the memory barrier effect of a control transfer before bulk?

I think it would be possible to noticeably improve performance later, by changing the host driver to submit asynchronous bulk transfers for frame data rather than waiting for each transfer to finish; bulk transfers will then pack optimally on the wire - but with a control transfer in between there's no chance of achieving that.

Having only one kind of transfer in the hot path would also simplify canceling still pending transfers (when using async later) if new data gets flushed before the previous frame is completely transfered.

* A fair bit of the EDID isn't used or has dummy values. Have you already considered and dismissed some other ways of accomplishing the same?

* Sorry if I've asked before - but what's the purpose of GUD_REQ_SET_STATE_CHECK and GUD_REQ_SET_STATE_COMMIT? Why/when does drm do pipe check vs. update?

* How do you feel about passing the parameters for GUD_REQ_SET_CONTROLLER_ENABLE and GUD_REQ_SET_DISPLAY_ENABLE in wValue? It would save the transfer data stage.

Kind regards

//Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#15 lore raw

Re: [PATCH v6 3/3] drm: Add GUD USB Display driver

Den 01.03.2021 19.31, skrev Peter Stuge:

Quoted 8 lines

Hi Noralf,

Quoted 1 line · Peter Stuge

I'll prepare a test setup for the RGB-TFT on the weekend.

So implementing a GUD and looking at the protocol from yet another angle gives more new insights - surprise. :)

Yep, my Raspberry Pi Pico implementation discovered a bug in the compression code. Running X on a 240x135 display produced some 2x2 buffer rectangles which was so small that they failed to compress.

Quoted 10 lines

Here are some thoughts so far:

* GUD_REQ_SET_VERSION does still rub me wrong; it seems potentially quite complex to maintain compatibility in two places; both for host and device. I don't want to insist on removing it, but at a minimum it's quite unusual.
Part of the idea in USB is that host software updates are easy if not fully automated but device firmware updates less so, thus complexity is rather placed in the host.

Alright, I'll remove it :) This can also be accomplished in the device by having a switch that turns it into a version 1 device.

Quoted 6 lines

* It's unclear to me from reading the header files in this v6 patch set which GUD_REQ:s and which properties are actually mandatory in devices. I'm getting hints from your STM32 device and reading the driver code in detail, but what do you think is a good way to document what's required vs. optional?

The device must respond to all requests, but can return zero on some like the property requests. I intend to do a writeup on the wiki later with some more details around the protocol and how DRM works for this usecase.

My RPi Pico implementation will also serve as a guide. I've put the protocol handling in one file for possible reuse:
- gud.h: https://gist.github.com/notro/26bbf918fa59fb89caf155d51d57a40f
- gud.c: https://gist.github.com/notro/c1b32cea591f84d3d1c94f30812c1ba6

I will publish it when I have cleaned up the rest of the code.

Quoted 4 lines

* GUD_REQ_SET_BUFFER my old nemesis. :) It's great that it's optional! But do you see any way to turn it into a bulk message, in order to remove the memory barrier effect of a control transfer before bulk?

On a 1920x1080 RGB565 full update (4MB), the control request is negligible. I did measure it (USB 2.0), but I don't remember the numbers, a few milliseconds.

Quoted 10 lines

I think it would be possible to noticeably improve performance later, by changing the host driver to submit asynchronous bulk transfers for frame data rather than waiting for each transfer to finish; bulk transfers will then pack optimally on the wire - but with a control transfer in between there's no chance of achieving that.

Having only one kind of transfer in the hot path would also simplify canceling still pending transfers (when using async later) if new data gets flushed before the previous frame is completely transfered.

The device is the bottle neck (unless it's powerful like a Pi4):

Host: compresses buffer
Host: sends ctrl transfer
Host: sends bulk transfer
Device: Decompresses buffer, much slower than a desktop CPU
Host: compresses buffer
Host: sends ctrl transfer, waits for device to respond
Device: Has decompressed, and is ready
Host: sends bulk transfer

I would have preferred to do a test on a USB 3.0 device which would put more stress on the host, but I couldn't find an affordable one with mainline Linux support.

As you say this can be optimized later. By using double buffering it's possible to compress the next buffer while the previous is in transit.

lz4 compression has worked really well in my testing, always 2x or better.

I will reconsider the ctrl req if you provide me with numbers that show it's a performance problem.

Quoted 3 lines

* A fair bit of the EDID isn't used or has dummy values. Have you already considered and dismissed some other ways of accomplishing the same?

EDID is optional (return zero in v7), but useful if you want userspace to give a name to the monitor in the GUI for instance.

This comment will be present in version 7:

/*
* Display modes can be fetched as either EDID data or an array of
&gud_display_mode_req.
*
* If GUD_REQ_GET_CONNECTOR_MODES returns zero, EDID is used to create
display modes.
* If both display modes and EDID are returned, EDID is just passed on
to userspace
* in the EDID connector property.
*/

Quoted 4 lines

* Sorry if I've asked before - but what's the purpose of GUD_REQ_SET_STATE_CHECK and GUD_REQ_SET_STATE_COMMIT? Why/when does drm do pipe check vs. update?

DRM atomic mode setting has 2 stages:
- Check to see if the mode is supported, return -EINVAL if not
- Commit the mode, this is not allowed to fail.

Userspace can also do just the check phase when figuring out which combination of options that is supported.

Quoted 4 lines

* How do you feel about passing the parameters for GUD_REQ_SET_CONTROLLER_ENABLE and GUD_REQ_SET_DISPLAY_ENABLE in wValue? It would save the transfer data stage.

That would make the request different form the others, wValue is only used by the connector requests for the index. I prefer to use the data stage for data. If there was any performance gain I would have looked into it, but these requests are only used on init and when turning on/off the display.

Noralf.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel