A new pure-Rust library for cross-platform low-level access to USB devices.

Related tags

Command-line nusb
Overview

nusb

A new pure-Rust library for cross-platform low-level access to USB devices.

Documentation

Compared to rusb and libusb

  • Pure Rust, no dependency on libusb or any other C library.
  • Async-first, while not requiring an async runtime like tokio or async-std. Still easily supports blocking with futures_lite::block_on.
  • No context object. You just open a device. There is a global event loop thread that is started when opening the first device.
  • Thinner layer over OS APIs, with less internal state.

šŸš§ Current status

  • Linux: Control, bulk and interrupt transfers work, minimally tested
  • Windows: Control, bulk and interrupt transfers work, minimally tested
  • macOS : Not yet implemented

License

MIT or Apache 2.0, at your option

Comments
  • linux: fix deadlock on event loop.

    linux: fix deadlock on event loop.

    the event loop may deadlock because it drops Arc<LinuxDevice>s while DEVICES is locked. Dropping a LinuxDevice will unregister its fd, which locks DEVICES reentrantly.

    problem seen in the wild, backtrace from gdb:

    #0  syscall () at ../sysdeps/unix/sysv/linux/x86_64/syscall.S:38
    #1  0x0000562ffff02214 in std::sys::unix::futex::futex_wait () at library/std/src/sys/unix/futex.rs:62
    #2  std::sys::unix::locks::futex_mutex::Mutex::lock_contended () at library/std/src/sys/unix/locks/futex_mutex.rs:56
    #3  0x0000562fffb3cc2d in std::sys::unix::locks::futex_mutex::Mutex::lock (self=<optimized out>)
        at /rustc/e0d7ed1f453fb54578cc96dfea859b0e7be15016/library/std/src/sys/unix/locks/futex_mutex.rs:28
    #4  std::sync::mutex::Mutex<slab::Slab<alloc::sync::Weak<nusb::platform::linux_usbfs::device::LinuxDevice, alloc::alloc::Global>>>::lock<slab::Slab<alloc::sync::Weak<nusb::platform::linux_usbfs::device::LinuxDevice, alloc::alloc::Global>>> (self=<optimized out>)
        at /rustc/e0d7ed1f453fb54578cc96dfea859b0e7be15016/library/std/src/sync/mutex.rs:273
    #5  nusb::platform::linux_usbfs::events::unregister (fd=<optimized out>, events_id=0) at src/platform/linux_usbfs/events.rs:48
    #6  0x0000562fffb3a245 in nusb::platform::linux_usbfs::device::{impl#1}::drop (self=<optimized out>)
        at src/platform/linux_usbfs/device.rs:116
    #7  core::ptr::drop_in_place<nusb::platform::linux_usbfs::device::LinuxDevice> ()
        at /rustc/e0d7ed1f453fb54578cc96dfea859b0e7be15016/library/core/src/ptr/mod.rs:497
    #8  alloc::sync::Arc<nusb::platform::linux_usbfs::device::LinuxDevice, alloc::alloc::Global>::drop_slow<nusb::platform::linux_usbfs::device::LinuxDevice, alloc::alloc::Global> (self=<optimized out>)
        at /rustc/e0d7ed1f453fb54578cc96dfea859b0e7be15016/library/alloc/src/sync.rs:1751
    #9  0x0000562fffb3cf29 in alloc::sync::{impl#33}::drop<nusb::platform::linux_usbfs::device::LinuxDevice, alloc::alloc::Global> (
        self=0x7fad8f800b28) at /rustc/e0d7ed1f453fb54578cc96dfea859b0e7be15016/library/alloc/src/sync.rs:2407
    #10 core::ptr::drop_in_place<alloc::sync::Arc<nusb::platform::linux_usbfs::device::LinuxDevice, alloc::alloc::Global>> ()
        at /rustc/e0d7ed1f453fb54578cc96dfea859b0e7be15016/library/core/src/ptr/mod.rs:497
    #11 nusb::platform::linux_usbfs::events::event_loop () at src/platform/linux_usbfs/events.rs:63
    #12 0x0000562fffb3bdc6 in core::ops::function::FnOnce::call_once<fn(), ()> ()
        at /rustc/e0d7ed1f453fb54578cc96dfea859b0e7be15016/library/core/src/ops/function.rs:250
    #13 std::sys_common::backtrace::__rust_begin_short_backtrace<fn(), ()> (f=<optimized out>)
        at /rustc/e0d7ed1f453fb54578cc96dfea859b0e7be15016/library/std/src/sys_common/backtrace.rs:154
    #14 0x0000562fffb392e1 in std::thread::{impl#0}::spawn_unchecked_::{closure#1}::{closure#0}<fn(), ()> ()
        at /rustc/e0d7ed1f453fb54578cc96dfea859b0e7be15016/library/std/src/thread/mod.rs:529
    
    opened by Dirbaio 1
  • Allow control transfers on Linux without claiming interface

    Allow control transfers on Linux without claiming interface

    Important for some use cases, but I'm not going to go as far as libusb to support this on WIndows -- libusb will temporarily claim a random interface just to submit a control transfer that is not necessarily related to that interface. So this is Linux-only, and portable code will need to use the Interface method. Should be supportable on macOS as well.

    opened by kevinmehall 0
  • Use RAW_IO on Windows for bulk and interrupt IN endpoints

    Use RAW_IO on Windows for bulk and interrupt IN endpoints

    Windows serializes IN transfers by default, negating the usefulness of Queue. RAW_IO disables this.

    Libusb is adding an option for RAW_IO and decided to not turn it on by default for fear of breaking existing code that makes requests that are not a multiple of maxPacketSize. But nusb has no such consideration, and already documents that IN transfers must be a multiple of maxPacketSize, because it's a good idea anyway to avoid errors on overflow. I think RAW_IO should just be turned on unconditionally, and not doing so is just failing to provide the streaming behavior that users would expect.

    Windows 
    opened by kevinmehall 3
  • Hotplug events on device connect and disconnect

    Hotplug events on device connect and disconnect

    enhancement 
    opened by kevinmehall 4
  • Timeouts for blocking IO

    Timeouts for blocking IO

    For Rust async, it's normal for futures not to have their own timeouts. To implement a timeout you race the future with a timeout future such that the main future will be cancelled if the timeout completes first. But this requires a reactor to provide timer functionality as a generic future, and I want to make nusb not require pulling in tokio or async-std for the common case where you don't actually need async in the rest of the app.

    futures_lite::future::block_on is implemented on top of parking, which has an API with a timeout. So we could see if futures_lite would add a timeout (seems like many uses of blocking on async IO would want this?), find another implementation that has this to recommend, or copy that code here.

    enhancement 
    opened by kevinmehall 0
  • macOS support

    macOS support

    https://developer.apple.com/documentation/iokit/usb_h_user-space

    @ktemkin's code in https://github.com/ktemkin/usrs would be a good starting point for this, and I'd like to avoid duplicating efforts if she plans to continue developing it.

    enhancement macOS 
    opened by kevinmehall 0
  • Add descriptor parsing.

    Add descriptor parsing.

    This PR adds methods to DeviceInfo to get either un-parsed descriptors (as raw bytes), or parsed descriptors (as a tree of Configuration, Interface, InterfaceAlternateSetting, Endpoint) structs.

    Parsing is based off what libusb does, API is similar to rusb's.

    Tested working on Linux and Windows. On windows it's only possible to get descriptors for winusb devices.

    opened by Dirbaio 4
