Skip to content

Commit d704c99

Browse files
Discard high nibble when decoding Unit Exponent (#17)
HidSharp assumes the high nibble in a unit exponent field is meaningful, while the HID Usage Tables 1.3 defines the unit exponent as a 4-bit value (Exponent 0 through F, page 302 / 303). Report Descriptors only support values in multitudes of 8 bits, so the first 4 bits are padding / meaningless. This means we should ignore the high nibble when decoding the report descriptor and not make any assumptions about its meaning. There are some devices which define the unit exponent as 0xF0 through 0xFF. Reconstructing the report descriptor fails on these devices. This is how the Linux kernel does it as well: https://github.com/torvalds/linux/blob/master/drivers/hid/hid-core.c#L427
1 parent c6e9365 commit d704c99

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

HidSharp/Reports/Units/Unit.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ public int GetExponent(UnitKind kind)
6363

6464
/// <summary>
6565
/// Decodes an encoded HID unit exponent.
66+
/// The HID Usage Table 1.3 states that valid values for the unit
67+
/// exponent field are 0x_0 - 0x_F so we ignore the high nibble.
6668
/// </summary>
6769
/// <param name="value">The encoded exponent.</param>
6870
/// <returns>The exponent.</returns>
6971
public static int DecodeExponent(uint value)
7072
{
71-
if (value > 15) { throw new ArgumentOutOfRangeException("value", "Value range is [0, 15]."); }
73+
value &= 0x0f;
7274
return value >= 8 ? (int)value - 16 : (int)value;
7375
}
7476

0 commit comments

Comments
 (0)