summaryrefslogtreecommitdiff
path: root/meta-quanta/meta-common/recipes-quanta/network/usb-network/usb-network.sh
blob: 8fc28cca77eb621e295ebad316465f747eab143b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash

mac_config="/usr/share/mac-address/config.txt"
dev_mac_path="/tmp/usb0_dev"
host_mac_path="/tmp/usb0_host"

check_usb_local_administered() {
    is_enable="$(cat ${mac_config} | grep 'USBLAA')"
    echo "${is_enable}"
}

# Set the locally administered bit (the second least-significant
# bit of the first octet) of the MAC address
set_local_administered_bit() {
    mac="$(tr -d '\0' < "$1")"
    first_byte="${mac:0:2}"
    first_byte="$((0x$first_byte|2))"
    first_byte="$(printf "%02x\n" "$first_byte")"
    mac="${first_byte}${mac:2}"
    echo "$mac"
}

cd /sys/kernel/config/usb_gadget || exit 1

if [ ! -f "g1" ]; then
    mkdir g1
    cd g1 || exit 1

    echo 0x1d6b > idVendor  # Linux foundation
    echo 0x0104 > idProduct # Multifunction composite gadget
    mkdir -p strings/0x409
    echo "Linux" > strings/0x409/manufacturer
    echo "Etherned/ECM gadget" > strings/0x409/product

    mkdir -p configs/c.1
    echo 100 > configs/c.1/MaxPower
    mkdir -p configs/c.1/strings/0x409
    echo "ECM" > configs/c.1/strings/0x409/configuration


    if [[ $(check_usb_local_administered) == "USBLAA=true" ]]; then
        dev_mac="$(set_local_administered_bit $dev_mac_path)"
        host_mac="$(set_local_administered_bit $host_mac_path)"
        echo "$dev_mac" > $dev_mac_path
        echo "$host_mac" > $host_mac_path
    fi

    mkdir -p functions/ecm.usb0
    cat $dev_mac_path > functions/ecm.usb0/dev_addr # write device mac address
    cat $host_mac_path > functions/ecm.usb0/host_addr # write usb mac address

    ln -s functions/ecm.usb0 configs/c.1

    echo "$UDC" > UDC

    rm $dev_mac_path
    rm $host_mac_path

fi

exit 0