diff mbox series

[1/3,hid] Minor cleanup

Message ID 20210519143836.1.I8e23cff04fc537877668462d98d5054bac10d940@changeid (mailing list archive)
State New, archived
Headers show
Series 64-bit Digitizer Serial Numbers | expand

Commit Message

Kenneth Albanowski May 20, 2021, 12:22 a.m. UTC
No functional changes, just cleanup.

Clarify actual integer size limits in a few comments.

Make one constant explicit to improve readability.

Replace open-coded sign-extension with common routine.

Signed-off-by: Kenneth Albanowski <kenalba@google.com>
---

 drivers/hid/hid-core.c | 19 +++++++------------
 include/linux/hid.h    |  3 ++-
 2 files changed, 9 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 5b30783e2353d..19b2b0aae0c7e 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -337,7 +337,8 @@  static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsign
 }
 
 /*
- * Read data value from item.
+ * Read data value from global items, which are
+ * a maximum of 32 bits in size.
  */
 
 static u32 item_udata(struct hid_item *item)
@@ -709,7 +710,7 @@  static void hid_device_release(struct device *dev)
 
 /*
  * Fetch a report description item from the data stream. We support long
- * items, though they are not used yet.
+ * items, though there are not yet any defined uses for them.
  */
 
 static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
@@ -745,6 +746,7 @@  static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
 	item->format = HID_ITEM_FORMAT_SHORT;
 	item->size = b & 3;
 
+	/* Map size values 0,1,2,3 to actual sizes 0,1,2,4 */
 	switch (item->size) {
 	case 0:
 		return start;
@@ -763,7 +765,7 @@  static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
 		return start;
 
 	case 3:
-		item->size++;
+		item->size = 4;
 		if ((end - start) < 4)
 			return NULL;
 		item->data.u32 = get_unaligned_le32(start);
@@ -1300,9 +1302,7 @@  int hid_open_report(struct hid_device *device)
 EXPORT_SYMBOL_GPL(hid_open_report);
 
 /*
- * Convert a signed n-bit integer to signed 32-bit integer. Common
- * cases are done through the compiler, the screwed things has to be
- * done by hand.
+ * Convert a signed n-bit integer to signed 32-bit integer.
  */
 
 static s32 snto32(__u32 value, unsigned n)
@@ -1310,12 +1310,7 @@  static s32 snto32(__u32 value, unsigned n)
 	if (!value || !n)
 		return 0;
 
-	switch (n) {
-	case 8:  return ((__s8)value);
-	case 16: return ((__s16)value);
-	case 32: return ((__s32)value);
-	}
-	return value & (1 << (n - 1)) ? value | (~0U << n) : value;
+	return sign_extend32(value, n - 1);
 }
 
 s32 hid_snto32(__u32 value, unsigned n)
diff --git a/include/linux/hid.h b/include/linux/hid.h
index 7a13e9d3441a4..59828a6080e83 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -391,6 +391,7 @@  struct hid_item {
 
 struct hid_global {
 	unsigned usage_page;
+	/* HID Global fields are constrained by spec to 32-bits */
 	__s32    logical_minimum;
 	__s32    logical_maximum;
 	__s32    physical_minimum;
@@ -457,7 +458,7 @@  struct hid_field {
 	unsigned  maxusage;		/* maximum usage index */
 	unsigned  flags;		/* main-item flags (i.e. volatile,array,constant) */
 	unsigned  report_offset;	/* bit offset in the report */
-	unsigned  report_size;		/* size of this field in the report */
+	unsigned  report_size;		/* size of this field in the report, in bits */
 	unsigned  report_count;		/* number of this field in the report */
 	unsigned  report_type;		/* (input,output,feature) */
 	__s32    *value;		/* last known value(s) */