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