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