* [PATCH 1/2] usbgadget: fix error code in common code base
@ 2024-05-21 8:06 Marco Felsch
2024-05-21 8:06 ` [PATCH 2/2] usbgadget: split usbgadget_register into prepare and register Marco Felsch
2024-05-21 11:07 ` [PATCH 1/2] usbgadget: fix error code in common code base Sascha Hauer
0 siblings, 2 replies; 3+ messages in thread
From: Marco Felsch @ 2024-05-21 8:06 UTC (permalink / raw)
To: barebox
Don't use command error codes in the common code base. Instead the
commands should convert common error codes into command error codes.
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
commands/dfu.c | 2 +-
commands/usbgadget.c | 5 +++--
common/usbgadget.c | 2 +-
3 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/commands/dfu.c b/commands/dfu.c
index 2116747f6885..bbe75841b914 100644
--- a/commands/dfu.c
+++ b/commands/dfu.c
@@ -31,7 +31,7 @@ static int do_dfu(int argc, char *argv[])
funcs.dfu_opts = argv[optind];
ret = usbgadget_register(&funcs);
if (ret)
- return ret;
+ return COMMAND_ERROR_USAGE;
command_slice_release();
while (!usb_dfu_detached()) {
diff --git a/commands/usbgadget.c b/commands/usbgadget.c
index e69660fde541..736ccb5d9185 100644
--- a/commands/usbgadget.c
+++ b/commands/usbgadget.c
@@ -20,6 +20,7 @@ static int do_usbgadget(int argc, char *argv[])
{
struct usbgadget_funcs funcs = {};
int opt;
+ int ret;
while ((opt = getopt(argc, argv, "asdA::D::S::b")) > 0) {
switch (opt) {
@@ -50,8 +51,8 @@ static int do_usbgadget(int argc, char *argv[])
}
}
-
- return usbgadget_register(&funcs);
+ ret = usbgadget_register(&funcs);
+ return ret ? COMMAND_ERROR_USAGE : 0;
}
BAREBOX_CMD_HELP_START(usbgadget)
diff --git a/common/usbgadget.c b/common/usbgadget.c
index 371355116364..9974be57d1cb 100644
--- a/common/usbgadget.c
+++ b/common/usbgadget.c
@@ -71,7 +71,7 @@ int usbgadget_register(const struct usbgadget_funcs *funcs)
if (usb_multi_count_functions(opts) == 0) {
pr_warn("No functions to register\n");
- ret = COMMAND_ERROR_USAGE;
+ ret = -EINVAL;
goto err;
}
--
2.39.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] usbgadget: split usbgadget_register into prepare and register
2024-05-21 8:06 [PATCH 1/2] usbgadget: fix error code in common code base Marco Felsch
@ 2024-05-21 8:06 ` Marco Felsch
2024-05-21 11:07 ` [PATCH 1/2] usbgadget: fix error code in common code base Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Marco Felsch @ 2024-05-21 8:06 UTC (permalink / raw)
To: barebox
Currently usbgadget_register() prepares the struct::f_multi_opts
according a given configuration string and registers the the usbgadget
device.
This is not optimal for adding custom fastboot_opts. Split the single
function into usbgadget_prepare() and usbgadget_register() to avoid code
duplication. This way it's easy to reuse the preparation code and add
the custom fastboot_opts afterwards.
In addition usbgadget_prepare_register() is added to keep the simplicity
of the single prepare and register functionality.
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
commands/dfu.c | 2 +-
commands/usbgadget.c | 2 +-
common/usbgadget.c | 36 ++++++++++++++++++++++++--------
include/linux/usb/gadget-multi.h | 4 +++-
4 files changed, 32 insertions(+), 12 deletions(-)
diff --git a/commands/dfu.c b/commands/dfu.c
index bbe75841b914..ab784c638200 100644
--- a/commands/dfu.c
+++ b/commands/dfu.c
@@ -29,7 +29,7 @@ static int do_dfu(int argc, char *argv[])
funcs.flags |= USBGADGET_DFU;
funcs.dfu_opts = argv[optind];
- ret = usbgadget_register(&funcs);
+ ret = usbgadget_prepare_register(&funcs);
if (ret)
return COMMAND_ERROR_USAGE;
diff --git a/commands/usbgadget.c b/commands/usbgadget.c
index 736ccb5d9185..1cd8c915b1b1 100644
--- a/commands/usbgadget.c
+++ b/commands/usbgadget.c
@@ -51,7 +51,7 @@ static int do_usbgadget(int argc, char *argv[])
}
}
- ret = usbgadget_register(&funcs);
+ ret = usbgadget_prepare_register(&funcs);
return ret ? COMMAND_ERROR_USAGE : 0;
}
diff --git a/common/usbgadget.c b/common/usbgadget.c
index 9974be57d1cb..1333eaa413ea 100644
--- a/common/usbgadget.c
+++ b/common/usbgadget.c
@@ -31,11 +31,9 @@ static inline struct file_list *get_dfu_function(void)
return system_partitions_get_null();
}
-int usbgadget_register(const struct usbgadget_funcs *funcs)
+struct f_multi_opts *usbgadget_prepare(const struct usbgadget_funcs *funcs)
{
- int ret;
int flags = funcs->flags;
- struct device *dev;
struct f_multi_opts *opts;
opts = xzalloc(sizeof(*opts));
@@ -71,10 +69,18 @@ int usbgadget_register(const struct usbgadget_funcs *funcs)
if (usb_multi_count_functions(opts) == 0) {
pr_warn("No functions to register\n");
- ret = -EINVAL;
- goto err;
+ usb_multi_opts_release(opts);
+ return ERR_PTR(-EINVAL);
}
+ return opts;
+}
+
+int usbgadget_register(struct f_multi_opts *opts)
+{
+ int ret;
+ struct device *dev;
+
/*
* Creating a gadget with both DFU and Fastboot may not work.
* fastboot 1:8.1.0+r23-5 can deal with it, but dfu-util 0.9
@@ -90,11 +96,23 @@ int usbgadget_register(const struct usbgadget_funcs *funcs)
ret = usb_multi_register(opts);
if (ret)
- goto err;
+ return ret;
return 0;
-err:
- usb_multi_opts_release(opts);
+}
+
+int usbgadget_prepare_register(const struct usbgadget_funcs *funcs)
+{
+ struct f_multi_opts *opts;
+ int ret;
+
+ opts = usbgadget_prepare(funcs);
+ if (IS_ERR(opts))
+ return PTR_ERR(opts);
+
+ ret = usbgadget_register(opts);
+ if (ret)
+ usb_multi_opts_release(opts);
return ret;
}
@@ -122,7 +140,7 @@ static int usbgadget_do_autostart(void)
funcs.flags |= USBGADGET_DFU | USBGADGET_FASTBOOT | USBGADGET_MASS_STORAGE;
- err = usbgadget_register(&funcs);
+ err = usbgadget_prepare_register(&funcs);
if (!err)
started = true;
diff --git a/include/linux/usb/gadget-multi.h b/include/linux/usb/gadget-multi.h
index 1027a10082e0..14aa609a5895 100644
--- a/include/linux/usb/gadget-multi.h
+++ b/include/linux/usb/gadget-multi.h
@@ -35,7 +35,9 @@ struct usbgadget_funcs {
const char *ums_opts;
};
-int usbgadget_register(const struct usbgadget_funcs *funcs);
+struct f_multi_opts *usbgadget_prepare(const struct usbgadget_funcs *funcs);
+int usbgadget_register(struct f_multi_opts *opts);
+int usbgadget_prepare_register(const struct usbgadget_funcs *funcs);
void usbgadget_autostart(bool enable);
--
2.39.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] usbgadget: fix error code in common code base
2024-05-21 8:06 [PATCH 1/2] usbgadget: fix error code in common code base Marco Felsch
2024-05-21 8:06 ` [PATCH 2/2] usbgadget: split usbgadget_register into prepare and register Marco Felsch
@ 2024-05-21 11:07 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-05-21 11:07 UTC (permalink / raw)
To: barebox, Marco Felsch
On Tue, 21 May 2024 10:06:16 +0200, Marco Felsch wrote:
> Don't use command error codes in the common code base. Instead the
> commands should convert common error codes into command error codes.
>
>
Applied, thanks!
[1/2] usbgadget: fix error code in common code base
https://git.pengutronix.de/cgit/barebox/commit/?id=41276fd07089 (link may not be stable)
[2/2] usbgadget: split usbgadget_register into prepare and register
https://git.pengutronix.de/cgit/barebox/commit/?id=ef3c7fac172c (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-05-21 11:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-21 8:06 [PATCH 1/2] usbgadget: fix error code in common code base Marco Felsch
2024-05-21 8:06 ` [PATCH 2/2] usbgadget: split usbgadget_register into prepare and register Marco Felsch
2024-05-21 11:07 ` [PATCH 1/2] usbgadget: fix error code in common code base Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox