Skip to contentSkip to conversation

drm/trigger5: Add MCT Trigger 5 USB display driver

PATCH Superseded Ho Jie Feng 2 participants 2 messages Oct 2

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 drm/trigger5: Add MCT Trigger 5 USB display driver

Add a DRM driver for MCT Trigger 5 USB display adapters. The driver implements modesetting and USB framebuffer updates, connector detection, and EDID retrieval.

The protocol is based on reverse-engineering work by Forest Crossman (cyrozap) at: https://github.com/cyrozap/mct-usb-display-adapter-re
and usb packet captures between the Windows driver and the device.

Only the HDMI path has been tested. VGA selection is inferred from the absence of USB audio interfaces.

Tested on JUA355. The list of USB device IDs are taken from the Windows driver INF.

---
Changes in v3:
- Flush the vmalloc buffer before submitting the USB transfer.
- Support display panning by using the source offset

Changes in v2:
- Move the buffer resizing to crtc_* functions so that we can report out of memory errors to userspace
- Change the PLL search function so that it is faster and doesn't produce checkpatch warnings
- Update drm functions: crtc reset becomes create_state
- Fix warnings reported by kernel test robot

sashiko review false positives in v1/v2:
- atomic in these function names mean that the changes should go through through at once or not at all, nothing to do with atomic context. Furthermore other drivers also have similar patterns
- drm_gem_fb_begin_cpu_access is also required

 MAINTAINERS                                   |    7 +
 drivers/gpu/drm/Kconfig                       |    1 +
 drivers/gpu/drm/Makefile                      |    1 +
 drivers/gpu/drm/trigger5/Kconfig              |   14 +
 drivers/gpu/drm/trigger5/Makefile             |    4 +
 drivers/gpu/drm/trigger5/trigger5.h           |  141 +++
 drivers/gpu/drm/trigger5/trigger5_connector.c |  102 ++
 drivers/gpu/drm/trigger5/trigger5_drv.c       | 1013 +++++++++++++++++
 8 files changed, 1283 insertions(+)
 create mode 100644 drivers/gpu/drm/trigger5/Kconfig
 create mode 100644 drivers/gpu/drm/trigger5/Makefile
 create mode 100644 drivers/gpu/drm/trigger5/trigger5.h
 create mode 100644 drivers/gpu/drm/trigger5/trigger5_connector.c
 create mode 100644 drivers/gpu/drm/trigger5/trigger5_drv.c
MAINTAINERS +7 -0
index 4d108339f44ed..851e14eb134c1 100644
@@ -8250,6 +8250,13 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/display/panel/mantix,mlaf057we51-x.yaml
 F:	drivers/gpu/drm/panel/panel-mantix-mlaf057we51.c
 
+DRM DRIVER FOR MCT TRIGGER 5 USB DISPLAY ADAPTERS
+M:	Ho Jie Feng <[email protected]>
+L:	[email protected]
+S:	Maintained
+T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
+F:	drivers/gpu/drm/trigger5/
+
 DRM DRIVER FOR MGA G200 GRAPHICS CHIPS
 M:	Dave Airlie <[email protected]>
 R:	Thomas Zimmermann <[email protected]>
drivers/gpu/drm/Kconfig +1 -0
index 61111d50b1c65..8a332a797016b 100644
@@ -354,6 +354,7 @@ source "drivers/gpu/drm/tegra/Kconfig"
 source "drivers/gpu/drm/tidss/Kconfig"
 source "drivers/gpu/drm/tilcdc/Kconfig"
 source "drivers/gpu/drm/tiny/Kconfig"
+source "drivers/gpu/drm/trigger5/Kconfig"
 source "drivers/gpu/drm/tve200/Kconfig"
 source "drivers/gpu/drm/tyr/Kconfig"
 source "drivers/gpu/drm/udl/Kconfig"
drivers/gpu/drm/Makefile +1 -0
index 2c6bc093a923a..f876eed90bcc6 100644
@@ -238,6 +238,7 @@ obj-$(CONFIG_DRM_SPRD) += sprd/
 obj-$(CONFIG_DRM_LOONGSON) += loongson/
 obj-$(CONFIG_DRM_POWERVR) += imagination/
 obj-$(CONFIG_DRM_VERISILICON_DC) += verisilicon/
+obj-$(CONFIG_DRM_TRIGGER5) += trigger5/
 
 # Ensure drm headers are self-contained and pass kernel-doc
 hdrtest-files := \
