From patchwork Tue Aug 2 20:15:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suniel Mahesh X-Patchwork-Id: 2291 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 02E9F3F1D0 for ; Tue, 2 Aug 2022 22:16:14 +0200 (CEST) Received: by mail-pj1-f71.google.com with SMTP id co4-20020a17090afe8400b001f4df09d662sf3466299pjb.2 for ; Tue, 02 Aug 2022 13:16:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1659471372; cv=pass; d=google.com; s=arc-20160816; b=RHTA1lxOJfezSFhf7grkcnlKb49qd7Q05zvz5hC0ntkN8bqZTGNwViVtfBh2IZ47Ho 6+AyEg56DythPgpkxsG1QeZg+FpCbOr2zTH7nfHymGGnS6FPP9VhiRZ0TJKjQQ7vTHZu sZyeJOt2ML6wLrddCbCxLYQiEjWEH42EQQacyVuL932XGS9hMjTNWOrFWcAQuzUAR8W2 TkBDuSZZ/QuVRJnafBvve0+laZqcp6p7ZhJAOMeHMsb+5hF+ISlqIHGLyuuddAjW+Wzk noPEoY6JmxeXjaNDVYvVOBEmSFtbnpjFXXBaqmvoDonv4LSdJW5F73RPW9w/ysfALnEJ UDzA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=iGg6OUUnvntvb/BYdqgreZzlc1nI7pRwM5cD1HPw4IQ=; b=B7sOEF7UKslxJanRWjLajGGRyovNoWONU3/o7bXdaVx1YxAyc+3sMNB0i5XAljO+wc yB7EI+CeRm+MXBxytA2LChL7hzgJq8M6ij9yUv37mYxg1odUgD7uqSHJszl8w3+JmVTo jqvIB/mUbA1eFTb4u8qjTzax74auY6sOF0BHpVYPOQOmlYV+9wivOTf18x7NCR18yM0Q Y79yJjgDssI0wDCrSKEKYKBZ87UIb8whZuD5CuJhQBfElLR1uudIF0WurPZbp1etM9qR ojlVIzHb0uxpk3BZOTS5uxBBpUgKQaIY1sfWoE8ZUdaWa8IebAuNOeBxc2sveYrumdyr 0zlw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=KYjbiOhx; spf=pass (google.com: domain of sunil@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=sunil@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=iGg6OUUnvntvb/BYdqgreZzlc1nI7pRwM5cD1HPw4IQ=; b=F2VL9RkN64GmRocfyAZjJ7s8v6U49M0c/WfKzEEgRN511CWYlAYGLoPyHfAC/uFzlM akuEbpb1M8IMDqW7jit+TPElV3F7XZSPC3hu6ESr/qoVgs95CGmnAdjYzZTk9XZTu9iP cAtEbLXCnl/KdmZD1APXeSo7TQ3S6OF5yipXM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-unsubscribe; bh=iGg6OUUnvntvb/BYdqgreZzlc1nI7pRwM5cD1HPw4IQ=; b=PtrneFriThKrUK6DvkPoGUYVvfhLDdbQdiwaFn8pOfPHzbJtY7Ojx8Pl2lZav9bj5J x4TIoSB3+DsEqSaFY5FB30Ajkc89dzYe9KnYn/2sQXaBA3hQb2brOebRRN5Rw+t0iyur aoaw+nA191ga4qY1hm9rufM3dS8cebMoOaKD0BlitPu1UQDjpyMiEhH7DIMGdRzI2H1r 8t7H/hX8xICW4rJFtoJv+wqWrO/wGKNsF8oa+xbIeTb6hTNsXZHNRtg1GdHWh5C8WecK i+cEFRvUDDcrVO5tXNRVEMHgln7g6EkTqZ1uS8i2kLtislwU87Oo0dUFORyvPu3B+NP0 XjpA== X-Gm-Message-State: AJIora/NUlLDfK9FSd3zUWOS30zLkJMIawlahV0PNlqSee8KfBKPn4R8 ZnZOl6++KTLyJBYPj5VDv9WKL9FK X-Google-Smtp-Source: AGRyM1t8oVkwGK3YI4CAINt3L0C1Cvyyrh9qNCXu3id36hq0vZVN99g6dCL1JogWao/17gd3NeQCkQ== X-Received: by 2002:a05:6a00:e0e:b0:522:990c:ab60 with SMTP id bq14-20020a056a000e0e00b00522990cab60mr22772755pfb.8.1659471372729; Tue, 02 Aug 2022 13:16:12 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a63:f110:0:b0:41b:203a:51a with SMTP id f16-20020a63f110000000b0041b203a051als7424952pgi.5.-pod-prod-gmail; Tue, 02 Aug 2022 13:16:12 -0700 (PDT) X-Received: by 2002:a63:2401:0:b0:41c:7576:83cd with SMTP id k1-20020a632401000000b0041c757683cdmr2663688pgk.321.1659471371885; Tue, 02 Aug 2022 13:16:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659471371; cv=none; d=google.com; s=arc-20160816; b=gKXqL3zSQfTxMHUL7hmV3gN7XwtxZVU4xTi+JYI6N+rhhkWkuha3P7WI8tY2ZEkcM7 5Lpu/4Xle73trsR6UBU9Mi8mNlXO9f7hQbQewc0KTUi65t4CaiOdGhUyCNqfGJJ5jS4B VX0F8JNQUjMAvA+XByCWYFc/XNwBUypSn9D8xqg6Y+qtLG5s+zg+8QFvtSenTi/lqcfv jMBD26B+q50FFzcotQkTBFmA2lf4XQE/k+KU8YBWQxEklmNYIBlzukJVzGlV2IHD73vs Y49XC4JBIU/nTIPY26WcxIFJj5WaIjw9rf6CbFE9ai8DcgLCfD+ZOSBU8msuF6MLCbXi +GAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=nC6Hx+qiMFP0kKK+gIayIBAMJyU5NtZQrF2GRnpsTMA=; b=dhBLHERpkPS5z8BYuGdyWRKq86ErRLEKwFP4dNnDlbaoTmXqB7JRPwJNnRHv1jCJaE EY1GYo5CdCxkq8/oOeFbNQnZnxWj4h8s70MErGJLYTza19qFU3xV2ABXlrPalpmcGeuT 4qOhKJzz25JcOX1j7gkqF1S07cpPfczOejSR9/yKj68KTbPptXJabU5LjfeOtu6bASIn iTOykcXZAdOBhaqB/iiJ3+azlkH6CRD2vjodfM3Rlft92X3vR9rAcbWFrlG6LU/DWJRQ mkdsBCfOIVLiOC1RLrldS+clh+k6rW1MIKJgjI926YkLPwQ4yv484lVswg+HT9W9EsYD iEpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=KYjbiOhx; spf=pass (google.com: domain of sunil@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=sunil@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id b2-20020a170902d50200b0016f03e00032sor117826plg.20.2022.08.02.13.16.11 for (Google Transport Security); Tue, 02 Aug 2022 13:16:11 -0700 (PDT) Received-SPF: pass (google.com: domain of sunil@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:a17:902:b08a:b0:16c:68b6:311 with SMTP id p10-20020a170902b08a00b0016c68b60311mr21685501plr.166.1659471371483; Tue, 02 Aug 2022 13:16:11 -0700 (PDT) Received: from localhost.localdomain ([183.83.136.224]) by smtp.gmail.com with ESMTPSA id w18-20020a170902a71200b0016ee26224a4sm80863plq.305.2022.08.02.13.16.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Aug 2022 13:16:11 -0700 (PDT) From: Suniel Mahesh To: Felipe Balbi , Greg Kroah-Hartman , Michael Trimarchi , Jagan Teki , Zachary Holland , Daniel McLean , Rene Samson Ambrose , David Sands Cc: linux-kernel@vger.kernel.org, linux-amarula@amarulasolutions.com, USB list Subject: [RFC v2 1/2] usb: gadget: f_hid: Extend functionality for GET_REPORT mode Date: Wed, 3 Aug 2022 01:45:55 +0530 Message-Id: <20220802201556.1510069-2-sunil@amarulasolutions.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220802201556.1510069-1-sunil@amarulasolutions.com> References: <20220802201556.1510069-1-sunil@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: sunil@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=KYjbiOhx; spf=pass (google.com: domain of sunil@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=sunil@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , The current kernel implementation for GET_REPORT is that the kernel sends back a zero filled report (of length == report_length), when the Host request's a particular report from the device/gadget. This changeset extends functionality for GET_REPORT by sending a particular report based on report type and report number. corresponding ioctl is also implemented. Signed-off-by: Suniel Mahesh Signed-off-by: Michael Trimarchi --- Changes for v2: - Map report type coming from userspace reports to the ones defined in kernel headers. --- drivers/usb/gadget/function/f_hid.c | 184 ++++++++++++++++++++++++++- include/{ => uapi}/linux/usb/g_hid.h | 10 ++ 2 files changed, 192 insertions(+), 2 deletions(-) rename include/{ => uapi}/linux/usb/g_hid.h (72%) diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c index ca0a7d9eaa34..e1da4fb10ab3 100644 --- a/drivers/usb/gadget/function/f_hid.c +++ b/drivers/usb/gadget/function/f_hid.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "u_f.h" #include "u_hid.h" @@ -27,6 +28,11 @@ static struct class *hidg_class; static DEFINE_IDA(hidg_ida); static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */ +struct report_entry { + struct uhid_set_report_req report_data; + struct list_head node; +}; + /*-------------------------------------------------------------------------*/ /* HID gadget struct */ @@ -71,6 +77,10 @@ struct f_hidg { wait_queue_head_t write_queue; struct usb_request *req; + /* hid report list */ + spinlock_t report_spinlock; + struct list_head report_list; + int minor; struct cdev cdev; struct usb_function func; @@ -553,6 +563,154 @@ static int f_hidg_open(struct inode *inode, struct file *fd) return 0; } +static enum uhid_report_type f_hidg_from_report_to_type(__u8 r_type) +{ + switch (r_type) { + case 1: + return UHID_FEATURE_REPORT; + case 2: + return UHID_OUTPUT_REPORT; + case 3: + return UHID_INPUT_REPORT; + default: + break; + } + return -EINVAL; +} + +static bool f_hidg_param_valid(struct report_entry *entry) +{ + enum uhid_report_type report_type; + + if (entry->report_data.size > UHID_DATA_MAX) + return false; + + report_type = f_hidg_from_report_to_type(entry->report_data.rtype); + + switch (report_type) { + case UHID_FEATURE_REPORT: + case UHID_OUTPUT_REPORT: + case UHID_INPUT_REPORT: + return true; + default: + break; + } + + return false; +} + +static struct report_entry *f_hidg_search_for_report(struct f_hidg *hidg, u8 rnum, u8 rtype) +{ + struct list_head *ptr; + struct report_entry *entry; + + list_for_each(ptr, &hidg->report_list) { + entry = list_entry(ptr, struct report_entry, node); + if (entry->report_data.rnum == rnum && + entry->report_data.rtype == rtype) { + return entry; + } + } + + return NULL; +} + +static long f_hidg_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct f_hidg *hidg = file->private_data; + struct report_entry *entry; + struct report_entry *ptr; + unsigned long flags; + struct uhid_get_report_req report; + u16 size; + + switch (cmd) { + case GADGET_ADD_REPORT_STATUS: + entry = kmalloc(sizeof(*entry), GFP_KERNEL); + if (!entry) + return -ENOMEM; + + if (copy_from_user(&entry->report_data, (struct uhid_set_report_req *)arg, + sizeof(struct uhid_set_report_req))) { + kfree(entry); + return -EFAULT; + } + + if (f_hidg_param_valid(entry) == false) { + kfree(entry); + return -EINVAL; + } + + spin_lock_irqsave(&hidg->report_spinlock, flags); + ptr = f_hidg_search_for_report(hidg, entry->report_data.rnum, + entry->report_data.rtype); + if (ptr) { + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + kfree(ptr); + return -EEXIST; + } + list_add_tail(&entry->node, &hidg->report_list); + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + break; + case GADGET_REMOVE_REPORT_STATUS: + spin_lock_irqsave(&hidg->report_spinlock, flags); + if (list_empty(&hidg->report_list)) { + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + return -ENODATA; + } + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + + if (copy_from_user(&report, (struct uhid_get_report_req *)arg, sizeof(report))) + return -EFAULT; + + spin_lock_irqsave(&hidg->report_spinlock, flags); + + ptr = f_hidg_search_for_report(hidg, report.rnum, report.rtype); + if (ptr) { + list_del(&ptr->node); + } else { + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + return -ENODATA; + } + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + kfree(ptr); + break; + case GADGET_UPDATE_REPORT_STATUS: + spin_lock_irqsave(&hidg->report_spinlock, flags); + if (list_empty(&hidg->report_list)) { + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + return -ENODATA; + } + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + + if (copy_from_user(&report, (struct uhid_get_report_req *)arg, sizeof(report))) + return -EFAULT; + + if (copy_from_user(&size, (void __user *)(arg + sizeof(report)), sizeof(size))) + return -EFAULT; + + if (size > UHID_DATA_MAX) + return -EINVAL; + + spin_lock_irqsave(&hidg->report_spinlock, flags); + + ptr = f_hidg_search_for_report(hidg, report.rnum, report.rtype); + if (!ptr) { + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + return -ENODATA; + } + + if (copy_from_user(&ptr->report_data, (struct uhid_set_report_req *)arg, + sizeof(struct uhid_set_report_req))) { + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + return -EFAULT; + } + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + break; + } + return 0; +} + /*-------------------------------------------------------------------------*/ /* usb_function */ @@ -634,6 +792,8 @@ static int hidg_setup(struct usb_function *f, struct f_hidg *hidg = func_to_hidg(f); struct usb_composite_dev *cdev = f->config->cdev; struct usb_request *req = cdev->req; + struct report_entry *entry; + unsigned long flags; int status = 0; __u16 value, length; @@ -649,9 +809,25 @@ static int hidg_setup(struct usb_function *f, | HID_REQ_GET_REPORT): VDBG(cdev, "get_report\n"); - /* send an empty report */ length = min_t(unsigned, length, hidg->report_length); - memset(req->buf, 0x0, length); + spin_lock_irqsave(&hidg->report_spinlock, flags); + if (list_empty(&hidg->report_list)) { + spin_unlock_irqrestore(&hidg->report_spinlock, flags); + memset(req->buf, 0x0, length); + goto respond; + } + + entry = f_hidg_search_for_report(hidg, value & 0xf, + value >> 8); + + /* send a report */ + if (entry) { + length = min_t(unsigned, length, entry->report_data.size); + memcpy(req->buf, entry->report_data.data, length); + } else { + memset(req->buf, 0x0, length); + } + spin_unlock_irqrestore(&hidg->report_spinlock, flags); goto respond; break; @@ -893,6 +1069,7 @@ static const struct file_operations f_hidg_fops = { .owner = THIS_MODULE, .open = f_hidg_open, .release = f_hidg_release, + .unlocked_ioctl = f_hidg_ioctl, .write = f_hidg_write, .read = f_hidg_read, .poll = f_hidg_poll, @@ -997,6 +1174,9 @@ static int hidg_bind(struct usb_configuration *c, struct usb_function *f) init_waitqueue_head(&hidg->read_queue); INIT_LIST_HEAD(&hidg->completed_out_req); + spin_lock_init(&hidg->report_spinlock); + INIT_LIST_HEAD(&hidg->report_list); + /* create char device */ cdev_init(&hidg->cdev, &f_hidg_fops); dev = MKDEV(major, hidg->minor); diff --git a/include/linux/usb/g_hid.h b/include/uapi/linux/usb/g_hid.h similarity index 72% rename from include/linux/usb/g_hid.h rename to include/uapi/linux/usb/g_hid.h index 7581e488c237..ba3e47f076bb 100644 --- a/include/linux/usb/g_hid.h +++ b/include/uapi/linux/usb/g_hid.h @@ -22,6 +22,8 @@ #ifndef __LINUX_USB_G_HID_H #define __LINUX_USB_G_HID_H +#include + struct hidg_func_descriptor { unsigned char subclass; unsigned char protocol; @@ -30,4 +32,12 @@ struct hidg_func_descriptor { unsigned char report_desc[]; }; +/* The 'g' code is also used by gadgetfs and printer ioctl requests. + * Don't add any colliding codes to either driver, and keep + * them in unique ranges (size 0x40 for now). + */ +#define GADGET_ADD_REPORT_STATUS _IOWR('g', 0x41, struct uhid_set_report_req) +#define GADGET_REMOVE_REPORT_STATUS _IOWR('g', 0x42, struct uhid_get_report_req) +#define GADGET_UPDATE_REPORT_STATUS _IOWR('g', 0x43, struct uhid_set_report_req) + #endif /* __LINUX_USB_G_HID_H */ From patchwork Tue Aug 2 20:15:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suniel Mahesh X-Patchwork-Id: 2292 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id EC42F3F1D0 for ; Tue, 2 Aug 2022 22:16:17 +0200 (CEST) Received: by mail-pl1-f200.google.com with SMTP id u10-20020a170902e5ca00b0016f091c4b6bsf1141274plf.10 for ; Tue, 02 Aug 2022 13:16:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1659471376; cv=pass; d=google.com; s=arc-20160816; b=Gbofc0u7laZLkyUxAx4OIuhdN4eqk+P0rXEwOim/ZYhiushL9X5anwvyQIMpXjRV/b KlBS4E2ToRQGZIvqLL6prMoVYmIJO0vwnLPtW7nk86EbegGRM+u3HflmDVUqKomQT6JT DJEnfPV4tGL3mzwsPJmyxav0lrxMB6v/QndU42O3JI6PgiuAqhNNJ3jOvwD6TMfEDyga q3OmixSfJ7xqskq7qy3s9v0YcCEzrv7kSvYj5Qx5Qjz/swr78kW+d23lKdSFVgwOMWCd UeWSpabt0NahLcD3JRNRp5LsWr6BP1Vuy7gvEqZlUnfdt5sQOLzBM0mj9AYpsLXkJ4Qq DPuQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=SgFHx2pwghyww+xAC7EIQogJeJRINbxgwrkmYwahL/g=; b=SOM8hpKs27XEXBS9oEOXQe8b9JMmM2VTOYxBxF7L5ee2z4M7PnfXmjm6wHbQ1dSdKn 5eL/ynf7OzRfO9UkZHCH31c/myEO3utDG/NZG79smgyAtkUpdf+aDuXhfLnhIJ9/juo/ gi7isJgvda/wlqTTijBRhxS3Mrq1aigYj7mzP50n2V5pe+7mDOE2aeGVbwJkY+RUXc3B XYPFaxkLcdIhKE+8NQki+ggk102y0Awq/fwsOADUi4W85KiNS8AA8nzJ+k89gz4vFHTM kSCXAPuY5c09Ilybs54A1jHYjEd7ITDJQEGvzR8zpyjY02BUdPpmJ4XDQXiJmBCoATg/ Wyow== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=eq1yzcWr; spf=pass (google.com: domain of sunil@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=sunil@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=SgFHx2pwghyww+xAC7EIQogJeJRINbxgwrkmYwahL/g=; b=lZgFZWRJqleaVBRoTolX02RTXv4FxqsKDAHF6B8/u3KP//GjUNppEruSFVQRYv+xm+ nH5RPvqLGOpYb2xVbAOczyXtbUzfNMUWcJRQVu49JE4hFhLwFdj1fDWloysenCo86pRg KjSgYr97Y8YzUi+izZSYphgu6kFs7qi/Ce1qw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-unsubscribe; bh=SgFHx2pwghyww+xAC7EIQogJeJRINbxgwrkmYwahL/g=; b=agH8Tca6pMoLhx8IFN+54Ynile04C2YeG0p7Bc+wmutjPfD/CPeeXX+6ki96oH9R4M pRzR+kRS31GwAyP2XimE6GjwWVZt+dSujbZELNwYpVEYX5klh1prTyAgEzQyUMS38BWX 9v3H/j3cCs/Ti8am9DjZcmroYhBTU3qAxSTTxWc6QsSadJiZJ8r/cjcqBDJtEVGlYma7 Y6pSBz7a5X54oWqQvS0vc1MZRb0eF5MpI6TeZ83jTXdo9cqybH5YhCEIzFk0sDLX5kEY h4daTOll0zqaNGHIVeuCNp3z5qJDVO/32rEpWXvIx8SAFNn40+WOKEYD4BV+GhU+ASVK aaQw== X-Gm-Message-State: AJIora//epQBiGhkV80BYy7g/y0pU43Ik1RAee+GgX82x6iq9aaI2MV5 Y0p16hgr4E46z/aEUNRIjIklCNa9 X-Google-Smtp-Source: AGRyM1uV/tdbO4JYkwkzqb8XFm8ofvPZrCAFwQdpMVpjQ4y7VEJuS9WXCUd5hYkOqELEqJvAqqj6Pw== X-Received: by 2002:a05:6a00:14c5:b0:52b:8877:8263 with SMTP id w5-20020a056a0014c500b0052b88778263mr22765845pfu.1.1659471376730; Tue, 02 Aug 2022 13:16:16 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:902:d352:b0:16d:2f35:559c with SMTP id l18-20020a170902d35200b0016d2f35559cls9928364plk.6.-pod-prod-gmail; Tue, 02 Aug 2022 13:16:16 -0700 (PDT) X-Received: by 2002:a17:90a:bb96:b0:1f4:414a:d89e with SMTP id v22-20020a17090abb9600b001f4414ad89emr1220233pjr.240.1659471375768; Tue, 02 Aug 2022 13:16:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659471375; cv=none; d=google.com; s=arc-20160816; b=0VRM0dtd1eN89MB2oFMnX1XTUdp8Towkb49krVb7+NkEPOBeeFNSAuVyKdiRkcjJ7g MAY3QbxUAWg3UB2omwOGguklvww8QkDVoDzXKWoHHzwEtCNKpo61SkVa9tlPrjj5Dxsj z61dqx3Xl26AuTF9R/Bp3BlR5nhR1khdnkeddSj2KX6pATNbF4y0BYES/zN/TUujzgob K5LcBiaOsWKTTGKET+Kvd/LPgHDhGYH5Jo048U+6U8j6033sd443wb4s8S6OQzYJJCBp 8St1mfAMvwOWK3Jj0CQXbzL22LGacq9p0tBmTPT1m1/GvGvQgdMabAk5jZvdoDTP+rwV fcNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=/MAG9t6WYn5YwOOz8KJK3JJWRPzE1bp64265jp1tLTQ=; b=eJ+rljIQRIacnRGzDxEhvzm83sQewHJaq9diWWTtQiKJBdm/F39iFJFCsV7jlnkss0 zmcZlpaMwzNe1o82hn4A3npETNA89xNJCIFYq3Mu87+dsEtl/rgFBJnntJRA1x4UMC26 dkJvxXJbnlkWpussEVdNn7lh+XST2em+2lG6hr26ut94NoNeVdqwSTxHAdkxoY6FtI3w NCkc6nz09q+od/QeGB4LczbpZNY1jet/kJtgk3BMf5uHULFn4AM6qXssWoGO9A9u8E3k Xyd0rg1683vIL9nx/qbZsGr4W0nHUJW+aioK+32wuaiaG85TjsDS6irLU6CDxrtQpZ2F wXWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=eq1yzcWr; spf=pass (google.com: domain of sunil@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=sunil@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id k22-20020a170902761600b0016efad0a60esor119052pll.6.2022.08.02.13.16.15 for (Google Transport Security); Tue, 02 Aug 2022 13:16:15 -0700 (PDT) Received-SPF: pass (google.com: domain of sunil@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:a17:902:ce0a:b0:16d:737b:feed with SMTP id k10-20020a170902ce0a00b0016d737bfeedmr22772225plg.152.1659471375383; Tue, 02 Aug 2022 13:16:15 -0700 (PDT) Received: from localhost.localdomain ([183.83.136.224]) by smtp.gmail.com with ESMTPSA id w18-20020a170902a71200b0016ee26224a4sm80863plq.305.2022.08.02.13.16.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Aug 2022 13:16:15 -0700 (PDT) From: Suniel Mahesh To: Felipe Balbi , Greg Kroah-Hartman , Michael Trimarchi , Jagan Teki , Zachary Holland , Daniel McLean , Rene Samson Ambrose , David Sands Cc: linux-kernel@vger.kernel.org, linux-amarula@amarulasolutions.com, USB list Subject: [RFC v2 2/2] HID: ghid: add example program for GET_REPORT Date: Wed, 3 Aug 2022 01:45:56 +0530 Message-Id: <20220802201556.1510069-3-sunil@amarulasolutions.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220802201556.1510069-1-sunil@amarulasolutions.com> References: <20220802201556.1510069-1-sunil@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: sunil@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=eq1yzcWr; spf=pass (google.com: domain of sunil@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=sunil@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This adds a user-space ghid sample get report program at gadget side. This program sends reports from userspace(gadget side) which are saved in the kernel in a list. When a host requests a particular report based on report number and type, corresponding report can be send to host, instead of a zero filled in report. This program can add, delete reports and modify an existing report. Signed-off-by: Suniel Mahesh --- Changes for v2: - no changes --- samples/Kconfig | 10 +++ samples/Makefile | 1 + samples/ghid/Makefile | 4 ++ samples/ghid/test-hid.c | 134 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 samples/ghid/Makefile create mode 100644 samples/ghid/test-hid.c diff --git a/samples/Kconfig b/samples/Kconfig index 470ee3baf2e1..f3d7873bb966 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -86,6 +86,16 @@ config SAMPLE_FPROBE This builds a fprobe example module. This module has an option 'symbol'. You can specify a probed symbol or symbols separated with ','. +config SAMPLE_GHID_GET_REPORT + bool "GHID sample get report" + depends on CC_CAN_LINK && HEADERS_INSTALL + help + Build GHID sample get report program. This program can send reports from + userspace(gadget side) which are saved in the kernel in a linked list. + When a host requests a particular report based on report number and type, + corresponding report can be send to host, instead of a zero filled in report. + This program can add, delete reports and modify an existing report. + config SAMPLE_KFIFO tristate "Build kfifo examples -- loadable modules only" depends on m diff --git a/samples/Makefile b/samples/Makefile index 701e912ab5af..1d58f7a0381e 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -6,6 +6,7 @@ subdir-$(CONFIG_SAMPLE_ANDROID_BINDERFS) += binderfs obj-$(CONFIG_SAMPLE_CONFIGFS) += configfs/ obj-$(CONFIG_SAMPLE_CONNECTOR) += connector/ obj-$(CONFIG_SAMPLE_FANOTIFY_ERROR) += fanotify/ +subdir-$(SAMPLE_GHID_GET_REPORT) += ghid subdir-$(CONFIG_SAMPLE_HIDRAW) += hidraw obj-$(CONFIG_SAMPLE_HW_BREAKPOINT) += hw_breakpoint/ obj-$(CONFIG_SAMPLE_KDB) += kdb/ diff --git a/samples/ghid/Makefile b/samples/ghid/Makefile new file mode 100644 index 000000000000..8c93ded625c1 --- /dev/null +++ b/samples/ghid/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-only +userprogs-always-y += test-hid + +userccflags += -I usr/include diff --git a/samples/ghid/test-hid.c b/samples/ghid/test-hid.c new file mode 100644 index 000000000000..f76786509047 --- /dev/null +++ b/samples/ghid/test-hid.c @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * usb hidg GET_REPORT example, device/gadget side + * This program tests the newly implemented GET_REPORT feature + * + * Copyright (c) 2022 Amarula Solutions India PVT LTD + * + * Authors: + * Copyright (c) 2022 Suniel Mahesh + * + */ + +#include +/* Linux */ +#include +#include +#include +#include +#include + +/* Unix */ +#include +#include +#include +#include +#include + +/* C */ +#include +#include +#include +#include +#include + +/* + * fix for failing compilation on systems that don't + * yet populate new version of uapi/linux/usb/g_hid.h + * to userspace. + */ +#define GADGET_ADD_REPORT_STATUS _IOWR('g', 0x41, struct uhid_set_report_req) +#define GADGET_REMOVE_REPORT_STATUS _IOWR('g', 0x42, struct uhid_get_report_req) +#define GADGET_UPDATE_REPORT_STATUS _IOWR('g', 0x43, struct uhid_set_report_req) + +struct uhid_set_report_req *create_report(void) +{ + struct uhid_set_report_req *rep; + int i; + + rep = (struct uhid_set_report_req *)calloc(1, sizeof(struct uhid_set_report_req)); + if (rep == NULL) { + perror("calloc() failed"); + exit(EXIT_FAILURE); + } + + printf("enter report id:\n"); + scanf("%u", &rep->id); + printf("enter report number:\n"); + scanf("%hhu", &rep->rnum); + printf("enter report type:\n"); + scanf("%hhu", &rep->rtype); + printf("enter report size:\n"); + scanf("%hu", &rep->size); + printf("enter report data:\n"); + + for (i = 0; i < rep->size; i++) + scanf("%hhu", &rep->data[i]); + + return rep; +} + +int main(int argc, char **argv) +{ + const char *filename = NULL; + struct uhid_set_report_req *report; + int fd = 0, res = 0, i, reports; + + if (argc < 1) { + fprintf(stderr, "Usage: %s /dev/hidg0\n", argv[0]); + return -1; + } + + filename = argv[1]; + fd = open(filename, O_RDWR, 0666); + + if (fd == -1) { + perror(filename); + return -2; + } + + printf("enter no of reports to send from userspace:\n"); + scanf("%d", &reports); + + if (reports == 0) + goto out; + + for (i = 0; i < reports; i++) { + report = create_report(); +/* send reports to device */ + res = ioctl(fd, GADGET_ADD_REPORT_STATUS, report); + if (res < 0) { + perror("GADGET_ADD_REPORT_STATUS"); + res = -3; + goto test_end; + } + free(report); + } + +/* delete report with report number specified */ + printf("deleting report w.r.t rtype and rnum:\n"); + report = create_report(); + res = ioctl(fd, GADGET_REMOVE_REPORT_STATUS, report); + if (res < 0) { + perror("GADGET_REMOVE_REPORT_STATUS"); + res = -4; + goto test_end; + } + free(report); + +/* modify an existing report identified by report number */ + printf("modify report w.r.t rtype and rnum:\n"); + report = create_report(); + res = ioctl(fd, GADGET_UPDATE_REPORT_STATUS, report); + if (res < 0) { + perror("GADGET_UPDATE_REPORT_STATUS"); + res = -5; + } + +test_end: + free(report); + report = NULL; +out: + close(fd); + return res; +}