drm/tiny: trigger5: Add MCT Trigger 5 USB display driver
Subsystems (2)
PATCH v8 1/1 drm/tiny: trigger5: Add MCT Trigger 5 USB display driver
Add a DRM driver for MCT Trigger 5 USB display adapters. This supports USB 3 to HDMI/VGA displays adapters with trigger5 chipsets from j5create, StarTech and various others.
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.
---
Patchwork:
https://patchwork.freedesktop.org/series/176855/
Changes in v8:
- Use the clipped plane source rectangle for the damage offset and size. (should be fixed in v7 but accidentally dropped due to shuffling some git stuff around)
- Use drm_mode_config_create_initial_state() and fail the probe on error.
Link to v7:
https://lore.kernel.org/dri-devel/[email protected]/
Changes in v7:
- Add a header comment.
- Move the connector last in struct trigger5_device.
- Only check the hardware limits in mode_valid.
- Drop the conversion buffer reservation from the plane atomic_check, since other USB drivers don't do that either.
- note that drm_sysfb_plane_helper_atomic_check()/ssd130x/ssd132x does the same check.
Link to v6:
https://lore.kernel.org/dri-devel/[email protected]/
Changes in v6:
- Replace the USB transfer worker and vmalloc frame buffers with eight URBs that send the frame in chunks
- Remove keepalive work, it is not needed if it is never sent, and disarm any keepalive it already has
- Use container_of_const(), drm_dbg(), and drm_err_once()
- Group plane code and function tables before CRTC code and tables.
- Use the standard NULL-CRTC plane-check pattern and remove redundant mode checks, the driver-specific no-EDID fallback, and the INF comment.
Link to v5:
https://lore.kernel.org/dri-devel/[email protected]/
Changes in v5:
- Fix warnings found by kernel test robot
Changes in v4:
- Move the driver to drm/tiny
- Remove the buffer resizing to reduce size of file
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-4:
- 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/tiny/Kconfig | 14 + drivers/gpu/drm/tiny/Makefile | 1 + drivers/gpu/drm/tiny/trigger5.c | 984 ++++++++++++++++++++++++++++++++ 4 files changed, 1006 insertions(+) create mode 100644 drivers/gpu/drm/tiny/trigger5.c
MAINTAINERS +7 -0
@@ -8455,6 +8455,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/tiny/trigger5.c + DRM DRIVER FOR MGA G200 GRAPHICS CHIPS M: Dave Airlie <[email protected]> R: Thomas Zimmermann <[email protected]>
drivers/gpu/drm/tiny/Kconfig +14 -0
@@ -109,6 +109,20 @@ config DRM_PIXPAPER If M is selected, the module will be built as pixpaper.ko. +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. + config TINYDRM_HX8357D tristate "DRM support for HX8357D display panels" depends on DRM && SPI
drivers/gpu/drm/tiny/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-$(CONFIG_DRM_GLANDA) += glandagpu.o obj-$(CONFIG_DRM_GM12U320) += gm12u320.o obj-$(CONFIG_DRM_PANEL_MIPI_DBI) += panel-mipi-dbi.o obj-$(CONFIG_DRM_PIXPAPER) += pixpaper.o +obj-$(CONFIG_DRM_TRIGGER5) += trigger5.o obj-$(CONFIG_TINYDRM_HX8357D) += hx8357d.o obj-$(CONFIG_TINYDRM_ILI9163) += ili9163.o obj-$(CONFIG_TINYDRM_ILI9225) += ili9225.o
drivers/gpu/drm/tiny/trigger5.c new file +984 -0
984 lines not on this page · raw message
base-commit: 52b66f0dfe8d1a393b4ca342afdb89bc16f0892c
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] drm/tiny: trigger5: broken USB reset lifecycle in trigger5_reset() - [Medium] drm/tiny: trigger5…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] drm/tiny: trigger5: broken USB reset lifecycle in trigger5_reset()
- [Medium] drm/tiny: trigger5: redundant divisions in trigger5_calculate_pll()
- [Medium] drm/tiny: trigger5: bulk stream desync on mid-frame trigger5_submit_urb() error
- [Medium] drm/tiny: trigger5: merged damage clips in trigger5_plane_atomic_update()
--
commit be1c2efedf7e6e195ef345bbfde257d8e6d37b95
Author: Ho Jie Feng <[email protected]>
drm/tiny: trigger5: Add MCT Trigger 5 USB display driver
This commit adds a DRM driver for MCT Trigger 5 USB 3 to HDMI/VGA display adapters, implementing modesetting, EDID retrieval, connector detection, and bulk URB framebuffer updates.
Quoted 5 lines
drivers/gpu/drm/tiny/trigger5.c new file +0
-0
[ ... ]
Quoted 32 lines
drivers/gpu/drm/tiny/trigger5.c new file +32
-0+static u64 trigger5_calculate_pll (struct trigger5_pll *pll,int clock) +{ +u64 ref_clock =10000000 ; +u64 target_clock = (u64 )clock *1000 ; +u64 calculated_clock, calculated_err, best_err =U64_MAX ; +int prediv_div2, prediv, mul1, mul2, div1, div2; + +/* Use values found in the capture */ +for (prediv_div2 =0x02 ; prediv_div2 <=0x100 ; prediv_div2 <<=1 ) { +for (mul1 =1 ; mul1 <=0x32 ; mul1++) { +for (mul2 = mul1; mul2 <=0x32 ; mul2++) { +for (div1 =1 ; div1 <=0x32 ; div1++) { +if (!best_err) +break ; + calculated_clock = +div_u64 (ref_clock * mul1 * mul2, + prediv_div2 * div1); + calculated_err = +abs_diff (calculated_clock, + target_clock); +if (prediv_div2 <=0x10 ) { + div2 = prediv_div2; + prediv =1 ; + }else { + div2 =0x10 ; + prediv = prediv_div2 >>4 ; + } +if (calculated_err < best_err) { + best_err = + calculated_err; + pll->mul1 = mul1; + pll->mul2 = mul2;
drivers/gpu/drm/tiny/trigger5.c line 241
Quoted 10 lines
drivers/gpu/drm/tiny/trigger5.c new file +10
-0+ pll->div1 = div1; + pll->div2 = div2; + pll->prediv = prediv; + } + } + } + } + } +return best_err; +}
[Severity: Medium]
Should the !best_err check return 0 instead of breaking only out of the innermost div1 loop, and can the div1 loop stop once calculated_clock <= target_clock?
Because calculated_clock decreases monotonically as div1 grows, larger div1 values only increase calculated_err once calculated_clock falls to or below target_clock. Without breaking there (or computing the closest div1 directly), any mode clock that cannot be synthesized with zero error, such as 640x480@60Hz at 25175 kHz, runs all 510,000 div_u64() calls for every mode validated in trigger5_crtc_mode_valid().
Quoted 42 lines
drivers/gpu/drm/tiny/trigger5.c new file +42
-0+ +static void trigger5_reset (struct trigger5_device *trigger5,int err) +{ +if (!atomic_xchg (&trigger5->needs_reset,1 )) +usb_queue_reset_device (trigger5->intf); + +drm_err (&trigger5->drm,"USB transfer failed: %d, resetting adapter\n" , + err); +} + +static void trigger5_urb_complete (struct urb *urb) +{ +struct trigger5_transfer *transfer = urb->context; +struct trigger5_device *trigger5 = transfer->trigger5; + +switch (urb->status) { +case -ENOENT : +case -ECONNRESET : +case -ESHUTDOWN : +break ; +case 0 : +if (urb->actual_length == urb->transfer_buffer_length) +break ; + fallthrough; +default : +trigger5_reset (trigger5, urb->status); +break ; + } + +complete (&transfer->done); +} + +static struct trigger5_transfer * +trigger5_get_next_urb(struct trigger5_device *trigger5) +{ + struct trigger5_transfer *transfer = + &trigger5->transfers[trigger5->next_transfer]; + + if (atomic_read(&trigger5->needs_reset)) + return ERR_PTR(-EIO); + + if (!wait_for_completion_timeout(&transfer->done,
drivers/gpu/drm/tiny/trigger5.c line 293
Quoted 10 lines
drivers/gpu/drm/tiny/trigger5.c new file +10
-0+ msecs_to_jiffies(TRIGGER5_BULK_TIMEOUT_MS))) { +trigger5_reset (trigger5, -ETIMEDOUT ); +return ERR_PTR (-ETIMEDOUT ); + } + + trigger5->next_transfer = + (trigger5->next_transfer +1 ) %TRIGGER5_NUM_URBS ; + +return transfer; +}
[Severity: High]
Does trigger5_reset() cause usb_reset_device() to unbind the driver and unplug the DRM device, or leave trigger5->needs_reset permanently set?
Because trigger5_driver does not provide pre_reset and post_reset callbacks (reset_resume is only used when resuming a suspended USB device), usb_reset_device() performs a forced unbind via trigger5_usb_disconnect() -> drm_dev_unplug(), tearing down the active DRM device instead of resetting the adapter in place.
Also, nothing in the driver ever clears trigger5->needs_reset. If __usb_queue_reset_device() drops the reset because usb_lock_device_for_reset() fails (for example during suspend or lock contention with snd-usb-audio on the sibling audio interface), trigger5_get_next_urb() permanently fails all later updates with -EIO and no further reset can be queued.
In addition, trigger5_get_next_urb() only checks trigger5->needs_reset before wait_for_completion_timeout(), so if an in-flight URB fails and trigger5_urb_complete() signals transfer->done, trigger5_get_next_urb() still returns the transfer for submission. Finally, when case 0 in trigger5_urb_complete() falls through on a short transfer, urb->status is 0, so trigger5_reset() logs 0 as the failure code.
[ ... ]
Quoted 11 lines
drivers/gpu/drm/tiny/trigger5.c new file +11
-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); +struct drm_plane_state *state = +drm_atomic_get_new_plane_state (atomic_state, plane); +struct drm_shadow_plane_state *shadow_plane_state = +to_drm_shadow_plane_state (state); +struct trigger5_device *trigger5 =to_trigger5 (plane->dev); +struct trigger5_transfer *transfer;
drivers/gpu/drm/tiny/trigger5.c line 408
Quoted 10 lines
drivers/gpu/drm/tiny/trigger5.c new file +10
-0+struct trigger5_bulk_header *header; +struct drm_rect current_rect, src_rect, clip; +struct iosys_map data_map; +size_t payload_len; +int width, height, lines_per_urb; +int idx, y; + +/* So far, no way to send a batch of rects atomically is found */ +if (!drm_atomic_helper_damage_merged (old_state, state, ¤t_rect)) +return ;
[Severity: Medium]
As reported by Thomas Zimmermann on v6 [1], should trigger5_plane_atomic_update() iterate over individual damage rectangles with drm_atomic_helper_damage_iter_init() and drm_atomic_for_each_plane_damage() instead of calling drm_atomic_helper_damage_merged()?
Merging all damage clips into a single bounding box converts and transfers the entire unchanged region between disjoint updates over USB.
Quoted 50 lines
drivers/gpu/drm/tiny/trigger5.c new file +50
-0+ +if (!drm_dev_enter (plane->dev, &idx)) +return ; + +drm_rect_fp_to_int (&src_rect, &state->src); + +/* Clip merged damage to the new resolution. */ +if (!drm_rect_intersect (¤t_rect, &src_rect)) + goto exit; + + width =drm_rect_width (¤t_rect); + height =drm_rect_height (¤t_rect); + payload_len =array3_size (width, height,3 ); + lines_per_urb =TRIGGER5_URB_BUF_SIZE / (width *3 ); + +if (drm_gem_fb_begin_cpu_access (state->fb,DMA_FROM_DEVICE ) <0 ) + goto exit; + + transfer =trigger5_get_next_urb (trigger5); +if (IS_ERR (transfer)) + goto exit_end_cpu_access; + + header = transfer->urb->transfer_buffer; + header->magic =0xfb ; + header->length =0x14 ; +/* flags 0: uncompressed 24-bit RGB888. */ + header->counter = +cpu_to_le16 ((trigger5->frame_counter++) &0xfff ); + header->horizontal_offset =cpu_to_le16 (current_rect.x1 - src_rect.x1); + header->vertical_offset =cpu_to_le16 (current_rect.y1 - src_rect.y1); + header->width =cpu_to_le16 (width); + header->height =cpu_to_le16 (height); + header->payload_length =cpu_to_le32 ((u32 )payload_len); + header->flags =0x1 ; + header->unknown1 =0 ; + header->unknown2 =0 ; + header->checksum =trigger5_bulk_header_checksum (header); + +if (trigger5_submit_urb (transfer,sizeof (*header))) + goto exit_end_cpu_access; + + clip = current_rect; +for (y = current_rect.y1; y < current_rect.y2; y += lines_per_urb) { + clip.y1 = y; + clip.y2 =min (y + lines_per_urb, current_rect.y2); + + transfer =trigger5_get_next_urb (trigger5); +if (IS_ERR (transfer)) +break ; +
drivers/gpu/drm/tiny/trigger5.c line 468
Quoted 10 lines
drivers/gpu/drm/tiny/trigger5.c new file +10
-0+iosys_map_set_vaddr (&data_map, transfer->urb->transfer_buffer); +drm_fb_xrgb8888_to_rgb888 (&data_map,NULL , + &shadow_plane_state->data[0 ], + state->fb, &clip, + &shadow_plane_state->fmtcnv_state); + +if (trigger5_submit_urb (transfer, + width *3 *drm_rect_height (&clip))) +break ; + }
[Severity: Medium]
If trigger5_submit_urb() fails on one of the scanline payload chunks after the header URB has already been submitted, does breaking out of this loop without calling trigger5_reset() desynchronize the bulk stream?
The device firmware has already been told to expect header->payload_length bytes of pixel data, so subsequent frame headers and pixels will be interpreted as the remaining bytes of the aborted frame.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1