drivers/gpu/drm/trigger5/Kconfig new file +14 -0
new file mode 100644
index 0000000000000..e489db641f3e1
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config DRM_TRIGGER5
+	tristate "MCT Trigger 5 USB display adapters"
+	depends on DRM && USB && MMU
+	select DRM_CLIENT_SELECTION
+	select DRM_GEM_SHMEM_HELPER
+	select DRM_KMS_HELPER
+	help
+	  DRM driver for USB display adapters based on the MCT Trigger 5
+	  chipset, including the StarTech USB32VGAES and USB32HDES and the
+	  j5create JUA310 and JUA350.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called trigger5.
drivers/gpu/drm/trigger5/Makefile new file +4 -0
new file mode 100644
index 0000000000000..03b146e2fe813
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+trigger5-y := trigger5_drv.o trigger5_connector.o
+obj-$(CONFIG_DRM_TRIGGER5) += trigger5.o
drivers/gpu/drm/trigger5/trigger5.h new file +141 -0

141 lines not on this page · raw message

drivers/gpu/drm/trigger5/trigger5_connector.c new file +102 -0

102 lines not on this page · raw message

drivers/gpu/drm/trigger5/trigger5_drv.c new file +1013 -0

1,013 lines not on this page · raw message

base-commit: 589e63c53a9a57cd3c14cff2ea42ad606c1e2a24

Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] drm/trigger5: sleeping functions called from atomic context in commit callbacks --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] drm/trigger5: sleeping functions called from atomic context in commit callbacks --

commit 4bfd327671381f9e3b72403f14f247b30db4659a
Author: Ho Jie Feng <[email protected]>
Subject: drm/trigger5: Add MCT Trigger 5 USB display driver

This commit introduces a DRM driver for MCT Trigger 5 USB display adapters. It implements modesetting, USB framebuffer updates, connector detection, and EDID retrieval, utilizing reverse-engineered USB protocol structures.

Quoted 3 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +0 -0

[ ... ]

