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) { |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 82 | if (!phy) |
| 83 | continue; |
| 84 | |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 85 | let name = phy.wiphy_name; |
| 86 | let path = phy_path(name); |
| 87 | let info = { |
| 88 | antenna_rx: phy.wiphy_antenna_avail_rx, |
| 89 | antenna_tx: phy.wiphy_antenna_avail_tx, |
| 90 | bands: {}, |
| 91 | }; |
| 92 | |
| 93 | let bands = info.bands; |
| 94 | for (let band in phy.wiphy_bands) { |
| 95 | if (!band || !band.freqs) |
| 96 | continue; |
| 97 | let freq = band.freqs[0].freq; |
| 98 | let band_info = {}; |
| 99 | let band_name; |
| 100 | if (freq > 50000) |
| 101 | band_name = "60G"; |
| 102 | else if (freq > 5900) |
| 103 | band_name = "6G"; |
| 104 | else if (freq > 4000) |
| 105 | band_name = "5G"; |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame] | 106 | else if (freq > 2000) |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 107 | band_name = "2G"; |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame] | 108 | else |
| 109 | continue; |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 110 | bands[band_name] = band_info; |
| 111 | if (band.ht_capa > 0) |
| 112 | band_info.ht = true; |
| 113 | if (band.vht_capa > 0) |
| 114 | band_info.vht = true; |
| 115 | let he_phy_cap = 0; |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 116 | let eht_phy_cap = 0; |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 117 | |
| 118 | for (let ift in band.iftype_data) { |
| 119 | if (!ift.he_cap_phy) |
| 120 | continue; |
| 121 | |
| 122 | band_info.he = true; |
| 123 | he_phy_cap |= ift.he_cap_phy[0]; |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 124 | |
| 125 | /* if (!ift.eht_cap_phy) */ |
| 126 | /* continue; */ |
| 127 | |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 128 | /* TODO: EHT */ |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame] | 129 | /* FIXME: hardcode */ |
| 130 | band_info.eht = true; |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 131 | eht_phy_cap = 2; |
| 132 | /* eht_phy_cap |= ift.eht_cap_phy[0]; */ |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 133 | } |
| 134 | |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame] | 135 | if (band_name == "6G" && band_info.eht) |
| 136 | band_info.max_width = 320; |
| 137 | else if (band_name != "2G" && |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 138 | (he_phy_cap & 0x18) || ((band.vht_capa >> 2) & 0x3)) |
| 139 | band_info.max_width = 160; |
| 140 | else if (band_name != "2G" && |
| 141 | (he_phy_cap & 4) || band.vht_capa > 0) |
| 142 | band_info.max_width = 80; |
| 143 | else if ((band.ht_capa & 0x2) || (he_phy_cap & 0x2)) |
| 144 | band_info.max_width = 40; |
| 145 | else |
| 146 | band_info.max_width = 20; |
| 147 | |
| 148 | let modes = band_info.modes = [ "NOHT" ]; |
| 149 | if (band_info.ht) |
| 150 | push(modes, "HT20"); |
| 151 | if (band_info.vht) |
| 152 | push(modes, "VHT20"); |
| 153 | if (band_info.he) |
| 154 | push(modes, "HE20"); |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame] | 155 | if (band_info.eht) |
| 156 | push(modes, "EHT20"); |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 157 | if (band.ht_capa & 0x2) { |
| 158 | push(modes, "HT40"); |
| 159 | if (band_info.vht) |
| 160 | push(modes, "VHT40") |
| 161 | } |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 162 | if (he_phy_cap & 2) |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 163 | push(modes, "HE40"); |
| 164 | |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 165 | if (eht_phy_cap && he_phy_cap & 2) |
| 166 | push(modes, "EHT40"); |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame] | 167 | |
| 168 | for (let freq in band.freqs) { |
| 169 | if (freq.disabled) |
| 170 | continue; |
| 171 | let chan = freq_to_channel(freq.freq); |
| 172 | if (!chan) |
| 173 | continue; |
| 174 | band_info.default_channel = chan; |
| 175 | break; |
| 176 | } |
| 177 | |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 178 | if (band_name == "2G") |
| 179 | continue; |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 180 | |
| 181 | if (he_phy_cap & 4) |
| 182 | push(modes, "HE40"); |
| 183 | if (eht_phy_cap && he_phy_cap & 4) |
| 184 | push(modes, "EHT40"); |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 185 | if (band_info.vht) |
| 186 | push(modes, "VHT80"); |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 187 | if (he_phy_cap & 4) |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 188 | push(modes, "HE80"); |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 189 | if (eht_phy_cap && he_phy_cap & 4) |
| 190 | push(modes, "EHT80"); |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 191 | if ((band.vht_capa >> 2) & 0x3) |
| 192 | push(modes, "VHT160"); |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 193 | if (he_phy_cap & 0x18) |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 194 | push(modes, "HE160"); |
developer | d0c8945 | 2024-10-11 16:53:27 +0800 | [diff] [blame^] | 195 | if (eht_phy_cap && he_phy_cap & 0x18) |
| 196 | push(modes, "EHT160"); |
| 197 | |
| 198 | if (eht_phy_cap & 2) |
developer | 42c7a43 | 2024-07-12 14:39:29 +0800 | [diff] [blame] | 199 | push(modes, "EHT320"); |
developer | 753619c | 2024-02-22 13:42:45 +0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | let entry = wiphy_get_entry(name, path); |
| 203 | entry.info = info; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | cleanup(); |
| 208 | wiphy_detect(); |
| 209 | if (!is_equal(prev_board_data, board_data)) { |
| 210 | let new_file = board_file + ".new"; |
| 211 | unlink(new_file); |
| 212 | let f = open(new_file, "wx"); |
| 213 | if (!f) |
| 214 | exit(1); |
| 215 | f.write(sprintf("%.J\n", board_data)); |
| 216 | f.close(); |
| 217 | rename(new_file, board_file); |
| 218 | } |