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