From patchwork Wed Nov 14 15:59:15 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 1742321 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 782CD3FC64 for ; Wed, 14 Nov 2012 16:02:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1422784Ab2KNP7q (ORCPT ); Wed, 14 Nov 2012 10:59:46 -0500 Received: from mail-wi0-f172.google.com ([209.85.212.172]:38882 "EHLO mail-wi0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1422641Ab2KNP7o (ORCPT ); Wed, 14 Nov 2012 10:59:44 -0500 Received: by mail-wi0-f172.google.com with SMTP id hj6so622577wib.1 for ; Wed, 14 Nov 2012 07:59:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:x-mailer:in-reply-to:references; bh=8KtRcF91RPNRwZ0/7UY76/3exi0jaOwRHD9dTdkGWH0=; b=KHdHcQ5T6hi4EWKx4Ius6fufufcNWexHNxUhYJ8V6/Wd5M2ToKBhCLuSsDuw1HpDin ww0/MU3jCzSp/ln5krJNDChQHyxO/vJtsKVqii/9kUjpiDX4pgLablRHDbLVEn+1mKke kdp+Knjxkr6+LbPQLjz+VF9WNG+7Gw2alKtAJ58ekpBOLpMMZt2MOyZnFGSK2C58U0Vk NF1+UPoIhPEI6KuOqIVPll5cQ+9zimxAzQ87cxCDfcQ9sXxwi5va3jBI5enRd825awa8 +d6eG4Nj8iThkx+3GBWJtfnPOK4rJyhExDMSfymir97KdhEKMEVoqZNHkNkjlpL0+RF9 g2vg== Received: by 10.180.105.162 with SMTP id gn2mr7842800wib.4.1352908783435; Wed, 14 Nov 2012 07:59:43 -0800 (PST) Received: from localhost.localdomain.com (lan31-8-82-247-176-67.fbx.proxad.net. [82.247.176.67]) by mx.google.com with ESMTPS id r10sm3050116wiz.0.2012.11.14.07.59.41 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 14 Nov 2012 07:59:42 -0800 (PST) From: Benjamin Tissoires To: "benjamin.tissoires" , Dmitry Torokhov , Henrik Rydberg , Jiri Kosina , Stephane Chatty , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 03/14] HID: core: fix unit exponent parsing Date: Wed, 14 Nov 2012 16:59:15 +0100 Message-Id: <1352908766-4492-4-git-send-email-benjamin.tissoires@gmail.com> X-Mailer: git-send-email 1.8.0 In-Reply-To: <1352908766-4492-1-git-send-email-benjamin.tissoires@gmail.com> References: <1352908766-4492-1-git-send-email-benjamin.tissoires@gmail.com> Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org HID spec details special values for the HID field unit exponent. Basically, the range [0x8..0xf] correspond to [-8..-1], so this is a standard two's complement on a half-byte. Signed-off-by: Benjamin Tissoires Acked-by: Jiri Kosina --- drivers/hid/hid-core.c | 16 +++++++++++++++- drivers/hid/hid-input.c | 13 +++++++++---- include/linux/hid.h | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 3d0da29..9da3007 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -315,6 +315,7 @@ static s32 item_sdata(struct hid_item *item) static int hid_parser_global(struct hid_parser *parser, struct hid_item *item) { + __u32 raw_value; switch (item->tag) { case HID_GLOBAL_ITEM_TAG_PUSH: @@ -365,7 +366,14 @@ static int hid_parser_global(struct hid_parser *parser, struct hid_item *item) return 0; case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT: - parser->global.unit_exponent = item_sdata(item); + /* Units exponent negative numbers are given through a + * two's complement. + * See "6.2.2.7 Global Items" for more information. */ + raw_value = item_udata(item); + if (!(raw_value & 0xfffffff0)) + parser->global.unit_exponent = hid_snto32(raw_value, 4); + else + parser->global.unit_exponent = raw_value; return 0; case HID_GLOBAL_ITEM_TAG_UNIT: @@ -865,6 +873,12 @@ static s32 snto32(__u32 value, unsigned n) return value & (1 << (n - 1)) ? value | (-1 << n) : value; } +s32 hid_snto32(__u32 value, unsigned n) +{ + return snto32(value, n); +} +EXPORT_SYMBOL_GPL(hid_snto32); + /* * Convert a signed 32-bit integer to a signed n-bit integer. */ diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c index 67044f3..7015080 100644 --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -192,7 +192,6 @@ static int hidinput_setkeycode(struct input_dev *dev, return -EINVAL; } - /** * hidinput_calc_abs_res - calculate an absolute axis resolution * @field: the HID report field to calculate resolution for @@ -235,17 +234,23 @@ __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code) case ABS_MT_TOOL_Y: case ABS_MT_TOUCH_MAJOR: case ABS_MT_TOUCH_MINOR: - if (field->unit == 0x11) { /* If centimeters */ + if (field->unit & 0xffffff00) /* Not a length */ + return 0; + unit_exponent += hid_snto32(field->unit >> 4, 4) - 1; + switch (field->unit & 0xf) { + case 0x1: /* If centimeters */ /* Convert to millimeters */ unit_exponent += 1; - } else if (field->unit == 0x13) { /* If inches */ + break; + case 0x3: /* If inches */ /* Convert to millimeters */ prev = physical_extents; physical_extents *= 254; if (physical_extents < prev) return 0; unit_exponent -= 1; - } else { + break; + default: return 0; } break; diff --git a/include/linux/hid.h b/include/linux/hid.h index 9edb06c..a110a3e 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -754,6 +754,7 @@ int hid_connect(struct hid_device *hid, unsigned int connect_mask); void hid_disconnect(struct hid_device *hid); const struct hid_device_id *hid_match_id(struct hid_device *hdev, const struct hid_device_id *id); +s32 hid_snto32(__u32 value, unsigned n); /** * hid_map_usage - map usage input bits