https://gist.github.com/kbingham/c39c4cc7c20882a104c08df5206e2f9f
[linux-usb-otg] / uvc-gadget.sh
1 #!/bin/sh
2
3 # https://gist.github.com/kbingham/c39c4cc7c20882a104c08df5206e2f9f
4
5 set -e
6 #set -x
7
8 CONFIGFS="/sys/kernel/config"
9 GADGET="$CONFIGFS/usb_gadget"
10 VID="0x0525"
11 PID="0xa4a2"
12 SERIAL="0123456789"
13 MANUF=$(hostname)
14 PRODUCT="UVC Gadget"
15
16 USBFILE=/root/usbstorage.img
17
18 BOARD=$(strings /proc/device-tree/model)
19
20 case $BOARD in
21         "Renesas Salvator-X board based on r8a7795 ES1.x")
22                 UDC_USB2=e6590000.usb
23                 UDC_USB3=ee020000.usb
24
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
28
29                 UDC=$UDC_USB2
30                 UDC_ROLE=$UDC_ROLE2
31                 ;;
32
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
36                 ;;
37
38         *)
39                 UDC=`ls /sys/class/udc` # will identify the 'first' UDC
40                 UDC_ROLE=/dev/null # Not generic
41                 ;;
42 esac
43
44 echo "Detecting platform:"
45 echo "  board : $BOARD"
46 echo "  udc   : $UDC"     
47
48 create_msd() {
49         # Example usage:
50         #       create_msd <target config> <function name> <image file>
51         #       create_msd configs/c.1 mass_storage.0 /root/backing.img
52         CONFIG=$1
53         FUNCTION=$2
54         BACKING_STORE=$3
55
56         if [ ! -f $BACKING_STORE ]
57         then
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
61                 echo "\tOK"
62         fi
63
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
70
71         ln -s functions/$FUNCTION configs/c.1
72
73         echo "\tOK"
74 }
75
76 delete_msd() {
77         # Example usage:
78         #       delete_msd <target config> <function name>
79         #       delete_msd config/c.1 uvc.0
80         CONFIG=$1
81         FUNCTION=$2
82
83         echo "Removing Mass Storage interface : $FUNCTION"
84         rm -f $CONFIG/$FUNCTION
85         rmdir functions/$FUNCTION
86         echo "OK"
87 }
88
89 create_uvc() {
90         # Example usage:
91         #       create_uvc <target config> <function name>
92         #       create_uvc config/c.1 uvc.0
93         CONFIG=$1
94         FUNCTION=$2
95
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
100 666666
101 1000000
102 5000000
103 EOF
104         mkdir functions/$FUNCTION/streaming/header/h
105         cd functions/$FUNCTION/streaming/header/h
106         ln -s ../../uncompressed/u
107         cd ../../class/fs
108         ln -s ../../header/h
109         cd ../../class/hs
110         ln -s ../../header/h
111         cd ../../../control
112         mkdir header/h
113         ln -s header/h class/fs
114         ln -s header/h class/ss
115         cd ../../../
116         echo 2048 > functions/$FUNCTION/streaming_maxpacket
117
118         ln -s functions/$FUNCTION configs/c.1
119 }
120
121 delete_uvc() {
122         # Example usage:
123         #       delete_uvc <target config> <function name>
124         #       delete_uvc config/c.1 uvc.0
125         CONFIG=$1
126         FUNCTION=$2
127
128         echo "  Deleting UVC gadget functionality : $FUNCTION"
129         rm $CONFIG/$FUNCTION
130
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
139 }
140
141 case "$1" in
142     start)
143         echo "Creating the USB gadget"
144         #echo "Loading composite module"
145         #modprobe libcomposite
146
147         echo "Creating gadget directory g1"
148         mkdir -p $GADGET/g1
149
150         cd $GADGET/g1
151         if [ $? -ne 0 ]; then
152             echo "Error creating usb gadget in configfs"
153             exit 1;
154         else
155             echo "OK"
156         fi
157
158         echo "Setting Vendor and Product ID's"
159         echo $VID > idVendor
160         echo $PID > idProduct
161         echo "OK"
162
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
168         echo "OK"
169
170         echo "Creating Config"
171         mkdir configs/c.1
172         mkdir configs/c.1/strings/0x409
173
174         echo "Creating functions..."
175         #create_msd configs/c.1 mass_storage.0 $USBFILE
176         create_uvc configs/c.1 uvc.0
177         echo "OK"
178
179
180         echo "Binding USB Device Controller"
181         echo $UDC > UDC
182         echo peripheral > $UDC_ROLE
183         cat $UDC_ROLE
184         echo "OK"
185         ;;
186
187     stop)
188         echo "Stopping the USB gadget"
189
190         set +e # Ignore all errors here on a best effort
191
192         cd $GADGET/g1
193
194         if [ $? -ne 0 ]; then
195             echo "Error: no configfs gadget found" 
196             exit 1;
197         fi
198
199         echo "Unbinding USB Device Controller"
200         grep $UDC UDC && echo "" > UDC
201         echo "OK"
202
203         delete_uvc configs/c.1 uvc.0
204         #delete_msd configs/c.1 mass_storage.0
205
206         echo "Clearing English strings"
207         rmdir strings/0x409
208         echo "OK"
209
210         echo "Cleaning up configuration"
211         rmdir configs/c.1/strings/0x409
212         rmdir configs/c.1
213         echo "OK"
214
215         echo "Removing gadget directory"
216         cd $GADGET
217         rmdir g1
218         cd /
219         echo "OK"
220
221         #echo "Disable composite USB gadgets"
222         #modprobe -r libcomposite
223         #echo "OK"
224         ;;
225     *)
226         echo "Usage : $0 {start|stop}"
227 esac