platform/x86: apple-gmux: Fix max_brightness limit check in gmux_probe()
PATCH v2 1/1 platform/x86: apple-gmux: Fix max_brightness limit check in gmux_probe()
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.
---
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/[email protected]/
drivers/platform/x86/apple-gmux.c | 45 ++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-)
drivers/platform/x86/apple-gmux.c +24 -21
@@ -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);