All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] platform/x86: apple-gmux: Fix max_brightness limit check in gmux_probe()
@ 2026-11-20 16:29 Thorsten Blum
  0 siblings, 0 replies; only message in thread
From: Thorsten Blum @ 2026-11-20 16:29 UTC (permalink / raw)
  To: Hans de Goede, Ilpo Järvinen, Seth Forshee, Matthew Garrett
  Cc: Thorsten Blum, platform-driver-x86

gmux_read32() returns a u32, but the maximum brightness is stored in the
signed ->max_brightness field. Large values can thus become negative and
bypass the GMUX_MAX_BRIGHTNESS check.

Store the maximum brightness in a local u32 variable until after the
bounds check, and then assign it to props.max_brightness. Use dev_warn()
instead of WARN_ON() to report an out-of-range hardware value.

Also move the backlight properties and maximum brightness calculation
into the device registration block, use a designated initializer for the
properties, and simplify the comments.

Fixes: 917ee75a5916 ("platform/x86: Add driver for Apple gmux device")
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
Changes in v2:
- Store the maximum brightness in a local u32 and assign it to
  props.max_brightness after the bounds check (Ilpo)
- Replace WARN_ON() with dev_warn() to report out-of-range values (Ilpo)
- Declare and initialize props in the device registration block
- Simplify the comments
- v1: https://lore.kernel.org/r/20261120142914.825047-2-blum@kernel.org/

 drivers/platform/x86/apple-gmux.c | 45 ++++++++++++++++---------------
 1 file changed, 24 insertions(+), 21 deletions(-)

diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c
index 9c728ac4e045..4f7ed3c563ce 100644
--- a/drivers/platform/x86/apple-gmux.c
+++ b/drivers/platform/x86/apple-gmux.c
@@ -781,7 +781,6 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
 {
 	struct apple_gmux_data *gmux_data;
 	struct resource *res;
-	struct backlight_properties props;
 	struct backlight_device *bdev = NULL;
 	u8 ver_major, ver_minor, ver_release;
 	bool register_bdev = true;
@@ -857,32 +856,36 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
 	pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor,
 		ver_release, gmux_data->config->name);
 
-	memset(&props, 0, sizeof(props));
-	props.type = BACKLIGHT_PLATFORM;
-
-	/*
-	 * All MMIO gmux's have 0xffff as max brightness, but some iMacs incorrectly
-	 * report 0x03ff, despite the firmware being happy to set 0xffff as the brightness
-	 * at boot. Force 0xffff for all MMIO gmux's so they all have the correct brightness
-	 * range.
-	 */
-	if (type == APPLE_GMUX_TYPE_MMIO)
-		props.max_brightness = MMIO_GMUX_MAX_BRIGHTNESS;
-	else
-		props.max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
-
 #if IS_REACHABLE(CONFIG_ACPI_VIDEO)
 	register_bdev = acpi_video_get_backlight_type() == acpi_backlight_apple_gmux;
 #endif
 	if (register_bdev) {
+		struct backlight_properties props = {
+			.type = BACKLIGHT_PLATFORM,
+		};
+		u32 max_brightness;
+
 		/*
-		 * Currently it's assumed that the maximum brightness is less than
-		 * 2^24 for compatibility with old gmux versions. Cap the max
-		 * brightness at this value, but print a warning if the hardware
-		 * reports something higher so that it can be fixed.
+		 * Some iMacs incorrectly report 0x03ff as the maximum
+		 * brightness even though the firmware accepts 0xffff at boot.
+		 * Use the full brightness range for all MMIO gmux devices.
 		 */
-		if (WARN_ON(props.max_brightness > GMUX_MAX_BRIGHTNESS))
-			props.max_brightness = GMUX_MAX_BRIGHTNESS;
+		if (type == APPLE_GMUX_TYPE_MMIO)
+			max_brightness = MMIO_GMUX_MAX_BRIGHTNESS;
+		else
+			max_brightness = gmux_read32(gmux_data, GMUX_PORT_MAX_BRIGHTNESS);
+
+		/*
+		 * Keep the maximum brightness below 2^24 for compatibility with
+		 * older gmux versions.
+		 */
+		if (max_brightness > GMUX_MAX_BRIGHTNESS) {
+			dev_warn(&pnp->dev,
+				 "Maximum brightness %#x exceeds limit, capping to %#x\n",
+				 max_brightness, GMUX_MAX_BRIGHTNESS);
+			max_brightness = GMUX_MAX_BRIGHTNESS;
+		}
+		props.max_brightness = max_brightness;
 
 		bdev = backlight_device_register("gmux_backlight", &pnp->dev,
 						 gmux_data, &gmux_bl_ops, &props);

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-20 16:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 16:29 [PATCH v2] platform/x86: apple-gmux: Fix max_brightness limit check in gmux_probe() Thorsten Blum

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.