2 * Core functions for libusb
3 * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
35 const struct usbi_os_backend * const usbi_backend = &linux_usbfs_backend;
37 #error "Unsupported OS"
40 static struct list_head usb_devs;
41 static pthread_mutex_t usb_devs_lock = PTHREAD_MUTEX_INITIALIZER;
43 struct list_head usbi_open_devs;
44 pthread_mutex_t usbi_open_devs_lock = PTHREAD_MUTEX_INITIALIZER;
47 * \mainpage libusb-1.0 API Reference
48 * libusb is an open source library that allows you to communicate with USB
49 * devices from userspace. For more info, see the
50 * <a href="http://libusb.sourceforge.net">libusb homepage</a>.
52 * This documentation is aimed at application developers wishing to
53 * communicate with USB peripherals from their own software. After reviewing
54 * this documentation, feedback and questions can be sent to the
55 * <a href="http://sourceforge.net/mail/?group_id=1674">libusb-devel mailing
58 * This documentation assumes knowledge of how to operate USB devices from
59 * a software standpoint (descriptors, configurations, interfaces, endpoints,
60 * control/bulk/interrupt/isochronous transfers, etc). Full information
61 * can be found in the <a href="http://www.usb.org/developers/docs/">USB 2.0
62 * Specification</a> which is available for free download. You can probably
63 * find less verbose introductions by searching the web.
65 * To begin reading the API documentation, start with the Modules page which
66 * links to the different categories of libusb's functionality.
68 * libusb does have imperfections. The \ref caveats "caveats" page attempts
73 * \page caveats Caveats
75 * \section devresets Device resets
77 * The libusb_reset_device() function allows you to reset a device. If your
78 * program has to call such a function, it should obviously be aware that
79 * the reset will cause device state to change (e.g. register values may be
82 * The problem is that any other program could reset the device your program
83 * is working with, at any time. libusb does not offer a mechanism to inform
84 * you when this has happened, so it will not be clear to your own program
85 * why the device state has changed.
87 * Ultimately, this is a limitation of writing drivers in userspace.
88 * Separation from the USB stack in the underlying kernel makes it difficult
89 * for the operating system to deliver such notifications to your program.
90 * The Linux kernel USB stack allows such reset notifications to be delivered
91 * to in-kernel USB drivers, but it is not clear how such notifications could
92 * be delivered to second-class drivers that live in userspace.
94 * \section blockonly Blocking-only functionality
96 * The functionality listed below is only available through synchronous,
97 * blocking functions. There are no asynchronous/non-blocking alternatives,
98 * and no clear ways of implementing these.
100 * - Configuration activation (libusb_set_configuration())
101 * - Interface/alternate setting activation (libusb_set_interface_alt_setting())
102 * - Clearing of halt/stall condition (libusb_clear_halt())
103 * - Device resets (libusb_reset_device())
107 * @defgroup lib Library initialization/deinitialization
108 * This page details how to initialize and deinitialize libusb. Initialization
109 * must be performed before using any libusb functionality, and similarly you
110 * must not call any libusb functions after deinitialization.
114 * @defgroup dev Device handling and enumeration
115 * The functionality documented below is designed to help with the following
117 * - Enumerating the USB devices currently attached to the system
118 * - Choosing a device to operate from your software
119 * - Opening and closing the chosen device
121 * \section nutshell In a nutshell...
123 * The description below really makes things sound more complicated than they
124 * actually are. The following sequence of function calls will be suitable
125 * for almost all scenarios and does not require you to have such a deep
126 * understanding of the resource management issues:
129 libusb_device **list;
130 libusb_device *found = NULL;
131 size_t cnt = libusb_get_device_list(&list);
136 for (i = 0; i < cnt; i++) {
137 libusb_device *device = list[i];
138 if (is_interesting(device)) {
145 libusb_device_handle *handle = libusb_open(found);
149 libusb_free_device_list(list, 1);
152 * The two important points:
153 * - You asked libusb_free_device_list() to unreference the devices (2nd
155 * - You opened the device before freeing the list and unreferencing the
158 * If you ended up with a handle, you can now proceed to perform I/O on the
161 * \section devshandles Devices and device handles
162 * libusb has a concept of a USB device, represented by the
163 * \ref libusb_device opaque type. A device represents a USB device that
164 * is currently or was previously connected to the system. Using a reference
165 * to a device, you can determine certain information about the device (e.g.
166 * you can read the descriptor data).
168 * The libusb_get_device_list() function can be used to obtain a list of
169 * devices currently connected to the system. This is known as device
172 * Just because you have a reference to a device does not mean it is
173 * necessarily usable. The device may have been unplugged, you may not have
174 * permission to operate such device, or another program or driver may be
177 * When you've found a device that you'd like to operate, you must ask
178 * libusb to open the device using the libusb_open() function. Assuming
179 * success, libusb then returns you a <em>device handle</em>
180 * (a \ref libusb_device_handle pointer). All "real" I/O operations then
181 * operate on the handle rather than the original device pointer.
183 * \section devref Device discovery and reference counting
185 * Device discovery (i.e. calling libusb_get_device_list()) returns a
186 * freshly-allocated list of devices. The list itself must be freed when
187 * you are done with it. libusb also needs to know when it is OK to free
188 * the contents of the list - the devices themselves.
190 * To handle these issues, libusb provides you with two separate items:
191 * - A function to free the list itself
192 * - A reference counting system for the devices inside
194 * New devices presented by the libusb_get_device_list() function all have a
195 * reference count of 1. You can increase and decrease reference count using
196 * libusb_ref_device() and libusb_unref_device(). A device is destroyed when
197 * it's reference count reaches 0.
199 * With the above information in mind, the process of opening a device can
200 * be viewed as follows:
201 * -# Discover devices using libusb_get_device_list().
202 * -# Choose the device that you want to operate, and call libusb_open().
203 * -# Unref all devices in the discovered device list.
204 * -# Free the discovered device list.
206 * The order is important - you must not unreference the device before
207 * attempting to open it, because unreferencing it may destroy the device.
209 * For convenience, the libusb_free_device_list() function includes a
210 * parameter to optionally unreference all the devices in the list before
211 * freeing the list itself. This combines steps 3 and 4 above.
213 * As an implementation detail, libusb_open() actually adds a reference to
214 * the device in question. This is because the device remains available
215 * through the handle via libusb_get_device(). The reference is deleted during
220 * @defgroup misc Miscellaneous structures and constants
221 * This page documents structures and constants that don't belong anywhere
225 /* we traverse usbfs without knowing how many devices we are going to find.
226 * so we create this discovered_devs model which is similar to a linked-list
227 * which grows when required. it can be freed once discovery has completed,
228 * eliminating the need for a list node in the libusb_device structure
230 #define DISCOVERED_DEVICES_SIZE_STEP 8
232 static struct discovered_devs *discovered_devs_alloc(void)
234 struct discovered_devs *ret =
235 malloc(sizeof(*ret) + (sizeof(void *) * DISCOVERED_DEVICES_SIZE_STEP));
239 ret->capacity = DISCOVERED_DEVICES_SIZE_STEP;
244 /* append a device to the discovered devices collection. may realloc itself,
245 * returning new discdevs. returns NULL on realloc failure. */
246 struct discovered_devs *discovered_devs_append(
247 struct discovered_devs *discdevs, struct libusb_device *dev)
249 size_t len = discdevs->len;
252 /* if there is space, just append the device */
253 if (len < discdevs->capacity) {
254 discdevs->devices[len] = libusb_ref_device(dev);
259 /* exceeded capacity, need to grow */
260 usbi_dbg("need to increase capacity");
261 capacity = discdevs->capacity + DISCOVERED_DEVICES_SIZE_STEP;
262 discdevs = realloc(discdevs,
263 sizeof(*discdevs) + (sizeof(void *) * capacity));
265 discdevs->capacity = capacity;
266 discdevs->devices[len] = libusb_ref_device(dev);
273 static void discovered_devs_free(struct discovered_devs *discdevs)
277 for (i = 0; i < discdevs->len; i++)
278 libusb_unref_device(discdevs->devices[i]);
283 /* allocate a new device with reference count 1 */
284 struct libusb_device *usbi_alloc_device(unsigned long session_id)
286 size_t priv_size = usbi_backend->device_priv_size;
287 struct libusb_device *dev = malloc(sizeof(*dev) + priv_size);
293 r = pthread_mutex_init(&dev->lock, NULL);
298 dev->session_data = session_id;
299 memset(&dev->os_priv, 0, priv_size);
301 pthread_mutex_lock(&usb_devs_lock);
302 list_add(&dev->list, &usb_devs);
303 pthread_mutex_unlock(&usb_devs_lock);
307 /* to be called by OS implementations when a new device is ready for final
308 * sanitization and checking before being returned in a device list. */
309 int usbi_sanitize_device(struct libusb_device *dev)
312 unsigned char raw_desc[DEVICE_DESC_LENGTH];
313 uint8_t num_configurations;
315 r = usbi_backend->get_device_descriptor(dev, raw_desc);
319 num_configurations = raw_desc[DEVICE_DESC_LENGTH - 1];
320 if (num_configurations > USB_MAXCONFIG) {
321 usbi_err("too many configurations");
322 return LIBUSB_ERROR_IO;
323 } else if (num_configurations < 1) {
324 usbi_dbg("no configurations?");
325 return LIBUSB_ERROR_IO;
328 dev->num_configurations = num_configurations;
332 struct libusb_device *usbi_get_device_by_session_id(unsigned long session_id)
334 struct libusb_device *dev;
335 struct libusb_device *ret = NULL;
337 pthread_mutex_lock(&usb_devs_lock);
338 list_for_each_entry(dev, &usb_devs, list)
339 if (dev->session_data == session_id) {
343 pthread_mutex_unlock(&usb_devs_lock);
349 * Returns a list of USB devices currently attached to the system. This is
350 * your entry point into finding a USB device to operate.
352 * You are expected to unreference all the devices when you are done with
353 * them, and then free the list with libusb_free_device_list(). Note that
354 * libusb_free_device_list() can unref all the devices for you. Be careful
355 * not to unreference a device you are about to open until after you have
358 * This return value of this function indicates the number of devices in
359 * the resultant list. The list is actually one element larger, as it is
362 * \param list output location for a list of devices. Must be later freed with
363 * libusb_free_device_list().
364 * \returns the number of devices in the outputted list, or LIBUSB_ERROR_NO_MEM
365 * on memory allocation failure.
367 API_EXPORTED ssize_t libusb_get_device_list(libusb_device ***list)
369 struct discovered_devs *discdevs = discovered_devs_alloc();
370 struct libusb_device **ret;
377 return LIBUSB_ERROR_NO_MEM;
379 r = usbi_backend->get_device_list(&discdevs);
385 /* convert discovered_devs into a list */
387 ret = malloc(sizeof(void *) * (len + 1));
389 len = LIBUSB_ERROR_NO_MEM;
394 for (i = 0; i < len; i++) {
395 struct libusb_device *dev = discdevs->devices[i];
396 ret[i] = libusb_ref_device(dev);
401 discovered_devs_free(discdevs);
406 * Frees a list of devices previously discovered using
407 * libusb_get_device_list(). If the unref_devices parameter is set, the
408 * reference count of each device in the list is decremented by 1.
409 * \param list the list to free
410 * \param unref_devices whether to unref the devices in the list
412 API_EXPORTED void libusb_free_device_list(libusb_device **list,
420 struct libusb_device *dev;
422 while ((dev = list[i++]) != NULL)
423 libusb_unref_device(dev);
429 * Get the number of the bus that a device is connected to.
430 * \param dev a device
431 * \returns the bus number
433 API_EXPORTED uint8_t libusb_get_bus_number(libusb_device *dev)
435 return dev->bus_number;
439 * Get the address of the device on the bus it is connected to.
440 * \param dev a device
441 * \returns the device address
443 API_EXPORTED uint8_t libusb_get_device_address(libusb_device *dev)
445 return dev->device_address;
449 * Convenience function to retrieve the wMaxPacketSize value for a particular
450 * endpoint in the active device configuration. This is useful for setting up
451 * isochronous transfers.
453 * \param dev a device
454 * \param endpoint address of the endpoint in question
455 * \returns the wMaxPacketSize value
456 * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
457 * \returns LIBUSB_ERROR_OTHER on other failure
459 API_EXPORTED int libusb_get_max_packet_size(libusb_device *dev,
460 unsigned char endpoint)
463 struct libusb_config_descriptor *config;
466 r = libusb_get_active_config_descriptor(dev, &config);
468 usbi_err("could not retrieve active config descriptor");
469 return LIBUSB_ERROR_OTHER;
472 r = LIBUSB_ERROR_NOT_FOUND;
473 for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) {
474 const struct libusb_interface *iface = &config->interface[iface_idx];
477 for (altsetting_idx = 0; altsetting_idx < iface->num_altsetting;
479 const struct libusb_interface_descriptor *altsetting
480 = &iface->altsetting[altsetting_idx];
483 for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints; ep_idx++) {
484 const struct libusb_endpoint_descriptor *ep =
485 &altsetting->endpoint[ep_idx];
486 if (ep->bEndpointAddress == endpoint) {
487 r = ep->wMaxPacketSize;
495 libusb_free_config_descriptor(config);
500 * Increment the reference count of a device.
501 * \param dev the device to reference
502 * \returns the same device
504 API_EXPORTED libusb_device *libusb_ref_device(libusb_device *dev)
506 pthread_mutex_lock(&dev->lock);
508 pthread_mutex_unlock(&dev->lock);
513 * Decrement the reference count of a device. If the decrement operation
514 * causes the reference count to reach zero, the device shall be destroyed.
515 * \param dev the device to unreference
517 API_EXPORTED void libusb_unref_device(libusb_device *dev)
524 pthread_mutex_lock(&dev->lock);
525 refcnt = --dev->refcnt;
526 pthread_mutex_unlock(&dev->lock);
529 usbi_dbg("destroy device %d.%d", dev->bus_number, dev->device_address);
531 if (usbi_backend->destroy_device)
532 usbi_backend->destroy_device(dev);
534 pthread_mutex_lock(&usb_devs_lock);
535 list_del(&dev->list);
536 pthread_mutex_unlock(&usb_devs_lock);
543 * Open a device and obtain a device handle. A handle allows you to perform
544 * I/O on the device in question.
546 * Internally, this function adds a reference to the device and makes it
547 * available to you through libusb_get_device(). This reference is removed
548 * during libusb_close().
550 * This is a non-blocking function; no requests are sent over the bus.
552 * \param dev the device to open
553 * \returns a handle for the device, or NULL on error
555 API_EXPORTED libusb_device_handle *libusb_open(libusb_device *dev)
557 struct libusb_device_handle *handle;
558 size_t priv_size = usbi_backend->device_handle_priv_size;
560 usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);
562 handle = malloc(sizeof(*handle) + priv_size);
566 r = pthread_mutex_init(&handle->lock, NULL);
570 handle->dev = libusb_ref_device(dev);
571 handle->claimed_interfaces = 0;
572 memset(&handle->os_priv, 0, priv_size);
574 r = usbi_backend->open(handle);
576 libusb_unref_device(dev);
581 pthread_mutex_lock(&usbi_open_devs_lock);
582 list_add(&handle->list, &usbi_open_devs);
583 pthread_mutex_unlock(&usbi_open_devs_lock);
588 * Convenience function for finding a device with a particular
589 * <tt>idVendor</tt>/<tt>idProduct</tt> combination. This function is intended
590 * for those scenarios where you are using libusb to knock up a quick test
591 * application - it allows you to avoid calling libusb_get_device_list() and
592 * worrying about traversing/freeing the list.
594 * This function has limitations and is hence not intended for use in real
595 * applications: if multiple devices have the same IDs it will only
596 * give you the first one, etc.
598 * \param vendor_id the idVendor value to search for
599 * \param product_id the idProduct value to search for
600 * \returns a handle for the first found device, or NULL on error or if the
601 * device could not be found. */
602 API_EXPORTED libusb_device_handle *libusb_open_device_with_vid_pid(
603 uint16_t vendor_id, uint16_t product_id)
605 struct libusb_device **devs;
606 struct libusb_device *found = NULL;
607 struct libusb_device *dev;
608 struct libusb_device_handle *handle = NULL;
611 if (libusb_get_device_list(&devs) < 0)
614 while ((dev = devs[i++]) != NULL) {
615 struct libusb_device_descriptor desc;
616 int r = libusb_get_device_descriptor(dev, &desc);
619 if (desc.idVendor == vendor_id && desc.idProduct == product_id) {
626 handle = libusb_open(found);
629 libusb_free_device_list(devs, 1);
633 static void do_close(struct libusb_device_handle *dev_handle)
635 usbi_backend->close(dev_handle);
636 libusb_unref_device(dev_handle->dev);
640 * Close a device handle. Should be called on all open handles before your
643 * Internally, this function destroys the reference that was added by
644 * libusb_open() on the given device.
646 * This is a non-blocking function; no requests are sent over the bus.
648 * \param dev_handle the handle to close
650 API_EXPORTED void libusb_close(libusb_device_handle *dev_handle)
656 pthread_mutex_lock(&usbi_open_devs_lock);
657 list_del(&dev_handle->list);
658 pthread_mutex_unlock(&usbi_open_devs_lock);
660 do_close(dev_handle);
665 * Get the underlying device for a handle. This function does not modify
666 * the reference count of the returned device, so do not feel compelled to
667 * unreference it when you are done.
668 * \param dev_handle a device handle
669 * \returns the underlying device
671 API_EXPORTED libusb_device *libusb_get_device(libusb_device_handle *dev_handle)
673 return dev_handle->dev;
677 * Set the active configuration for a device. The operating system may have
678 * already set an active configuration on the device, but for portability
679 * reasons you should use this function to select the configuration you want
680 * before claiming any interfaces.
682 * If you wish to change to another configuration at some later time, you
683 * must release all claimed interfaces using libusb_release_interface() before
684 * setting a new active configuration.
686 * A configuration value of -1 will put the device in unconfigured state.
687 * The USB specifications state that a configuration value of 0 does this,
688 * however buggy devices exist which actually have a configuration 0.
690 * You should always use this function rather than formulating your own
691 * SET_CONFIGURATION control request. This is because the underlying operating
692 * system needs to know when such changes happen.
694 * This is a blocking function.
696 * \param dev a device handle
697 * \param configuration the bConfigurationValue of the configuration you
698 * wish to activate, or -1 if you wish to put the device in unconfigured state
699 * \returns 0 on success
700 * \returns LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist
701 * \returns LIBUSB_ERROR_BUSY if interfaces are currently claimed
702 * \returns another LIBUSB_ERROR code on other failure
704 API_EXPORTED int libusb_set_configuration(libusb_device_handle *dev,
707 usbi_dbg("configuration %d", configuration);
708 return usbi_backend->set_configuration(dev, configuration);
712 * Claim an interface on a given device handle. You must claim the interface
713 * you wish to use before you can perform I/O on any of its endpoints.
715 * It is legal to attempt to claim an already-claimed interface, in which
716 * case libusb just returns 0 without doing anything.
718 * Claiming of interfaces is a purely logical operation; it does not cause
719 * any requests to be sent over the bus. Interface claiming is used to
720 * instruct the underlying operating system that your application wishes
721 * to take ownership of the interface.
723 * This is a non-blocking function.
725 * \param dev a device handle
726 * \param interface_number the <tt>bInterfaceNumber</tt> of the interface you
728 * \returns 0 on success
729 * \returns LIBUSB_ERROR_NOT_FOUND if the requested interface does not exist
730 * \returns LIBUSB_ERROR_BUSY if another program or driver has claimed the
732 * \returns a LIBUSB_ERROR code on other failure
734 API_EXPORTED int libusb_claim_interface(libusb_device_handle *dev,
735 int interface_number)
739 usbi_dbg("interface %d", interface_number);
740 if (interface_number >= sizeof(dev->claimed_interfaces) * 8)
741 return LIBUSB_ERROR_INVALID_PARAM;
743 pthread_mutex_lock(&dev->lock);
744 if (dev->claimed_interfaces & (1 << interface_number))
747 r = usbi_backend->claim_interface(dev, interface_number);
749 dev->claimed_interfaces |= 1 << interface_number;
752 pthread_mutex_unlock(&dev->lock);
757 * Release an interface previously claimed with libusb_claim_interface(). You
758 * should release all claimed interfaces before closing a device handle.
760 * This is a blocking function. A SET_INTERFACE control request will be sent
761 * to the device, resetting interface state to the first alternate setting.
763 * \param dev a device handle
764 * \param interface_number the <tt>bInterfaceNumber</tt> of the
765 * previously-claimed interface
766 * \returns 0 on success, or a LIBUSB_ERROR code on failure.
767 * LIBUSB_ERROR_NOT_FOUND indicates that the interface was not claimed.
769 API_EXPORTED int libusb_release_interface(libusb_device_handle *dev,
770 int interface_number)
774 usbi_dbg("interface %d", interface_number);
775 if (interface_number >= sizeof(dev->claimed_interfaces) * 8)
776 return LIBUSB_ERROR_INVALID_PARAM;
778 pthread_mutex_lock(&dev->lock);
779 if (!(dev->claimed_interfaces & (1 << interface_number))) {
780 r = LIBUSB_ERROR_NOT_FOUND;
784 r = usbi_backend->release_interface(dev, interface_number);
786 dev->claimed_interfaces &= ~(1 << interface_number);
789 pthread_mutex_unlock(&dev->lock);
794 * Activate an alternate setting for an interface. The interface must have
795 * been previously claimed with libusb_claim_interface().
797 * You should always use this function rather than formulating your own
798 * SET_INTERFACE control request. This is because the underlying operating
799 * system needs to know when such changes happen.
801 * This is a blocking function.
803 * \param dev a device handle
804 * \param interface_number the <tt>bInterfaceNumber</tt> of the
805 * previously-claimed interface
806 * \param alternate_setting the <tt>bAlternateSetting</tt> of the alternate
807 * setting to activate
808 * \returns 0 on success
809 * \returns LIBUSB_ERROR_NOT_FOUND if the interface was not claimed, or the
810 * requested alternate setting does not exist
811 * \returns another LIBUSB_ERROR code on other failure
813 API_EXPORTED int libusb_set_interface_alt_setting(libusb_device_handle *dev,
814 int interface_number, int alternate_setting)
816 usbi_dbg("interface %d altsetting %d", interface_number, alternate_setting);
817 if (interface_number >= sizeof(dev->claimed_interfaces) * 8)
818 return LIBUSB_ERROR_INVALID_PARAM;
820 pthread_mutex_lock(&dev->lock);
821 if (!(dev->claimed_interfaces & (1 << interface_number))) {
822 pthread_mutex_unlock(&dev->lock);
823 return LIBUSB_ERROR_NOT_FOUND;
825 pthread_mutex_unlock(&dev->lock);
827 return usbi_backend->set_interface_altsetting(dev, interface_number,
832 * Clear the halt/stall condition for an endpoint. Endpoints with halt status
833 * are unable to receive or transmit data until the halt condition is stalled.
835 * You should cancel all pending transfers before attempting to clear the halt
838 * This is a blocking function.
840 * \param dev a device handle
841 * \param endpoint the endpoint to clear halt status
842 * \returns 0 on success
843 * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
844 * \returns another LIBUSB_ERROR code on other failure
846 API_EXPORTED int libusb_clear_halt(libusb_device_handle *dev,
847 unsigned char endpoint)
849 usbi_dbg("endpoint %x", endpoint);
850 return usbi_backend->clear_halt(dev, endpoint);
854 * Perform a USB port reset to reinitialize a device. The system will attempt
855 * to restore the previous configuration and alternate settings after the
856 * reset has completed.
858 * If the reset fails, the descriptors change, or the previous state cannot be
859 * restored, the device will appear to be disconnected and reconnected. This
860 * means that the device handle is no longer valid (you should close it) and
861 * rediscover the device. A return code of LIBUSB_ERROR_NOT_FOUND indicates
862 * when this is the case.
864 * This is a blocking function which usually incurs a noticeable delay.
866 * \param dev a handle of the device to reset
867 * \returns 0 on success
868 * \returns LIBUSB_ERROR_NOT_FOUND if re-enumeration is required
869 * \returns another LIBUSB_ERROR code on other failure
871 API_EXPORTED int libusb_reset_device(libusb_device_handle *dev)
874 return usbi_backend->reset_device(dev);
878 * Determine if a kernel driver is active on an interface. If a kernel driver
879 * is active, you cannot claim the interface, and libusb will be unable to
882 * \param dev a device handle
883 * \param interface the interface to check
884 * \returns 0 if no kernel driver is active
885 * \returns 1 if a kernel driver is active
886 * \returns LIBUSB_ERROR code on failure
887 * \see libusb_detach_kernel_driver()
889 API_EXPORTED int libusb_kernel_driver_active(libusb_device_handle *dev,
892 usbi_dbg("interface %d", interface);
893 if (usbi_backend->kernel_driver_active)
894 return usbi_backend->kernel_driver_active(dev, interface);
896 return LIBUSB_ERROR_NOT_SUPPORTED;
900 * Detach a kernel driver from an interface. If successful, you will then be
901 * able to claim the interface and perform I/O.
903 * \param dev a device handle
904 * \param interface the interface to detach the driver from
905 * \returns 0 on success
906 * \returns LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
907 * \returns LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
908 * \returns another LIBUSB_ERROR code on other failure
909 * \see libusb_kernel_driver_active()
911 API_EXPORTED int libusb_detach_kernel_driver(libusb_device_handle *dev,
914 usbi_dbg("interface %d", interface);
915 if (usbi_backend->detach_kernel_driver)
916 return usbi_backend->detach_kernel_driver(dev, interface);
918 return LIBUSB_ERROR_NOT_SUPPORTED;
922 * Initialize libusb. This function must be called before calling any other
924 * \returns 0 on success, or a LIBUSB_ERROR code on failure
926 API_EXPORTED int libusb_init(void)
930 if (usbi_backend->init) {
931 int r = usbi_backend->init();
936 list_init(&usb_devs);
937 list_init(&usbi_open_devs);
943 * Deinitialize libusb. Should be called after closing all open devices and
944 * before your application terminates.
946 API_EXPORTED void libusb_exit(void)
950 pthread_mutex_lock(&usbi_open_devs_lock);
951 if (!list_empty(&usbi_open_devs)) {
952 struct libusb_device_handle *devh;
953 struct libusb_device_handle *tmp;
955 usbi_dbg("naughty app left some devices open!");
956 list_for_each_entry_safe(devh, tmp, &usbi_open_devs, list) {
957 list_del(&devh->list);
962 pthread_mutex_unlock(&usbi_open_devs_lock);
964 if (usbi_backend->exit)
965 usbi_backend->exit();
968 void usbi_log(enum usbi_log_level level, const char *function,
969 const char *format, ...)
972 FILE *stream = stdout;
979 case LOG_LEVEL_WARNING:
983 case LOG_LEVEL_ERROR:
987 case LOG_LEVEL_DEBUG:
997 fprintf(stream, "libusb:%s [%s] ", prefix, function);
999 va_start (args, format);
1000 vfprintf(stream, format, args);
1003 fprintf(stream, "\n");