Skip to contentSkip to conversation

GUD USB Display driver

PATCH Superseded Noralf Trønnes 3 participants 9 messages Feb 12, 2021 – Feb 16, 2021

Subsystems (4)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 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.

Minor changes this time, no protocol changes.

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 Generic 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 | 737 ++++++++++++++++++++++++++++
 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, 2379 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 v5 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 v5 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 v5 3/3 drm: Add Generic USB Display driver

In reply to Noralf Trønnes

This adds a generic 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

---
 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 | 737 ++++++++++++++++++++++++++++
 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, 2371 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..d2bb53a65de6
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+
+gud-objs			:= gud_drv.o gud_pipe.o gud_connector.o
+obj-$(CONFIG_DRM_GUD)		+= gud.o
drivers/gpu/drm/gud/gud_connector.c new file +737 -0

737 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 v5 3/3] drm: Add Generic USB Display driver

Hi Noralf,

I was happy to see v4 - thanks for accepting so much of my feedback - and I have to say that the new recursive acronym makes me smile! :)

drivers/gpu/drm/gud/Kconfig line 6

Quoted 8 lines · Noralf Trønnes

+++ b/drivers/gpu/drm/gud/Kconfig

drivers/gpu/drm/gud/Kconfig new file +6 -0
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config DRM_GUD
+	tristate "GUD USB Display"
+	depends on DRM && USB
+	select LZ4_COMPRESS

Just a thought: Maybe LZ4_COMPRESS should be optional also on the host?

Ie. not select it here and make lz4 code conditional on CONFIG_LZ4_COMPRESS?

drivers/gpu/drm/gud/Makefile line 3

Quoted 5 lines
drivers/gpu/drm/gud/Makefile new file +4 -0
+++ b/drivers/gpu/drm/gud/Makefile
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+
+gud-objs			:= gud_drv.o gud_pipe.o gud_connector.o

