Skip to content

Commit

Permalink
Add support for mouse input
Browse files Browse the repository at this point in the history
Fixes #40
  • Loading branch information
mtlynch committed Aug 11, 2020
1 parent 3fe402f commit c8b6563
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions scripts/usb-gadget/init-usb-gadget
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Adapted from https://github.com/girst/hardpass-sendHID/blob/master/README.md
# Configures USB gadgets per: https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt

# Exit on first error.
set -e
Expand All @@ -10,6 +10,10 @@ set -u

modprobe libcomposite

### Configure keyboard

# Adapted from https://github.com/girst/hardpass-sendHID/blob/master/README.md

cd /sys/kernel/config/usb_gadget/
mkdir -p g1
cd g1
Expand All @@ -23,16 +27,26 @@ STRINGS_DIR="strings/0x409"
mkdir -p "$STRINGS_DIR"
echo "6b65796d696d6570690" > "${STRINGS_DIR}/serialnumber"
echo "tinypilot" > "${STRINGS_DIR}/manufacturer"
echo "Generic USB Keyboard" > "${STRINGS_DIR}/product"
echo "Multifunction USB Device" > "${STRINGS_DIR}/product"

FUNCTIONS_DIR="functions/hid.usb0"
mkdir -p "$FUNCTIONS_DIR"
echo 1 > "${FUNCTIONS_DIR}/protocol" # Keyboard
echo 1 > "${FUNCTIONS_DIR}/subclass" # Boot interface subclass
echo 8 > "${FUNCTIONS_DIR}/report_length"
# Keyboard
KEYBOARD_FUNCTIONS_DIR="functions/hid.keyboard"
mkdir -p "$KEYBOARD_FUNCTIONS_DIR"
echo 1 > "${KEYBOARD_FUNCTIONS_DIR}/protocol" # Keyboard
echo 1 > "${KEYBOARD_FUNCTIONS_DIR}/subclass" # Boot interface subclass
echo 8 > "${KEYBOARD_FUNCTIONS_DIR}/report_length"
# Write the report descriptor
# Source: https://www.kernel.org/doc/html/latest/usb/gadget_hid.html
echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > "${FUNCTIONS_DIR}/report_desc"
echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > "${KEYBOARD_FUNCTIONS_DIR}/report_desc"

# Mouse
MOUSE_FUNCTIONS_DIR="functions/hid.mouse"
mkdir -p "$MOUSE_FUNCTIONS_DIR"
echo 2 > "${MOUSE_FUNCTIONS_DIR}/protocol" # Mouse
echo 0 > "${MOUSE_FUNCTIONS_DIR}/subclass" # No subclass
echo 4 > "${MOUSE_FUNCTIONS_DIR}/report_length"
# Write the report descriptor
echo -ne \\x05\\x01\\x09\\x02\\xA1\\x01\\x09\\x01\\xA1\\x00\\x05\\x09\\x19\\x01\\x29\\x05\\x15\\x00\\x25\\x01\\x95\\x05\\x75\\x01\\x81\\x02\\x95\\x01\\x75\\x03\\x81\\x01\\x05\\x01\\x09\\x30\\x09\\x31\\x09\\x38\\x15\\x81\\x25\\x7F\\x75\\x08\\x95\\x03\\x81\\x6\\xC0\\xC0 > "${MOUSE_FUNCTIONS_DIR}/report_desc"

CONFIG_INDEX=1
CONFIGS_DIR="configs/c.${CONFIG_INDEX}"
Expand All @@ -43,7 +57,9 @@ CONFIGS_STRINGS_DIR="${CONFIGS_DIR}/strings/0x409"
mkdir -p "$CONFIGS_STRINGS_DIR"
echo "Config ${CONFIG_INDEX}: ECM network" > "${CONFIGS_STRINGS_DIR}/configuration"

ln -s "$FUNCTIONS_DIR" "${CONFIGS_DIR}/"
ln -s "$KEYBOARD_FUNCTIONS_DIR" "${CONFIGS_DIR}/"
ln -s "$MOUSE_FUNCTIONS_DIR" "${CONFIGS_DIR}/"
ls /sys/class/udc > UDC

chmod 777 /dev/hidg0
chmod 777 /dev/hidg0
chmod 777 /dev/hidg1

0 comments on commit c8b6563

Please sign in to comment.