Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp1556249rdb; Sat, 23 Dec 2023 11:15:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IHYn6XMVdqIJ8EGe0uwq7oyJuRcuBqgSsA7+TxL139lyNDi3CxOq9WDYHyynpUT0tsWozJk X-Received: by 2002:a50:d702:0:b0:54c:6d39:b152 with SMTP id t2-20020a50d702000000b0054c6d39b152mr2003181edi.3.1703358930162; Sat, 23 Dec 2023 11:15:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703358930; cv=none; d=google.com; s=arc-20160816; b=SzXTiG/S4rC1cosg/TGBBv0sTYmrTpHu3d0iKyKH15qNt97bDlYB288nIbcQ+Jfp7D DvEDmvuzI3AO3GdVLWzVek/bMj+xL3hbSs8vGjdi55bV/OjAxwzsamlbQ+H4kSqv3Ms4 EZtRWo9ZSAnvdWXVjZb8SIpTOZEDg6jW02wU4vjMsCk7xC9dc+O0le6D/9pvqhXHxWMW eGku/oBV4Sa5bKzHaNxkcnHGngmrs2vYyenSGwswxhxiki7V6GiGOzfqnkqYk1KQjVwX re43KaFun7MqsU8yc/qJfXAzSKDRFupMMlBc+dmEu7qpFx5Z42EVs7vHcO5wRo1R2XKn b5NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=epdhbJc4U9lQ2pcLGsq7vqZYZ1tC+XPrj4+su/oHOyk=; fh=VcZ6xHaxv3i32PFApYWyyLsu/xSZ43X4sLXtkC92w+w=; b=CsHwxFv0dLJ9XijLez3BOgBPwcJdT4kVVN1J2LIeJRmdZEKxnlPNROooElMy1WOkcN zwo467lOeqShw2cg/GZF6kmrdhvj+vxoPvmSLfmz4osIO7JelFy3JvYbCKi2hOa9REwz 2wMc4O9DVggBW/iMvO1Q/xHBJk68NfsBoL26o7GDeqJT+GLnI3oTd7n5OksSxnrQuh1h VBfyOshveTvgMtDnGH4nNtdSNnRqj3zbBk5lHF05nHXnnhyUTvSLfPr/535Bqd/ZXm+l UI9mjlNRocIrBD6lqPEWcAYsIvC+99S6yllj+foMajKstNRN6x3hO0l+zAO3ZNoUkoWh BybA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@khvoinitsky.org header.s=key1 header.b=KLeM2ZPQ; spf=pass (google.com: domain of linux-kernel+bounces-10560-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-10560-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=khvoinitsky.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id b93-20020a509f66000000b00554b11ab79asi149173edf.54.2023.12.23.11.15.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Dec 2023 11:15:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-10560-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@khvoinitsky.org header.s=key1 header.b=KLeM2ZPQ; spf=pass (google.com: domain of linux-kernel+bounces-10560-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-10560-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=khvoinitsky.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E7B361F21C20 for ; Sat, 23 Dec 2023 19:15:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0EE921D681; Sat, 23 Dec 2023 19:15:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=khvoinitsky.org header.i=@khvoinitsky.org header.b="KLeM2ZPQ" X-Original-To: linux-kernel@vger.kernel.org Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AABCF199D1 for ; Sat, 23 Dec 2023 19:14:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=khvoinitsky.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=khvoinitsky.org X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=khvoinitsky.org; s=key1; t=1703358895; 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=epdhbJc4U9lQ2pcLGsq7vqZYZ1tC+XPrj4+su/oHOyk=; b=KLeM2ZPQl5XwOj3Oyp9ho7k3DpwFG/cPM8tT0uuxf0scpM7lBWIAZ/ZqhkcAVghxi3qz64 N94mfuogMzg9xfFcwZGbkDwnzzu28NOhTeQ2booQmzEwEen/LvrnbFHmmqEUdUR/GM8Cb4 nkv+H6t3lrf7GAScVXSLsv9gF6mxI8I= From: Mikhail Khvainitski To: Jiri Kosina , Benjamin Tissoires Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Mikhail Khvainitski , iam@valdikss.org.ru, jekhor@gmail.com, u-v@mailbox.org Subject: [PATCH] HID: lenovo: Add middleclick_workaround sysfs knob for cptkbd Date: Sat, 23 Dec 2023 21:12:13 +0200 Message-ID: <20231223191401.70727-1-me@khvoinitsky.org> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT Previous attempt to autodetect well-behaving patched firmware introduced in commit 46a0a2c96f0f ("HID: lenovo: Detect quirk-free fw on cptkbd and stop applying workaround") has shown that there are false-positives on original firmware (on both 1st gen and 2nd gen keyboards) which causes the middle button click workaround to be mistakenly disabled. This commit adds explicit parameter to sysfs to control this workaround. Fixes: 46a0a2c96f0f ("HID: lenovo: Detect quirk-free fw on cptkbd and stop applying workaround") Fixes: 43527a0094c1 ("HID: lenovo: Restrict detection of patched firmware only to USB cptkbd") Signed-off-by: Mikhail Khvainitski --- drivers/hid/hid-lenovo.c | 57 +++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/drivers/hid/hid-lenovo.c b/drivers/hid/hid-lenovo.c index 149a3c74346b..f86c1ea83a03 100644 --- a/drivers/hid/hid-lenovo.c +++ b/drivers/hid/hid-lenovo.c @@ -54,10 +54,10 @@ struct lenovo_drvdata { /* 0: Up * 1: Down (undecided) * 2: Scrolling - * 3: Patched firmware, disable workaround */ u8 middlebutton_state; bool fn_lock; + bool middleclick_workaround_cptkbd; }; #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c)) @@ -621,6 +621,36 @@ static ssize_t attr_sensitivity_store_cptkbd(struct device *dev, return count; } +static ssize_t attr_middleclick_workaround_show_cptkbd(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct hid_device *hdev = to_hid_device(dev); + struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); + + return snprintf(buf, PAGE_SIZE, "%u\n", + cptkbd_data->middleclick_workaround_cptkbd); +} + +static ssize_t attr_middleclick_workaround_store_cptkbd(struct device *dev, + struct device_attribute *attr, + const char *buf, + size_t count) +{ + struct hid_device *hdev = to_hid_device(dev); + struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); + int value; + + if (kstrtoint(buf, 10, &value)) + return -EINVAL; + if (value < 0 || value > 1) + return -EINVAL; + + cptkbd_data->middleclick_workaround_cptkbd = !!value; + + return count; +} + static struct device_attribute dev_attr_fn_lock = __ATTR(fn_lock, S_IWUSR | S_IRUGO, @@ -632,10 +662,16 @@ static struct device_attribute dev_attr_sensitivity_cptkbd = attr_sensitivity_show_cptkbd, attr_sensitivity_store_cptkbd); +static struct device_attribute dev_attr_middleclick_workaround_cptkbd = + __ATTR(middleclick_workaround, S_IWUSR | S_IRUGO, + attr_middleclick_workaround_show_cptkbd, + attr_middleclick_workaround_store_cptkbd); + static struct attribute *lenovo_attributes_cptkbd[] = { &dev_attr_fn_lock.attr, &dev_attr_sensitivity_cptkbd.attr, + &dev_attr_middleclick_workaround_cptkbd.attr, NULL }; @@ -686,23 +722,7 @@ static int lenovo_event_cptkbd(struct hid_device *hdev, { struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev); - if (cptkbd_data->middlebutton_state != 3) { - /* REL_X and REL_Y events during middle button pressed - * are only possible on patched, bug-free firmware - * so set middlebutton_state to 3 - * to never apply workaround anymore - */ - if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD && - cptkbd_data->middlebutton_state == 1 && - usage->type == EV_REL && - (usage->code == REL_X || usage->code == REL_Y)) { - cptkbd_data->middlebutton_state = 3; - /* send middle button press which was hold before */ - input_event(field->hidinput->input, - EV_KEY, BTN_MIDDLE, 1); - input_sync(field->hidinput->input); - } - + if (cptkbd_data->middleclick_workaround_cptkbd) { /* "wheel" scroll events */ if (usage->type == EV_REL && (usage->code == REL_WHEEL || usage->code == REL_HWHEEL)) { @@ -1166,6 +1186,7 @@ static int lenovo_probe_cptkbd(struct hid_device *hdev) cptkbd_data->middlebutton_state = 0; cptkbd_data->fn_lock = true; cptkbd_data->sensitivity = 0x05; + cptkbd_data->middleclick_workaround_cptkbd = true; lenovo_features_set_cptkbd(hdev); ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd); -- 2.43.0