xref: /freebsd/lib/libusb/libusb20.3 (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1.\"
2.\" Copyright (c) 2008-2019 Hans Petter Selasky
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.Dd December 27, 2019
28.Dt LIBUSB20 3
29.Os
30.Sh NAME
31.Nm libusb20
32.
33.Nd "USB access library"
34.
35.
36.Sh LIBRARY
37.
38.
39USB access library (libusb -lusb)
40.
41.
42.
43.Sh SYNOPSIS
44.In libusb20.h
45.Ft int
46.Fn libusb20_tr_close "struct libusb20_transfer *xfer"
47.Ft int
48.Fn libusb20_tr_open "struct libusb20_transfer *xfer" "uint32_t max_buf_size" "uint32_t max_frame_count" "uint8_t ep_no"
49.Fn libusb20_tr_open_stream "struct libusb20_transfer *xfer" "uint32_t max_buf_size" "uint32_t max_frame_count" "uint8_t ep_no" "uint16_t stream_id"
50.Ft struct libusb20_transfer*
51.Fn libusb20_tr_get_pointer "struct libusb20_device *pdev"  "uint16_t tr_index"
52.Ft uint16_t
53.Fn libusb20_tr_get_time_complete "struct libusb20_transfer *xfer"
54.Ft uint32_t
55.Fn libusb20_tr_get_actual_frames "struct libusb20_transfer *xfer"
56.Ft uint32_t
57.Fn libusb20_tr_get_actual_length "struct libusb20_transfer *xfer"
58.Ft uint32_t
59.Fn libusb20_tr_get_max_frames "struct libusb20_transfer *xfer"
60.Ft uint32_t
61.Fn libusb20_tr_get_max_packet_length "struct libusb20_transfer *xfer"
62.Ft uint32_t
63.Fn libusb20_tr_get_max_total_length "struct libusb20_transfer *xfer"
64.Ft uint8_t
65.Fn libusb20_tr_get_status "struct libusb20_transfer *xfer"
66.Ft uint8_t
67.Fn libusb20_tr_pending "struct libusb20_transfer *xfer"
68.Ft void
69.Fn libusb20_tr_callback_wrapper "struct libusb20_transfer *xfer"
70.Ft void
71.Fn libusb20_tr_clear_stall_sync "struct libusb20_transfer *xfer"
72.Ft void
73.Fn libusb20_tr_drain "struct libusb20_transfer *xfer"
74.Ft void
75.Fn libusb20_tr_set_buffer "struct libusb20_transfer *xfer" "void *buffer" "uint16_t fr_index"
76.Ft void
77.Fn libusb20_tr_set_callback "struct libusb20_transfer *xfer" "libusb20_tr_callback_t *cb"
78.Ft void
79.Fn libusb20_tr_set_flags "struct libusb20_transfer *xfer" "uint8_t flags"
80.Ft uint32_t
81.Fn libusb20_tr_get_length "struct libusb20_transfer *xfer" "uint16_t fr_index"
82.Ft void
83.Fn libusb20_tr_set_length "struct libusb20_transfer *xfer" "uint32_t length" "uint16_t fr_index"
84.Ft void
85.Fn libusb20_tr_set_priv_sc0 "struct libusb20_transfer *xfer" "void *sc0"
86.Ft void
87.Fn libusb20_tr_set_priv_sc1 "struct libusb20_transfer *xfer" "void *sc1"
88.Ft void
89.Fn libusb20_tr_set_timeout "struct libusb20_transfer *xfer" "uint32_t timeout"
90.Ft void
91.Fn libusb20_tr_set_total_frames "struct libusb20_transfer *xfer" "uint32_t nframes"
92.Ft void
93.Fn libusb20_tr_setup_bulk "struct libusb20_transfer *xfer" "void *pbuf" "uint32_t length" "uint32_t timeout"
94.Ft void
95.Fn libusb20_tr_setup_control "struct libusb20_transfer *xfer" "void *psetup" "void *pbuf" "uint32_t timeout"
96.Ft void
97.Fn libusb20_tr_setup_intr "struct libusb20_transfer *xfer" "void *pbuf" "uint32_t length" "uint32_t timeout"
98.Ft void
99.Fn libusb20_tr_setup_isoc "struct libusb20_transfer *xfer" "void *pbuf" "uint32_t length" "uint61_t fr_index"
100.Ft uint8_t
101.Fn libusb20_tr_bulk_intr_sync "struct libusb20_transfer *xfer" "void *pbuf" "uint32_t length" "uint32_t *pactlen" "uint32_t timeout"
102.Ft void
103.Fn libusb20_tr_start "struct libusb20_transfer *xfer"
104.Ft void
105.Fn libusb20_tr_stop "struct libusb20_transfer *xfer"
106.Ft void
107.Fn libusb20_tr_submit "struct libusb20_transfer *xfer"
108.Ft void *
109.Fn libusb20_tr_get_priv_sc0 "struct libusb20_transfer *xfer"
110.Ft void *
111.Fn libusb20_tr_get_priv_sc1 "struct libusb20_transfer *xfer"
112.Ft const char *
113.Fn libusb20_dev_get_backend_name "struct libusb20_device *"
114.Ft int
115.Fn libusb20_dev_get_port_path "struct libusb20_device *pdev" "uint8_t *buf" "uint8_t bufsize"
116.Ft int
117.Fn libusb20_dev_get_info "struct libusb20_device *pdev" "struct usb_device_info *pinfo"
118.Ft int
119.Fn libusb20_dev_get_iface_desc "struct libusb20_device *pdev" "uint8_t iface_index" "char *buf" "uint8_t len"
120.Ft const char *
121.Fn libusb20_dev_get_desc "struct libusb20_device *pdev"
122.Ft int
123.Fn libusb20_dev_get_stats "struct libusb20_device *pdev" "struct libusb20_device_stats *pstats"
124.Ft int
125.Fn libusb20_dev_close "struct libusb20_device *pdev"
126.Ft int
127.Fn libusb20_dev_detach_kernel_driver "struct libusb20_device *pdev" "uint8_t iface_index"
128.Ft int
129.Fn libusb20_dev_set_config_index "struct libusb20_device *pdev" "uint8_t configIndex"
130.Ft int
131.Fn libusb20_dev_get_debug "struct libusb20_device *pdev"
132.Ft int
133.Fn libusb20_dev_get_fd "struct libusb20_device *pdev"
134.Ft int
135.Fn libusb20_dev_kernel_driver_active "struct libusb20_device *pdev" "uint8_t iface_index"
136.Ft int
137.Fn libusb20_dev_open "struct libusb20_device *pdev" "uint16_t transfer_max"
138.Ft int
139.Fn libusb20_dev_process "struct libusb20_device *pdev"
140.Ft int
141.Fn libusb20_dev_request_sync "struct libusb20_device *pdev" "struct LIBUSB20_CONTROL_SETUP_DECODED *setup" "void *data" "uint16_t *pactlen" "uint32_t timeout" "uint8_t flags"
142.Ft int
143.Fn libusb20_dev_req_string_sync "struct libusb20_device *pdev" "uint8_t index" "uint16_t langid" "void *ptr" "uint16_t len"
144.Ft int
145.Fn libusb20_dev_req_string_simple_sync "struct libusb20_device *pdev" "uint8_t index" "void *ptr" "uint16_t len"
146.Ft int
147.Fn libusb20_dev_reset "struct libusb20_device *pdev"
148.Ft int
149.Fn libusb20_dev_check_connected "struct libusb20_device *pdev"
150.Ft int
151.Fn libusb20_dev_set_power_mode "struct libusb20_device *pdev" "uint8_t power_mode"
152.Ft uint8_t
153.Fn libusb20_dev_get_power_mode "struct libusb20_device *pdev"
154.Ft uint16_t
155.Fn libusb20_dev_get_power_usage "struct libusb20_device *pdev"
156.Ft int
157.Fn libusb20_dev_set_alt_index "struct libusb20_device *pdev" "uint8_t iface_index" "uint8_t alt_index"
158.Ft struct LIBUSB20_DEVICE_DESC_DECODED *
159.Fn libusb20_dev_get_device_desc "struct libusb20_device *pdev"
160.Ft struct libusb20_config *
161.Fn libusb20_dev_alloc_config "struct libusb20_device *pdev" "uint8_t config_index"
162.Ft struct libusb20_device *
163.Fn libusb20_dev_alloc "void"
164.Ft uint8_t
165.Fn libusb20_dev_get_address "struct libusb20_device *pdev"
166.Ft uint8_t
167.Fn libusb20_dev_get_parent_address "struct libusb20_device *pdev"
168.Ft uint8_t
169.Fn libusb20_dev_get_parent_port "struct libusb20_device *pdev"
170.Ft uint8_t
171.Fn libusb20_dev_get_bus_number "struct libusb20_device *pdev"
172.Ft uint8_t
173.Fn libusb20_dev_get_mode "struct libusb20_device *pdev"
174.Ft uint8_t
175.Fn libusb20_dev_get_speed "struct libusb20_device *pdev"
176.Ft uint8_t
177.Fn libusb20_dev_get_config_index "struct libusb20_device *pdev"
178.Ft void
179.Fn libusb20_dev_free "struct libusb20_device *pdev"
180.Ft void
181.Fn libusb20_dev_set_debug "struct libusb20_device *pdev" "int debug"
182.Ft void
183.Fn libusb20_dev_wait_process "struct libusb20_device *pdev" "int timeout"
184.Ft int
185.Fn libusb20_be_get_template "struct libusb20_backend *pbe" "int *ptemp"
186.Ft int
187.Fn libusb20_be_set_template "struct libusb20_backend *pbe" "int temp"
188.Ft int
189.Fn libusb20_be_get_dev_quirk "struct libusb20_backend *pber" "uint16_t index" "struct libusb20_quirk *pq"
190.Ft int
191.Fn libusb20_be_get_quirk_name "struct libusb20_backend *pbe" "uint16_t index" "struct libusb20_quirk *pq"
192.Ft int
193.Fn libusb20_be_add_dev_quirk "struct libusb20_backend *pbe" "struct libusb20_quirk *pq"
194.Ft int
195.Fn libusb20_be_remove_dev_quirk "struct libusb20_backend *pbe" "struct libusb20_quirk *pq"
196.Ft struct libusb20_backend *
197.Fn libusb20_be_alloc_default "void"
198.Ft struct libusb20_backend *
199.Fn libusb20_be_alloc_freebsd "void"
200.Ft struct libusb20_backend *
201.Fn libusb20_be_alloc_linux "void"
202.Ft struct libusb20_device *
203.Fn libusb20_be_device_foreach  "struct libusb20_backend *pbe" "struct libusb20_device *pdev"
204.Ft void
205.Fn libusb20_be_dequeue_device "struct libusb20_backend *pbe" "struct libusb20_device *pdev"
206.Ft void
207.Fn libusb20_be_enqueue_device "struct libusb20_backend *pbe" "struct libusb20_device *pdev"
208.Ft void
209.Fn libusb20_be_free "struct libusb20_backend *pbe"
210.Ft uint8_t
211.Fn libusb20_me_get_1 "const struct libusb20_me_struct *me" "uint16_t off"
212.Ft uint16_t
213.Fn libusb20_me_get_2 "const struct libusb20_me_struct *me" "uint16_t off"
214.Ft uint16_t
215.Fn libusb20_me_encode "void *pdata" "uint16_t len" "const void *pdecoded"
216.Ft uint16_t
217.Fn libusb20_me_decode "const void *pdata" "uint16_t len" "void *pdecoded"
218.Ft "const uint8_t *"
219.Fn libusb20_desc_foreach "const struct libusb20_me_struct *me" "const uint8_t *pdesc"
220.Ft "const char *"
221.Fn libusb20_strerror "int code"
222.Ft "const char *"
223.Fn libusb20_error_name "int code"
224.
225.
226.Sh DESCRIPTION
227.
228The
229.Nm
230library implements functions to be able to easily access and control
231USB through the USB file system interface.
232The
233.Nm
234interfaces are specific to the
235.Fx
236usb stack and are not available on other operating systems, portable
237applications should consider using
238.Xr libusb 3 .
239.
240.
241.Sh USB TRANSFER OPERATIONS
242.
243.
244.Fn libusb20_tr_close
245will release all kernel resources associated with an USB
246.Fa xfer .
247.
248This function returns zero upon success.
249.
250Non-zero return values indicate a LIBUSB20_ERROR value.
251.
252.Pp
253.
254.Fn libusb20_tr_open
255will allocate kernel buffer resources according to
256.Fa max_buf_size
257and
258.Fa max_frame_count
259associated with an USB
260.Fa pxfer
261and bind the transfer to the specified
262.Fa ep_no .
263.Fa max_buf_size
264is the minimum buffer size which the data transport layer has to support.
265If
266.Fa max_buf_size
267is zero, the
268.Nm
269library will use wMaxPacketSize to compute the buffer size.
270This can be useful for isochronous transfers.
271The actual buffer size can be greater than
272.Fa max_buf_size
273and is returned by
274.Fn libusb20_tr_get_max_total_length .
275.
276If
277.Fa max_frame_count
278is OR'ed with LIBUSB20_MAX_FRAME_PRE_SCALE the remaining part of the
279argument is converted from milliseconds into the actual number of
280frames rounded up, when this function returns.
281This flag is only valid for ISOCHRONOUS transfers and has no effect
282for other transfer types.
283The actual number of frames setup is found by calling
284.Fn libusb20_tr_get_max_frames .
285.
286This function returns zero upon success.
287.
288Non-zero return values indicate a LIBUSB20_ERROR value.
289.
290.Pp
291.
292.Fn libusb20_tr_open_stream
293is identical to
294.Fn libusb20_tr_open
295except that a stream ID can be specified for BULK endpoints having
296such a feature.
297.Fn libusb20_tr_open
298can be used to open stream ID zero.
299.
300.Pp
301.
302.Fn libusb20_tr_get_pointer
303will return a pointer to the allocated USB transfer according to the
304.Fa pdev
305and
306.Fa tr_index
307arguments.
308.
309This function returns NULL in case of failure.
310.
311.Pp
312.
313.Fn libusb20_tr_get_time_complete
314will return the completion time of an USB transfer in
315millisecond units.
316This function is most useful for isochronous USB transfers when doing echo
317cancelling.
318.
319.Pp
320.
321.Fn libusb20_tr_get_actual_frames
322will return the actual number of USB frames after an USB
323transfer completed.
324A value of zero means that no data was transferred.
325.Pp
326.
327.Fn libusb20_tr_get_actual_length
328will return the sum of the actual length for all
329transferred USB frames for the given USB transfer.
330.
331.Pp
332.
333.Fn libusb20_tr_get_max_frames
334will return the maximum number of USB frames that were
335allocated when an USB transfer was setup for the given USB transfer.
336.
337.Pp
338.
339.Fn libusb20_tr_get_max_packet_length
340will return the maximum packet length in bytes
341associated with the given USB transfer.
342.
343The packet length can be used round up buffer sizes so that short USB
344packets are avoided for proxy buffers.
345.
346.
347.Pp
348.
349.Fn libusb20_tr_get_max_total_length
350will return the maximum value for the data length sum of all USB
351frames associated with an USB transfer.
352In case of control transfers the value returned does not include the
353length of the SETUP packet, 8 bytes, which is part of frame zero.
354The returned value of this function is always aligned to the maximum
355packet size, wMaxPacketSize, of the endpoint which the USB transfer is
356bound to.
357.
358.Pp
359.
360.Fn libusb20_tr_get_status
361will return the status of an USB transfer.
362.
363Status values are defined by a set of LIBUSB20_TRANSFER_XXX enums.
364.
365.Pp
366.
367.Fn libusb20_tr_pending
368will return non-zero if the given USB transfer is
369pending for completion.
370.
371Else this function returns zero.
372.
373.Pp
374.
375.Fn libusb20_tr_callback_wrapper
376This is an internal function used to wrap asynchronous USB callbacks.
377.
378.Pp
379.
380.Fn libusb20_tr_clear_stall_sync
381This is an internal function used to synchronously clear the stall on
382the given USB transfer.
383.
384Please see the USB specification for more information on stall
385clearing.
386.
387If the given USB transfer is pending when this function is called, the
388USB transfer will complete with an error after that this function has
389been called.
390.
391.Pp
392.
393.Fn libusb20_tr_drain
394will stop the given USB transfer and will not return
395until the USB transfer has been stopped in hardware.
396.
397.Pp
398.
399.Fn libusb20_tr_set_buffer
400is used to set the
401.Fa buffer
402pointer for the given USB transfer and
403.Fa fr_index .
404.
405Typically the frame index is zero.
406.
407.
408.Pp
409.
410.Fn libusb20_tr_set_callback
411is used to set the USB callback for asynchronous USB
412transfers.
413.
414The callback type is defined by libusb20_tr_callback_t.
415.
416.Pp
417.
418.Fn libusb20_tr_set_flags
419is used to set various USB flags for the given USB transfer.
420.Bl -tag -width "LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK"
421.It LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK
422Report a short frame as error.
423.It LIBUSB20_TRANSFER_MULTI_SHORT_NOT_OK
424Multiple short frames are not allowed.
425.It LIBUSB20_TRANSFER_FORCE_SHORT
426All transmitted frames are short terminated.
427.It LIBUSB20_TRANSFER_DO_CLEAR_STALL
428Will do a clear-stall before starting the transfer.
429.El
430.
431.Pp
432.
433.Fn libusb20_tr_get_length
434returns the length of the given USB frame by index.
435After an USB transfer is complete the USB frame length will get updated to the actual transferred length.
436.
437.Pp
438.
439.Fn libusb20_tr_set_length
440sets the length of the given USB frame by index.
441.
442.Pp
443.
444.Fn libusb20_tr_set_priv_sc0
445sets private driver pointer number zero.
446.
447.Pp
448.
449.Fn libusb20_tr_set_priv_sc1
450sets private driver pointer number one.
451.
452.Pp
453.
454.Fn libusb20_tr_set_timeout
455sets the timeout for the given USB transfer.
456.
457A timeout value of zero means no timeout.
458.
459The timeout is given in milliseconds.
460.
461.Pp
462.
463.Fn libusb20_tr_set_total_frames
464sets the total number of frames that should be executed when the USB transfer is submitted.
465.
466The total number of USB frames must be less than the maximum number of USB frames associated with the given USB transfer.
467.
468.Pp
469.
470.Fn libusb20_tr_setup_bulk
471is a helper function for setting up a single frame USB BULK transfer.
472.
473.Pp
474.
475.Fn libusb20_tr_setup_control
476is a helper function for setting up a single or dual
477frame USB CONTROL transfer depending on the control transfer length.
478.
479.Pp
480.
481.Fn libusb20_tr_setup_intr
482is a helper function for setting up a single frame USB INTERRUPT transfer.
483.
484.Pp
485.
486.Fn libusb20_tr_setup_isoc
487is a helper function for setting up a multi frame USB ISOCHRONOUS transfer.
488.
489.Pp
490.
491.Fn libusb20_tr_bulk_intr_sync
492will perform a synchronous BULK or INTERRUPT transfer having length given by the
493.Fa length
494argument and buffer pointer given by the
495.Fa pbuf
496argument on the USB transfer given by the
497.Fa xfer
498argument.
499.
500If the
501.Fa pactlen
502argument is non-NULL the actual transfer length will be stored at the given pointer destination.
503.
504If the
505.Fa timeout
506argument is non-zero the transfer will timeout after the given value in milliseconds.
507.
508This function does not change the transfer flags, like short packet not ok.
509.
510This function returns zero on success else a LIBUSB20_TRANSFER_XXX value is returned.
511.
512.Pp
513.
514.Fn libusb20_tr_start
515will get the USB transfer started, if not already
516started.
517.
518This function will not get the transfer queued in hardware.
519.
520This function is non-blocking.
521.
522.Pp
523.
524.Fn libusb20_tr_stop
525will get the USB transfer stopped, if not already stopped.
526.
527This function is non-blocking, which means that the actual stop can
528happen after the return of this function.
529.
530.Pp
531.
532.Fn libusb20_tr_submit
533will get the USB transfer queued in hardware.
534.
535.
536.Pp
537.
538.Fn libusb20_tr_get_priv_sc0
539returns private driver pointer number zero associated
540with an USB transfer.
541.
542.
543.Pp
544.
545.Fn libusb20_tr_get_priv_sc1
546returns private driver pointer number one associated
547with an USB transfer.
548.
549.
550.Sh USB DEVICE OPERATIONS
551.
552.
553.Fn libusb20_dev_get_backend_name
554returns a zero terminated string describing the backend used.
555.
556.Pp
557.
558.Fn libusb20_dev_get_port_path
559retrieves the list of USB port numbers which the datastream for a given USB device follows.
560The first port number is the Root HUB port number.
561Then children port numbers follow.
562The Root HUB device itself has a port path length of zero.
563Valid port numbers start at one and range until and including 255.
564Typically there should not be more than 16 levels, due to electrical and protocol limitations.
565This functions returns the number of actual port levels upon success
566else a LIBUSB20_ERROR value is returned which are always negative.
567If the actual number of port levels is greater than the maximum
568specified, a LIBUSB20_ERROR value is returned.
569.
570.Pp
571.
572.Fn libusb20_dev_get_info
573retrieves the BSD specific usb_device_info structure into the memory location given by
574.Fa pinfo .
575The USB device given by
576.Fa pdev
577must be opened before this function will succeed.
578This function returns zero on success else a LIBUSB20_ERROR value is returned.
579.
580.Pp
581.
582.Fn libusb20_dev_get_iface_desc
583retrieves the kernel interface description for the given USB
584.Fa iface_index .
585The format of the USB interface description is: "drivername<unit>: <description>"
586The description string is always zero terminated.
587A zero length string is written in case no driver is attached to the given interface.
588The USB device given by
589.Fa pdev
590must be opened before this function will succeed.
591This function returns zero on success else a LIBUSB20_ERROR value is returned.
592.
593.Pp
594.
595.Fn libusb20_dev_get_desc
596returns a zero terminated string describing the given USB device.
597The format of the string is: "drivername<unit>: <description>"
598.
599.Pp
600.
601.Fn libusb20_dev_get_stats
602retrieves the device statistics into the structure pointed to by the
603.Fa pstats
604argument.
605This function returns zero on success else a LIBUSB20_ERROR value is returned.
606.
607.Pp
608.
609.Fn libusb20_dev_close
610will close the given USB device.
611.
612This function returns zero on success else a LIBUSB20_ERROR value is
613returned.
614.
615.Pp
616.
617.Fn libusb20_dev_detach_kernel_driver
618will try to detach the kernel driver for the USB interface given by
619.Fa iface_index .
620.
621This function returns zero on success else a LIBUSB20_ERROR value is
622returned.
623.
624.Pp
625.
626.Fn libusb20_dev_set_config_index
627will try to set the configuration index on an USB
628device.
629.
630The first configuration index is zero.
631.
632The un-configure index is 255.
633.
634This function returns zero on success else a LIBUSB20_ERROR value is returned.
635.
636.Pp
637.
638.Fn libusb20_dev_get_debug
639returns the debug level of an USB device.
640.
641.Pp
642.
643.Fn libusb20_dev_get_fd
644returns the file descriptor of the given USB device.
645.
646A negative value is returned when no file descriptor is present.
647.
648The file descriptor can be used for polling purposes.
649.
650.Pp
651.
652.Fn libusb20_dev_kernel_driver_active
653returns zero if a kernel driver is active on the given USB interface.
654.
655Else a LIBUSB20_ERROR value is returned.
656.
657.Pp
658.
659.Fn libusb20_dev_open
660opens an USB device so that setting up USB transfers
661becomes possible.
662.
663The number of USB transfers can be zero which means only control
664transfers are allowed.
665.
666This function returns zero on success else a LIBUSB20_ERROR value is
667returned.
668.
669A return value of LIBUSB20_ERROR_BUSY means that the device is already
670opened.
671.
672.Pp
673.
674.Fn libusb20_dev_process
675is called to sync kernel USB transfers with userland USB
676transfers.
677.
678This function returns zero on success else a LIBUSB20_ERROR value is
679returned typically indicating that the given USB device has been
680detached.
681.
682.Pp
683.
684.Fn libusb20_dev_request_sync
685will perform a synchronous control request on the given
686USB device.
687.
688Before this call will succeed the USB device must be opened.
689.
690.Fa setup
691is a pointer to a decoded and host endian SETUP packet.
692.Fa data
693is a pointer to a data transfer buffer associated with the control transaction.
694This argument can be NULL.
695.Fa pactlen
696is a pointer to a variable that will hold the actual transfer length after the
697control transaction is complete.
698.Fa timeout
699is the transaction timeout given in milliseconds.
700A timeout of zero means no timeout.
701.Fa flags
702is used to specify transaction flags, for example LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK.
703.
704This function returns zero on success else a LIBUSB20_ERROR value is
705returned.
706.
707.Pp
708.
709.Fn libusb20_dev_req_string_sync
710will synchronously request an USB string by language ID
711and string index into the given buffer limited by a maximum length.
712.
713This function returns zero on success else a LIBUSB20_ERROR value is
714returned.
715.
716.Pp
717.
718.Fn libusb20_dev_req_string_simple_sync
719will synchronously request an USB string using the
720default language ID and convert the string into ASCII before storing
721the string into the given buffer limited by a maximum length which
722includes the terminating zero.
723.
724This function returns zero on success else a LIBUSB20_ERROR value is
725returned.
726.
727.
728.Pp
729.
730.Fn libusb20_dev_reset
731will try to BUS reset the given USB device and restore
732the last set USB configuration.
733.
734This function returns zero on success else a LIBUSB20_ERROR value is
735returned.
736.
737.
738.Pp
739.
740.Fn libusb20_dev_check_connected
741will check if an opened USB device is still connected.
742.
743This function returns zero if the device is still connected else a LIBUSB20_ERROR value is returned.
744.
745.
746.Pp
747.
748.Fn libusb20_dev_set_power_mode
749sets the power mode of the USB device.
750.
751Valid power modes:
752.Bl -tag -width "LIBUSB20_POWER_OFF"
753.It LIBUSB20_POWER_OFF
754.It LIBUSB20_POWER_ON
755.It LIBUSB20_POWER_SAVE
756.It LIBUSB20_POWER_SUSPEND
757.It LIBUSB20_POWER_RESUME
758.El
759.Pp
760.
761This function returns zero on success else a LIBUSB20_ERROR value is
762returned.
763.
764.Pp
765.
766.Fn libusb20_dev_get_power_mode
767returns the currently selected power mode for the given
768USB device.
769.
770.Pp
771.
772.Fn libusb20_dev_get_power_usage
773returns the reported power usage in milliamps for the given USB device.
774A power usage of zero typically means that the device is self powered.
775.
776.Pp
777.
778.Fn libusb20_dev_set_alt_index
779will try to set the given alternate index for the given
780USB interface index.
781.
782This function returns zero on success else a LIBUSB20_ERROR value is
783returned.
784.
785.Pp
786.
787.Fn libusb20_dev_get_device_desc
788returns a pointer to the decoded and host endian version
789of the device descriptor.
790.
791The USB device need not be opened when calling this function.
792.
793.Pp
794.
795.Fn libusb20_dev_alloc_config
796will read out and decode the USB config descriptor for the given USB device
797and config index.
798This function returns a pointer to the decoded configuration which must eventually
799be passed to free().
800NULL is returned in case of failure.
801.
802.Pp
803.
804.Fn libusb20_dev_alloc
805is an internal function to allocate a new USB device.
806.
807.Pp
808.
809.Fn libusb20_dev_get_address
810returns the internal and not necessarily the real
811hardware address of the given USB device.
812Valid addresses start at one.
813.
814.Pp
815.
816.Fn libusb20_dev_get_parent_address
817returns the internal and not necessarily the real hardware address of
818the given parent USB HUB device.
819This value is zero for the root HUB which usually has a device address
820equal to one.
821Valid addresses start at one.
822.
823.Pp
824.
825.Fn libusb20_dev_get_parent_port
826returns the port number on the parent USB HUB device.
827This value is zero for the root HUB which usually has a device address
828equal to one.
829Valid port numbers start at one.
830.
831.Pp
832.
833.Fn libusb20_dev_get_bus_number
834returns the internal bus number which the given USB
835device belongs to.
836Valid bus numbers start at zero.
837.
838.Pp
839.
840.Fn libusb20_dev_get_mode
841returns the current operation mode of the USB entity.
842.
843Valid return values are:
844.Bl -tag -width "LIBUSB20_MODE_DEVICE"
845.It LIBUSB20_MODE_HOST
846.It LIBUSB20_MODE_DEVICE
847.El
848.
849.Pp
850.
851.Fn libusb20_dev_get_speed
852returns the current speed of the given USB device.
853.
854.Bl -tag -width "LIBUSB20_SPEED_VARIABLE"
855.It LIBUSB20_SPEED_UNKNOWN
856.It LIBUSB20_SPEED_LOW
857.It LIBUSB20_SPEED_FULL
858.It LIBUSB20_SPEED_HIGH
859.It LIBUSB20_SPEED_VARIABLE
860.It LIBUSB20_SPEED_SUPER
861.El
862.
863.Pp
864.
865.Fn libusb20_dev_get_config_index
866returns the currently selected config index for the given
867USB device.
868.
869.Pp
870.
871.Fn libusb20_dev_free
872will free the given USB device and all associated USB
873transfers.
874.
875.Pp
876.
877.Fn libusb20_dev_set_debug
878will set the debug level for the given USB device.
879.
880.Pp
881.
882.Fn libusb20_dev_wait_process
883will wait until a pending USB transfer has completed on
884the given USB device.
885.
886A timeout value can be specified which is passed on to the
887.Xr poll 2
888function.
889.
890.Sh USB BACKEND OPERATIONS
891.
892.Fn libusb20_be_get_template
893will return the currently selected global USB device
894side mode template into the integer pointer
895.Fa ptemp .
896This function returns zero on success else a LIBUSB20_ERROR value is
897returned.
898.
899.Pp
900.
901.Fn libusb20_be_set_template
902will set the global USB device side mode template to
903.Fa temp .
904The new template is not activated until after the next USB
905enumeration.
906The template number decides how the USB device will present itself to
907the USB Host, like Mass Storage Device, USB Ethernet Device.
908Also see the
909.Xr usb2_template 4
910module.
911This function returns zero on success else a LIBUSB20_ERROR value is
912returned.
913.
914.Pp
915.
916.Fn libusb20_be_get_dev_quirk
917will return the device quirk according to
918.Fa index
919into the libusb20_quirk structure pointed to by
920.Fa pq .
921This function returns zero on success else a LIBUSB20_ERROR value is
922returned.
923.
924If the given quirk does not exist LIBUSB20_ERROR_NOT_FOUND is
925returned.
926.
927.Pp
928.
929.Fn libusb20_be_get_quirk_name
930will return the quirk name according to
931.Fa index
932into the libusb20_quirk structure pointed to by
933.Fa pq .
934This function returns zero on success else a LIBUSB20_ERROR value is
935returned.
936.
937If the given quirk does not exist LIBUSB20_ERROR_NOT_FOUND is
938returned.
939.
940.Pp
941.
942.Fn libusb20_be_add_dev_quirk
943will add the libusb20_quirk structure pointed to by the
944.Fa pq
945argument into the device quirk list.
946.
947This function returns zero on success else a LIBUSB20_ERROR value is
948returned.
949.
950If the given quirk cannot be added LIBUSB20_ERROR_NO_MEM is
951returned.
952.
953.Pp
954.
955.Fn libusb20_be_remove_dev_quirk
956will remove the quirk matching the libusb20_quirk structure pointed to by the
957.Fa pq
958argument from the device quirk list.
959.
960This function returns zero on success else a LIBUSB20_ERROR value is
961returned.
962.
963If the given quirk does not exist LIBUSB20_ERROR_NOT_FOUND is
964returned.
965.
966.Pp
967.
968.Fn libusb20_be_alloc_default
969.Fn libusb20_be_alloc_freebsd
970.Fn libusb20_be_alloc_linux
971These functions are used to allocate a specific USB backend or the operating system
972default USB backend.
973Allocating a backend is a way to scan for currently present USB devices.
974.Pp
975.
976.Fn libusb20_be_device_foreach
977is used to iterate USB devices present in a USB backend.
978.
979The starting value of
980.Fa pdev
981is NULL.
982.
983This function returns the next USB device in the list.
984.
985If NULL is returned the end of the USB device list has been reached.
986.
987.Pp
988.
989.Fn libusb20_be_dequeue_device
990will dequeue the given USB device pointer from the
991backend USB device list.
992.
993Dequeued USB devices will not be freed when the backend is freed.
994.
995.Pp
996.
997.Fn libusb20_be_enqueue_device
998will enqueue the given USB device pointer in the backend USB device list.
999.
1000Enqueued USB devices will get freed when the backend is freed.
1001.
1002.Pp
1003.
1004.Fn libusb20_be_free
1005will free the given backend and all USB devices in its device list.
1006.
1007.
1008.Sh USB DESCRIPTOR PARSING
1009.
1010.Fn libusb20_me_get_1 pie offset
1011This function will return a byte at the given byte offset of a message
1012entity.
1013.
1014This function is safe against invalid offsets.
1015.
1016.Pp
1017.
1018.Fn libusb20_me_get_2 pie offset
1019This function will return a little endian 16-bit value at the given byte offset of a message
1020entity.
1021.
1022This function is safe against invalid offsets.
1023.
1024.Pp
1025.
1026.Fn libusb20_me_encode pbuf len pdecoded
1027This function will encode a so-called *DECODED structure into binary
1028format.
1029.
1030The total encoded length that will fit in the given buffer is
1031returned.
1032.
1033If the buffer pointer is NULL no data will be written to the buffer
1034location.
1035.
1036.Pp
1037.
1038.Fn libusb20_me_decode pbuf len pdecoded
1039This function will decode a binary structure into a so-called *DECODED
1040structure.
1041.
1042The total decoded length is returned.
1043.
1044The buffer pointer cannot be NULL.
1045.
1046.
1047.Sh USB DEBUGGING
1048.Ft const char *
1049.Fn libusb20_strerror "int code"
1050Get the ASCII representation of the error given by the
1051.Fa code
1052argument.
1053This function does not return NULL.
1054.Pp
1055.Ft const char *
1056.Fn libusb20_error_name "int code"
1057Get the ASCII representation of the error enum given by the
1058.Fa code
1059argument.
1060This function does not return NULL.
1061.
1062.Sh FILES
1063.Bl -tag -width Pa
1064.It Pa /dev/usb
1065.El
1066.Sh SEE ALSO
1067.Xr libusb 3 ,
1068.Xr usb 4 ,
1069.Xr usbconfig 8 ,
1070.Xr usbdump 8
1071.
1072.
1073.Sh HISTORY
1074.
1075.
1076Some parts of the
1077.Nm
1078API derives from the libusb project at sourceforge.
1079