From patchwork Tue Mar 14 13:29:13 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomasz Kramkowski X-Patchwork-Id: 9623345 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CC33460522 for ; Tue, 14 Mar 2017 13:30:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C2BE328576 for ; Tue, 14 Mar 2017 13:30:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B793028578; Tue, 14 Mar 2017 13:30:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C299028576 for ; Tue, 14 Mar 2017 13:30:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750991AbdCNNaM (ORCPT ); Tue, 14 Mar 2017 09:30:12 -0400 Received: from erebus.the-tk.com ([109.74.205.187]:57322 "EHLO erebus.the-tk.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752228AbdCNNaK (ORCPT ); Tue, 14 Mar 2017 09:30:10 -0400 Received: from erebus.the-tk.com (localhost [127.0.0.1]) by erebus.the-tk.com (OpenSMTPD) with ESMTP id ea8c81d0; Tue, 14 Mar 2017 13:29:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=the-tk.com; h=from:to:cc :subject:date:message-id; s=sel0; bh=RHVBBpUfvOILeeXYaFbDggAhG/U =; b=xm85f3pO1vwHolpNYt1lzhmG2q2OEfI+kRm8864Y+I+nIFlqIcGu8LmvwOI tEZ5NzndeMJq6wF2rEibjVm5eGLbcxLrT1L4FFUYPTHs3ihzY5sSbyhiOhh3ENh/ 7JLnwihzHh9yFcX/hOirvA5QQkZUC+ry8H3663SLlhaAn57s= DomainKey-Signature: a=rsa-sha1; c=nofws; d=the-tk.com; h=from:to:cc :subject:date:message-id; q=dns; s=sel0; b=wjwEsnMBK0iyf1iRi9jSD 39Df8f6pRTBZTLE9UVFyklABihaDjGO3EmnWf9d04mkV7lcuhi+OWmzBCTfsaMtb MQh//fJHYx6d8v6Kl25UGfxo+yFTrj6DNjPQf4LCRx/s3liSNxW+XWzkckXciC+N LedpD5pVX90p+uDrXiOge4= Received: from localhost (host217-40-21-105.in-addr.btopenworld.com [217.40.21.105]) by erebus.the-tk.com (OpenSMTPD) with ESMTPSA id 08a10f5d (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Tue, 14 Mar 2017 13:29:53 +0000 (UTC) From: Tomasz Kramkowski To: Jiri Kosina Cc: Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Tomasz Kramkowski Subject: [PATCH v2] HID: clamp input to logical range if no null state Date: Tue, 14 Mar 2017 13:29:13 +0000 Message-Id: <20170314132913.9730-1-tk@the-tk.com> X-Mailer: git-send-email 2.12.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch fixes an issue in drivers/hid/hid-input.c where values outside of the logical range are not clamped when "null state" bit of the input control is not set. This was discussed on the lists [1] and this change stems from the fact due to the ambiguity of the HID specification it might be appropriate to follow Microsoft's own interpretation of the specification. As noted in Microsoft's documentation [2] in the section titled "Required HID usages for digitizers" it is noted that values reported outside the logical range "will be considered as invalid data and the value will be changed to the nearest boundary value (logical min/max)." This patch fixes an issue where the (1292:4745) Innomedia INNEX GENESIS/ATARI reports out of range values for its X and Y axis of the DPad which, due to the null state bit being unset, are forwarded to userspace as is. Now these values will get clamped to the logical range before being forwarded to userspace. This device was also used to test this patch. This patch expands on commit 3f3752705dbd ("HID: reject input outside logical range only if null state is set"). Signed-off-by: Tomasz Kramkowski Acked-by: Benjamin Tissoires --- v1 was: https://lkml.kernel.org/r/20170308215211.24263-1-tk@the-tk.com v2: - Expand comment to mention change and link to MSDN. - Use clamp instead of ?: . - Don't range check twice. - Add Acked-by. --- drivers/hid/hid-input.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c index cf8256aac2bd..a1ebdd7d4d4d 100644 --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -1150,19 +1150,26 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct /* * Ignore out-of-range values as per HID specification, - * section 5.10 and 6.2.25. + * section 5.10 and 6.2.25, when NULL state bit is present. + * When it's not, clamp the value to match Microsoft's input + * driver as mentioned in "Required HID usages for digitizers": + * https://msdn.microsoft.com/en-us/library/windows/hardware/dn672278(v=vs.85).asp * * The logical_minimum < logical_maximum check is done so that we * don't unintentionally discard values sent by devices which * don't specify logical min and max. */ if ((field->flags & HID_MAIN_ITEM_VARIABLE) && - (field->flags & HID_MAIN_ITEM_NULL_STATE) && - (field->logical_minimum < field->logical_maximum) && - (value < field->logical_minimum || - value > field->logical_maximum)) { - dbg_hid("Ignoring out-of-range value %x\n", value); - return; + (field->logical_minimum < field->logical_maximum)) { + if (field->flags & HID_MAIN_ITEM_NULL_STATE && + (value < field->logical_minimum || + value > field->logical_maximum)) { + dbg_hid("Ignoring out-of-range value %x\n", value); + return; + } + value = clamp(value, + field->logical_minimum, + field->logical_maximum); } /*