Should this be gud-y instead, like in other drm/*/Makefile ?

Quoted 1 line
(quoted diff) +1 -0
+++ b/drivers/gpu/drm/gud/gud_connector.c

..

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

Quoted 3 lines
drivers/gpu/drm/gud/gud_connector.c new file +3 -0
+static int gud_connector_atomic_check(struct drm_connector *connector,
+				      struct drm_atomic_state *state)
+{

This always returns 0, so could be void?

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

Quoted 11 lines
drivers/gpu/drm/gud/gud_connector.c new file +11 -0
+int gud_connector_create(struct gud_device *gdrm, unsigned int index)
+{
+	struct gud_connector_descriptor_req desc;
+	struct drm_device *drm = &gdrm->drm;
+	struct gud_connector *gconn;
+	struct drm_connector *connector;
+	struct drm_encoder *encoder;
+	int ret, connector_type;
+	u32 flags;
+
+	ret = gud_usb_get(gdrm, GUD_REQ_GET_CONNECTOR, index, &desc, sizeof(desc));

Watch out for endianness bugs.

I'd suggest to stay with the pattern "little-endian on wire" and add complexity on the host to deserialize/convert transfered data to native, but perhaps with some generic method that scales better than explicitly converting values on every use.

Quoted 1 line

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

..

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

Quoted 13 lines
drivers/gpu/drm/gud/gud_drv.c new file +13 -0
+static int gud_usb_control_msg(struct usb_device *usb, u8 ifnum, bool in,
+			       u8 request, u16 value, void *buf, size_t len)
+{
+	u8 requesttype = USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
+	unsigned int pipe;
+	int ret;
+
+	if (in) {
+		pipe = usb_rcvctrlpipe(usb, 0);
+		requesttype |= USB_DIR_IN;
+	} else {
+		pipe = usb_sndctrlpipe(usb, 0);
+		requesttype |= USB_DIR_OUT;

The above line seems unneccessary since USB_DIR_OUT is 0 by spec.

Quoted 5 lines
drivers/gpu/drm/gud/gud_drv.c new file +5 -0
+static int gud_get_display_descriptor(struct usb_interface *interface,
+				      struct gud_display_descriptor_req *desc)
+{
+	u8 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
+	struct usb_device *usb = interface_to_usbdev(interface);

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

Quoted 10 lines
drivers/gpu/drm/gud/gud_drv.c new file +10 -0
+	void *buf;
+	int ret;
+
+	buf = kmalloc(sizeof(*desc), GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = gud_usb_control_msg(usb, ifnum, true, GUD_REQ_GET_DESCRIPTOR, 0, buf, sizeof(*desc));
+	memcpy(desc, buf, sizeof(*desc));
+	kfree(buf);

Is buf neccessary here? This isn't the hot path, but less dynamic memory and copying is always nicer.

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

Quoted 2 lines
drivers/gpu/drm/gud/gud_drv.c new file +2 -0
+	if (desc->magic != GUD_DISPLAY_MAGIC)
+		return -ENODATA;

It seems like this checks overlooks endianness, which happens very easily.

Maybe it's a good idea to create a function to fix endianness directly after data transfers?

Such a function could take a pointer to memory and a kind of format string made up of 'b', 'w', 'l' and 'q' or '1', '2', '4' and '8' to describe field sizes, and would then convert wlq fields to native endianness in-place.

Or are there some parts of the code that could really benefit from keeping wire-endian values in host memory?

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

Quoted 12 lines
drivers/gpu/drm/gud/gud_drv.c new file +12 -0
+static int gud_usb_get_status(struct usb_device *usb, u8 ifnum, u8 *status)
+{
+	u8 *buf;
+	int ret;
+
+	buf = kmalloc(sizeof(*buf), GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = gud_usb_control_msg(usb, ifnum, true, GUD_REQ_GET_STATUS, 0, buf, sizeof(*buf));
+	*status = *buf;
+	kfree(buf);

Ouch, kmalloc for a single byte here, this is the extreme case! :)

If it's not cool to transfer data directly through to the provided pointer then how about bouncing onto a stack variable rather than kmalloc memory?

Ie:

u8 val;

gud_usb_control_msg(.. GUD_REQ_GET_STATUS, 0, &val, sizeof val)

Quoted 10 lines
drivers/gpu/drm/gud/gud_drv.c new file +10 -0
+static int gud_usb_transfer(struct gud_device *gdrm, bool in, u8 request, u16 index,
+			    void *buf, size_t len)
+{
+	struct usb_device *usb = gud_to_usb_device(gdrm);
+	void *trbuf = NULL;
+	int idx, ret;
+
+	drm_dbg(&gdrm->drm, "%s: request=0x%x index=%u len=%zu\n",
+		in ? "get" : "set", request, index, len);
+

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

Quoted 10 lines
drivers/gpu/drm/gud/gud_drv.c new file +10 -0
+	if (!drm_dev_enter(&gdrm->drm, &idx))
+		return -ENODEV;
+
+	mutex_lock(&gdrm->ctrl_lock);
+
+	if (buf) {
+		if (in)
+			trbuf = kmalloc(len, GFP_KERNEL);
+		else
+			trbuf = kmemdup(buf, len, GFP_KERNEL);

Also not the hot path, but is there no way to avoid copying memory around?

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

Quoted 11 lines
drivers/gpu/drm/gud/gud_drv.c new file +11 -0
+static int gud_set_version(struct usb_device *usb, u8 ifnum, u32 flags, u8 version)
+{
+	u8 *buf;
+	int ret;
+
+	buf = kmalloc(sizeof(*buf), GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	*buf = version;
+	ret = gud_usb_control_msg(usb, ifnum, false, GUD_REQ_SET_VERSION, 0, buf, sizeof(*buf));

USB devices are usually expected to only support one protocol as advertised by the device using some combination of bcdDevice in the device descriptor and bInterfaceProtocol in interface descriptor(s), while the host is expected to handle the complexities of different versions - so something like this where the host instructs the device about versions isn't typical for USB.

The motivation behind that split is that USB devices are typically simpler and lower cost than USB hosts, so devices should try to be trivial to implement.

You can of course keep it anyway if you really want, I'm just letting you know about the common pattern.

Quoted 1 line

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

..

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

Quoted 7 lines
drivers/gpu/drm/gud/gud_drv.c new file +7 -0
+retry:
+	/*
+	 * Use plain kmalloc here since devm_kmalloc() places struct devres at the beginning
+	 * of the buffer it allocates. This wastes a lot of memory when allocating big buffers.
+	 * Asking for 2M would actually allocate 4M. This would also prevent getting the biggest
+	 * possible buffer potentially leading to split transfers.
+	 */

Just a note here that split transfers are not per se a big problem if they are all submitted at once, since the USB host controller is able to schedule different transfers back-to-back on the wire.

