drm: Add Fresco Logic FL2000 USB display driver
Subsystems (2)
cover drm: Add Fresco Logic FL2000 USB display driver
Hi,
This series adds a DRM driver for USB display adapters built around the Fresco Logic FL2000DX (USB ID 1d5c:2000). These are sold under many brands as cheap USB 3.0 to VGA/HDMI dongles; the HDMI variants pair the FL2000 with an ITE IT66121 HDMI transmitter on the FL2000's I2C bus. Today these adapters only work on Linux with out-of-tree drivers.
The driver started as Artem Mygaiev's out-of-tree fl2000_drm driver, itself partly based on Fresco Logic's reference driver; their copyright lines are kept. I ported it to current kernels, fixed the problems found on real hardware (EDID, bridge lifetime across USB resets, crashes on disconnect, frames that were never terminated, imported buffers read from the wrong VRAM), added the analog VGA output and cleaned it up for submission.
I intend to maintain this driver for the long term: patch 1 adds a MAINTAINERS entry with me as maintainer, I have the hardware to test on, and I will keep following review and bug reports on dri-devel.
The series builds the driver up in four steps, each of which compiles on its own:
1. USB driver, FL2000 register access (regmap) and I2C adapter
2. IT66121 HDMI transmitter bridge, including the EDID fallback
3. frame streaming over bulk URBs
4. the KMS pipeline, which makes the display usable, including the analog VGA output of adapters that have both connectors
It is posted as an RFC because there are design points I would like feedback on before doing the larger reworks:
1. IT66121 support. The series carries its own small IT66121 bridge (fl2000_it66121.c) instead of reusing drivers/gpu/drm/bridge/ ite-it66121.c, because I could not find a way to use the in-tree driver on a PC: it depends on OF (x86 distribution kernels have no CONFIG_OF), reads bus-width and its next bridge through of_graph, requires a reset GPIO the FL2000 boards do not have, and DRM has no bridge lookup without OF. It also reads the EDID through its DDC engine, which on these boards is not connected to the monitor (the EDID is read with the FL2000 I2C engine instead).
Reusing it would take converting ite-it66121 to the fwnode API with an optional reset GPIO, a fwnode-based bridge lookup in drm_bridge.c, and a small bridge of this driver providing the EDID. Is that the direction you would like, or is a private IT66121 bridge acceptable for a USB device like this one?
2. Hotplug. The bridge attaches with DRM_BRIDGE_ATTACH_NO_CONNECTOR and the HDMI connector comes from drm_bridge_connector. The bridge reports DETECT and EDID only: the IT66121 interrupt reaches the host through the FL2000 interrupt endpoint, which makes the driver send a hotplug event for the device, so there is no DRM_BRIDGE_OP_HPD and the connector is also polled. Would reporting HPD with drm_bridge_hpd_notify() from that path be preferred?
3. The FL2000 exposes three USB interfaces, and the state they share (regmap, I2C adapter, IT66121 client, component match) is kept as drvdata and devres of the struct usb_device rather than of an interface, so that it survives the unbind/rebind cycles of usb_reset_device(). Is there a better established pattern for a multi-interface device that needs this?
4. Pixel conversion. The stream converts with its own small line converters instead of drm_fb_xrgb8888_to_*(). The framebuffer is usually imported from the primary GPU and sits in write-combined VRAM; the format helpers read such I/O memory with memcpy_fromio(), which takes ~88 ms per 1080p frame, while drm_memcpy_from_wc() into a cached line buffer takes ~8 ms. The converters also apply the FL2000's 32-bit word swap and only touch the damaged rectangle of a shadow frame. Would a helper that reads its source through drm_memcpy_from_wc() be welcome, so this could go away?
5. VGA detection. The chip has a single monitor sense input, which on the adapter I have follows whichever monitor is plugged in, HDMI or VGA. The VGA connector therefore reports a monitor when the chip senses one and the IT66121 hot plug line is low, which means a monitor on VGA is not reported while one is on HDMI (clone mode needs video=VGA-1:e). Is this an acceptable way to do it, and is there a better signal I should look for in the chip?
The rest of the pipeline follows the current helpers: regular atomic plane/CRTC/encoder objects with managed initialisation, a cursor plane blended by the CPU, GEM SHMEM with shadow-plane access, the DRM vblank timer (the FL2000 has no vblank interrupt) and fbdev emulation through drm_client_setup().
Other known limitations: RGB565, used when the USB link cannot carry RGB888 (2560x1080 on USB 3.0, 640x480 on USB 2.0, as there is no compression), has not been tested on hardware; and adapters with a VGA connector only, without an IT66121, are not supported: the analog output is only added next to the HDMI one.
Tested on x86_64 (amdgpu laptop, GNOME Wayland) with a UGREEN USB 3.0 adapter with HDMI and VGA connectors: a 1920x1080 monitor on HDMI, and an old 1366x768 monitor without a usable EDID on VGA. EDID, mode setting, the desktop and the cursor work, at 59 frames per second on the link.
Every patch builds without warnings with W=1 on top of drm-misc-next, sparse is clean, and the series passes checkpatch --strict.
Tool use, as described in Documentation/process/generated-content.rst: I used an AI coding assistant (Claude Code with Anthropic Claude models) extensively while preparing this series. It helped port the driver to the current APIs, clean it up to kernel style, write the shadow-frame streaming and the cursor plane, track down and fix the hangs on unplug, and draft the commit messages and this letter. I reviewed the result and tested it on the hardware described above; the Signed-off-by lines are mine. The series was generated and checked (per-patch builds, checkpatch) by scripts in the out-of-tree repository this driver is developed in.
---
Angel Gabriel Crispin Valdivia (4):
drm/fl2000: Add USB driver with register and I2C access drm/fl2000: Add ITE IT66121 HDMI transmitter support drm/fl2000: Add frame streaming
drm/fl2000: Add KMS display pipeline
MAINTAINERS | 7 + drivers/gpu/drm/Kconfig | 1 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/fl2000/Kconfig | 19 + drivers/gpu/drm/fl2000/Makefile | 13 + drivers/gpu/drm/fl2000/fl2000.h | 192 +++++ drivers/gpu/drm/fl2000/fl2000_drm.c | 986 ++++++++++++++++++++++++ drivers/gpu/drm/fl2000/fl2000_drv.c | 281 +++++++ drivers/gpu/drm/fl2000/fl2000_i2c.c | 182 +++++ drivers/gpu/drm/fl2000/fl2000_interrupt.c | 237 ++++++ drivers/gpu/drm/fl2000/fl2000_it66121.c | 1030 ++++++++++++++++++++++++++ drivers/gpu/drm/fl2000/fl2000_it66121.h | 19 + drivers/gpu/drm/fl2000/fl2000_it66121_regs.h | 283 +++++++ drivers/gpu/drm/fl2000/fl2000_registers.c | 320 ++++++++ drivers/gpu/drm/fl2000/fl2000_registers.h | 272 +++++++ drivers/gpu/drm/fl2000/fl2000_streaming.c | 993 +++++++++++++++++++++++++ drivers/gpu/drm/fl2000/fl2000_vga.c | 256 +++++++ 17 files changed, 5092 insertions(+)
---
base-commit: 41ecf852c07391687beb129e12a231760b5d783f
change-id: 20261105-fl2000-upstream-3fc2e1f00d24
Best regards,
Signature · 1 line
Angel Gabriel Crispin Valdivia <[email protected]>
RFC PATCH 1/4 drm/fl2000: Add USB driver with register and I2C access
The Fresco Logic FL2000DX is a USB 3.0 display controller used in inexpensive USB to VGA and USB to HDMI adapters (USB ID 1d5c:2000). The FL2000 itself outputs parallel RGB/VGA; HDMI adapters pair it with an ITE IT66121 HDMI transmitter that sits on the FL2000's I2C bus.
This first patch adds the USB side of the driver: it binds the three FL2000 interfaces, describes the FL2000 registers, provides a regmap on top of the vendor control transfers that access them, and registers an I2C adapter backed by the FL2000 I2C engine. The engine only transfers aligned 32-bit words, so byte accesses are done as read-modify-write of the containing word and the adapter advertises the matching quirks.
State shared by the interfaces is attached to the usb_device so that it survives the unbind/rebind cycles of usb_reset_device().
The driver is based on the out-of-tree fl2000_drm driver by Artem Mygaiev, which carries code from the Fresco Logic reference driver.
--- MAINTAINERS | 7 + drivers/gpu/drm/Kconfig | 1 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/fl2000/Kconfig | 19 ++ drivers/gpu/drm/fl2000/Makefile | 8 + drivers/gpu/drm/fl2000/fl2000.h | 88 ++++++++ drivers/gpu/drm/fl2000/fl2000_drv.c | 150 ++++++++++++++ drivers/gpu/drm/fl2000/fl2000_i2c.c | 182 +++++++++++++++++ drivers/gpu/drm/fl2000/fl2000_registers.c | 320 ++++++++++++++++++++++++++++++ drivers/gpu/drm/fl2000/fl2000_registers.h | 272 +++++++++++++++++++++++++ 10 files changed, 1048 insertions(+)
MAINTAINERS +7 -0
@@ -8324,6 +8324,13 @@ F: include/linux/aperture.h F: include/linux/sysfb.h F: include/video/nomodeset.h +DRM DRIVER FOR FRESCO LOGIC FL2000 USB DISPLAY ADAPTERS +M: Angel Gabriel Crispin Valdivia <[email protected]> +L: [email protected] +S: Maintained +T: git https://gitlab.freedesktop.org/drm/misc/kernel.git +F: drivers/gpu/drm/fl2000/ + DRM DRIVER FOR GENERIC EDP PANELS R: Douglas Anderson <[email protected]> F: Documentation/devicetree/bindings/display/panel/panel-edp.yaml
drivers/gpu/drm/Kconfig +1 -0
@@ -313,6 +313,7 @@ source "drivers/gpu/drm/atmel-hlcdc/Kconfig" source "drivers/gpu/drm/bridge/Kconfig" source "drivers/gpu/drm/etnaviv/Kconfig" source "drivers/gpu/drm/exynos/Kconfig" +source "drivers/gpu/drm/fl2000/Kconfig" source "drivers/gpu/drm/fsl-dcu/Kconfig" source "drivers/gpu/drm/gma500/Kconfig" source "drivers/gpu/drm/gud/Kconfig"
drivers/gpu/drm/Makefile +1 -0
@@ -190,6 +190,7 @@ obj-$(CONFIG_DRM_NOUVEAU) +=nouveau/ obj-$(CONFIG_DRM_EXYNOS) +=exynos/ obj-$(CONFIG_DRM_ROCKCHIP) +=rockchip/ obj-$(CONFIG_DRM_GMA500) += gma500/ +obj-$(CONFIG_DRM_FL2000) += fl2000/ obj-$(CONFIG_DRM_UDL) += udl/ obj-$(CONFIG_DRM_AST) += ast/ obj-$(CONFIG_DRM_ARMADA) += armada/
drivers/gpu/drm/fl2000/Kconfig new file +19 -0
@@ -0,0 +1,19 @@ +# SPDX-License-Identifier: GPL-2.0-only +config DRM_FL2000 + tristate "Fresco Logic FL2000 USB display adapters" + depends on DRM && USB && MMU + depends on USB_ARCH_HAS_HCD + select DRM_BRIDGE_CONNECTOR + select DRM_CLIENT_SELECTION + select DRM_DISPLAY_HELPER + select DRM_GEM_SHMEM_HELPER + select DRM_KMS_HELPER + select I2C + select REGMAP + select REGMAP_I2C + help + This is a KMS driver for USB display adapters based on the + Fresco Logic FL2000DX USB 3.0 to VGA controller, paired with an + ITE IT66121 HDMI transmitter (USB ID 1d5c:2000). + + If M is selected the module will be called fl2000.
drivers/gpu/drm/fl2000/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-only + +fl2000-y := \ + fl2000_drv.o \ + fl2000_i2c.o \ + fl2000_registers.o + +obj-$(CONFIG_DRM_FL2000) += fl2000.o
drivers/gpu/drm/fl2000/fl2000.h new file +88 -0
88 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_drv.c new file +150 -0
150 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_i2c.c new file +182 -0
182 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_registers.c new file +320 -0
320 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_registers.h new file +272 -0
272 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 1/4] drm/fl2000: Add USB driver with register and I2C access
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] drm/fl2000: UAF and leaks attaching devres to usb_dev in fl2000_get_devices() - [High] drm/fl2000…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] drm/fl2000: UAF and leaks attaching devres to usb_dev in fl2000_get_devices()
- [High] drm/fl2000: unvalidated message length and flags in fl2000_i2c_xfer()
- [Medium] drm/fl2000: destructive second read of status reg in fl2000_read_status()
- [Medium] drm/fl2000: unlocked FL2000_VGA_I2C_SC_REG update in fl2000_usb_magic() --
commit 65acb73d91d8bb3b60de43aba3fc31aba51bb815
Author: Angel Gabriel Crispin Valdivia <[email protected]>
drm/fl2000: Add USB driver with register and I2C access
This patch adds the USB driver for the Fresco Logic FL2000DX display controller, binding its three USB interfaces, setting up a regmap over vendor control transfers, and registering an I2C adapter for the chip's I2C engine.
Quoted 6 lines
drivers/gpu/drm/fl2000/fl2000_drv.c new file +0
-0@@ -0,0 +1,150 @@
[ ... ]
Quoted 31 lines
drivers/gpu/drm/fl2000/fl2000_drv.c new file +31
-0+/* Devices that are independent of interfaces, created for the lifetime of the USB device */ +struct fl2000_devs { +struct regmap *regmap; +struct i2c_adapter *adapter; +int active_if; +}; + +static struct fl2000_devs *fl2000_get_devices (struct usb_device *usb_dev) +{ +struct fl2000_devs *devs; + + devs =devm_kzalloc (&usb_dev->dev,sizeof (*devs),GFP_KERNEL ); +if (!devs) +return ERR_PTR (-ENOMEM ); + + devs->regmap =fl2000_regmap_init (usb_dev); +if (IS_ERR (devs->regmap)) { +dev_err (&usb_dev->dev,"Cannot initialize regmap (%pe)\n" , devs->regmap); +return ERR_CAST (devs->regmap); + } + + devs->adapter =fl2000_i2c_init (usb_dev); +if (IS_ERR (devs->adapter)) { +dev_err (&usb_dev->dev,"Cannot initialize I2C adapter (%pe)\n" , devs->adapter); +return ERR_CAST (devs->adapter); + } + +dev_set_drvdata (&usb_dev->dev, devs); + +return devs; +}
[ ... ]
Quoted 5 lines
drivers/gpu/drm/fl2000/fl2000_drv.c new file +5
-0+static void fl2000_disconnect (struct usb_interface *interface) +{ +u8 iface_num = interface->cur_altsetting->desc.bInterfaceNumber; +struct usb_device *usb_dev =interface_to_usbdev (interface); +struct fl2000_devs *devs =dev_get_drvdata (&usb_dev->dev);
drivers/gpu/drm/fl2000/fl2000_drv.c line 104
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drv.c new file +10
-0+ +if (!devs) +return ; + +switch (iface_num) { +case FL2000_USBIF_AVCONTROL : +case FL2000_USBIF_STREAMING : +case FL2000_USBIF_INTERRUPT : + devs->active_if &= ~BIT (iface_num); +break ;
[Severity: High]
What cleans up devs, devs->regmap, devs->adapter, and usb_dev->dev driver_data when the driver is unbound or unloaded while the USB device remains plugged in?
fl2000_driver binds to struct usb_interface devices (&interface->dev), while the parent struct usb_device (&usb_dev->dev) stays bound to usb_generic_driver for the entire physical connection. However, fl2000_get_devices(), fl2000_regmap_init(), and fl2000_i2c_init() attach all devm allocations and registrations to &usb_dev->dev:
fl2000_probe()
fl2000_get_devices()
devm_kzalloc(&usb_dev->dev, ...)
fl2000_regmap_init()
devm_regmap_init(&usb_dev->dev, ...)
fl2000_i2c_init()
devm_kzalloc(&usb_dev->dev, ...)
devm_i2c_add_adapter(&usb_dev->dev, adapter)
dev_set_drvdata(&usb_dev->dev, devs)When fl2000_driver is unbound from the interfaces (for example via rmmod fl2000 or sysfs unbind), device_unbind_cleanup() only releases devres on &interface->dev, not on &usb_dev->dev, and fl2000_disconnect() only clears BIT(iface_num) in devs->active_if.
After module unload, the I2C adapter and regmap remain registered with callbacks and structures (such as fl2000_i2c_algorithm, fl2000_i2c_quirks, fl2000_regmap_config, fl2000_reg_read, and fl2000_reg_write) pointing into freed module memory, and dev_get_drvdata(&usb_dev->dev) retains the stale pointer. Won't any subsequent I2C transfer, regmap debugfs access, module reload, or USB unplug trigger a use-after-free?
Also, if fl2000_i2c_init() fails in fl2000_get_devices() (or if fl2000_probe() fails on an unexpected interface), really_probe() only cleans up &interface->dev, leaking the devm allocations attached to &usb_dev->dev on each interface probe attempt. Should the shared state be refcounted across the bound interfaces and explicitly torn down when the last interface disconnects (or on probe failure)?
Quoted 6 lines
drivers/gpu/drm/fl2000/fl2000_i2c.c new file +0
-0@@ -0,0 +1,182 @@
[ ... ]
Quoted 39 lines
drivers/gpu/drm/fl2000/fl2000_i2c.c new file +39
-0+static int fl2000_i2c_xfer (struct i2c_adapter *adapter,struct i2c_msg *msgs,int num) +{ +int ret; +bool read; +u16 addr; +u8 idx; +u8 offset; +u32 data; + + addr = msgs[0 ].addr; + offset = msgs[0 ].buf[0 ] &I2C_XFER_ADDR_MASK ; + idx = msgs[0 ].buf[0 ] - offset; + +/* +* We expect the following: +* - 2 messages, each 1 byte, first write then read +* - 1 message, 2 bytes, write +*/ +if (num ==2 ) { + read = true; + }else if (num ==1 ) { +if (msgs[0 ].len ==2 && !(msgs[0 ].flags &I2C_M_RD )) + read = false; +else +return -EOPNOTSUPP ; + }else { +return -EOPNOTSUPP ; + } + +/* +* The FL2000 I2C engine only transfers aligned dwords, so the offset is aligned down to a +* 4-byte boundary and the requested byte is picked out of (or merged into) the dword. +*/ +if (read) { + ret =fl2000_i2c_read_dword (adapter->algo_data, addr, offset, &data); +if (ret) +return ret; + + msgs[1 ].buf[0 ] = data >> (idx *8 );
[ ... ]
Quoted 9 lines
drivers/gpu/drm/fl2000/fl2000_i2c.c new file +9
-0+static u32 fl2000_i2c_func (struct i2c_adapter *adap) +{ +return I2C_FUNC_I2C |I2C_FUNC_NOSTART ; +} + +static const struct i2c_algorithm fl2000_i2c_algorithm = { + .xfer = fl2000_i2c_xfer, + .functionality = fl2000_i2c_func, +};
drivers/gpu/drm/fl2000/fl2000_i2c.c line 153
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_i2c.c new file +10
-0+ +static const struct i2c_adapter_quirks fl2000_i2c_quirks = { +/* Combined transfers only: an address write, then data, on the same address */ + .flags =I2C_AQ_COMB |I2C_AQ_COMB_WRITE_FIRST |I2C_AQ_COMB_SAME_ADDR , + .max_num_msgs =I2C_CMESSAGES_NUM , + .max_write_len =2 *I2C_REG_DATA_SIZE , + .max_read_len =I2C_REG_DATA_SIZE , + .max_comb_1st_msg_len =I2C_REG_ADDR_SIZE , + .max_comb_2nd_msg_len =I2C_REG_DATA_SIZE , +};
[Severity: High]
Can fl2000_i2c_xfer() dereference a NULL or ZERO_SIZE_PTR buffer on zero-length messages, or misclassify a 2-message write as a read?
At the start of fl2000_i2c_xfer(), msgs[0].buf[0] is read before checking msgs[0].len. Because fl2000_i2c_quirks.flags does not set I2C_AQ_NO_ZERO_LEN, i2c_check_for_quirks() allows messages with len == 0 (for example, SMBus quick transfers from i2cdetect where msgs[0].buf is NULL, or zero-length I2C_RDWR ioctls where msgs[i].buf is ZERO_SIZE_PTR), which will fault on msgs[0].buf[0] or msgs[1].buf[0].
In addition, when num == 2, fl2000_i2c_xfer() sets read = true without checking that msgs[1].flags & I2C_M_RD is set or that both messages have len == 1. Meanwhile, fl2000_i2c_func() advertises I2C_FUNC_NOSTART and fl2000_i2c_quirks.flags omits I2C_AQ_COMB_READ_SECOND (i.e. I2C_AQ_COMB_WRITE_THEN_READ). If a caller issues a 2-message write (such as regmap_i2c_gather_write() using I2C_M_NOSTART, or via i2c-dev), won't fl2000_i2c_xfer() perform a hardware read instead of a write and overwrite the caller's write buffer msgs[1].buf[0]?
Quoted 6 lines
drivers/gpu/drm/fl2000/fl2000_registers.c new file +0
-0@@ -0,0 +1,320 @@
[ ... ]
drivers/gpu/drm/fl2000/fl2000_registers.c line 195
Quoted 7 lines
drivers/gpu/drm/fl2000/fl2000_registers.c new file +7
-0+int fl2000_usb_magic (struct usb_device *usb_dev) +{ +struct regmap *regmap =dev_get_regmap (&usb_dev->dev,NULL ); +u32 val; + + val =FL2000_VGA_I2C_SC_MONITOR_DETECT |FL2000_VGA_I2C_SC_EDID_DETECT ; +regmap_write_bits (regmap,FL2000_VGA_I2C_SC_REG , val, val);
[Severity: Medium]
Can updating FL2000_VGA_I2C_SC_REG in fl2000_usb_magic() race with an active I2C transfer or trigger an unintended I2C transaction?
In fl2000_i2c_dword(), writing FL2000_VGA_I2C_SC_REG with bit 31 (FL2000_VGA_I2C_SC_DONE) cleared to 0 initiates an I2C transfer using the address, command, and offset in bits 15:0, and callers serialize the multi-step sequence (including polling across 200us sleeps in regmap_read_poll_timeout()) with i2c_lock_bus().
Here in fl2000_usb_magic(), regmap_write_bits() performs a read-modify-write on the volatile FL2000_VGA_I2C_SC_REG without holding i2c_lock_bus() and without setting FL2000_VGA_I2C_SC_DONE in the value written back. If an I2C transfer is in progress when fl2000_usb_magic() runs, or if FL2000_VGA_I2C_SC_DONE reads back as 0 after reset, won't regmap_write_bits() write FL2000_VGA_I2C_SC_DONE = 0 back to FL2000_VGA_I2C_SC_REG and corrupt or re-trigger an I2C transaction?
[ ... ]
Quoted 9 lines
drivers/gpu/drm/fl2000/fl2000_registers.c new file +9
-0+/* +* Read the status register. Its event bits clear on read, so whoever reads it owns the events it +* returns: callers must not drop them. +*/ +int fl2000_read_status (struct usb_device *usb_dev,u32 *status) +{ +struct regmap *regmap =dev_get_regmap (&usb_dev->dev,NULL ); +unsigned int val; +u32 mask;
drivers/gpu/drm/fl2000/fl2000_registers.c line 241
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_registers.c new file +10
-0+int ret; + + ret =regmap_read (regmap,FL2000_VGA_STATUS_REG , &val); +if (ret) +return ret; + +/* LBUF issues are recoverable */ + mask = val & (FL2000_VGA_STATUS_LBUF_OVERFLOW |FL2000_VGA_STATUS_LBUF_UNDERFLOW ); +if (mask) +regmap_write_bits (regmap,FL2000_VGA_STATUS_REG , mask, val);
[Severity: Medium]
Does calling regmap_write_bits() on FL2000_VGA_STATUS_REG in fl2000_read_status() perform a second hardware read that silently drops pending event bits?
FL2000_VGA_STATUS_REG has seven read-self-clearing event bits (such as FL2000_VGA_STATUS_HDMI_EVENT, FL2000_VGA_STATUS_MONITOR_EVENT, and FL2000_VGA_STATUS_EDID_EVENT) and is marked volatile in FL2000_REG_VOLATILE().
When FL2000_VGA_STATUS_LBUF_OVERFLOW or FL2000_VGA_STATUS_LBUF_UNDERFLOW is set in val, fl2000_read_status() calls regmap_write_bits() on FL2000_VGA_STATUS_REG. Because the register is volatile (and precious_reg only prevents debugfs reads, not _regmap_update_bits()), regmap_write_bits() issues a second _regmap_read() of FL2000_VGA_STATUS_REG before writing it back. Any self-clearing event bits that assert between the initial regmap_read() and the internal read in regmap_write_bits() will be cleared by that second read and discarded instead of being returned in *status. Should this use regmap_write() with val directly instead of regmap_write_bits()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH 2/4 drm/fl2000: Add ITE IT66121 HDMI transmitter support
In reply to Angel Gabriel Crispin ValdiviaProbe the IT66121 on the FL2000 I2C bus and expose it as a DRM bridge. It attaches with DRM_BRIDGE_ATTACH_NO_CONNECTOR and reports the hot plug line and the EDID (DRM_BRIDGE_OP_DETECT and DRM_BRIDGE_OP_EDID), so the connector is the display driver's. The transmitter (input bus, AFE, AVI infoframe, HDMI/DVI mode) is programmed in atomic_enable, where the connector that tells HDMI from DVI is known. The EDID is kept while the monitor stays, since reading it over USB takes about a second.
EDID is first read through the IT66121 DDC engine. On the tested adapter, as on the Fresco Logic reference design, the monitor's DDC lines are wired to the FL2000 I2C bus instead and that engine never completes, so the driver falls back to reading the EDID at address 0x50 directly with the FL2000 I2C engine.
The bridge is registered as a component of the FL2000 I2C adapter and lives as long as the USB device: re-creating it on every bind would leave stale pointers in the component match data across USB resets.
--- drivers/gpu/drm/fl2000/Makefile | 1 + drivers/gpu/drm/fl2000/fl2000_drv.c | 60 ++ drivers/gpu/drm/fl2000/fl2000_it66121.c | 1030 ++++++++++++++++++++++++++ drivers/gpu/drm/fl2000/fl2000_it66121.h | 19 + drivers/gpu/drm/fl2000/fl2000_it66121_regs.h | 283 +++++++ 5 files changed, 1393 insertions(+)
drivers/gpu/drm/fl2000/Makefile +1 -0
@@ -3,6 +3,7 @@ fl2000-y := \ fl2000_drv.o \ fl2000_i2c.o \ + fl2000_it66121.o \ fl2000_registers.o obj-$(CONFIG_DRM_FL2000) += fl2000.o
drivers/gpu/drm/fl2000/fl2000_drv.c +60 -0
90 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +1030 -0
1,030 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_it66121.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* (C) Copyright 2019, Artem Mygaiev +*/ + +#ifndef FL2000_IT66121_H +#define FL2000_IT66121_H + +struct drm_bridge ; +struct i2c_adapter ; +struct i2c_client ; + +struct i2c_client *it66121_create (struct i2c_adapter *adapter); +void it66121_destroy (struct i2c_client *client); +int it66121_hpd_connected (struct drm_bridge *bridge); +void it66121_suspend (struct drm_bridge *bridge); +void it66121_resume (struct drm_bridge *bridge); + +#endif /* FL2000_IT66121_H */
drivers/gpu/drm/fl2000/fl2000_it66121_regs.h new file +283 -0
283 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 2/4] drm/fl2000: Add ITE IT66121 HDMI transmitter support
Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider: - [High] drm/fl2000: broken I2C probing and return values in it66121_i2c_probe() - [High] drm/fl2000: UAF…
Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider:
- [High] drm/fl2000: broken I2C probing and return values in it66121_i2c_probe()
- [High] drm/fl2000: UAF and resource leak from usb_dev devres in fl2000_probe()
- [High] drm/fl2000: unsafe teardown ordering and UAF race in it66121_destroy()
- [Medium] drm/fl2000: duplicate IT66121 bridge driver in it66121_create()
- [Medium] drm/fl2000: EDID extension block corruption in it66121_do_get_edid_block()
- [Medium] drm/fl2000: broken IRQ clear, mask, and error handling in it66121_intr_work()
- [Medium] drm/fl2000: stale regmap and EDID cache in it66121_bridge_attach() on rebind
- [Medium] drm/fl2000: unlocked IT66121_SW_RST RMW race in it66121_abort_ddc_ops()
- [Low] drm/fl2000: out-of-mask value in it66121_configure_afe() register write
- [Low] drm/fl2000: uninitialized status logged in it66121_wait_ddc_ready() --
commit 1d8fe736e9a996195ce28bd6f054bf21a532ce7c
Author: Angel Gabriel Crispin Valdivia <[email protected]>
drm/fl2000: Add ITE IT66121 HDMI transmitter support
This commit adds support for the ITE IT66121 HDMI transmitter on the FL2000 I2C bus and exposes it as a DRM bridge component with hotplug detection and EDID reading (via both the IT66121 DDC FIFO and a direct FL2000 I2C fallback).
Quoted 4 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +0
-0
[ ... ]
Quoted 41 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +37
-0@@ -52,6 +54,44 @@ static struct fl2000_devs *fl2000_get_devices(struct usb_device *usb_dev)return devs; } +static void fl2000_bridge_release (struct device *dev,void *res) +{ +struct i2c_client **client = res; + +dev_dbg (dev,"Destroying IT66121 bridge\n" ); +it66121_destroy (*client); +} + +/* +* Create the IT66121 bridge and tie its destruction to the usb_device +* lifetime: it must survive component master del/add cycles (USB resets) and +* die only when the USB device is actually gone. The devres is released +* before the I2C adapter devres (LIFO), so the client is destroyed while its +* adapter still exists. +*/ +static int fl2000_bridge_create (struct usb_device *usb_dev,struct fl2000_devs *devs) +{ +struct i2c_client *client; +struct i2c_client **holder; + + holder =devres_alloc (fl2000_bridge_release,sizeof (*holder),GFP_KERNEL ); +if (!holder) +return -ENOMEM ; + + client =it66121_create (devs->adapter); +if (IS_ERR (client)) { +dev_err (&usb_dev->dev,"Cannot create HDMI bridge (%pe)\n" , client); +devres_free (holder); +return -ENODEV ; + } + + *holder = client; +devres_add (&usb_dev->dev, holder); + devs->bridge_client = client; + +return 0 ; +}
[ ... ]
drivers/gpu/drm/fl2000/fl2000_drv.c static void fl2000_disconnect(struct usb_interface *interface) · line 158
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +6
-0@@ -96,6 +150,12 @@ static void fl2000_disconnect(struct usb_interface *interface)if (!devs)return ; +/* +* The IT66121 bridge is intentionally NOT destroyed here: disconnect +* also runs for the transient unbind/rebind cycles of +* usb_reset_device(), where the usb_device survives. Its destruction is +* a devres of the usb_device, see fl2000_bridge_create(). +*/
[Severity: High]
Can attaching fl2000_bridge_release to &usb_dev->dev in fl2000_bridge_create() cause a use-after-free on driver unbind or module unload, or leak resources when probing an interface fails?
fl2000_driver is a struct usb_driver that binds to struct usb_interface (&interface->dev), while the parent struct usb_device (&usb_dev->dev) is bound to usb_generic_driver in usbcore.
When fl2000_driver is unbound via sysfs or unloaded via rmmod fl2000 while the USB device stays plugged in (or if fl2000_probe() fails on an interface), usb_unbind_interface() only releases devres on &interface->dev. Because &usb_dev->dev remains bound to usb_generic_driver, devres_release_all(&usb_dev->dev) does not run until physical USB unplug, and fl2000_disconnect() intentionally skips tearing down devs->bridge_client and devs (allocated on &usb_dev->dev in fl2000_get_devices()).
If the fl2000 module is unloaded while the USB dongle is connected:
- priv->bridge remains in the global DRM bridge_list with bridge.funcs pointing to unmapped it66121_bridge_funcs
- priv->client->dev remains in the global component_list with ops pointing to unmapped it66121_component_ops
- priv->work_queue stays active running it66121_intr_work() in unmapped module text
- &usb_dev->dev retains devres release callbacks (such as fl2000_bridge_release) pointing into freed module memory when the USB device is later unplugged.
Should the lifecycle of devs->bridge_client and devs instead be reference- counted across the bound USB interfaces and torn down when active_if drops to 0 outside of a reset (for example using pre_reset and post_reset callbacks)?
Quoted 6 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +0
-0@@ -0,0 +1,1030 @@
[ ... ]
Quoted 9 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +9
-0+static int it66121_configure_afe (struct it66121_priv *priv,int clock_khz) +{ +int ret; + + ret =regmap_write (priv->regmap,IT66121_AFE_DRV_CONTROL ,IT66121_AFE_RST ); +if (ret) +return ret; + +/* Values from the vendor reference driver, the bits are undocumented */
drivers/gpu/drm/fl2000/fl2000_it66121.c line 167
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +10
-0+if (clock_khz >80000 ) { + ret =regmap_write_bits (priv->regmap,IT66121_AFE_XP_CONTROL ,0x90 ,0x80 ); +if (ret) +return ret; + ret =regmap_write_bits (priv->regmap,IT66121_AFE_IP_CONTROL_1 ,0x89 ,0x80 ); +if (ret) +return ret; + ret =regmap_write_bits (priv->regmap,IT66121_AFE_IP_CONTROL_3 ,0x10 ,0x80 ); +if (ret) +return ret;
[Severity: Low]
In it66121_configure_afe(), when clock_khz > 80000, regmap_write_bits() is called on IT66121_AFE_IP_CONTROL_3 with mask 0x10 and value 0x80.
Since regmap_update_bits_base() computes (orig & ~mask) | (val & mask), and (0x80 & 0x10) is 0, bit 7 (0x80) is masked out and only bit 4 (0x10) is cleared. Was the value meant to be 0x00 to clear bit 4 (matching drivers/gpu/drm/bridge/ite-it66121.c), or was bit 7 meant to be included in the mask?
[ ... ]
Quoted 11 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +11
-0+static int it66121_wait_ddc_ready (struct it66121_priv *priv) +{ +int ret; +unsigned int val; + +/* +* Wait for completion only. On this hardware the FL2000 I2C master and +* the IT66121 DDC master share the physical bus, so 'wait bus' and +* 'arbitration lose' flags are live/latched transients of the engine +* retrying on a contended bus, not terminal errors - the only reliable +* terminal signal is TX_DONE (or the timeout).
drivers/gpu/drm/fl2000/fl2000_it66121.c line 213
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +10
-0+*/ + ret =regmap_read_poll_timeout (priv->regmap,IT66121_DDC_STATUS , val, + val &DDC_STATUS_TX_DONE ,EDID_SLEEP ,EDID_TIMEOUT ); +if (ret) { +dev_err (&priv->client->dev,"DDC not done (%d), status 0x%02X\n" , ret, val); +return ret; + } + +return 0 ; +}
[Severity: Low]
In it66121_wait_ddc_ready(), if the first regmap_read() inside regmap_read_poll_timeout() fails with a negative error code (for example due to a USB or I2C transfer error), regmap_read_poll_timeout() returns immediately without writing to val.
Won't dev_err() then read and print the uninitialized stack variable val?
[ ... ]
Quoted 18 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +18
-0+static int it66121_abort_ddc_ops (struct it66121_priv *priv) +{ +int ret; +unsigned int swreset, cpdesire; + +/* +* The HDCP engine shares the DDC master and holds the bus after chip +* reset (DDC status reports 'wait bus' + 'arbitration lose', 0x1A). +* Follow the ITE reference sequence, as the ite-it66121 driver does: +* clear HDCP CP_DESIRE, reset the HDCP engine and re-select the host as +* DDC master before issuing the abort command. +*/ + ret =regmap_read (priv->regmap,IT66121_SW_RST , &swreset); +if (ret) +return ret; + + ret =regmap_read (priv->regmap,IT66121_HDCP_CONTROL , &cpdesire); +if (ret)
drivers/gpu/drm/fl2000/fl2000_it66121.c line 253
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +10
-0+return ret; + + ret =regmap_write (priv->regmap,IT66121_HDCP_CONTROL , + cpdesire & ~IT66121_HDCP_CP_DESIRE ); +if (ret) +return ret; + + ret =regmap_write (priv->regmap,IT66121_SW_RST , swreset |IT66121_SW_HDCP_RST ); +if (ret) +return ret;
[Severity: Medium]
Can it66121_abort_ddc_ops() race with it66121_set_mode() when modifying IT66121_SW_RST?
In it66121_get_edid_block() -> it66121_do_get_edid_block() -> it66121_abort_ddc_ops(), IT66121_SW_RST is read into swreset, two USB I2C transfers are performed on IT66121_HDCP_CONTROL, and then swreset | IT66121_SW_HDCP_RST is written back under priv->ddc_lock.
Meanwhile, it66121_bridge_atomic_enable() -> it66121_set_mode() runs in the atomic commit tail without holding priv->ddc_lock, setting and clearing IT66121_SW_REF_RST_HDMITX | IT66121_SW_HDMI_VID_RST in IT66121_SW_RST:
regmap_write_bits(priv->regmap, IT66121_SW_RST,
IT66121_SW_REF_RST_HDMITX | IT66121_SW_HDMI_VID_RST,
IT66121_SW_REF_RST_HDMITX | IT66121_SW_HDMI_VID_RST);
...
regmap_write_bits(priv->regmap, IT66121_SW_RST,
IT66121_SW_REF_RST_HDMITX | IT66121_SW_HDMI_VID_RST, 0);If it66121_abort_ddc_ops() reads swreset while the HDMI reset bits are set and writes swreset | IT66121_SW_HDCP_RST back after it66121_set_mode() clears them, won't the transmitter remain stuck in reset? Using regmap_write_bits() for IT66121_SW_HDCP_RST (and IT66121_HDCP_CP_DESIRE) would keep each update atomic under the regmap lock.
[ ... ]
Quoted 17 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +17
-0+static void it66121_intr_work (struct work_struct *work_item) +{ +int ret; +unsigned int val; +struct delayed_work *dwork =to_delayed_work (work_item); + struct it66121_priv *priv = container_of(dwork, struct it66121_priv, work); +/* Do not use bridge.dev here: it is NULL while the bridge is detached */ +struct device *dev = &priv->client->dev; + + ret =regmap_field_read (priv->irq_pending, &val); +if (ret) { +dev_err (dev,"Cannot read interrupt status (%d)\n" , ret); +return ; + } + +if (val) { + ret =regmap_read (priv->regmap,IT66121_INT_STATUS_1 , &val);
drivers/gpu/drm/fl2000/fl2000_it66121.c line 326
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +10
-0+if (ret) { +dev_err (dev,"Cannot read IT66121_INT_STATUS_1 (%d)\n" , ret); +return ; + } + +regmap_field_write (priv->clr_irq,1 ); + } + +queue_delayed_work (priv->work_queue, &priv->work,msecs_to_jiffies (IRQ_POLL_INTRVL )); +}
[Severity: Medium]
There seem to be three issues with the interrupt polling in it66121_intr_work() and it66121_start_interrupts():
1. regmap_field_write(priv->clr_irq, 1) calls regmap_field_update_bits_base() with force = false. If bit 0 of IT66121_SYS_STATUS (0x0E) already reads as 1, regmap skips the bus write and the interrupt is not cleared. Should this use regmap_field_force_write(priv->clr_irq, 1)?
2. it66121_start_interrupts() only unmasks IT66121_MASK_DDC in IT66121_INT_MASK_1, leaving HPD (IT66121_INT_STATUS_1_HPD) masked, and it66121_intr_work() reads IT66121_INT_STATUS_1 without writing to IT66121_INT_CLEAR_1 to clear latched interrupt sources.
3. If either regmap_field_read() or regmap_read() fails once due to a transient USB or I2C transfer error, it66121_intr_work() returns before queue_delayed_work(), permanently stopping the polling work until a system suspend/resume cycle.
Quoted 36 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +36
-0+/* +* EDID read through the IT66121 DDC master (EDID FIFO command), for boards +* where the monitor's DDC lines are wired to the IT66121. +*/ +static int it66121_do_get_edid_block (struct it66121_priv *priv,u8 *buf,unsigned int block, +size_t len) +{ +int ret; +size_t remain = len; +unsigned int val; +unsigned int segment = block >>1 ; +unsigned int offset = (block &1 ) ?128 :0 ; +static const u8 header[EDID_LOSS_LEN ] = {0x00 ,0xFF ,0xFF }; + +/* Abort any ongoing DDC operation */ + ret =it66121_abort_ddc_ops (priv); +if (ret) +return ret; + +/* +* Statically fill first 3 bytes (due to EDID reading HW bug they are +* lost, but they are constant in any valid EDID header). +*/ +while (offset <EDID_LOSS_LEN && remain >0 ) { + *(buf++) = header[offset]; + remain--; + offset++; + } + +while (remain >0 ) { +/* Add bytes that will be lost during EDID read */ +size_t size = remain +EDID_LOSS_LEN ; + +/* ... and check size fits FIFO */ + size =min_t (size_t, size,EDID_FIFO_SIZE ); +
drivers/gpu/drm/fl2000/fl2000_it66121.c line 373
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +10
-0+ ret =it66121_clear_ddc_fifo (priv); +if (ret) +return ret; + + ret =regmap_write (priv->regmap,IT66121_DDC_ADDRESS ,EDID_DDC_ADDR ); +if (ret) +return ret; + +/* Account 3 bytes that will be lost */ + ret =regmap_write (priv->regmap,IT66121_DDC_OFFSET , offset -EDID_LOSS_LEN );
[Severity: Medium]
Does it66121_do_get_edid_block() corrupt even-numbered EDID extension blocks and misalign FIFO reads?
First, for any even-numbered extension block (such as block 2, where segment = 1 and offset = 0), the loop:
while (offset < EDID_LOSS_LEN && remain > 0) {
*(buf++) = header[offset];
remain--;
offset++;
}checks only offset < EDID_LOSS_LEN without checking block == 0, overwriting the first 3 bytes of the extension block (the extension tag, revision, and DTD offset) with the base EDID block 0 header bytes { 0x00, 0xFF, 0xFF }.
Second, IT66121_DDC_STATUS (0x16) and IT66121_DDC_RD_FIFO (0x17) lie in the same 4-byte aligned dword (0x14..0x17). Because fl2000_i2c_xfer() executes every 1-byte register read as a 4-byte aligned dword read via fl2000_i2c_read_dword(), each poll of IT66121_DDC_STATUS in it66121_wait_ddc_ready() also reads IT66121_DDC_RD_FIFO (0x17) and pops a byte from the DDC FIFO whenever data is available. With EDID_SLEEP set to 5000 us, the number of polls in it66121_wait_ddc_ready() is not guaranteed to equal the hardcoded EDID_LOSS_LEN (3), shifting the remaining bytes read from IT66121_DDC_RD_FIFO.
[ ... ]
Quoted 24 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +24
-0+static int it66121_bridge_attach (struct drm_bridge *bridge,struct drm_encoder *encoder, +enum drm_bridge_attach_flags flags) +{ + struct it66121_priv *priv = container_of(bridge, struct it66121_priv, bridge); + +/* The connector is the display driver's (drm_bridge_connector_init()) */ +if (!(flags &DRM_BRIDGE_ATTACH_NO_CONNECTOR )) { +drm_err (bridge->dev,"The IT66121 bridge does not create a connector\n" ); +return -EINVAL ; + } + +if (!bridge->encoder) { +drm_err (bridge->dev,"Parent encoder object not found\n" ); +return -ENODEV ; + } + +it66121_init_chip (priv); + +it66121_start_interrupts (priv); + +drm_dbg_kms (bridge->dev,"Bridge attached\n" ); + +return 0 ; +}
[ ... ]
drivers/gpu/drm/fl2000/fl2000_it66121.c line 678
Quoted 11 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +11
-0+void it66121_resume (struct drm_bridge *bridge) +{ + struct it66121_priv *priv = container_of(bridge, struct it66121_priv, bridge); + +regcache_drop_region (priv->regmap,0 , it66121_regmap_config.max_register); +it66121_init_chip (priv); +it66121_start_interrupts (priv); + +/* Another monitor may have been plugged in meanwhile */ +WRITE_ONCE (priv->edid_stale, true); +}
[Severity: Medium]
Since the IT66121 bridge persists across usb_reset_device() and USB interface unbind/rebind cycles, should it66121_bridge_attach() also drop the regmap cache and mark priv->edid_stale = true like it66121_resume() does?
When the bridge is re-attached on rebind (and after it66121_init_chip() issues IT66121_SW_REF_RST_HDMITX), priv->regmap still holds cached register values in REGCACHE_MAPLE from before the reset (including the bank selector register IT66121_SYS_CONTROL and non-volatile Bank 0/1 registers), which can cause regmap_write_bits() and bank selection to skip necessary hardware writes. In addition, priv->edid_stale is not set to true on re-attach.
[ ... ]
Quoted 33 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +33
-0+static int it66121_i2c_probe (struct i2c_adapter *adapter,unsigned short address) +{ +int ret; +u8 id_regs[] = {IT66121_VENDOR_ID_1 ,IT66121_VENDOR_ID_2 ,IT66121_DEVICE_ID_1 , +IT66121_DEVICE_ID_2 }; +u8 id[ARRAY_SIZE (id_regs)]; +u16 vendor, device; + +dev_dbg (&adapter->dev,"Detecting IT66121 at address 0x%X on %s\n" , address, + adapter->name); + +/* We rely on full I2C protocol + 1 byte SMBUS read for detection */ + ret =i2c_check_functionality (adapter,I2C_FUNC_I2C |I2C_FUNC_SMBUS_READ_BYTE ); +if (!ret) { +dev_dbg (&adapter->dev,"Adapter does not support I2C functions properly\n" ); +return -ENODEV ; + } + +for (int i =0 ; i <ARRAY_SIZE (id_regs); i++) { +struct i2c_msg msgs[] = { + { .addr = address, .flags =0 , .len =1 , .buf = &id_regs[i] }, + { .addr = address, .flags =I2C_M_RD , .len =1 , .buf = &id[i] } + }; + + ret =i2c_transfer (adapter, msgs,ARRAY_SIZE (msgs)); +if (ret <0 ) { +dev_err (&adapter->dev,"I2C transfer failed (%d)\n" , ret); +return -ENODEV ; + } + } + + vendor =get_unaligned_le16 (&id[0 ]); + device =get_unaligned_le16 (&id[2 ]);
drivers/gpu/drm/fl2000/fl2000_it66121.c line 916
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +10
-0+if (vendor !=VENDOR_ID || (device & ~REVISION_MASK ) !=DEVICE_ID ) { +dev_dbg (&adapter->dev,"IT66121 not found (0x%X-0x%X)\n" , vendor, device); +return -ENODEV ; + } + +dev_dbg (&adapter->dev,"IT66121 found, revision %d\n" , + (device &REVISION_MASK ) >>REVISION_SHIFT ); + +return 0 ; +}
[Severity: High]
Aren't the return values and functionality check in it66121_i2c_probe() inverted for i2c_new_scanned_device()?
In drivers/i2c/i2c-core-base.c, i2c_new_scanned_device() checks:
if (probe(adap, addr_list[i]))
break;so the custom probe callback must return 1 (non-zero) when a device is found and 0 when it is not found. Instead, it66121_i2c_probe() returns -ENODEV (non-zero) on error or ID mismatch and 0 on a valid match.
Furthermore, it66121_i2c_probe() checks for I2C_FUNC_SMBUS_READ_BYTE even though the loop below uses raw i2c_transfer() messages. In drivers/gpu/drm/fl2000/fl2000_i2c.c, fl2000_i2c_func() only advertises:
return I2C_FUNC_I2C | I2C_FUNC_NOSTART;
Because I2C_FUNC_SMBUS_READ_BYTE is not set, i2c_check_functionality() always returns 0, causing it66121_i2c_probe() to immediately return -ENODEV without performing any I2C transfers on the bus. i2c_new_scanned_device() treats that non-zero -ENODEV return value as a positive match at the first address (0x98 >> 1 = 0x4c), unconditionally instantiating the IT66121 client even on FL2000 boards without an IT66121 and never scanning the second address (0x9A >> 1 = 0x4d).
drivers/gpu/drm/fl2000/fl2000_it66121.c line 930
Quoted 13 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +13
-0+static struct i2c_client *it66121_i2c_init (struct i2c_adapter *adapter) +{ +/* +* Not "it66121": that name would make the ite-it66121 driver, if built, +* try to bind this client and release our devres when its probe fails. +*/ +struct i2c_board_info board_info = {I2C_BOARD_INFO ("fl2000-it66121" ,0 ) }; + +/* According to datasheet IT66121 addresses are 0x98 or 0x9A including cmd */ +const unsigned short it66121_addr[] = { (0x98 >>1 ), (0x9A >>1 ),I2C_CLIENT_END }; + +return i2c_new_scanned_device (adapter, &board_info, it66121_addr, it66121_i2c_probe); +}
[Severity: Medium]
This isn't a runtime bug in itself, but the kernel already has an in-tree DRM bridge driver for the ITE IT66121 in drivers/gpu/drm/bridge/ite-it66121.c. Could drivers/gpu/drm/bridge/ite-it66121.c be extended to support non-OF instantiation and a custom DDC fallback rather than duplicating the IT66121 driver in drivers/gpu/drm/fl2000/fl2000_it66121.c?
Quoted 31 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +31
-0+void it66121_destroy (struct i2c_client *client) +{ +struct drm_bridge *bridge; +struct it66121_priv *priv; + +if (!client) +return ; + + bridge =dev_get_drvdata (&client->dev); + priv = container_of(bridge, struct it66121_priv, bridge); + +cancel_delayed_work_sync (&priv->work); + +destroy_workqueue (priv->work_queue); + +component_del (&priv->client->dev, &it66121_component_ops); + +drm_edid_free (priv->drm_edid); + priv->drm_edid =NULL ; + +drm_bridge_remove (&priv->bridge); + +/* +* Drops the devm reference to the bridge; priv is freed together with +* the bridge once DRM releases the remaining references. +*/ +i2c_unregister_device (priv->client); +} + +struct i2c_client *it66121_create (struct i2c_adapter *adapter) +{
[ ... ]
Quoted 18 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +18
-0+ priv->bridge.type =DRM_MODE_CONNECTOR_HDMIA ; + priv->bridge.ops =DRM_BRIDGE_OP_DETECT |DRM_BRIDGE_OP_EDID ; + +drm_bridge_add (&priv->bridge); + +/* Setup work queue for interrupt processing work */ + priv->work_queue =alloc_ordered_workqueue ("it66121_work" ,0 ); +if (!priv->work_queue) { +drm_bridge_remove (&priv->bridge); +i2c_unregister_device (client); +return ERR_PTR (-ENOMEM ); + } + +/* Store bridge pointer at I2C client device for further binding */ +dev_set_drvdata (&priv->client->dev, &priv->bridge); + +/* Bind component of I2C client to master I2C adapter */ + ret =component_add (&priv->client->dev, &it66121_component_ops);
drivers/gpu/drm/fl2000/fl2000_it66121.c line 1030
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_it66121.c new file +10
-0+if (ret) { +dev_err (&client->dev,"Cannot register IT66121 component (%d)\n" , ret); +destroy_workqueue (priv->work_queue); +drm_bridge_remove (&priv->bridge); +i2c_unregister_device (client); +return ERR_PTR (ret); + } + +return client; +}
[Severity: High]
Can the teardown ordering in it66121_destroy() and the error path in it66121_create() trigger a use-after-free?
1. In it66121_destroy(), cancel_delayed_work_sync(&priv->work) and destroy_workqueue(priv->work_queue) are called before component_del(&priv->client->dev, &it66121_component_ops) and drm_bridge_remove(&priv->bridge). Until component_del() finishes, a concurrent attach or it66121_resume() call can still invoke it66121_start_interrupts() and queue work onto the destroyed priv->work_queue.
2. In it66121_create(), drm_bridge_add(&priv->bridge) is called before priv->work_queue is allocated, and if component_add() fails after it66121_bind() -> drm_bridge_attach() -> it66121_start_interrupts() has already queued priv->work during master bring-up, the error path calls destroy_workqueue(priv->work_queue) without first calling cancel_delayed_work_sync(&priv->work).
3. If userspace holds an open DRM fd across USB unplug, the DRM device and encoder can outlive it66121_destroy() until the final drm_dev_put(). However, it66121_destroy() frees priv->drm_edid without locking against it66121_bridge_edid_read(), and i2c_unregister_device(priv->client) releases all devm resources on &client->dev (priv->regmap, priv->hpd, priv->irq_pending, priv->clr_irq) and frees priv->client without calling drm_bridge_unplug() or guarding bridge callbacks with drm_bridge_enter().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RFC PATCH 3/4 drm/fl2000: Add frame streaming
In reply to Angel Gabriel Crispin ValdiviaFrames are sent as one bulk transfer per frame on endpoint 1 OUT, after switching the AV control interface to altsetting 1. Three URBs are kept in flight from a pool of six scatter-gather buffers, and when no new frame is ready the current one is sent again, so the FL2000 always has data to scan out.
The current frame is kept in a shadow buffer in the device's pixel format. Only the damaged rectangle of the framebuffer is converted, into the shadow, and each outgoing buffer is brought up to date from it just before it is submitted, by replaying the damage rectangles it missed or with a full copy when it is too far behind. The send path never waits for a conversion in progress: it resends the newest complete frame instead. A cursor sprite is blended into each outgoing buffer rather than into the shadow, so moving the pointer does not touch the framebuffer.
The framebuffer is often a buffer imported from the primary GPU and lives in its VRAM, mapped write-combined. It is read a scanline at a time with drm_memcpy_from_wc(), which uses streaming loads: a 1080p frame then converts in about 8 ms instead of the ~88 ms of memcpy_fromio(). The FL2000 wants the two 32-bit halves of every 64-bit word swapped, which the converters do as they write. Copies run with the buffer taken off the lists, so list_lock, which is also taken from URB completion, is never held across multi-megabyte work.
The stream is wired up to KMS in the next patch.
--- drivers/gpu/drm/fl2000/Makefile | 3 +- drivers/gpu/drm/fl2000/fl2000.h | 67 ++ drivers/gpu/drm/fl2000/fl2000_streaming.c | 993 ++++++++++++++++++++++++++++++ 3 files changed, 1062 insertions(+), 1 deletion(-)
drivers/gpu/drm/fl2000/Makefile +2 -1
@@ -4,6 +4,7 @@ fl2000-y := \ fl2000_drv.o \ fl2000_i2c.o \ fl2000_it66121.o \ - fl2000_registers.o + fl2000_registers.o \ + fl2000_streaming.o obj-$(CONFIG_DRM_FL2000) += fl2000.o
drivers/gpu/drm/fl2000/fl2000.h +67 -0
79 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +993 -0
993 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 3/4] drm/fl2000: Add frame streaming
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [Critical] drm/fl2000: out-of-bounds shadow write after fl2000_stream_mode_set() error - [High]…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [Critical] drm/fl2000: out-of-bounds shadow write after fl2000_stream_mode_set() error
- [High] drm/fl2000: NULL pointer dereference of sb->pages in fl2000_free_sb()
- [High] drm/fl2000: infinite retry loop on -EAGAIN in fl2000_submit_urb()
- [High] drm/fl2000: invalid URB transfer_buffer usage in fl2000_stream_work()
- [Medium] drm/fl2000: missing flush_kernel_vmap_range() before SG DMA in fl2000_stream_work() --
commit 3826e48192d62fc9fe0e2b4c5ca7c2e42d218844
Author: Angel Gabriel Crispin Valdivia <[email protected]>
drm/fl2000: Add frame streaming
This commit adds frame streaming support for the FL2000 USB display adapter, managing a pool of scatter-gather URB buffers, a shadow framebuffer for damage updates, pixel format conversion, and cursor blending.
Quoted 21 lines
drivers/gpu/drm/fl2000/fl2000.h +13
-0@@ -42,6 +42,48 @@ enum fl2000_interface { FL2000_USBIF_INTERRUPT = 2, }; +/* Process context only: may sleep between retries */ +static inline int fl2000_submit_urb (struct urb *urb) +{ +int ret; +int attempts =10 ; + + do { + ret = usb_submit_urb(urb, GFP_KERNEL); + switch (ret) { + case -ENXIO: + case -ENOMEM: + if (attempts--) { + cond_resched();
drivers/gpu/drm/fl2000/fl2000.h enum fl2000_interface { · line 67
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000.h +10
-0+ ret = -EAGAIN ; + } +break ; +default : +break ; + } + }while (ret == -EAGAIN ); + +return ret; +}
[Severity: High]
What happens in fl2000_submit_urb() in drivers/gpu/drm/fl2000/fl2000.h if usb_submit_urb() returns -EAGAIN directly?
For example, usb_hcd_map_urb_for_dma() in drivers/usb/core/hcd.c and xhci_map_temp_buffer() in drivers/usb/host/xhci.c return -EAGAIN when DMA mapping fails (such as dma_map_sg() or dma_map_single()).
When usb_submit_urb() returns -EAGAIN, the switch statement hits the default branch without decrementing attempts or calling cond_resched(), while the loop condition (ret == -EAGAIN) remains true. Could this spin indefinitely in fl2000_stream_work() -> fl2000_stream_submit() -> fl2000_submit_urb()?
[ ... ]
Quoted 5 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +0
-0
[ ... ]
Quoted 44 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +44
-0+static void fl2000_free_sb (struct fl2000_stream_buf *sb) +{ +vunmap (sb->vaddr); + +sg_free_table (&sb->sgt); + +for (int i =0 ; i < sb->nr_pages && sb->pages[i]; i++) +__free_page (sb->pages[i]); + +kfree (sb->pages); + +kfree (sb); +} + +static struct fl2000_stream_buf *fl2000_alloc_sb (unsigned int size) +{ +int ret; +struct fl2000_stream_buf *sb; +unsigned int nr_pages =PAGE_ALIGN (size) >>PAGE_SHIFT ; + + sb =kzalloc_obj (*sb); +if (!sb) +return NULL ; + + sb->nr_pages = nr_pages; + + sb->pages =kzalloc_objs (*sb->pages, nr_pages); +if (!sb->pages) + goto error; + +for (int i =0 ; i < nr_pages; i++) { + sb->pages[i] =alloc_page (GFP_KERNEL ); +if (!sb->pages[i]) + goto error; + } + + ret =sg_alloc_table_from_pages (&sb->sgt, sb->pages, nr_pages,0 , size,GFP_KERNEL ); +if (ret !=0 ) + goto error; + + sb->vaddr =vmap (sb->pages, nr_pages,VM_MAP ,PAGE_KERNEL ); +if (!sb->vaddr) + goto error; +
drivers/gpu/drm/fl2000/fl2000_streaming.c line 226
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +10
-0+INIT_LIST_HEAD (&sb->list); +memset (sb->vaddr,0 , nr_pages <<PAGE_SHIFT ); + +return sb; + +error: +fl2000_free_sb (sb); + +return NULL ; +}
[Severity: High]
In drivers/gpu/drm/fl2000/fl2000_streaming.c, fl2000_alloc_sb() sets sb->nr_pages = nr_pages before allocating sb->pages with kzalloc_objs(). If that allocation fails, it jumps to error and calls fl2000_free_sb(sb).
In fl2000_free_sb(), the loop checks:
for (int i = 0; i < sb->nr_pages && sb->pages[i]; i++)
When sb->nr_pages is non-zero and sb->pages is NULL, won't evaluating sb->pages[0] dereference a NULL pointer?
[ ... ]
Quoted 11 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +11
-0+static void fl2000_stream_data_completion (struct urb *urb) +{ +struct fl2000_stream_buf *cur_sb = urb->transfer_buffer; +struct usb_device *usb_dev = urb->dev; +struct fl2000_stream *stream = urb->context; +unsigned long flags; + +if (stream) { +spin_lock_irqsave (&stream->list_lock, flags); +list_move_tail (&cur_sb->list, &stream->render_list); +spin_unlock_irqrestore (&stream->list_lock, flags);
[ ... ]
drivers/gpu/drm/fl2000/fl2000_streaming.c line 596
Quoted 11 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +11
-0+/* +* Building the frame copies up to several MB and must run with +* interrupts enabled, so cur_sb is owned (off all lists) for +* the duration. Holding list_lock (which disables IRQs) across +* it starves the whole system. +*/ +fl2000_stream_build (stream, cur_sb); + +spin_lock_irq (&stream->list_lock); +list_add_tail (&cur_sb->list, &stream->wait_list); +spin_unlock_irq (&stream->list_lock);
[Severity: Medium]
In fl2000_alloc_sb() in drivers/gpu/drm/fl2000/fl2000_streaming.c, the scatter-gather pages in sb->sgt are mapped into a virtually contiguous kernel range via vmap() at sb->vaddr, and fl2000_stream_build() writes the frame and cursor data through cur_sb->vaddr.
When fl2000_stream_work() then submits data_urb with data_urb->sg and data_urb->num_sgs, usb_hcd_map_urb_for_dma() calls dma_map_sg(), which only flushes the linear direct-map alias for each scatterlist page rather than the vmap() alias cur_sb->vaddr.
On architectures with virtually indexed (VIVT or aliasing VIPT) caches where ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE is set, does fl2000_stream_work() need to call flush_kernel_vmap_range(cur_sb->vaddr, stream->buf_size) after fl2000_stream_build() and before submitting the URB so dirty cache lines in the vmap alias are written back before DMA?
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +10
-0+ + data_urb =usb_alloc_urb (0 ,GFP_KERNEL ); + eof_urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!data_urb || !eof_urb) { +usb_free_urb (data_urb); +usb_free_urb (eof_urb); +dev_err (&usb_dev->dev,"Data URB allocation error\n" ); +WRITE_ONCE (stream->enabled, false); +return ; + }
drivers/gpu/drm/fl2000/fl2000_streaming.c line 616
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +10
-0+ +/* +* Endpoint 1 bulk out. We store pointer to current stream buffer structure in +* transfer_buffer field of URB which is unused due to SGT. +*/ +usb_fill_bulk_urb (data_urb, usb_dev,usb_sndbulkpipe (usb_dev,1 ), cur_sb, + (int )stream->buf_size, fl2000_stream_data_completion, stream); + data_urb->interval =0 ; + data_urb->sg = cur_sb->sgt.sgl; + data_urb->num_sgs = cur_sb->sgt.nents;
[Severity: High]
Is it safe to store cur_sb in data_urb->transfer_buffer while setting data_urb->transfer_buffer_length to stream->buf_size in fl2000_stream_work()?
Even when urb->sg and urb->num_sgs are set, several paths in the USB core and host controller drivers inspect or overwrite urb->transfer_buffer:
1. usb_submit_urb() calls kmsan_handle_urb(), which checks urb->transfer_buffer_length bytes starting at urb->transfer_buffer when urb->transfer_buffer is non-NULL, reading stream->buf_size bytes (several megabytes) out-of-bounds past the cur_sb slab object.
2. In usb_hcd_map_urb_for_dma(), HCDs with hcd->localmem_pool check localmem_pool before urb->num_sgs and call hcd_alloc_coherent(), which copies urb->transfer_buffer_length bytes out-of-bounds from cur_sb and overwrites urb->transfer_buffer.
3. In xhci_map_urb_for_dma(), when XHCI_SG_TRB_CACHE_SIZE_QUIRK triggers xhci_map_temp_buffer(), xHCI replaces urb->transfer_buffer with a temporary bounce buffer and xhci_unmap_temp_buf() sets urb->transfer_buffer = NULL before completion, causing fl2000_stream_data_completion() to read cur_sb as NULL and dereference it at list_move_tail(&cur_sb->list, &stream->render_list).
Could data_urb->transfer_buffer be left NULL and urb->context (with a back pointer in struct fl2000_stream_buf to struct fl2000_stream) be used instead?
[ ... ]
Quoted 20 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +20
-0+void fl2000_stream_compress (struct fl2000_stream *stream,const struct iosys_map *src_map, +bool imported,u32 format,unsigned int width,unsigned int height, +unsigned int pitch,const struct drm_rect *clip) +{ +bool wc = imported || src_map->is_iomem; +u32 dst_line_len; +u32 x_start, x_end, y_start, y_end; +size_t span; + + x_start =min_t (u32,max_t (int, clip->x1,0 ), width); + x_end =min_t (u32,max_t (int, clip->x2,0 ), width); + y_start =min_t (u32,max_t (int, clip->y1,0 ), height); + y_end =min_t (u32,max_t (int, clip->y2,0 ), height); +if (x_start >= x_end || y_start >= y_end) +return ; + +guard (mutex)(&stream->shadow_lock); + +if (!stream->shadow || width != stream->width || height != stream->height) +return ;
[ ... ]
Quoted 31 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +31
-0+int fl2000_stream_mode_set (struct fl2000_stream *stream,unsigned int width, +unsigned int height,u32 bytes_pix) +{ +int ret; +unsigned int size; +void *shadow; + +/* Round buffer size up to multiple of 8 to meet HW expectations */ + size =ALIGN (width * height * bytes_pix,8 ); + +guard (mutex)(&stream->shadow_lock); + + stream->bytes_pix = bytes_pix; + stream->width = width; + stream->height = height; + +/* If there are buffers with same size - keep them */ +if (stream->buf_size == size) +return 0 ; + +/* Destroy wrong size buffers if they exist */ +if (!list_empty (&stream->render_list)) +fl2000_stream_put_buffers (stream); + +/* Allocate new buffers */ + ret =fl2000_stream_get_buffers (stream, size); +if (ret) { +fl2000_stream_put_buffers (stream); + stream->buf_size =0 ; +return ret; + }
drivers/gpu/drm/fl2000/fl2000_streaming.c line 878
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_streaming.c new file +10
-0+ +/* One full frame in the device format; only the CPU ever reads it */ + shadow =vzalloc (size); +if (!shadow) { +fl2000_stream_put_buffers (stream); + stream->buf_size =0 ; +return -ENOMEM ; + } +vfree (stream->shadow); + stream->shadow = shadow;
[Severity: Critical]
In fl2000_stream_mode_set() in drivers/gpu/drm/fl2000/fl2000_streaming.c, stream->bytes_pix, stream->width, and stream->height are updated to the new mode dimensions before fl2000_stream_get_buffers(stream, size) and vzalloc(size) succeed.
If a smaller mode was previously active and switching to a larger mode fails in fl2000_stream_get_buffers() or vzalloc(), the error paths set stream->buf_size = 0 but leave stream->shadow pointing to the old, smaller buffer while stream->width, stream->height, and stream->bytes_pix hold the new, larger dimensions.
When fl2000_stream_compress() is subsequently called for the new mode, its check:
if (!stream->shadow || width != stream->width || height != stream->height) return;
succeeds because stream->shadow is non-NULL and width and height match the updated fields. Doesn't the conversion loop then write up to width * height * stream->bytes_pix bytes past the end of the old smaller stream->shadow allocation?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
RFC PATCH 4/4 drm/fl2000: Add KMS display pipeline
In reply to Angel Gabriel Crispin ValdiviaRegister the DRM device once all FL2000 interfaces are bound and the IT66121 component is available, and connect the pieces:
- a primary plane, a cursor plane, a CRTC and an encoder, with GEM SHMEM framebuffers accessed through the shadow-plane helpers and XRGB8888 (and the two 10-bit formats compositors render in, reduced to 8 bits) as formats. The framebuffer is only read by the CPU, so it does not need to be physically contiguous. The primary plane passes its merged damage to the stream; the cursor plane hands over its sprite, or only its position when the framebuffer is unchanged. Without a cursor plane, compositors keep the pointer on the primary GPU and it never appears on this output
- mode validation and PLL search in the CRTC's atomic_check (which adjusts the clock in adjusted_mode), and FL2000 programming in mode_set_nofb, before the bridge is enabled. The PLL cannot make most pixel clocks exactly: digital sinks are only offered modes it hits within 500 ppm, while the VGA output accepts 5000 ppm, since an analog monitor does not notice that but does notice a changed htotal
- the HDMI connector from drm_bridge_connector_init() on the encoder the IT66121 bridge is attached to. It is polled, as the bridge has no hot plug interrupt of its own
- the analog VGA output of the chip, which adapters with a VGA connector expose next to the HDMI one: a DAC encoder and a VGA connector on the same CRTC (the two encoders are clones of each other). The chip has one monitor sense input, which follows whichever monitor is plugged in, so the VGA connector reports a monitor when the chip senses one and the IT66121 does not; the EDID is read with the FL2000 I2C engine, kept while the monitor stays, and without one the standard timings are offered
- vblank from the DRM vblank timer, as the FL2000 has no vblank interrupt, and fbdev emulation
- the FL2000 interrupt endpoint, which reports monitor and IT66121 events and triggers a hotplug event
Tested on x86_64 with a UGREEN USB 3.0 adapter with HDMI and VGA connectors (FL2000DX + IT66121FN): a 1920x1080 monitor on HDMI and an old 1366x768 monitor without a usable EDID on VGA. EDID, mode setting and a GNOME Wayland desktop work, with the cursor, at 59 frames per second on the link and no measurable effect on the rest of the system. Adapters with a VGA connector only (no IT66121) are not supported.
--- drivers/gpu/drm/fl2000/Makefile | 5 +- drivers/gpu/drm/fl2000/fl2000.h | 37 ++ drivers/gpu/drm/fl2000/fl2000_drm.c | 986 ++++++++++++++++++++++++++++++ drivers/gpu/drm/fl2000/fl2000_drv.c | 93 ++- drivers/gpu/drm/fl2000/fl2000_interrupt.c | 237 +++++++ drivers/gpu/drm/fl2000/fl2000_vga.c | 256 ++++++++ 6 files changed, 1602 insertions(+), 12 deletions(-)
drivers/gpu/drm/fl2000/Makefile +4 -1
@@ -1,10 +1,13 @@ # SPDX-License-Identifier: GPL-2.0-only fl2000-y := \ + fl2000_drm.o \ fl2000_drv.o \ fl2000_i2c.o \ + fl2000_interrupt.o \ fl2000_it66121.o \ fl2000_registers.o \ - fl2000_streaming.o + fl2000_streaming.o \ + fl2000_vga.o obj-$(CONFIG_DRM_FL2000) += fl2000.o
drivers/gpu/drm/fl2000/fl2000.h +37 -0
@@ -131,6 +131,36 @@ void fl2000_stream_move_cursor(struct fl2000_stream *stream, int x, int y);int fl2000_stream_enable (struct fl2000_stream *stream);void fl2000_stream_disable (struct fl2000_stream *stream); +/* Interrupt polling task */ +struct fl2000_intr ; +struct fl2000_intr *fl2000_intr_create (struct usb_device *usb_dev,struct drm_device *drm); +void fl2000_intr_quiesce (struct fl2000_intr *intr); +void fl2000_intr_suspend (struct fl2000_intr *intr); +int fl2000_intr_resume (struct fl2000_intr *intr); +int fl2000_intr_monitor_connected (struct fl2000_intr *intr); +void fl2000_intr_destroy (struct usb_device *usb_dev); + +/* VGA output: a DAC encoder and connector next to the HDMI ones, on the same CRTC */ +struct drm_edid ; +struct fl2000_vga { +struct drm_encoder encoder; +struct drm_connector connector; +struct i2c_adapter *adapter; +struct fl2000_intr *intr; +/* The HDMI transmitter shares the chip's monitor sense with this output */ +struct drm_bridge *hdmi_bridge; +/* EDID of the monitor, kept for as long as it stays connected; NULL if it has none */ +const struct drm_edid *edid; +unsigned int edid_tries; +/* The monitor changed or went away: read the EDID again on the next probe */ +bool edid_stale; +}; + +int fl2000_vga_init (struct drm_device *drm,struct fl2000_vga *vga,struct drm_crtc *crtc, +struct drm_encoder *hdmi_encoder,struct i2c_adapter *adapter, +struct fl2000_intr *intr); +void fl2000_vga_sink_event (struct fl2000_vga *vga); +/* I2C adapter interface creation */ struct i2c_adapter *fl2000_i2c_init (struct usb_device *usb_dev);int fl2000_ddc_read_block (struct i2c_adapter *adapter,u8 *buf,unsigned int block,size_t len);
@@ -152,4 +182,11 @@ int fl2000_enable_interrupts(struct usb_device *usb_dev);int fl2000_read_status (struct usb_device *usb_dev,u32 *status);int fl2000_i2c_dword (struct usb_device *usb_dev,bool read,u16 addr,u8 offset,u32 *data); +/* DRM device creation */ +int fl2000_drm_bind (struct device *master); +void fl2000_drm_unbind (struct device *master); +void fl2000_drm_hotplug (struct drm_device *drm,u32 events); +int fl2000_drm_suspend (struct device *master); +int fl2000_drm_resume (struct device *master); +#endif /* FL2000_H */
drivers/gpu/drm/fl2000/fl2000_drm.c new file +986 -0
986 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_drv.c +82 -11
151 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_interrupt.c new file +237 -0
237 lines not on this page · raw message
drivers/gpu/drm/fl2000/fl2000_vga.c new file +256 -0
256 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 4/4] drm/fl2000: Add KMS display pipeline
Thank you for your contribution! Sashiko AI review found 13 potential issue(s) to consider: - [Critical] drm/fl2000: missing mode size check causes buffer overflow in stream setup - [Critical]…
Thank you for your contribution! Sashiko AI review found 13 potential issue(s) to consider:
- [Critical] drm/fl2000: missing mode size check causes buffer overflow in stream setup
- [Critical] drm/fl2000: OOB shadow write after fl2000_stream_mode_set() alloc failure
- [High] drm/fl2000: use-after-free of stream on unplug in cursor and CRTC callbacks
- [High] drm/fl2000: false VGA detection in fl2000_vga_sense() on HDMI unplug
- [High] drm/fl2000: usb_device devres outlives driver unbind and blocks rebind
- [High] drm/fl2000: active_if corruption and error handling bugs in fl2000_probe()
- [Medium] drm/fl2000: frame updates dropped in fl2000_plane_atomic_update() on larger fb
- [Medium] drm/fl2000: IT66121 bridge not detached on unbind when DRM fd stays open
- [Medium] drm/fl2000: missing fl2000_enable_interrupts() and racy status event reads
- [Medium] drm/fl2000: alpha formats exposed without blend mode in fl2000_drm_bind()
- [Medium] drm/fl2000: unsynchronized chip reset and I2C access vs it66121_intr_work()
- [Medium] drm/fl2000: corrupted URB interval, endpoint, and events in fl2000_intr
- [Medium] drm/fl2000: broken devs->suspended tracking on fl2000_suspend() error --
commit db62c82606de6ffbad39e1ab2d4b34dcf543976b
Author: Angel Gabriel Crispin Valdivia <[email protected]>
drm/fl2000: Add KMS display pipeline
This commit adds the KMS display pipeline for the Fresco Logic FL2000 USB display driver, including primary and cursor planes, CRTC mode validation and programming, HDMI bridge and analog VGA connectors, and USB interrupt handling.
Quoted 5 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +0
-0
[ ... ]
Quoted 6 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +6
-0+static enum drm_mode_status fl2000_crtc_mode_valid (struct drm_crtc *crtc, +const struct drm_display_mode *mode) +{ +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (crtc->dev); +struct drm_display_mode adjusted_mode; +struct fl2000_pll pll;
drivers/gpu/drm/fl2000/fl2000_drm.c line 313
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+ +/* Get PLL configuration and check if mode adjustments are needed */ +if (fl2000_mode_calc (mode, &adjusted_mode, &pll,FL2000_PPM_ERR_MAX_VGA )) +return MODE_BAD ; + +if (fl2000_get_bytes_pix (drm_if->usb_dev->speed, &adjusted_mode) ==0 ) +return MODE_CLOCK_HIGH ; + +return MODE_OK ; +}
[Severity: Critical]
Can a user-supplied display mode bypass FL2000_MAX_WIDTH and FL2000_MAX_HEIGHT here and in fl2000_crtc_atomic_check(), overflowing the 32-bit buffer size calculation in fl2000_stream_mode_set()?
While fl2000_drm_bind() sets mode_config->max_width and mode_config->max_height to 4000, the DRM core only enforces those limits when creating framebuffers and validating connector-probed modes. A DRM master submitting a custom mode with large hdisplay and vdisplay (for example hdisplay = 43691, vdisplay = 32768, clock = 25000 kHz) and only the cursor plane enabled passes fl2000_crtc_mode_valid() and fl2000_crtc_atomic_check().
When fl2000_crtc_mode_set_nofb() then calls fl2000_stream_mode_set():
drivers/gpu/drm/fl2000/fl2000_streaming.c:fl2000_stream_mode_set() {
...
/* Round buffer size up to multiple of 8 to meet HW expectations */
size = ALIGN(width * height * bytes_pix, 8);
...
}the 32-bit unsigned int multiplication width * height * bytes_pix wraps around, allocating small stream->shadow and transfer buffers while setting stream->width and stream->height to the unvalidated dimensions and causing out-of-bounds reads and writes during cursor blending and frame updates.
[ ... ]
Quoted 40 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +40
-0+static int fl2000_plane_atomic_check (struct drm_plane *plane,struct drm_atomic_commit *state) +{ +struct drm_plane_state *new_plane_state =drm_atomic_get_new_plane_state (state, plane); +struct drm_crtc_state *new_crtc_state =NULL ; +bool is_cursor = plane->type ==DRM_PLANE_TYPE_CURSOR ; + +/* Clipping a larger sprite would put a truncated pointer on screen */ +if (is_cursor && new_plane_state->fb && + (new_plane_state->fb->width >FL2000_CURSOR_MAX || + new_plane_state->fb->height >FL2000_CURSOR_MAX )) +return -EINVAL ; + +if (new_plane_state->crtc) + new_crtc_state =drm_atomic_get_new_crtc_state (state, new_plane_state->crtc); + +/* The cursor may be placed anywhere; the primary plane is the frame */ +return drm_atomic_helper_check_plane_state (new_plane_state, new_crtc_state, +DRM_PLANE_NO_SCALING ,DRM_PLANE_NO_SCALING , + is_cursor, false); +} + +/* +* Only the damaged rectangle is converted, into the stream's shadow frame, +* which is the one persistent copy every outgoing buffer is built from. +*/ +static void fl2000_plane_atomic_update (struct drm_plane *plane,struct drm_atomic_commit *state) +{ +struct drm_plane_state *old_plane_state =drm_atomic_get_old_plane_state (state, plane); +struct drm_plane_state *plane_state =drm_atomic_get_new_plane_state (state, plane); +struct drm_shadow_plane_state *shadow_plane_state =to_drm_shadow_plane_state (plane_state); +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (plane->dev); +struct drm_framebuffer *fb = plane_state->fb; +struct drm_rect damage; +ktime_t start; +int idx; + +if (!fb) +return ;/* plane is disabled */ + +if (!drm_atomic_helper_damage_merged (old_plane_state, plane_state, &damage))
drivers/gpu/drm/fl2000/fl2000_drm.c line 373
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+return ; + +if (drm_gem_fb_begin_cpu_access (fb,DMA_FROM_DEVICE )) +return ; + +if (drm_dev_enter (plane->dev, &idx)) { + start =ktime_get (); +fl2000_stream_compress (drm_if->stream, &shadow_plane_state->data[0 ], +drm_gem_is_imported (fb->obj[0 ]), fb->format->format, + fb->width, fb->height, fb->pitches[0 ], &damage);
[Severity: Medium]
What happens in fl2000_plane_atomic_update() when the attached framebuffer is larger than the active CRTC mode (for example after fbdev switches from 1920x1080 to a smaller mode while reusing its initial framebuffer, or in a multi-monitor Xorg layout with a non-zero source offset)?
In fl2000_plane_atomic_check(), drm_atomic_helper_check_plane_state() allows any framebuffer whose dimensions are at least as large as the CRTC mode. However, fl2000_crtc_mode_set_nofb() sets stream->width and stream->height to mode->hdisplay and mode->vdisplay, whereas fl2000_plane_atomic_update() passes fb->width, fb->height, and untranslated damage coordinates to fl2000_stream_compress():
drivers/gpu/drm/fl2000/fl2000_streaming.c:fl2000_stream_compress() {
...
if (!stream->shadow || width != stream->width || height != stream->height)
return;
...
}Doesn't fl2000_stream_compress() silently drop every frame update whenever fb->width != mode->hdisplay or fb->height != mode->vdisplay?
Quoted 23 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +23
-0+fl2000_stream_note_render (drm_if->stream, + (unsigned int )ktime_us_delta (ktime_get (), start), +drm_rect_width (&damage) *drm_rect_height (&damage)); +drm_dev_exit (idx); + } + +drm_gem_fb_end_cpu_access (fb,DMA_FROM_DEVICE ); +} + +/* +* Hand the cursor sprite to the stream, which blends it into every outgoing +* frame. Moving the pointer keeps the same framebuffer and skips the copy. +*/ +static void fl2000_cursor_atomic_update (struct drm_plane *plane,struct drm_atomic_commit *state) +{ +struct drm_plane_state *old_plane_state =drm_atomic_get_old_plane_state (state, plane); +struct drm_plane_state *plane_state =drm_atomic_get_new_plane_state (state, plane); +struct drm_shadow_plane_state *shadow_plane_state =to_drm_shadow_plane_state (plane_state); +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (plane->dev); +struct drm_framebuffer *fb = plane_state->fb; +unsigned int w, h; +u32 *sprite; +int idx;
drivers/gpu/drm/fl2000/fl2000_drm.c line 406
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+ +if (!fb || !plane_state->visible) { +fl2000_stream_set_cursor (drm_if->stream,NULL ,0 ,0 ,0 ,0 ); +return ; + } + +if (fb == old_plane_state->fb && old_plane_state->visible) { +fl2000_stream_move_cursor (drm_if->stream, plane_state->crtc_x, plane_state->crtc_y); +return ; + }
[Severity: High]
Can these early cursor branches, as well as fl2000_crtc_mode_set_nofb(), fl2000_crtc_atomic_enable(), and fl2000_crtc_atomic_disable() below, cause a use-after-free of drm_if->stream if a USB disconnect races with an in-flight atomic commit?
In fl2000_drm_if_release(), drm_dev_unplug() and drm_atomic_helper_shutdown() are followed immediately by fl2000_stream_destroy(usb_dev), which frees drm_if->stream while drm_if remains allocated until the last open DRM file descriptor is closed. An ioctl or non-blocking commit that entered before drm_dev_unplug() and was waiting on modeset locks will proceed once drm_atomic_helper_shutdown() releases those locks.
Unlike the sprite upload branch below (which checks drm_dev_enter()), the cursor hide and move paths here, along with fl2000_crtc_mode_set_nofb(), fl2000_crtc_atomic_enable(), and fl2000_crtc_atomic_disable(), dereference drm_if->stream and drm_if->usb_dev without a drm_dev_enter() guard.
[ ... ]
drivers/gpu/drm/fl2000/fl2000_drm.c line 553
Quoted 7 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +7
-0+/* Enable interrupts */ +fl2000_enable_interrupts (usb_dev); + +fl2000_afe_magic (usb_dev); + +fl2000_stream_mode_set (drm_if->stream, mode->hdisplay, mode->vdisplay, bytes_pix); +}
[Severity: Critical]
What happens if fl2000_stream_mode_set() fails to allocate buffers when switching from a smaller resolution to a larger one?
In fl2000_crtc_mode_set_nofb(), the return value of fl2000_stream_mode_set() is ignored. Inside fl2000_stream_mode_set():
drivers/gpu/drm/fl2000/fl2000_streaming.c:fl2000_stream_mode_set() {
...
stream->bytes_pix = bytes_pix;
stream->width = width;
stream->height = height;
...
ret = fl2000_stream_get_buffers(stream, size);
if (ret) {
fl2000_stream_put_buffers(stream);
stream->buf_size = 0;
return ret;
}
shadow = vzalloc(size);
if (!shadow) {
fl2000_stream_put_buffers(stream);
stream->buf_size = 0;
return -ENOMEM;
}
...
}stream->width, stream->height, and stream->bytes_pix are updated to the new dimensions before allocation, while stream->shadow still points to the old smaller buffer on failure. When fl2000_plane_atomic_update() subsequently calls fl2000_stream_compress(), the dimension check passes against the new stream->width and stream->height, writing past the end of the old smaller stream->shadow buffer.
[ ... ]
Quoted 30 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +30
-0+static void fl2000_drm_if_release (struct device *dev,void *res) +{ +struct fl2000_drm_if *drm_if = *(struct fl2000_drm_if **)res; +struct drm_device *drm = &drm_if->drm; +struct usb_device *usb_dev = drm_if->usb_dev; + +dev_dbg (dev,"Unbinding FL2000 master\n" ); + +/* No hotplug events for a node that is about to disappear */ +fl2000_intr_quiesce (drm_if->intr); +drm_kms_helper_poll_fini (drm); + +/* +* Block userspace access and shut down the display pipeline while all +* the components are still bound. +*/ +drm_dev_unplug (drm); +drm_atomic_helper_shutdown (drm); + +/* +* Detach bridge component. This release function runs with the global +* component mutex held (component_master_del() -> master unbind -> +* devres_release()), so it must never call component_del() - that would +* self-deadlock. The IT66121 is destroyed by a usb_device devres instead. +*/ +component_unbind_all (dev, drm); + +/* Stop streaming interface */ +fl2000_stream_destroy (usb_dev); +
drivers/gpu/drm/fl2000/fl2000_drm.c line 680
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+/* Stop interrupts interface */ +fl2000_intr_destroy (usb_dev); + +/* +* Drop the driver's reference. The DRM device is freed when the last +* userspace file is closed; it only pins the USB device, never the I2C +* adapter, so the disconnect does not wait for userspace. +*/ +drm_dev_put (drm); +}
[Severity: Medium]
If the interfaces unbind and rebind (for example across usb_reset_device()) while userspace still holds an open file descriptor to the DRM device, does the IT66121 bridge remain attached to the old encoder?
In fl2000_drm_bind(), drm_if->encoder is initialized with drmm_encoder_init(), so drm_encoder_cleanup() -> drm_bridge_detach() only runs when the final reference to struct drm_device is dropped. Because it66121_unbind() is a no-op:
drivers/gpu/drm/fl2000/fl2000_it66121.c:it66121_unbind() {
/* The bridge is detached by the DRM core when the encoder is cleaned up */
dev_dbg(comp, "Unbinding IT66121 component\n");
}calling component_unbind_all() and drm_dev_put() here while userspace holds an open DRM fd leaves bridge->dev pointing to the old drm_device and leaves priv->work running. When fl2000_drm_bind() runs on rebind, won't it66121_bind() -> drm_bridge_attach() fail with -EBUSY?
[ ... ]
Quoted 13 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +13
-0+int fl2000_drm_resume (struct device *master) +{ +struct fl2000_drm_if *drm_if =fl2000_drm_if_of (master); +struct usb_device *usb_dev; +struct drm_bridge *bridge; +int ret; + +if (!drm_if) +return 0 ; + usb_dev = drm_if->usb_dev; + +/* First, as the IT66121 is reached through the FL2000 I2C engine */ +fl2000_regmap_drop_cache (usb_dev);
drivers/gpu/drm/fl2000/fl2000_drm.c line 757
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+ + bridge =drm_bridge_chain_get_first_bridge (&drm_if->encoder); +if (bridge) { +it66121_resume (bridge); +drm_bridge_put (bridge); + } + +/* The same order as in fl2000_drm_bind() */ +fl2000_reset (usb_dev); +fl2000_usb_magic (usb_dev);
[Severity: Medium]
Can fl2000_reset() and fl2000_usb_magic() race with it66121_intr_work() here, in fl2000_drm_bind(), and in fl2000_crtc_mode_set_nofb()?
Calling it66121_resume() (or component_bind_all() in fl2000_drm_bind()) starts the periodic it66121_intr_work() task, which polls IT66121 registers over the FL2000 I2C controller every 100 ms. Immediately afterwards, fl2000_reset() issues a software reset and fl2000_usb_magic() modifies FL2000_VGA_I2C_SC_REG without holding the I2C bus lock. If an I2C read in it66121_intr_work() coincides with the reset or register write and fails, it66121_intr_work() returns early without re-queueing priv->work, permanently stopping IT66121 interrupt polling.
[ ... ]
Quoted 8 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +8
-0+ ret =drm_universal_plane_init (drm, &drm_if->plane,0 , &fl2000_plane_funcs, + fl2000_pixel_formats,ARRAY_SIZE (fl2000_pixel_formats), + fl2000_plane_format_modifiers,DRM_PLANE_TYPE_PRIMARY ,NULL ); +if (ret) { +drm_err (drm,"Cannot initialize plane (%d)\n" , ret); + goto err_free_holder; + } +drm_plane_helper_add (&drm_if->plane, &fl2000_plane_helper_funcs);
drivers/gpu/drm/fl2000/fl2000_drm.c line 850
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+ +/* +* Enable the damage-clips property the update path relies on +* (drm_atomic_helper_damage_merged()). +*/ +drm_plane_enable_fb_damage_clips (&drm_if->plane); + + ret =drm_universal_plane_init (drm, &drm_if->cursor,0 , &fl2000_plane_funcs, + fl2000_cursor_formats,ARRAY_SIZE (fl2000_cursor_formats), + fl2000_plane_format_modifiers,DRM_PLANE_TYPE_CURSOR ,NULL );
[Severity: Medium]
Should fl2000_drm_bind() attach a blend mode property via drm_plane_create_blend_mode_property() to drm_if->plane and drm_if->cursor?
Both fl2000_pixel_formats (DRM_FORMAT_ARGB2101010) and fl2000_cursor_formats (DRM_FORMAT_ARGB8888) expose formats with an alpha channel. Without a blend mode property on those planes, drm_dev_register() -> drm_mode_config_validate() logs a drm_warn() for each plane on every bind, and userspace cannot tell that the primary plane ignores alpha (DRM_MODE_BLEND_PIXEL_NONE) while the cursor plane blends with coverage alpha (DRM_MODE_BLEND_COVERAGE).
[ ... ]
drivers/gpu/drm/fl2000/fl2000_drm.c line 931
Quoted 9 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +9
-0+ ret =drm_vblank_init (drm, drm->mode_config.num_crtc); +if (ret) { +drm_err (drm,"Failed to initialize %d VBLANK(s) (%d)\n" , + drm->mode_config.num_crtc, ret); + goto err_unbind; + } + +fl2000_reset (usb_dev); +fl2000_usb_magic (usb_dev);
[Severity: Medium]
Should fl2000_enable_interrupts(usb_dev) also be called after fl2000_usb_magic(usb_dev) in fl2000_drm_bind() and fl2000_drm_resume()?
Currently fl2000_enable_interrupts() is only called in fl2000_crtc_mode_set_nofb(), so FL2000_VGA_CTRL_ACLK_EDID_MON_INT_EN and FL2000_VGA_CTRL2_ACLK_HDMI_INT_EN remain disabled after bind and after system resume whenever no modeset is active.
In addition, when fl2000_vga_detect_ctx() reads the self-clearing FL2000_VGA_STATUS_REG via fl2000_intr_monitor_connected() -> fl2000_intr_read_status(), any pending FL2000_VGA_STATUS_MONITOR_EVENT or FL2000_VGA_STATUS_EDID_EVENT bits are cleared from hardware and ORed into intr->events, but fl2000_vga_detect_ctx() does not set vga->edid_stale when status is connector_status_connected, so fl2000_vga_get_modes() can reuse a stale EDID on that probe while a later fl2000_intr_work() run marks the newly read EDID stale.
[ ... ]
Quoted 4 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +0
-0
[ ... ]
Quoted 15 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +4
-0@@ -49,6 +84,8 @@ static struct fl2000_devs *fl2000_get_devices(struct usb_device *usb_dev)return ERR_CAST (devs->adapter); } +component_match_add (&devs->adapter->dev, &devs->match, fl2000_compare, devs->adapter); +dev_set_drvdata (&usb_dev->dev, devs);return devs;@@ -138,6 +175,16 @@ static int fl2000_probe(struct usb_interface *interface, const struct usb_devicebreak ; } +/* When all interfaces are up - proceed with registration */ +if (devs->active_if ==FL2000_ALL_IFS ) {
drivers/gpu/drm/fl2000/fl2000_drv.c static int fl2000_probe(struct usb_interface *interface, const struct usb_device · line 189
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +8
-0+ ret =component_master_add_with_match (&devs->adapter->dev, &fl2000_master_ops, + devs->match); +if (ret) { +dev_err (&usb_dev->dev,"Cannot register component master (%d)\n" , ret); +return ret; + } + } +return ret; }
[Severity: High]
Are there a few error-handling regressions here in fl2000_get_devices() and fl2000_probe()?
First, if component_match_add() fails to allocate in fl2000_get_devices(), it sets devs->match to ERR_PTR(-ENOMEM), which is not checked before component_master_add_with_match() dereferences match->num in component_match_realloc().
Second, in fl2000_probe(), BIT(iface_num) is set in devs->active_if before calling component_master_add_with_match(). If component_master_add_with_match() fails, fl2000_probe() returns ret without clearing BIT(iface_num), and the driver core will not call fl2000_disconnect() for the interface that failed to probe, leaving devs->active_if permanently corrupted.
Third, in the default branch of switch (iface_num), fl2000_probe() sets ret = -ENODEV and breaks instead of returning early; if devs->active_if is already FL2000_ALL_IFS, execution falls through and calls component_master_add_with_match() a second time.
Quoted 9 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +1
-4@@ -151,11 +198,18 @@ static void fl2000_disconnect(struct usb_interface *interface)return ;/* -* The IT66121 bridge is intentionally NOT destroyed here: disconnect -* also runs for the transient unbind/rebind cycles of -* usb_reset_device(), where the usb_device survives. Its destruction is -* a devres of the usb_device, see fl2000_bridge_create(). +* The IT66121 bridge component is intentionally NOT destroyed here.
drivers/gpu/drm/fl2000/fl2000_drv.c static void fl2000_disconnect(struct usb_interface *interface) · line 211
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +9
-0+* Disconnect also runs for the transient unbind/rebind cycles of +* usb_reset_device(), where the usb_device (and the I2C adapter and +* aggregate match data) survive: the component must persist across +* those. Its destruction is a devres of the usb_device, see +* fl2000_bridge_create(). Destroying it from the master unbind path is +* also forbidden - component_del() would self-deadlock on the global +* component mutex held across the unbind callback. */ +if (devs->active_if ==FL2000_ALL_IFS ) +component_master_del (&devs->adapter->dev, &fl2000_master_ops);
[Severity: High]
What happens if the fl2000 module is unloaded (or the interfaces are unbound via sysfs) while the USB device remains plugged in?
Because fl2000_get_devices() and fl2000_bridge_create() attach devs, regmap, the I2C adapter, the component match callback fl2000_compare(), and the IT66121 bridge component as devres on &usb_dev->dev and &devs->adapter->dev, and &usb_dev->dev stays bound to usb_generic_driver when the usb_interface devices unbind, fl2000_disconnect() leaves those resources, workqueues, and callbacks registered on usb_device after all interfaces have disconnected. Unloading the module while the adapter is plugged in will leave devres release callbacks and function pointers on usb_device pointing into unmapped module memory.
[ ... ]
Quoted 12 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +6
-5+static int fl2000_suspend (struct usb_interface *interface,pm_message_t message) +{ +struct usb_device *usb_dev =interface_to_usbdev (interface); +struct fl2000_devs *devs =dev_get_drvdata (&usb_dev->dev); +int ret; -/* -* Nothing to save yet. Providing the callback keeps the USB core from -* unbinding the driver across system sleep. -*/ -dev_dbg (&usb_dev->dev,"suspend\n" ); +if (!devs || devs->suspended)
drivers/gpu/drm/fl2000/fl2000_drv.c static void fl2000_disconnect(struct usb_interface *interface) · line 250
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drv.c +7
-0+return 0 ; + + ret =fl2000_drm_suspend (&devs->adapter->dev); +if (ret) +return ret; + + devs->suspended = true;return 0 ; }
[Severity: Medium]
If fl2000_drm_suspend() returns an error during system sleep, won't the USB device still suspend with devs->suspended left false?
During system sleep (!PMSG_IS_AUTO(message)), usb_suspend_both() in the USB core ignores errors returned by usb_suspend_interface() and proceeds to suspend the device anyway. If drm_mode_config_helper_suspend() fails inside fl2000_drm_suspend(), fl2000_drm_suspend() restarts the interrupt URB and skips it66121_suspend(), and fl2000_suspend() leaves devs->suspended false, causing fl2000_resume() on wake or reset-resume to return early without calling fl2000_drm_resume().
[ ... ]
Quoted 5 lines
drivers/gpu/drm/fl2000/fl2000_interrupt.c new file +0
-0
[ ... ]
Quoted 19 lines
drivers/gpu/drm/fl2000/fl2000_interrupt.c new file +19
-0+static void fl2000_intr_completion (struct urb *urb) +{ +int ret; +struct usb_device *usb_dev = urb->dev; +struct fl2000_intr *intr = urb->context; + + ret =fl2000_urb_status (usb_dev, urb->status); +if (ret) { +/* Killed on purpose (suspend, unplug) is no reason to complain */ +if (ret != -ENOENT && ret != -ECONNRESET ) +dev_err (&usb_dev->dev,"Stopping interrupts\n" ); +return ; + } + +/* Processing reads registers over USB, which sleeps: defer it to a work item */ +queue_work (intr->work_queue, &intr->work); + +/* +* For interrupt URBs, as part of successful URB submission urb->interval is modified to
drivers/gpu/drm/fl2000/fl2000_interrupt.c line 117
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_interrupt.c new file +10
-0+* reflect the actual transfer period used, so we need to restore it. +*/ + urb->interval = intr->poll_interval; + urb->start_frame =-1 ; + +/* Restart urb */ + ret =usb_submit_urb (urb,GFP_ATOMIC ); +if (ret) +dev_err (&usb_dev->dev,"URB submission failed (%d)\n" , ret); +}
[Severity: Medium]
Doesn't setting urb->interval = intr->poll_interval here corrupt the High-Speed / SuperSpeed polling interval on every resubmission?
In fl2000_intr_create(), intr->poll_interval stores the raw logarithmic desc->bInterval exponent, and usb_fill_int_urb() converts that exponent into microframes (1 << (bInterval - 1)) in urb->interval. Overwriting urb->interval with the raw intr->poll_interval before usb_submit_urb() passes the exponent as a microframe count.
Also in fl2000_intr_create(), usb_find_int_in_endpoint() locates desc, but usb_fill_int_urb() hardcodes endpoint 3 via usb_rcvintpipe(usb_dev, 3) instead of using usb_endpoint_num(desc); and in fl2000_intr_work(), atomic_xchg(&intr->events, 0) consumes pending events before fl2000_drm_hotplug() checks drm_if->registered, so any sink or EDID event arriving before registration is dropped without calling fl2000_vga_sink_event(&drm_if->vga).
[ ... ]
Quoted 5 lines
drivers/gpu/drm/fl2000/fl2000_vga.c new file +0
-0
[ ... ]
Quoted 13 lines
drivers/gpu/drm/fl2000/fl2000_vga.c new file +13
-0+static enum drm_connector_status fl2000_vga_sense (struct fl2000_vga *vga) +{ +int ret =fl2000_intr_monitor_connected (vga->intr); + +if (ret <0 ) +return connector_status_unknown; +if (!ret) +return connector_status_disconnected; + +if (vga->hdmi_bridge) { +/* +* Not knowing whether there is an HDMI sink is not the same as there being none: +* two connected outputs on the one CRTC can leave a compositor with neither.
drivers/gpu/drm/fl2000/fl2000_vga.c line 74
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_vga.c new file +10
-0+*/ + ret =it66121_hpd_connected (vga->hdmi_bridge); +if (ret <0 ) +return connector_status_unknown; +if (ret) +return connector_status_disconnected; + } + +return connector_status_connected; +}
[Severity: High]
Won't fl2000_vga_sense() falsely report a connected VGA monitor whenever an active HDMI monitor is unplugged?
While the CRTC is driving the HDMI encoder, fl2000_crtc_force_connect() keeps FL2000_VGA_CTRL_ACLK_FORCE_VGA_CONNECT set, which forces the chip's shared FL2000_VGA_STATUS_MONITOR_STATUS bit to 1 until fl2000_crtc_atomic_disable() clears it. When the HDMI cable is unplugged, connector detection runs before the CRTC is disabled:it66121_hpd_connected(vga->hdmi_bridge) returns 0, while fl2000_intr_monitor_connected(vga->intr) still returns 1 because monitor sense is forced, causing fl2000_vga_sense() to report connector_status_connected on the empty VGA connector with fallback modes.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Thomas Zimmermann
maintainerRe: [PATCH RFC 4/4] drm/fl2000: Add KMS display pipeline
In reply to Angel Gabriel Crispin ValdiviaHi,
you'll find a review below.
Am 06.11.26 um 03:34 schrieb Angel Gabriel Crispin Valdivia via B4 Relay:
Quoted 94 lines
From: Angel Gabriel Crispin Valdivia <[email protected]>
Register the DRM device once all FL2000 interfaces are bound and the IT66121 component is available, and connect the pieces:
- a primary plane, a cursor plane, a CRTC and an encoder, with GEM SHMEM framebuffers accessed through the shadow-plane helpers and XRGB8888 (and the two 10-bit formats compositors render in, reduced to 8 bits) as formats. The framebuffer is only read by the CPU, so it does not need to be physically contiguous. The primary plane passes its merged damage to the stream; the cursor plane hands over its sprite, or only its position when the framebuffer is unchanged. Without a cursor plane, compositors keep the pointer on the primary GPU and it never appears on this output
- mode validation and PLL search in the CRTC'satomic_check(which adjusts the clock inadjusted_mode), and FL2000 programming inmode_set_nofb, before the bridge is enabled. The PLL cannot make most pixel clocks exactly: digital sinks are only offered modes it hits within 500 ppm, while the VGA output accepts 5000 ppm, since an analog monitor does not notice that but does notice a changed htotal
- the HDMI connector fromdrm_bridge_connector_init()on the encoder the IT66121 bridge is attached to. It is polled, as the bridge has no hot plug interrupt of its own
- the analog VGA output of the chip, which adapters with a VGA connector expose next to the HDMI one: a DAC encoder and a VGA connector on the same CRTC (the two encoders are clones of each other). The chip has one monitor sense input, which follows whichever monitor is plugged in, so the VGA connector reports a monitor when the chip senses one and the IT66121 does not; the EDID is read with the FL2000 I2C engine, kept while the monitor stays, and without one the standard timings are offered
- vblank from the DRM vblank timer, as the FL2000 has no vblank interrupt, and fbdev emulation
- the FL2000 interrupt endpoint, which reports monitor and IT66121 events and triggers a hotplug eventTested on
x86_64with a UGREEN USB 3.0 adapter with HDMI and VGA connectors (FL2000DX + IT66121FN): a 1920x1080 monitor on HDMI and an old 1366x768 monitor without a usable EDID on VGA. EDID, mode setting and a GNOME Wayland desktop work, with the cursor, at 59 frames per second on the link and no measurable effect on the rest of the system. Adapters with a VGA connector only (no IT66121) are not supported.Signed-off-by Angel Gabriel Crispin Valdivia <[email protected]>--- drivers/gpu/drm/fl2000/Makefile | 5 +- drivers/gpu/drm/fl2000/fl2000.h | 37 ++ drivers/gpu/drm/fl2000/fl2000_drm.c | 986 ++++++++++++++++++++++++++++++ drivers/gpu/drm/fl2000/fl2000_drv.c | 93 ++- drivers/gpu/drm/fl2000/fl2000_interrupt.c | 237 +++++++ drivers/gpu/drm/fl2000/fl2000_vga.c | 256 ++++++++ 6 files changed, 1602 insertions(+), 12 deletions(-)drivers/gpu/drm/fl2000/Makefile +4
-1@@ -1,10 +1,13 @@ # SPDX-License-Identifier: GPL-2.0-only fl2000-y := \ + fl2000_drm.o \ fl2000_drv.o \ fl2000_i2c.o \ + fl2000_interrupt.o \ fl2000_it66121.o \ fl2000_registers.o \ - fl2000_streaming.o + fl2000_streaming.o \ + fl2000_vga.o obj-$(CONFIG_DRM_FL2000) += fl2000.odrivers/gpu/drm/fl2000/fl2000.h +14
-0@@ -131,6 +131,36 @@ void fl2000_stream_move_cursor(struct fl2000_stream *stream, int x, int y);int fl2000_stream_enable (struct fl2000_stream *stream);void fl2000_stream_disable (struct fl2000_stream *stream); +/* Interrupt polling task */ +struct fl2000_intr ; +struct fl2000_intr *fl2000_intr_create (struct usb_device *usb_dev,struct drm_device *drm); +void fl2000_intr_quiesce (struct fl2000_intr *intr); +void fl2000_intr_suspend (struct fl2000_intr *intr); +int fl2000_intr_resume (struct fl2000_intr *intr); +int fl2000_intr_monitor_connected (struct fl2000_intr *intr); +void fl2000_intr_destroy (struct usb_device *usb_dev); + +/* VGA output: a DAC encoder and connector next to the HDMI ones, on the same CRTC */ +struct drm_edid ; +struct fl2000_vga { +struct drm_encoder encoder; +struct drm_connector connector;
drivers/gpu/drm/fl2000/fl2000.h void fl2000_stream_move_cursor(struct fl2000_stream *stream, int x, int y); · line 157
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000.h +9
-0+struct i2c_adapter *adapter; +struct fl2000_intr *intr; +/* The HDMI transmitter shares the chip's monitor sense with this output */ +struct drm_bridge *hdmi_bridge; +/* EDID of the monitor, kept for as long as it stays connected; NULL if it has none */ +const struct drm_edid *edid; +unsigned int edid_tries; +/* The monitor changed or went away: read the EDID again on the next probe */ +bool edid_stale;
Don't cache the EDID. Rather re-read it from the device. We have elaborate helpers for EDID and best it to not try to out-smart them.
Quoted 48 lines
drivers/gpu/drm/fl2000/fl2000.h +7
-0+}; + +int fl2000_vga_init (struct drm_device *drm,struct fl2000_vga *vga,struct drm_crtc *crtc, +struct drm_encoder *hdmi_encoder,struct i2c_adapter *adapter, +struct fl2000_intr *intr); +void fl2000_vga_sink_event (struct fl2000_vga *vga); +/* I2C adapter interface creation */ struct i2c_adapter *fl2000_i2c_init (struct usb_device *usb_dev);int fl2000_ddc_read_block(struct i2c_adapter *adapter, u8 *buf, unsigned int block, size_t len);drivers/gpu/drm/fl2000/fl2000.h +7
-0@@ -152,4 +182,11 @@ int fl2000_enable_interrupts(struct usb_device *usb_dev);int fl2000_read_status (struct usb_device *usb_dev,u32 *status);int fl2000_i2c_dword (struct usb_device *usb_dev,bool read,u16 addr,u8 offset,u32 *data); +/* DRM device creation */ +int fl2000_drm_bind (struct device *master); +void fl2000_drm_unbind (struct device *master); +void fl2000_drm_hotplug (struct drm_device *drm,u32 events); +int fl2000_drm_suspend (struct device *master); +int fl2000_drm_resume (struct device *master); +#endif /* FL2000_H */ drivers/gpu/drm/fl2000/fl2000_drm.c new file +20
-0@@ -0,0 +1,986 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* (C) Copyright 2012, Red Hat +* (C) Copyright 2018-2020, Artem Mygaiev +*/ + +#include <linux/math64.h> + +#include <drm/clients/drm_client_setup.h> +#include <drm/drm_bridge_connector.h> +#include <drm/drm_connector.h> +#include <drm/drm_fbdev_shmem.h> +#include <drm/drm_gem_atomic_helper.h> +#include <drm/drm_modeset_helper.h> +#include <drm/drm_probe_helper.h> +#include <drm/drm_vblank_helper.h> + +#include "fl2000.h" +#include "fl2000_it66121.h" +
drivers/gpu/drm/fl2000/fl2000_drm.c line 30
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+#define DRM_DRIVER_NAME "fl2000" +#define DRM_DRIVER_DESC "Fresco Logic FL2000 USB display" + +#define DRM_DRIVER_MAJOR 0 +#define DRM_DRIVER_MINOR 0 +#define DRM_DRIVER_PATCHLEVEL 1 + +/* Maximum supported resolution, no documented hardware limit */ +#define FL2000_MAX_WIDTH 4000 +#define FL2000_MAX_HEIGHT 4000
How large a value could you write into the registers?
drivers/gpu/drm/fl2000/fl2000_drm.c line 42
Quoted 12 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +12
-0+ +/* +* 32-bit input. The 10-bit formats are accepted because the compositor hands +* us its scanout buffer as it is: Mutter renders ARGB2101010 and, when the +* secondary output cannot take that format, falls back to a copy path that +* has leaked GPU memory until the desktop stalled. +*/ +#define FL2000_FB_BPP 32 +static const u32 fl2000_pixel_formats[] = { +DRM_FORMAT_XRGB8888 , +DRM_FORMAT_XRGB2101010 , +DRM_FORMAT_ARGB2101010 ,
No ARGB on planes that cannot handle it. Primary planes most likely don't.
I assume that XRGB2101010 is the hardware's native or preferred format? It should go first then.
Quoted 35 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +35
-0+}; + +/* The cursor sprite is blended into the outgoing frame by the CPU */ +static const u32 fl2000_cursor_formats[] = { +DRM_FORMAT_ARGB8888 , +}; + +/* +* Maximum pixel clock set to 500MHz. It is hard to get more or less precise PLL configuration for +* higher clock. +*/ +#define FL2000_MAX_PIXCLOCK 500000000 + +/* PLL computing precision is 6 digits after the decimal point */ +#define FL2000_PLL_PRECISION 1000000 + +/* Input xtal clock, Hz */ +#define FL2000_XTAL 10000000/* 10 MHz */ + +/* Internal vco clock min/max, Hz */ +#define FL2000_VCOCLOCK_MIN 62500000/* 62.5 MHz */ +#define FL2000_VCOCLOCK_MAX 1000000000/* 1 GHz */ + +/* +* Maximum acceptable ppm error of the pixel clock. Digital sinks get the exact clock or nothing; +* an analog monitor locks to the line rate of whatever it is sent, so a clock off by half a +* percent goes unnoticed, while changing the length of a line (htotal) throws its sampling off. +*/ +#define FL2000_PPM_ERR_MAX 500 +#define FL2000_PPM_ERR_MAX_VGA 5000 + +/* +* Bulk payload the link sustains, bytes per second. High speed carries at most 13 packets of 512 +* bytes per 125 us microframe (53 MB/s); SuperSpeed 500 MB/s after its 8b/10b encoding. Packet and +* link overhead are taken as 10 %. On SuperSpeed 1920x1080 at 60 Hz in RGB888 (373 MB/s) streams
drivers/gpu/drm/fl2000/fl2000_drm.c line 87
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+* fine, and the vendor driver offers 1920x1200 at 60 Hz in RGB888 (415 MB/s). The FL2000 is a +* 5 Gb/s device: it never runs at SuperSpeed Plus. +*/ +#define FL2000_BULK_BW_HIGH_SPEED (53248000ull * 90 / 100) +#define FL2000_BULK_BW_SUPER_SPEED (500000000ull * 90 / 100) + +/* +* Bytes per pixel the USB link can carry for @mode: 3 (RGB888), 2 (RGB565) or 0 for neither. The +* FL2000 has a line buffer and no frame buffer: it takes the active pixels of exactly one frame per +* refresh, so the stream needs the rate of the active area, not the pixel clock.
That line buffer sounds awful to work with.
Quoted 29 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +29
-0+*/ +static unsigned int fl2000_get_bytes_pix (enum usb_device_speed speed, +const struct drm_display_mode *mode) +{ +u64 max_bw, rate; +u32 total = (u32 )mode->htotal * mode->vtotal; +u64 bytes_pix; + +switch (speed) { +case USB_SPEED_HIGH : + max_bw =FL2000_BULK_BW_HIGH_SPEED ; +break ; +case USB_SPEED_SUPER : +case USB_SPEED_SUPER_PLUS : + max_bw =FL2000_BULK_BW_SUPER_SPEED ; +break ; +default : +return 0 ; + } + +if (!total) +return 0 ; + +/* Active pixels per second; at most 500 MHz x 4000 x 4000, so no overflow */ + rate =div_u64 ((u64 )mode->clock *1000 * mode->hdisplay * mode->vdisplay, total); +if (!rate) +return 0 ; + + bytes_pix =div64_u64 (max_bw, rate);
drivers/gpu/drm/fl2000/fl2000_drm.c line 126
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+if (bytes_pix >=3 ) +return 3 ;/* RGB888 */ +if (bytes_pix ==2 ) +return 2 ;/* RGB565 */ + +/* RGB332 is not supported */ +return 0 ; +} + +struct fl2000_drm_if {
I'd rather call this structure fl2000_device. Although located at a USB interface, it is a DRM device.
drivers/gpu/drm/fl2000/fl2000_drm.c line 133
Quoted 7 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +7
-0+struct usb_device *usb_dev; +struct drm_device drm; +struct drm_plane plane; +/* +* Without a cursor plane of its own the compositor keeps the pointer on +* the primary GPU's hardware cursor, where it never reaches this output. +*/
That comment ???
If this refers to the cursor disappearing if another device has a hardware cursor, I'm aware of this problem wrt Gnome. Please fix this in the compositor. It's not a driver bug.
drivers/gpu/drm/fl2000/fl2000_drm.c line 134
Quoted 1 line
drivers/gpu/drm/fl2000/fl2000_drm.c new file +1
-0+struct drm_plane cursor;
From what I understand, the frame-streaming helpers emulate the cursor plane. We don't do this in the kernel. Compositors need to handle this. Please remove cursor support.
drivers/gpu/drm/fl2000/fl2000_drm.c line 141
Quoted 8 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +7
-0+struct drm_crtc crtc; +struct drm_encoder encoder; +struct fl2000_vga vga; +struct fl2000_stream *stream; +struct fl2000_intr *intr; +/* Set once userspace can see the device: events before that have nobody to tell */ +bool registered;
Drop that state please. No other driver uses this, as it's something that DRM helpers take care of.
Quoted 5 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +5
-0+}; + +static inline struct fl2000_drm_if *to_fl2000_drm_if (struct drm_device *drm) +{ + return container_of(drm, struct fl2000_drm_if, drm);
drivers/gpu/drm/fl2000/fl2000_drm.c line 156
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+} + +DEFINE_DRM_GEM_FOPS (fl2000_drm_driver_fops); + +/* +* Mode config cleanup is managed by drmm_mode_config_init(), and the display +* pipeline is shut down on unbind, so no .release callback is needed. +*/ +static struct drm_driver fl2000_drm_driver = { + .driver_features =DRIVER_MODESET |DRIVER_GEM |DRIVER_ATOMIC ,
Sorted flags please.
drivers/gpu/drm/fl2000/fl2000_drm.c line 163
Quoted 7 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +7
-0+ .fops = &fl2000_drm_driver_fops, + +/* +* Paged shmem buffers: the framebuffer is only ever read by the CPU +* (fl2000_stream_compress), never DMA'd to the device, so it does not +* need to be physically contiguous. +*/
Please drop the comment. It's not the right place to document gem-shmem behavior.
drivers/gpu/drm/fl2000/fl2000_drm.c line 173
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+ .dumb_create = drm_gem_shmem_dumb_create, +/* +* Import with a mapping, not DRM_GEM_SHMEM_DRIVER_OPS' no-map import. +* Mapping the attachment makes the exporter pin the buffer where this +* device can reach it, system memory for amdgpu. Left unmapped, amdgpu +* keeps it in VRAM and vmaps it as one linear range from where it +* thinks it starts, which for a buffer split over several VRAM blocks +* is right for the last block only: at 1360x768 and 1366x768 all but +* the bottom ~10 % of the desktop came out black or garbled. +*/
Also please drop. It's not the right place to document this.
Quoted 105 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +105
-0+ .gem_prime_import = drm_gem_prime_import, + .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, +DRM_FBDEV_SHMEM_DRIVER_OPS , + + .name = DRM_DRIVER_NAME, + .desc = DRM_DRIVER_DESC, + .major = DRM_DRIVER_MAJOR, + .minor = DRM_DRIVER_MINOR, + .patchlevel = DRM_DRIVER_PATCHLEVEL, +}; + +static const struct drm_mode_config_funcs fl2000_mode_config_funcs = { + .fb_create = drm_gem_fb_create_with_dirty, + .atomic_check = drm_atomic_helper_check, + .atomic_commit = drm_atomic_helper_commit, +}; + +/* Integer division compute of ppm error */ +static u64 fl2000_pll_ppm_err (u64 clock_mil,u32 vco_clk,u32 divisor) +{ +u64 pll_clk_mil =div_u64 ((u64 )vco_clk *FL2000_PLL_PRECISION , divisor); +u64 pll_clk_err; + +/* Not using abs() here to avoid a possible overflow */ +if (pll_clk_mil > clock_mil) + pll_clk_err = pll_clk_mil - clock_mil; +else + pll_clk_err = clock_mil - pll_clk_mil; + +return div64_u64 (pll_clk_err,div_u64 (clock_mil,FL2000_PLL_PRECISION )); +} + +static inline u32 fl2000_pll_get_divisor (u64 clock_mil,u32 vco_clk,u64 *min_ppm_err) +{ +static const u32 divisor_arr[] = { +2 ,4 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 , +20 ,21 ,22 ,23 ,24 ,25 ,26 ,27 ,28 ,29 ,30 ,31 ,32 ,33 ,34 ,35 , +36 ,37 ,38 ,39 ,40 ,41 ,42 ,43 ,44 ,45 ,46 ,47 ,48 ,49 ,50 ,51 , +52 ,53 ,54 ,55 ,56 ,57 ,58 ,59 ,60 ,61 ,62 ,63 ,64 ,65 ,66 ,67 , +68 ,69 ,70 ,71 ,72 ,73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 ,83 , +84 ,85 ,86 ,87 ,88 ,89 ,90 ,91 ,92 ,93 ,94 ,95 ,96 ,97 ,98 ,99 , +100 ,101 ,102 ,103 ,104 ,105 ,106 ,107 ,108 ,109 ,110 ,111 ,112 ,113 ,114 ,115 , +116 ,117 ,118 ,119 ,120 ,121 ,122 ,123 ,124 ,125 ,126 ,127 ,128 + }; +u32 best_divisor =0 ; + +for (int i =0 ; i <ARRAY_SIZE (divisor_arr); i++) { +u32 divisor = divisor_arr[i]; +u64 ppm_err =fl2000_pll_ppm_err (clock_mil, vco_clk, divisor); + +if (ppm_err < *min_ppm_err) { + *min_ppm_err = ppm_err; + best_divisor = divisor; + } + } + +return best_divisor; +} + +/* Try to match pixel clock - find parameters with minimal PLL error */ +static u64 fl2000_pll_calc (u64 clock_mil,struct fl2000_pll *pll,u32 *clock_calculated) +{ +static const u32 prescaler_max =2 ; +static const u32 multiplier_max =128 ; +u64 min_ppm_err = (u64)(-1 ); + +for (u32 prescaler =1 ; prescaler <= prescaler_max; prescaler++) +for (u32 multiplier =1 ; multiplier <= multiplier_max; multiplier++) { +/* Do not need precision here yet, no 10^6 multiply */ +u32 vco_clk =FL2000_XTAL / prescaler * multiplier; +u32 divisor; + +if (vco_clk <FL2000_VCOCLOCK_MIN || vco_clk >FL2000_VCOCLOCK_MAX ) +continue ; + + divisor =fl2000_pll_get_divisor (clock_mil, vco_clk, &min_ppm_err); +if (divisor ==0 ) +continue ; + + pll->prescaler = prescaler; + pll->multiplier = multiplier; + pll->divisor = divisor; +if (vco_clk <125000000 ) + pll->function =0 ; +else if (vco_clk <250000000 ) + pll->function =1 ; +else if (vco_clk <500000000 ) + pll->function =2 ; +else + pll->function =3 ; + *clock_calculated = vco_clk / divisor; + } + +return min_ppm_err; +} + +/* +* Find the PLL setting for a mode, accepting a pixel clock off by up to @max_ppm parts per +* million. The timings of the mode are never touched, only the clock the PLL can make for it. +*/ +static int fl2000_mode_calc (const struct drm_display_mode *mode, +struct drm_display_mode *adjusted_mode,struct fl2000_pll *pll, +u64 max_ppm) +{ +u32 clock_calculated;
drivers/gpu/drm/fl2000/fl2000_drm.c line 288
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+u64 ppm_err; + +if (mode->clock *1000 >FL2000_MAX_PIXCLOCK ) +return -EINVAL ; + +/* To keep precision use clock multiplied by 10^6 */ + ppm_err =fl2000_pll_calc ((u64 )mode->clock *1000 *FL2000_PLL_PRECISION , pll, + &clock_calculated); +if (ppm_err >= max_ppm) +return -EINVAL ;
Rather return MODE_* status codes here and forward them in mode_valid below...
drivers/gpu/drm/fl2000/fl2000_drm.c line 293
Quoted 5 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +5
-0+ +if (adjusted_mode) { +drm_mode_copy (adjusted_mode, mode); + adjusted_mode->clock = clock_calculated /1000 ; + }
... or just inline it into the respective calls.
Quoted 7 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +7
-0+ +return 0 ; +} + +static enum drm_mode_status fl2000_crtc_mode_valid (struct drm_crtc *crtc, +const struct drm_display_mode *mode) +{
drivers/gpu/drm/fl2000/fl2000_drm.c line 310
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+struct fl2000_drm_if *drm_if =to_fl2000_drm_if (crtc->dev); +struct drm_display_mode adjusted_mode; +struct fl2000_pll pll; + +/* Get PLL configuration and check if mode adjustments are needed */ +if (fl2000_mode_calc (mode, &adjusted_mode, &pll,FL2000_PPM_ERR_MAX_VGA )) +return MODE_BAD ; + +if (fl2000_get_bytes_pix (drm_if->usb_dev->speed, &adjusted_mode) ==0 ) +return MODE_CLOCK_HIGH ;
CRTC's mode_valid should only validate against the limits of the given CRTC. This call seems to validate something with pixel formats. That's at least questionable.
Quoted 14 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +14
-0+ +return MODE_OK ; +} + +/* +* Primary plane +*/ + +static const u64 fl2000_plane_format_modifiers[] = { +DRM_FORMAT_MOD_LINEAR , +DRM_FORMAT_MOD_INVALID , +}; + +static int fl2000_plane_atomic_check (struct drm_plane *plane,struct drm_atomic_commit *state)
drivers/gpu/drm/fl2000/fl2000_drm.c line 334
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+{ +struct drm_plane_state *new_plane_state =drm_atomic_get_new_plane_state (state, plane); +struct drm_crtc_state *new_crtc_state =NULL ; +bool is_cursor = plane->type ==DRM_PLANE_TYPE_CURSOR ; + +/* Clipping a larger sprite would put a truncated pointer on screen */ +if (is_cursor && new_plane_state->fb && + (new_plane_state->fb->width >FL2000_CURSOR_MAX || + new_plane_state->fb->height >FL2000_CURSOR_MAX )) +return -EINVAL ;
We check this limit when we create the framebuffer. Please don't do this here unless the plane has different limits.
drivers/gpu/drm/fl2000/fl2000_drm.c line 342
Quoted 8 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +8
-0+ +if (new_plane_state->crtc) + new_crtc_state =drm_atomic_get_new_crtc_state (state, new_plane_state->crtc); + +/* The cursor may be placed anywhere; the primary plane is the frame */ +return drm_atomic_helper_check_plane_state (new_plane_state, new_crtc_state, +DRM_PLANE_NO_SCALING ,DRM_PLANE_NO_SCALING , + is_cursor, false);
This block should always go first in plane atomic check.
Quoted 115 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +115
-0+} + +/* +* Only the damaged rectangle is converted, into the stream's shadow frame, +* which is the one persistent copy every outgoing buffer is built from. +*/ +static void fl2000_plane_atomic_update (struct drm_plane *plane,struct drm_atomic_commit *state) +{ +struct drm_plane_state *old_plane_state =drm_atomic_get_old_plane_state (state, plane); +struct drm_plane_state *plane_state =drm_atomic_get_new_plane_state (state, plane); +struct drm_shadow_plane_state *shadow_plane_state =to_drm_shadow_plane_state (plane_state); +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (plane->dev); +struct drm_framebuffer *fb = plane_state->fb; +struct drm_rect damage; +ktime_t start; +int idx; + +if (!fb) +return ;/* plane is disabled */ + +if (!drm_atomic_helper_damage_merged (old_plane_state, plane_state, &damage)) +return ; + +if (drm_gem_fb_begin_cpu_access (fb,DMA_FROM_DEVICE )) +return ; + +if (drm_dev_enter (plane->dev, &idx)) { + start =ktime_get (); +fl2000_stream_compress (drm_if->stream, &shadow_plane_state->data[0 ], +drm_gem_is_imported (fb->obj[0 ]), fb->format->format, + fb->width, fb->height, fb->pitches[0 ], &damage); +fl2000_stream_note_render (drm_if->stream, + (unsigned int )ktime_us_delta (ktime_get (), start), +drm_rect_width (&damage) *drm_rect_height (&damage)); +drm_dev_exit (idx); + } + +drm_gem_fb_end_cpu_access (fb,DMA_FROM_DEVICE ); +} + +/* +* Hand the cursor sprite to the stream, which blends it into every outgoing +* frame. Moving the pointer keeps the same framebuffer and skips the copy. +*/ +static void fl2000_cursor_atomic_update (struct drm_plane *plane,struct drm_atomic_commit *state) +{ +struct drm_plane_state *old_plane_state =drm_atomic_get_old_plane_state (state, plane); +struct drm_plane_state *plane_state =drm_atomic_get_new_plane_state (state, plane); +struct drm_shadow_plane_state *shadow_plane_state =to_drm_shadow_plane_state (plane_state); +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (plane->dev); +struct drm_framebuffer *fb = plane_state->fb; +unsigned int w, h; +u32 *sprite; +int idx; + +if (!fb || !plane_state->visible) { +fl2000_stream_set_cursor (drm_if->stream,NULL ,0 ,0 ,0 ,0 ); +return ; + } + +if (fb == old_plane_state->fb && old_plane_state->visible) { +fl2000_stream_move_cursor (drm_if->stream, plane_state->crtc_x, plane_state->crtc_y); +return ; + } + +/* Size is bounded by the atomic check */ + w = fb->width; + h = fb->height; + +/* Repack into a tightly packed sprite: the framebuffer has its own pitch */ + sprite =kmalloc_objs (*sprite, (size_t )w * h); +if (!sprite) +return ; + +if (drm_gem_fb_begin_cpu_access (fb,DMA_FROM_DEVICE )) + goto out_free; + +if (drm_dev_enter (plane->dev, &idx)) { +for (unsigned int y =0 ; y < h; y++) +iosys_map_memcpy_from (sprite + (size_t )y * w, &shadow_plane_state->data[0 ], + (size_t )y * fb->pitches[0 ], (size_t )w *sizeof (u32)); + +fl2000_stream_set_cursor (drm_if->stream, sprite, w, h, plane_state->crtc_x, + plane_state->crtc_y); +drm_dev_exit (idx); + } + +drm_gem_fb_end_cpu_access (fb,DMA_FROM_DEVICE ); +out_free: +kfree (sprite); +} + +static const struct drm_plane_helper_funcs fl2000_cursor_helper_funcs = { +DRM_GEM_SHADOW_PLANE_HELPER_FUNCS , + .atomic_check = fl2000_plane_atomic_check, + .atomic_update = fl2000_cursor_atomic_update, +}; + +static const struct drm_plane_helper_funcs fl2000_plane_helper_funcs = { +DRM_GEM_SHADOW_PLANE_HELPER_FUNCS , + .atomic_check = fl2000_plane_atomic_check, + .atomic_update = fl2000_plane_atomic_update, +}; + +static const struct drm_plane_funcs fl2000_plane_funcs = { + .update_plane = drm_atomic_helper_update_plane, + .disable_plane = drm_atomic_helper_disable_plane, + .destroy = drm_plane_cleanup, +DRM_GEM_SHADOW_PLANE_FUNCS , +}; + +/* +* CRTC +*/ +
drivers/gpu/drm/fl2000/fl2000_drm.c line 467
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+static int fl2000_crtc_atomic_check (struct drm_crtc *crtc,struct drm_atomic_commit *state) +{ +struct drm_crtc_state *crtc_state =drm_atomic_get_new_crtc_state (state, crtc); +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (crtc->dev); +struct fl2000_pll pll; +int ret; + + ret =drm_crtc_helper_atomic_check (crtc, state); +if (ret) +return ret;
Please open-code this function here. It's meant to be a drop-in implementation for the atomic_check callback.
drivers/gpu/drm/fl2000/fl2000_drm.c line 476
Quoted 9 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +9
-0+ +if (!crtc_state->enable || !drm_atomic_crtc_needs_modeset (crtc_state)) +return 0 ; + +/* PLL configuration, with htotal and clock adjusted to what the PLL can produce */ + ret =fl2000_mode_calc (&crtc_state->mode, &crtc_state->adjusted_mode, &pll, +FL2000_PPM_ERR_MAX_VGA ); +if (ret) +return ret;
Fixing up display modes goes into the mode_fixup callback. That's where you can adjust the PLL.
drivers/gpu/drm/fl2000/fl2000_drm.c line 480
Quoted 4 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +4
-0+ +/* The USB link must be able to carry the adjusted pixel clock */ +if (!fl2000_get_bytes_pix (drm_if->usb_dev->speed, &crtc_state->adjusted_mode)) +return -EINVAL ;
Same here.
Quoted 24 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +24
-0+ +return 0 ; +} + +/* +* HDMI has always run with the monitor sense forced, so it stays so whenever HDMI is driven. +* Driving VGA alone leaves it to the chip, which then powers the DAC by itself and notices the +* cable being unplugged. +*/ +static void fl2000_crtc_force_connect (struct fl2000_drm_if *drm_if, +const struct drm_crtc_state *crtc_state) +{ +fl2000_force_connect (drm_if->usb_dev, + crtc_state->encoder_mask &drm_encoder_mask (&drm_if->encoder)); +} + +/* Runs before the bridge is enabled: the IT66121 is programmed with the FL2000 clocking it */ +static void fl2000_crtc_mode_set_nofb (struct drm_crtc *crtc) +{ +struct drm_device *drm = crtc->dev; +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (drm); +struct usb_device *usb_dev = drm_if->usb_dev; +const struct drm_display_mode *mode = &crtc->state->mode; +const struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
drivers/gpu/drm/fl2000/fl2000_drm.c line 514
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+struct fl2000_timings timings; +struct fl2000_pll pll; +unsigned int bytes_pix; + +/* +* Same computation as in atomic_check, which already validated the mode +* and stored the adjusted htotal and clock in adjusted_mode. +*/ +if (WARN_ON (fl2000_mode_calc (mode,NULL , &pll,FL2000_PPM_ERR_MAX_VGA ))) +return ;
If you use mode_fixup, state->adjusted_mode will already have the correctly adjusted mode.
drivers/gpu/drm/fl2000/fl2000_drm.c line 515
Quoted 1 line
drivers/gpu/drm/fl2000/fl2000_drm.c new file +1
-0+ bytes_pix =fl2000_get_bytes_pix (usb_dev->speed, adjusted_mode);
CRTC should not mess with the pixel format.
If the hardware cannot handle a certain pixel format at a resolution, please report this from the plane's atomic_check. Let the compositor figure it out. DRM drivers don't do these behind-the-back conversion; except for supporting XRGB8888.
drivers/gpu/drm/fl2000/fl2000_drm.c line 518
Quoted 3 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +3
-0+ +drm_dbg_kms (drm,"Mode requested: " DRM_MODE_FMT "\n" ,DRM_MODE_ARG (mode)); +drm_dbg_kms (drm,"Mode configured: " DRM_MODE_FMT "\n" ,DRM_MODE_ARG (adjusted_mode));
Drivers should always use drm_dbg(). The others are severed for DRM core and helpers.
Quoted 20 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +20
-0+ +/* Prepare timing configuration */ + timings.hactive = adjusted_mode->hdisplay; + timings.htotal = adjusted_mode->htotal; + timings.hsync_width = adjusted_mode->hsync_end - adjusted_mode->hsync_start; + timings.hstart = adjusted_mode->htotal - adjusted_mode->hsync_start +1 ; + timings.vactive = adjusted_mode->vdisplay; + timings.vtotal = adjusted_mode->vtotal; + timings.vsync_width = adjusted_mode->vsync_end - adjusted_mode->vsync_start; + timings.vstart = adjusted_mode->vtotal - adjusted_mode->vsync_start +1 ; + +/* Set PLL settings */ +fl2000_set_pll (usb_dev, &pll); + +fl2000_crtc_force_connect (drm_if, crtc->state); + +/* Reset FL2000 and confirm PLL settings */ +fl2000_reset (usb_dev); + +/* Set timings settings */
drivers/gpu/drm/fl2000/fl2000_drm.c line 548
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+fl2000_set_timings (usb_dev, &timings); + +/* Pixel format according to number of bytes per pixel */ +fl2000_set_pixfmt (usb_dev, bytes_pix); + +/* Configure frame transfers */ +fl2000_set_transfers (usb_dev); + +/* Enable interrupts */ +fl2000_enable_interrupts (usb_dev);
This helper enable various interrupts unconditionally. That's likely not correct. Here you should only enable those that the CRTC need for running.
If you have vblank interrupts, enable them in ->enable_vblank; disable them in ->disable_vblank.
If your hardware has display sensing, the interrupt must remain enabled all the time so that you detect display hotplugs.
Quoted 6 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +6
-0+ +fl2000_afe_magic (usb_dev); + +fl2000_stream_mode_set (drm_if->stream, mode->hdisplay, mode->vdisplay, bytes_pix); +} +
drivers/gpu/drm/fl2000/fl2000_drm.c line 564
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+static void fl2000_crtc_atomic_enable (struct drm_crtc *crtc,struct drm_atomic_commit *state) +{ +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (crtc->dev); + +/* mode_set_nofb does not run when only the connector changed, and disable cleared it */ +fl2000_crtc_force_connect (drm_if,drm_atomic_get_new_crtc_state (state, crtc)); + +fl2000_stream_enable (drm_if->stream); + +drm_crtc_vblank_atomic_enable (crtc, state);
Please open-code this function. It's a drop-in implementation for atomic_enable
drivers/gpu/drm/fl2000/fl2000_drm.c line 574
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+} + +static void fl2000_crtc_atomic_disable (struct drm_crtc *crtc,struct drm_atomic_commit *state) +{ +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (crtc->dev); + +fl2000_stream_disable (drm_if->stream); + +/* Nothing is shown any more: let the chip's own sense report the monitors again */ +fl2000_force_connect (drm_if->usb_dev, false);
I'm not quite sure what this does. It looks like it's code for the encoder/connector.
Quoted 14 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +14
-0+ +drm_crtc_vblank_atomic_disable (crtc, state); +} + +static const struct drm_crtc_helper_funcs fl2000_crtc_helper_funcs = { + .mode_valid = fl2000_crtc_mode_valid, + .atomic_check = fl2000_crtc_atomic_check, + .mode_set_nofb = fl2000_crtc_mode_set_nofb, + .atomic_flush = drm_crtc_vblank_atomic_flush, + .atomic_enable = fl2000_crtc_atomic_enable, + .atomic_disable = fl2000_crtc_atomic_disable, +}; + +static const struct drm_crtc_funcs fl2000_crtc_funcs = {
drivers/gpu/drm/fl2000/fl2000_drm.c line 598
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+ .atomic_create_state = drm_atomic_helper_crtc_create_state, + .set_config = drm_atomic_helper_set_config, + .page_flip = drm_atomic_helper_page_flip, + .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, + .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, +/* +* The FL2000 has no vblank interrupt: vblank events come from a timer +* running at the mode's refresh rate. +*/ + DRM_CRTC_VBLANK_TIMER_FUNCS,
Please remove this for now. Your driver will still work correctly. That also obsoletes the other vblank support code in the driver.
We currently only have vblank timers for virtual displays. We might reconsider using it for other drivers at some later time.
drivers/gpu/drm/fl2000/fl2000_drm.c line 610
Quoted 12 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +12
-0+}; + +/* +* Digital sinks are only offered the modes whose pixel clock the PLL hits closely: they expect the +* exact clock of the mode they advertised. The VGA output accepts a larger error. +*/ +static enum drm_mode_status fl2000_hdmi_encoder_mode_valid (struct drm_encoder *encoder, +const struct drm_display_mode *mode) +{ +struct fl2000_pll pll; + +return fl2000_mode_calc (mode,NULL , &pll,FL2000_PPM_ERR_MAX ) ?MODE_NOCLOCK :MODE_OK ;
If you have strict limits on what the encoder accepts, you can again use mode_fixup to adjust the frequency.
Quoted 14 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +14
-0+} + +static const struct drm_encoder_helper_funcs fl2000_hdmi_encoder_helper_funcs = { + .mode_valid = fl2000_hdmi_encoder_mode_valid, +}; + +/** +* fl2000_drm_hotplug() - tell userspace that a monitor came, went or changed +* @drm: DRM device +* @events: the sink event bits of the status register that were seen +* +* Called from the interrupt work. +*/ +void fl2000_drm_hotplug (struct drm_device *drm,u32 events)
drivers/gpu/drm/fl2000/fl2000_drm.c line 634
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+{ +struct fl2000_drm_if *drm_if =to_fl2000_drm_if (drm); +int idx; + +/* The device sends uevents only once it has been registered */ +if (!smp_load_acquire (&drm_if->registered) || !drm_dev_enter (drm, &idx)) +return ; + +if (events & (FL2000_VGA_STATUS_MONITOR_EVENT |FL2000_VGA_STATUS_EDID_EVENT )) +fl2000_vga_sink_event (&drm_if->vga);
That's the kind of EDID tracking that should usually not be done by the driver.
Quoted 34 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +34
-0+ +drm_kms_helper_hotplug_event (drm); + +drm_dev_exit (idx); +} + +static void fl2000_drm_if_release (struct device *dev,void *res) +{ +struct fl2000_drm_if *drm_if = *(struct fl2000_drm_if **)res; +struct drm_device *drm = &drm_if->drm; +struct usb_device *usb_dev = drm_if->usb_dev; + +dev_dbg (dev,"Unbinding FL2000 master\n" ); + +/* No hotplug events for a node that is about to disappear */ +fl2000_intr_quiesce (drm_if->intr); +drm_kms_helper_poll_fini (drm); + +/* +* Block userspace access and shut down the display pipeline while all +* the components are still bound. +*/ +drm_dev_unplug (drm); +drm_atomic_helper_shutdown (drm); + +/* +* Detach bridge component. This release function runs with the global +* component mutex held (component_master_del() -> master unbind -> +* devres_release()), so it must never call component_del() - that would +* self-deadlock. The IT66121 is destroyed by a usb_device devres instead. +*/ +component_unbind_all (dev, drm); + +/* Stop streaming interface */
drivers/gpu/drm/fl2000/fl2000_drm.c line 678
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+fl2000_stream_destroy (usb_dev); + +/* Stop interrupts interface */ +fl2000_intr_destroy (usb_dev); + +/* +* Drop the driver's reference. The DRM device is freed when the last +* userspace file is closed; it only pins the USB device, never the I2C +* adapter, so the disconnect does not wait for userspace. +*/
Please drop all these comment here. It's not the right place to document all this.
Quoted 79 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +79
-0+drm_dev_put (drm); +} + +/* The DRM device while the master is bound, NULL otherwise */ +static struct fl2000_drm_if *fl2000_drm_if_of (struct device *master) +{ +struct fl2000_drm_if **holder =devres_find (master, fl2000_drm_if_release,NULL ,NULL ); + +return holder ? *holder :NULL ; +} + +/** +* fl2000_drm_suspend() - stop the display for system sleep +* @master: the component master +* +* Called once for the device, before any of its interfaces is suspended. +* +* Return: 0 on success, a negative error code otherwise, which aborts the sleep +*/ +int fl2000_drm_suspend (struct device *master) +{ +struct fl2000_drm_if *drm_if =fl2000_drm_if_of (master); +struct drm_bridge *bridge; +int ret; + +if (!drm_if) +return 0 ; + +/* No hotplug events while going to sleep */ +fl2000_intr_suspend (drm_if->intr); + +/* Saves the state, turns the outputs off and stops the stream */ + ret =drm_mode_config_helper_suspend (&drm_if->drm); +if (ret) { +fl2000_intr_resume (drm_if->intr); +return ret; + } + + bridge =drm_bridge_chain_get_first_bridge (&drm_if->encoder); +if (bridge) { +it66121_suspend (bridge); +drm_bridge_put (bridge); + } + +return 0 ; +} + +/** +* fl2000_drm_resume() - bring the display back after system sleep +* @master: the component master +* +* Called once for the device, when all of it is awake again, also after a reset-resume. The chips +* may have lost power: they are set up as at bind time before the saved state is restored. +* +* Return: 0 on success, a negative error code otherwise +*/ +int fl2000_drm_resume (struct device *master) +{ +struct fl2000_drm_if *drm_if =fl2000_drm_if_of (master); +struct usb_device *usb_dev; +struct drm_bridge *bridge; +int ret; + +if (!drm_if) +return 0 ; + usb_dev = drm_if->usb_dev; + +/* First, as the IT66121 is reached through the FL2000 I2C engine */ +fl2000_regmap_drop_cache (usb_dev); + + bridge =drm_bridge_chain_get_first_bridge (&drm_if->encoder); +if (bridge) { +it66121_resume (bridge); +drm_bridge_put (bridge); + } + +/* The same order as in fl2000_drm_bind() */ +fl2000_reset (usb_dev); +fl2000_usb_magic (usb_dev);
drivers/gpu/drm/fl2000/fl2000_drm.c line 767
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+ +/* Without interrupts the connectors are still polled */ +fl2000_intr_resume (drm_if->intr); + +/* Another monitor may have been plugged in meanwhile */ +fl2000_vga_sink_event (&drm_if->vga); + + ret =drm_mode_config_helper_resume (&drm_if->drm); +if (ret) +drm_err (&drm_if->drm,"Cannot restore the display after sleep (%d)\n" , ret);
I think it's better to resume mode setting first before resuming hardware. Otherwise the incoming interrupt might run on a suspended pipeline. (Not sure about the resume logic.)
Quoted 52 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +52
-0+ +drm_kms_helper_hotplug_event (&drm_if->drm); + +return ret; +} + +int fl2000_drm_bind (struct device *master) +{ +int ret =0 ; +struct usb_device *usb_dev =to_usb_device (master->parent); +struct fl2000_drm_if *drm_if; +struct fl2000_drm_if **holder; +struct drm_device *drm; +struct drm_mode_config *mode_config; +struct drm_connector_list_iter conn_iter; +struct drm_connector *connector; + +dev_dbg (master,"Binding FL2000 master\n" ); + +/* +* The DRM device is a child of the USB device, not of the I2C adapter +* that is the component master. It holds a reference on its parent, and +* i2c_del_adapter() (run when the USB device is unbound) waits for every +* reference on the adapter: parenting it there deadlocks the hub worker +* on unplug and no device enumerates again. The put is explicit, in +* fl2000_drm_if_release(). +*/ + drm_if =__drm_dev_alloc (&usb_dev->dev, &fl2000_drm_driver,sizeof (*drm_if), + offsetof(struct fl2000_drm_if , drm)); +if (IS_ERR (drm_if)) { +dev_err (master,"Cannot allocate DRM structure (%pe)\n" , drm_if); +return PTR_ERR (drm_if); + } + drm = &drm_if->drm; + drm_if->usb_dev = usb_dev; + +/* A USB device cannot DMA by itself: dma-buf imports go through its host controller */ +drm_dev_set_dma_dev (drm, usb_dev->bus->sysdev); + +/* +* Devres that fl2000_drm_unbind() releases to tear everything down, +* registered once binding succeeded. +*/ + holder =devres_alloc (fl2000_drm_if_release,sizeof (*holder),GFP_KERNEL ); +if (!holder) { +drm_dev_put (drm); +return -ENOMEM ; + } + + ret =drmm_mode_config_init (drm); +if (ret) { +drm_err (drm,"Cannot initialize DRM mode (%d)\n" , ret);
drivers/gpu/drm/fl2000/fl2000_drm.c line 828
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +9
-0+ goto err_free_holder; + } + + mode_config = &drm->mode_config; + mode_config->funcs = &fl2000_mode_config_funcs; + mode_config->min_width =1 ; + mode_config->max_width =FL2000_MAX_WIDTH ; + mode_config->min_height =1 ; + mode_config->max_height =FL2000_MAX_HEIGHT ;
These are the limits of the framebuffer. Setting 1 likely makes no sense. Just keep it to zero.
Quoted 17 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +17
-0+/* Userspace sizes its sprite to this; a larger one is rejected */ + mode_config->cursor_width =FL2000_CURSOR_MAX ; + mode_config->cursor_height =FL2000_CURSOR_MAX ; + + ret =drm_universal_plane_init (drm, &drm_if->plane,0 , &fl2000_plane_funcs, + fl2000_pixel_formats,ARRAY_SIZE (fl2000_pixel_formats), + fl2000_plane_format_modifiers,DRM_PLANE_TYPE_PRIMARY ,NULL ); +if (ret) { +drm_err (drm,"Cannot initialize plane (%d)\n" , ret); + goto err_free_holder; + } +drm_plane_helper_add (&drm_if->plane, &fl2000_plane_helper_funcs); + +/* +* Enable the damage-clips property the update path relies on +* (drm_atomic_helper_damage_merged()). +*/
drivers/gpu/drm/fl2000/fl2000_drm.c line 855
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+drm_plane_enable_fb_damage_clips (&drm_if->plane); + + ret =drm_universal_plane_init (drm, &drm_if->cursor,0 , &fl2000_plane_funcs, + fl2000_cursor_formats,ARRAY_SIZE (fl2000_cursor_formats), + fl2000_plane_format_modifiers,DRM_PLANE_TYPE_CURSOR ,NULL ); +if (ret) { +drm_err (drm,"Cannot initialize cursor plane (%d)\n" , ret); + goto err_free_holder; + } +drm_plane_helper_add (&drm_if->cursor, &fl2000_cursor_helper_funcs);
The cursor plane needs to go.
Quoted 25 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +25
-0+ + ret =drmm_crtc_init_with_planes (drm, &drm_if->crtc, &drm_if->plane, &drm_if->cursor, + &fl2000_crtc_funcs,NULL ); +if (ret) { +drm_err (drm,"Cannot initialize CRTC (%d)\n" , ret); + goto err_free_holder; + } +drm_crtc_helper_add (&drm_if->crtc, &fl2000_crtc_helper_funcs); + drm_if->plane.possible_crtcs =drm_crtc_mask (&drm_if->crtc); + drm_if->cursor.possible_crtcs =drm_crtc_mask (&drm_if->crtc); + +/* The FL2000 is programmed from the CRTC; the IT66121 bridge hangs off this encoder */ + ret =drmm_encoder_init (drm, &drm_if->encoder,NULL ,DRM_MODE_ENCODER_NONE ,NULL ); +if (ret) { +drm_err (drm,"Cannot initialize encoder (%d)\n" , ret); + goto err_free_holder; + } + drm_if->encoder.possible_crtcs =drm_crtc_mask (&drm_if->crtc); +drm_encoder_helper_add (&drm_if->encoder, &fl2000_hdmi_encoder_helper_funcs); + +/* Start streaming interface */ + drm_if->stream =fl2000_stream_create (usb_dev); +if (IS_ERR (drm_if->stream)) { + ret =PTR_ERR (drm_if->stream); +drm_err (drm,"Cannot create stream (%d)\n" , ret);
drivers/gpu/drm/fl2000/fl2000_drm.c line 890
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+ goto err_free_holder; + } + +/* Start interrupts interface */ + drm_if->intr =fl2000_intr_create (usb_dev, drm); +if (IS_ERR (drm_if->intr)) { + ret =PTR_ERR (drm_if->intr); +drm_err (drm,"Cannot start interrupt polling (%d)\n" , ret); + goto err_destroy_stream; + }
Once that is up, do you receive interrupts? If so, it would better go after drm_mode_config_reset().
Quoted 23 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +23
-0+ +/* Attach bridge */ + ret =component_bind_all (master, drm); +if (ret) { +drm_err (drm,"Cannot attach bridge (%d)\n" , ret); + goto err_destroy_intr; + } + +/* +* The HDMI connector is the generic one of the bridge chain: the IT66121 attached without +* a connector of its own and reports the hot plug line and the EDID. +*/ + connector =drm_bridge_connector_init (drm, &drm_if->encoder); +if (IS_ERR (connector)) { + ret =PTR_ERR (connector); +drm_err (drm,"Cannot initialize HDMI connector (%d)\n" , ret); + goto err_unbind; + } + +/* +* The VGA output goes in after the bridge: the IT66121 attaches to the first encoder of +* the device, which has to be the HDMI one. +*/
drivers/gpu/drm/fl2000/fl2000_drm.c line 923
Quoted 10 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +10
-0+ ret =fl2000_vga_init (drm, &drm_if->vga, &drm_if->crtc, &drm_if->encoder, +i2c_verify_adapter (master), drm_if->intr); +if (ret) { +drm_err (drm,"Cannot initialize VGA output (%d)\n" , ret); + goto err_unbind; + } + +drm_mode_config_reset (drm); + + ret =drm_vblank_init (drm, drm->mode_config.num_crtc);
Not needed without vblank timers.
Best regards
Thomas
Quoted 732 lines
drivers/gpu/drm/fl2000/fl2000_drm.c new file +63
-0+if (ret) { +drm_err (drm,"Failed to initialize %d VBLANK(s) (%d)\n" , + drm->mode_config.num_crtc, ret); + goto err_unbind; + } + +fl2000_reset (usb_dev); +fl2000_usb_magic (usb_dev); + +/* +* Probe the connector before userspace can see the device. A compositor +* that finds a new GPU reads the cached connector state without probing +* it, and does not look again when the hotplug event follows; with an +* unprobed connector it never enables the output until something else +* makes it reload. +*/ +mutex_lock (&drm->mode_config.mutex); +drm_connector_list_iter_begin (drm, &conn_iter); +drm_for_each_connector_iter (connector, &conn_iter) +drm_helper_probe_single_connector_modes (connector,FL2000_MAX_WIDTH , +FL2000_MAX_HEIGHT ); +drm_connector_list_iter_end (&conn_iter); +mutex_unlock (&drm->mode_config.mutex); + + ret =drm_dev_register (drm,0 ); +if (ret) { +drm_err (drm,"Cannot register DRM device (%d)\n" , ret); + goto err_unbind; + } + + *holder = drm_if; +devres_add (master, holder); + +/* From here on the interrupt work may tell userspace about monitors */ +smp_store_release (&drm_if->registered, true); + +/* Polls both connectors: events can be missed, and HDMI has no hot plug of its own */ +drm_kms_helper_poll_init (drm); + +/* +* fbdev emulation, so the console can use the display. Last, because it +* may set a mode right away and the reset above would undo it. +*/ +drm_client_setup (drm,NULL ); + +return 0 ; + +err_unbind: +component_unbind_all (master, drm); +err_destroy_intr: +fl2000_intr_destroy (usb_dev); +err_destroy_stream: +fl2000_stream_destroy (usb_dev); +err_free_holder: +devres_free (holder); +drm_dev_put (drm); +return ret; +} + +void fl2000_drm_unbind (struct device *master) +{ +devres_release (master, fl2000_drm_if_release,NULL ,NULL ); +}drivers/gpu/drm/fl2000/fl2000_drv.c +82
-11@@ -15,6 +15,9 @@#define USB_VENDOR_FRESCO_LOGIC 0x1D5C#define USB_PRODUCT_FL2000 0x2000 +#define FL2000_ALL_IFS \ + (BIT(FL2000_USBIF_AVCONTROL) | BIT(FL2000_USBIF_STREAMING) | BIT(FL2000_USBIF_INTERRUPT)) +static const struct usb_device_id fl2000_id_table[] = { {USB_DEVICE_INTERFACE_CLASS (USB_VENDOR_FRESCO_LOGIC ,USB_PRODUCT_FL2000 ,USB_CLASS_AV ) }, {},@@ -26,9 +29,41 @@ struct fl2000_devs {struct regmap *regmap;struct i2c_adapter *adapter;struct i2c_client *bridge_client; +struct component_match *match;int active_if; +/* The display was stopped for system sleep and has not been restarted yet */ +bool suspended; +}; + +static const struct component_master_ops fl2000_master_ops = { + .bind = fl2000_drm_bind, + .unbind = fl2000_drm_unbind, }; +static int fl2000_compare (struct device *dev,void *data) +{ +struct i2c_client *client =i2c_verify_client (dev); +struct i2c_adapter *adapter = data; +static const char *const fl2000_supported_bridges[] = { +"fl2000-it66121" , + }; + +/* +* Only match clients sitting on this FL2000 instance's own I2C bus: a +* client with a matching name may belong to another FL2000 device that +* is disconnecting or re-enumerating concurrently. +*/ +if (!client || client->adapter != adapter) +return 0 ; + +/* Check this is a supported DRM bridge */ +for (int i =0 ; i <ARRAY_SIZE (fl2000_supported_bridges); i++) +if (!strncmp (fl2000_supported_bridges[i], client->name,sizeof (client->name))) +return 1 ; + +return 0 ; +} +static struct fl2000_devs *fl2000_get_devices (struct usb_device *usb_dev) {struct fl2000_devs *devs;@@ -49,6 +84,8 @@ static struct fl2000_devs *fl2000_get_devices(struct usb_device *usb_dev)return ERR_CAST (devs->adapter); } +component_match_add (&devs->adapter->dev, &devs->match, fl2000_compare, devs->adapter); +dev_set_drvdata (&usb_dev->dev, devs);return devs;@@ -138,6 +175,16 @@ static int fl2000_probe(struct usb_interface *interface, const struct usb_devicebreak ; } +/* When all interfaces are up - proceed with registration */ +if (devs->active_if ==FL2000_ALL_IFS ) { + ret =component_master_add_with_match (&devs->adapter->dev, &fl2000_master_ops, + devs->match); +if (ret) { +dev_err (&usb_dev->dev,"Cannot register component master (%d)\n" , ret); +return ret; + } + } +return ret; }@@ -151,11 +198,18 @@ static void fl2000_disconnect(struct usb_interface *interface)return ;/* -* The IT66121 bridge is intentionally NOT destroyed here: disconnect -* also runs for the transient unbind/rebind cycles of -* usb_reset_device(), where the usb_device survives. Its destruction is -* a devres of the usb_device, see fl2000_bridge_create(). +* The IT66121 bridge component is intentionally NOT destroyed here. +* Disconnect also runs for the transient unbind/rebind cycles of +* usb_reset_device(), where the usb_device (and the I2C adapter and +* aggregate match data) survive: the component must persist across +* those. Its destruction is a devres of the usb_device, see +* fl2000_bridge_create(). Destroying it from the master unbind path is +* also forbidden - component_del() would self-deadlock on the global +* component mutex held across the unbind callback. */ +if (devs->active_if ==FL2000_ALL_IFS ) +component_master_del (&devs->adapter->dev, &fl2000_master_ops); +switch (iface_num) {case FL2000_USBIF_AVCONTROL :case FL2000_USBIF_STREAMING :@@ -170,15 +224,27 @@ static void fl2000_disconnect(struct usb_interface *interface) } } +/* +* System sleep. The USB core suspends the interfaces one by one, the last first, and resumes them +* in order once the device is awake. The display is one thing for all three: stop it at the first +* suspend, before any interface sleeps, and start it again at the first resume. The same callback +* serves a reset-resume, after which the chips are set up from scratch: without one the USB core +* would unbind and bind the driver again, which a running compositor sees as an unplug. +*/ static int fl2000_suspend (struct usb_interface *interface,pm_message_t message) {struct usb_device *usb_dev =interface_to_usbdev (interface); +struct fl2000_devs *devs =dev_get_drvdata (&usb_dev->dev); +int ret; -/* -* Nothing to save yet. Providing the callback keeps the USB core from -* unbinding the driver across system sleep. -*/ -dev_dbg (&usb_dev->dev,"suspend\n" ); +if (!devs || devs->suspended) +return 0 ; + + ret =fl2000_drm_suspend (&devs->adapter->dev); +if (ret) +return ret; + + devs->suspended = true;return 0 ; }@@ -186,10 +252,14 @@ static int fl2000_suspend(struct usb_interface *interface, pm_message_t message)static int fl2000_resume (struct usb_interface *interface) {struct usb_device *usb_dev =interface_to_usbdev (interface); +struct fl2000_devs *devs =dev_get_drvdata (&usb_dev->dev); -dev_dbg (&usb_dev->dev,"resume\n" ); +if (!devs || !devs->suspended) +return 0 ; -return 0 ; + devs->suspended = false; + +return fl2000_drm_resume (&devs->adapter->dev); }static struct usb_driver fl2000_driver = {@@ -198,6 +268,7 @@ static struct usb_driver fl2000_driver = { .disconnect = fl2000_disconnect, .suspend = fl2000_suspend, .resume = fl2000_resume, + .reset_resume = fl2000_resume, .id_table = fl2000_id_table, .supports_autosuspend = false, .disable_hub_initiated_lpm = true,drivers/gpu/drm/fl2000/fl2000_interrupt.c new file +237
-0@@ -0,0 +1,237 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* (C) Copyright 2017, Fresco Logic, Incorporated. +* (C) Copyright 2018-2020, Artem Mygaiev +*/ + +#include "fl2000.h" + +#define INTR_BUFSIZE 1 + +struct fl2000_intr { +struct usb_device *usb_dev; +struct drm_device *drm; +u8 poll_interval; +struct urb *urb; +u8 *buf; +dma_addr_t transfer_dma; +struct work_struct work; +struct workqueue_struct *work_queue; +/* Sink events read by anyone, not yet reported to userspace */ +atomic_t events; +}; + +#define FL2000_SINK_EVENTS (FL2000_VGA_STATUS_HDMI_EVENT | FL2000_VGA_STATUS_MONITOR_EVENT | \ + FL2000_VGA_STATUS_EDID_EVENT) + +/* +* The event bits of the status register clear on read, and both the work below and the connector +* detection read it: keep the events from every read until the work reports them. +*/ +static int fl2000_intr_read_status (struct fl2000_intr *intr,u32 *status) +{ +int ret; + + ret =fl2000_read_status (intr->usb_dev, status); +if (!ret) +atomic_or (*status &FL2000_SINK_EVENTS , &intr->events); + +return ret; +} + +/** +* fl2000_intr_monitor_connected() - does the FL2000 sense a monitor on its monitor input +* @intr: interrupt context +* +* The chip powers the DAC and the PLL only while this is the case. On the boards with both +* outputs the input follows whichever monitor is connected, so it does not tell VGA from HDMI. +* +* Return: 1 if a monitor is sensed, 0 if not, a negative error code if the status cannot be read +*/ +int fl2000_intr_monitor_connected (struct fl2000_intr *intr) +{ +u32 status; +int ret; + + ret =fl2000_intr_read_status (intr, &status); +if (ret) +return ret; + +return !!(status &FL2000_VGA_STATUS_MONITOR_STATUS ); +} + +static void fl2000_intr_work (struct work_struct *work) +{ +u32 status, events; + struct fl2000_intr *intr = container_of(work, struct fl2000_intr, work); + +if (fl2000_intr_read_status (intr, &status)) +return ; + + events =atomic_xchg (&intr->events,0 ); +if (events) +fl2000_drm_hotplug (intr->drm, events); +} + +static void fl2000_intr_release (struct device *dev,void *res) +{ +struct fl2000_intr *intr = res; +struct usb_device *usb_dev =to_usb_device (dev); + +usb_poison_urb (intr->urb); +cancel_work_sync (&intr->work); +if (intr->work_queue) +destroy_workqueue (intr->work_queue); +usb_free_coherent (usb_dev,INTR_BUFSIZE , intr->buf, intr->transfer_dma); +usb_free_urb (intr->urb); +} + +static void fl2000_intr_completion (struct urb *urb) +{ +int ret; +struct usb_device *usb_dev = urb->dev; +struct fl2000_intr *intr = urb->context; + + ret =fl2000_urb_status (usb_dev, urb->status); +if (ret) { +/* Killed on purpose (suspend, unplug) is no reason to complain */ +if (ret != -ENOENT && ret != -ECONNRESET ) +dev_err (&usb_dev->dev,"Stopping interrupts\n" ); +return ; + } + +/* Processing reads registers over USB, which sleeps: defer it to a work item */ +queue_work (intr->work_queue, &intr->work); + +/* +* For interrupt URBs, as part of successful URB submission urb->interval is modified to +* reflect the actual transfer period used, so we need to restore it. +*/ + urb->interval = intr->poll_interval; + urb->start_frame =-1 ; + +/* Restart urb */ + ret =usb_submit_urb (urb,GFP_ATOMIC ); +if (ret) +dev_err (&usb_dev->dev,"URB submission failed (%d)\n" , ret); +} + +/** +* fl2000_intr_create() - interrupt processing context creation +* @usb_dev: FL2000 USB device +* @drm: DRM device that receives the hotplug events +* +* Called on DRM master bind. Starts polling the interrupt endpoint. +* +* Return: interrupt context, or an ERR_PTR() on failure +*/ +struct fl2000_intr *fl2000_intr_create (struct usb_device *usb_dev,struct drm_device *drm) +{ +int ret; +struct fl2000_intr *intr; +struct usb_endpoint_descriptor *desc; +struct usb_interface *interface =usb_ifnum_to_if (usb_dev,FL2000_USBIF_INTERRUPT ); + +/* +* There's only one altsetting (#0) and one endpoint (#3) in the interrupt interface (#2) +* but let's try and "find" it anyway. +*/ + ret =usb_find_int_in_endpoint (interface->cur_altsetting, &desc); +if (ret) { +dev_err (&usb_dev->dev,"Cannot find interrupt endpoint\n" ); +return ERR_PTR (ret); + } + + intr =devres_alloc (&fl2000_intr_release,sizeof (*intr),GFP_KERNEL ); +if (!intr) +return ERR_PTR (-ENOMEM ); +devres_add (&usb_dev->dev, intr); + + intr->poll_interval = desc->bInterval; + intr->usb_dev = usb_dev; + intr->drm = drm; +INIT_WORK (&intr->work, &fl2000_intr_work); +atomic_set (&intr->events,0 ); + + intr->urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!intr->urb) { +devres_release (&usb_dev->dev, fl2000_intr_release,NULL ,NULL ); +return ERR_PTR (-ENOMEM ); + } + + intr->buf =usb_alloc_coherent (usb_dev,INTR_BUFSIZE ,GFP_KERNEL , &intr->transfer_dma); +if (!intr->buf) { +devres_release (&usb_dev->dev, fl2000_intr_release,NULL ,NULL ); +return ERR_PTR (-ENOMEM ); + } + + intr->work_queue =alloc_ordered_workqueue ("fl2000_interrupt" ,0 ); +if (!intr->work_queue) { +devres_release (&usb_dev->dev, fl2000_intr_release,NULL ,NULL ); +return ERR_PTR (-ENOMEM ); + } + +/* Interrupt URB configuration is static, including allocated buffer */ +usb_fill_int_urb (intr->urb, usb_dev,usb_rcvintpipe (usb_dev,3 ), intr->buf,INTR_BUFSIZE , + fl2000_intr_completion, intr, intr->poll_interval); + intr->urb->transfer_dma = intr->transfer_dma; + intr->urb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; + +/* Start checking for interrupts */ + ret =usb_submit_urb (intr->urb,GFP_KERNEL ); +if (ret) { +dev_err (&usb_dev->dev,"URB submission failed (%d)\n" , ret); +devres_release (&usb_dev->dev, fl2000_intr_release,NULL ,NULL ); +return ERR_PTR (ret); + } + +return intr; +} + +/** +* fl2000_intr_quiesce() - stop reporting hotplug events +* @intr: interrupt context +* +* Called before the DRM device is unplugged: an event raised after that would +* announce a change on a node that was just removed, and probing the connector +* would talk to hardware that is already gone. +*/ +void fl2000_intr_quiesce (struct fl2000_intr *intr) +{ +usb_poison_urb (intr->urb); +cancel_work_sync (&intr->work); +} + +/** +* fl2000_intr_suspend() - stop polling the interrupt endpoint for system sleep +* @intr: interrupt context +* +* Unlike fl2000_intr_quiesce(), the URB can be submitted again by fl2000_intr_resume(). +*/ +void fl2000_intr_suspend (struct fl2000_intr *intr) +{ +usb_kill_urb (intr->urb); +cancel_work_sync (&intr->work); +} + +/** +* fl2000_intr_resume() - poll the interrupt endpoint again after system sleep +* @intr: interrupt context +* +* Return: 0 on success, the URB submission error otherwise +*/ +int fl2000_intr_resume (struct fl2000_intr *intr) +{ +int ret; + + ret =usb_submit_urb (intr->urb,GFP_KERNEL ); +if (ret) +dev_err (&intr->usb_dev->dev,"URB submission failed (%d)\n" , ret); + +return ret; +} + +void fl2000_intr_destroy (struct usb_device *usb_dev) +{ +devres_release (&usb_dev->dev, fl2000_intr_release,NULL ,NULL ); +}drivers/gpu/drm/fl2000/fl2000_vga.c new file +256
-0@@ -0,0 +1,256 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* VGA output of the FL2000: the analog DAC of the chip, on an encoder and a connector of its own +* next to the HDMI ones. All of them share the one CRTC, so a compositor can use either output +* alone, and a dual-output adapter can show the same frame on both ("clone mode"). +*/ + +#include <drm/drm_atomic_state_helper.h> +#include <drm/drm_drv.h> +#include <drm/drm_edid.h> +#include <drm/drm_managed.h> +#include <drm/drm_modes.h> + +#include "fl2000.h" +#include "fl2000_it66121.h" + +/* +* Highest pixel clock offered on VGA, kHz. The DAC has no documented limit; 1920x1080 at 60 Hz is +* what the vendor advertises for it. +*/ +#define FL2000_VGA_MAX_CLOCK 165000 + +/* The first probes of a connection retry the EDID, in case the monitor is still waking up */ +#define FL2000_VGA_EDID_TRIES 3 + +/* Offered as the preferred mode when there is no EDID: any VGA monitor takes it */ +#define FL2000_VGA_FALLBACK_WIDTH 1024 +#define FL2000_VGA_FALLBACK_HEIGHT 768 + +/* Largest mode listed from the standard timings when there is no EDID */ +#define FL2000_VGA_NOEDID_WIDTH 1920 +#define FL2000_VGA_NOEDID_HEIGHT 1200 + +static inline struct fl2000_vga *to_fl2000_vga (struct drm_connector *connector) +{ + return container_of(connector, struct fl2000_vga, connector); +} + +static int fl2000_vga_read_block (void *context,u8 *buf,unsigned int block,size_t len) +{ +struct fl2000_vga *vga = context; + +return fl2000_ddc_read_block (vga->adapter, buf, block, len); +} + +/* +* The chip has one monitor sense input, and on the adapters with both outputs it follows whichever +* monitor is plugged in. The HDMI transmitter has a hot plug line of its own, so a sensed monitor +* with no HDMI sink behind it is a VGA one. With both connected the HDMI sink wins and the VGA +* output reports nothing: it can still be forced on (video=VGA-1:e) to mirror the picture. +*/ +static enum drm_connector_status fl2000_vga_sense (struct fl2000_vga *vga) +{ +int ret =fl2000_intr_monitor_connected (vga->intr); + +if (ret <0 ) +return connector_status_unknown; +if (!ret) +return connector_status_disconnected; + +if (vga->hdmi_bridge) { +/* +* Not knowing whether there is an HDMI sink is not the same as there being none: +* two connected outputs on the one CRTC can leave a compositor with neither. +*/ + ret =it66121_hpd_connected (vga->hdmi_bridge); +if (ret <0 ) +return connector_status_unknown; +if (ret) +return connector_status_disconnected; + } + +return connector_status_connected; +} + +static int fl2000_vga_detect_ctx (struct drm_connector *connector, +struct drm_modeset_acquire_ctx *ctx,bool force) +{ +struct fl2000_vga *vga =to_fl2000_vga (connector); +enum drm_connector_status status; +int idx; + +if (!drm_dev_enter (connector->dev, &idx)) +return connector_status_disconnected; + + status =fl2000_vga_sense (vga); +if (status == connector_status_unknown) +/* An unreadable status says nothing: keep what userspace was told */ + status = connector->status; +else if (status != connector_status_connected) +/* Whatever comes next may be another monitor: its EDID must be read again */ +WRITE_ONCE (vga->edid_stale, true); + +drm_dev_exit (idx); + +return status; +} + +/* +* The EDID is read through the FL2000 I2C engine. It is kept for as long as the monitor stays, and +* only a few probes try for it: a read that finds nothing is quick, but old monitors often do not +* answer DDC at all, and every probe runs with the mode config locked. +*/ +static int fl2000_vga_get_modes (struct drm_connector *connector) +{ +struct fl2000_vga *vga =to_fl2000_vga (connector); +int count =0 , idx; + +if (!drm_dev_enter (connector->dev, &idx)) +return 0 ; + +if (READ_ONCE (vga->edid_stale)) { +WRITE_ONCE (vga->edid_stale, false); +drm_edid_free (vga->edid); + vga->edid =NULL ; + vga->edid_tries =0 ; + } + +if (!vga->edid && vga->edid_tries <FL2000_VGA_EDID_TRIES ) { + vga->edid_tries++; + vga->edid =drm_edid_read_custom (connector, fl2000_vga_read_block, vga); +drm_dbg_kms (connector->dev,"VGA EDID read %s (try %u)\n" , + vga->edid ?"ok" :"failed" , vga->edid_tries); + } + +drm_edid_connector_update (connector, vga->edid); +if (vga->edid) + count =drm_edid_connector_add_modes (connector); + +if (!count) { +/* +* No EDID, or nothing usable in it: offer the standard timings and let 1024x768 +* be the default. Another mode can be chosen from the desktop, or forced on the +* kernel command line (video=VGA-1:1366x768@60e), and an EDID file can replace +* the missing one (drm.edid_firmware=VGA-1:edid/monitor.bin). +*/ + count =drm_add_modes_noedid (connector,FL2000_VGA_NOEDID_WIDTH , +FL2000_VGA_NOEDID_HEIGHT ); +drm_set_preferred_mode (connector,FL2000_VGA_FALLBACK_WIDTH , +FL2000_VGA_FALLBACK_HEIGHT ); + } + +drm_dev_exit (idx); + +return count; +} + +static const struct drm_connector_helper_funcs fl2000_vga_connector_helper_funcs = { + .get_modes = fl2000_vga_get_modes, + .detect_ctx = fl2000_vga_detect_ctx, +}; + +static const struct drm_connector_funcs fl2000_vga_connector_funcs = { + .fill_modes = drm_helper_probe_single_connector_modes, + .atomic_create_state = drm_atomic_helper_connector_create_state, + .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, + .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, +}; + +/* +* Applies to the VGA output only, both when probing and in atomic commits. The PLL limits are +* checked by the CRTC. +*/ +static enum drm_mode_status fl2000_vga_encoder_mode_valid (struct drm_encoder *encoder, +const struct drm_display_mode *mode) +{ +if (mode->clock >FL2000_VGA_MAX_CLOCK ) +return MODE_CLOCK_HIGH ; + +return MODE_OK ; +} + +static const struct drm_encoder_helper_funcs fl2000_vga_encoder_helper_funcs = { + .mode_valid = fl2000_vga_encoder_mode_valid, +}; + +/** +* fl2000_vga_sink_event() - the chip reports that a monitor appeared, went away or changed EDID +* @vga: VGA output +* +* Called from the interrupt work. The EDID is read again by the next probe. +*/ +void fl2000_vga_sink_event (struct fl2000_vga *vga) +{ +WRITE_ONCE (vga->edid_stale, true); +} + +static void fl2000_vga_release (struct drm_device *drm,void *res) +{ +struct fl2000_vga *vga = res; + +drm_edid_free (vga->edid); +drm_bridge_put (vga->hdmi_bridge); +} + +/** +* fl2000_vga_init() - add the VGA encoder and connector +* @drm: DRM device +* @vga: VGA output to set up, part of the driver's device structure +* @crtc: the CRTC, which every output shares +* @hdmi_encoder: the encoder of the HDMI output, with its bridge already attached +* @adapter: the FL2000 I2C adapter, which carries the monitor's DDC +* @intr: interrupt context, to sense the monitor +* +* The HDMI bridge must be attached already, and nothing may be probed before this returns: the +* two encoders are marked as clones of each other, in both directions as the core requires. +* +* Return: 0 on success, a negative error code otherwise +*/ +int fl2000_vga_init (struct drm_device *drm,struct fl2000_vga *vga,struct drm_crtc *crtc, +struct drm_encoder *hdmi_encoder,struct i2c_adapter *adapter, +struct fl2000_intr *intr) +{ +u32 clones; +int ret; + + vga->adapter = adapter; + vga->intr = intr; + vga->hdmi_bridge =drm_bridge_chain_get_first_bridge (hdmi_encoder); + + ret =drmm_add_action_or_reset (drm, fl2000_vga_release, vga); +if (ret) +return ret; + + ret =drmm_encoder_init (drm, &vga->encoder,NULL ,DRM_MODE_ENCODER_DAC ,NULL ); +if (ret) +return ret; + vga->encoder.possible_crtcs =drm_crtc_mask (crtc); +drm_encoder_helper_add (&vga->encoder, &fl2000_vga_encoder_helper_funcs); + +/* +* No ddc adapter: its transfers carry a single byte, so the EDID is read with +* fl2000_ddc_read_block() instead of the DDC helpers. +*/ + ret =drmm_connector_init (drm, &vga->connector, &fl2000_vga_connector_funcs, +DRM_MODE_CONNECTOR_VGA ,NULL ); +if (ret) +return ret; +drm_connector_helper_add (&vga->connector, &fl2000_vga_connector_helper_funcs); + +/* +* The chip raises an event when the monitor comes or goes (fl2000_interrupt.c), but polling +* recovers from one that was missed, and it costs a status read every few seconds. +*/ + vga->connector.polled =DRM_CONNECTOR_POLL_CONNECT |DRM_CONNECTOR_POLL_DISCONNECT ; + + ret =drm_connector_attach_encoder (&vga->connector, &vga->encoder); +if (ret) +return ret; + + clones =drm_encoder_mask (hdmi_encoder) |drm_encoder_mask (&vga->encoder); + hdmi_encoder->possible_clones = clones; + vga->encoder.possible_clones = clones; + +return 0 ; +}
Signature · 6 lines
-- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
Re: [PATCH RFC 4/4] drm/fl2000: Add KMS display pipeline
Hi Thomas,
thank you for the review. I have applied all of it for v2 and tried it on the adapter. One change has a cost that you should know about before I send it, and the answers to your questions are below.
Quoted 1 line
I assume that XRGB2101010 is the hardware's native or preferred format?
It is not. The vendor's API has RGB888, RGB565 and RGB555, and the mode bits I know of in the chip's control register are for 565, 555 and 332. I have no datasheet, but nothing I have seen has a 10-bit mode. The driver accepted the 10-bit formats and reduced them to 8 bits, because Mutter renders in them.
v2 offers XRGB8888, converted to RGB888, and RGB565 as it is. I saw RGB565 work at 1280x720 on HDMI and at 1024x768 on VGA.
Dropping the 10-bit formats has a visible cost here. With an amdgpu laptop, kernel 7.2.11 and Mutter 51.1:
- With XRGB8888 and RGB565 only, gnome-shell logs "Zero-copy disabled for /dev/dri/card2, import failed: drmModeAddFB2 failed (Invalid argument) [...] format=0x30335241 (AR30)". mem_info_gtt_used went from 96 MB to 6251 of 6939 MB in under a minute on VGA at 1280x720, and was at 1922 MB two minutes after boot on HDMI at 1920x1080. On an earlier version of the driver, the same message was followed by a desktop stall after about 30 minutes ("bind failed" in amdgpu_dm_plane_helper_prepare_fb).
- With XRGB2101010 and ARGB2101010 accepted, there is no such message, and GTT stays between 107 and 134 MB over 45 minutes.
This is Mutter's: issue #5101 reports its copy path leaking a framebuffer per frame on a DisplayLink output. Its main branch already releases that framebuffer, since 15 October, but no release has it yet, as 51.1 was tagged on 12 October. So I plan to send v2 without these formats, as you asked, and GNOME users will have this until the next Mutter release. Tell me if you see it differently.
Quoted 1 line
How large a value could you write into the registers?
The register definitions I inherited from Artem Mygaiev's driver have 12-bit active and total fields, so 4095, and v2 takes its limits from them instead of the number I had made up. I don't have the manual, and reading back does not confirm it: these registers return all 32 bits as written.
Quoted 2 lines
Once that is up, do you receive interrupts? If so, it would better go after
drm_mode_config_reset().
Yes. v2 only starts polling the interrupt endpoint after drm_dev_register(), which also lets the state you asked to drop go away.
Quoted 2 lines
I'm not quite sure what this does. It looks like it's code for the encoder/connector.
It makes the chip believe that a monitor is connected to its analog sense input, whatever the input says. The driver keeps it set while HDMI is driven, and clear on VGA, where it would hide the cable being unplugged. The vendor driver only sets it while programming the HDMI transmitter, and I have not tried that yet. It is in the encoder in v2.
Quoted 3 lines
I think it's better to resume mode setting first before resuming hardware. Otherwise the incoming interrupt might run on a suspended pipeline. (Not sure about the resume logic.)
v2 starts the interrupts after drm_mode_config_helper_resume(), so no event runs on a suspended pipeline. The chips are still set up before
it: the adapter may have lost power, the register caches have to be dropped, and restoring the state writes to those registers. Tell me if you would rather see it done differently.
Best regards,
Angel