From nobody Sun Apr 12 04:21:08 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 4DA80C00140 for ; Fri, 5 Aug 2022 07:05:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240151AbiHEHFx (ORCPT ); Fri, 5 Aug 2022 03:05:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240169AbiHEHFd (ORCPT ); Fri, 5 Aug 2022 03:05:33 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF95D6594 for ; Fri, 5 Aug 2022 00:05:29 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id w10so1945763plq.0 for ; Fri, 05 Aug 2022 00:05:29 -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=J2MAC1RRxVe+1E8IQ0U0gCK1zi4qNZSQc9yqeL3gN+4=; b=NbfxvjgWG69LlH8j2cSswd2Y6HKyHPgrpCtf4kp7syy92Cg7PAPZl3o0RMgAO9A2qp oabMu0+bCsycuH3eBHpjq93yVwYBso5HsPgdrjPLKpvNPfMwgi+CFPnaOSZKu5zw0agM fHsFubjClJkK1BGjRzaUGDqGMJ+8TZqNZLHGo= 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=J2MAC1RRxVe+1E8IQ0U0gCK1zi4qNZSQc9yqeL3gN+4=; b=Hr+VhARHSzGXz1fgNVvEOMa65yCAcPgwN82dAboe7qwKx2whgwv3j5+bp1lqhdgqb7 +B18gkYvH1lhfCzgrchQ9O0smJr63e5vPfi7IpeTQXTXI9j2kwt6BRxC1JFQmZGsGlWW WV4mnaXIi0gvT9HBxPfk4sQZTugbciQt5tmp2lXf6CtFTGKdijcsXebQj2QJNyRsSSN6 opbk2P2PxpOOXRRJGAKe/54YCeaAGXGjgaA30lnyo24nkZScNEma+4ezJtGuoXIanvQG YSXq9KS/960R1NLf/zRfzBrMLTAabahb9xXKi+4WE6GmIblVRLYlXE1Vkjk47LrDMqAE O9Fg== X-Gm-Message-State: ACgBeo0+yDmipt01/nyM+rwU55r0gZPoDYd1aIfkMcj0jzkhCxoG9TX1 lro6KbJsGj3AO//1LLUf/qj/+A== X-Google-Smtp-Source: AA6agR4+kfhOoUeOilvy9n1Ie32ckD1WStk/9cUUQ8LDSDaWk2ty1LkUSjtpukpoOWHOIy9Ih4jL/Q== X-Received: by 2002:a17:902:8b87:b0:16f:1bb7:984a with SMTP id ay7-20020a1709028b8700b0016f1bb7984amr5454865plb.113.1659683129239; Fri, 05 Aug 2022 00:05:29 -0700 (PDT) Received: from localhost.localdomain ([183.83.136.224]) by smtp.gmail.com with ESMTPSA id e60-20020a17090a6fc200b001f262f6f717sm4877484pjk.3.2022.08.05.00.05.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 00:05:29 -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 v3 2/2] HID: ghid: add example program for GET_REPORT Date: Fri, 5 Aug 2022 12:35:07 +0530 Message-Id: <20220805070507.123151-3-sunil@amarulasolutions.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220805070507.123151-1-sunil@amarulasolutions.com> References: <20220805070507.123151-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 --- Changes for v3: - no changes 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 ','. =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