mac80211: rename phy according to board.json entries on bringup
authorFelix Fietkau <nbd@nbd.name>
Fri, 30 Sep 2022 18:14:05 +0000 (20:14 +0200)
committerFelix Fietkau <nbd@nbd.name>
Fri, 14 Oct 2022 11:12:42 +0000 (13:12 +0200)
This allows phy names specified in board.json to be used directly instead of
the path option

Signed-off-by: Felix Fietkau <nbd@nbd.name>
package/kernel/mac80211/files/lib/netifd/wireless/mac80211.sh

index e05a5b8feeaba3e6ca84f683333996a1a00edc72..ed2805213e4a5e295decd998a2edae071d31219b 100644 (file)
@@ -581,15 +581,77 @@ mac80211_generate_mac() {
                $(( (0x$6 + $id) % 0x100 ))
 }
 
+get_board_phy_name() (
+       local path="$1"
+       local fallback_phy=""
+
+       __check_phy() {
+               local val="$1"
+               local key="$2"
+               local ref_path="$3"
+
+               json_select "$key"
+               json_get_values path
+               json_select ..
+
+               [ "${ref_path%+*}" = "$path" ] && fallback_phy=$key
+               [ "$ref_path" = "$path" ] || return 0
+
+               echo "$key"
+               exit
+       }
+
+       json_load_file /etc/board.json
+       json_for_each_item __check_phy wlan "$path"
+       [ -n "$fallback_phy" ] && echo "${fallback_phy}.${path##*+}"
+)
+
+rename_board_phy_by_path() {
+       local path="$1"
+
+       local new_phy="$(get_board_phy_name "$path")"
+       [ -z "$new_phy" -o "$new_phy" = "$phy" ] && return
+
+       iw "$phy" set name "$new_phy" && phy="$new_phy"
+}
+
+rename_board_phy_by_name() (
+       local phy="$1"
+       local suffix="${phy##*.}"
+       [ "$suffix" = "$phy" ] && suffix=
+
+       json_load_file /etc/board.json
+       json_select wlan
+       json_select "${phy%.*}" || return 0
+       json_get_values path
+
+       prev_phy="$(iwinfo nl80211 phyname "path=$path${suffix:++$suffix}")"
+       [ -n "$prev_phy" ] || return 0
+
+       [ "$prev_phy" = "$phy" ] && return 0
+
+       iw "$prev_phy" set name "$phy"
+)
+
 find_phy() {
-       [ -n "$phy" -a -d /sys/class/ieee80211/$phy ] && return 0
+       [ -n "$phy" ] && {
+               rename_board_phy_by_name "$phy"
+               [ -d /sys/class/ieee80211/$phy ] && return 0
+       }
        [ -n "$path" ] && {
                phy="$(iwinfo nl80211 phyname "path=$path")"
-               [ -n "$phy" ] && return 0
+               [ -n "$phy" ] && {
+                       rename_board_phy_by_path "$path"
+                       return 0
+               }
        }
        [ -n "$macaddr" ] && {
                for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
-                       grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && return 0
+                       grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && {
+                               path="$(iwinfo nl80211 path "$phy")"
+                               rename_board_phy_by_path "$path"
+                               return 0
+                       }
                done
        }
        return 1