blob: 8fc28cca77eb621e295ebad316465f747eab143b [file] [log] [blame]
Spencer Ku04d737c2020-04-20 15:24:35 +08001#!/bin/bash
2
George Hung1a9ed112020-07-13 14:12:03 +08003mac_config="/usr/share/mac-address/config.txt"
4dev_mac_path="/tmp/usb0_dev"
5host_mac_path="/tmp/usb0_host"
6
7check_usb_local_administered() {
8 is_enable="$(cat ${mac_config} | grep 'USBLAA')"
Patrick Williams8c226232023-04-15 20:05:21 -05009 echo "${is_enable}"
George Hung1a9ed112020-07-13 14:12:03 +080010}
11
12# Set the locally administered bit (the second least-significant
13# bit of the first octet) of the MAC address
14set_local_administered_bit() {
Patrick Williams8c226232023-04-15 20:05:21 -050015 mac="$(tr -d '\0' < "$1")"
George Hung1a9ed112020-07-13 14:12:03 +080016 first_byte="${mac:0:2}"
17 first_byte="$((0x$first_byte|2))"
18 first_byte="$(printf "%02x\n" "$first_byte")"
19 mac="${first_byte}${mac:2}"
Patrick Williams8c226232023-04-15 20:05:21 -050020 echo "$mac"
George Hung1a9ed112020-07-13 14:12:03 +080021}
22
Patrick Williams8c226232023-04-15 20:05:21 -050023cd /sys/kernel/config/usb_gadget || exit 1
Spencer Ku04d737c2020-04-20 15:24:35 +080024
25if [ ! -f "g1" ]; then
26 mkdir g1
Patrick Williams8c226232023-04-15 20:05:21 -050027 cd g1 || exit 1
Spencer Ku04d737c2020-04-20 15:24:35 +080028
29 echo 0x1d6b > idVendor # Linux foundation
30 echo 0x0104 > idProduct # Multifunction composite gadget
31 mkdir -p strings/0x409
32 echo "Linux" > strings/0x409/manufacturer
33 echo "Etherned/ECM gadget" > strings/0x409/product
34
35 mkdir -p configs/c.1
36 echo 100 > configs/c.1/MaxPower
37 mkdir -p configs/c.1/strings/0x409
38 echo "ECM" > configs/c.1/strings/0x409/configuration
39
George Hung1a9ed112020-07-13 14:12:03 +080040
41 if [[ $(check_usb_local_administered) == "USBLAA=true" ]]; then
42 dev_mac="$(set_local_administered_bit $dev_mac_path)"
43 host_mac="$(set_local_administered_bit $host_mac_path)"
Patrick Williams8c226232023-04-15 20:05:21 -050044 echo "$dev_mac" > $dev_mac_path
45 echo "$host_mac" > $host_mac_path
George Hung1a9ed112020-07-13 14:12:03 +080046 fi
47
Spencer Ku04d737c2020-04-20 15:24:35 +080048 mkdir -p functions/ecm.usb0
George Hung1a9ed112020-07-13 14:12:03 +080049 cat $dev_mac_path > functions/ecm.usb0/dev_addr # write device mac address
50 cat $host_mac_path > functions/ecm.usb0/host_addr # write usb mac address
Spencer Ku04d737c2020-04-20 15:24:35 +080051
52 ln -s functions/ecm.usb0 configs/c.1
53
54 echo "$UDC" > UDC
55
George Hung1a9ed112020-07-13 14:12:03 +080056 rm $dev_mac_path
57 rm $host_mac_path
Spencer Ku04d737c2020-04-20 15:24:35 +080058
59fi
60
61exit 0