diff --git a/lib/libusb/Makefile b/lib/libusb/Makefile index 3982fda478fb..1648664b8815 100644 --- a/lib/libusb/Makefile +++ b/lib/libusb/Makefile @@ -1,274 +1,275 @@ # # $FreeBSD$ # # Makefile for the FreeBSD specific LibUSB 2.0 # LIB= usb SHLIB_MAJOR= 3 SHLIB_MINOR= 0 SRCS= libusb20.c SRCS+= libusb20_desc.c SRCS+= libusb20_ugen20.c INCS+= libusb20.h INCS+= libusb20_desc.h MAN= libusb.3 libusb20.3 MKLINT= no NOGCCERROR= PTHREAD_LIBS?= -lpthread WARNS?= 2 LIBADD= pthread MLINKS+= libusb.3 usb.3 # libusb 0.1 compat INCS+= usb.h SRCS+= libusb01.c # libusb 1.0 compat INCS+= libusb.h SRCS+= libusb10.c SRCS+= libusb10_desc.c SRCS+= libusb10_hotplug.c SRCS+= libusb10_io.c .if defined(COMPAT_32BIT) CFLAGS+= -DCOMPAT_32BIT .endif PCFILES= libusb-0.1.pc libusb-1.0.pc libusb-2.0.pc # # Cross platform support # # Examples: # make LIBUSB_GLOBAL_INCLUDE_FILE=libusb_global_linux.h # make COMPAT_32BIT=YES \ # LIBUSB_GLOBAL_INCLUDE_FILE=libusb_global_linux.h \ # DEBUG_FLAGS="-g" # # From Ubuntu 10.04: # freebsd-make LIBUSB_GLOBAL_INCLUDE_FILE=libusb_global_linux.h \ # PTHREAD_LIBS="-lpthread -lrt" # freebsd-make COMPAT32_BIT=YES \ # LIBUSB_GLOBAL_INCLUDE_FILE=libusb_global_linux.h \ # PTHREAD_LIBS="-lpthread -lrt" # # .if defined(LIBUSB_GLOBAL_INCLUDE_FILE) CFLAGS+= -DLIBUSB_GLOBAL_INCLUDE_FILE=\"${LIBUSB_GLOBAL_INCLUDE_FILE}\" CFLAGS+= -DUSB_GLOBAL_INCLUDE_FILE=\"${LIBUSB_GLOBAL_INCLUDE_FILE}\" CFLAGS+= -I ../../sys .endif # LibUSB v1.0 MLINKS += libusb.3 libusb_get_version.3 MLINKS += libusb.3 libusb_init.3 +MLINKS += libusb.3 libusb_init_context.3 MLINKS += libusb.3 libusb_exit.3 MLINKS += libusb.3 libusb_has_capability.3 MLINKS += libusb.3 libusb_strerror.3 MLINKS += libusb.3 libusb_error_name.3 MLINKS += libusb.3 libusb_set_debug.3 MLINKS += libusb.3 libusb_get_device_list.3 MLINKS += libusb.3 libusb_free_device_list.3 MLINKS += libusb.3 libusb_get_bus_number.3 MLINKS += libusb.3 libusb_get_port_number.3 MLINKS += libusb.3 libusb_get_device_address.3 MLINKS += libusb.3 libusb_get_device_speed.3 MLINKS += libusb.3 libusb_get_max_packet_size.3 MLINKS += libusb.3 libusb_get_max_iso_packet_size.3 MLINKS += libusb.3 libusb_ref_device.3 MLINKS += libusb.3 libusb_unref_device.3 MLINKS += libusb.3 libusb_open.3 MLINKS += libusb.3 libusb_open_device_with_vid_pid.3 MLINKS += libusb.3 libusb_close.3 MLINKS += libusb.3 libusb_get_device.3 MLINKS += libusb.3 libusb_get_configuration.3 MLINKS += libusb.3 libusb_set_configuration.3 MLINKS += libusb.3 libusb_claim_interface.3 MLINKS += libusb.3 libusb_release_interface.3 MLINKS += libusb.3 libusb_set_interface_alt_setting.3 MLINKS += libusb.3 libusb_clear_halt.3 MLINKS += libusb.3 libusb_reset_device.3 MLINKS += libusb.3 libusb_check_connected.3 MLINKS += libusb.3 libusb_kernel_driver_active.3 MLINKS += libusb.3 libusb_get_driver.3 MLINKS += libusb.3 libusb_get_driver_np.3 MLINKS += libusb.3 libusb_detach_kernel_driver.3 MLINKS += libusb.3 libusb_detach_kernel_driver_np.3 MLINKS += libusb.3 libusb_attach_kernel_driver.3 MLINKS += libusb.3 libusb_set_auto_detach_kernel_driver.3 MLINKS += libusb.3 libusb_get_device_descriptor.3 MLINKS += libusb.3 libusb_get_active_config_descriptor.3 MLINKS += libusb.3 libusb_get_config_descriptor.3 MLINKS += libusb.3 libusb_get_config_descriptor_by_value.3 MLINKS += libusb.3 libusb_free_config_descriptor.3 MLINKS += libusb.3 libusb_get_string_descriptor.3 MLINKS += libusb.3 libusb_get_string_descriptor_ascii.3 MLINKS += libusb.3 libusb_parse_ss_endpoint_comp.3 MLINKS += libusb.3 libusb_free_ss_endpoint_comp.3 MLINKS += libusb.3 libusb_get_ss_endpoint_companion_descriptor.3 MLINKS += libusb.3 libusb_free_ss_endpoint_companion_descriptor.3 MLINKS += libusb.3 libusb_parse_bos_descriptor.3 MLINKS += libusb.3 libusb_free_bos_descriptor.3 MLINKS += libusb.3 libusb_get_usb_2_0_extension_descriptor.3 MLINKS += libusb.3 libusb_free_usb_2_0_extension_descriptor.3 MLINKS += libusb.3 libusb_get_ss_usb_device_capability_descriptor.3 MLINKS += libusb.3 libusb_free_ss_usb_device_capability_descriptor.3 MLINKS += libusb.3 libusb_get_container_id_descriptor.3 MLINKS += libusb.3 libusb_free_container_id_descriptor.3 MLINKS += libusb.3 libusb_alloc_streams.3 MLINKS += libusb.3 libusb_free_streams.3 MLINKS += libusb.3 libusb_alloc_transfer.3 MLINKS += libusb.3 libusb_free_transfer.3 MLINKS += libusb.3 libusb_transfer_set_stream_id.3 MLINKS += libusb.3 libusb_transfer_get_stream_id.3 MLINKS += libusb.3 libusb_submit_transfer.3 MLINKS += libusb.3 libusb_cancel_transfer.3 MLINKS += libusb.3 libusb_control_transfer.3 MLINKS += libusb.3 libusb_bulk_transfer.3 MLINKS += libusb.3 libusb_interrupt_transfer.3 MLINKS += libusb.3 libusb_try_lock_events.3 MLINKS += libusb.3 libusb_lock_events.3 MLINKS += libusb.3 libusb_unlock_events.3 MLINKS += libusb.3 libusb_event_handling_ok.3 MLINKS += libusb.3 libusb_event_handler_active.3 MLINKS += libusb.3 libusb_interrupt_event_handler.3 MLINKS += libusb.3 libusb_lock_event_waiters.3 MLINKS += libusb.3 libusb_unlock_event_waiters.3 MLINKS += libusb.3 libusb_wait_for_event.3 MLINKS += libusb.3 libusb_handle_events_timeout_completed.3 MLINKS += libusb.3 libusb_handle_events_completed.3 MLINKS += libusb.3 libusb_handle_events_timeout.3 MLINKS += libusb.3 libusb_handle_events.3 MLINKS += libusb.3 libusb_handle_events_locked.3 MLINKS += libusb.3 libusb_get_next_timeout.3 MLINKS += libusb.3 libusb_set_pollfd_notifiers.3 MLINKS += libusb.3 libusb_get_pollfds.3 MLINKS += libusb.3 libusb_hotplug_register_callback.3 MLINKS += libusb.3 libusb_hotplug_deregister_callback.3 # LibUSB v0.1 MLINKS += libusb.3 usb_open.3 MLINKS += libusb.3 usb_close.3 MLINKS += libusb.3 usb_get_string.3 MLINKS += libusb.3 usb_get_string_simple.3 MLINKS += libusb.3 usb_get_descriptor_by_endpoint.3 MLINKS += libusb.3 usb_get_descriptor.3 MLINKS += libusb.3 usb_parse_descriptor.3 MLINKS += libusb.3 usb_parse_configuration.3 MLINKS += libusb.3 usb_destroy_configuration.3 MLINKS += libusb.3 usb_fetch_and_parse_descriptors.3 MLINKS += libusb.3 usb_bulk_write.3 MLINKS += libusb.3 usb_bulk_read.3 MLINKS += libusb.3 usb_interrupt_write.3 MLINKS += libusb.3 usb_interrupt_read.3 MLINKS += libusb.3 usb_control_msg.3 MLINKS += libusb.3 usb_set_configuration.3 MLINKS += libusb.3 usb_claim_interface.3 MLINKS += libusb.3 usb_release_interface.3 MLINKS += libusb.3 usb_set_altinterface.3 MLINKS += libusb.3 usb_resetep.3 MLINKS += libusb.3 usb_clear_halt.3 MLINKS += libusb.3 usb_reset.3 MLINKS += libusb.3 usb_strerror.3 MLINKS += libusb.3 usb_init.3 MLINKS += libusb.3 usb_set_debug.3 MLINKS += libusb.3 usb_find_busses.3 MLINKS += libusb.3 usb_find_devices.3 MLINKS += libusb.3 usb_device.3 MLINKS += libusb.3 usb_get_busses.3 MLINKS += libusb.3 usb_check_connected.3 # LibUSB v2.0 MLINKS += libusb20.3 libusb20_tr_close.3 MLINKS += libusb20.3 libusb20_tr_open.3 MLINKS += libusb20.3 libusb20_tr_get_pointer.3 MLINKS += libusb20.3 libusb20_tr_get_time_complete.3 MLINKS += libusb20.3 libusb20_tr_get_actual_frames.3 MLINKS += libusb20.3 libusb20_tr_get_actual_length.3 MLINKS += libusb20.3 libusb20_tr_get_max_frames.3 MLINKS += libusb20.3 libusb20_tr_get_max_packet_length.3 MLINKS += libusb20.3 libusb20_tr_get_max_total_length.3 MLINKS += libusb20.3 libusb20_tr_get_status.3 MLINKS += libusb20.3 libusb20_tr_pending.3 MLINKS += libusb20.3 libusb20_tr_callback_wrapper.3 MLINKS += libusb20.3 libusb20_tr_clear_stall_sync.3 MLINKS += libusb20.3 libusb20_tr_drain.3 MLINKS += libusb20.3 libusb20_tr_set_buffer.3 MLINKS += libusb20.3 libusb20_tr_set_callback.3 MLINKS += libusb20.3 libusb20_tr_set_flags.3 MLINKS += libusb20.3 libusb20_tr_get_length.3 MLINKS += libusb20.3 libusb20_tr_set_length.3 MLINKS += libusb20.3 libusb20_tr_set_priv_sc0.3 MLINKS += libusb20.3 libusb20_tr_set_priv_sc1.3 MLINKS += libusb20.3 libusb20_tr_set_timeout.3 MLINKS += libusb20.3 libusb20_tr_set_total_frames.3 MLINKS += libusb20.3 libusb20_tr_setup_bulk.3 MLINKS += libusb20.3 libusb20_tr_setup_control.3 MLINKS += libusb20.3 libusb20_tr_setup_intr.3 MLINKS += libusb20.3 libusb20_tr_setup_isoc.3 MLINKS += libusb20.3 libusb20_tr_bulk_intr_sync.3 MLINKS += libusb20.3 libusb20_tr_start.3 MLINKS += libusb20.3 libusb20_tr_stop.3 MLINKS += libusb20.3 libusb20_tr_submit.3 MLINKS += libusb20.3 libusb20_tr_get_priv_sc0.3 MLINKS += libusb20.3 libusb20_tr_get_priv_sc1.3 MLINKS += libusb20.3 libusb20_dev_get_backend_name.3 MLINKS += libusb20.3 libusb20_dev_get_info.3 MLINKS += libusb20.3 libusb20_dev_get_iface_desc.3 MLINKS += libusb20.3 libusb20_dev_get_desc.3 MLINKS += libusb20.3 libusb20_dev_get_stats.3 MLINKS += libusb20.3 libusb20_dev_close.3 MLINKS += libusb20.3 libusb20_dev_detach_kernel_driver.3 MLINKS += libusb20.3 libusb20_dev_set_config_index.3 MLINKS += libusb20.3 libusb20_dev_get_debug.3 MLINKS += libusb20.3 libusb20_dev_get_fd.3 MLINKS += libusb20.3 libusb20_dev_kernel_driver_active.3 MLINKS += libusb20.3 libusb20_dev_open.3 MLINKS += libusb20.3 libusb20_dev_process.3 MLINKS += libusb20.3 libusb20_dev_request_sync.3 MLINKS += libusb20.3 libusb20_dev_req_string_sync.3 MLINKS += libusb20.3 libusb20_dev_req_string_simple_sync.3 MLINKS += libusb20.3 libusb20_dev_reset.3 MLINKS += libusb20.3 libusb20_dev_check_connected.3 MLINKS += libusb20.3 libusb20_dev_set_power_mode.3 MLINKS += libusb20.3 libusb20_dev_get_power_mode.3 MLINKS += libusb20.3 libusb20_dev_get_port_path.3 MLINKS += libusb20.3 libusb20_dev_get_power_usage.3 MLINKS += libusb20.3 libusb20_dev_set_alt_index.3 MLINKS += libusb20.3 libusb20_dev_get_device_desc.3 MLINKS += libusb20.3 libusb20_dev_alloc_config.3 MLINKS += libusb20.3 libusb20_dev_alloc.3 MLINKS += libusb20.3 libusb20_dev_get_address.3 MLINKS += libusb20.3 libusb20_dev_get_parent_address.3 MLINKS += libusb20.3 libusb20_dev_get_parent_port.3 MLINKS += libusb20.3 libusb20_dev_get_bus_number.3 MLINKS += libusb20.3 libusb20_dev_get_mode.3 MLINKS += libusb20.3 libusb20_dev_get_speed.3 MLINKS += libusb20.3 libusb20_dev_get_config_index.3 MLINKS += libusb20.3 libusb20_dev_free.3 MLINKS += libusb20.3 libusb20_dev_set_debug.3 MLINKS += libusb20.3 libusb20_dev_wait_process.3 MLINKS += libusb20.3 libusb20_be_get_template.3 MLINKS += libusb20.3 libusb20_be_set_template.3 MLINKS += libusb20.3 libusb20_be_get_dev_quirk.3 MLINKS += libusb20.3 libusb20_be_get_quirk_name.3 MLINKS += libusb20.3 libusb20_be_add_dev_quirk.3 MLINKS += libusb20.3 libusb20_be_remove_dev_quirk.3 MLINKS += libusb20.3 libusb20_be_alloc_default.3 MLINKS += libusb20.3 libusb20_be_device_foreach.3 MLINKS += libusb20.3 libusb20_be_dequeue_device.3 MLINKS += libusb20.3 libusb20_be_enqueue_device.3 MLINKS += libusb20.3 libusb20_be_free.3 MLINKS += libusb20.3 libusb20_me_get_1.3 MLINKS += libusb20.3 libusb20_me_get_2.3 MLINKS += libusb20.3 libusb20_me_encode.3 MLINKS += libusb20.3 libusb20_me_decode.3 MLINKS += libusb20.3 libusb20_desc_foreach.3 MLINKS += libusb20.3 libusb20_strerror.3 MLINKS += libusb20.3 libusb20_error_name.3 .include diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3 index d4c638d986e4..fe56639fa59f 100644 --- a/lib/libusb/libusb.3 +++ b/lib/libusb/libusb.3 @@ -1,792 +1,807 @@ .\" .\" Copyright (c) 2009 Sylvestre Gallon .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" -.Dd October, 2, 2022 +.Dd January, 26, 2023 .Dt LIBUSB 3 .Os .Sh NAME .Nm libusb .Nd "USB access library" .Sh LIBRARY USB access library .Pq libusb, -lusb .Sh SYNOPSIS .In libusb.h .Sh DESCRIPTION The .Nm library contains interfaces for directly managing a usb device. The current implementation supports v1.0 of the libusb API. .Sh LIBRARY INITIALISATION AND DEINITIALISATION .Ft "const struct libusb_version *" .Fn libusb_get_version "void" This function returns version information about LibUSB. .Pp .Ft int .Fn libusb_init "libusb_context **ctx" -This function initialises libusb. -It must be called at the beginning -of the program, before other libusb routines are used. -This function returns 0 on success or LIBUSB_ERROR on -failure. +Call this function before any other libusb v1.0 API function, to +initialise a valid libusb v1.0 context. +If the +.Fa ctx +argument is non-NULL, a pointer to the libusb context is stored at +the given location. +This function returns 0 upon success or LIBUSB_ERROR on failure. +.Pp +.Ft int +.Fn libusb_init_context "libusb_context **ctx" "const struct libusb_init_option []" "int num_options" +Call this function before any other libusb v1.0 API function, to +initialise a valid libusb v1.0 context. +If the +.Fa ctx +argument is non-NULL, a pointer to the libusb context is stored at +the given location. +Additional options, like the USB debug level, may be given using the +second and third argument. +If no options are needed, simply use libusb_init(). +This function returns 0 upon success or a LIBUSB_ERROR value on failure. .Pp .Ft void .Fn libusb_exit "libusb_context *ctx" Deinitialise libusb. Must be called at the end of the application. Other libusb routines may not be called after this function. .Pp .Ft int .Fn libusb_has_capability "uint32_t capability" This function checks the runtime capabilities of .Nm . This function will return non-zero if the given .Fa capability is supported, 0 if it is not supported. The valid values for .Fa capability are: .Bl -tag -width LIBUSB_CAP -offset indent .It Va LIBUSB_CAP_HAS_CAPABILITY .Nm supports .Fn libusb_has_capability . .It Va LIBUSB_CAP_HAS_HOTPLUG .Nm supports hotplug notifications. .It Va LIBUSB_CAP_HAS_HID_ACCESS .Nm can access HID devices without requiring user intervention. .It Va LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER .Nm supports detaching of the default USB driver with .Fn libusb_detach_kernel_driver . .El .Pp .Ft const char * .Fn libusb_strerror "int code" Get the ASCII representation of the error given by the .Fa code argument. This function does not return NULL. .Pp .Ft const char * .Fn libusb_error_name "int code" Get the ASCII representation of the error enum given by the .Fa code argument. This function does not return NULL. .Pp .Ft void .Fn libusb_set_debug "libusb_context *ctx" "int level" Set the debug level to .Fa level . .Pp .Ft ssize_t .Fn libusb_get_device_list "libusb_context *ctx" "libusb_device ***list" Populate .Fa list with the list of usb devices available, adding a reference to each device in the list. All the list entries created by this function must have their reference counter decremented when you are done with them, and the list itself must be freed. This function returns the number of devices in the list or a LIBUSB_ERROR code. .Pp .Ft void .Fn libusb_free_device_list "libusb_device **list" "int unref_devices" Free the list of devices discovered by libusb_get_device_list. If .Fa unref_device is set to 1 all devices in the list have their reference counter decremented once. .Pp .Ft uint8_t .Fn libusb_get_bus_number "libusb_device *dev" Returns the number of the bus contained by the device .Fa dev . .Pp .Ft uint8_t .Fn libusb_get_port_number "libusb_device *dev" Returns the port number which the device given by .Fa dev is attached to. .Pp .Ft int .Fn libusb_get_port_numbers "libusb_device *dev" "uint8_t *buf" "uint8_t bufsize" Stores, in the buffer .Fa buf of size .Fa bufsize , the list of all port numbers from root for the device .Fa dev . .Pp .Ft int .Fn libusb_get_port_path "libusb_context *ctx" "libusb_device *dev" "uint8_t *buf" "uint8_t bufsize" Deprecated function equivalent to libusb_get_port_numbers. .Pp .Ft uint8_t .Fn libusb_get_device_address "libusb_device *dev" Returns the device_address contained by the device .Fa dev . .Pp .Ft enum libusb_speed .Fn libusb_get_device_speed "libusb_device *dev" Returns the wire speed at which the device is connected. See the LIBUSB_SPEED_XXX enums for more information. LIBUSB_SPEED_UNKNOWN is returned in case of unknown wire speed. .Pp .Ft int .Fn libusb_get_max_packet_size "libusb_device *dev" "unsigned char endpoint" Returns the wMaxPacketSize value on success, LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist and LIBUSB_ERROR_OTHERS on other failure. .Pp .Ft int .Fn libusb_get_max_iso_packet_size "libusb_device *dev" "unsigned char endpoint" Returns the packet size multiplied by the packet multiplier on success, LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist and LIBUSB_ERROR_OTHERS on other failure. .Pp .Ft libusb_device * .Fn libusb_ref_device "libusb_device *dev" Increment the reference counter of the device .Fa dev . .Pp .Ft void .Fn libusb_unref_device "libusb_device *dev" Decrement the reference counter of the device .Fa dev . .Pp .Ft int .Fn libusb_open "libusb_device *dev" "libusb_device_handle **devh" Open a device and obtain a device_handle. Returns 0 on success, LIBUSB_ERROR_NO_MEM on memory allocation problems, LIBUSB_ERROR_ACCESS on permissions problems, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on other errors. .Pp .Ft libusb_device_handle * .Fn libusb_open_device_with_vid_pid "libusb_context *ctx" "uint16_t vid" "uint16_t pid" A convenience function to open a device by vendor and product IDs .Fa vid and .Fa pid . Returns NULL on error. .Pp .Ft void .Fn libusb_close "libusb_device_handle *devh" Close a device handle. .Pp .Ft libusb_device * .Fn libusb_get_device "libusb_device_handle *devh" Get the device contained by devh. Returns NULL on error. .Pp .Ft int .Fn libusb_get_configuration "libusb_device_handle *devh" "int *config" Returns the value of the current configuration. Returns 0 on success, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on error. .Pp .Ft int .Fn libusb_set_configuration "libusb_device_handle *devh" "int config" Set the active configuration to .Fa config for the device contained by .Fa devh . This function returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist, LIBUSB_ERROR_BUSY if the interfaces are currently claimed, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_claim_interface "libusb_device_handle *devh" "int interface_number" Claim an interface in a given libusb_handle .Fa devh . This is a non-blocking function. It returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the requested interface does not exist, LIBUSB_ERROR_BUSY if a program or driver has claimed the interface, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_release_interface "libusb_device_handle *devh" "int interface_number" This function releases an interface. All the claimed interfaces on a device must be released before closing the device. Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the interface was not claimed, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and LIBUSB_ERROR on failure. .Pp .Ft int .Fn libusb_set_interface_alt_setting "libusb_device_handle *dev" "int interface_number" "int alternate_setting" Activate an alternate setting for an interface. Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the interface was not claimed or the requested setting does not exist, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_clear_halt "libusb_device_handle *devh" "unsigned char endpoint" Clear an halt/stall for a endpoint. Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_reset_device "libusb_device_handle *devh" Perform an USB port reset for an usb device. Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if re-enumeration is required or if the device has been disconnected and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_check_connected "libusb_device_handle *devh" Test if the USB device is still connected. Returns 0 on success, LIBUSB_ERROR_NO_DEVICE if it has been disconnected and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_kernel_driver_active "libusb_device_handle *devh" "int interface" Determine if a driver is active on a interface. Returns 0 if no kernel driver is active and 1 if a kernel driver is active, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_get_driver "libusb_device_handle *devh" "int interface" "char *name" "int namelen" or .Ft int .Fn libusb_get_driver_np "libusb_device_handle *devh" "int interface" "char *name" "int namelen" Copy the name of the driver attached to the given .Fa device and .Fa interface into the buffer .Fa name of length .Fa namelen . Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if no kernel driver is attached to the given interface and LIBUSB_ERROR_INVALID_PARAM if the interface does not exist. This function is non-portable. The buffer pointed to by .Fa name is only zero terminated on success. .Pp .Ft int .Fn libusb_detach_kernel_driver "libusb_device_handle *devh" "int interface" or .Ft int .Fn libusb_detach_kernel_driver_np "libusb_device_handle *devh" "int interface" Detach a kernel driver from an interface. This is needed to claim an interface already claimed by a kernel driver. Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if no kernel driver was active, LIBUSB_ERROR_INVALID_PARAM if the interface does not exist, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on failure. This function is non-portable. .Pp .Ft int .Fn libusb_attach_kernel_driver "libusb_device_handle *devh" "int interface" Re-attach an interface kernel driver that was previously detached. Returns 0 on success, LIBUSB_ERROR_INVALID_PARAM if the interface does not exist, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected, LIBUSB_ERROR_BUSY if the driver cannot be attached because the interface is claimed by a program or driver and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_set_auto_detach_kernel_driver "libusb_device_handle *devh" "int enable" This function enables automatic kernel interface driver detach when an interface is claimed. When the interface is restored the kernel driver is allowed to be re-attached. If the .Fa enable argument is non-zero the feature is enabled. Else disabled. Returns 0 on success and a LIBUSB_ERROR code on failure. .Sh USB DESCRIPTORS .Ft int .Fn libusb_get_device_descriptor "libusb_device *dev" "libusb_device_descriptor *desc" Get the USB device descriptor for the device .Fa dev . This is a non-blocking function. Returns 0 on success and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_get_active_config_descriptor "libusb_device *dev" "struct libusb_config_descriptor **config" Get the USB configuration descriptor for the active configuration. Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the device is in an unconfigured state and a LIBUSB_ERROR code on error. .Pp .Ft int .Fn libusb_get_config_descriptor "libusb_device *dev" "uint8_t config_index" "libusb_config_descriptor **config" Get a USB configuration descriptor based on its index .Fa idx . Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the configuration does not exist and a LIBUSB_ERROR code on error. .Pp .Ft int .Fn libusb_get_config_descriptor_by_value "libusb_device *dev" "uint8 bConfigurationValue" "libusb_config_descriptor **config" Get a USB configuration descriptor with a specific bConfigurationValue. This is a non-blocking function which does not send a request through the device. Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the configuration does not exist and a LIBUSB_ERROR code on failure. .Pp .Ft void .Fn libusb_free_config_descriptor "libusb_config_descriptor *config" Free a configuration descriptor. .Pp .Ft int .Fn libusb_get_string_descriptor "libusb_device_handle *devh" "uint8_t desc_idx" "uint16_t langid" "unsigned char *data" "int length" Retrieve a string descriptor in raw format. Returns the number of bytes actually transferred on success or a negative LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_get_string_descriptor_ascii "libusb_device_handle *devh" "uint8_t desc_idx" "unsigned char *data" "int length" Retrieve a string descriptor in C style ASCII. Returns the positive number of bytes in the resulting ASCII string on success and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_parse_ss_endpoint_comp "const void *buf" "int len" "libusb_ss_endpoint_companion_descriptor **ep_comp" This function parses the USB 3.0 endpoint companion descriptor in host endian format pointed to by .Fa buf and having a length of .Fa len . Typically these arguments are the extra and extra_length fields of the endpoint descriptor. On success the pointer to resulting descriptor is stored at the location given by .Fa ep_comp . Returns zero on success and a LIBUSB_ERROR code on failure. On success the parsed USB 3.0 endpoint companion descriptor must be freed using the libusb_free_ss_endpoint_comp function. .Pp .Ft void .Fn libusb_free_ss_endpoint_comp "libusb_ss_endpoint_companion_descriptor *ep_comp" This function is NULL safe and frees a parsed USB 3.0 endpoint companion descriptor given by .Fa ep_comp . .Pp .Ft int .Fn libusb_get_ss_endpoint_companion_descriptor "struct libusb_context *ctx" "const struct libusb_endpoint_descriptor *endpoint" "struct libusb_ss_endpoint_companion_descriptor **ep_comp" This function finds and parses the USB 3.0 endpoint companion descriptor given by .Fa endpoint . Returns zero on success and a LIBUSB_ERROR code on failure. On success the parsed USB 3.0 endpoint companion descriptor must be freed using the libusb_free_ss_endpoint_companion_descriptor function. .Pp .Ft void .Fn libusb_free_ss_endpoint_companion_descriptor "struct libusb_ss_endpoint_companion_descriptor *ep_comp" This function is NULL safe and frees a parsed USB 3.0 endpoint companion descriptor given by .Fa ep_comp . .Pp .Ft int .Fn libusb_get_bos_descriptor "libusb_device_handle *handle" "struct libusb_bos_descriptor **bos" This function queries the USB device given by .Fa handle and stores a pointer to a parsed BOS descriptor into .Fa bos . Returns zero on success and a LIBUSB_ERROR code on failure. On success the parsed BOS descriptor must be freed using the libusb_free_bos_descriptor function. .Pp .Ft int .Fn libusb_parse_bos_descriptor "const void *buf" "int len" "libusb_bos_descriptor **bos" This function parses a Binary Object Store, BOS, descriptor into host endian format pointed to by .Fa buf and having a length of .Fa len . On success the pointer to resulting descriptor is stored at the location given by .Fa bos . Returns zero on success and a LIBUSB_ERROR code on failure. On success the parsed BOS descriptor must be freed using the libusb_free_bos_descriptor function. .Pp .Ft void .Fn libusb_free_bos_descriptor "libusb_bos_descriptor *bos" This function is NULL safe and frees a parsed BOS descriptor given by .Fa bos . .Pp .Ft int .Fn libusb_get_usb_2_0_extension_descriptor "struct libusb_context *ctx" "struct libusb_bos_dev_capability_descriptor *dev_cap" "struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension" This function parses the USB 2.0 extension descriptor from the descriptor given by .Fa dev_cap and stores a pointer to the parsed descriptor into .Fa usb_2_0_extension . Returns zero on success and a LIBUSB_ERROR code on failure. On success the parsed USB 2.0 extension descriptor must be freed using the libusb_free_usb_2_0_extension_descriptor function. .Pp .Ft void .Fn libusb_free_usb_2_0_extension_descriptor "struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension" This function is NULL safe and frees a parsed USB 2.0 extension descriptor given by .Fa usb_2_0_extension . .Pp .Ft int .Fn libusb_get_ss_usb_device_capability_descriptor "struct libusb_context *ctx" "struct libusb_bos_dev_capability_descriptor *dev_cap" "struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_capability" This function parses the SuperSpeed device capability descriptor from the descriptor given by .Fa dev_cap and stores a pointer to the parsed descriptor into .Fa ss_usb_device_capability . Returns zero on success and a LIBUSB_ERROR code on failure. On success the parsed SuperSpeed device capability descriptor must be freed using the libusb_free_ss_usb_device_capability_descriptor function. .Pp .Ft void .Fn libusb_free_ss_usb_device_capability_descriptor "struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_capability" This function is NULL safe and frees a parsed SuperSpeed device capability descriptor given by .Fa ss_usb_device_capability . .Pp .Ft int .Fn libusb_get_container_id_descriptor "struct libusb_context *ctx" "struct libusb_bos_dev_capability_descriptor *dev_cap" "struct libusb_container_id_descriptor **container_id" This function parses the container ID descriptor from the descriptor given by .Fa dev_cap and stores a pointer to the parsed descriptor into .Fa container_id . Returns zero on success and a LIBUSB_ERROR code on failure. On success the parsed container ID descriptor must be freed using the libusb_free_container_id_descriptor function. .Pp .Ft void .Fn libusb_free_container_id_descriptor "struct libusb_container_id_descriptor *container_id" This function is NULL safe and frees a parsed container ID descriptor given by .Fa container_id . .Sh USB ASYNCHRONOUS I/O .Ft struct libusb_transfer * .Fn libusb_alloc_transfer "int iso_packets" Allocate a transfer with the number of isochronous packet descriptors specified by .Fa iso_packets . Returns NULL on error. .Pp .Ft void .Fn libusb_free_transfer "struct libusb_transfer *tr" Free a transfer. .Pp .Ft int .Fn libusb_submit_transfer "struct libusb_transfer *tr" This function will submit a transfer and returns immediately. Returns 0 on success, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on other failure. .Pp .Ft int .Fn libusb_cancel_transfer "struct libusb_transfer *tr" This function asynchronously cancels a transfer. Returns 0 on success and a LIBUSB_ERROR code on failure. .Sh USB SYNCHRONOUS I/O .Ft int .Fn libusb_control_transfer "libusb_device_handle *devh" "uint8_t bmRequestType" "uint8_t bRequest" "uint16_t wValue" "uint16_t wIndex" "unsigned char *data" "uint16_t wLength" "unsigned int timeout" Perform a USB control transfer. Returns the actual number of bytes transferred on success, in the range from and including zero up to and including .Fa wLength . On error a LIBUSB_ERROR code is returned, for example LIBUSB_ERROR_TIMEOUT if the transfer timed out, LIBUSB_ERROR_PIPE if the control request was not supported, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and another LIBUSB_ERROR code on other failures. The LIBUSB_ERROR codes are all negative. .Pp .Ft int .Fn libusb_bulk_transfer "struct libusb_device_handle *devh" "unsigned char endpoint" "unsigned char *data" "int length" "int *transferred" "unsigned int timeout" Perform an USB bulk transfer. A timeout value of zero means no timeout. The timeout value is given in milliseconds. Returns 0 on success, LIBUSB_ERROR_TIMEOUT if the transfer timed out, LIBUSB_ERROR_PIPE if the control request was not supported, LIBUSB_ERROR_OVERFLOW if the device offered more data, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on other failure. .Pp .Ft int .Fn libusb_interrupt_transfer "struct libusb_device_handle *devh" "unsigned char endpoint" "unsigned char *data" "int length" "int *transferred" "unsigned int timeout" Perform an USB Interrupt transfer. A timeout value of zero means no timeout. The timeout value is given in milliseconds. Returns 0 on success, LIBUSB_ERROR_TIMEOUT if the transfer timed out, LIBUSB_ERROR_PIPE if the control request was not supported, LIBUSB_ERROR_OVERFLOW if the device offered more data, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a LIBUSB_ERROR code on other failure. .Sh USB STREAMS SUPPORT .Ft int .Fn libusb_alloc_streams "libusb_device_handle *dev" "uint32_t num_streams" "unsigned char *endpoints" "int num_endpoints" This function verifies that the given number of streams using the given number of endpoints is allowed and allocates the resources needed to use so-called USB streams. Currently only a single stream per endpoint is supported to simplify the internals of LibUSB. This function returns 0 on success or a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_free_streams "libusb_device_handle *dev" "unsigned char *endpoints" "int num_endpoints" This function release resources needed for streams usage. Returns 0 on success or a LIBUSB_ERROR code on failure. .Pp .Ft void .Fn libusb_transfer_set_stream_id "struct libusb_transfer *transfer" "uint32_t stream_id" This function sets the stream ID for the given USB transfer. .Pp .Ft uint32_t .Fn libusb_transfer_get_stream_id "struct libusb_transfer *transfer" This function returns the stream ID for the given USB transfer. If no stream ID is used a value of zero is returned. .Sh USB EVENTS .Ft int .Fn libusb_try_lock_events "libusb_context *ctx" Try to acquire the event handling lock. Returns 0 if the lock was obtained and 1 if not. .Pp .Ft void .Fn libusb_lock_events "libusb_context *ctx" Acquire the event handling lock. This function is blocking. .Pp .Ft void .Fn libusb_unlock_events "libusb_context *ctx" Release the event handling lock. This will wake up any thread blocked on .Fn libusb_wait_for_event . .Pp .Ft int .Fn libusb_event_handling_ok "libusb_context *ctx" Determine if it still OK for this thread to be doing event handling. Returns 1 if event handling can start or continue. Returns 0 if this thread must give up the events lock. .Pp .Ft int .Fn libusb_event_handler_active "libusb_context *ctx" Determine if an active thread is handling events. Returns 1 if there is a thread handling events and 0 if there are no threads currently handling events. .Pp .Ft void .Fn libusb_interrupt_event_handler "libusb_context *ctx" Causes the .Fn libusb_handle_events familiy of functions to return to the caller one time. The .Fn libusb_handle_events functions may be called again after calling this function. .Pp .Ft void .Fn libusb_lock_event_waiters "libusb_context *ctx" Acquire the event_waiters lock. This lock is designed to be obtained in the situation where you want to be aware when events are completed, but some other thread is event handling so calling .Fn libusb_handle_events is not allowed. .Pp .Ft void .Fn libusb_unlock_event_waiters "libusb_context *ctx" Release the event_waiters lock. .Pp .Ft int .Fn libusb_wait_for_event "libusb_context *ctx" "struct timeval *tv" Wait for another thread to signal completion of an event. Must be called with the event waiters lock held, see .Fn libusb_lock_event_waiters . This will block until the timeout expires or a transfer completes or a thread releases the event handling lock through .Fn libusb_unlock_events . Returns 0 after a transfer completes or another thread stops event handling, and 1 if the timeout expired. .Pp .Ft int .Fn libusb_handle_events_timeout_completed "libusb_context *ctx" "struct timeval *tv" "int *completed" Handle any pending events by checking if timeouts have expired and by checking the set of file descriptors for activity. If the .Fa completed argument is not equal to NULL, this function will loop until a transfer completion callback sets the variable pointed to by the .Fa completed argument to non-zero. If the .Fa tv argument is not equal to NULL, this function will return LIBUSB_ERROR_TIMEOUT after the given timeout. Returns 0 on success, or a LIBUSB_ERROR code on failure or timeout. .Pp .Ft int .Fn libusb_handle_events_completed "libusb_context *ctx" "int *completed" Handle any pending events by checking the set of file descriptors for activity. If the .Fa completed argument is not equal to NULL, this function will loop until a transfer completion callback sets the variable pointed to by the .Fa completed argument to non-zero. Returns 0 on success, or a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_handle_events_timeout "libusb_context *ctx" "struct timeval *tv" Handle any pending events by checking if timeouts have expired and by checking the set of file descriptors for activity. Returns 0 on success, or a LIBUSB_ERROR code on failure or timeout. .Pp .Ft int .Fn libusb_handle_events "libusb_context *ctx" Handle any pending events in blocking mode with a sensible timeout. Returns 0 on success and a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_handle_events_locked "libusb_context *ctx" "struct timeval *tv" Handle any pending events by polling file descriptors, without checking if another thread is already doing so. Must be called with the event lock held. .Pp .Ft int .Fn libusb_get_next_timeout "libusb_context *ctx" "struct timeval *tv" Determine the next internal timeout that libusb needs to handle. Returns 0 if there are no pending timeouts, 1 if a timeout was returned, or a LIBUSB_ERROR code on failure or timeout. .Pp .Ft void .Fn libusb_set_pollfd_notifiers "libusb_context *ctx" "libusb_pollfd_added_cb added_cb" "libusb_pollfd_removed_cb remove_cb" "void *user_data" Register notification functions for file descriptor additions/removals. These functions will be invoked for every new or removed file descriptor that libusb uses as an event source. .Pp .Ft const struct libusb_pollfd ** .Fn libusb_get_pollfds "libusb_context *ctx" Retrieve a list of file descriptors that should be polled by your main loop as libusb event sources. Returns a NULL-terminated list on success or NULL on failure. .Pp .Ft int .Fn libusb_hotplug_register_callback "libusb_context *ctx" "libusb_hotplug_event events" "libusb_hotplug_flag flags" "int vendor_id" "int product_id" "int dev_class" "libusb_hotplug_callback_fn cb_fn" "void *user_data" "libusb_hotplug_callback_handle *handle" This function registers a hotplug filter. The .Fa events argument select which events makes the hotplug filter trigger. Available event values are LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED and LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT. One or more events must be specified. The .Fa vendor_id , .Fa product_id and .Fa dev_class arguments can be set to LIBUSB_HOTPLUG_MATCH_ANY to match any value in the USB device descriptor. Else the specified value is used for matching. If the .Fa flags argument is set to LIBUSB_HOTPLUG_ENUMERATE, all currently attached and matching USB devices will be passed to the hotplug filter, given by the .Fa cb_fn argument. Else the .Fa flags argument should be set to LIBUSB_HOTPLUG_NO_FLAGS. This function returns 0 upon success or a LIBUSB_ERROR code on failure. .Pp .Ft int .Fn libusb_hotplug_callback_fn "libusb_context *ctx" "libusb_device *device" "libusb_hotplug_event event" "void *user_data" The hotplug filter function. If this function returns non-zero, the filter is removed. Else the filter is kept and can receive more events. The .Fa user_data argument is the same as given when the filter was registered. The .Fa event argument can be either of LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED or LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT. .Pp .Ft void .Fn libusb_hotplug_deregister_callback "libusb_context *ctx" "libusb_hotplug_callback_handle handle" This function unregisters a hotplug filter. .Sh LIBUSB VERSION 0.1 COMPATIBILITY The library is also compliant with LibUSB version 0.1.12. .Pp .Fn usb_open .Fn usb_close .Fn usb_get_string .Fn usb_get_string_simple .Fn usb_get_descriptor_by_endpoint .Fn usb_get_descriptor .Fn usb_parse_descriptor .Fn usb_parse_configuration .Fn usb_destroy_configuration .Fn usb_fetch_and_parse_descriptors .Fn usb_bulk_write .Fn usb_bulk_read .Fn usb_interrupt_write .Fn usb_interrupt_read .Fn usb_control_msg .Fn usb_set_configuration .Fn usb_claim_interface .Fn usb_release_interface .Fn usb_set_altinterface .Fn usb_resetep .Fn usb_clear_halt .Fn usb_reset .Fn usb_strerror .Fn usb_init .Fn usb_set_debug .Fn usb_find_busses .Fn usb_find_devices .Fn usb_device .Fn usb_get_busses .Fn usb_check_connected .Fn usb_get_driver_np .Fn usb_detach_kernel_driver_np .Sh SEE ALSO .Xr libusb20 3 , .Xr usb 4 , .Xr usbconfig 8 , .Xr usbdump 8 .Pp .Lk https://libusb.info/ .Sh HISTORY .Nm support first appeared in .Fx 8.0 . diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h index 9eaee671b8b3..28fbece4955a 100644 --- a/lib/libusb/libusb.h +++ b/lib/libusb/libusb.h @@ -1,600 +1,616 @@ /* $FreeBSD$ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef __LIBUSB_H__ #define __LIBUSB_H__ #ifndef LIBUSB_GLOBAL_INCLUDE_FILE #include #include #include #endif #define LIBUSB_API_VERSION 0x01000102 #define LIBUSB_CALL #ifdef __cplusplus extern "C" { #endif #if 0 } /* indent fix */ #endif /* libusb enums */ enum libusb_class_code { LIBUSB_CLASS_PER_INTERFACE = 0, LIBUSB_CLASS_AUDIO = 1, LIBUSB_CLASS_COMM = 2, LIBUSB_CLASS_HID = 3, LIBUSB_CLASS_PHYSICAL = 5, LIBUSB_CLASS_PTP = 6, LIBUSB_CLASS_IMAGE = 6, LIBUSB_CLASS_PRINTER = 7, LIBUSB_CLASS_MASS_STORAGE = 8, LIBUSB_CLASS_HUB = 9, LIBUSB_CLASS_DATA = 10, LIBUSB_CLASS_SMART_CARD = 11, LIBUSB_CLASS_CONTENT_SECURITY = 13, LIBUSB_CLASS_VIDEO = 14, LIBUSB_CLASS_PERSONAL_HEALTHCARE = 15, LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc, LIBUSB_CLASS_WIRELESS = 0xe0, LIBUSB_CLASS_APPLICATION = 0xfe, LIBUSB_CLASS_VENDOR_SPEC = 0xff, }; enum libusb_descriptor_type { LIBUSB_DT_DEVICE = 0x01, LIBUSB_DT_CONFIG = 0x02, LIBUSB_DT_STRING = 0x03, LIBUSB_DT_INTERFACE = 0x04, LIBUSB_DT_ENDPOINT = 0x05, LIBUSB_DT_HID = 0x21, LIBUSB_DT_REPORT = 0x22, LIBUSB_DT_PHYSICAL = 0x23, LIBUSB_DT_HUB = 0x29, LIBUSB_DT_BOS = 0x0f, LIBUSB_DT_DEVICE_CAPABILITY = 0x10, LIBUSB_DT_SS_ENDPOINT_COMPANION = 0x30, }; enum libusb_device_capability_type { LIBUSB_WIRELESS_USB_DEVICE_CAPABILITY = 0x1, LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY = 0x2, LIBUSB_SS_USB_DEVICE_CAPABILITY = 0x3, LIBUSB_CONTAINER_ID_DEVICE_CAPABILITY = 0x4, }; #define LIBUSB_DT_DEVICE_SIZE 18 #define LIBUSB_DT_CONFIG_SIZE 9 #define LIBUSB_DT_INTERFACE_SIZE 9 #define LIBUSB_DT_ENDPOINT_SIZE 7 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 #define LIBUSB_DT_HUB_NONVAR_SIZE 7 #define LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE 6 #define LIBUSB_DT_BOS_SIZE 5 #define LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY_SIZE 7 #define LIBUSB_SS_USB_DEVICE_CAPABILITY_SIZE 10 #define LIBUSB_BT_USB_2_0_EXTENSION_SIZE 7 #define LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE 10 #define LIBUSB_BT_CONTAINER_ID_SIZE 20 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f #define LIBUSB_ENDPOINT_DIR_MASK 0x80 enum libusb_endpoint_direction { LIBUSB_ENDPOINT_IN = 0x80, LIBUSB_ENDPOINT_OUT = 0x00, }; #define LIBUSB_TRANSFER_TYPE_MASK 0x03 enum libusb_transfer_type { LIBUSB_TRANSFER_TYPE_CONTROL = 0, LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, LIBUSB_TRANSFER_TYPE_BULK = 2, LIBUSB_TRANSFER_TYPE_INTERRUPT = 3, }; enum libusb_standard_request { LIBUSB_REQUEST_GET_STATUS = 0x00, LIBUSB_REQUEST_CLEAR_FEATURE = 0x01, LIBUSB_REQUEST_SET_FEATURE = 0x03, LIBUSB_REQUEST_SET_ADDRESS = 0x05, LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06, LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07, LIBUSB_REQUEST_GET_CONFIGURATION = 0x08, LIBUSB_REQUEST_SET_CONFIGURATION = 0x09, LIBUSB_REQUEST_GET_INTERFACE = 0x0A, LIBUSB_REQUEST_SET_INTERFACE = 0x0B, LIBUSB_REQUEST_SYNCH_FRAME = 0x0C, LIBUSB_REQUEST_SET_SEL = 0x30, LIBUSB_REQUEST_SET_ISOCH_DELAY = 0x31, }; enum libusb_request_type { LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5), LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5), LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5), LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5), }; enum libusb_request_recipient { LIBUSB_RECIPIENT_DEVICE = 0x00, LIBUSB_RECIPIENT_INTERFACE = 0x01, LIBUSB_RECIPIENT_ENDPOINT = 0x02, LIBUSB_RECIPIENT_OTHER = 0x03, }; #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C enum libusb_iso_sync_type { LIBUSB_ISO_SYNC_TYPE_NONE = 0, LIBUSB_ISO_SYNC_TYPE_ASYNC = 1, LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2, LIBUSB_ISO_SYNC_TYPE_SYNC = 3, }; #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30 enum libusb_iso_usage_type { LIBUSB_ISO_USAGE_TYPE_DATA = 0, LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1, LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2, }; enum libusb_bos_type { LIBUSB_BT_WIRELESS_USB_DEVICE_CAPABILITY = 1, LIBUSB_BT_USB_2_0_EXTENSION = 2, LIBUSB_BT_SS_USB_DEVICE_CAPABILITY = 3, LIBUSB_BT_CONTAINER_ID = 4, }; enum libusb_capability { /* libusb supports libusb_has_capability(). */ LIBUSB_CAP_HAS_CAPABILITY = 0, /* Hotplug support is available. */ LIBUSB_CAP_HAS_HOTPLUG, /* Can access HID devices without requiring user intervention. */ LIBUSB_CAP_HAS_HID_ACCESS, /* * Supports detaching of the default USB driver with * libusb_detach_kernel_driver(). */ LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER, }; enum libusb_error { LIBUSB_SUCCESS = 0, LIBUSB_ERROR_IO = -1, LIBUSB_ERROR_INVALID_PARAM = -2, LIBUSB_ERROR_ACCESS = -3, LIBUSB_ERROR_NO_DEVICE = -4, LIBUSB_ERROR_NOT_FOUND = -5, LIBUSB_ERROR_BUSY = -6, LIBUSB_ERROR_TIMEOUT = -7, LIBUSB_ERROR_OVERFLOW = -8, LIBUSB_ERROR_PIPE = -9, LIBUSB_ERROR_INTERRUPTED = -10, LIBUSB_ERROR_NO_MEM = -11, LIBUSB_ERROR_NOT_SUPPORTED = -12, LIBUSB_ERROR_OTHER = -99, }; enum libusb_speed { LIBUSB_SPEED_UNKNOWN = 0, LIBUSB_SPEED_LOW = 1, LIBUSB_SPEED_FULL = 2, LIBUSB_SPEED_HIGH = 3, LIBUSB_SPEED_SUPER = 4, }; enum libusb_transfer_status { LIBUSB_TRANSFER_COMPLETED, LIBUSB_TRANSFER_ERROR, LIBUSB_TRANSFER_TIMED_OUT, LIBUSB_TRANSFER_CANCELLED, LIBUSB_TRANSFER_STALL, LIBUSB_TRANSFER_NO_DEVICE, LIBUSB_TRANSFER_OVERFLOW, }; enum libusb_transfer_flags { LIBUSB_TRANSFER_SHORT_NOT_OK = 1 << 0, LIBUSB_TRANSFER_FREE_BUFFER = 1 << 1, LIBUSB_TRANSFER_FREE_TRANSFER = 1 << 2, }; enum libusb_log_level { LIBUSB_LOG_LEVEL_NONE = 0, LIBUSB_LOG_LEVEL_ERROR, LIBUSB_LOG_LEVEL_WARNING, LIBUSB_LOG_LEVEL_INFO, LIBUSB_LOG_LEVEL_DEBUG }; /* XXX */ /* libusb_set_debug should take parameters from libusb_log_level * above according to * http://libusb.sourceforge.net/api-1.0/group__lib.html */ enum libusb_debug_level { LIBUSB_DEBUG_NO=0, LIBUSB_DEBUG_FUNCTION=1, LIBUSB_DEBUG_TRANSFER=2, }; #define LIBUSB_HOTPLUG_MATCH_ANY -1 typedef enum { LIBUSB_HOTPLUG_NO_FLAGS = 0, LIBUSB_HOTPLUG_ENUMERATE = 1 << 0, } libusb_hotplug_flag; typedef enum { LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 1, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 2, } libusb_hotplug_event; +enum libusb_option { + LIBUSB_OPTION_LOG_LEVEL = 0, + LIBUSB_OPTION_USE_USBDK = 1, + LIBUSB_OPTION_NO_DEVICE_DISCOVERY = 2, + LIBUSB_OPTION_WEAK_AUTHORITY = 2, + LIBUSB_OPTION_MAX = 3, +}; + /* libusb structures */ struct libusb_context; struct libusb_device; struct libusb_transfer; struct libusb_device_handle; struct libusb_hotplug_callback_handle_struct; struct libusb_pollfd { int fd; short events; }; struct libusb_version { const uint16_t major; const uint16_t minor; const uint16_t micro; const uint16_t nano; const char *rc; const char *describe; }; +struct libusb_init_option { + enum libusb_option option; + union { + int64_t ival; + } value; +}; + typedef struct libusb_context libusb_context; typedef struct libusb_device libusb_device; typedef struct libusb_device_handle libusb_device_handle; typedef struct libusb_pollfd libusb_pollfd; typedef void (*libusb_pollfd_added_cb) (int fd, short events, void *user_data); typedef void (*libusb_pollfd_removed_cb) (int fd, void *user_data); typedef struct libusb_hotplug_callback_handle_struct *libusb_hotplug_callback_handle; typedef struct libusb_device_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t bcdUSB; uint8_t bDeviceClass; uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize0; uint16_t idVendor; uint16_t idProduct; uint16_t bcdDevice; uint8_t iManufacturer; uint8_t iProduct; uint8_t iSerialNumber; uint8_t bNumConfigurations; } libusb_device_descriptor; typedef struct libusb_endpoint_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bEndpointAddress; uint8_t bmAttributes; uint16_t wMaxPacketSize; uint8_t bInterval; uint8_t bRefresh; uint8_t bSynchAddress; uint8_t *extra; int extra_length; } libusb_endpoint_descriptor __aligned(sizeof(void *)); typedef struct libusb_ss_endpoint_companion_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bMaxBurst; uint8_t bmAttributes; uint16_t wBytesPerInterval; } libusb_ss_endpoint_companion_descriptor __aligned(sizeof(void *)); typedef struct libusb_interface_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bInterfaceNumber; uint8_t bAlternateSetting; uint8_t bNumEndpoints; uint8_t bInterfaceClass; uint8_t bInterfaceSubClass; uint8_t bInterfaceProtocol; uint8_t iInterface; struct libusb_endpoint_descriptor *endpoint; uint8_t *extra; int extra_length; } libusb_interface_descriptor __aligned(sizeof(void *)); typedef struct libusb_interface { struct libusb_interface_descriptor *altsetting; int num_altsetting; } libusb_interface __aligned(sizeof(void *)); typedef struct libusb_config_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t MaxPower; struct libusb_interface *interface; uint8_t *extra; int extra_length; } libusb_config_descriptor __aligned(sizeof(void *)); typedef struct libusb_usb_2_0_device_capability_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bDevCapabilityType; uint32_t bmAttributes; #define LIBUSB_USB_2_0_CAPABILITY_LPM_SUPPORT (1 << 1) } libusb_usb_2_0_device_capability_descriptor __aligned(sizeof(void *)); typedef struct libusb_ss_usb_device_capability_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bDevCapabilityType; uint8_t bmAttributes; #define LIBUSB_SS_USB_CAPABILITY_LPM_SUPPORT (1 << 1) uint16_t wSpeedSupported; #define LIBUSB_CAPABILITY_LOW_SPEED_OPERATION (1) #define LIBUSB_CAPABILITY_FULL_SPEED_OPERATION (1 << 1) #define LIBUSB_CAPABILITY_HIGH_SPEED_OPERATION (1 << 2) #define LIBUSB_CAPABILITY_5GBPS_OPERATION (1 << 3) uint8_t bFunctionalitySupport; uint8_t bU1DevExitLat; uint16_t wU2DevExitLat; } libusb_ss_usb_device_capability_descriptor __aligned(sizeof(void *)); typedef struct libusb_bos_dev_capability_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bDevCapabilityType; uint8_t dev_capability_data[0]; } libusb_bos_dev_capability_descriptor __aligned(sizeof(void *)); typedef struct libusb_bos_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumDeviceCapabilities; struct libusb_usb_2_0_device_capability_descriptor *usb_2_0_ext_cap; struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap; struct libusb_bos_dev_capability_descriptor **dev_capability; } libusb_bos_descriptor __aligned(sizeof(void *)); typedef struct libusb_usb_2_0_extension_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bDevCapabilityType; uint32_t bmAttributes; } libusb_usb_2_0_extension_descriptor __aligned(sizeof(void *)); typedef struct libusb_container_id_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bDevCapabilityType; uint8_t bReserved; uint8_t ContainerID[16]; } libusb_container_id_descriptor __aligned(sizeof(void *)); typedef struct libusb_control_setup { uint8_t bmRequestType; uint8_t bRequest; uint16_t wValue; uint16_t wIndex; uint16_t wLength; } libusb_control_setup; #define LIBUSB_CONTROL_SETUP_SIZE 8 /* bytes */ typedef struct libusb_iso_packet_descriptor { uint32_t length; uint32_t actual_length; enum libusb_transfer_status status; } libusb_iso_packet_descriptor __aligned(sizeof(void *)); typedef void (*libusb_transfer_cb_fn) (struct libusb_transfer *transfer); typedef struct libusb_transfer { libusb_device_handle *dev_handle; uint8_t flags; uint8_t endpoint; uint8_t type; uint32_t timeout; enum libusb_transfer_status status; int length; int actual_length; libusb_transfer_cb_fn callback; void *user_data; uint8_t *buffer; int num_iso_packets; struct libusb_iso_packet_descriptor iso_packet_desc[0]; } libusb_transfer __aligned(sizeof(void *)); /* Library initialisation */ void libusb_set_debug(libusb_context * ctx, int level); const struct libusb_version *libusb_get_version(void); const char *libusb_strerror(int code); const char *libusb_error_name(int code); int libusb_init(libusb_context ** context); +int libusb_init_context(libusb_context **, const struct libusb_init_option [], int num_options); void libusb_exit(struct libusb_context *ctx); int libusb_has_capability(uint32_t capability); /* Device handling and enumeration */ ssize_t libusb_get_device_list(libusb_context * ctx, libusb_device *** list); void libusb_free_device_list(libusb_device ** list, int unref_devices); uint8_t libusb_get_bus_number(libusb_device * dev); uint8_t libusb_get_port_number(libusb_device * dev); int libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize); int libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf, uint8_t bufsize); uint8_t libusb_get_device_address(libusb_device * dev); enum libusb_speed libusb_get_device_speed(libusb_device * dev); int libusb_clear_halt(libusb_device_handle *devh, uint8_t endpoint); int libusb_get_max_packet_size(libusb_device * dev, uint8_t endpoint); int libusb_get_max_iso_packet_size(libusb_device * dev, uint8_t endpoint); libusb_device *libusb_ref_device(libusb_device * dev); void libusb_unref_device(libusb_device * dev); int libusb_open(libusb_device * dev, libusb_device_handle ** devh); libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context * ctx, uint16_t vendor_id, uint16_t product_id); void libusb_close(libusb_device_handle * devh); libusb_device *libusb_get_device(libusb_device_handle * devh); int libusb_get_configuration(libusb_device_handle * devh, int *config); int libusb_set_configuration(libusb_device_handle * devh, int configuration); int libusb_claim_interface(libusb_device_handle * devh, int interface_number); int libusb_release_interface(libusb_device_handle * devh, int interface_number); int libusb_reset_device(libusb_device_handle * devh); int libusb_check_connected(libusb_device_handle * devh); int libusb_kernel_driver_active(libusb_device_handle * devh, int interface); int libusb_get_driver_np(libusb_device_handle * devh, int interface, char *name, int namelen); int libusb_get_driver(libusb_device_handle * devh, int interface, char *name, int namelen); int libusb_detach_kernel_driver_np(libusb_device_handle * devh, int interface); int libusb_detach_kernel_driver(libusb_device_handle * devh, int interface); int libusb_attach_kernel_driver(libusb_device_handle * devh, int interface); int libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable); int libusb_set_interface_alt_setting(libusb_device_handle * devh, int interface_number, int alternate_setting); /* USB Descriptors */ int libusb_get_device_descriptor(libusb_device * dev, struct libusb_device_descriptor *desc); int libusb_get_active_config_descriptor(libusb_device * dev, struct libusb_config_descriptor **config); int libusb_get_config_descriptor(libusb_device * dev, uint8_t config_index, struct libusb_config_descriptor **config); int libusb_get_config_descriptor_by_value(libusb_device * dev, uint8_t bConfigurationValue, struct libusb_config_descriptor **config); void libusb_free_config_descriptor(struct libusb_config_descriptor *config); int libusb_get_ss_endpoint_companion_descriptor(struct libusb_context *ctx, const struct libusb_endpoint_descriptor *endpoint, struct libusb_ss_endpoint_companion_descriptor **ep_comp); void libusb_free_ss_endpoint_companion_descriptor(struct libusb_ss_endpoint_companion_descriptor *ep_comp); int libusb_get_string_descriptor(libusb_device_handle * devh, uint8_t desc_index, uint16_t langid, unsigned char *data, int length); int libusb_get_string_descriptor_ascii(libusb_device_handle * devh, uint8_t desc_index, uint8_t *data, int length); int libusb_get_descriptor(libusb_device_handle * devh, uint8_t desc_type, uint8_t desc_index, uint8_t *data, int length); int libusb_parse_ss_endpoint_comp(const void *buf, int len, struct libusb_ss_endpoint_companion_descriptor **ep_comp); void libusb_free_ss_endpoint_comp(struct libusb_ss_endpoint_companion_descriptor *ep_comp); int libusb_parse_bos_descriptor(const void *buf, int len, struct libusb_bos_descriptor **bos); void libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos); int libusb_get_bos_descriptor(libusb_device_handle *handle, struct libusb_bos_descriptor **bos); int libusb_get_usb_2_0_extension_descriptor(struct libusb_context *ctx, struct libusb_bos_dev_capability_descriptor *dev_cap, struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension); void libusb_free_usb_2_0_extension_descriptor(struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension); int libusb_get_ss_usb_device_capability_descriptor(struct libusb_context *ctx, struct libusb_bos_dev_capability_descriptor *dev_cap, struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_capability); void libusb_free_ss_usb_device_capability_descriptor(struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_capability); int libusb_get_container_id_descriptor(struct libusb_context *ctx, struct libusb_bos_dev_capability_descriptor *dev_cap, struct libusb_container_id_descriptor **container_id); void libusb_free_container_id_descriptor(struct libusb_container_id_descriptor *container_id); /* Asynchronous device I/O */ struct libusb_transfer *libusb_alloc_transfer(int iso_packets); void libusb_free_transfer(struct libusb_transfer *transfer); int libusb_submit_transfer(struct libusb_transfer *transfer); int libusb_cancel_transfer(struct libusb_transfer *transfer); uint8_t *libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index); uint8_t *libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index); void libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length); uint8_t *libusb_control_transfer_get_data(struct libusb_transfer *transfer); struct libusb_control_setup *libusb_control_transfer_get_setup(struct libusb_transfer *transfer); void libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength); void libusb_fill_control_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t *buf, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout); void libusb_fill_bulk_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, int length, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout); void libusb_fill_interrupt_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, int length, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout); void libusb_fill_iso_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, int length, int npacket, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout); /* Polling and timing */ int libusb_try_lock_events(libusb_context * ctx); void libusb_lock_events(libusb_context * ctx); void libusb_unlock_events(libusb_context * ctx); int libusb_event_handling_ok(libusb_context * ctx); int libusb_event_handler_active(libusb_context * ctx); void libusb_interrupt_event_handler(libusb_context *ctx); void libusb_lock_event_waiters(libusb_context * ctx); void libusb_unlock_event_waiters(libusb_context * ctx); int libusb_wait_for_event(libusb_context * ctx, struct timeval *tv); int libusb_handle_events_timeout_completed(libusb_context * ctx, struct timeval *tv, int *completed); int libusb_handle_events_completed(libusb_context * ctx, int *completed); int libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv); int libusb_handle_events(libusb_context * ctx); int libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv); int libusb_get_next_timeout(libusb_context * ctx, struct timeval *tv); void libusb_set_pollfd_notifiers(libusb_context * ctx, libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, void *user_data); const struct libusb_pollfd **libusb_get_pollfds(libusb_context * ctx); /* Synchronous device I/O */ int libusb_control_transfer(libusb_device_handle * devh, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t *data, uint16_t wLength, uint32_t timeout); int libusb_bulk_transfer(libusb_device_handle * devh, uint8_t endpoint, uint8_t *data, int length, int *transferred, uint32_t timeout); int libusb_interrupt_transfer(libusb_device_handle * devh, uint8_t endpoint, uint8_t *data, int length, int *transferred, uint32_t timeout); /* Byte-order */ uint16_t libusb_cpu_to_le16(uint16_t x); uint16_t libusb_le16_to_cpu(uint16_t x); /* Hotplug support */ typedef int (*libusb_hotplug_callback_fn)(libusb_context *ctx, libusb_device *device, libusb_hotplug_event event, void *user_data); int libusb_hotplug_register_callback(libusb_context *ctx, libusb_hotplug_event events, libusb_hotplug_flag flags, int vendor_id, int product_id, int dev_class, libusb_hotplug_callback_fn cb_fn, void *user_data, libusb_hotplug_callback_handle *handle); void libusb_hotplug_deregister_callback(libusb_context *ctx, libusb_hotplug_callback_handle handle); /* Streams support */ int libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams, unsigned char *endpoints, int num_endpoints); int libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints); void libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id); uint32_t libusb_transfer_get_stream_id(struct libusb_transfer *transfer); #if 0 { /* indent fix */ #endif #ifdef __cplusplus } #endif #endif /* __LIBUSB_H__ */ diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c index 1fc89fc409f0..793152a30910 100644 --- a/lib/libusb/libusb10.c +++ b/lib/libusb/libusb10.c @@ -1,1749 +1,1777 @@ /* $FreeBSD$ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. - * Copyright (c) 2009 Hans Petter Selasky. All rights reserved. + * Copyright (c) 2009-2023 Hans Petter Selasky * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifdef LIBUSB_GLOBAL_INCLUDE_FILE #include LIBUSB_GLOBAL_INCLUDE_FILE #else #include #include #include #include #include #include #include #include #include #include #include #include #include #include #endif #define libusb_device_handle libusb20_device #include "libusb20.h" #include "libusb20_desc.h" #include "libusb20_int.h" #include "libusb.h" #include "libusb10.h" #define LIBUSB_NUM_SW_ENDPOINTS (16 * 4) static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER; struct libusb_context *usbi_default_context = NULL; /* Prototypes */ static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t); static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *); static int libusb10_convert_error(uint8_t status); static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int); static void libusb10_isoc_proxy(struct libusb20_transfer *); static void libusb10_bulk_intr_proxy(struct libusb20_transfer *); static void libusb10_ctrl_proxy(struct libusb20_transfer *); static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t); /* Library initialisation / deinitialisation */ static const struct libusb_version libusb_version = { .major = 1, .minor = 0, .micro = 0, .nano = 2016, .rc = "", .describe = "https://www.freebsd.org" }; const struct libusb_version * libusb_get_version(void) { return (&libusb_version); } void libusb_set_debug(libusb_context *ctx, int level) { ctx = GET_CONTEXT(ctx); /* debug_fixed is set when the environment overrides libusb_set_debug */ if (ctx && ctx->debug_fixed == 0) ctx->debug = level; } static void libusb_set_nonblocking(int f) { int flags; /* * We ignore any failures in this function, hence the * non-blocking flag is not critical to the operation of * libUSB. We use F_GETFL and F_SETFL to be compatible with * Linux. */ flags = fcntl(f, F_GETFL, NULL); if (flags == -1) return; flags |= O_NONBLOCK; fcntl(f, F_SETFL, flags); } void libusb_interrupt_event_handler(libusb_context *ctx) { uint8_t dummy; int err; if (ctx == NULL) return; dummy = 0; err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); if (err < (int)sizeof(dummy)) { /* ignore error, if any */ DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "Waking up event loop failed!"); } } int libusb_init(libusb_context **context) +{ + return (libusb_init_context(context, NULL, 0)); +} + +int +libusb_init_context(libusb_context **context, + const struct libusb_init_option option[], int num_options) { struct libusb_context *ctx; pthread_condattr_t attr; char *debug, *ep; int ret; + if (num_options < 0) + return (LIBUSB_ERROR_INVALID_PARAM); + ctx = malloc(sizeof(*ctx)); if (!ctx) return (LIBUSB_ERROR_INVALID_PARAM); memset(ctx, 0, sizeof(*ctx)); debug = getenv("LIBUSB_DEBUG"); if (debug != NULL) { /* - * If LIBUSB_DEBUG is set, we'll honor that and use it to - * override libusb_set_debug calls. + * If LIBUSB_DEBUG is set, we'll honor that first and + * use it to override any future libusb_set_debug() + * calls or init options. */ errno = 0; ctx->debug = strtol(debug, &ep, 10); if (errno == 0 && *ep == '\0') { ctx->debug_fixed = 1; } else { /* * LIBUSB_DEBUG conversion failed for some reason, but * we don't care about the specifics all that much. We * can't use it either way. Force it to the default, * 0, in case we had a partial number. */ ctx->debug = 0; } + } else { + /* + * If the LIBUSB_OPTION_LOG_LEVEL is set, honor that. + */ + for (int i = 0; i != num_options; i++) { + if (option[i].option != LIBUSB_OPTION_LOG_LEVEL) + continue; + + ctx->debug = (int)option[i].value.ival; + if ((int64_t)ctx->debug == option[i].value.ival) { + ctx->debug_fixed = 1; + } else { + free(ctx); + return (LIBUSB_ERROR_INVALID_PARAM); + } + } } + TAILQ_INIT(&ctx->pollfds); TAILQ_INIT(&ctx->tr_done); TAILQ_INIT(&ctx->hotplug_cbh); TAILQ_INIT(&ctx->hotplug_devs); if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) { free(ctx); return (LIBUSB_ERROR_NO_MEM); } if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) { pthread_mutex_destroy(&ctx->ctx_lock); free(ctx); return (LIBUSB_ERROR_NO_MEM); } if (pthread_condattr_init(&attr) != 0) { pthread_mutex_destroy(&ctx->ctx_lock); pthread_mutex_destroy(&ctx->hotplug_lock); free(ctx); return (LIBUSB_ERROR_NO_MEM); } if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) { pthread_mutex_destroy(&ctx->ctx_lock); pthread_mutex_destroy(&ctx->hotplug_lock); pthread_condattr_destroy(&attr); free(ctx); return (LIBUSB_ERROR_OTHER); } if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) { pthread_mutex_destroy(&ctx->ctx_lock); pthread_mutex_destroy(&ctx->hotplug_lock); pthread_condattr_destroy(&attr); free(ctx); return (LIBUSB_ERROR_NO_MEM); } pthread_condattr_destroy(&attr); ctx->ctx_handler = NO_THREAD; ctx->hotplug_handler = NO_THREAD; ret = pipe(ctx->ctrl_pipe); if (ret < 0) { pthread_mutex_destroy(&ctx->ctx_lock); pthread_mutex_destroy(&ctx->hotplug_lock); pthread_cond_destroy(&ctx->ctx_cond); free(ctx); return (LIBUSB_ERROR_OTHER); } /* set non-blocking mode on the control pipe to avoid deadlock */ libusb_set_nonblocking(ctx->ctrl_pipe[0]); libusb_set_nonblocking(ctx->ctrl_pipe[1]); libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN); pthread_mutex_lock(&default_context_lock); if (usbi_default_context == NULL) { usbi_default_context = ctx; } pthread_mutex_unlock(&default_context_lock); if (context) *context = ctx; DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete"); signal(SIGPIPE, SIG_IGN); return (0); } void libusb_exit(libusb_context *ctx) { ctx = GET_CONTEXT(ctx); if (ctx == NULL) return; /* stop hotplug thread, if any */ if (ctx->hotplug_handler != NO_THREAD) { pthread_t td; void *ptr; HOTPLUG_LOCK(ctx); td = ctx->hotplug_handler; ctx->hotplug_handler = NO_THREAD; HOTPLUG_UNLOCK(ctx); pthread_join(td, &ptr); } /* XXX cleanup devices */ libusb10_remove_pollfd(ctx, &ctx->ctx_poll); close(ctx->ctrl_pipe[0]); close(ctx->ctrl_pipe[1]); pthread_mutex_destroy(&ctx->ctx_lock); pthread_mutex_destroy(&ctx->hotplug_lock); pthread_cond_destroy(&ctx->ctx_cond); pthread_mutex_lock(&default_context_lock); if (ctx == usbi_default_context) { usbi_default_context = NULL; } pthread_mutex_unlock(&default_context_lock); free(ctx); } /* Device handling and initialisation. */ ssize_t libusb_get_device_list(libusb_context *ctx, libusb_device ***list) { struct libusb20_backend *usb_backend; struct libusb20_device *pdev; struct libusb_device *dev; int i; ctx = GET_CONTEXT(ctx); if (ctx == NULL) return (LIBUSB_ERROR_INVALID_PARAM); if (list == NULL) return (LIBUSB_ERROR_INVALID_PARAM); usb_backend = libusb20_be_alloc_default(); if (usb_backend == NULL) return (LIBUSB_ERROR_NO_MEM); /* figure out how many USB devices are present */ pdev = NULL; i = 0; while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) i++; /* allocate device pointer list */ *list = malloc((i + 1) * sizeof(void *)); if (*list == NULL) { libusb20_be_free(usb_backend); return (LIBUSB_ERROR_NO_MEM); } /* create libusb v1.0 compliant devices */ i = 0; while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) { dev = malloc(sizeof(*dev)); if (dev == NULL) { while (i != 0) { libusb_unref_device((*list)[i - 1]); i--; } free(*list); *list = NULL; libusb20_be_free(usb_backend); return (LIBUSB_ERROR_NO_MEM); } /* get device into libUSB v1.0 list */ libusb20_be_dequeue_device(usb_backend, pdev); memset(dev, 0, sizeof(*dev)); /* init transfer queues */ TAILQ_INIT(&dev->tr_head); /* set context we belong to */ dev->ctx = ctx; /* link together the two structures */ dev->os_priv = pdev; pdev->privLuData = dev; (*list)[i] = libusb_ref_device(dev); i++; } (*list)[i] = NULL; libusb20_be_free(usb_backend); return (i); } void libusb_free_device_list(libusb_device **list, int unref_devices) { int i; if (list == NULL) return; /* be NULL safe */ if (unref_devices) { for (i = 0; list[i] != NULL; i++) libusb_unref_device(list[i]); } free(list); } uint8_t libusb_get_bus_number(libusb_device *dev) { if (dev == NULL) return (0); /* should not happen */ return (libusb20_dev_get_bus_number(dev->os_priv)); } uint8_t libusb_get_port_number(libusb_device *dev) { if (dev == NULL) return (0); /* should not happen */ return (libusb20_dev_get_parent_port(dev->os_priv)); } int libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize) { return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize)); } int libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf, uint8_t bufsize) { return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize)); } uint8_t libusb_get_device_address(libusb_device *dev) { if (dev == NULL) return (0); /* should not happen */ return (libusb20_dev_get_address(dev->os_priv)); } enum libusb_speed libusb_get_device_speed(libusb_device *dev) { if (dev == NULL) return (LIBUSB_SPEED_UNKNOWN); /* should not happen */ switch (libusb20_dev_get_speed(dev->os_priv)) { case LIBUSB20_SPEED_LOW: return (LIBUSB_SPEED_LOW); case LIBUSB20_SPEED_FULL: return (LIBUSB_SPEED_FULL); case LIBUSB20_SPEED_HIGH: return (LIBUSB_SPEED_HIGH); case LIBUSB20_SPEED_SUPER: return (LIBUSB_SPEED_SUPER); default: break; } return (LIBUSB_SPEED_UNKNOWN); } int libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint) { struct libusb_config_descriptor *pdconf; struct libusb_interface *pinf; struct libusb_interface_descriptor *pdinf; struct libusb_endpoint_descriptor *pdend; int i; int j; int k; int ret; if (dev == NULL) return (LIBUSB_ERROR_NO_DEVICE); ret = libusb_get_active_config_descriptor(dev, &pdconf); if (ret < 0) return (ret); ret = LIBUSB_ERROR_NOT_FOUND; for (i = 0; i < pdconf->bNumInterfaces; i++) { pinf = &pdconf->interface[i]; for (j = 0; j < pinf->num_altsetting; j++) { pdinf = &pinf->altsetting[j]; for (k = 0; k < pdinf->bNumEndpoints; k++) { pdend = &pdinf->endpoint[k]; if (pdend->bEndpointAddress == endpoint) { ret = pdend->wMaxPacketSize; goto out; } } } } out: libusb_free_config_descriptor(pdconf); return (ret); } int libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint) { int multiplier; int ret; ret = libusb_get_max_packet_size(dev, endpoint); switch (libusb20_dev_get_speed(dev->os_priv)) { case LIBUSB20_SPEED_LOW: case LIBUSB20_SPEED_FULL: break; default: if (ret > -1) { multiplier = (1 + ((ret >> 11) & 3)); if (multiplier > 3) multiplier = 3; ret = (ret & 0x7FF) * multiplier; } break; } return (ret); } libusb_device * libusb_ref_device(libusb_device *dev) { if (dev == NULL) return (NULL); /* be NULL safe */ CTX_LOCK(dev->ctx); dev->refcnt++; CTX_UNLOCK(dev->ctx); return (dev); } void libusb_unref_device(libusb_device *dev) { if (dev == NULL) return; /* be NULL safe */ CTX_LOCK(dev->ctx); dev->refcnt--; CTX_UNLOCK(dev->ctx); if (dev->refcnt == 0) { libusb20_dev_free(dev->os_priv); free(dev); } } int libusb_open(libusb_device *dev, libusb_device_handle **devh) { libusb_context *ctx = dev->ctx; struct libusb20_device *pdev = dev->os_priv; int err; if (devh == NULL) return (LIBUSB_ERROR_INVALID_PARAM); /* set default device handle value */ *devh = NULL; dev = libusb_ref_device(dev); if (dev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS); if (err) { libusb_unref_device(dev); return (LIBUSB_ERROR_NO_MEM); } /* * Clear the device gone flag, in case the device was opened * after a re-attach, to allow new transaction: */ CTX_LOCK(ctx); dev->device_is_gone = 0; CTX_UNLOCK(ctx); libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); /* make sure our event loop detects the new device */ libusb_interrupt_event_handler(ctx); *devh = pdev; return (0); } libusb_device_handle * libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id, uint16_t product_id) { struct libusb_device **devs; struct libusb20_device *pdev; struct LIBUSB20_DEVICE_DESC_DECODED *pdesc; int i; int j; ctx = GET_CONTEXT(ctx); if (ctx == NULL) return (NULL); /* be NULL safe */ DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid enter"); if ((i = libusb_get_device_list(ctx, &devs)) < 0) return (NULL); pdev = NULL; for (j = 0; j < i; j++) { struct libusb20_device *tdev; tdev = devs[j]->os_priv; pdesc = libusb20_dev_get_device_desc(tdev); /* * NOTE: The USB library will automatically swap the * fields in the device descriptor to be of host * endian type! */ if (pdesc->idVendor == vendor_id && pdesc->idProduct == product_id) { libusb_open(devs[j], &pdev); break; } } libusb_free_device_list(devs, 1); DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid leave"); return (pdev); } void libusb_close(struct libusb20_device *pdev) { libusb_context *ctx; struct libusb_device *dev; if (pdev == NULL) return; /* be NULL safe */ dev = libusb_get_device(pdev); ctx = dev->ctx; libusb10_remove_pollfd(ctx, &dev->dev_poll); libusb20_dev_close(pdev); /* unref will free the "pdev" when the refcount reaches zero */ libusb_unref_device(dev); /* make sure our event loop detects the closed device */ libusb_interrupt_event_handler(ctx); } libusb_device * libusb_get_device(struct libusb20_device *pdev) { if (pdev == NULL) return (NULL); return ((libusb_device *)pdev->privLuData); } int libusb_get_configuration(struct libusb20_device *pdev, int *config) { struct libusb20_config *pconf; if (pdev == NULL || config == NULL) return (LIBUSB_ERROR_INVALID_PARAM); pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev)); if (pconf == NULL) return (LIBUSB_ERROR_NO_MEM); *config = pconf->desc.bConfigurationValue; free(pconf); return (0); } int libusb_set_configuration(struct libusb20_device *pdev, int configuration) { struct libusb20_config *pconf; struct libusb_device *dev; int err; uint8_t i; dev = libusb_get_device(pdev); if (dev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); if (configuration < 1) { /* unconfigure */ i = 255; } else { for (i = 0; i != 255; i++) { uint8_t found; pconf = libusb20_dev_alloc_config(pdev, i); if (pconf == NULL) return (LIBUSB_ERROR_INVALID_PARAM); found = (pconf->desc.bConfigurationValue == configuration); free(pconf); if (found) goto set_config; } return (LIBUSB_ERROR_INVALID_PARAM); } set_config: libusb10_cancel_all_transfer(dev); libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); err = libusb20_dev_set_config_index(pdev, i); libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); return (err ? LIBUSB_ERROR_INVALID_PARAM : 0); } int libusb_claim_interface(struct libusb20_device *pdev, int interface_number) { libusb_device *dev; int err = 0; dev = libusb_get_device(pdev); if (dev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); if (interface_number < 0 || interface_number > 31) return (LIBUSB_ERROR_INVALID_PARAM); if (pdev->auto_detach != 0) { err = libusb_detach_kernel_driver(pdev, interface_number); if (err != 0) goto done; } CTX_LOCK(dev->ctx); dev->claimed_interfaces |= (1 << interface_number); CTX_UNLOCK(dev->ctx); done: return (err); } int libusb_release_interface(struct libusb20_device *pdev, int interface_number) { libusb_device *dev; int err = 0; dev = libusb_get_device(pdev); if (dev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); if (interface_number < 0 || interface_number > 31) return (LIBUSB_ERROR_INVALID_PARAM); if (pdev->auto_detach != 0) { err = libusb_attach_kernel_driver(pdev, interface_number); if (err != 0) goto done; } CTX_LOCK(dev->ctx); if (!(dev->claimed_interfaces & (1 << interface_number))) err = LIBUSB_ERROR_NOT_FOUND; else dev->claimed_interfaces &= ~(1 << interface_number); CTX_UNLOCK(dev->ctx); done: return (err); } int libusb_set_interface_alt_setting(struct libusb20_device *pdev, int interface_number, int alternate_setting) { libusb_device *dev; int err = 0; dev = libusb_get_device(pdev); if (dev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); if (interface_number < 0 || interface_number > 31) return (LIBUSB_ERROR_INVALID_PARAM); CTX_LOCK(dev->ctx); if (!(dev->claimed_interfaces & (1 << interface_number))) err = LIBUSB_ERROR_NOT_FOUND; CTX_UNLOCK(dev->ctx); if (err) return (err); libusb10_cancel_all_transfer(dev); libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); err = libusb20_dev_set_alt_index(pdev, interface_number, alternate_setting); libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); return (err ? LIBUSB_ERROR_OTHER : 0); } static struct libusb20_transfer * libusb10_get_transfer(struct libusb20_device *pdev, uint8_t endpoint, uint8_t xfer_index) { xfer_index &= 1; /* double buffering */ xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4; if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) { /* this is an IN endpoint */ xfer_index |= 2; } return (libusb20_tr_get_pointer(pdev, xfer_index)); } int libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint) { struct libusb20_transfer *xfer; struct libusb_device *dev; int err; xfer = libusb10_get_transfer(pdev, endpoint, 0); if (xfer == NULL) return (LIBUSB_ERROR_INVALID_PARAM); dev = libusb_get_device(pdev); if (dev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); CTX_LOCK(dev->ctx); err = libusb20_tr_open(xfer, 0, 1, endpoint); CTX_UNLOCK(dev->ctx); if (err != 0 && err != LIBUSB20_ERROR_BUSY) return (LIBUSB_ERROR_OTHER); libusb20_tr_clear_stall_sync(xfer); /* check if we opened the transfer */ if (err == 0) { CTX_LOCK(dev->ctx); libusb20_tr_close(xfer); CTX_UNLOCK(dev->ctx); } return (0); /* success */ } int libusb_reset_device(struct libusb20_device *pdev) { libusb_device *dev; int err; dev = libusb_get_device(pdev); if (dev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); libusb10_cancel_all_transfer(dev); libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); err = libusb20_dev_reset(pdev); libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); return (err ? LIBUSB_ERROR_OTHER : 0); } int libusb_check_connected(struct libusb20_device *pdev) { libusb_device *dev; int err; dev = libusb_get_device(pdev); if (dev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); err = libusb20_dev_check_connected(pdev); return (err ? LIBUSB_ERROR_NO_DEVICE : 0); } int libusb_kernel_driver_active(struct libusb20_device *pdev, int interface) { if (pdev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); if (libusb20_dev_kernel_driver_active(pdev, interface)) return (0); /* no kernel driver is active */ else return (1); /* kernel driver is active */ } int libusb_get_driver_np(struct libusb20_device *pdev, int interface, char *name, int namelen) { return (libusb_get_driver(pdev, interface, name, namelen)); } int libusb_get_driver(struct libusb20_device *pdev, int interface, char *name, int namelen) { char *ptr; int err; if (pdev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); if (namelen < 1) return (LIBUSB_ERROR_INVALID_PARAM); if (namelen > 255) namelen = 255; err = libusb20_dev_get_iface_desc( pdev, interface, name, namelen); if (err != 0) return (LIBUSB_ERROR_OTHER); /* we only want the driver name */ ptr = strstr(name, ":"); if (ptr != NULL) *ptr = 0; return (0); } int libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface) { return (libusb_detach_kernel_driver(pdev, interface)); } int libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface) { int err; if (pdev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); err = libusb20_dev_detach_kernel_driver( pdev, interface); return (err ? LIBUSB_ERROR_OTHER : 0); } int libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface) { if (pdev == NULL) return (LIBUSB_ERROR_INVALID_PARAM); /* stub - currently not supported by libusb20 */ return (0); } int libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable) { dev->auto_detach = (enable ? 1 : 0); return (0); } /* Asynchronous device I/O */ struct libusb_transfer * libusb_alloc_transfer(int iso_packets) { struct libusb_transfer *uxfer; struct libusb_super_transfer *sxfer; int len; len = sizeof(struct libusb_transfer) + sizeof(struct libusb_super_transfer) + (iso_packets * sizeof(libusb_iso_packet_descriptor)); sxfer = malloc(len); if (sxfer == NULL) return (NULL); memset(sxfer, 0, len); uxfer = (struct libusb_transfer *)( ((uint8_t *)sxfer) + sizeof(*sxfer)); /* set default value */ uxfer->num_iso_packets = iso_packets; return (uxfer); } void libusb_free_transfer(struct libusb_transfer *uxfer) { struct libusb_super_transfer *sxfer; if (uxfer == NULL) return; /* be NULL safe */ /* check if we should free the transfer buffer */ if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER) free(uxfer->buffer); sxfer = (struct libusb_super_transfer *)( (uint8_t *)uxfer - sizeof(*sxfer)); free(sxfer); } static uint32_t libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer) { uint32_t ret; switch (xfer->type) { case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */ break; case LIBUSB_TRANSFER_TYPE_CONTROL: ret = 2; break; default: ret = 1; break; } return (ret); } static int libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer) { int ret; int usb_speed; usb_speed = libusb20_dev_get_speed(pdev); switch (xfer->type) { case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: ret = 0; /* kernel will auto-select */ break; case LIBUSB_TRANSFER_TYPE_CONTROL: ret = 1024; break; default: switch (usb_speed) { case LIBUSB20_SPEED_LOW: ret = 256; break; case LIBUSB20_SPEED_FULL: ret = 4096; break; case LIBUSB20_SPEED_SUPER: ret = 65536; break; default: ret = 16384; break; } break; } return (ret); } static int libusb10_convert_error(uint8_t status) { ; /* indent fix */ switch (status) { case LIBUSB20_TRANSFER_START: case LIBUSB20_TRANSFER_COMPLETED: return (LIBUSB_TRANSFER_COMPLETED); case LIBUSB20_TRANSFER_OVERFLOW: return (LIBUSB_TRANSFER_OVERFLOW); case LIBUSB20_TRANSFER_NO_DEVICE: return (LIBUSB_TRANSFER_NO_DEVICE); case LIBUSB20_TRANSFER_STALL: return (LIBUSB_TRANSFER_STALL); case LIBUSB20_TRANSFER_CANCELLED: return (LIBUSB_TRANSFER_CANCELLED); case LIBUSB20_TRANSFER_TIMED_OUT: return (LIBUSB_TRANSFER_TIMED_OUT); default: return (LIBUSB_TRANSFER_ERROR); } } /* This function must be called locked */ static void libusb10_complete_transfer(struct libusb20_transfer *pxfer, struct libusb_super_transfer *sxfer, int status) { struct libusb_transfer *uxfer; struct libusb_device *dev; uxfer = (struct libusb_transfer *)( ((uint8_t *)sxfer) + sizeof(*sxfer)); if (pxfer != NULL) libusb20_tr_set_priv_sc1(pxfer, NULL); /* set transfer status */ uxfer->status = status; /* update super transfer state */ sxfer->state = LIBUSB_SUPER_XFER_ST_NONE; dev = libusb_get_device(uxfer->dev_handle); TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry); } /* This function must be called locked */ static void libusb10_isoc_proxy(struct libusb20_transfer *pxfer) { struct libusb_super_transfer *sxfer; struct libusb_transfer *uxfer; uint32_t actlen; uint16_t iso_packets; uint16_t i; uint8_t status; status = libusb20_tr_get_status(pxfer); sxfer = libusb20_tr_get_priv_sc1(pxfer); actlen = libusb20_tr_get_actual_length(pxfer); iso_packets = libusb20_tr_get_max_frames(pxfer); if (sxfer == NULL) return; /* cancelled - nothing to do */ uxfer = (struct libusb_transfer *)( ((uint8_t *)sxfer) + sizeof(*sxfer)); if (iso_packets > uxfer->num_iso_packets) iso_packets = uxfer->num_iso_packets; if (iso_packets == 0) return; /* nothing to do */ /* make sure that the number of ISOCHRONOUS packets is valid */ uxfer->num_iso_packets = iso_packets; switch (status) { case LIBUSB20_TRANSFER_COMPLETED: /* update actual length */ uxfer->actual_length = actlen; for (i = 0; i != iso_packets; i++) { uxfer->iso_packet_desc[i].actual_length = libusb20_tr_get_length(pxfer, i); } libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); break; case LIBUSB20_TRANSFER_START: /* setup length(s) */ actlen = 0; for (i = 0; i != iso_packets; i++) { libusb20_tr_setup_isoc(pxfer, &uxfer->buffer[actlen], uxfer->iso_packet_desc[i].length, i); actlen += uxfer->iso_packet_desc[i].length; } /* no remainder */ sxfer->rem_len = 0; libusb20_tr_set_total_frames(pxfer, iso_packets); libusb20_tr_submit(pxfer); /* fork another USB transfer, if any */ libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); break; default: libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); break; } } /* This function must be called locked */ static void libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer) { struct libusb_super_transfer *sxfer; struct libusb_transfer *uxfer; uint32_t max_bulk; uint32_t actlen; uint8_t status; uint8_t flags; status = libusb20_tr_get_status(pxfer); sxfer = libusb20_tr_get_priv_sc1(pxfer); max_bulk = libusb20_tr_get_max_total_length(pxfer); actlen = libusb20_tr_get_actual_length(pxfer); if (sxfer == NULL) return; /* cancelled - nothing to do */ uxfer = (struct libusb_transfer *)( ((uint8_t *)sxfer) + sizeof(*sxfer)); flags = uxfer->flags; switch (status) { case LIBUSB20_TRANSFER_COMPLETED: uxfer->actual_length += actlen; /* check for short packet */ if (sxfer->last_len != actlen) { if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); } else { libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); } break; } /* check for end of data */ if (sxfer->rem_len == 0) { libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); break; } /* FALLTHROUGH */ case LIBUSB20_TRANSFER_START: if (max_bulk > sxfer->rem_len) { max_bulk = sxfer->rem_len; } /* setup new BULK or INTERRUPT transaction */ libusb20_tr_setup_bulk(pxfer, sxfer->curr_data, max_bulk, uxfer->timeout); /* update counters */ sxfer->last_len = max_bulk; sxfer->curr_data += max_bulk; sxfer->rem_len -= max_bulk; libusb20_tr_submit(pxfer); /* check if we can fork another USB transfer */ if (sxfer->rem_len == 0) libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); break; default: libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); break; } } /* This function must be called locked */ static void libusb10_ctrl_proxy(struct libusb20_transfer *pxfer) { struct libusb_super_transfer *sxfer; struct libusb_transfer *uxfer; uint32_t max_bulk; uint32_t actlen; uint8_t status; uint8_t flags; status = libusb20_tr_get_status(pxfer); sxfer = libusb20_tr_get_priv_sc1(pxfer); max_bulk = libusb20_tr_get_max_total_length(pxfer); actlen = libusb20_tr_get_actual_length(pxfer); if (sxfer == NULL) return; /* cancelled - nothing to do */ uxfer = (struct libusb_transfer *)( ((uint8_t *)sxfer) + sizeof(*sxfer)); flags = uxfer->flags; switch (status) { case LIBUSB20_TRANSFER_COMPLETED: uxfer->actual_length += actlen; /* subtract length of SETUP packet, if any */ actlen -= libusb20_tr_get_length(pxfer, 0); /* check for short packet */ if (sxfer->last_len != actlen) { if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); } else { libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); } break; } /* check for end of data */ if (sxfer->rem_len == 0) { libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); break; } /* FALLTHROUGH */ case LIBUSB20_TRANSFER_START: if (max_bulk > sxfer->rem_len) { max_bulk = sxfer->rem_len; } /* setup new CONTROL transaction */ if (status == LIBUSB20_TRANSFER_COMPLETED) { /* next fragment - don't send SETUP packet */ libusb20_tr_set_length(pxfer, 0, 0); } else { /* first fragment - send SETUP packet */ libusb20_tr_set_length(pxfer, 8, 0); libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0); } if (max_bulk != 0) { libusb20_tr_set_length(pxfer, max_bulk, 1); libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1); libusb20_tr_set_total_frames(pxfer, 2); } else { libusb20_tr_set_total_frames(pxfer, 1); } /* update counters */ sxfer->last_len = max_bulk; sxfer->curr_data += max_bulk; sxfer->rem_len -= max_bulk; libusb20_tr_submit(pxfer); /* check if we can fork another USB transfer */ if (sxfer->rem_len == 0) libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); break; default: libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); break; } } /* The following function must be called locked */ static void libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint) { struct libusb20_transfer *pxfer0; struct libusb20_transfer *pxfer1; struct libusb_super_transfer *sxfer; struct libusb_transfer *uxfer; struct libusb_device *dev; int err; int buffsize; int maxframe; int temp; dev = libusb_get_device(pdev); pxfer0 = libusb10_get_transfer(pdev, endpoint, 0); pxfer1 = libusb10_get_transfer(pdev, endpoint, 1); if (pxfer0 == NULL || pxfer1 == NULL) return; /* shouldn't happen */ temp = 0; if (libusb20_tr_pending(pxfer0)) temp |= 1; if (libusb20_tr_pending(pxfer1)) temp |= 2; switch (temp) { case 3: /* wait till one of the transfers complete */ return; case 2: sxfer = libusb20_tr_get_priv_sc1(pxfer1); if (sxfer == NULL) return; /* cancelling */ if (sxfer->rem_len) return; /* cannot queue another one */ /* swap transfers */ pxfer1 = pxfer0; break; case 1: sxfer = libusb20_tr_get_priv_sc1(pxfer0); if (sxfer == NULL) return; /* cancelling */ if (sxfer->rem_len) return; /* cannot queue another one */ /* swap transfers */ pxfer0 = pxfer1; break; default: break; } /* find next transfer on same endpoint */ TAILQ_FOREACH(sxfer, &dev->tr_head, entry) { uxfer = (struct libusb_transfer *)( ((uint8_t *)sxfer) + sizeof(*sxfer)); if (uxfer->endpoint == endpoint) { TAILQ_REMOVE(&dev->tr_head, sxfer, entry); sxfer->entry.tqe_prev = NULL; goto found; } } return; /* success */ found: libusb20_tr_set_priv_sc0(pxfer0, pdev); libusb20_tr_set_priv_sc1(pxfer0, sxfer); /* reset super transfer state */ sxfer->rem_len = uxfer->length; sxfer->curr_data = uxfer->buffer; uxfer->actual_length = 0; switch (uxfer->type) { case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy); break; case LIBUSB_TRANSFER_TYPE_BULK: case LIBUSB_TRANSFER_TYPE_INTERRUPT: libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy); break; case LIBUSB_TRANSFER_TYPE_CONTROL: libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy); if (sxfer->rem_len < 8) goto failure; /* remove SETUP packet from data */ sxfer->rem_len -= 8; sxfer->curr_data += 8; break; default: goto failure; } buffsize = libusb10_get_buffsize(pdev, uxfer); maxframe = libusb10_get_maxframe(pdev, uxfer); /* make sure the transfer is opened */ err = libusb20_tr_open_stream(pxfer0, buffsize, maxframe, endpoint, sxfer->stream_id); if (err && (err != LIBUSB20_ERROR_BUSY)) { goto failure; } libusb20_tr_start(pxfer0); return; failure: libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR); /* make sure our event loop spins the done handler */ libusb_interrupt_event_handler(dev->ctx); } /* The following function must be called unlocked */ int libusb_submit_transfer(struct libusb_transfer *uxfer) { struct libusb20_transfer *pxfer0; struct libusb20_transfer *pxfer1; struct libusb_super_transfer *sxfer; struct libusb_device *dev; uint8_t endpoint; int err; if (uxfer == NULL) return (LIBUSB_ERROR_INVALID_PARAM); if (uxfer->dev_handle == NULL) return (LIBUSB_ERROR_INVALID_PARAM); endpoint = uxfer->endpoint; dev = libusb_get_device(uxfer->dev_handle); DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter"); sxfer = (struct libusb_super_transfer *)( (uint8_t *)uxfer - sizeof(*sxfer)); CTX_LOCK(dev->ctx); pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0); pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1); if (pxfer0 == NULL || pxfer1 == NULL) { err = LIBUSB_ERROR_OTHER; } else if ((sxfer->entry.tqe_prev != NULL) || (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) || (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) { err = LIBUSB_ERROR_BUSY; } else if (dev->device_is_gone != 0) { err = LIBUSB_ERROR_NO_DEVICE; } else { /* set pending state */ sxfer->state = LIBUSB_SUPER_XFER_ST_PEND; /* insert transfer into transfer head list */ TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry); /* start work transfers */ libusb10_submit_transfer_sub( uxfer->dev_handle, endpoint); err = 0; /* success */ } CTX_UNLOCK(dev->ctx); DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err); return (err); } /* Asynchronous transfer cancel */ int libusb_cancel_transfer(struct libusb_transfer *uxfer) { struct libusb20_transfer *pxfer0; struct libusb20_transfer *pxfer1; struct libusb_super_transfer *sxfer; struct libusb_device *dev; struct libusb_device_handle *devh; uint8_t endpoint; int retval; if (uxfer == NULL) return (LIBUSB_ERROR_INVALID_PARAM); /* check if not initialised */ if ((devh = uxfer->dev_handle) == NULL) return (LIBUSB_ERROR_NOT_FOUND); endpoint = uxfer->endpoint; dev = libusb_get_device(devh); DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter"); sxfer = (struct libusb_super_transfer *)( (uint8_t *)uxfer - sizeof(*sxfer)); retval = 0; CTX_LOCK(dev->ctx); pxfer0 = libusb10_get_transfer(devh, endpoint, 0); pxfer1 = libusb10_get_transfer(devh, endpoint, 1); if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) { /* only update the transfer status */ uxfer->status = LIBUSB_TRANSFER_CANCELLED; retval = LIBUSB_ERROR_NOT_FOUND; } else if (sxfer->entry.tqe_prev != NULL) { /* we are lucky - transfer is on a queue */ TAILQ_REMOVE(&dev->tr_head, sxfer, entry); sxfer->entry.tqe_prev = NULL; libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_CANCELLED); /* make sure our event loop spins the done handler */ libusb_interrupt_event_handler(dev->ctx); } else if (pxfer0 == NULL || pxfer1 == NULL) { /* not started */ retval = LIBUSB_ERROR_NOT_FOUND; } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) { libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_CANCELLED); if (dev->device_is_gone != 0) { /* clear transfer pointer */ libusb20_tr_set_priv_sc1(pxfer0, NULL); /* make sure our event loop spins the done handler */ libusb_interrupt_event_handler(dev->ctx); } else { libusb20_tr_stop(pxfer0); /* make sure the queue doesn't stall */ libusb10_submit_transfer_sub(devh, endpoint); } } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) { libusb10_complete_transfer(pxfer1, sxfer, LIBUSB_TRANSFER_CANCELLED); /* check if handle is still active */ if (dev->device_is_gone != 0) { /* clear transfer pointer */ libusb20_tr_set_priv_sc1(pxfer1, NULL); /* make sure our event loop spins the done handler */ libusb_interrupt_event_handler(dev->ctx); } else { libusb20_tr_stop(pxfer1); /* make sure the queue doesn't stall */ libusb10_submit_transfer_sub(devh, endpoint); } } else { /* not started */ retval = LIBUSB_ERROR_NOT_FOUND; } CTX_UNLOCK(dev->ctx); DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave"); return (retval); } UNEXPORTED void libusb10_cancel_all_transfer(libusb_device *dev) { struct libusb20_device *pdev = dev->os_priv; unsigned x; for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) { struct libusb20_transfer *xfer; xfer = libusb20_tr_get_pointer(pdev, x); if (xfer == NULL) continue; libusb20_tr_close(xfer); } } UNEXPORTED void libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev) { struct libusb_super_transfer *sxfer; unsigned x; for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) { struct libusb20_transfer *xfer; xfer = libusb20_tr_get_pointer(pdev, x); if (xfer == NULL) continue; if (libusb20_tr_pending(xfer) == 0) continue; sxfer = libusb20_tr_get_priv_sc1(xfer); if (sxfer == NULL) continue; /* complete pending transfer */ libusb10_complete_transfer(xfer, sxfer, LIBUSB_TRANSFER_ERROR); } while ((sxfer = TAILQ_FIRST(&dev->tr_head))) { TAILQ_REMOVE(&dev->tr_head, sxfer, entry); /* complete pending transfer */ libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_ERROR); } } uint16_t libusb_cpu_to_le16(uint16_t x) { return (htole16(x)); } uint16_t libusb_le16_to_cpu(uint16_t x) { return (le16toh(x)); } const char * libusb_strerror(int code) { switch (code) { case LIBUSB_SUCCESS: return ("Success"); case LIBUSB_ERROR_IO: return ("I/O error"); case LIBUSB_ERROR_INVALID_PARAM: return ("Invalid parameter"); case LIBUSB_ERROR_ACCESS: return ("Permissions error"); case LIBUSB_ERROR_NO_DEVICE: return ("No device"); case LIBUSB_ERROR_NOT_FOUND: return ("Not found"); case LIBUSB_ERROR_BUSY: return ("Device busy"); case LIBUSB_ERROR_TIMEOUT: return ("Timeout"); case LIBUSB_ERROR_OVERFLOW: return ("Overflow"); case LIBUSB_ERROR_PIPE: return ("Pipe error"); case LIBUSB_ERROR_INTERRUPTED: return ("Interrupted"); case LIBUSB_ERROR_NO_MEM: return ("Out of memory"); case LIBUSB_ERROR_NOT_SUPPORTED: return ("Not supported"); case LIBUSB_ERROR_OTHER: return ("Other error"); default: return ("Unknown error"); } } const char * libusb_error_name(int code) { switch (code) { case LIBUSB_SUCCESS: return ("LIBUSB_SUCCESS"); case LIBUSB_ERROR_IO: return ("LIBUSB_ERROR_IO"); case LIBUSB_ERROR_INVALID_PARAM: return ("LIBUSB_ERROR_INVALID_PARAM"); case LIBUSB_ERROR_ACCESS: return ("LIBUSB_ERROR_ACCESS"); case LIBUSB_ERROR_NO_DEVICE: return ("LIBUSB_ERROR_NO_DEVICE"); case LIBUSB_ERROR_NOT_FOUND: return ("LIBUSB_ERROR_NOT_FOUND"); case LIBUSB_ERROR_BUSY: return ("LIBUSB_ERROR_BUSY"); case LIBUSB_ERROR_TIMEOUT: return ("LIBUSB_ERROR_TIMEOUT"); case LIBUSB_ERROR_OVERFLOW: return ("LIBUSB_ERROR_OVERFLOW"); case LIBUSB_ERROR_PIPE: return ("LIBUSB_ERROR_PIPE"); case LIBUSB_ERROR_INTERRUPTED: return ("LIBUSB_ERROR_INTERRUPTED"); case LIBUSB_ERROR_NO_MEM: return ("LIBUSB_ERROR_NO_MEM"); case LIBUSB_ERROR_NOT_SUPPORTED: return ("LIBUSB_ERROR_NOT_SUPPORTED"); case LIBUSB_ERROR_OTHER: return ("LIBUSB_ERROR_OTHER"); default: return ("LIBUSB_ERROR_UNKNOWN"); } } int libusb_has_capability(uint32_t capability) { switch (capability) { case LIBUSB_CAP_HAS_CAPABILITY: case LIBUSB_CAP_HAS_HOTPLUG: case LIBUSB_CAP_HAS_HID_ACCESS: case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER: return (1); default: return (0); } }