Kind regards

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

#5 lore raw

Re: [PATCH v5 3/3] drm: Add Generic USB Display driver

Den 15.02.2021 12.58, skrev Peter Stuge:

Quoted 5 lines

Hi Noralf,

I was happy to see v4 - thanks for accepting so much of my feedback - and I have to say that the new recursive acronym makes me smile! :)

Yeah I also had to smile when Simon Ser suggested it.

Quoted 13 lines

drivers/gpu/drm/gud/Kconfig line 6

Quoted 8 lines · Noralf Trønnes

+++ b/drivers/gpu/drm/gud/Kconfig

drivers/gpu/drm/gud/Kconfig new file +6 -0
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config DRM_GUD
+	tristate "GUD USB Display"
+	depends on DRM && USB
+	select LZ4_COMPRESS

Just a thought: Maybe LZ4_COMPRESS should be optional also on the host?

Quoted 2 lines

Ie. not select it here and make lz4 code conditional on CONFIG_LZ4_COMPRESS?

LZ4_COMPRESS is a tiny library so I'd like it to stay to keep things simple.

Quoted 9 lines

drivers/gpu/drm/gud/Makefile line 3

Quoted 6 lines
drivers/gpu/drm/gud/Makefile new file +4 -0
+++ b/drivers/gpu/drm/gud/Makefile
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+
+gud-objs			:= gud_drv.o gud_pipe.o gud_connector.o

