Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1314001pxb; Tue, 8 Feb 2022 14:26:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJzrR7EIazzNAX9K6fHfNgjE1hbTEP8FeYZKC8lq3uStKqyhvI5aPTHv+z4SKJjKH8J+hpqI X-Received: by 2002:a17:902:e0c1:: with SMTP id e1mr6555655pla.18.1644359218875; Tue, 08 Feb 2022 14:26:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644359218; cv=none; d=google.com; s=arc-20160816; b=aZFo2lHXalcbSBv80U+1LxDlpWj1LBsJwxxYmP7KIpML+4rP2h+SFzWhn5fYRaHL7Y cHmIDIxPZPFg3/lqgCK40EjdCOvax50q+xeRsXUuFoILl6cmDmSBj2KcflHpYVVTp5hB YkJTf5eal+YKKVZ8bw6tT9pxIjNmjYFl0xFZb06Jlj8TonSrkgnOcUgkzNOrtQXzqQKL FytE63aTlrLOUXbaRPoG1YulIc4tJZXgWKlPQYYcBeghEuLL+kDvKHmtTM27Nxm7SIBT 1kbjXe8PculYaPe8rcLEtBhu3tJOJoHjpRnZn3UTfI1nsdEtlO6DMD2L+UK5hMwSm4iR qLHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=b5j0N0MXhPzEQU00nTmU5V9bnIUoEjM+0zli124kmSk=; b=UAZW/Wz/KB+KBE0EUDEEnglvQtKRoN4Ilx2eiLNam+7cbEQ/v6VCQXZJNSGz3DmiBw 6yYFzGV73jaDf/8gWNa+0zWOm7ILMoLSHSZg8EjfYcry5YLclxXD+8zRfB6ZyQShaA4L ryn/oQMcr3CiETXd7SKtpplw1Pv6Pisktjw8uuRoXEc3Dgvk4qDyxcvXT6kMYC09flhL 7Jc94YgMIx+oA9c+b/R09+dM6fQBJqrxFFY1QBlveVN5azghOOQ2YDFpeIOjiywwNLnZ Gga8Ik8AFD+yYu9JCF7F/tEX3h4g4fhprP7f0CTlnkxne8AJzGTHcjokzL2XMpvUp12T Vtlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WMUgyK8Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n3si13057115pff.351.2022.02.08.14.26.45; Tue, 08 Feb 2022 14:26:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WMUgyK8Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351302AbiBCOde (ORCPT + 99 others); Thu, 3 Feb 2022 09:33:34 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:26624 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351220AbiBCOdT (ORCPT ); Thu, 3 Feb 2022 09:33:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643898799; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b5j0N0MXhPzEQU00nTmU5V9bnIUoEjM+0zli124kmSk=; b=WMUgyK8Za2R1EB+AJynBvgbLLJuLuXGGdeecPS2i9CaBxQ4b//4+b40pUryEoDD6sPjCOP Lyst1C1CzCX7sKuonYF22knKOjHJfLs75DwbUAu9dzr7VSixaMMJUMBkoOmnZ1aeWfX5Fg 3xjuGR+IfQBzNw/FFkoqbj89TKgwWsc= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-520-sFs62tweOrq32XIQq9Jtng-1; Thu, 03 Feb 2022 09:33:14 -0500 X-MC-Unique: sFs62tweOrq32XIQq9Jtng-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AD64B18C89CF; Thu, 3 Feb 2022 14:33:11 +0000 (UTC) Received: from plouf.redhat.com (unknown [10.39.192.114]) by smtp.corp.redhat.com (Postfix) with ESMTP id 38E0C7D4D0; Thu, 3 Feb 2022 14:33:09 +0000 (UTC) From: Benjamin Tissoires To: Jiri Kosina , Dmitry Torokhov , Jonathan Corbet , =?UTF-8?q?Ahelenia=20Ziemia=C5=84ska?= , Ping Cheng , Aaron Armstrong Skomra , Jason Gerecke , Peter Hutterer Cc: linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Benjamin Tissoires Subject: [PATCH v2 07/12] HID: compute an ordered list of input fields to process Date: Thu, 3 Feb 2022 15:32:21 +0100 Message-Id: <20220203143226.4023622-8-benjamin.tissoires@redhat.com> In-Reply-To: <20220203143226.4023622-1-benjamin.tissoires@redhat.com> References: <20220203143226.4023622-1-benjamin.tissoires@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This will be used in a later commit: we build a list of input fields (and usage_index) that is ordered based on a usage priority. Changing the usage priority allows to re-order the processed list, meaning that we can enforce some usages to be process before others. For instance, before processing InRange in the HID tablets, we need to know if we are using the eraser (side or button). Enforcing a higher (lower number) priority for Invert allows to force the input stack to process that field before. Signed-off-by: Benjamin Tissoires --- drivers/hid/hid-core.c | 105 ++++++++++++++++++++++++++++++++++++++++- include/linux/hid.h | 10 ++++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 34188d7ac0f7..b38528118642 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -81,6 +81,7 @@ struct hid_report *hid_register_report(struct hid_device *device, report_enum->report_id_hash[id] = report; list_add_tail(&report->list, &report_enum->report_list); + INIT_LIST_HEAD(&report->field_entry_list); return report; } @@ -101,7 +102,7 @@ static struct hid_field *hid_register_field(struct hid_report *report, unsigned field = kzalloc((sizeof(struct hid_field) + usages * sizeof(struct hid_usage) + - 2 * usages * sizeof(unsigned int)), GFP_KERNEL); + 3 * usages * sizeof(unsigned int)), GFP_KERNEL); if (!field) return NULL; @@ -110,6 +111,7 @@ static struct hid_field *hid_register_field(struct hid_report *report, unsigned field->usage = (struct hid_usage *)(field + 1); field->value = (s32 *)(field->usage + usages); field->new_value = (s32 *)(field->value + usages); + field->usages_priorities = (s32 *)(field->new_value + usages); field->report = report; return field; @@ -657,6 +659,8 @@ static void hid_free_report(struct hid_report *report) { unsigned n; + kfree(report->field_entries); + for (n = 0; n < report->maxfield; n++) kfree(report->field[n]); kfree(report); @@ -1667,6 +1671,103 @@ static void hid_process_report(struct hid_device *hid, } } +/* + * Insert a given usage_index in a field in the list + * of processed usages in the report. + * + * The elements of lower priority score are processed + * first. + */ +static void __hid_insert_field_entry(struct hid_device *hid, + struct hid_report *report, + struct hid_field_entry *entry, + struct hid_field *field, + unsigned int usage_index) +{ + struct hid_field_entry *next; + + entry->field = field; + entry->index = usage_index; + entry->priority = field->usages_priorities[usage_index]; + + /* insert the element at the correct position */ + list_for_each_entry(next, + &report->field_entry_list, + list) { + /* + * the priority of our element is strictly higher + * than the next one, insert it before + */ + if (entry->priority > next->priority) { + list_add_tail(&entry->list, &next->list); + return; + } + } + + /* lowest priority score: insert at the end */ + list_add_tail(&entry->list, &report->field_entry_list); +} + +static void hid_report_process_ordering(struct hid_device *hid, + struct hid_report *report) +{ + struct hid_field *field; + struct hid_field_entry *entries; + unsigned int a, u, usages; + unsigned int count = 0; + + /* count the number of individual fields in the report */ + for (a = 0; a < report->maxfield; a++) { + field = report->field[a]; + + if (field->flags & HID_MAIN_ITEM_VARIABLE) + count += field->report_count; + else + count++; + } + + /* allocate the memory to process the fields */ + entries = kcalloc(count, sizeof(*entries), GFP_KERNEL); + if (!entries) + return; + + report->field_entries = entries; + + /* + * walk through all fields in the report and + * store them by priority order in report->field_entry_list + * + * - Var elements are individualized (field + usage_index) + * - Arrays are taken as one, we can not chose an order for them + */ + usages = 0; + for (a = 0; a < report->maxfield; a++) { + field = report->field[a]; + + if (field->flags & HID_MAIN_ITEM_VARIABLE) { + for (u = 0; u < field->report_count; u++) { + __hid_insert_field_entry(hid, report, + &entries[usages], + field, u); + usages++; + } + } else { + __hid_insert_field_entry(hid, report, &entries[usages], + field, 0); + usages++; + } + } +} + +static void hid_process_ordering(struct hid_device *hid) +{ + struct hid_report *report; + struct hid_report_enum *report_enum = &hid->report_enum[HID_INPUT_REPORT]; + + list_for_each_entry(report, &report_enum->report_list, list) + hid_report_process_ordering(hid, report); +} + /* * Output the field into the report. */ @@ -2050,6 +2151,8 @@ int hid_connect(struct hid_device *hdev, unsigned int connect_mask) return -ENODEV; } + hid_process_ordering(hdev); + if ((hdev->claimed & HID_CLAIMED_INPUT) && (connect_mask & HID_CONNECT_FF) && hdev->ff_init) hdev->ff_init(hdev); diff --git a/include/linux/hid.h b/include/linux/hid.h index cf79eb3da465..f25020c0d6b8 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -477,6 +477,7 @@ struct hid_field { unsigned report_type; /* (input,output,feature) */ __s32 *value; /* last known value(s) */ __s32 *new_value; /* newly read value(s) */ + __s32 *usages_priorities; /* priority of each usage when reading the report */ __s32 logical_minimum; __s32 logical_maximum; __s32 physical_minimum; @@ -493,13 +494,22 @@ struct hid_field { #define HID_MAX_FIELDS 256 +struct hid_field_entry { + struct list_head list; + struct hid_field *field; + unsigned int index; + __s32 priority; +}; + struct hid_report { struct list_head list; struct list_head hidinput_list; + struct list_head field_entry_list; /* ordered list of input fields */ unsigned int id; /* id of this report */ unsigned int type; /* report type */ unsigned int application; /* application usage for this report */ struct hid_field *field[HID_MAX_FIELDS]; /* fields of the report */ + struct hid_field_entry *field_entries; /* allocated memory of input field_entry */ unsigned maxfield; /* maximum valid field index */ unsigned size; /* size of the report (bits) */ struct hid_device *device; /* associated device */ -- 2.33.1