1.\" 2.\" Copyright (c) 2009 Sylvestre Gallon 3.\" 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" SUCH DAMAGE. 26.\" 27.\" $FreeBSD$ 28.\" 29.Dd August 16, 2011 30.Dt LIBUSB 3 31.Os 32.Sh NAME 33.Nm libusb 34.Nd "USB access library" 35.Sh LIBRARY 36USB access library 37.Pq libusb, -lusb 38.Sh SYNOPSIS 39.In libusb.h 40.Sh DESCRIPTION 41The 42.Nm 43library contains interfaces for directly managing a usb device. 44The current implementation supports v1.0 of the libusb API. 45.Sh LIBRARY INITIALISATION / DEINITIALISATION 46.Pp 47.Ft int 48.Fn libusb_init libusb_context **ctx 49This function initialises libusb. 50It must be called at the beginning 51of the program, before other libusb routines are used. 52This function returns 0 on success or LIBUSB_ERROR on 53failure. 54.Pp 55.Ft void 56.Fn libusb_exit "libusb_context *ctx" 57Deinitialise libusb. 58Must be called at the end of the application. 59Other libusb routines may not be called after this function. 60.Pp 61.Ft const char * 62.Fn libusb_strerror "int code" 63Get the ASCII representation of the error given by the 64.Fa code 65argument. 66.Pp 67.Ft void 68.Fn libusb_set_debug "libusb_context *ctx" "int level" 69Set the debug level to 70.Fa level . 71.Pp 72.Ft ssize_t 73.Fn libusb_get_device_list "libusb_context *ctx" "libusb_device ***list" 74Populate 75.Fa list 76with the list of usb devices available, adding a reference to each 77device in the list. 78All the list entries created by this 79function must have their reference counter 80decremented when you are done with them, 81and the list itself must be freed. 82This 83function returns the number of devices in the list or a LIBUSB_ERROR code. 84.Pp 85.Ft void 86.Fn libusb_free_device_list "libusb_device **list" "int unref_devices" 87Free the list of devices discovered by libusb_get_device_list. 88If 89.Fa unref_device 90is set to 1 all devices in the list have their reference 91counter decremented once. 92.Pp 93.Ft uint8_t 94.Fn libusb_get_bus_number "libusb_device *dev" 95Returns the number of the bus contained by the device 96.Fa dev. 97.Pp 98.Ft uint8_t 99.Fn libusb_get_device_address "libusb_device *dev" 100Returns the device_address contained by the device 101.Fa dev. 102.Pp 103.Ft enum libusb_speed 104.Fn libusb_get_device_speed "libusb_device *dev" 105Returns the wire speed at which the device is connected. 106See the LIBUSB_SPEED_XXX enums for more information. 107LIBUSB_SPEED_UNKNOWN is returned in case of unknown wire speed. 108.Pp 109.Ft int 110.Fn libusb_get_max_packet_size "libusb_device *dev" "unsigned char endpoint" 111Returns the wMaxPacketSize value on success, LIBUSB_ERROR_NOT_FOUND if the 112endpoint does not exist and LIBUSB_ERROR_OTHERS on other failure. 113.Pp 114.Ft libusb_device * 115.Fn libusb_ref_device "libusb_device *dev" 116Increment the reference counter of the device 117.Fa dev. 118.Pp 119.Ft void 120.Fn libusb_unref_device "libusb_device *dev" 121Decrement the reference counter of the device 122.Fa dev. 123.Pp 124.Ft int 125.Fn libusb_open "libusb_device *dev" "libusb_device_handle **devh" 126Open a device and obtain a device_handle. 127Returns 0 on success, 128LIBUSB_ERROR_NO_MEM on memory allocation problems, LIBUSB_ERROR_ACCESS 129on permissions problems, LIBUSB_ERROR_NO_DEVICE if the device has been 130disconnected and a LIBUSB_ERROR code on other errors. 131.Pp 132.Ft libusb_device_handle * 133.Fn libusb_open_device_with_vid_pid "libusb_context *ctx" "uint16_t vid" "uint16_t pid" 134A convenience function to open a device by vendor and product IDs 135.Fa vid 136and 137.Fa pid. 138Returns NULL on error. 139.Pp 140.Ft void 141.Fn libusb_close "libusb_device_handle *devh" 142Close a device handle. 143.Pp 144.Ft libusb_device * 145.Fn libusb_get_device "libusb_device_handle *devh" 146Get the device contained by devh. 147Returns NULL on error. 148.Pp 149.Ft int 150.Fn libusb_get_configuration "libusb_device_handle *devh" "int *config" 151Returns the bConfiguration value of the current configuration. 152Returns 0 153on success, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected 154and a LIBUSB_ERROR code on error. 155.Pp 156.Ft int 157.Fn libusb_set_configuration "libusb_device_handle *devh" "int config" 158Set the active configuration to 159.Fa config 160for the device contained by 161.Fa devh. 162This function returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the requested 163configuration does not exist, LIBUSB_ERROR_BUSY if the interfaces are currently 164claimed, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a 165LIBUSB_ERROR code on failure. 166.Pp 167.Ft int 168.Fn libusb_claim_interface "libusb_device_handle *devh" "int interface_number" 169Claim an interface in a given libusb_handle 170.Fa devh. 171This is a non-blocking function. 172It returns 0 on success, LIBUSB_ERROR_NOT_FOUND 173if the requested interface does not exist, LIBUSB_ERROR_BUSY if a program or 174driver has claimed the interface, LIBUSB_ERROR_NO_DEVICE if the device has 175been disconnected and a LIBUSB_ERROR code on failure. 176.Pp 177.Ft int 178.Fn libusb_release_interface "libusb_device_handle *devh" "int interface_number" 179This function releases an interface. 180All the claimed interfaces on a device must be released 181before closing the device. 182Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the 183interface was not claimed, LIBUSB_ERROR_NO_DEVICE if the device has been 184disconnected and LIBUSB_ERROR on failure. 185.Pp 186.Ft int 187.Fn libusb_set_interface_alt_setting "libusb_device_handle *dev" "int interface_number" "int alternate_setting" 188Activate an alternate setting for an interface. 189Returns 0 on success, 190LIBUSB_ERROR_NOT_FOUND if the interface was not claimed or the requested 191setting does not exist, LIBUSB_ERROR_NO_DEVICE if the device has been 192disconnected and a LIBUSB_ERROR code on failure. 193.Pp 194.Ft int 195.Fn libusb_clear_halt "libusb_device_handle *devh" "unsigned char endpoint" 196Clear an halt/stall for a endpoint. 197Returns 0 on success, LIBUSB_ERROR_NOT_FOUND 198if the endpoint does not exist, LIBUSB_ERROR_NO_DEVICE if the device has been 199disconnected and a LIBUSB_ERROR code on failure. 200.Pp 201.Ft int 202.Fn libusb_reset_device "libusb_device_handle *devh" 203Perform an USB port reset for an usb device. 204Returns 0 on success, 205LIBUSB_ERROR_NOT_FOUND if re-enumeration is required or if the device has 206been disconnected and a LIBUSB_ERROR code on failure. 207.Pp 208.Ft int 209.Fn libusb_check_connected "libusb_device_handle *devh" 210Test if the USB device is still connected. 211Returns 0 on success, 212LIBUSB_ERROR_NO_DEVICE if it has been disconnected and a LIBUSB_ERROR 213code on failure. 214.Pp 215.Ft int 216.Fn libusb_kernel_driver_active "libusb_device_handle *devh" "int interface" 217Determine if a driver is active on a interface. 218Returns 0 if no kernel driver 219is active, 1 if a kernel driver is active, LIBUSB_ERROR_NO_DEVICE 220if the device has been disconnected and a LIBUSB_ERROR code on failure. 221.Pp 222.Ft int 223.Fn libusb_get_driver "libusb_device_handle *devh" "int interface" "char *name" "int namelen" 224or 225.Ft int 226.Fn libusb_get_driver_np "libusb_device_handle *devh" "int interface" "char *name" "int namelen" 227Copy the name of the driver attached to the given 228.Fa device 229and 230.Fa interface 231into the buffer 232.Fa name 233of length 234.Fa namelen . 235Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if no kernel driver is attached 236to the given interface and LIBUSB_ERROR_INVALID_PARAM if the interface does 237not exist. 238This function is non-portable. 239The buffer pointed to by 240.Fa name 241is only zero terminated on success. 242.Pp 243.Ft int 244.Fn libusb_detach_kernel_driver "libusb_device_handle *devh" "int interface" 245or 246.Ft int 247.Fn libusb_detach_kernel_driver_np "libusb_device_handle *devh" "int interface" 248Detach a kernel driver from an interface. 249This is needed to claim an interface already claimed by a kernel driver. 250Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if no kernel driver was active, 251LIBUSB_ERROR_INVALID_PARAM if the interface does not exist, 252LIBUSB_ERROR_NO_DEVICE if the device has been disconnected 253and a LIBUSB_ERROR code on failure. 254This function is non-portable. 255.Pp 256.Ft int 257.Fn libusb_attach_kernel_driver "libusb_device_handle *devh" "int interface" 258Re-attach an interface kernel driver that was previously detached. 259Returns 0 on success, 260LIBUSB_ERROR_INVALID_PARAM if the interface does not exist, 261LIBUSB_ERROR_NO_DEVICE 262if the device has been disconnected, LIBUSB_ERROR_BUSY if the driver cannot be 263attached because the interface is claimed by a program or driver and a 264LIBUSB_ERROR code on failure. 265.Pp 266.Sh USB DESCRIPTORS 267.Pp 268.Ft int 269.Fn libusb_get_device_descriptor "libusb_device *dev" "libusb_device_descriptor *desc" 270Get the USB device descriptor for the device 271.Fa dev. 272This is a non-blocking function. 273Returns 0 on success and a LIBUSB_ERROR code on 274failure. 275.Pp 276.Ft int 277.Fn libsub_get_active_config_descriptor "libusb_device *dev" "struct libusb_config_descriptor **config" 278Get the USB configuration descriptor for the active configuration. 279Returns 0 on 280success, LIBUSB_ERROR_NOT_FOUND if the device is in 281an unconfigured state 282and a LIBUSB_ERROR code on error. 283.Pp 284.Ft int 285.Fn libusb_get_config_descriptor "libusb_device *dev" "uint8_t config_index" "libusb_config_descriptor **config" 286Get a USB configuration descriptor based on its index 287.Fa idx. 288Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the configuration does not exist 289and a LIBUSB_ERROR code on error. 290.Pp 291.Ft int 292.Fn libusb_get_config_descriptor_by_value "libusb_device *dev" "uint8 bConfigurationValue" "libusb_config_descriptor **config" 293Get a USB configuration descriptor with a specific bConfigurationValue. 294This is 295a non-blocking function which does not send a request through the device. 296Returns 0 297on success, LIBUSB_ERROR_NOT_FOUND if the configuration 298does not exist and a 299LIBUSB_ERROR code on failure. 300.Pp 301.Ft void 302.Fn libusb_free_config_descriptor "libusb_config_descriptor *config" 303Free a configuration descriptor. 304.Pp 305.Ft int 306.Fn libusb_get_string_descriptor_ascii "libusb_device_handle *devh" "uint8_t desc_idx" "unsigned char *data" "int length" 307Retrieve a string descriptor in C style ASCII. 308Returns the positive number of bytes in the resulting ASCII string 309on success and a LIBUSB_ERROR code on failure. 310.Pp 311.Sh USB ASYNCHRONOUS I/O 312.Pp 313.Ft struct libusb_transfer * 314.Fn libusb_alloc_transfer "int iso_packets" 315Allocate a transfer with the number of isochronous packet descriptors 316specified by 317.Fa iso_packets . 318Returns NULL on error. 319.Pp 320.Ft void 321.Fn libusb_free_transfer "struct libusb_transfer *tr" 322Free a transfer. 323.Pp 324.Ft int 325.Fn libusb_submit_transfer "struct libusb_transfer *tr" 326This function will submit a transfer and returns immediately. 327Returns 0 on success, LIBUSB_ERROR_NO_DEVICE if 328the device has been disconnected and a 329LIBUSB_ERROR code on other failure. 330.Pp 331.Ft int 332.Fn libusb_cancel_transfer "struct libusb_transfer *tr" 333This function asynchronously cancels a transfer. 334Returns 0 on success and a LIBUSB_ERROR code on failure. 335.Pp 336.Sh USB SYNCHRONOUS I/O 337.Pp 338.Ft int 339.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" 340Perform a USB control transfer. 341Returns the actual number of bytes 342transferred on success, in the range from and including zero up to and 343including 344.Fa wLength . 345On error a LIBUSB_ERROR code is returned, for example 346LIBUSB_ERROR_TIMEOUT if the transfer timed out, LIBUSB_ERROR_PIPE if the 347control request was not supported, LIBUSB_ERROR_NO_DEVICE if the 348device has been disconnected and another LIBUSB_ERROR code on other failures. 349The LIBUSB_ERROR codes are all negative. 350.Pp 351.Ft int 352.Fn libusb_bulk_transfer "struct libusb_device_handle *devh" "unsigned char endpoint" "unsigned char *data" "int length" "int *transferred" "unsigned int timeout" 353Perform an USB bulk transfer. 354A timeout value of zero means no timeout. 355The timeout value is given in milliseconds. 356Returns 0 on success, LIBUSB_ERROR_TIMEOUT 357if the transfer timed out, LIBUSB_ERROR_PIPE if the control request was not 358supported, LIBUSB_ERROR_OVERFLOW if the device offered more data, 359LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and 360a LIBUSB_ERROR code on other failure. 361.Pp 362.Ft int 363.Fn libusb_interrupt_transfer "struct libusb_device_handle *devh" "unsigned char endpoint" "unsigned char *data" "int length" "int *transferred" "unsigned int timeout" 364Perform an USB Interrupt transfer. 365A timeout value of zero means no timeout. 366The timeout value is given in milliseconds. 367Returns 0 on success, LIBUSB_ERROR_TIMEOUT 368if the transfer timed out, LIBUSB_ERROR_PIPE if the control request was not 369supported, LIBUSB_ERROR_OVERFLOW if the device offered more data, 370LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and 371a LIBUSB_ERROR code on other failure. 372.Pp 373.Sh USB EVENTS 374.Pp 375.Ft int 376.Fn libusb_try_lock_events "libusb_context *ctx" 377Try to acquire the event handling lock. 378Returns 0 if the lock was obtained and 1 if not. 379.Pp 380.Ft void 381.Fn libusb_lock_events "libusb_context *ctx" 382Acquire the event handling lock. 383This function is blocking. 384.Pp 385.Ft void 386.Fn libusb_unlock_events "libusb_context *ctx" 387Release the event handling lock. 388This will wake up any thread blocked 389on 390.B libusb_wait_for_event() . 391.Pp 392.Ft int 393.Fn libusb_event_handling_ok "libusb_context *ctx" 394Determine if it still OK for this thread to be doing event handling. 395Returns 1 396if event handling can start or continue. 397Returns 0 if this thread must give up 398the events lock. 399.Pp 400.Ft int 401.Fn libusb_event_handler_active "libusb_context *ctx" 402Determine if an active thread is handling events. 403Returns 1 if there is a thread handling events and 0 if there 404are no threads currently handling events. 405.Pp 406.Ft void 407.Fn libusb_lock_event_waiters "libusb_context *ctx" 408Acquire the event_waiters lock. 409This lock is designed to be obtained in the 410situation where you want to be aware when events are completed, but some other 411thread is event handling so calling libusb_handle_events() is not allowed. 412.Pp 413.Ft void 414.Fn libusb_unlock_event_waiters "libusb_context *ctx" 415Release the event_waiters lock. 416.Pp 417.Ft int 418.Fn libusb_wait_for_event "libusb_context *ctx" "struct timeval *tv" 419Wait for another thread to signal completion of an event. 420Must be called 421with the event waiters lock held, see libusb_lock_event_waiters(). 422This will 423block until the timeout expires or a transfer completes or a thread releases 424the event handling lock through libusb_unlock_events(). 425Returns 0 after a 426transfer completes or another thread stops event handling, and 1 if the 427timeout expired. 428.Pp 429.Ft int 430.Fn libusb_handle_events_timeout "libusb_context *ctx" "struct timeval *tv" 431Handle any pending events by checking if timeouts have expired and by 432checking the set of file descriptors for activity. 433Returns 0 on success, or a 434LIBUSB_ERROR code on failure. 435.Pp 436.Ft int 437.Fn libusb_handle_events "libusb_context *ctx" 438Handle any pending events in blocking mode with a sensible timeout. 439Returns 0 440on success and a LIBUSB_ERROR code on failure. 441.Pp 442.Ft int 443.Fn libusb_handle_events_locked "libusb_context *ctx" "struct timeval *tv" 444Handle any pending events by polling file desciptors, without checking if 445another thread is already doing so. 446Must be called with the event lock held. 447.Pp 448.Ft int 449.Fn libusb_get_next_timeout "libusb_context *ctx" "struct timeval *tv" 450Determine the next internal timeout that libusb needs to handle. 451Returns 0 452if there are no pending timeouts, 1 if a timeout was returned, or a LIBUSB_ERROR 453code on failure. 454.Pp 455.Ft void 456.Fn libusb_set_pollfd_notifiers "libusb_context *ctx" "libusb_pollfd_added_cb added_cb" "libusb_pollfd_removed_cb remove_cb" "void *user_data" 457Register notification functions for file descriptor additions/removals. 458These functions will be invoked for every new or removed file descriptor 459that libusb uses as an event source. 460.Pp 461.Ft const struct libusb_pollfd ** 462.Fn libusb_get_pollfds "libusb_context *ctx" 463Retrive a list of file descriptors that should be polled by your main loop as 464libusb event sources. 465Returns a NULL-terminated list on success or NULL on failure. 466.Sh LIBUSB VERSION 0.1 COMPATIBILITY 467.Pp 468The library is also compliant with LibUSB version 0.1.12. 469.Pp 470.Fn usb_open 471.Fn usb_close 472.Fn usb_get_string 473.Fn usb_get_string_simple 474.Fn usb_get_descriptor_by_endpoint 475.Fn usb_get_descriptor 476.Fn usb_parse_descriptor 477.Fn usb_parse_configuration 478.Fn usb_destroy_configuration 479.Fn usb_fetch_and_parse_descriptors 480.Fn usb_bulk_write 481.Fn usb_bulk_read 482.Fn usb_interrupt_write 483.Fn usb_interrupt_read 484.Fn usb_control_msg 485.Fn usb_set_configuration 486.Fn usb_claim_interface 487.Fn usb_release_interface 488.Fn usb_set_altinterface 489.Fn usb_resetep 490.Fn usb_clear_halt 491.Fn usb_reset 492.Fn usb_strerror 493.Fn usb_init 494.Fn usb_set_debug 495.Fn usb_find_busses 496.Fn usb_find_devices 497.Fn usb_device 498.Fn usb_get_busses 499.Fn usb_check_connected 500.Fn usb_get_driver_np 501.Fn usb_detach_kernel_driver_np 502.Sh SEE ALSO 503.Xr libusb20 3 , 504.Xr usb 4 , 505.Xr usbconfig 8 506.Pp 507.Pa http://libusb.sourceforge.net/ 508.Sh HISTORY 509.Nm 510support first appeared in 511.Fx 8.0 . 512