Should this be gud-y instead, like in other drm/*/Makefile ?

Yeah, everyone else does it seems.

Quoted 9 lines
Quoted 2 lines
(quoted diff) +1 -0
+++ b/drivers/gpu/drm/gud/gud_connector.c

..

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

Quoted 3 lines
drivers/gpu/drm/gud/gud_connector.c new file +3 -0
+static int gud_connector_atomic_check(struct drm_connector *connector,
+				      struct drm_atomic_state *state)
+{

This always returns 0, so could be void?

It needs to follow the struct drm_connector_helper_funcs.atomic_check signature.

Quoted 15 lines

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

Quoted 12 lines
drivers/gpu/drm/gud/gud_connector.c new file +11 -0
+int gud_connector_create(struct gud_device *gdrm, unsigned int index)
+{
+	struct gud_connector_descriptor_req desc;
+	struct drm_device *drm = &gdrm->drm;
+	struct gud_connector *gconn;
+	struct drm_connector *connector;
+	struct drm_encoder *encoder;
+	int ret, connector_type;
+	u32 flags;
+
+	ret = gud_usb_get(gdrm, GUD_REQ_GET_CONNECTOR, index, &desc, sizeof(desc));

Watch out for endianness bugs.

Quoted 5 lines

I'd suggest to stay with the pattern "little-endian on wire" and add complexity on the host to deserialize/convert transfered data to native, but perhaps with some generic method that scales better than explicitly converting values on every use.

I follow USB here and use little endian for all request structures. All req members are marked with __u8, __le16, __le32 or __le64.

For this particular request only the flags field needs treatment, like this:

	flags = le32_to_cpu(desc.flags);
Quoted 30 lines
Quoted 2 lines

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

..

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

Quoted 13 lines
drivers/gpu/drm/gud/gud_drv.c new file +13 -0
+static int gud_usb_control_msg(struct usb_device *usb, u8 ifnum, bool in,
+			       u8 request, u16 value, void *buf, size_t len)
+{
+	u8 requesttype = USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
+	unsigned int pipe;
+	int ret;
+
+	if (in) {
+		pipe = usb_rcvctrlpipe(usb, 0);
+		requesttype |= USB_DIR_IN;
+	} else {
+		pipe = usb_sndctrlpipe(usb, 0);
+		requesttype |= USB_DIR_OUT;

The above line seems unneccessary since USB_DIR_OUT is 0 by spec.

Quoted 10 lines
drivers/gpu/drm/gud/gud_drv.c new file +10 -0
+static int gud_get_display_descriptor(struct usb_interface *interface,
+				      struct gud_display_descriptor_req *desc)
+{
+	u8 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
+	struct usb_device *usb = interface_to_usbdev(interface);
+	void *buf;
+	int ret;
+
+	buf = kmalloc(sizeof(*desc), GFP_KERNEL);
+	if (!buf)
Quoted 9 lines

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

Quoted 5 lines
drivers/gpu/drm/gud/gud_drv.c new file +5 -0
+		return -ENOMEM;
+
+	ret = gud_usb_control_msg(usb, ifnum, true, GUD_REQ_GET_DESCRIPTOR, 0, buf, sizeof(*desc));
+	memcpy(desc, buf, sizeof(*desc));
+	kfree(buf);

Is buf neccessary here? This isn't the hot path, but less dynamic memory and copying is always nicer.

The USB stack wants DMA capable buffers so it can't be avoided.

Quoted 6 lines

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

Quoted 3 lines
drivers/gpu/drm/gud/gud_drv.c new file +2 -0
+	if (desc->magic != GUD_DISPLAY_MAGIC)
+		return -ENODATA;

It seems like this checks overlooks endianness, which happens very easily.

Thanks, I missed this one :)

Quoted 8 lines

Maybe it's a good idea to create a function to fix endianness directly after data transfers?

Such a function could take a pointer to memory and a kind of format string made up of 'b', 'w', 'l' and 'q' or '1', '2', '4' and '8' to describe field sizes, and would then convert wlq fields to native endianness in-place.

I would like to avoid more layers and just use the leNN_to_cpu() functions directly.

Quoted 3 lines

Or are there some parts of the code that could really benefit from keeping wire-endian values in host memory?

Everything on the wire should be little endian. Two reasons for this, USB does it like that, and I don't think we will ever see a big endian device. Or are there big endian microcontrollers maybe?

Quoted 16 lines

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

Quoted 13 lines
drivers/gpu/drm/gud/gud_drv.c new file +12 -0
+static int gud_usb_get_status(struct usb_device *usb, u8 ifnum, u8 *status)
+{
+	u8 *buf;
+	int ret;
+
+	buf = kmalloc(sizeof(*buf), GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	ret = gud_usb_control_msg(usb, ifnum, true, GUD_REQ_GET_STATUS, 0, buf, sizeof(*buf));
+	*status = *buf;
+	kfree(buf);

Ouch, kmalloc for a single byte here, this is the extreme case! :)

Quoted 9 lines

If it's not cool to transfer data directly through to the provided pointer then how about bouncing onto a stack variable rather than kmalloc memory?

Ie:

u8 val;

gud_usb_control_msg(.. GUD_REQ_GET_STATUS, 0, &val, sizeof val)

The USB stack needs the buffer to DMA accessible.
And a small buffer like this is likely to be available in the cache AFAIK.

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/gud/gud_drv.c new file +14 -0
+static int gud_usb_transfer(struct gud_device *gdrm, bool in, u8 request, u16 index,
+			    void *buf, size_t len)
+{
+	struct usb_device *usb = gud_to_usb_device(gdrm);
+	void *trbuf = NULL;
+	int idx, ret;
+
+	drm_dbg(&gdrm->drm, "%s: request=0x%x index=%u len=%zu\n",
+		in ? "get" : "set", request, index, len);
+
+	if (!drm_dev_enter(&gdrm->drm, &idx))
+		return -ENODEV;
+
+	mutex_lock(&gdrm->ctrl_lock);
Quoted 9 lines

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

Quoted 6 lines
drivers/gpu/drm/gud/gud_drv.c new file +6 -0
+
+	if (buf) {
+		if (in)
+			trbuf = kmalloc(len, GFP_KERNEL);
+		else
+			trbuf = kmemdup(buf, len, GFP_KERNEL);

Also not the hot path, but is there no way to avoid copying memory around?

I'm afraid not, but this is really fast though. Most buffers are smaller than 32 bytes. The exception is EDID which is 128 or 256 bytes.

Quoted 19 lines

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

Quoted 12 lines
drivers/gpu/drm/gud/gud_drv.c new file +11 -0
+static int gud_set_version(struct usb_device *usb, u8 ifnum, u32 flags, u8 version)
+{
+	u8 *buf;
+	int ret;
+
+	buf = kmalloc(sizeof(*buf), GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	*buf = version;
+	ret = gud_usb_control_msg(usb, ifnum, false, GUD_REQ_SET_VERSION, 0, buf, sizeof(*buf));

USB devices are usually expected to only support one protocol as advertised by the device using some combination of bcdDevice in the device descriptor and bInterfaceProtocol in interface descriptor(s), while the host is expected to handle the complexities of different versions - so something like this where the host instructs the device about versions isn't typical for USB.

Quoted 7 lines

The motivation behind that split is that USB devices are typically simpler and lower cost than USB hosts, so devices should try to be trivial to implement.

You can of course keep it anyway if you really want, I'm just letting you know about the common pattern.

The device can just ignore this if they're not interested to work with older drivers.
It's for devices that want to work with older drivers that don't support the new protocol.

Not sure if there ever will be a version 2 of the protocol, but who knows.

Quoted 15 lines
Quoted 2 lines

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

..

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

Quoted 7 lines
drivers/gpu/drm/gud/gud_drv.c new file +7 -0
+retry:
+	/*
+	 * Use plain kmalloc here since devm_kmalloc() places struct devres at the beginning
+	 * of the buffer it allocates. This wastes a lot of memory when allocating big buffers.
+	 * Asking for 2M would actually allocate 4M. This would also prevent getting the biggest
+	 * possible buffer potentially leading to split transfers.
+	 */

