From patchwork Mon Mar 24 20:50:01 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petri Gynther X-Patchwork-Id: 3884511 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id D913BBF540 for ; Mon, 24 Mar 2014 21:19:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C4FE3201BF for ; Mon, 24 Mar 2014 21:19:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AB67D200D7 for ; Mon, 24 Mar 2014 21:19:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751268AbaCXVTW (ORCPT ); Mon, 24 Mar 2014 17:19:22 -0400 Received: from mail-vc0-f201.google.com ([209.85.220.201]:50015 "EHLO mail-vc0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751070AbaCXVTV (ORCPT ); Mon, 24 Mar 2014 17:19:21 -0400 X-Greylist: delayed 1396 seconds by postgrey-1.27 at vger.kernel.org; Mon, 24 Mar 2014 17:19:21 EDT Received: by mail-vc0-f201.google.com with SMTP id ik5so771871vcb.4 for ; Mon, 24 Mar 2014 14:19:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:message-id:date; bh=oq2jd4sXFLRC1gSoj24PoUJitecoop23qgrD8+wD/Xw=; b=bxeFOEHtTBUJvDqZlgU7CAQO6GIQOSBXvar47ILXl+m89NAvoS7e3hP+t2SLzHyF9x +7i/t0iyYugCn2Du4Mu82OA9Bc8pxMLTBP/QHCadQIGPeZUm+foAoPGTnEd8o3mNMWNh 8WPUJu8cmxK2NDN8OxgvlTLAU4qfM+22OltZnv5WpL9srjHqht8uFtCQZePA8jvUz2UU jXEd614Zm14QyO6f7bSliZQsiD42RdhFlCEZMtuNMCyCgMeOto3QA7YoePuM15Le1a2B fYp6Z3MX35xMLs3TsTCSLtGUdBN2D8T7WNdsG0RlFLwHA8O6QnWVb8smOfcyZHp+BPBw V3Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:message-id:date; bh=oq2jd4sXFLRC1gSoj24PoUJitecoop23qgrD8+wD/Xw=; b=mNZUXgw5NO2W23gi4RF6z6cxW1uL/S2c/mPxBEgVvS9+n0mjJyvV4SBoU4Yc9HLqAd g5OiQaM9ECIr3LN0fuRzQeMiH9LGc69PY8mkFVGls0SekS3FAXmNJaDuA5X2Wety+VZ3 tE03EibqIhK5yXsrhEl4iJqSECjrlycV7H1GGCZhQJnhExQNfyz0XVTflqqxFHBR8Jv0 x4T9iktstb+nFG6zxmV5PhYD9T2g3zX8f+lVis2Fzd/nM5Il2nbLe1w/0Hxh71UkvaSW lx2sRowlPvJR4hXwVdhy7wH09lptlOqjKmITUHyk8K1EP8/jIDmokKLRww09HkDHoRBw gPfw== X-Gm-Message-State: ALoCoQn9kP/XPnEW/MwYkP2gwEMIxzqBYRQVwCUPGgtSJQJjZhfcQcZLrQUVEe7anAR2X7n+WKQ10JHzouajj9r/PMcTpWNsKLK8kIF0Tl836Eq63olj9MZyuVfOBD8CfcA8ME5Nk1zpftb/ud+VOB+NvI4JE+Qdt5SBynIfW84j/zbNj88W9xCFqaSTidlJCBbZCMDjzE+ut1/vyBxO1J4Lg8Z6WjwbDw== X-Received: by 10.236.203.113 with SMTP id e77mr24442719yho.15.1395694202239; Mon, 24 Mar 2014 13:50:02 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id a66si2022563yhb.6.2014.03.24.13.50.02 for (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 24 Mar 2014 13:50:02 -0700 (PDT) Received: from puck.mtv.corp.google.com (puck.mtv.corp.google.com [172.27.88.166]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id 0A85631C2C3; Mon, 24 Mar 2014 13:50:02 -0700 (PDT) Received: by puck.mtv.corp.google.com (Postfix, from userid 68020) id 981DD100D4F; Mon, 24 Mar 2014 13:50:01 -0700 (PDT) From: Petri Gynther To: linux-input@vger.kernel.org Cc: dh.herrmann@gmail.com, jkosina@suse.cz Subject: [PATCH v2] HID: uhid: Add UHID_CREATE2 + UHID_INPUT2 Message-Id: <20140324205001.981DD100D4F@puck.mtv.corp.google.com> Date: Mon, 24 Mar 2014 13:50:01 -0700 (PDT) Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP UHID_CREATE2: HID report descriptor data (rd_data) is an array in struct uhid_create2_req, instead of a pointer. Enables use from languages that don't support pointers, e.g. Python. UHID_INPUT2: Data array is the last field of struct uhid_input2_req. Enables userspace to write only the required bytes to kernel (ev.type + ev.u.input2.size + the part of the data array that matters), instead of the entire struct uhid_input2_req. Note: UHID_CREATE2 increases the total size of struct uhid_event slightly, thus increasing the size of messages that are queued for userspace. However, this won't affect the userspace processing of these events. Signed-off-by: Petri Gynther Reviewed-by: David Herrmann --- Documentation/hid/uhid.txt | 11 +++++++ drivers/hid/uhid.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/uhid.h | 23 +++++++++++++ 3 files changed, 114 insertions(+) diff --git a/Documentation/hid/uhid.txt b/Documentation/hid/uhid.txt index dc35a2b..ee65936 100644 --- a/Documentation/hid/uhid.txt +++ b/Documentation/hid/uhid.txt @@ -93,6 +93,11 @@ the request was handled successfully. event to the kernel. The payload is of type struct uhid_create_req and contains information about your device. You can start I/O now. + UHID_CREATE2: + Same as UHID_CREATE, but the HID report descriptor data (rd_data) is an array + inside struct uhid_create2_req, instead of a pointer to a separate array. + Enables use from languages that don't support pointers, e.g. Python. + UHID_DESTROY: This destroys the internal HID device. No further I/O will be accepted. There may still be pending messages that you can receive with read() but no further @@ -105,6 +110,12 @@ the request was handled successfully. contains a data-payload. This is the raw data that you read from your device. The kernel will parse the HID reports and react on it. + UHID_INPUT2: + Same as UHID_INPUT, but the data array is the last field of uhid_input2_req. + Enables userspace to write only the required bytes to kernel (ev.type + + ev.u.input2.size + the part of the data array that matters), instead of + the entire struct uhid_input2_req. + UHID_FEATURE_ANSWER: If you receive a UHID_FEATURE request you must answer with this request. You must copy the "id" field from the request into the answer. Set the "err" field diff --git a/drivers/hid/uhid.c b/drivers/hid/uhid.c index cedc6da..c5ee173 100644 --- a/drivers/hid/uhid.c +++ b/drivers/hid/uhid.c @@ -407,6 +407,69 @@ err_free: return ret; } +static int uhid_dev_create2(struct uhid_device *uhid, + const struct uhid_event *ev) +{ + struct hid_device *hid; + int ret; + + if (uhid->running) + return -EALREADY; + + uhid->rd_size = ev->u.create2.rd_size; + if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE) + return -EINVAL; + + uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL); + if (!uhid->rd_data) + return -ENOMEM; + + memcpy(uhid->rd_data, ev->u.create2.rd_data, uhid->rd_size); + + hid = hid_allocate_device(); + if (IS_ERR(hid)) { + ret = PTR_ERR(hid); + goto err_free; + } + + strncpy(hid->name, ev->u.create2.name, 127); + hid->name[127] = 0; + strncpy(hid->phys, ev->u.create2.phys, 63); + hid->phys[63] = 0; + strncpy(hid->uniq, ev->u.create2.uniq, 63); + hid->uniq[63] = 0; + + hid->ll_driver = &uhid_hid_driver; + hid->hid_get_raw_report = uhid_hid_get_raw; + hid->hid_output_raw_report = uhid_hid_output_raw; + hid->bus = ev->u.create2.bus; + hid->vendor = ev->u.create2.vendor; + hid->product = ev->u.create2.product; + hid->version = ev->u.create2.version; + hid->country = ev->u.create2.country; + hid->driver_data = uhid; + hid->dev.parent = uhid_misc.this_device; + + uhid->hid = hid; + uhid->running = true; + + ret = hid_add_device(hid); + if (ret) { + hid_err(hid, "Cannot register HID device\n"); + goto err_hid; + } + + return 0; + +err_hid: + hid_destroy_device(hid); + uhid->hid = NULL; + uhid->running = false; +err_free: + kfree(uhid->rd_data); + return ret; +} + static int uhid_dev_destroy(struct uhid_device *uhid) { if (!uhid->running) @@ -435,6 +498,17 @@ static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev) return 0; } +static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev) +{ + if (!uhid->running) + return -EINVAL; + + hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data, + min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0); + + return 0; +} + static int uhid_dev_feature_answer(struct uhid_device *uhid, struct uhid_event *ev) { @@ -571,12 +645,18 @@ static ssize_t uhid_char_write(struct file *file, const char __user *buffer, case UHID_CREATE: ret = uhid_dev_create(uhid, &uhid->input_buf); break; + case UHID_CREATE2: + ret = uhid_dev_create2(uhid, &uhid->input_buf); + break; case UHID_DESTROY: ret = uhid_dev_destroy(uhid); break; case UHID_INPUT: ret = uhid_dev_input(uhid, &uhid->input_buf); break; + case UHID_INPUT2: + ret = uhid_dev_input2(uhid, &uhid->input_buf); + break; case UHID_FEATURE_ANSWER: ret = uhid_dev_feature_answer(uhid, &uhid->input_buf); break; diff --git a/include/uapi/linux/uhid.h b/include/uapi/linux/uhid.h index 414b74b..1e3b09c 100644 --- a/include/uapi/linux/uhid.h +++ b/include/uapi/linux/uhid.h @@ -21,6 +21,7 @@ #include #include +#include enum uhid_event_type { UHID_CREATE, @@ -34,6 +35,8 @@ enum uhid_event_type { UHID_INPUT, UHID_FEATURE, UHID_FEATURE_ANSWER, + UHID_CREATE2, + UHID_INPUT2, }; struct uhid_create_req { @@ -50,6 +53,19 @@ struct uhid_create_req { __u32 country; } __attribute__((__packed__)); +struct uhid_create2_req { + __u8 name[128]; + __u8 phys[64]; + __u8 uniq[64]; + __u16 rd_size; + __u16 bus; + __u32 vendor; + __u32 product; + __u32 version; + __u32 country; + __u8 rd_data[HID_MAX_DESCRIPTOR_SIZE]; +} __attribute__((__packed__)); + #define UHID_DATA_MAX 4096 enum uhid_report_type { @@ -63,6 +79,11 @@ struct uhid_input_req { __u16 size; } __attribute__((__packed__)); +struct uhid_input2_req { + __u16 size; + __u8 data[UHID_DATA_MAX]; +} __attribute__((__packed__)); + struct uhid_output_req { __u8 data[UHID_DATA_MAX]; __u16 size; @@ -100,6 +121,8 @@ struct uhid_event { struct uhid_output_ev_req output_ev; struct uhid_feature_req feature; struct uhid_feature_answer_req feature_answer; + struct uhid_create2_req create2; + struct uhid_input2_req input2; } u; } __attribute__((__packed__));