rpikvm/gadget_enable.sh

101 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
gname=rpikvm
gserial=$(awk '/Serial/ { print $3 }' /proc/cpuinfo)
gmanufacturer="Petrkr.NET"
gproduct="rPi KVM"
KEYBOARD=""
KEYBOARD="$KEYBOARD\\x05\\x01\\x09\\x06" # Usage generic desktop, Usage keyboard
KEYBOARD="$KEYBOARD\\xa1\\x01\\x05\\x07" # Collection App, Use page Keyboard
KEYBOARD="$KEYBOARD\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08" # Usage min (LeftControl) max (Right GUI), log min 0 log min 1, report size 1 count 8
KEYBOARD="$KEYBOARD\\x81\\x02" # Input data, variable, absolute
KEYBOARD="$KEYBOARD\\x75\\x08\\x95\\x01" # Report size 8, count 1
KEYBOARD="$KEYBOARD\\x81\\x03" # Input Const, variable, absolute
KEYBOARD="$KEYBOARD\\x75\\x01\\x95\\x05" # Report size 1 count 5
KEYBOARD="$KEYBOARD\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\xff\\x05\\x07\\x19\\x00\\x29\\x65"
KEYBOARD="$KEYBOARD\\x81\\x00" # Input data array absolute
KEYBOARD="$KEYBOARD\\xc0" # End Collection Application
#Mouse absolute descriptor
MOUSE0=""
MOUSE0="$MOUSE0\\x05\\x01\\x09\\x02" # Usage generic desktop, Usage mouse
MOUSE0="$MOUSE0\\xa1\\x01\\x09\\x01" # Collection App, Usage Pointer
MOUSE0="$MOUSE0\\xa1\\x00" # Collection Physical
MOUSE0="$MOUSE0\\x05\\x09" # Use Page buttons
MOUSE0="$MOUSE0\\x19\\x01\\x29\\x08\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08" # Usage min 1, max 8, logical min 0, max 1, report 8, size 1
MOUSE0="$MOUSE0\\x81\\x02" # Input data, variable, absolute
MOUSE0="$MOUSE0\\x05\\x01" # Usage page generic desktop
MOUSE0="$MOUSE0\\x09\\x30\\x35\\x00\\x46\\x80\\x07\\x15\\x00\\x26\\x80\\x07\\x75\\x10\\x95\\x01" # Usage X, Log min 0, log max 1920, size 16, report 1
MOUSE0="$MOUSE0\\x81\\x02" # Input data, variable, absolute
MOUSE0="$MOUSE0\\x05\\x01" # Usage page generic desktop
MOUSE0="$MOUSE0\\x09\\x31\\x35\\x00\\x46\\x38\\x04\\x15\\x00\\x26\\x38\\x04\\x75\\x10\\x95\\x01" # Usage Y, Log min 0, log max 1080, size 16, report 1
MOUSE0="$MOUSE0\\x81\\x02" # Input data, variable, absolute
MOUSE0="$MOUSE0\\x05\\x01" # Usage page generic desktop
MOUSE0="$MOUSE0\\x09\\x38\\x35\\x00\\x45\\x00\\x15\\x81\\x25\\x7f\\x75\\x08\\x95\\x01" # Usage Wheel, log min -127, max 127, size 8, report 1
MOUSE0="$MOUSE0\\x81\\x06" # Input data, variable, relative
MOUSE0="$MOUSE0\\xc0" # End collection Physical
MOUSE0="$MOUSE0\\xc0" # End collection Application
cd /sys/kernel/config/usb_gadget/
mkdir -p $gname
cd $gname
echo "Creating USB Device"
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
echo "Creating strings"
mkdir -p strings/0x409
echo "$gserial" > strings/0x409/serialnumber
echo "$gmanufacturer" > strings/0x409/manufacturer
echo "$gproduct" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config: KVM Composite" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower
echo "Creating usb devices"
# Gadget HID Keyboard
echo " HID Keyboard"
mkdir -p functions/hid.kbd0
echo 1 > functions/hid.kbd0/protocol
echo 1 > functions/hid.kbd0/subclass
echo 8 > functions/hid.kbd0/report_length
echo -ne $KEYBOARD > functions/hid.kbd0/report_desc
echo " HID Mouse"
mkdir -p functions/hid.mouse0
echo 2 > functions/hid.mouse0/protocol
echo 1 > functions/hid.mouse0/subclass
echo 8 > functions/hid.mouse0/report_length
echo -ne $MOUSE0 > functions/hid.mouse0/report_desc
echo " Mass storage"
mkdir -p functions/mass_storage.disk1
echo "RPi KVM DISK" > functions/mass_storage.disk1/lun.0/inquiry_string
echo " CD-ROM"
mkdir -p functions/mass_storage.cdrom1
echo 1 > functions/mass_storage.cdrom1/lun.0/cdrom
echo 1 > functions/mass_storage.cdrom1/lun.0/removable
echo "RPi KVM CDROM" > functions/mass_storage.cdrom1/lun.0/inquiry_string
echo "Activating gadgeds"
ln -s functions/hid.kbd0 configs/c.1/
ln -s functions/hid.mouse0 configs/c.1/
ln -s functions/mass_storage.disk1 configs/c.1/
ln -s functions/mass_storage.cdrom1 configs/c.1/
echo "Turn on gadget"
ls /sys/class/udc > UDC
echo "Change permissions"
chgrp input /dev/hidg0; chmod 660 /dev/hidg0
chgrp input /dev/hidg1; chmod 660 /dev/hidg1