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