Skip to content

Commit

Permalink
HID: core: move Usage Page concatenation to Main item
Browse files Browse the repository at this point in the history
[ Upstream commit 58e75155009cc800005629955d3482f36a1e0eec ]

As seen on some USB wireless keyboards manufactured by Primax, the HID
parser was using some assumptions that are not always true. In this case
it's s the fact that, inside the scope of a main item, an Usage Page
will always precede an Usage.

The spec is not pretty clear as 6.2.2.7 states "Any usage that follows
is interpreted as a Usage ID and concatenated with the Usage Page".
While 6.2.2.8 states "When the parser encounters a main item it
concatenates the last declared Usage Page with a Usage to form a
complete usage value." Being somewhat contradictory it was decided to
match Window's implementation, which follows 6.2.2.8.

In summary, the patch moves the Usage Page concatenation from the local
item parsing function to the main item parsing function.

Change-Id: Icefeed2a2b29c5d8a5488617af325d100ba9fbdb
Signed-off-by: Nicolas Saenz Julienne <[email protected]>
Reviewed-by: Terry Junge <[email protected]>
Signed-off-by: Benjamin Tissoires <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
Git-commit: 69f6720
Git-repo: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
[[email protected]: resolved trivial conflict in drivers/hid/hid-core.c]
Signed-off-by: Ivaylo Georgiev <[email protected]>
  • Loading branch information
Nicolas Saenz Julienne authored and Ivaylo Georgiev committed Jan 28, 2020
1 parent 5fd5fbe commit 4504b6c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
36 changes: 24 additions & 12 deletions drivers/hid/hid-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,14 @@ static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
* Add a usage to the temporary parser table.
*/

static int hid_add_usage(struct hid_parser *parser, unsigned int usage)
static int hid_add_usage(struct hid_parser *parser, unsigned usage, u8 size)
{
if (parser->local.usage_index >= HID_MAX_USAGES) {
hid_err(parser->device, "usage index exceeded\n");
return -1;
}
parser->local.usage[parser->local.usage_index] = usage;
parser->local.usage_size[parser->local.usage_index] = size;
parser->local.collection_index[parser->local.usage_index] =
parser->collection_stack_ptr ?
parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
Expand Down Expand Up @@ -482,10 +483,7 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
return 0;
}

if (item->size <= 2)
data = (parser->global.usage_page << 16) + data;

return hid_add_usage(parser, data);
return hid_add_usage(parser, data, item->size);

case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:

Expand All @@ -494,9 +492,6 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
return 0;
}

if (item->size <= 2)
data = (parser->global.usage_page << 16) + data;

parser->local.usage_minimum = data;
return 0;

Expand All @@ -507,9 +502,6 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
return 0;
}

if (item->size <= 2)
data = (parser->global.usage_page << 16) + data;

count = data - parser->local.usage_minimum;
if (count + parser->local.usage_index >= HID_MAX_USAGES) {
/*
Expand All @@ -529,7 +521,7 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
}

for (n = parser->local.usage_minimum; n <= data; n++)
if (hid_add_usage(parser, n)) {
if (hid_add_usage(parser, n, item->size)) {
dbg_hid("hid_add_usage failed\n");
return -1;
}
Expand All @@ -543,6 +535,22 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
return 0;
}

/*
* Concatenate Usage Pages into Usages where relevant:
* As per specification, 6.2.2.8: "When the parser encounters a main item it
* concatenates the last declared Usage Page with a Usage to form a complete
* usage value."
*/

static void hid_concatenate_usage_page(struct hid_parser *parser)
{
int i;

for (i = 0; i < parser->local.usage_index; i++)
if (parser->local.usage_size[i] <= 2)
parser->local.usage[i] += parser->global.usage_page << 16;
}

/*
* Process a main item.
*/
Expand All @@ -552,6 +560,8 @@ static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
__u32 data;
int ret;

hid_concatenate_usage_page(parser);

data = item_udata(item);

switch (item->tag) {
Expand Down Expand Up @@ -761,6 +771,8 @@ static int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
__u32 data;
int i;

hid_concatenate_usage_page(parser);

data = item_udata(item);

switch (item->tag) {
Expand Down
1 change: 1 addition & 0 deletions include/linux/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ struct hid_global {

struct hid_local {
unsigned usage[HID_MAX_USAGES]; /* usage array */
u8 usage_size[HID_MAX_USAGES]; /* usage size array */
unsigned collection_index[HID_MAX_USAGES]; /* collection index array */
unsigned usage_index;
unsigned usage_minimum;
Expand Down

0 comments on commit 4504b6c

Please sign in to comment.