Just a note here that split transfers are not per se a big problem if they are all submitted at once, since the USB host controller is able to schedule different transfers back-to-back on the wire.

This isn't a USB split transfer as such, it's the driver splitting up the changed buffer rectangle because the device is unable to decompress the entire buffer in one go (the lz4 compression method used doesn't handle streams). This can kick in for devices that set gud_display_descriptor_req.max_buffer_size. See gud_flush_work() for details.

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

#6 lore raw

Re: [PATCH v5 3/3] drm: Add Generic USB Display driver

In reply to Noralf Trønnes

Am Freitag, den 12.02.2021, 18:46 +0100 schrieb Noralf Trønnes:

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

Quoted 7 lines
drivers/gpu/drm/gud/gud_connector.c new file +7 -0
+static void gud_connector_early_unregister(struct drm_connector *connector)
+{
+       struct gud_connector *gconn = to_gud_connector(connector);
+
+       backlight_device_unregister(gconn->backlight);
+       cancel_work_sync(&gconn->backlight_work);
+}

Hi,

this looks like you are creating a race condition where the queued work may operate on an already unregistered backlight.

Regards
Oliver

#7 lore raw

Re: [PATCH v5 3/3] drm: Add Generic USB Display driver

Den 16.02.2021 14.36, skrev Oliver Neukum:

Quoted 11 lines

Am Freitag, den 12.02.2021, 18:46 +0100 schrieb Noralf Trønnes:

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

Quoted 7 lines
drivers/gpu/drm/gud/gud_connector.c new file +7 -0
+static void gud_connector_early_unregister(struct drm_connector *connector)
+{
+       struct gud_connector *gconn = to_gud_connector(connector);
+
+       backlight_device_unregister(gconn->backlight);
+       cancel_work_sync(&gconn->backlight_work);
+}

Hi,

Quoted 3 lines

this looks like you are creating a race condition where the queued work may operate on an already unregistered backlight.

backlight_device_unregister() sets bd->ops = NULL. This means that userspace can't update brightness anymore, and thus won't call into this driver. After that it should be safe to flush/cancel the workqueue.

Unless I'm missing something here.

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

#8 lore raw

Re: [PATCH v5 3/3] drm: Add Generic USB Display driver

Den 16.02.2021 17.40, skrev Noralf Trønnes:

Quoted 18 lines

Den 16.02.2021 14.36, skrev Oliver Neukum:

Quoted 11 lines

Am Freitag, den 12.02.2021, 18:46 +0100 schrieb Noralf Trønnes:

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

Quoted 7 lines
drivers/gpu/drm/gud/gud_connector.c new file +7 -0
+static void gud_connector_early_unregister(struct drm_connector *connector)
+{
+       struct gud_connector *gconn = to_gud_connector(connector);
+
+       backlight_device_unregister(gconn->backlight);
+       cancel_work_sync(&gconn->backlight_work);
+}

Hi,

Quoted 3 lines

this looks like you are creating a race condition where the queued work may operate on an already unregistered backlight.

Quoted 6 lines

backlight_device_unregister() sets bd->ops = NULL. This means that userspace can't update brightness anymore, and thus won't call into this driver. After that it should be safe to flush/cancel the workqueue.

Unless I'm missing something here.

And it's ok for the worker to operate on an unregistered backlight, the memory is still available. It's freed when the connector goes away.

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