Linux USB
 help / color / mirror / Atom feed
From: Chanh Nguyen <chanh@os.amperecomputing.com>
To: OpenBMC Maillist <openbmc@lists.ozlabs.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
	Dan Vacura <w36195@motorola.com>,
	Jakob Koschel <jakobkoschel@gmail.com>,
	Alan Stern <stern@rowland.harvard.edu>,
	Vijayavardhan Vennapusa <vvreddy@codeaurora.org>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Open Source Submission <patches@amperecomputing.com>
Cc: Chanh Nguyen <chanh@os.amperecomputing.com>
Subject: [PATCH] USB: gadget: Add ID numbers to configfs-gadget driver names
Date: Tue, 13 Dec 2022 11:12:03 +0700	[thread overview]
Message-ID: <20221213041203.21080-1-chanh@os.amperecomputing.com> (raw)

It is unable to use configfs to attach more than one gadget. When
attaching the second gadget, it always fails and the kernel message
prints out:

Error: Driver 'configfs-gadget' is already registered, aborting...
UDC core: g1: driver registration failed: -16

This commit fixes the problem by a ".N" suffix added to each
configfs_gadget's driver name (where N is a unique ID number),
thus making the names distinct.

Fixes: fc274c1e9973 ("USB: gadget: Add a new bus for gadgets")
Signed-off-by: Chanh Nguyen <chanh@os.amperecomputing.com>
---
 drivers/usb/gadget/configfs.c | 42 +++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 96121d1c8df4..d8c5156ad777 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -3,6 +3,7 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/device.h>
+#include <linux/idr.h>
 #include <linux/kstrtox.h>
 #include <linux/nls.h>
 #include <linux/usb/composite.h>
@@ -11,6 +12,16 @@
 #include "u_f.h"
 #include "u_os_desc.h"
 
+static DEFINE_IDA(driver_id_numbers);
+
+/*
+ * Driver name has the form of "configfs-gadget.%d", where %d
+ * is id allocated by ida_alloc(). The max value returns by
+ * ida_alloc() is INT_MAX, in 64-bit system, it is about nine
+ * quintillion: 19 digits in decimal. Set the max length to 35.
+ */
+#define DRIVER_NAME_LENGTH_MAX 35
+
 int check_user_usb_string(const char *name,
 		struct usb_gadget_strings *stringtab_dev)
 {
@@ -52,6 +63,9 @@ struct gadget_info {
 	char qw_sign[OS_STRING_QW_SIGN_LEN];
 	spinlock_t spinlock;
 	bool unbind;
+
+	/* Make driver names unique */
+	int driver_id_number;
 };
 
 static inline struct gadget_info *to_gadget_info(struct config_item *item)
@@ -1582,6 +1596,8 @@ static struct config_group *gadgets_make(
 		const char *name)
 {
 	struct gadget_info *gi;
+	char *driver_name;
+	int ret;
 
 	gi = kzalloc(sizeof(*gi), GFP_KERNEL);
 	if (!gi)
@@ -1623,6 +1639,21 @@ static struct config_group *gadgets_make(
 
 	gi->composite.gadget_driver = configfs_driver_template;
 
+	ret = ida_alloc(&driver_id_numbers, GFP_KERNEL);
+	if (ret < 0)
+		goto err;
+	gi->driver_id_number = ret;
+
+	driver_name = kmalloc(DRIVER_NAME_LENGTH_MAX, GFP_KERNEL);
+	if (!driver_name)
+		goto out_free_driver_id_number;
+
+	ret = scnprintf(driver_name, DRIVER_NAME_LENGTH_MAX,
+			"configfs-gadget.%d", gi->driver_id_number);
+	if (ret < 0)
+		goto out_free_driver_name;
+
+	gi->composite.gadget_driver.driver.name = driver_name;
 	gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
 	gi->composite.name = gi->composite.gadget_driver.function;
 
@@ -1630,6 +1661,11 @@ static struct config_group *gadgets_make(
 		goto err;
 
 	return &gi->group;
+
+out_free_driver_name:
+	kfree(driver_name);
+out_free_driver_id_number:
+	ida_free(&driver_id_numbers, gi->driver_id_number);
 err:
 	kfree(gi);
 	return ERR_PTR(-ENOMEM);
@@ -1637,6 +1673,12 @@ static struct config_group *gadgets_make(
 
 static void gadgets_drop(struct config_group *group, struct config_item *item)
 {
+	struct gadget_info *gi = to_gadget_info(item);
+
+	mutex_lock(&gi->lock);
+	kfree(gi->composite.gadget_driver.driver.name);
+	ida_free(&driver_id_numbers, gi->driver_id_number);
+	mutex_unlock(&gi->lock);
 	config_item_put(item);
 }
 
-- 
2.17.1


             reply	other threads:[~2022-12-13  4:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-13  4:12 Chanh Nguyen [this message]
2022-12-13  7:22 ` [PATCH] USB: gadget: Add ID numbers to configfs-gadget driver names Christophe JAILLET
2022-12-14  4:15   ` Chanh Nguyen
2022-12-14  4:20     ` [EXT] " Frank Li
2022-12-15  9:08       ` Chanh Nguyen
2022-12-15 16:15         ` Frank Li
2022-12-14 15:24     ` Alan Stern
2022-12-19 10:12       ` Chanh Nguyen

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=20221213041203.21080-1-chanh@os.amperecomputing.com \
    --to=chanh@os.amperecomputing.com \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=gregkh@linuxfoundation.org \
    --cc=jakobkoschel@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=openbmc@lists.ozlabs.org \
    --cc=patches@amperecomputing.com \
    --cc=stern@rowland.harvard.edu \
    --cc=vvreddy@codeaurora.org \
    --cc=w36195@motorola.com \
    /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