23 Commits

Author SHA1 Message Date
Chris Dickens
db00b074b2 hotplug: Cast ssize_t to int in error path to avoid compiler warning
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:51:28 -08:00
Chris Dickens
3dca962104 hotplug: Improve internal structure and deregistration mechanism
This commit shrinks the size of the internal hotplug callback structure
by removing unused fields, using the correctly sized types for matching
fields, and adding a new flags field whose bits control how the callback
structure should behave.

The hotplug callback handle ID counter has also been moved to the
context structure instead of being a global variable shared amongst all
contexts. This lets each context independently manage handle IDs and use
the maximum range of possible IDs.

Finally, the hotplug callback deregistration mechanism has been improved
to signal to the event handler that an explicit deregistration needs to
be handled. This removes the need to send a dummy hotplug message, which
was using an invalid libusb_hotplug_event value anyway that was causing
some compilers to complain.

Closes #373

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-27 23:30:16 -08:00
Stefan Tauner
1fd08a1513 Replace obsolete usleep with nanosleep
This fixes compiling libusb with uclibc.

Closes #141, #165

Signed-off-by: Stefan Tauner <stefan.tauner@gmx.at>
2016-03-05 07:55:25 -07:00
Chris Dickens
7ee92df12a Misc: Prefix doxygen references in order to namespace libusb
This change add "libusb_" to every group and page definition (and
updates all references accordingly) so that generated man pages
are namespaced for libusb, thus avoiding possible conflict with
other packages.

Closes #131

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2016-02-24 01:07:20 -08:00
Chris Dickens
8a0c14372f Misc: Make API parameter names consistent and sensible
Prior to this commit, API functions taking a libusb_device_handle
argument had the parameter named dev, handle, or dev_handle. This
commit changes the name of all libusb_device_handle parameters to
dev_handle in both the documentation and actual code.

Closes #132

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2016-02-24 00:23:49 -08:00
Chris Dickens
960a6e7506 core: Prevent attempts to recursively hande events
Prior to this commit, it was possible to call certain functions from
within a hotplug or transfer completion callback that would in turn
attempt to handle events (e.g. any of the sync transfer APIs). This
is dangerous because certain events may cause the nested calls to
free memory that is currently in use by the previous calls.

This implementation uses thread-local storage to store a key within
the context that is set to a non-NULL value whenever event handling
is occurring. This allows us to detect when dangerous calls are made
from within event handling context. Such calls will return an error
code of LIBUSB_ERROR_BUSY.

Note that this implementation was chosen because of its portability.
It is supported on all platforms that libusb supports.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2015-09-28 21:51:30 -07:00
Chris Dickens
542fd04e02 Fixing erroneous double semi-colon 2015-01-05 23:38:19 -08:00
Chris Dickens
89b5407b9a core: Signal the event pipe at most one time
This change will ensure that the event pipe is only signalled
at most one time during the course of any incoming events. New
events that occur while the event pipe is in the signalled state
will not cause the event pipe to be signalled again.

This change will provide support for the use of native events on
the Windows/WinCE backends, as these events are binary and do not
"count" the number of times they are signalled.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2014-12-19 11:22:03 -08:00
Chris Dickens
3b371f1a6a core: Eliminate hotplug pipe, using list and event pipe instead
To further consolidate libusb internal events, this change removes
the hotplug pipe. Hotplug messages are now kept in a list within the
context, and the event pipe is signalled when a new hotplug message
is added. When handling events, the hotplug messages will be processed
from the list instead of from a separate pipe.

This change is greatly beneficial for the Windows/WinCE backends which
do not allow pipes to be used in the WaitFor* functions.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2014-12-19 11:21:24 -08:00
Ludovic Rousseau
de6130dc0d Doxygen: fix a duplicate label
libusb/hotplug.c:46: warning: multiple use of section label 'intro'
while adding section, (first occurrence: libusb/core.c, line 79)

