Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp736866ybl; Wed, 4 Dec 2019 10:02:36 -0800 (PST) X-Google-Smtp-Source: APXvYqx9TPfS9+jbJCpdbRVi1/nxW2DyCghQJe9jam7a/rzDxTMUqVdoNdT3osD0MTZRpqORh0Yg X-Received: by 2002:a9d:7a4d:: with SMTP id z13mr237474otm.148.1575482556282; Wed, 04 Dec 2019 10:02:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575482556; cv=none; d=google.com; s=arc-20160816; b=odFuz8wlVNnUDUsxI1DFkCBlnPT+YqufW9fkRh1+WUtybk+DuCF4zPhB9N97VhftSx rNdkCjaatXoVumpTHhPMK4aBW6jsy4Weoh8nujXW5Zuw/mby8llyCcXE/KElMbcYc3Jx oskfHchA81zxlqVxcspZZdX+7gAvmekPBwHbmCa8RZb8xho/ROCY0HSUEDVFIFbR/Bli wPC0XF6XfsdQ9FfRk4t6UQ6Ux/lmYtpI2DNe/snX7wb/dcKnuMiBywJxLw4Vf87CXUkc Ay0FH3b2xOL3UYpbVnw8A1z6ilqiBCnSkFuQGDjOViV9g+cv7YQvHJMGSPMZX0HJM8gp zE6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=b3J3vn4nfCs8Nuv15uk0OagW42kHfMtfHITy/4cuzHU=; b=WgyBTk64kfx0e7D7wYliSkGhbGJGfnRoQ+cUmy476vEsRzaT03hr76gVknCTY5SF7+ PSNve3eq1Enus8FvNabFMKtJyJk2AXAfUe/QAFofnt3kliXVlrSgqEdqEpj1h0Hwd0vb BBeVzd2AKah2fvrjWaBry5Qw6ZbW3eOzpg6D4yr81hTKtGi0kEKvRKAuHbLAKkxUiEPY 0dca60ppezTrjn9ndVs78Nu2uvH6hqFK1qqtSyElCxRADRKMXkdRHsh4oRPYm3hnDsOZ 80tzylYQewjWdqQ8mSpONfhQXMnvj6iygvTNqe02D7yzdtfj/FyosE5mZDUaTcRxahB/ jHfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gl6TfCyx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g25si3722725otp.20.2019.12.04.10.02.22; Wed, 04 Dec 2019 10:02:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gl6TfCyx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729482AbfLDSBC (ORCPT + 99 others); Wed, 4 Dec 2019 13:01:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:40636 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729472AbfLDSBB (ORCPT ); Wed, 4 Dec 2019 13:01:01 -0500 Received: from localhost (unknown [217.68.49.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A1DE720833; Wed, 4 Dec 2019 18:00:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575482460; bh=eTLW9auAY7BPKUpSarCQ9h6zUJRvDEvNCM/3/FLyHYA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gl6TfCyxixtCs7AwuN7Y7pwOkwR6SsKQRhQTz19Y90PYHopSKhE6uraoBxxZ/kuOx Cd+0HdwjO81PjcbuwJ0T4uWnBHVgj/yzicvZ+IuogCdsxYK4WaxsoGY7O5FiqxXY+r dis0L/FPIvfEarXSdY3czI1AsGa8nvu3h12r9lh8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Candle Sun , Nianfu Bai , Benjamin Tissoires , Jiri Kosina , Siarhei Vishniakou Subject: [PATCH 4.4 90/92] HID: core: check whether Usage Page item is after Usage ID items Date: Wed, 4 Dec 2019 18:50:30 +0100 Message-Id: <20191204174335.562787874@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191204174327.215426506@linuxfoundation.org> References: <20191204174327.215426506@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Candle Sun commit 1cb0d2aee26335d0bccf29100c7bed00ebece851 upstream. Upstream commit 58e75155009c ("HID: core: move Usage Page concatenation to Main item") adds support for Usage Page item after Usage ID items (such as keyboards manufactured by Primax). Usage Page concatenation in Main item works well for following report descriptor patterns: USAGE_PAGE (Keyboard) 05 07 USAGE_MINIMUM (Keyboard LeftControl) 19 E0 USAGE_MAXIMUM (Keyboard Right GUI) 29 E7 LOGICAL_MINIMUM (0) 15 00 LOGICAL_MAXIMUM (1) 25 01 REPORT_SIZE (1) 75 01 REPORT_COUNT (8) 95 08 INPUT (Data,Var,Abs) 81 02 ------------- USAGE_MINIMUM (Keyboard LeftControl) 19 E0 USAGE_MAXIMUM (Keyboard Right GUI) 29 E7 LOGICAL_MINIMUM (0) 15 00 LOGICAL_MAXIMUM (1) 25 01 REPORT_SIZE (1) 75 01 REPORT_COUNT (8) 95 08 USAGE_PAGE (Keyboard) 05 07 INPUT (Data,Var,Abs) 81 02 But it makes the parser act wrong for the following report descriptor pattern(such as some Gamepads): USAGE_PAGE (Button) 05 09 USAGE (Button 1) 09 01 USAGE (Button 2) 09 02 USAGE (Button 4) 09 04 USAGE (Button 5) 09 05 USAGE (Button 7) 09 07 USAGE (Button 8) 09 08 USAGE (Button 14) 09 0E USAGE (Button 15) 09 0F USAGE (Button 13) 09 0D USAGE_PAGE (Consumer Devices) 05 0C USAGE (Back) 0a 24 02 USAGE (HomePage) 0a 23 02 LOGICAL_MINIMUM (0) 15 00 LOGICAL_MAXIMUM (1) 25 01 REPORT_SIZE (1) 75 01 REPORT_COUNT (11) 95 0B INPUT (Data,Var,Abs) 81 02 With Usage Page concatenation in Main item, parser recognizes all the 11 Usages as consumer keys, it is not the HID device's real intention. This patch checks whether Usage Page is really defined after Usage ID items by comparing usage page using status. Usage Page concatenation on currently defined Usage Page will always do in local parsing when Usage ID items encountered. When Main item is parsing, concatenation will do again with last defined Usage Page if this page has not been used in the previous usages concatenation. Signed-off-by: Candle Sun Signed-off-by: Nianfu Bai Cc: Benjamin Tissoires Signed-off-by: Jiri Kosina Cc: Siarhei Vishniakou Signed-off-by: Greg Kroah-Hartman --- drivers/hid/hid-core.c | 51 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -197,6 +197,18 @@ static unsigned hid_lookup_collection(st } /* + * Concatenate usage which defines 16 bits or less with the + * currently defined usage page to form a 32 bit usage + */ + +static void complete_usage(struct hid_parser *parser, unsigned int index) +{ + parser->local.usage[index] &= 0xFFFF; + parser->local.usage[index] |= + (parser->global.usage_page & 0xFFFF) << 16; +} + +/* * Add a usage to the temporary parser table. */ @@ -207,6 +219,14 @@ static int hid_add_usage(struct hid_pars return -1; } parser->local.usage[parser->local.usage_index] = usage; + + /* + * If Usage item only includes usage id, concatenate it with + * currently defined usage page + */ + if (size <= 2) + complete_usage(parser, parser->local.usage_index); + parser->local.usage_size[parser->local.usage_index] = size; parser->local.collection_index[parser->local.usage_index] = parser->collection_stack_ptr ? @@ -523,13 +543,32 @@ static int hid_parser_local(struct hid_p * usage value." */ -static void hid_concatenate_usage_page(struct hid_parser *parser) +static void hid_concatenate_last_usage_page(struct hid_parser *parser) { int i; + unsigned int usage_page; + unsigned int current_page; + + if (!parser->local.usage_index) + return; - 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; + usage_page = parser->global.usage_page; + + /* + * Concatenate usage page again only if last declared Usage Page + * has not been already used in previous usages concatenation + */ + for (i = parser->local.usage_index - 1; i >= 0; i--) { + if (parser->local.usage_size[i] > 2) + /* Ignore extended usages */ + continue; + + current_page = parser->local.usage[i] >> 16; + if (current_page == usage_page) + break; + + complete_usage(parser, i); + } } /* @@ -541,7 +580,7 @@ static int hid_parser_main(struct hid_pa __u32 data; int ret; - hid_concatenate_usage_page(parser); + hid_concatenate_last_usage_page(parser); data = item_udata(item); @@ -756,7 +795,7 @@ static int hid_scan_main(struct hid_pars __u32 data; int i; - hid_concatenate_usage_page(parser); + hid_concatenate_last_usage_page(parser); data = item_udata(item);