git: f1d955be2a73 - main - hidraw(4): Implement HIDRAW_GET_DEVICEINFO ioctl

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Sun, 06 Aug 2023 11:51:43 UTC
The branch main has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=f1d955be2a7367ef755d70257c381f83b8367288

commit f1d955be2a7367ef755d70257c381f83b8367288
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2023-08-06 11:51:08 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2023-08-06 11:51:08 +0000

    hidraw(4): Implement HIDRAW_GET_DEVICEINFO ioctl
    
    In commit c77bfaa75051 uhid(4) gained support for ioctl from
    USB_GET_DEVICEINFO. This is used in libraries like libfido2 to
    retrieve information about a device.
    
    This commit adds binary compatible version to hidraw(4).
    
    PR:             264843
    MFC after:      1 month
    Requested by:   grembo
---
 share/man/man4/hidraw.4 |  5 ++++-
 sys/dev/hid/hidraw.c    | 28 +++++++++++++++++++++++-----
 sys/dev/hid/hidraw.h    | 15 +++++++++++++++
 sys/dev/usb/usb_ioctl.h |  3 ++-
 4 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/share/man/man4/hidraw.4 b/share/man/man4/hidraw.4
index 60398c234fb2..09c4ed7f8f7b 100644
--- a/share/man/man4/hidraw.4
+++ b/share/man/man4/hidraw.4
@@ -29,7 +29,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd July 1, 2018
+.Dd August 6, 2023
 .Dt HIDRAW 4
 .Os
 .Sh NAME
@@ -147,6 +147,9 @@ The report data begins from the second byte.
 For devices which do not use numbered reports, the report data begins at the
 first byte.
 This call may fail if the device does not support this feature.
+.It Dv HIDRAW_GET_DEVICEINFO Pq Vt "struct hidraw_device_info"
+Returns information about the device, like vendor ID and product ID.
+This call will not issue any hardware transfers.
 .El
 .Pp
 Linux
diff --git a/sys/dev/hid/hidraw.c b/sys/dev/hid/hidraw.c
index c18d039286ee..fb1b0231d2cd 100644
--- a/sys/dev/hid/hidraw.c
+++ b/sys/dev/hid/hidraw.c
@@ -566,9 +566,10 @@ hidraw_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
 #endif
 	void *buf;
 	struct hidraw_softc *sc;
+	struct hidraw_device_info *hdi;
 	struct hidraw_gen_descriptor *hgd;
 	struct hidraw_report_descriptor *hrd;
-	struct hidraw_devinfo *hdi;
+	struct hidraw_devinfo *hd;
 	const char *devname;
 	uint32_t size;
 	int id, len;
@@ -788,6 +789,23 @@ hidraw_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
 		*(int *)addr = 0;	/* XXX: we only support reportid 0? */
 		return (0);
 
+	case HIDRAW_GET_DEVICEINFO:
+		hdi = (struct hidraw_device_info *)addr;
+		bzero(hdi, sizeof(struct hidraw_device_info));
+		hdi->hdi_product = sc->sc_hw->idProduct;
+		hdi->hdi_vendor = sc->sc_hw->idVendor;
+		hdi->hdi_version = sc->sc_hw->idVersion;
+		hdi->hdi_bustype = sc->sc_hw->idBus;
+		strlcpy(hdi->hdi_name, sc->sc_hw->name,
+		    sizeof(hdi->hdi_name));
+		strlcpy(hdi->hdi_phys, device_get_nameunit(sc->sc_dev),
+		    sizeof(hdi->hdi_phys));
+		strlcpy(hdi->hdi_uniq, sc->sc_hw->serial,
+		    sizeof(hdi->hdi_uniq));
+		snprintf(hdi->hdi_release, sizeof(hdi->hdi_release), "%x.%02x",
+		    sc->sc_hw->idVersion >> 8, sc->sc_hw->idVersion & 0xff);
+		return(0);
+
 	case HIDIOCGRDESCSIZE:
 		*(int *)addr = sc->sc_hw->rdescsize;
 		return (0);
@@ -813,10 +831,10 @@ hidraw_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
 		return (error);
 
 	case HIDIOCGRAWINFO:
-		hdi = (struct hidraw_devinfo *)addr;
-		hdi->bustype = sc->sc_hw->idBus;
-		hdi->vendor = sc->sc_hw->idVendor;
-		hdi->product = sc->sc_hw->idProduct;
+		hd = (struct hidraw_devinfo *)addr;
+		hd->bustype = sc->sc_hw->idBus;
+		hd->vendor = sc->sc_hw->idVendor;
+		hd->product = sc->sc_hw->idProduct;
 		return (0);
 	}
 
diff --git a/sys/dev/hid/hidraw.h b/sys/dev/hid/hidraw.h
index 9aaaa42d2a7c..acaaa67f3ae7 100644
--- a/sys/dev/hid/hidraw.h
+++ b/sys/dev/hid/hidraw.h
@@ -51,6 +51,20 @@ struct hidraw_gen_descriptor {
 	uint8_t reserved[8];
 };
 
+/* Compatible with usb_device_info structure */
+struct hidraw_device_info {
+	uint16_t	hdi_product;
+	uint16_t	hdi_vendor;
+	uint16_t	hdi_version;
+	uint8_t		occupied[18];	/* by usb_device_info */
+	uint16_t	hdi_bustype;
+	uint8_t		reserved[14];	/* leave space for the future */
+	char		hdi_name[128];
+	char		hdi_phys[128];
+	char		hdi_uniq[64];
+	char		hdi_release[8];	/* decrypted USB bcdDevice */
+};
+
 struct hidraw_report_descriptor {
 	uint32_t	size;
 	uint8_t		value[HID_MAX_DESCRIPTOR_SIZE];
@@ -69,6 +83,7 @@ struct hidraw_devinfo {
 #define	HIDRAW_SET_REPORT	_IOW ('U', 24, struct hidraw_gen_descriptor)
 #define	HIDRAW_GET_REPORT_ID	_IOR ('U', 25, int)
 #define	HIDRAW_SET_REPORT_DESC	_IOW ('U', 26, struct hidraw_gen_descriptor)
+#define	HIDRAW_GET_DEVICEINFO	_IOR ('U', 112, struct hidraw_device_info)
 
 /* Linux hidraw-compatible ioctl interface */
 #define	HIDIOCGRDESCSIZE	_IOR('U', 30, int)
diff --git a/sys/dev/usb/usb_ioctl.h b/sys/dev/usb/usb_ioctl.h
index 01cbef6205a1..f80d951ffba7 100644
--- a/sys/dev/usb/usb_ioctl.h
+++ b/sys/dev/usb/usb_ioctl.h
@@ -120,7 +120,8 @@ struct usb_device_info {
 	uint8_t	udi_hubport;		/* parent HUB port */
 	uint8_t	udi_power_mode;		/* see "USB_POWER_MODE_XXX" */
 	uint8_t	udi_suspended;		/* set if device is suspended */
-	uint8_t	udi_reserved[16];	/* leave space for the future */
+	uint16_t udi_bustypeNo;
+	uint8_t	udi_reserved[14];	/* leave space for the future */
 	char	udi_product[128];
 	char	udi_vendor[128];
 	char	udi_serial[64];