#!/bin/bash -e

#
# Initialises the configfs USB gadget subsystem.
#

modprobe libcomposite

cd /sys/kernel/config/usb_gadget/
mkdir g && cd g

echo 0x1781 > idVendor  # Linux Foundation
echo 0x0B99 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB    # USB 2.0

echo 0xEF > bDeviceClass
echo 0x02 > bDeviceSubClass
echo 0x01 > bDeviceProtocol

mkdir -p strings/0x409
echo "deadbeef00115600" > strings/0x409/serialnumber
echo "Keystone Elec. Solns." > strings/0x409/manufacturer
echo "Guinnux (Blue Penguin)"   > strings/0x409/product

mkdir -p functions/rndis.usb0  # network
mkdir -p functions/acm.usb0    # serial

mkdir -p configs/c.1
echo 2 > configs/c.1/MaxPower
ln -s functions/rndis.usb0 configs/c.1/
ln -s functions/acm.usb0   configs/c.1/

# OS descriptors. This helps Windows configure the RNDIS driver correctly.
echo 1       > os_desc/use
echo 0xcd    > os_desc/b_vendor_code
echo MSFT100 > os_desc/qw_sign

echo RNDIS   > functions/rndis.usb0/os_desc/interface.rndis/compatible_id
echo 5162001 > functions/rndis.usb0/os_desc/interface.rndis/sub_compatible_id

ln -s configs/c.1 os_desc

udevadm settle -t 5 || :

udc_classes=(`ls /sys/class/udc/`)
udc=${udc_classes[0]}
echo $udc > UDC
