From nobody Sun Apr 12 02:46:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 112CEC00144 for ; Mon, 1 Aug 2022 12:45:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232527AbiHAMpH (ORCPT ); Mon, 1 Aug 2022 08:45:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234879AbiHAMoX (ORCPT ); Mon, 1 Aug 2022 08:44:23 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25C3DB7F1 for ; Mon, 1 Aug 2022 05:30:26 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id y15so10288882plp.10 for ; Mon, 01 Aug 2022 05:30:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=7o91HERS/4hsq8FU5w5VEekCf+JsdrZ+o3ocEJkypqs=; b=GgGkvY84gXaQc+2I8pszEPJbyM4Ph2Rk/lwEbg3egwKN3HIfpLN7OyZ/i/YDfcPl6Z fQxZDJSpfpdSaPa7fEpmor+CT3V24bppJn4iLeVXMX8nERJO2m8I2baX36bag5fTkgjL G3bcGRgypZk7tIy9W8QbrcMpilAp8Qrf712Cg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=7o91HERS/4hsq8FU5w5VEekCf+JsdrZ+o3ocEJkypqs=; b=eI4tcwGHa84d9XrBNMaLaQFR2XyF0b7lAQfEuCUH3CSD1Bs5Z1yNlzf3YmFLMg5HM6 YtLk7c/fgXcsox71Pi6OqL+gYD11NAfVlGgWJTRiPEAQRRIOYT5zE4xg+Tn5symh2obl N12LbOS4xsmaIM21+yw/wlNrWsbTN/kkjbx5NOat91NGxGiLOOEAs/j0SYO/lb5NoZCJ lns3PXtkNgAYkF2+troliBiYG4gwI9SAprjznWg0XDAlKFioFTipJAR9ZWHFLpO8aq3o +hL39Jv/1Temlbrz9RuME+pLqztfPMighq/PeeCGOzSc7RSd0dfIhgPQlPwwDWO3KY9Q hEbg== X-Gm-Message-State: ACgBeo30+7m8qqeBTIamzMYxnX41D/uVQaHvLA+xGnfJd5KhLTpIyT4P 9OzPlYlhl3xl20/iMalL8tuZUA== X-Google-Smtp-Source: AA6agR7q2oQCzE2Dj6UzhPjXnAKFHMShOervH2Te1LPRNyiHIDscNEEurFTMdVRTsB9p2/ykkVHQJQ== X-Received: by 2002:a17:90b:3c1:b0:1f3:17bd:2e5b with SMTP id go1-20020a17090b03c100b001f317bd2e5bmr19069861pjb.107.1659357025540; Mon, 01 Aug 2022 05:30:25 -0700 (PDT) Received: from localhost.localdomain ([183.83.136.224]) by smtp.gmail.com with ESMTPSA id a14-20020a1709027e4e00b0015e8d4eb219sm3473230pln.99.2022.08.01.05.30.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Aug 2022 05:30:25 -0700 (PDT) From: Suniel Mahesh To: Felipe Balbi , Greg Kroah-Hartman , Michael Trimarchi , Jagan Teki Cc: linux-kernel@vger.kernel.org, linux-amarula@amarulasolutions.com, USB list Subject: [RFC 1/2] usb: gadget: f_hid: Extend functionality for GET_REPORT mode Date: Mon, 1 Aug 2022 18:00:09 +0530 Message-Id: <20220801123010.2984864-2-sunil@amarulasolutions.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220801123010.2984864-1-sunil@amarulasolutions.com> References: <20220801123010.2984864-1-sunil@amarulasolutions.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The current kernel implementation for GET_REPORT is that the kernel sends back a zero filled report (of length =3D=3D 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 --- drivers/usb/gadget/function/f_hid.c | 166 ++++++++++++++++++++++++++- include/{ =3D> uapi}/linux/usb/g_hid.h | 10 ++ 2 files changed, 174 insertions(+), 2 deletions(-) rename include/{ =3D> uapi}/linux/usb/g_hid.h (72%) diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/funct= ion/f_hid.c index ca0a7d9eaa34..70787c73caf9 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 =20 #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 */ =20 +struct report_entry { + struct uhid_set_report_req report_data; + struct list_head node; +}; + /*------------------------------------------------------------------------= -*/ /* HID gadget struct = */ =20 @@ -71,6 +77,10 @@ struct f_hidg { wait_queue_head_t write_queue; struct usb_request *req; =20 + /* hid report list */ + spinlock_t report_spinlock; + struct list_head report_list; + int minor; struct cdev cdev; struct usb_function func; @@ -553,6 +563,136 @@ static int f_hidg_open(struct inode *inode, struct fi= le *fd) return 0; } =20 +static bool f_hidg_param_valid(struct report_entry *entry) +{ + + if (entry->report_data.size > UHID_DATA_MAX) + return false; + + switch (entry->report_data.rtype) { + 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 =3D list_entry(ptr, struct report_entry, node); + if (entry->report_data.rnum =3D=3D rnum && + entry->report_data.rtype =3D=3D rtype) { + return entry; + } + } + + return NULL; +} + +static long f_hidg_ioctl(struct file *file, unsigned int cmd, unsigned lon= g arg) +{ + struct f_hidg *hidg =3D 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 =3D kmalloc(sizeof(*entry), GFP_KERNEL); + if (!entry) + return -ENOMEM; + + if (copy_from_user(&entry->report_data, (struct uhid_set_report_req *)ar= g, + sizeof(struct uhid_set_report_req))) { + kfree(entry); + return -EFAULT; + } + + if (f_hidg_param_valid(entry) =3D=3D false) { + kfree(entry); + return -EINVAL; + } + + spin_lock_irqsave(&hidg->report_spinlock, flags); + ptr =3D 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(re= port))) + return -EFAULT; + + spin_lock_irqsave(&hidg->report_spinlock, flags); + + ptr =3D 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(re= port))) + 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 =3D 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 = */ =20 @@ -634,6 +774,8 @@ static int hidg_setup(struct usb_function *f, struct f_hidg *hidg =3D func_to_hidg(f); struct usb_composite_dev *cdev =3D f->config->cdev; struct usb_request *req =3D cdev->req; + struct report_entry *entry; + unsigned long flags; int status =3D 0; __u16 value, length; =20 @@ -649,9 +791,25 @@ static int hidg_setup(struct usb_function *f, | HID_REQ_GET_REPORT): VDBG(cdev, "get_report\n"); =20 - /* send an empty report */ length =3D 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 =3D f_hidg_search_for_report(hidg, value & 0xf, + value >> 8); + + /* send a report */ + if (entry) { + length =3D 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); =20 goto respond; break; @@ -893,6 +1051,7 @@ static const struct file_operations f_hidg_fops =3D { .owner =3D THIS_MODULE, .open =3D f_hidg_open, .release =3D f_hidg_release, + .unlocked_ioctl =3D f_hidg_ioctl, .write =3D f_hidg_write, .read =3D f_hidg_read, .poll =3D f_hidg_poll, @@ -997,6 +1156,9 @@ static int hidg_bind(struct usb_configuration *c, stru= ct usb_function *f) init_waitqueue_head(&hidg->read_queue); INIT_LIST_HEAD(&hidg->completed_out_req); =20 + spin_lock_init(&hidg->report_spinlock); + INIT_LIST_HEAD(&hidg->report_list); + /* create char device */ cdev_init(&hidg->cdev, &f_hidg_fops); dev =3D 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 =20 +#include + struct hidg_func_descriptor { unsigned char subclass; unsigned char protocol; @@ -30,4 +32,12 @@ struct hidg_func_descriptor { unsigned char report_desc[]; }; =20 +/* 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_r= eq) +#define GADGET_REMOVE_REPORT_STATUS _IOWR('g', 0x42, struct uhid_get_repor= t_req) +#define GADGET_UPDATE_REPORT_STATUS _IOWR('g', 0x43, struct uhid_set_repor= t_req) + #endif /* __LINUX_USB_G_HID_H */ --=20 2.25.1 From nobody Sun Apr 12 02:46:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35CE4C00144 for ; Mon, 1 Aug 2022 12:45:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234773AbiHAMpK (ORCPT ); Mon, 1 Aug 2022 08:45:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234884AbiHAMoX (ORCPT ); Mon, 1 Aug 2022 08:44:23 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFF3BBC9F for ; Mon, 1 Aug 2022 05:30:28 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id u133so3801350pfc.10 for ; Mon, 01 Aug 2022 05:30:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=79wsiIuqmJHlDnyKBpsU0dcqa0Z1I6VJ8b46FtTPbYM=; b=jTNNzOOmgHHd+AvOuz5rDsU21f94l1AkStN4gk6xkuFxwap3k1ovDoQSmfuCITnqIq S48kXaiIvPUki3wUKChY6ECaHwx1fkG6hNtLes5dlpzieakVBHqRQ6ZutqBA0ToF1IKd LKHZdX6WB7fJ4rjYN7gO4iYDF/KPBmcFzdKt4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=79wsiIuqmJHlDnyKBpsU0dcqa0Z1I6VJ8b46FtTPbYM=; b=iDzvl0P+zy5T5Q30HgjOWkedm9pqbJt1WBA87TR9CVcTB/aASpTMlaLr9vagNOwQRp aTh+0Ii07UCl47clLw9yz7mfx+KaqmZp49D27oK8Kjg+kEJVSC6d+mI7EfrPE/66r5e6 2mbpTjF9F1hjGwOO2sOWTXIEq4yleRlYTd4B1XHKopP0F5EKg2KlF84+3qb3oy1XqpWI /1V/E94TQQfdwigUfeQN+018o2k3BcFNIiFtnVd++W5YL8wsjV9SkVaRP+DaI4+GgboY VUf4E8BwL1pjNeivF1PiMcJ/xIBe7PU0gw8J0B0/XKlb3SAeld8FY+NYu1TjRR4TNTxU ySOw== X-Gm-Message-State: ACgBeo03Uj7ct5bM24rAV597YirkD3xOhwv4PS0sKpxOGl6+z3jaYlyx v3jodLoHUZqJ5SYlH3eDaT4ugQ== X-Google-Smtp-Source: AA6agR5IZ0ynGGVF4vFaonoCeVUtJtBEWv5+dEytr+8vY2V3PghKfQwt4/ksjGBBZ/CuKYf5jjacRA== X-Received: by 2002:a05:6a00:190b:b0:52c:eb0e:e1b6 with SMTP id y11-20020a056a00190b00b0052ceb0ee1b6mr12456249pfi.54.1659357028121; Mon, 01 Aug 2022 05:30:28 -0700 (PDT) Received: from localhost.localdomain ([183.83.136.224]) by smtp.gmail.com with ESMTPSA id a14-20020a1709027e4e00b0015e8d4eb219sm3473230pln.99.2022.08.01.05.30.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Aug 2022 05:30:27 -0700 (PDT) From: Suniel Mahesh To: Felipe Balbi , Greg Kroah-Hartman , Michael Trimarchi , Jagan Teki Cc: linux-kernel@vger.kernel.org, linux-amarula@amarulasolutions.com, USB list Subject: [RFC 2/2] HID: ghid: add example program for GET_REPORT Date: Mon, 1 Aug 2022 18:00:10 +0530 Message-Id: <20220801123010.2984864-3-sunil@amarulasolutions.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220801123010.2984864-1-sunil@amarulasolutions.com> References: <20220801123010.2984864-1-sunil@amarulasolutions.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- 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 ','. =20 +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 repo= rts 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 typ= e, + corresponding report can be send to host, instead of a zero filled in r= eport. + 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) +=3D binderfs obj-$(CONFIG_SAMPLE_CONFIGFS) +=3D configfs/ obj-$(CONFIG_SAMPLE_CONNECTOR) +=3D connector/ obj-$(CONFIG_SAMPLE_FANOTIFY_ERROR) +=3D fanotify/ +subdir-$(SAMPLE_GHID_GET_REPORT) +=3D ghid subdir-$(CONFIG_SAMPLE_HIDRAW) +=3D hidraw obj-$(CONFIG_SAMPLE_HW_BREAKPOINT) +=3D hw_breakpoint/ obj-$(CONFIG_SAMPLE_KDB) +=3D 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 +=3D test-hid + +userccflags +=3D -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_r= eq) +#define GADGET_REMOVE_REPORT_STATUS _IOWR('g', 0x42, struct uhid_get_repor= t_req) +#define GADGET_UPDATE_REPORT_STATUS _IOWR('g', 0x43, struct uhid_set_repor= t_req) + +struct uhid_set_report_req *create_report(void) +{ + struct uhid_set_report_req *rep; + int i; + + rep =3D (struct uhid_set_report_req *)calloc(1, sizeof(struct uhid_set_re= port_req)); + if (rep =3D=3D 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 =3D 0; i < rep->size; i++) + scanf("%hhu", &rep->data[i]); + + return rep; +} + +int main(int argc, char **argv) +{ + const char *filename =3D NULL; + struct uhid_set_report_req *report; + int fd =3D 0, res =3D 0, i, reports; + + if (argc < 1) { + fprintf(stderr, "Usage: %s /dev/hidg0\n", argv[0]); + return -1; + } + + filename =3D argv[1]; + fd =3D open(filename, O_RDWR, 0666); + + if (fd =3D=3D -1) { + perror(filename); + return -2; + } + + printf("enter no of reports to send from userspace:\n"); + scanf("%d", &reports); + + if (reports =3D=3D 0) + goto out; + + for (i =3D 0; i < reports; i++) { + report =3D create_report(); +/* send reports to device */ + res =3D ioctl(fd, GADGET_ADD_REPORT_STATUS, report); + if (res < 0) { + perror("GADGET_ADD_REPORT_STATUS"); + res =3D -3; + goto test_end; + } + free(report); + } + +/* delete report with report number specified */ + printf("deleting report w.r.t rtype and rnum:\n"); + report =3D create_report(); + res =3D ioctl(fd, GADGET_REMOVE_REPORT_STATUS, report); + if (res < 0) { + perror("GADGET_REMOVE_REPORT_STATUS"); + res =3D -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 =3D create_report(); + res =3D ioctl(fd, GADGET_UPDATE_REPORT_STATUS, report); + if (res < 0) { + perror("GADGET_UPDATE_REPORT_STATUS"); + res =3D -5; + } + +test_end: + free(report); + report =3D NULL; +out: + close(fd); + return res; +} --=20 2.25.1