Quoted 9 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +9 -0
+static void trigger5_atomic_commit_tail(struct drm_atomic_commit *state)
+{
+	struct trigger5_device *trigger5 = to_trigger5(state->dev);
+	struct drm_crtc_state *crtc_state;
+	struct trigger5_crtc_state *tstate;
+	int idx, i;
+
+	crtc_state = drm_atomic_get_new_crtc_state(state, &trigger5->crtc);
+	if (!crtc_state)

drivers/gpu/drm/trigger5/trigger5_drv.c line 256

Quoted 10 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +10 -0
+		goto commit;
+
+	tstate = to_trigger5_crtc_state(crtc_state);
+	if (!tstate->bufs[0].data)
+		goto commit;
+
+	if (!drm_dev_enter(state->dev, &idx))
+		goto commit;
+
+	trigger5_stop_io(trigger5);

[Severity: High]
Is it safe to call trigger5_stop_io() from trigger5_atomic_commit_tail()? trigger5_stop_io() invokes flush_workqueue() and cancel_delayed_work_sync(), both of which sleep. Calling sleeping functions from atomic commit paths can lead to system deadlocks and "sleeping function called from invalid context" kernel panics.

[ ... ]

Quoted 15 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +15 -0
+static void trigger5_crtc_atomic_enable(struct drm_crtc *crtc,
+					struct drm_atomic_commit *state)
+{
+	struct trigger5_device *trigger5 = to_trigger5(crtc->dev);
+	struct usb_device *udev = interface_to_usbdev(trigger5->intf);
+	struct drm_crtc_state *crtc_state =
+		drm_atomic_get_new_crtc_state(state, crtc);
+	struct drm_display_mode *mode = &crtc_state->mode;
+	struct trigger5_mode_request request = {};
+	u8 data[4];
+	u64 clk;
+	int idx, ret;
+
+	if (!drm_dev_enter(crtc->dev, &idx))
+		return;

drivers/gpu/drm/trigger5/trigger5_drv.c line 431

Quoted 10 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +10 -0
+
+	trigger5_stop_io(trigger5);
+
+	/* Sequence recovered from USB captures. */
+	ret = usb_control_msg_recv(udev, 0,
+				   TRIGGER5_REQUEST_FIRMWARE_RESET,
+				   USB_DIR_IN | USB_TYPE_VENDOR |
+					   USB_RECIP_DEVICE,
+				   0x0000, 0x0000, data, 1,
+				   USB_CTRL_GET_TIMEOUT, GFP_KERNEL);

[Severity: High]
Could this usb_control_msg_recv() call sleep in atomic context? The trigger5_crtc_atomic_enable() callback executes in atomic context where sleeping is forbidden. However, it performs synchronous USB I/O and uses a GFP_KERNEL allocation, both of which sleep and may cause instability.

[ ... ]

Quoted 5 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +5 -0
+	trigger5_calculate_pll(&request.pll, mode->clock);
+	clk = div_u64(10000000ULL * request.pll.mul1 * request.pll.mul2,
+		      (u32)request.pll.prediv * request.pll.div1 *
+			      request.pll.div2 * 1000);
+	drm_dbg_kms(&trigger5->drm,

drivers/gpu/drm/trigger5/trigger5_drv.c line 473

Quoted 10 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +10 -0
+		    "pll: %02x %02x %02x %02x %02x -> %llu kHz (want %d kHz)\n",
+		    request.pll.prediv, request.pll.mul1, request.pll.mul2,
+		    request.pll.div1, request.pll.div2, clk, mode->clock);
+
+	/* wValue can be any value since we are sending a custom mode */
+	ret = usb_control_msg_send(udev, 0, TRIGGER5_REQUEST_SET_MODE,
+				   USB_DIR_OUT | USB_TYPE_VENDOR |
+					   USB_RECIP_DEVICE,
+				   0, 0, &request, sizeof(request),
+				   USB_CTRL_SET_TIMEOUT, GFP_KERNEL);

[Severity: High]
Does this sequence trigger a sleep inside the CRTC atomic enable callback? usb_control_msg_send() is a sleeping function and also performs a GFP_KERNEL allocation, which can lead to deadlocks since this code path executes in atomic context.

[ ... ]

Quoted 9 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +9 -0
+static void trigger5_crtc_atomic_disable(struct drm_crtc *crtc,
+					 struct drm_atomic_commit *state)
+{
+	struct trigger5_device *trigger5 = to_trigger5(crtc->dev);
+	struct usb_device *udev = interface_to_usbdev(trigger5->intf);
+	u8 data;
+	int idx, ret;
+
+	if (!drm_dev_enter(crtc->dev, &idx))

drivers/gpu/drm/trigger5/trigger5_drv.c line 536

Quoted 10 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +10 -0
+		return;
+
+	trigger5_stop_io(trigger5);
+
+	ret = usb_control_msg_recv(udev, 0,
+				   TRIGGER5_REQUEST_FIRMWARE_RESET,
+				   USB_DIR_IN | USB_TYPE_VENDOR |
+					   USB_RECIP_DEVICE,
+				   0x0001, 0x0000, &data, 1,
+				   USB_CTRL_GET_TIMEOUT, GFP_KERNEL);

[Severity: High]
Can this control message execution cause a kernel panic? Like atomic_enable, trigger5_crtc_atomic_disable() runs in atomic context. Executing a synchronous USB control message with a GFP_KERNEL allocation here will sleep and may cause issues.

[ ... ]

Quoted 5 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +5 -0
+static void trigger5_plane_atomic_update(struct drm_plane *plane,
+					 struct drm_atomic_commit *atomic_state)
+{
+	struct drm_plane_state *old_state =
+		drm_atomic_get_old_plane_state(atomic_state, plane);

[ ... ]

drivers/gpu/drm/trigger5/trigger5_drv.c line 708

Quoted 12 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +12 -0
+	/* Buffers are sized for the full mode in crtc atomic_check. */
+	if (drm_WARN_ON_ONCE(plane->dev, frame_len > current_transfer->buf.len))
+		goto exit;
+
+	/*
+	 * This should almost never wait because we have should have a
+	 * pending transfer ready to be de-queued above in case the transfer
+	 * hasn't finished, but do a bounded wait just in case it gets stuck
+	 */
+	if (!wait_for_completion_timeout(&current_transfer->frame_complete,
+					 msecs_to_jiffies(20)))
+		goto exit_save_pending;

[Severity: High]
Could this bounded wait result in sleeping in atomic context? trigger5_plane_atomic_update() runs as a plane atomic update callback, which executes in atomic context. Calling wait_for_completion_timeout() directly inside this path violates the DRM subsystem guidelines against sleeping in atomic commit paths.

[ ... ]

drivers/gpu/drm/trigger5/trigger5_drv.c line 732

Quoted 6 lines
drivers/gpu/drm/trigger5/trigger5_drv.c new file +6 -0
+	header->checksum = trigger5_bulk_header_checksum(header);
+
+	iosys_map_set_vaddr(&data_map,
+			    current_transfer->buf.data + sizeof(*header));
+
+	ret = drm_gem_fb_begin_cpu_access(state->fb, DMA_FROM_DEVICE);

[Severity: High]
Is it possible for drm_gem_fb_begin_cpu_access() to block here? This function takes DMA reservation locks and may sleep waiting for fences. Because trigger5_plane_atomic_update() executes in atomic context, blocking here might lead to instability or system deadlocks.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1