Owner
Kevin Mehall
Kevin Mehall
Low level access to processors using the AArch64 execution state.

aarch64-cpu Low level access to processors using the AArch64 execution state. Usage Please note that for using this crate's register definitions (as p

Rust Embedded 18 Jan 5, 2023
Tool and framework for securely reading untrusted USB mass storage devices.

usbsas is a free and open source (GPLv3) tool and framework for securely reading untrusted USB mass storage devices. Description Following the concept

CEA IT Security 250 Aug 16, 2023
A cross-platform library for retrieving information about connected devices.

Devices devices is a cross-platform library for retrieving information about connected devices. Combined with a library like sysinfo, a more or less c

Hank Jordan 4 Jan 8, 2023
Cross-platform file sharig application for desktop and mobile devices

Skylite Description Getting Started Dependencies Installing Executing program License Acknowledgments Description Cross platform file sharing applicat

Adeoye Adefemi 5 Nov 16, 2023
Pure rust library for reading / writing DNG files providing access to the raw data in a zero-copy friendly way.

DNG-rs ā€ƒ A pure rust library for reading / writing DNG files providing access to the raw data in a zero-copy friendly way. Also containing code for re

apertusĀ° - open source cinema 4 Dec 1, 2022
Low-level Rust library for implementing terminal command line interface, like in embedded systems.

