#!/bin/sh

# USB Audio configuration:
CONFIGFS_HOME=/config
CAPTURE_CHANNEL_MASK=3
#CAPTURE_SAMPLE_RATES=44100,48000,96000,192000
#CAPTURE_SAMPLE_SIZE=2,3
CAPTURE_SAMPLE_RATES=96000
CAPTURE_SAMPLE_SIZE=2
#CAPTURE_SAMPLE_RATES=48000
#CAPTURE_SAMPLE_SIZE=4
PLAY_CHANNEL_MASK=3
PLAY_SAMPLE_RATES=96000
PLAY_SAMPLE_SIZE=2
#PLAY_SAMPLE_RATES=44100,48000,96000,192000
#PLAY_SAMPLE_SIZE=2,3
#PLAY_SAMPLE_RATES=48000
#PLAY_SAMPLE_SIZE=4

# Load libcomposite
# modprobe snd
# modprobe snd-timer
# modprobe snd-pcm
# modprobe snd-aloop
modprobe libcomposite
mount none $CONFIGFS_HOME -t configfs

# Create a gadget called AVDSP_Gadget
mkdir $CONFIGFS_HOME/usb_gadget/uacAVDSP
cd $CONFIGFS_HOME/usb_gadget/uacAVDSP

# Configure our gadget details
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0101 > idProduct # Linux-USB Audio Gadget or 0x0104 for Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
mkdir -p strings/0x409
echo "2351117mgt" > strings/0x409/serialnumber
echo "Mini Great Things" > strings/0x409/manufacturer
echo "uacAVDSP" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409

# UAC2 (audio) gadget
# attributes from: https://www.kernel.org/doc/Documentation/ABI/testing/configfs-usb-gadget-uac2
mkdir -p functions/uac2.usb0
echo $CAPTURE_CHANNEL_MASK > functions/uac2.usb0/c_chmask
echo $CAPTURE_SAMPLE_RATES > functions/uac2.usb0/c_srate
echo $CAPTURE_SAMPLE_SIZE > functions/uac2.usb0/c_ssize
echo $PLAY_CHANNEL_MASK > functions/uac2.usb0/p_chmask
echo $PLAY_SAMPLE_RATES > functions/uac2.usb0/p_srate
echo $PLAY_SAMPLE_SIZE > functions/uac2.usb0/p_ssize
ln -s functions/uac2.usb0 configs/c.1/

# End functions
ls /sys/class/udc > UDC