3 # https://gist.github.com/kbingham/c39c4cc7c20882a104c08df5206e2f9f
4 # needs user-space part https://github.com/wlhe/uvc-gadget
6 if [ ! -d uvc-gadget ] ; then
7 git clone https://github.com/wlhe/uvc-gadget
14 cd /sys/kernel/config/usb_gadget/ || modprobe libcomposite
19 CONFIGFS="/sys/kernel/config"
20 GADGET="$CONFIGFS/usb_gadget"
27 USBFILE=/root/usbstorage.img
29 BOARD=$(strings /proc/device-tree/model)
32 "Renesas Salvator-X board based on r8a7795 ES1.x")
36 UDC_ROLE2=/sys/devices/platform/soc/ee080200.usb-phy/role
37 UDC_ROLE2=/dev/null #Not needed - always peripheral
38 UDC_ROLE3=/sys/devices/platform/soc/ee020000.usb/role
44 "TI OMAP4 PandaBoard-ES")
45 UDC=`ls /sys/class/udc` # Should be musb-hdrc.0.auto
46 UDC_ROLE=/dev/null # Not needed - peripheral enabled
50 UDC=`ls /sys/class/udc` # will identify the 'first' UDC
51 UDC_ROLE=/dev/null # Not generic
55 echo "Detecting platform:"
56 echo " board : $BOARD"
61 # create_msd <target config> <function name> <image file>
62 # create_msd configs/c.1 mass_storage.0 /root/backing.img
67 if [ ! -f $BACKING_STORE ]
69 echo "\tCreating backing file"
70 dd if=/dev/zero of=$BACKING_STORE bs=1M count=32 > /dev/null 2>&1
71 mkfs.ext4 $USBFILE > /dev/null 2>&1
75 echo "\tCreating MSD gadget functionality"
76 mkdir functions/$FUNCTION
77 echo 1 > functions/$FUNCTION/stall
78 echo $BACKING_STORE > functions/$FUNCTION/lun.0/file
79 echo 1 > functions/$FUNCTION/lun.0/removable
80 echo 0 > functions/$FUNCTION/lun.0/cdrom
82 ln -s functions/$FUNCTION configs/c.1
89 # delete_msd <target config> <function name>
90 # delete_msd config/c.1 uvc.0
94 echo "Removing Mass Storage interface : $FUNCTION"
95 rm -f $CONFIG/$FUNCTION
96 rmdir functions/$FUNCTION
102 # create_uvc <target config> <function name>
103 # create_uvc config/c.1 uvc.0
107 echo " Creating UVC gadget functionality : $FUNCTION"
108 mkdir functions/$FUNCTION
109 mkdir -p functions/$FUNCTION/streaming/uncompressed/u/360p
110 cat <<EOF > functions/$FUNCTION/streaming/uncompressed/u/360p/dwFrameInterval
115 mkdir functions/$FUNCTION/streaming/header/h
116 cd functions/$FUNCTION/streaming/header/h
117 ln -s ../../uncompressed/u
124 ln -s header/h class/fs
125 ln -s header/h class/ss
127 echo 2048 > functions/$FUNCTION/streaming_maxpacket
129 ln -s functions/$FUNCTION configs/c.1
134 # delete_uvc <target config> <function name>
135 # delete_uvc config/c.1 uvc.0
139 echo " Deleting UVC gadget functionality : $FUNCTION"
142 rm functions/$FUNCTION/control/class/*/h
143 rm functions/$FUNCTION/streaming/class/*/h
144 rm functions/$FUNCTION/streaming/header/h/u
145 rmdir functions/$FUNCTION/streaming/uncompressed/u/360p
146 rmdir functions/$FUNCTION/streaming/uncompressed/u
147 rmdir functions/$FUNCTION/streaming/header/h
148 rmdir functions/$FUNCTION/control/header/h
149 rmdir functions/$FUNCTION
154 echo "Creating the USB gadget"
155 #echo "Loading composite module"
156 #modprobe libcomposite
158 echo "Creating gadget directory g1"
162 if [ $? -ne 0 ]; then
163 echo "Error creating usb gadget in configfs"
169 echo "Setting Vendor and Product ID's"
171 echo $PID > idProduct
174 echo "Setting English strings"
175 mkdir -p strings/0x409
176 echo $SERIAL > strings/0x409/serialnumber
177 echo $MANUF > strings/0x409/manufacturer
178 echo $PRODUCT > strings/0x409/product
181 echo "Creating Config"
183 mkdir configs/c.1/strings/0x409
185 echo "Creating functions..."
186 #create_msd configs/c.1 mass_storage.0 $USBFILE
187 create_uvc configs/c.1 uvc.0
191 echo "Binding USB Device Controller"
193 echo peripheral > $UDC_ROLE
199 echo "Stopping the USB gadget"
201 set +e # Ignore all errors here on a best effort
205 if [ $? -ne 0 ]; then
206 echo "Error: no configfs gadget found"
210 echo "Unbinding USB Device Controller"
211 grep $UDC UDC && echo "" > UDC
214 delete_uvc configs/c.1 uvc.0
215 #delete_msd configs/c.1 mass_storage.0
217 echo "Clearing English strings"
221 echo "Cleaning up configuration"
222 rmdir configs/c.1/strings/0x409
226 echo "Removing gadget directory"
232 #echo "Disable composite USB gadgets"
233 #modprobe -r libcomposite
237 echo "Usage : $0 {start|stop}"