Repository navigation
Conversation
|
Since the default context is made in libusb_init, commit a524555 broke WEAK_AUTHORITY. It looks like the intention of the commit is to move options like WEAK_AUTHORITY and The return block pasted to the end of the function in this PR is redundant with the return statement following it; I might comment it out instead of moving it, with a link to for example this PR in the comment, not sure. #935 also works with this option. |
fdd3633 to
b025282
Compare
|
Thank you for your feedback @xloem (and you're right the return block was redundant, sorry). I've removed it and made the modification so that the behaviour with Renaming |
|
@harbulot @hjelmn I took a different approach to this problem (both approaches actually address different option uses, merge without conflict, and are merged together in my big pr) at master...xloem:weak-authority . The weak_authority variable holds the same information as the new is_set struct member, so I removed the former and replaced it. What do you think of this approach? I'm not set up to test weak_authority so it may have a bug. |
This commit updates libusb_set_option to save the options if setting them on the default context. This ensures the options 1) can be set before libusb_init(NULL, ...), and 2) are honored even if the default context is destroyed and re-created. Signed-off-by: Nathan Hjelm <[email protected]>
|
Ping @harbulot |
|
Ping @xloem |
|
As far as I can tell, yes it is. It looks like the control flow change still prevents no_enumeration from being set. edit: to add more, I could update #874 to not need this PR, since it now checks the default context options directly. But, at present, android support is broken in libusb HEAD, and this PR #942 should fix that. [i have rebased this PR in the next comment] |
|
harbulot#1 , or as a rebase at xloem@9a08754 |
|
usbi.set_backend should be getting called when a context is created. Doesn't matter to me when it happens but that was the idea. Sounds like that did not work as intended. I will take a look at this tomorrow. |
|
I see the issue. The backend set option was getting called after the backend init. That is too late for this use case. I can move the set to before the backend init. |
|
@hjelmn it sounds like you view the issue in terms of the call order to set_backend ... I'm not too familiar with the core code, but this handles a legacy use of set_option before usb_init, that was broken with the redesign. OPTION_NO_DEVICE_DISCOVERY is used before there is any context object. I kind of expect that anybody who's worked with the code would want to migrate off of that, but it would mean changing the init() flow around a lot in linux_usbfs, or providing for passing options to usb_init. Let us know when/if you have a change in place. |
This commit restores the previous behavior with regards to setting some backend options and restores the ability to set default context options before libusb_init. None of the backends use anything set in init in their set_option function so this should be safe with all backends and options. References #942 Signed-off-by: Nathan Hjelm <[email protected]>
|
I submitted my fix. Can you verify that it addresses the issue? |
|
@harbulot are you available to test the alternative fix for this issue you raised? What we need here is to test a |
b025282 to
d04b8c8
Compare
|
Apologies for the delay. I've just tried and rebased/altered the patch I'd initially submitted onto the current master revision (1a90627). This PR should be updated now, it's very short. Essentially, it was still returning too early if --- a/libusb/core.c
+++ b/libusb/core.c
@@ -2208,14 +2208,11 @@ int API_EXPORTED libusb_set_option(libusb_context *ctx,
}
ctx = usbi_get_context(ctx);
- if (NULL == ctx) {
- return LIBUSB_SUCCESS;
- }
switch (option) {
case LIBUSB_OPTION_LOG_LEVEL:
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
- if (!ctx->debug_fixed)
+ if (ctx && !ctx->debug_fixed)
ctx->debug = (enum libusb_log_level)arg;
#endif
break;I can also confirms it also works with the new |
|
It looks like there's still some bug. I think the intended behavior of hjelmn's change was to make the needed set_option call again in libusb_init after the ctx object is set. Now with this PR there are two solutions, which is not harmful and fixes the serious issue, but is a little disordered. Maybe the set_option call is out of order in hjelmn's fix or some other issue with the details of the execution flow. |
|
@harbulot Can you try with just the head (not with this patch)? It should just work as we are caching the options set before libusb_init and applying them before the backend is initialized. That should catch this use case. If that still crashes please post a debug log and I can take a look. |
|
Thanks @hjelmn, I think I've found the cause of the problem. I'm not using the default ( Here is the code I'm using: int r;
libusb_context *ctx = NULL;
r = libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY);
__android_log_print(ANDROID_LOG_DEBUG, TAG,
"libusb_set_option LIBUSB_OPTION_NO_DEVICE_DISCOVERY: %d", r);
r = libusb_init(&ctx);
__android_log_print(ANDROID_LOG_DEBUG, TAG, "libusb_init(&ctx): %d", r);
if (r < 0) {
return r;
}With the latest commit (1a90627), I get this trace (and the app crashes): With the patch in this PR, the app works and I get this trace: (That was because When I change int r;
r = libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY);
__android_log_print(ANDROID_LOG_DEBUG, TAG,
"libusb_set_option LIBUSB_OPTION_NO_DEVICE_DISCOVERY: %d", r);
r = libusb_init(NULL);
__android_log_print(ANDROID_LOG_DEBUG, TAG, "libusb_init(NULL): %d", r);
if (r < 0) {
return r;
}Having looked a bit further into the code, it makes sense, since /* default context should be initialized before calling usbi_dbg */
if (!ctx) {
usbi_default_context = _ctx;
default_context_refcnt = 1;
usbi_dbg(usbi_default_context, "created default context");
for (enum libusb_option option = 0 ; option < LIBUSB_OPTION_MAX ; option++) {
if (LIBUSB_OPTION_LOG_LEVEL == option || !default_context_options[option].is_set) {
continue;
}
r = libusb_set_option(_ctx, option);
if (LIBUSB_SUCCESS != r)
goto err_free_ctx;
}
}I can't find where I've read it, but my understanding was that it was recommended to use a non-default context for Android apps (i.e. initialise a context with I think the main problem in the latest master revision is that there appears to be some ambiguity about the role of
As it stands, it looks like it may be a mix of both: default options that can be set before initialising the default context (and only the default context). What is it actually intended to be? |
|
Ah, I see. Makes sense. I see no reason to apply the default context options set before libusb_init(NULL) to any new context. |
Sorry, not sure I understand what you mean. Are you saying we shouldn't be able to use Should |
|
@harbulot I mean if an option is set on the NULL context before any call to libusb_init it makes sense to apply it to all the following libusb_init() calls regardless of context. Instead of thinking of them as default options instead of default context options. |
… to newly initialized contexts.
d04b8c8 to
0b1b2ba
Compare
|
Thanks @hjelmn, I've just updated the PR. Essentially, in The only reason I've put this condition is to avoid setting the options again if the default context had already been initialised. Does it make sense? I've tried with my test case on Android, and it works. |
|
@hjelmn would it be helpful here if somebody wrote up a dummy backend that can reproduce the issue without running on android? |
|
@mcuee Just to clarify, it seems to be an issue that affects particularly Android, because that's where the usage flow requires options to be configured before initialising a context, but that would generally apply to other platforms too where default context options have to be set, so it's not really Android-specific as such. |
|
In that case, feel free to change your merge request title to not include "Android". |
|
Would this be fine to add to the libusb_set_option documentation? I use the word "created" because it is consistent with the libusb_init() and "Contexts" documentation. In the discussion above it is often said "initialized" which means the same. I mean, there is no "default context" existing before the first libusb_init(NULL). Now, if you want to have one context where you want to skip enumeration (because you use libusb_wrap_sys() anyway) and another context with enumeration, you will have to initialize the latter first, then set LIBUSB_OPTION_NO_DEVICE_DISCOVERY and initialize the former. If these are in independent plug-ins (like the example in the Contexts documentation) this is not ideal. I guess we could make libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, 0) work to turn it off, or we need something more sophisticated to manipulate the default options in general. If we were to start from scratch, I think there should have been a flag option argument to libusb_init(), and the options being bit masks... |
|
Just a note that any of the various forms of this PR appear fine to me to merge into a release. They are small changes that prevent a regression and can be reverted later if the options system is unified or improved further. |
|
@hjelmn I pinged you late september and it's now november; is it all right if something is merged to resolve this regression? |
References #942 Signed-off-by: Tormod Volden <[email protected]>
|
I reviewed this again closely and it seems fine. I took the liberty to adjust some comments and wrote a proper commit message. |
After commit f7084fe the if clause would always be true, so remove it. Thanks to "xloem" for reporting. References #942 Signed-off-by: Tormod Volden <[email protected]>
Following the dicussion in #830, it appears that:
Calling
libusb_init(NULL)on Android doesn't work.We should call
libusb_set_option(NULL, LIBUSB_OPTION_WEAK_AUTHORITY)before callinglibusb_init(&ctx).LIBUSB_OPTION_WEAK_AUTHORITYis effectively used inlinux_usbfs.c, inop_set_option, itself called fromcore.cviausbi_backend.set_option.However, following the changes in commit a524555, it seems that
usbi_backend.set_option(...)is never called if no default context has been set up. In the following code fragment (line 2207):If the default context (
usbi_default_context) isNULLand we calllibusb_set_optionwith aNULLcontext, thenusbi_get_context(ctx)returnsNULL:As a result, in this case, we return from
libusb_set_optionat line 2209 (if (NULL == ctx) { return LIBUSB_SUCCESS; }), sousbi_backend.set_optionis never called, andweak_authorityinlinux_usbfs.cis never set to 1.Moving
if (NULL == ctx) { return LIBUSB_SUCCESS; }after that switch statement seems to fix the problem forLIBUSB_OPTION_WEAK_AUTHORITY.That said, I'm not sure how it affects
LIBUSB_OPTION_USE_USBDK, which is currently treated in the same case asLIBUSB_OPTION_WEAK_AUTHORITYin the switch statement. Althoughop_set_optiondoesn't make use ofctxinlinux_usbfs.cwhen it deals withLIBUSB_OPTION_WEAK_AUTHORITY, it seems thatwindows_set_optioninwindows_common.cmakes use of it.I don't know enough about the implementation details of libusb, but maybe
LIBUSB_OPTION_USE_USBDKandLIBUSB_OPTION_WEAK_AUTHORITYshould be treated separately, perhaps like this: