linux: fix topology

Find the parent dev by removing the last number after the . or -
(and prepending with usb if only one number is left) and comparing
it against the sysfs_dir or previously scanned devices.

HdG: Drop hardcoded special casing of root-hubs, this is fixed in a better
way in a later commit.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
configure: Fix --enable-udev help text

We use AC_ARG_ENABLE so the help string should be --enable-udev, so that
the user knows to pass --disable-udev to disable udev

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux: linux_parent_dev search for . from the right

If hubs are chained to other hubs there will be multiple '.' characters, and
want the last one.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux: Take hotplug_lock for scan_devices, hotplug_enumerate and _disconnect

As soon as we've started listening for hp events, hotplug_enumerate and
_disconnect can run, they do test then add / remove operations on the
device list. This can race with scan_devices adding devices, so take
the lock around all 3 to avoid the race.

Also fix the lock not being released in case of linux_start_event_monitor
failure.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
core: Add ctx to the active context list before backend->init

As soon as backend->init has completed hotplug events may fire, and they
will not get processed on the created context until it has been added
to the active_context list.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
core: Fixup cleanup on init error

-mutexes were not being destroyed on backend init failure
-hotplug_cbs_lock mutex was never being destroyed

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
core: Fixup default_context handling

Now that we set the default context earlier in libusb_init:
-There is no need to do it a second time at the end of libusb_init
-We need to set it to NULL if libusb_init fails later on
-Since it now can be set then reset by libusb_init, libusb_exit needs to
 take the lock while testing if the passed in ctx == default_context

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux: Replace pthread_mutex with usbi_mutex_static

Boils down to the same thing, but mixing and matching lock types in one
file looks ugly.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux: hotplug_enumerate and _disconnect take active_contexts_lock

Take the active_contexts_lock when enumerating over active_contexts.

Note that the active_contexts_lock is taken *before* the hotplug lock, as they
are taken in the same order in the libusb_init path.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux: Move check for a device being added twice to linux_enumerate_device

It is possible for a device to show up between the hotplug code starting to
listen for new devices, and the enumeration of existing devices. This will
cause a device to get enumerated twice.

The next patch in this series adds a different code path which can cause
a device to be enumerated twice, this patch therefor moves the check for
this to linux_enumerate_device, so that this will get caught in all cases.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
linux: Deal with devices being enumerated before their parents are

The linux netlink enumeration code processes devices in readdir order, which
means devices may get enumerated before their parent is, IE on my system
readdir order is (ls -U)
1-1   usb4       2-1.8        6-0:1.0    1-1.4.3    1-1.4.4.1:1.0
2-1   usb5       6-1.4        6-1:1.0    1-1.4.4    1-1.4.4.1:1.1
6-1   usb6       3-0:1.0      2-1.8:1.0  1-0:1.0    1-1.4.4.1:1.2
7-1   usb7       1-1.4.3:1.0  2-1.8:1.1  1-1:1.0
usb1  1-1.4.4.1  1-1.4.4:1.0  2-1.8:1.2  6-1.4:1.0
usb2  1-1.4:1.0  4-0:1.0      7-0:1.0    2-0:1.0
usb3  1-1.4      5-0:1.0      7-1:1.0    2-1:1.0

So 1.4.4.1 will get added (way) before 1.4.4 and indeed:
[ 0.002243] [00004055] libusbx: debug [linux_get_device_address] scan 1-1.4.4.1
Dev 0x1973bc0 (1-1.4.4.1) has parent (nil)

This patch fixes this by forcing enumeration of the parent from
linux_parent_dev.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>