Skip to main content

USBGadget

A python library for creating and interfacing with USB Gadgets on Linux through libcomposite.

This library assumes the ConfigFS gadget directory to be /sys/kernel/config/usb_gadget. If this is not the case, specify the correct directory in the call to USBGadget(name, path).

Creating a gadget

Any gadget type that libcomposite supports can be created.

import usb_gadget
gadget = usb_gadget.USBGadget('my_test_gadget')
# ... set up gadget parameters, functions and configurations
gadget.activate()

Interfacing with a gadget

This library has an interface for HID gadgets. Specifically, keyboard, mouse and game controller.

import usb_gadget
gadget = usb_gadget.USBGadget('my_test_gadget')
function = usb_gadget.HIDFunction(gadget, 'keyboard0')
keyboard = usb_gadget.KeyboardGadget(function.device)
keyboard.press_and_release('a')

See also example.py for a full keyboard setup

Example HID Report Descriptors

Keyboard
  • 1 byte modifier keys
  • 1 byte empty
  • 6 bytes scancodes currently pressed keys

Report length: 8

0x05, 0x01,        # Usage Page (Generic Desktop Ctrls)
0x09, 0x06,        # Usage (Keyboard)
0xA1, 0x01,        # Collection (Application)
0x05, 0x07,        #   Usage Page (Kbrd/Keypad)
0x19, 0xE0,        #   Usage Minimum (0xE0)
0x29, 0xE7,        #   Usage Maximum (0xE7)
0x15, 0x00,        #   Logical Minimum (0)
0x25, 0x01,        #   Logical Maximum (1)
0x75, 0x01,        #   Report Size (1)
0x95, 0x08,        #   Report Count (8)
0x81, 0x02,        #   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, 0x01,        #   Report Count (1)
0x75, 0x08,        #   Report Size (8)
0x81, 0x03,        #   Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, 0x06,        #   Report Count (6)
0x75, 0x08,        #   Report Size (8)
0x15, 0x00,        #   Logical Minimum (0)
0x25, 0x65,        #   Logical Maximum (101)
0x05, 0x07,        #   Usage Page (Kbrd/Keypad)
0x19, 0x00,        #   Usage Minimum (0x00)
0x29, 0x65,        #   Usage Maximum (0x65)
0x81, 0x00,        #   Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0,              # End Collection
Mouse
  • 1 byte currently pressed buttons
  • 2 bytes X movement
  • 2 bytes Y movement
  • 1 byte vertical scroll wheel
  • 1 byte horizontal scroll wheel

Report length: 7

0x05, 0x01,        # Usage Page (Generic Desktop Ctrls)
0x09, 0x02,        # Usage (Mouse)
0xA1, 0x01,        # Collection (Application)
0x05, 0x09,        #   Usage Page (Button)
0x19, 0x01,        #   Usage Minimum (0x01)
0x29, 0x08,        #   Usage Maximum (0x08)
0x15, 0x00,        #   Logical Minimum (0)
0x25, 0x01,        #   Logical Maximum (1)
0x95, 0x08,        #   Report Count (8)
0x75, 0x01,        #   Report Size (1)
0x81, 0x02,        #   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x05, 0x01,        #   Usage Page (Generic Desktop Ctrls)
0x09, 0x30,        #   Usage (X)
0x09, 0x31,        #   Usage (Y)
0x16, 0x00, 0x80,  #   Logical Minimum (-32768)
0x26, 0xFF, 0x7F,  #   Logical Maximum (32767)
0x75, 0x10,        #   Report Size (16)
0x95, 0x02,        #   Report Count (2)
0x81, 0x06,        #   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x38,        #   Usage (Wheel)
0x15, 0x81,        #   Logical Minimum (-127)
0x25, 0x7F,        #   Logical Maximum (127)
0x75, 0x08,        #   Report Size (8)
0x95, 0x01,        #   Report Count (1)
0x81, 0x06,        #   Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
0x05, 0x0C,        #   Usage Page (Consumer)
0x0A, 0x38, 0x02,  #   Usage (AC Pan)
0x15, 0x81,        #   Logical Minimum (-127)
0x25, 0x7F,        #   Logical Maximum (127)
0x75, 0x08,        #   Report Size (8)
0x95, 0x01,        #   Report Count (1)
0x81, 0x06,        #   Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
0xC0,              # End Collection
Joystick
  • 4 bytes joystick positions (2x XY)
  • 2 bytes trigger positions (2x Z)
  • 3 bytes currently pressed buttons (24 buttons total)

Report length: 9

0x05, 0x01,        # Usage Page (Generic Desktop Ctrls)
0x09, 0x04,        # Usage (Joystick)
0xA1, 0x01,        # Collection (Application)
0x15, 0x81,        #   Logical Minimum (-127)
0x25, 0x7F,        #   Logical Maximum (127)
0x09, 0x01,        #   Usage (Pointer)
0xA1, 0x00,        #   Collection (Physical)
0x09, 0x30,        #     Usage (X)
0x09, 0x31,        #     Usage (Y)
0x09, 0x33,        #     Usage (Rx)
0x09, 0x34,        #     Usage (Ry)
0x75, 0x08,        #     Report Size (8)
0x95, 0x04,        #     Report Count (4)
0x81, 0x02,        #     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0,              #   End Collection
0x15, 0x00,        #   Logical Minimum (0)
0x25, 0x7F,        #   Logical Maximum (127)
0x09, 0x01,        #   Usage (Pointer)
0xA1, 0x00,        #   Collection (Physical)
0x09, 0x32,        #     Usage (Z)
0x09, 0x35,        #     Usage (Rz)
0x75, 0x08,        #     Report Size (8)
0x95, 0x02,        #     Report Count (2)
0x81, 0x02,        #     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0,              #   End Collection
0xA1, 0x00,        #   Collection (Physical)
0x05, 0x09,        #     Usage Page (Button)
0x19, 0x01,        #     Usage Minimum (0x01)
0x29, 0x18,        #     Usage Maximum (0x18)
0x15, 0x00,        #     Logical Minimum (0)
0x25, 0x01,        #     Logical Maximum (1)
0x75, 0x01,        #     Report Size (1)
0x95, 0x18,        #     Report Count (24)
0x81, 0x02,        #     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0,              #   End Collection
0xC0,              # End Collection

Metadata

Release files for usb-gadget 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for usb-gadget 0.2
File Size Uploaded Provenance
usb_gadget-0.2.tar.gz 8.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for usb-gadget 0.2
File Interpreter ABI Platform
usb_gadget-0.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / usb_gadget-0.2.tar.gz

Download URL usb_gadget-0.2.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d97bb66148f4aef55c183ce09cc0397012d42ae57f7ba1e77ad4bb45b749c94f
BLAKE2b-256 checksum
How to use checksums
24c78fd92142dbec6b72c1af33370e91b58c7ab395902d61e059034a5b10e0bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.7

Release files / usb_gadget-0.2-py3-none-any.whl

Download URL usb_gadget-0.2-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c36ff3a04b30a946388907c626f36d888b9448640bc24fa774a31d3ecdf012b
BLAKE2b-256 checksum
How to use checksums
173755c0cd4cc0d6ff5365f211e0d538b64ab9b7fb85c16abb92251138d5e730
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.7

Security for usb-gadget 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page