developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 1 | #!/usr/bin/env ucode |
| 2 | 'use strict'; |
| 3 | import { readfile, writefile, realpath, glob, basename, unlink, open, rename } from "fs"; |
| 4 | import { is_equal } from "/usr/share/hostap/common.uc"; |
| 5 | let nl = require("nl80211"); |
| 6 | |
| 7 | let board_file = "/etc/board.json"; |
| 8 | let prev_board_data = json(readfile(board_file)); |
| 9 | let board_data = json(readfile(board_file)); |
| 10 | |
| 11 | function phy_idx(name) { |
| 12 | return +rtrim(readfile(`/sys/class/ieee80211/${name}/index`)); |
| 13 | } |
| 14 | |
| 15 | function phy_path(name) { |
| 16 | let devpath = realpath(`/sys/class/ieee80211/${name}/device`); |
| 17 | |
| 18 | devpath = replace(devpath, /^\/sys\/devices\//, ""); |
| 19 | if (match(devpath, /^platform\/.*\/pci/)) |
| 20 | devpath = replace(devpath, /^platform\//, ""); |
| 21 | let dev_phys = map(glob(`/sys/class/ieee80211/${name}/device/ieee80211/*`), basename); |
| 22 | sort(dev_phys, (a, b) => phy_idx(a) - phy_idx(b)); |
| 23 | |
| 24 | let ofs = index(dev_phys, name); |
| 25 | if (ofs > 0) |
| 26 | devpath += `+${ofs}`; |
| 27 | |
| 28 | return devpath; |
| 29 | } |
| 30 | |
| 31 | function cleanup() { |
| 32 | let wlan = board_data.wlan; |
| 33 | |
| 34 | for (let name in wlan) |
| 35 | if (substr(name, 0, 3) == "phy") |
| 36 | delete wlan[name]; |
| 37 | else |
| 38 | delete wlan[name].info; |
| 39 | } |
| 40 | |
| 41 | function wiphy_get_entry(phy, path) { |
| 42 | board_data.wlan ??= {}; |
| 43 | |
| 44 | let wlan = board_data.wlan; |
| 45 | for (let name in wlan) |
| 46 | if (wlan[name].path == path) |
| 47 | return wlan[name]; |
| 48 | |
| 49 | wlan[phy] = { |
| 50 | path: path |
| 51 | }; |
| 52 | |
| 53 | return wlan[phy]; |
| 54 | } |
| 55 | |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 56 | function freq_to_channel(freq) { |
| 57 | if (freq < 1000) |
| 58 | return 0; |
| 59 | if (freq == 2484) |
| 60 | return 14; |
| 61 | if (freq == 5935) |
| 62 | return 2; |
| 63 | if (freq < 2484) |
| 64 | return (freq - 2407) / 5; |
| 65 | if (freq >= 4910 && freq <= 4980) |
| 66 | return (freq - 4000) / 5; |
| 67 | if (freq < 5950) |
| 68 | return (freq - 5000) / 5; |
| 69 | if (freq <= 45000) |
| 70 | return (freq - 5950) / 5; |
| 71 | if (freq >= 58320 && freq <= 70200) |
| 72 | return (freq - 56160) / 2160; |
| 73 | return 0; |
| 74 | } |
| 75 | |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 76 | function wiphy_detect() { |
| 77 | let phys = nl.request(nl.const.NL80211_CMD_GET_WIPHY, nl.const.NLM_F_DUMP, { split_wiphy_dump: true }); |
| 78 | if (!phys) |
| 79 | return; |
| 80 | |
| 81 | for (let phy in phys) { |
| 82 | let name = phy.wiphy_name; |
| 83 | let path = phy_path(name); |
| 84 | let info = { |
| 85 | antenna_rx: phy.wiphy_antenna_avail_rx, |
| 86 | antenna_tx: phy.wiphy_antenna_avail_tx, |
| 87 | bands: {}, |
| 88 | }; |
| 89 | |
| 90 | let bands = info.bands; |
| 91 | for (let band in phy.wiphy_bands) { |
| 92 | if (!band || !band.freqs) |
| 93 | continue; |
| 94 | let freq = band.freqs[0].freq; |
| 95 | let band_info = {}; |
| 96 | let band_name; |
| 97 | if (freq > 50000) |
| 98 | band_name = "60G"; |
| 99 | else if (freq > 5900) |
| 100 | band_name = "6G"; |
| 101 | else if (freq > 4000) |
| 102 | band_name = "5G"; |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 103 | else if (freq > 2000) |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 104 | band_name = "2G"; |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 105 | else |
| 106 | continue; |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 107 | bands[band_name] = band_info; |
| 108 | if (band.ht_capa > 0) |
| 109 | band_info.ht = true; |
| 110 | if (band.vht_capa > 0) |
| 111 | band_info.vht = true; |
| 112 | let he_phy_cap = 0; |
| 113 | |
| 114 | for (let ift in band.iftype_data) { |
| 115 | if (!ift.he_cap_phy) |
| 116 | continue; |
| 117 | |
| 118 | band_info.he = true; |
| 119 | he_phy_cap |= ift.he_cap_phy[0]; |
| 120 | /* TODO: EHT */ |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 121 | /* FIXME: hardcode */ |
| 122 | band_info.eht = true; |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 123 | } |
| 124 | |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 125 | if (band_name == "6G" && band_info.eht) |
| 126 | band_info.max_width = 320; |
| 127 | else if (band_name != "2G" && |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 128 | (he_phy_cap & 0x18) || ((band.vht_capa >> 2) & 0x3)) |
| 129 | band_info.max_width = 160; |
| 130 | else if (band_name != "2G" && |
| 131 | (he_phy_cap & 4) || band.vht_capa > 0) |
| 132 | band_info.max_width = 80; |
| 133 | else if ((band.ht_capa & 0x2) || (he_phy_cap & 0x2)) |
| 134 | band_info.max_width = 40; |
| 135 | else |
| 136 | band_info.max_width = 20; |
| 137 | |
| 138 | let modes = band_info.modes = [ "NOHT" ]; |
| 139 | if (band_info.ht) |
| 140 | push(modes, "HT20"); |
| 141 | if (band_info.vht) |
| 142 | push(modes, "VHT20"); |
| 143 | if (band_info.he) |
| 144 | push(modes, "HE20"); |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 145 | if (band_info.eht) |
| 146 | push(modes, "EHT20"); |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 147 | if (band.ht_capa & 0x2) { |
| 148 | push(modes, "HT40"); |
| 149 | if (band_info.vht) |
| 150 | push(modes, "VHT40") |
| 151 | } |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 152 | if (he_phy_cap & 0x2) { |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 153 | push(modes, "HE40"); |
| 154 | |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 155 | if (band_info.eht) |
| 156 | push(modes, "EHT40"); |
| 157 | } |
| 158 | |
| 159 | for (let freq in band.freqs) { |
| 160 | if (freq.disabled) |
| 161 | continue; |
| 162 | let chan = freq_to_channel(freq.freq); |
| 163 | if (!chan) |
| 164 | continue; |
| 165 | band_info.default_channel = chan; |
| 166 | break; |
| 167 | } |
| 168 | |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 169 | if (band_name == "2G") |
| 170 | continue; |
| 171 | if (band_info.vht) |
| 172 | push(modes, "VHT80"); |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 173 | if (he_phy_cap & 4) { |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 174 | push(modes, "HE80"); |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 175 | if (band_info.eht) |
| 176 | push(modes, "EHT80"); |
| 177 | } |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 178 | if ((band.vht_capa >> 2) & 0x3) |
| 179 | push(modes, "VHT160"); |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 180 | if (he_phy_cap & 0x18) { |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 181 | push(modes, "HE160"); |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame^] | 182 | if (band_info.eht) |
| 183 | push(modes, "EHT160"); |
| 184 | } |
| 185 | if (band_name == "6G" && band_info.eht) |
| 186 | push(modes, "EHT320"); |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | let entry = wiphy_get_entry(name, path); |
| 190 | entry.info = info; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | cleanup(); |
| 195 | wiphy_detect(); |
| 196 | if (!is_equal(prev_board_data, board_data)) { |
| 197 | let new_file = board_file + ".new"; |
| 198 | unlink(new_file); |
| 199 | let f = open(new_file, "wx"); |
| 200 | if (!f) |
| 201 | exit(1); |
| 202 | f.write(sprintf("%.J\n", board_data)); |
| 203 | f.close(); |
| 204 | rename(new_file, board_file); |
| 205 | } |