HID: input: move up out-of-range processing of input values
authorBenjamin Tissoires <benjamin.tissoires@redhat.com>
Thu, 3 Feb 2022 14:32:20 +0000 (15:32 +0100)
committerJiri Kosina <jkosina@suse.cz>
Tue, 1 Mar 2022 14:46:02 +0000 (15:46 +0100)
It actually makes sense to clamp the value to its boundaries before
doing further processing.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Reviewed-by: Ping Cheng <ping.cheng@wacom.com>
Acked-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
drivers/hid/hid-input.c

index 54b3e9c..8770d9a 100644 (file)
@@ -1364,6 +1364,30 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
                return;
        }
 
+       /*
+        * Ignore out-of-range values as per HID specification,
+        * section 5.10 and 6.2.25, when NULL state bit is present.
+        * When it's not, clamp the value to match Microsoft's input
+        * driver as mentioned in "Required HID usages for digitizers":
+        * https://msdn.microsoft.com/en-us/library/windows/hardware/dn672278(v=vs.85).asp
+        *
+        * The logical_minimum < logical_maximum check is done so that we
+        * don't unintentionally discard values sent by devices which
+        * don't specify logical min and max.
+        */
+       if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
+           field->logical_minimum < field->logical_maximum) {
+               if (field->flags & HID_MAIN_ITEM_NULL_STATE &&
+                   (value < field->logical_minimum ||
+                    value > field->logical_maximum)) {
+                       dbg_hid("Ignoring out-of-range value %x\n", value);
+                       return;
+               }
+               value = clamp(value,
+                             field->logical_minimum,
+                             field->logical_maximum);
+       }
+
        switch (usage->hid) {
        case HID_DG_INVERT:
                *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
@@ -1432,30 +1456,6 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
        }
 
        /*
-        * Ignore out-of-range values as per HID specification,
-        * section 5.10 and 6.2.25, when NULL state bit is present.
-        * When it's not, clamp the value to match Microsoft's input
-        * driver as mentioned in "Required HID usages for digitizers":
-        * https://msdn.microsoft.com/en-us/library/windows/hardware/dn672278(v=vs.85).asp
-        *
-        * The logical_minimum < logical_maximum check is done so that we
-        * don't unintentionally discard values sent by devices which
-        * don't specify logical min and max.
-        */
-       if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
-           (field->logical_minimum < field->logical_maximum)) {
-               if (field->flags & HID_MAIN_ITEM_NULL_STATE &&
-                   (value < field->logical_minimum ||
-                    value > field->logical_maximum)) {
-                       dbg_hid("Ignoring out-of-range value %x\n", value);
-                       return;
-               }
-               value = clamp(value,
-                             field->logical_minimum,
-                             field->logical_maximum);
-       }
-
-       /*
         * Ignore reports for absolute data if the data didn't change. This is
         * not only an optimization but also fixes 'dead' key reports. Some
         * RollOver implementations for localized keys (like BACKSLASH/PIPE; HID