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