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