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