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