xref: /freebsd/lib/libusb/libusb.3 (revision 5686c6c38a3e1cc78804eaf5f880bda23dcf592f)
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 May 7, 2013
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.Ft int
47.Fn libusb_init libusb_context **ctx
48This function initialises libusb.
49It must be called at the beginning
50of the program, before other libusb routines are used.
51This function returns 0 on success or LIBUSB_ERROR on
52failure.
53.Pp
54.Ft void
55.Fn libusb_exit "libusb_context *ctx"
56Deinitialise libusb.
57Must be called at the end of the application.
58Other libusb routines may not be called after this function.
59.Pp
60.Ft const char *
61.Fn libusb_strerror "int code"
62Get the ASCII representation of the error given by the
63.Fa code
64argument.
65This function does not return NULL.
66.Pp
67.Ft const char *
68.Fn libusb_error_name "int code"
69Get the ASCII representation of the error enum given by the
70.Fa code
71argument.
72This function does not return NULL.
73.Pp
74.Ft void
75.Fn libusb_set_debug "libusb_context *ctx" "int level"
76Set the debug level to
77.Fa level .
78.Pp
79.Ft ssize_t
80.Fn libusb_get_device_list "libusb_context *ctx" "libusb_device ***list"
81Populate
82.Fa list
83with the list of usb devices available, adding a reference to each
84device in the list.
85All the list entries created by this
86function must have their reference counter
87decremented when you are done with them,
88and the list itself must be freed.
89This
90function returns the number of devices in the list or a LIBUSB_ERROR code.
91.Pp
92.Ft void
93.Fn libusb_free_device_list "libusb_device **list" "int unref_devices"
94Free the list of devices discovered by libusb_get_device_list.
95If
96.Fa unref_device
97is set to 1 all devices in the list have their reference
98counter decremented once.
99.Pp
100.Ft uint8_t
101.Fn libusb_get_bus_number "libusb_device *dev"
102Returns the number of the bus contained by the device
103.Fa dev .
104.Pp
105.Ft int
106.Fn libusb_get_port_path "libusb_context *ctx" "libusb_device *dev" "uint8_t *buf" "uint8_t bufsize"
107Stores, in the buffer
108.Fa buf
109of size
110.Fa bufsize ,
111the list of all port numbers from root for the device
112.Fa dev .
113.Pp
114.Ft uint8_t
115.Fn libusb_get_device_address "libusb_device *dev"
116Returns the device_address contained by the device
117.Fa dev .
118.Pp
119.Ft enum libusb_speed
120.Fn libusb_get_device_speed "libusb_device *dev"
121Returns the wire speed at which the device is connected.
122See the LIBUSB_SPEED_XXX enums for more information.
123LIBUSB_SPEED_UNKNOWN is returned in case of unknown wire speed.
124.Pp
125.Ft int
126.Fn libusb_get_max_packet_size "libusb_device *dev" "unsigned char endpoint"
127Returns the wMaxPacketSize value on success, LIBUSB_ERROR_NOT_FOUND if the
128endpoint does not exist and LIBUSB_ERROR_OTHERS on other failure.
129.Pp
130.Ft int
131.Fn libusb_get_max_iso_packet_size "libusb_device *dev" "unsigned char endpoint"
132Returns the packet size multiplied by the packet multiplier on success,
133LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist and
134LIBUSB_ERROR_OTHERS on other failure.
135.Pp
136.Ft libusb_device *
137.Fn libusb_ref_device "libusb_device *dev"
138Increment the reference counter of the device
139.Fa dev .
140.Pp
141.Ft void
142.Fn libusb_unref_device "libusb_device *dev"
143Decrement the reference counter of the device
144.Fa dev .
145.Pp
146.Ft int
147.Fn libusb_open "libusb_device *dev" "libusb_device_handle **devh"
148Open a device and obtain a device_handle.
149Returns 0 on success,
150LIBUSB_ERROR_NO_MEM on memory allocation problems, LIBUSB_ERROR_ACCESS
151on permissions problems, LIBUSB_ERROR_NO_DEVICE if the device has been
152disconnected and a LIBUSB_ERROR code on other errors.
153.Pp
154.Ft libusb_device_handle *
155.Fn libusb_open_device_with_vid_pid "libusb_context *ctx" "uint16_t vid" "uint16_t pid"
156A convenience function to open a device by vendor and product IDs
157.Fa vid
158and
159.Fa pid .
160Returns NULL on error.
161.Pp
162.Ft void
163.Fn libusb_close "libusb_device_handle *devh"
164Close a device handle.
165.Pp
166.Ft libusb_device *
167.Fn libusb_get_device "libusb_device_handle *devh"
168Get the device contained by devh.
169Returns NULL on error.
170.Pp
171.Ft int
172.Fn libusb_get_configuration "libusb_device_handle *devh" "int *config"
173Returns the value of the current configuration.
174Returns 0
175on success, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
176and a LIBUSB_ERROR code on error.
177.Pp
178.Ft int
179.Fn libusb_set_configuration "libusb_device_handle *devh" "int config"
180Set the active configuration to
181.Fa config
182for the device contained by
183.Fa devh .
184This function returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the requested
185configuration does not exist, LIBUSB_ERROR_BUSY if the interfaces are currently
186claimed, LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a
187LIBUSB_ERROR code on failure.
188.Pp
189.Ft int
190.Fn libusb_claim_interface "libusb_device_handle *devh" "int interface_number"
191Claim an interface in a given libusb_handle
192.Fa devh .
193This is a non-blocking function.
194It returns 0 on success, LIBUSB_ERROR_NOT_FOUND
195if the requested interface does not exist, LIBUSB_ERROR_BUSY if a program or
196driver has claimed the interface, LIBUSB_ERROR_NO_DEVICE if the device has
197been disconnected and a LIBUSB_ERROR code on failure.
198.Pp
199.Ft int
200.Fn libusb_release_interface "libusb_device_handle *devh" "int interface_number"
201This function releases an interface.
202All the claimed interfaces on a device must be released
203before closing the device.
204Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the
205interface was not claimed, LIBUSB_ERROR_NO_DEVICE if the device has been
206disconnected and LIBUSB_ERROR on failure.
207.Pp
208.Ft int
209.Fn libusb_set_interface_alt_setting "libusb_device_handle *dev" "int interface_number" "int alternate_setting"
210Activate an alternate setting for an interface.
211Returns 0 on success,
212LIBUSB_ERROR_NOT_FOUND if the interface was not claimed or the requested
213setting does not exist, LIBUSB_ERROR_NO_DEVICE if the device has been
214disconnected and a LIBUSB_ERROR code on failure.
215.Pp
216.Ft int
217.Fn libusb_clear_halt "libusb_device_handle *devh" "unsigned char endpoint"
218Clear an halt/stall for a endpoint.
219Returns 0 on success, LIBUSB_ERROR_NOT_FOUND
220if the endpoint does not exist, LIBUSB_ERROR_NO_DEVICE if the device has been
221disconnected and a LIBUSB_ERROR code on failure.
222.Pp
223.Ft int
224.Fn libusb_reset_device "libusb_device_handle *devh"
225Perform an USB port reset for an usb device.
226Returns 0 on success,
227LIBUSB_ERROR_NOT_FOUND if re-enumeration is required or if the device has
228been disconnected and a LIBUSB_ERROR code on failure.
229.Pp
230.Ft int
231.Fn libusb_check_connected "libusb_device_handle *devh"
232Test if the USB device is still connected.
233Returns 0 on success,
234LIBUSB_ERROR_NO_DEVICE if it has been disconnected and a LIBUSB_ERROR
235code on failure.
236.Pp
237.Ft int
238.Fn libusb_kernel_driver_active "libusb_device_handle *devh" "int interface"
239Determine if a driver is active on a interface.
240Returns 0 if no kernel driver is active
241and 1 if a kernel driver is active, LIBUSB_ERROR_NO_DEVICE
242if the device has been disconnected and a LIBUSB_ERROR code on failure.
243.Pp
244.Ft int
245.Fn libusb_get_driver "libusb_device_handle *devh" "int interface" "char *name" "int namelen"
246or
247.Ft int
248.Fn libusb_get_driver_np "libusb_device_handle *devh" "int interface" "char *name" "int namelen"
249Copy the name of the driver attached to the given
250.Fa device
251and
252.Fa interface
253into the buffer
254.Fa name
255of length
256.Fa namelen .
257Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if no kernel driver is attached
258to the given interface and LIBUSB_ERROR_INVALID_PARAM if the interface does
259not exist.
260This function is non-portable.
261The buffer pointed to by
262.Fa name
263is only zero terminated on success.
264.Pp
265.Ft int
266.Fn libusb_detach_kernel_driver "libusb_device_handle *devh" "int interface"
267or
268.Ft int
269.Fn libusb_detach_kernel_driver_np "libusb_device_handle *devh" "int interface"
270Detach a kernel driver from an interface.
271This is needed to claim an interface already claimed by a kernel driver.
272Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if no kernel driver was active,
273LIBUSB_ERROR_INVALID_PARAM if the interface does not exist,
274LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
275and a LIBUSB_ERROR code on failure.
276This function is non-portable.
277.Pp
278.Ft int
279.Fn libusb_attach_kernel_driver "libusb_device_handle *devh" "int interface"
280Re-attach an interface kernel driver that was previously detached.
281Returns 0 on success,
282LIBUSB_ERROR_INVALID_PARAM if the interface does not exist,
283LIBUSB_ERROR_NO_DEVICE
284if the device has been disconnected, LIBUSB_ERROR_BUSY if the driver cannot be
285attached because the interface is claimed by a program or driver and a
286LIBUSB_ERROR code on failure.
287.Sh USB DESCRIPTORS
288.Ft int
289.Fn libusb_get_device_descriptor "libusb_device *dev" "libusb_device_descriptor *desc"
290Get the USB device descriptor for the device
291.Fa dev .
292This is a non-blocking function.
293Returns 0 on success and a LIBUSB_ERROR code on
294failure.
295.Pp
296.Ft int
297.Fn libusb_get_active_config_descriptor "libusb_device *dev" "struct libusb_config_descriptor **config"
298Get the USB configuration descriptor for the active configuration.
299Returns 0 on
300success, LIBUSB_ERROR_NOT_FOUND if the device is in
301an unconfigured state
302and a LIBUSB_ERROR code on error.
303.Pp
304.Ft int
305.Fn libusb_get_config_descriptor "libusb_device *dev" "uint8_t config_index" "libusb_config_descriptor **config"
306Get a USB configuration descriptor based on its index
307.Fa idx.
308Returns 0 on success, LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
309and a LIBUSB_ERROR code on error.
310.Pp
311.Ft int
312.Fn libusb_get_config_descriptor_by_value "libusb_device *dev" "uint8 bConfigurationValue" "libusb_config_descriptor **config"
313Get a USB configuration descriptor with a specific bConfigurationValue.
314This is
315a non-blocking function which does not send a request through the device.
316Returns 0
317on success, LIBUSB_ERROR_NOT_FOUND if the configuration
318does not exist and a
319LIBUSB_ERROR code on failure.
320.Pp
321.Ft void
322.Fn libusb_free_config_descriptor "libusb_config_descriptor *config"
323Free a configuration descriptor.
324.Pp
325.Ft int
326.Fn libusb_get_string_descriptor "libusb_device_handle *devh" "uint8_t desc_idx" "uint16_t langid" "unsigned char *data" "int length"
327Retrieve a string descriptor in raw format.
328Returns the number of bytes actually transferred on success
329or a negative LIBUSB_ERROR code on failure.
330.Pp
331.Ft int
332.Fn libusb_get_string_descriptor_ascii "libusb_device_handle *devh" "uint8_t desc_idx" "unsigned char *data" "int length"
333Retrieve a string descriptor in C style ASCII.
334Returns the positive number of bytes in the resulting ASCII string
335on success and a LIBUSB_ERROR code on failure.
336.Pp
337.Ft int
338.Fn libusb_parse_ss_endpoint_comp "const void *buf" "int len" "libusb_ss_endpoint_companion_descriptor **ep_comp"
339This function parses the USB 3.0 endpoint companion descriptor in host endian format pointed to by
340.Fa buf
341and having a length of
342.Fa len .
343Typically these arguments are the extra and extra_length fields of the
344endpoint descriptor.
345On success the pointer to resulting descriptor is stored at the location given by
346.Fa ep_comp .
347Returns zero on success and a LIBUSB_ERROR code on failure.
348On success the parsed USB 3.0 endpoint companion descriptor must be
349freed using the libusb_free_ss_endpoint_comp function.
350.Pp
351.Ft void
352.Fn libusb_free_ss_endpoint_comp "libusb_ss_endpoint_companion_descriptor *ep_comp"
353This function is NULL safe and frees a parsed USB 3.0 endpoint companion descriptor.
354.Pp
355.Ft int
356.Fn libusb_parse_bos_descriptor "const void *buf" "int len" "libusb_bos_descriptor **bos"
357This function parses a Binary Object Store, BOS, descriptor into host endian format pointed to by
358.Fa buf
359and having a length of
360.Fa len .
361On success the pointer to resulting descriptor is stored at the location given by
362.Fa bos .
363Returns zero on success and a LIBUSB_ERROR code on failure.
364On success the parsed BOS descriptor must be freed using the
365libusb_free_bos_descriptor function.
366.Pp
367.Ft void
368.Fn libusb_free_bos_descriptor "libusb_bos_descriptor *bos"
369This function is NULL safe and frees a parsed BOS descriptor.
370.Sh USB ASYNCHRONOUS I/O
371.Ft struct libusb_transfer *
372.Fn libusb_alloc_transfer "int iso_packets"
373Allocate a transfer with the number of isochronous packet descriptors
374specified by
375.Fa iso_packets .
376Returns NULL on error.
377.Pp
378.Ft void
379.Fn libusb_free_transfer "struct libusb_transfer *tr"
380Free a transfer.
381.Pp
382.Ft int
383.Fn libusb_submit_transfer "struct libusb_transfer *tr"
384This function will submit a transfer and returns immediately.
385Returns 0 on success, LIBUSB_ERROR_NO_DEVICE if
386the device has been disconnected and a
387LIBUSB_ERROR code on other failure.
388.Pp
389.Ft int
390.Fn libusb_cancel_transfer "struct libusb_transfer *tr"
391This function asynchronously cancels a transfer.
392Returns 0 on success and a LIBUSB_ERROR code on failure.
393.Sh USB SYNCHRONOUS I/O
394.Ft int
395.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"
396Perform a USB control transfer.
397Returns the actual number of bytes
398transferred on success, in the range from and including zero up to and
399including
400.Fa wLength .
401On error a LIBUSB_ERROR code is returned, for example
402LIBUSB_ERROR_TIMEOUT if the transfer timed out, LIBUSB_ERROR_PIPE if the
403control request was not supported, LIBUSB_ERROR_NO_DEVICE if the
404device has been disconnected and another LIBUSB_ERROR code on other failures.
405The LIBUSB_ERROR codes are all negative.
406.Pp
407.Ft int
408.Fn libusb_bulk_transfer "struct libusb_device_handle *devh" "unsigned char endpoint" "unsigned char *data" "int length" "int *transferred" "unsigned int timeout"
409Perform an USB bulk transfer.
410A timeout value of zero means no timeout.
411The timeout value is given in milliseconds.
412Returns 0 on success, LIBUSB_ERROR_TIMEOUT
413if the transfer timed out, LIBUSB_ERROR_PIPE if the control request was not
414supported, LIBUSB_ERROR_OVERFLOW if the device offered more data,
415LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and
416a LIBUSB_ERROR code on other failure.
417.Pp
418.Ft int
419.Fn libusb_interrupt_transfer "struct libusb_device_handle *devh" "unsigned char endpoint" "unsigned char *data" "int length" "int *transferred" "unsigned int timeout"
420Perform an USB Interrupt transfer.
421A timeout value of zero means no timeout.
422The timeout value is given in milliseconds.
423Returns 0 on success, LIBUSB_ERROR_TIMEOUT
424if the transfer timed out, LIBUSB_ERROR_PIPE if the control request was not
425supported, LIBUSB_ERROR_OVERFLOW if the device offered more data,
426LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and
427a LIBUSB_ERROR code on other failure.
428.Sh USB EVENTS
429.Ft int
430.Fn libusb_try_lock_events "libusb_context *ctx"
431Try to acquire the event handling lock.
432Returns 0 if the lock was obtained and 1 if not.
433.Pp
434.Ft void
435.Fn libusb_lock_events "libusb_context *ctx"
436Acquire the event handling lock.
437This function is blocking.
438.Pp
439.Ft void
440.Fn libusb_unlock_events "libusb_context *ctx"
441Release the event handling lock.
442This will wake up any thread blocked
443on
444.Fn libusb_wait_for_event .
445.Pp
446.Ft int
447.Fn libusb_event_handling_ok "libusb_context *ctx"
448Determine if it still OK for this thread to be doing event handling.
449Returns 1
450if event handling can start or continue.
451Returns 0 if this thread must give up
452the events lock.
453.Pp
454.Ft int
455.Fn libusb_event_handler_active "libusb_context *ctx"
456Determine if an active thread is handling events.
457Returns 1 if there is a thread handling events and 0 if there
458are no threads currently handling events.
459.Pp
460.Ft void
461.Fn libusb_lock_event_waiters "libusb_context *ctx"
462Acquire the event_waiters lock.
463This lock is designed to be obtained in the
464situation where you want to be aware when events are completed, but some other
465thread is event handling so calling libusb_handle_events() is not allowed.
466.Pp
467.Ft void
468.Fn libusb_unlock_event_waiters "libusb_context *ctx"
469Release the event_waiters lock.
470.Pp
471.Ft int
472.Fn libusb_wait_for_event "libusb_context *ctx" "struct timeval *tv"
473Wait for another thread to signal completion of an event.
474Must be called
475with the event waiters lock held, see libusb_lock_event_waiters().
476This will
477block until the timeout expires or a transfer completes or a thread releases
478the event handling lock through libusb_unlock_events().
479Returns 0 after a
480transfer completes or another thread stops event handling, and 1 if the
481timeout expired.
482.Pp
483.Ft int
484.Fn libusb_handle_events_timeout "libusb_context *ctx" "struct timeval *tv"
485Handle any pending events by checking if timeouts have expired and by
486checking the set of file descriptors for activity.
487Returns 0 on success, or a
488LIBUSB_ERROR code on failure.
489.Pp
490.Ft int
491.Fn libusb_handle_events "libusb_context *ctx"
492Handle any pending events in blocking mode with a sensible timeout.
493Returns 0
494on success and a LIBUSB_ERROR code on failure.
495.Pp
496.Ft int
497.Fn libusb_handle_events_locked "libusb_context *ctx" "struct timeval *tv"
498Handle any pending events by polling file descriptors, without checking if
499another thread is already doing so.
500Must be called with the event lock held.
501.Pp
502.Ft int
503.Fn libusb_get_next_timeout "libusb_context *ctx" "struct timeval *tv"
504Determine the next internal timeout that libusb needs to handle.
505Returns 0
506if there are no pending timeouts, 1 if a timeout was returned, or a LIBUSB_ERROR
507code on failure.
508.Pp
509.Ft void
510.Fn libusb_set_pollfd_notifiers "libusb_context *ctx" "libusb_pollfd_added_cb added_cb" "libusb_pollfd_removed_cb remove_cb" "void *user_data"
511Register notification functions for file descriptor additions/removals.
512These functions will be invoked for every new or removed file descriptor
513that libusb uses as an event source.
514.Pp
515.Ft const struct libusb_pollfd **
516.Fn libusb_get_pollfds "libusb_context *ctx"
517Retrive a list of file descriptors that should be polled by your main loop as
518libusb event sources.
519Returns a NULL-terminated list on success or NULL on failure.
520.Sh LIBUSB VERSION 0.1 COMPATIBILITY
521The library is also compliant with LibUSB version 0.1.12.
522.Pp
523.Fn usb_open
524.Fn usb_close
525.Fn usb_get_string
526.Fn usb_get_string_simple
527.Fn usb_get_descriptor_by_endpoint
528.Fn usb_get_descriptor
529.Fn usb_parse_descriptor
530.Fn usb_parse_configuration
531.Fn usb_destroy_configuration
532.Fn usb_fetch_and_parse_descriptors
533.Fn usb_bulk_write
534.Fn usb_bulk_read
535.Fn usb_interrupt_write
536.Fn usb_interrupt_read
537.Fn usb_control_msg
538.Fn usb_set_configuration
539.Fn usb_claim_interface
540.Fn usb_release_interface
541.Fn usb_set_altinterface
542.Fn usb_resetep
543.Fn usb_clear_halt
544.Fn usb_reset
545.Fn usb_strerror
546.Fn usb_init
547.Fn usb_set_debug
548.Fn usb_find_busses
549.Fn usb_find_devices
550.Fn usb_device
551.Fn usb_get_busses
552.Fn usb_check_connected
553.Fn usb_get_driver_np
554.Fn usb_detach_kernel_driver_np
555.Sh SEE ALSO
556.Xr libusb20 3 ,
557.Xr usb 4 ,
558.Xr usbconfig 8 ,
559.Xr usbdump 8
560.Pp
561.Pa http://libusb.sourceforge.net/
562.Sh HISTORY
563.Nm
564support first appeared in
565.Fx 8.0 .
566