linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniele Barcella <daniele.barcella@pm.me>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Daniele Barcella <daniele.barcella@pm.me>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] usb: modalias: Added a manufacturer string to usb modalias sysfs output
Date: Sun, 06 Jul 2025 14:27:26 +0000	[thread overview]
Message-ID: <20250706142720.8596-1-daniele.barcella@pm.me> (raw)

The patch improves the usb modalias sysfs output by including the manufacturer string ath the end in the format 'mnf%s' after filtering the string, like how it is done for the dmi id device.

For example usb:v0483p5740d0100dcEFdsc02dp01ic02isc02ip00in00mnfFlipperDevicesInc.

This change allows hwdb rules to target devices with the same vid+pid but different manufacturer.
For example, the STMicroelectronics Virtual COM Port (0483:5740) is widely used in both prototyping and production devices that don't have a unique vendor ID.

For further context, refer to the related discussion on systemd's GitHub: https://github.com/systemd/systemd/pull/24869.

Signed-off-by: Daniele Barcella <daniele.barcella@pm.me>
---
 drivers/usb/core/sysfs.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index 23f3cb1989f4..dc191fa94372 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -1154,20 +1154,39 @@ static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_RO(interface);
 
+static void ascii_filter(char *d, const char *s)
+{
+	/* Filter out characters we don't want to see in the modalias string */
+	for (; *s; s++)
+		if (*s > ' ' && *s < 127 && *s != ':')
+			*(d++) = *s;
+
+	*d = 0;
+}
+
 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
 			     char *buf)
 {
 	struct usb_interface *intf;
 	struct usb_device *udev;
 	struct usb_host_interface *alt;
+	char *manufacturer;
+	int emit;
 
 	intf = to_usb_interface(dev);
 	udev = interface_to_usbdev(intf);
 	alt = READ_ONCE(intf->cur_altsetting);
 
-	return sysfs_emit(buf,
+	if (udev->manufacturer) {
+		manufacturer = kmalloc(strlen(udev->manufacturer) + 1, GFP_KERNEL);
+		ascii_filter(manufacturer, udev->manufacturer);
+	} else {
+		manufacturer = kstrdup("", GFP_KERNEL);
+	}
+
+	emit = sysfs_emit(buf,
 			"usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
-			"ic%02Xisc%02Xip%02Xin%02X\n",
+			"ic%02Xisc%02Xip%02Xin%02Xmnf%s\n",
 			le16_to_cpu(udev->descriptor.idVendor),
 			le16_to_cpu(udev->descriptor.idProduct),
 			le16_to_cpu(udev->descriptor.bcdDevice),
@@ -1177,7 +1196,11 @@ static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
 			alt->desc.bInterfaceClass,
 			alt->desc.bInterfaceSubClass,
 			alt->desc.bInterfaceProtocol,
-			alt->desc.bInterfaceNumber);
+			alt->desc.bInterfaceNumber,
+			manufacturer
+		);
+	kfree(manufacturer);
+	return emit;
 }
 static DEVICE_ATTR_RO(modalias);
 
-- 
2.47.2



             reply	other threads:[~2025-07-06 14:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-06 14:27 Daniele Barcella [this message]
2025-07-07  8:00 ` [PATCH] usb: modalias: Added a manufacturer string to usb modalias sysfs output Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250706142720.8596-1-daniele.barcella@pm.me \
    --to=daniele.barcella@pm.me \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).