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