blob: 08f9ad23bbcf7d015307b3f06d2bc82fef57b89a [file] [log] [blame]
Maksym Sloyko96342732021-08-03 21:54:28 +00001#!/bin/bash
2# Copyright 2021 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17# List of options the script accepts. Trailing column means that the option
18# requires an argument.
19ARGUMENT_LIST=(
20 "help"
21 "product-id:"
22 "product-name:"
23 "host-mac:"
24 "bind-device:"
25 "dev-mac:"
26 "gadget-dir-name:"
27 "iface-name:"
28)
29
30print_usage() {
31 cat <<HELP
32$0 [OPTIONS] [stop|start]
33 Create USB Gadget Configuration
34 --product-id USB Product Id for the gadget.
35 --product-name Product name string (en) for the gadget.
36 --host-mac MAC address of the host part of the connection. Optional.
37 --dev-mac MAC address of the device (gadget) part of the connection. Optional.
38 --bind-device Name of the device to bind, as listed in /sys/class/udc/
39 --gadget-dir-name Optional base name for gadget directory. Default: "g1"
40 --iface-name Optional name of the network interface. Default: "usb0"
41 --help Print this help and exit.
42HELP
43}
44
45gadget_start() {
46 local gadget_dir="${CONFIGFS_HOME}/usb_gadget/${GADGET_DIR_NAME}"
47 mkdir -p "${gadget_dir}"
48 echo ${ID_VENDOR} > "${gadget_dir}/idVendor"
49 echo ${ID_PRODUCT} > "${gadget_dir}/idProduct"
50
51 local str_en_dir="${gadget_dir}/strings/0x409"
52 mkdir -p "${str_en_dir}"
53 echo ${STR_EN_VENDOR} > "${str_en_dir}/manufacturer"
54 echo ${STR_EN_PRODUCT} > "${str_en_dir}/product"
55
56 local config_dir="${gadget_dir}/configs/c.1"
57 mkdir -p "${config_dir}"
58 echo 100 > "${config_dir}/MaxPower"
59 mkdir -p "${config_dir}/strings/0x409"
60 echo "ECM" > "${config_dir}/strings/0x409/configuration"
61
62 local func_dir="${gadget_dir}/functions/ecm.${IFACE_NAME}"
63 mkdir -p "${func_dir}"
64
65 if [[ -n $HOST_MAC_ADDR ]]; then
66 echo ${HOST_MAC_ADDR} > ${func_dir}/host_addr
67 fi
68
69 if [[ -n $DEV_MAC_ADDR ]]; then
70 echo ${DEV_MAC_ADDR} > ${func_dir}/dev_addr
71 fi
72
73 ln -s "${func_dir}" "${config_dir}"
74
75 echo "${BIND_DEVICE}" > ${gadget_dir}/UDC
76}
77
78gadget_stop() {
79 local gadget_dir="${CONFIGFS_HOME}/usb_gadget/${GADGET_DIR_NAME}"
80 rm -f ${gadget_dir}/configs/c.1/ecm.${IFACE_NAME}
81 rm -rf ${gadget_dir}/configs/c.1/strings/0x409
82 rm -rf ${gadget_dir}/configs/c.1
83 rm -rf ${gadget_dir}/strings/0x409
84 rm -rf ${gadget_dir}/functions/ecm.${IFACE_NAME}
85 rm -rf ${gadget_dir}
86}
87
88opts=$(getopt \
89 --longoptions "$(printf "%s," "${ARGUMENT_LIST[@]}")" \
90 --name "$(basename "$0")" \
91 --options "" \
92 -- "$@"
93)
94
95eval set --$opts
96
97CONFIGFS_HOME=${CONFIGFS_HOME:-/sys/kernel/config}
98ID_VENDOR="0x18d1" # Google
99ID_PRODUCT=""
100STR_EN_VENDOR="Google"
101STR_EN_PRODUCT=""
102DEV_MAC_ADDR=""
103HOST_MAC_ADDR=""
104BIND_DEVICE=""
105ACTION="start"
106GADGET_DIR_NAME="g1"
107IFACE_NAME="usb0"
108while [[ $# -gt 0 ]]; do
109 case "$1" in
110 --product-id)
111 ID_PRODUCT=$2
112 shift 2
113 ;;
114 --product-name)
115 STR_EN_PRODUCT=$2
116 shift 2
117 ;;
118 --host-mac)
119 HOST_MAC_ADDR=$2
120 shift 2
121 ;;
122 --dev-mac)
123 DEV_MAC_ADDR=$2
124 shift 2
125 ;;
126 --bind-device)
127 BIND_DEVICE=$2
128 shift 2
129 ;;
130 --gadget-dir-name)
131 GADGET_DIR_NAME=$2
132 shift 2
133 ;;
134 --iface-name)
135 IFACE_NAME=$2
136 shift 2
137 ;;
138 --help)
139 print_usage
140 exit 0
141 ;;
142 start)
143 ACTION="start"
144 shift 1
145 break
146 ;;
147 stop)
148 ACTION="stop"
149 shift 1
150 break
151 ;;
152 --)
153 shift 1
154 ;;
155 *)
156 break
157 ;;
158 esac
159done
160
161if [[ $ACTION == "stop" ]]; then
162 gadget_stop
163else
164 gadget_start
165fi