3 # https://gist.github.com/kbingham/c39c4cc7c20882a104c08df5206e2f9f
8 CONFIGFS="/sys/kernel/config"
9 GADGET="$CONFIGFS/usb_gadget"
16 USBFILE=/root/usbstorage.img
18 BOARD=$(strings /proc/device-tree/model)
21 "Renesas Salvator-X board based on r8a7795 ES1.x")
25 UDC_ROLE2=/sys/devices/platform/soc/ee080200.usb-phy/role
26 UDC_ROLE2=/dev/null #Not needed - always peripheral
27 UDC_ROLE3=/sys/devices/platform/soc/ee020000.usb/role
33 "TI OMAP4 PandaBoard-ES")
34 UDC=`ls /sys/class/udc` # Should be musb-hdrc.0.auto
35 UDC_ROLE=/dev/null # Not needed - peripheral enabled
39 UDC=`ls /sys/class/udc` # will identify the 'first' UDC
40 UDC_ROLE=/dev/null # Not generic
44 echo "Detecting platform:"
45 echo " board : $BOARD"
50 # create_msd <target config> <function name> <image file>
51 # create_msd configs/c.1 mass_storage.0 /root/backing.img
56 if [ ! -f $BACKING_STORE ]
58 echo "\tCreating backing file"
59 dd if=/dev/zero of=$BACKING_STORE bs=1M count=32 > /dev/null 2>&1
60 mkfs.ext4 $USBFILE > /dev/null 2>&1
64 echo "\tCreating MSD gadget functionality"
65 mkdir functions/$FUNCTION
66 echo 1 > functions/$FUNCTION/stall
67 echo $BACKING_STORE > functions/$FUNCTION/lun.0/file
68 echo 1 > functions/$FUNCTION/lun.0/removable
69 echo 0 > functions/$FUNCTION/lun.0/cdrom
71 ln -s functions/$FUNCTION configs/c.1
78 # delete_msd <target config> <function name>
79 # delete_msd config/c.1 uvc.0
83 echo "Removing Mass Storage interface : $FUNCTION"
84 rm -f $CONFIG/$FUNCTION
85 rmdir functions/$FUNCTION
91 # create_uvc <target config> <function name>
92 # create_uvc config/c.1 uvc.0
96 echo " Creating UVC gadget functionality : $FUNCTION"
97 mkdir functions/$FUNCTION
98 mkdir -p functions/$FUNCTION/streaming/uncompressed/u/360p
99 cat <<EOF > functions/$FUNCTION/streaming/uncompressed/u/360p/dwFrameInterval
104 mkdir functions/$FUNCTION/streaming/header/h
105 cd functions/$FUNCTION/streaming/header/h
106 ln -s ../../uncompressed/u
113 ln -s header/h class/fs
114 ln -s header/h class/ss
116 echo 2048 > functions/$FUNCTION/streaming_maxpacket
118 ln -s functions/$FUNCTION configs/c.1
123 # delete_uvc <target config> <function name>
124 # delete_uvc config/c.1 uvc.0
128 echo " Deleting UVC gadget functionality : $FUNCTION"
131 rm functions/$FUNCTION/control/class/*/h
132 rm functions/$FUNCTION/streaming/class/*/h
133 rm functions/$FUNCTION/streaming/header/h/u
134 rmdir functions/$FUNCTION/streaming/uncompressed/u/360p
135 rmdir functions/$FUNCTION/streaming/uncompressed/u
136 rmdir functions/$FUNCTION/streaming/header/h
137 rmdir functions/$FUNCTION/control/header/h
138 rmdir functions/$FUNCTION
143 echo "Creating the USB gadget"
144 #echo "Loading composite module"
145 #modprobe libcomposite
147 echo "Creating gadget directory g1"
151 if [ $? -ne 0 ]; then
152 echo "Error creating usb gadget in configfs"
158 echo "Setting Vendor and Product ID's"
160 echo $PID > idProduct
163 echo "Setting English strings"
164 mkdir -p strings/0x409
165 echo $SERIAL > strings/0x409/serialnumber
166 echo $MANUF > strings/0x409/manufacturer
167 echo $PRODUCT > strings/0x409/product
170 echo "Creating Config"
172 mkdir configs/c.1/strings/0x409
174 echo "Creating functions..."
175 #create_msd configs/c.1 mass_storage.0 $USBFILE
176 create_uvc configs/c.1 uvc.0
180 echo "Binding USB Device Controller"
182 echo peripheral > $UDC_ROLE
188 echo "Stopping the USB gadget"
190 set +e # Ignore all errors here on a best effort
194 if [ $? -ne 0 ]; then
195 echo "Error: no configfs gadget found"
199 echo "Unbinding USB Device Controller"
200 grep $UDC UDC && echo "" > UDC
203 delete_uvc configs/c.1 uvc.0
204 #delete_msd configs/c.1 mass_storage.0
206 echo "Clearing English strings"
210 echo "Cleaning up configuration"
211 rmdir configs/c.1/strings/0x409
215 echo "Removing gadget directory"
221 #echo "Disable composite USB gadgets"
222 #modprobe -r libcomposite
226 echo "Usage : $0 {start|stop}"