Terminal CLI Need to build an interactive command prompt, with commands, properties and with full autocomplete? This is for you. Example, output only

HashMismatch 47 Nov 25, 2022
A pure Rust, cross-platform soundboard app

afx This thing is my attempt at a soundboard program. afx.mp4 Why? I tried some prior art and decided that none of the existing options fit my needs.

Andrew Kvapil 11 Dec 15, 2022
Shell Of A New Machine: Quickly configure new environments

Shell Of A New Machine soanm is a dead-simple tool for easily configuring new UNIX machines, with almost zero prerequisites on the target machine. All

Ben Weinstein-Raun 41 Dec 22, 2022
A low-level ncurses wrapper for Rust

ncurses-rs This is a very thin wrapper around the ncurses TUI lib. NOTE: The ncurses lib is terribly unsafe and ncurses-rs is only the lightest wrappe

Jeaye Wilkerson 628 Jan 7, 2023
Verified Rust for low-level systems code

See Goals for a brief description of the project's goals. Building the project The main project source is in source. tools contains scripts for settin

Secure Foundations Lab 95 Dec 27, 2022
Horus is an open source tool for running forensic and administrative tasks at the kernel level using eBPF, a low-overhead in-kernel virtual machine, and the Rust programming language.

Horus Horus is an open-source tool for running forensic and administrative tasks at the kernel level using eBPF, a low-overhead in-kernel virtual mach

null 4 Dec 15, 2022
Rust low-level minimalist APNG writer and PNG reader with just a few dependencies with all possible formats coverage (including HDR).

project Wiki https://github.com/js29a/micro_png/wiki at glance use micro_png::*; fn main() { // load an image let image = read_png("tmp/test.

jacek SQ6KBQ 8 Aug 30, 2023
Rust client-side implementation of the rock usb protocol

Utility crates to interact with Rockchip devices Rockchip SoCs implement a custom USB protocol when starting in a special recovery mode (sometimes cal

Collabora 12 Mar 14, 2023
A low-level MVCC file format for storing blobs.

Sediment This repository isn't ready for public consumption. It just reached a stage where I wanted to start sharing ideas with others as well as usin

Khonsu Labs 24 Jan 8, 2023
Unopinionated low level API bindings focused on soundness, safety, and stronger types over raw FFI.

?? firehazard ?? Create a fire hazard by locking down your (Microsoft) Windows so nobody can escape (your security sandbox.) Unopinionated low level A

null 5 Nov 17, 2022
Simple low-level web server to serve file uploads with some shell scripting-friendly features

http_file_uploader Simple low-level web server to serve file uploads with some shell scripting-friendly features. A bridge between Web's multipart/for

Vitaly Shukela 2 Oct 27, 2022
High-performance, low-level framework for composing flexible web integrations

High-performance, low-level framework for composing flexible web integrations. Used mainly as a dependency of `barter-rs` project

Barter 8 Dec 28, 2022
Devices can use this SDK to connect to the Spotflow IoT Platform. Supported languages: Rust, Python, C.

Device SDK for Spotflow IoT Platform Languages | Features | Architecture | Building and Testing | License Devices can use this SDK to connect to the S

Spotflow 6 Aug 12, 2024
EVA ICS v4 is a new-generation Industrial-IoT platform for Industry-4.0 automated control systems.

EVA ICS v4 EVA ICSĀ® v4 is a new-generation Industrial-IoT platform for Industry-4.0 automated control systems. The world-first and only Enterprise aut

EVA ICS 25 Feb 1, 2023