-
Notifications
You must be signed in to change notification settings - Fork 12
/
gadget-setup.sh
49 lines (45 loc) · 3.8 KB
/
gadget-setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
# This script will setup a simple HID keyboard gadget via ConfigFS.
# In order to use it, you must have kernel >= 3.19 and configfs enabled
# when the kernel was compiled (it usually is).
# variables and strings
MANUFACTURER="Some Company" # manufacturer attribute
SERIAL="Frosted Flakes" # device serial number
IDPRODUCT="0xa4ac" # hex product ID, issued by USB Group
IDVENDOR="0x0525" # hex vendor ID, assigned by USB Group
PRODUCT="Emulated HID Keyboard" # cleartext product description
CONFIG_NAME="Configuration 1" # name of this configuration
MAX_POWER_MA=120 # max power this configuration can consume in mA
PROTOCOL=1 # 1 for keyboard. see usb spec
SUBCLASS=1 # it seems either 1 or 0 works, dunno why
REPORT_LENGTH=8 # number of bytes per report
DESCRIPTOR=/config/usb_gadget/keyboardgadget/kybd-descriptor.bin # binary blob of report descriptor, see HID class spec
UDC=ci_hdrc.0 # name of the UDC driver to use (found in /sys/class/udc/)
# gadget configuration
modprobe libcomposite # load configfs
mount -t configfs none /config || { mkdir /config && \
mount --rbind -t configfs /sys/kernel/config /config; } # mount configfs as type configfs to /config or from /sys/kernel/config (bind operation)
mkdir /config/usb_gadget/keyboardgadget # make a new gadget skeleton
cd /config/usb_gadget/keyboardgadget # cd to gadget dir
mkdir configs/c.1 # make the skeleton for a config for this gadget
mkdir functions/hid.usb0 # add hid function (will fail if kernel < 3.19, which hid was added in)
echo $PROTOCOL > functions/hid.usb0/protocol # set the HID protocol
echo $SUBCLASS > functions/hid.usb0/subclass # set the device subclass
echo $REPORT_LENGTH > functions/hid.usb0/report_length # set the byte length of HID reports
cat $DESCRIPTOR > functions/hid.usb0/report_desc # write the binary blob of the report descriptor to report_desc; see HID class spec
mkdir strings/0x409 # setup standard device attribute strings
mkdir configs/c.1/strings/0x409
echo $IDPRODUCT > idProduct
echo $IDVENDOR > idVendor
echo $SERIAL > strings/0x409/serialnumber
echo $MANUFACTURER > strings/0x409/manufacturer
echo $PRODUCT > strings/0x409/product
echo $CONFIG_NAME > configs/c.1/strings/0x409/configuration
echo $MAX_POWER_MA > configs/c.1/MaxPower
ln -s functions/hid.usb0 configs/c.1 # put the function into the configuration by creating a symlink
# binding
echo $UDC > UDC # bind gadget to UDC driver (brings gadget online). This will only
# succeed if there are no gadgets already bound to the driver. Do
# lsmod and if there's anything in there like g_*, you'll need to
# rmmod it before bringing this gadget online. Otherwise you'll get
# "device or resource busy."