Use label "hotplug_intro" instead of "intro"
2014-12-16 21:49:19 +01:00
Markus Heidelberg
93de199d8d Documentation: fix hotplug example
* add a missing call to libusb_handle_events_completed()
* add a missing argument to libusb_hotplug_deregister_callback()
2014-02-02 15:51:07 +01:00
hjelmn@cs.unm.edu
1eff220474 Misc: Revert all references to libusb/libusb.info 2014-01-08 23:51:01 +00:00
Xiaofan Chen
def6a8f879 Windows: Address an VS2012/x64 warning in hotplug.c
..\libusb\hotplug.c(255): warning C4244: '=' : conversion from
'ssize_t' to 'int', possible loss of data
2013-09-02 11:37:35 +01:00
Hans de Goede
0fc304da83 hotplug: Document that callbacks cannot unregister themselves when called from hotplug_register_callback
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-08-21 14:30:51 +02:00
Hans de Goede
be76bef5b7 hotplug: Don't call the user callback while holding various locks
Calling user callbacks with locks held is a bad idea and should be avoided
whenever possible. Before this patch this could lead ie to the following hang:

1) User calls libusb_hotplug_register_callback with the
   LIBUSB_HOTPLUG_ENUMERATE flag
2) libusb_hotplug_register_callback calls the user callback while holding the
   hotplug_cbs_lock
3) The callback calls a synchronous libusb function
4) The synchronous libusb function calls libusb_handle_events
5) There is an hotplug event waiting in the hotplug pipe and
   libusb_handle_events calls usbi_hotplug_match
6) usbi_hotplug_match tries to take the lock a 2nd time
7) hang / assert / abort (depending on the platform)

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-08-21 14:18:09 +02:00
Nathan Hjelm
707d500b9f keep a reference to the device for each active transfer and let the backend handle cancelling active transfers when a device is disconnected
This commit should fix issues with active transfers when a device is
disconnected. The backend is responsible for making sure the completion
callbacks are made, not the hotplug code. This should fix a number of
issues including duplicate callbacks and segmentation faults.

References #124.
2013-07-30 09:08:39 -06:00
Florian Albrechtskirchinger
97958ba756 hotplug: Pass explicit context to callbacks
* Instead of passing NULL for the context to hotplug callbacks, if the
  context happens to be the default context, always pass the explicit
  context pointer.
2013-07-23 20:23:13 +01:00
Hans de Goede
ac41bfc0fd hotplug: Wakeup libusb_handle_events on libusb_hotplug_deregister_callback
This serves 2 purposes:

1) We use lazy free-ing of the callback structure, for it to be actually
free-ed usbi_hotplug_match() needs to be called. This ensures this actually
happens (rather then waiting for a hotplug event to arrive, and not freeing
the callback as long as no such event arrives).

2) It causes libusb_handle_events to return to its caller on a call to
libusb_hotplug_deregister_callback, which is very useful for apps which use
a thread to do their apps (hotplug) event handling, otherwise that thread will
hang when the app tries to stop until some event happens.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-05 09:06:21 +02:00
Hans de Goede
f505929798 hotplug: Give the usbi_hotplug_match* functions a context parameter
So that the device parameter can be NULL, in combination with a 0
events parameter, to be used to force lazy deregistration.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-07-05 08:38:13 +02:00
Pete Batard
b4c18fac65 Doc: update hotplug, topology and descriptor documentation
* Also fix some typos
* Closes #95
2013-07-04 18:39:22 +01:00
Toby Gray
3abc6f3ba5 Hotplug: Make use of HAVE_SYS_TYPES_H define
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-05-28 12:29:03 +02:00
Hans de Goede
86031e26ad Bump LIBUSBX_API_VERSION for new hotplug API
And document the minimum version under which hotplug is supported

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-05-15 20:43:35 +02:00
Nathan Hjelm
7801ff94fa Add hotplug support.
The internal API is changing as follows:
 - Adding two new functions. usbi_connect_device, and usbi_disconnect_device.
   Backends must call these functions to add them to the context's device list
   at one of two places: initial enumeration (done at init), and on device
   attach and removal. These functions need to be called once per context.
 - Backends that support hotplug should not provide a get_device_list funtion.
   This function is now deprecated and will likely be removed once all backends
   support hotplug.

The external API is changing as follows:
 - Two new functions have been added to register and deregister callbacks for
   hotplug notification: libusb_hotplug_register_callback(),
   libusb_hotplug_deregister_callback(). Hotplug callbacks are called by
   libusb_handle_events(). Details of the new API can be found in libusb.h.
 - A new capability check has been added to check for hotplug support. See
   LIBUSB_CAP_HAS_HOTPLUG.

Aa suggested by Xiaofan add new example has been added to show how to use
the new external hotplug API. See examples/hotplugtest.c.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-05-15 17:28:06 +02:00