From 4f4ab4bcd5de770b3ed0e00a6dfeac548b2c8340 Mon Sep 17 00:00:00 2001 From: Jason Gerecke Date: Tue, 10 Dec 2024 13:00:58 -0800 Subject: HID: wacom: Improve behavior of non-standard LED brightness values Assigning a non-standard brightness value to an LED can cause the value to slowly drift downward over time as the effects of integer division accumulate. Each time that an LED is triggered, a series of set and get calls occur. For example, if we assume a tablet with max_hlv = 100, then when brightness is set to "200" through sysfs, the hlv value written to hardware will be `200*100/255 = 78`. If the LED trigger is later activated, the hlv value will be used to determine the brightness: `78*255/100 = 198`. This lower brightness then used to set the brightness of the next LED. However, `198*100/255 = 77`, so the next LED ends up slightly dimmer. Each subsequent trigger activation will cause the brightness to continue drifting down until we reach a point where the result of integer divsion does not introduce any new error. This commit corrects the issue by being more careful about how we handle scaling between the two ranges (0..max_{h,l}lv) and (0..LED_FULL). Signed-off-by: Jason Gerecke Signed-off-by: Jiri Kosina --- drivers/hid/wacom.h | 8 ++++++++ drivers/hid/wacom_sys.c | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/hid/wacom.h b/drivers/hid/wacom.h index 6f1443999d1d..1deacb4568cb 100644 --- a/drivers/hid/wacom.h +++ b/drivers/hid/wacom.h @@ -218,6 +218,14 @@ static inline __u32 wacom_s32tou(s32 value, __u8 n) return value & (1 << (n - 1)) ? value & (~(~0U << n)) : value; } +static inline u32 wacom_rescale(u32 value, u32 in_max, u32 out_max) +{ + if (in_max == 0 || out_max == 0) + return 0; + value = clamp(value, 0, in_max); + return DIV_ROUND_CLOSEST(value * out_max, in_max); +} + extern const struct hid_device_id wacom_ids[]; void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len); diff --git a/drivers/hid/wacom_sys.c b/drivers/hid/wacom_sys.c index 9843b52bd017..9652288c3571 100644 --- a/drivers/hid/wacom_sys.c +++ b/drivers/hid/wacom_sys.c @@ -1302,10 +1302,10 @@ enum led_brightness wacom_leds_brightness_get(struct wacom_led *led) struct wacom *wacom = led->wacom; if (wacom->led.max_hlv) - return led->hlv * LED_FULL / wacom->led.max_hlv; + return wacom_rescale(led->hlv, wacom->led.max_hlv, LED_FULL); if (wacom->led.max_llv) - return led->llv * LED_FULL / wacom->led.max_llv; + return wacom_rescale(led->llv, wacom->led.max_llv, LED_FULL); /* device doesn't support brightness tuning */ return LED_FULL; @@ -1337,8 +1337,8 @@ static int wacom_led_brightness_set(struct led_classdev *cdev, goto out; } - led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL; - led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL; + led->llv = wacom->led.llv = wacom_rescale(brightness, LED_FULL, wacom->led.max_llv); + led->hlv = wacom->led.hlv = wacom_rescale(brightness, LED_FULL, wacom->led.max_hlv); wacom->led.groups[led->group].select = led->id; -- cgit v1.2.3 From d2c342334141521cc5a0439bd66ff35da8bc5aad Mon Sep 17 00:00:00 2001 From: Jason Gerecke Date: Tue, 10 Dec 2024 13:00:59 -0800 Subject: HID: wacom: Status luminance properties should set brightness of all LEDs The wacom driver has (deprecated) sysfs properties `status0_luminance` and `status1_luminance` that are used to control the low- and high- level brightness values (llv and hlv) of the status LEDs. These two properties had an effect on /all/ of the status LEDs. After our driver switched to exposing each status LED individually through the LED class, this behavior changed. These controls started having only a temporary effect on the currently-lit LED. If a trigger changed the current LED, the driver would switch the brightness back to the llv/hlv values stored per-LED. (The code's current behavior of updating the "global" e.g. `wacom->led.llv` values has essentially no effect because those values are only used at initialization time). This commit restores the original behavior by ensuring these properties update the per-LED brightness for all LEDs. Signed-off-by: Jason Gerecke Signed-off-by: Jiri Kosina --- drivers/hid/wacom_sys.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/hid/wacom_sys.c b/drivers/hid/wacom_sys.c index 9652288c3571..c1cde342018d 100644 --- a/drivers/hid/wacom_sys.c +++ b/drivers/hid/wacom_sys.c @@ -1084,6 +1084,17 @@ static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest, mutex_lock(&wacom->lock); *dest = value & 0x7f; + for (unsigned int i = 0; i < wacom->led.count; i++) { + struct wacom_group_leds *group = &wacom->led.groups[i]; + + for (unsigned int j = 0; j < group->count; j++) { + if (dest == &wacom->led.llv) + group->leds[j].llv = *dest; + else if (dest == &wacom->led.hlv) + group->leds[j].hlv = *dest; + } + } + err = wacom_led_control(wacom); mutex_unlock(&wacom->lock); -- cgit v1.2.3 From c4c123504a65583e3689b3de04a61dc5272e453a Mon Sep 17 00:00:00 2001 From: Even Xu Date: Thu, 26 Dec 2024 09:35:27 +0800 Subject: HID: Wacom: Add PCI Wacom device support Add PCI device ID of wacom device into driver support list. Signed-off-by: Even Xu Tested-by: Tatsunosuke Tobita Reviewed-by: Ping Cheng Signed-off-by: Jiri Kosina --- drivers/hid/wacom_wac.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/hid/wacom_wac.c b/drivers/hid/wacom_wac.c index 5501a560fb07..b60bfafc6a8f 100644 --- a/drivers/hid/wacom_wac.c +++ b/drivers/hid/wacom_wac.c @@ -4946,6 +4946,10 @@ static const struct wacom_features wacom_features_0x94 = HID_DEVICE(BUS_I2C, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\ .driver_data = (kernel_ulong_t)&wacom_features_##prod +#define PCI_DEVICE_WACOM(prod) \ + HID_DEVICE(BUS_PCI, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\ + .driver_data = (kernel_ulong_t)&wacom_features_##prod + #define USB_DEVICE_LENOVO(prod) \ HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, prod), \ .driver_data = (kernel_ulong_t)&wacom_features_##prod @@ -5115,6 +5119,7 @@ const struct hid_device_id wacom_ids[] = { { USB_DEVICE_WACOM(HID_ANY_ID) }, { I2C_DEVICE_WACOM(HID_ANY_ID) }, + { PCI_DEVICE_WACOM(HID_ANY_ID) }, { BT_DEVICE_WACOM(HID_ANY_ID) }, { } }; -- cgit v1.2.3