Skip to content

Instantly share code, notes, and snippets.

@Sucareto
Last active May 26, 2024 11:10
Show Gist options
  • Save Sucareto/f8b9b1d0594dd87ba1318db549282b67 to your computer and use it in GitHub Desktop.
Save Sucareto/f8b9b1d0594dd87ba1318db549282b67 to your computer and use it in GitHub Desktop.
使用 USB Gadget 功能,创建一个“键盘+鼠标+存储”复合设备,设备 ID 来自 Dell KB216 键盘。可配合 https://github.com/Sucareto/Android_HID_Keyboard 使用。
#!/bin/sh
# https://github.com/Sucareto/Android_HID_Keyboard
device_path="/config/usb_gadget/KB216"
ms_file="/data/data/com.termux/files/home/storage/tmp/mass_storage.img"
# create mass_storage image file
# dd if=/dev/zero of=$ms_file bs=1024MiB count=1
# fallocate -l 1G $ms_file
# loop_device=$(losetup -f)
echo "Clean up old files..."
rm "$device_path/configs/c.1/hid.keyboard"
rm "$device_path/configs/c.1/hid.mouse"
rm "$device_path/configs/c.1/mass_storage.0"
rmdir "$device_path/configs/c.1/strings/0x409"
rmdir "$device_path/configs/c.1"
rmdir "$device_path/functions/hid.keyboard"
rmdir "$device_path/functions/hid.mouse"
rmdir "$device_path/functions/mass_storage.0"
rmdir "$device_path/strings/0x409"
rmdir "$device_path"
# Clean only
[ "$1" == "-d" ] && exit 0
echo "Create new files..."
mkdir -p $device_path && cd $device_path
if [ $? -eq 0 ]; then
echo 0x00 >bDeviceClass
echo 0x00 >bDeviceProtocol
echo 0x00 >bDeviceSubClass
echo 0x08 >bMaxPacketSize0
echo 0x0108 >bcdDevice
echo 0x0100 >bcdUSB
echo 0x2113 >idProduct
echo 0x413c >idVendor
else
echo "device create fail"
exit 1
fi
mkdir -p "$device_path/strings/0x409" && cd "$device_path/strings/0x409/"
if [ $? -eq 0 ]; then
echo "Dell Computer Corp." >manufacturer
echo "KB216 Wired Keyboard" >product
# echo "0" >serialnumber
else
echo "strings create fail"
exit 1
fi
# create keyboard function
mkdir -p "$device_path/functions/hid.keyboard" && cd "$device_path/functions/hid.keyboard"
if [ $? -eq 0 ]; then
echo 1 >protocol # Keyboard
echo 1 >subclass # Boot Interface Subclass
echo 8 >report_length
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" >report_desc
else
echo "keyboard functions create fail"
exit 1
fi
# create mouse function
mkdir -p "$device_path/functions/hid.mouse" && cd "$device_path/functions/hid.mouse"
if [ $? -eq 0 ]; then
echo 2 >protocol # Mouse
echo 1 >subclass # Boot Interface Subclass
echo 4 >report_length
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\\x06\\xc0\\xc0" >report_desc
else
echo "mouse functions create fail"
exit 1
fi
# create mass_storage function
if [ -n "$ms_file" ]; then
mkdir -p "$device_path/functions/mass_storage.0" && cd "$device_path/functions/mass_storage.0/lun.0"
if [ $? -eq 0 ]; then
echo 1 >../stall
echo 0 >cdrom
echo "Dell" >inquiry_string
echo 0 >removable
echo 0 >ro
echo 0 >nofua
echo "$ms_file" >file
else
echo "mass_storage functions create fail"
exit 1
fi
fi
# enable functions
mkdir -p "$device_path/configs/c.1/strings/0x409" && cd "$device_path/configs/c.1/"
if [ $? -eq 0 ]; then
echo 100 >MaxPower
echo "Sucareto Devices" >strings/0x409/configuration
ln -s "$device_path/functions/hid.keyboard" ./
ln -s "$device_path/functions/hid.mouse" ./
[ -n "$ms_file" ] && ln -s "$device_path/functions/mass_storage.0" ./
else
echo "functions enable fail"
exit 1
fi
cd $device_path
ls /sys/class/udc >UDC
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment