nl80211: prefer non-supplicant-based devices
[project/iwinfo.git] / iwinfo_nl80211.c
index 916192f8d8147e330f75d19da400dda781d292b3..f598ef5688a757cec5858b162420fcebbcc7ddbd 100644 (file)
@@ -742,6 +742,20 @@ static int nl80211_channel2freq(int channel, const char *band, bool ax)
        return 0;
 }
 
+static uint8_t nl80211_freq2band(int freq)
+{
+       if (freq >= 2412 && freq <= 2484)
+               return IWINFO_BAND_24;
+       else if (freq >= 5160 && freq <= 5885)
+               return IWINFO_BAND_5;
+       else if (freq >= 5925 && freq <= 7125)
+               return IWINFO_BAND_6;
+       else if (freq >= 58320 && freq <= 69120)
+               return IWINFO_BAND_60;
+
+       return 0;
+}
+
 static int nl80211_phyname_cb(struct nl_msg *msg, void *arg)
 {
        char *buf = arg;
@@ -793,10 +807,9 @@ nla_put_failure:
 
 static char * nl80211_phy2ifname(const char *ifname)
 {
-       int ifidx = -1, cifidx = -1, phyidx = -1;
+       int ifidx = -1, cifidx, lmode = 1, clmode, phyidx;
        char buffer[512];
        static char nif[IFNAMSIZ] = { 0 };
-
        DIR *d;
        struct dirent *e;
 
@@ -812,31 +825,34 @@ static char * nl80211_phy2ifname(const char *ifname)
 
        memset(nif, 0, sizeof(nif));
 
-       if (phyidx > -1)
+       snprintf(buffer, sizeof(buffer),
+                "/sys/class/ieee80211/phy%d/device/net", phyidx);
+
+       if ((d = opendir(buffer)) != NULL)
        {
-               if ((d = opendir("/sys/class/net")) != NULL)
+               while ((e = readdir(d)) != NULL)
                {
-                       while ((e = readdir(d)) != NULL)
-                       {
-                               snprintf(buffer, sizeof(buffer),
-                                        "/sys/class/net/%s/phy80211/index", e->d_name);
+                       snprintf(buffer, sizeof(buffer),
+                                "/sys/class/net/%s/ifindex", e->d_name);
+                       cifidx = nl80211_readint(buffer);
 
-                               if (nl80211_readint(buffer) == phyidx)
-                               {
-                                       snprintf(buffer, sizeof(buffer),
-                                                "/sys/class/net/%s/ifindex", e->d_name);
+                       if (cifidx < 0)
+                               continue;
 
-                                       if ((cifidx = nl80211_readint(buffer)) >= 0 &&
-                                           ((ifidx < 0) || (cifidx < ifidx)))
-                                       {
-                                               ifidx = cifidx;
-                                               strncpy(nif, e->d_name, sizeof(nif) - 1);
-                                       }
-                               }
-                       }
+                       snprintf(buffer, sizeof(buffer),
+                                "/sys/class/net/%s/link_mode", e->d_name);
+                       clmode = nl80211_readint(buffer);
 
-                       closedir(d);
+                       /* prefer non-supplicant-based devices */
+                       if ((ifidx < 0) || (cifidx < ifidx) || ((lmode == 1) && (clmode != 1)))
+                       {
+                               ifidx = cifidx;
+                               lmode = clmode;
+                               strncpy(nif, e->d_name, sizeof(nif) - 1);
+                       }
                }
+
+               closedir(d);
        }
 
        return nif[0] ? nif : NULL;
@@ -2619,8 +2635,11 @@ static int nl80211_get_scanlist_cb(struct nl_msg *msg, void *arg)
                sl->e->crypto.enabled = 1;
 
        if (bss[NL80211_BSS_FREQUENCY])
-               sl->e->channel = nl80211_freq2channel(nla_get_u32(
-                       bss[NL80211_BSS_FREQUENCY]));
+       {
+               sl->e->mhz = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
+               sl->e->band = nl80211_freq2band(sl->e->mhz);
+               sl->e->channel = nl80211_freq2channel(sl->e->mhz);
+       }
 
        if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
                nl80211_get_scanlist_ie(bss, sl->e);
@@ -2842,7 +2861,9 @@ static int nl80211_get_scanlist_wpactl(const char *ifname, char *buf, int *len)
                                e->mode = IWINFO_OPMODE_MASTER;
 
                        /* Channel */
-                       e->channel = nl80211_freq2channel(atoi(freq));
+                       e->mhz = atoi(freq);
+                       e->band = nl80211_freq2band(e->mhz);
+                       e->channel = nl80211_freq2channel(e->mhz);
 
                        /* Signal */
                        rssi = atoi(signal);
@@ -3009,14 +3030,10 @@ static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
                                            freqs[NL80211_FREQUENCY_ATTR_DISABLED])
                                                continue;
 
+                                       e->band = nl80211_get_band(band->nla_type);
                                        e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
                                        e->channel = nl80211_freq2channel(e->mhz);
 
-                                       e->restricted = (
-                                               freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
-                                               !freqs[NL80211_FREQUENCY_ATTR_RADAR]
-                                       ) ? 1 : 0;
-
                                        if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
                                                e->flags |= IWINFO_FREQ_NO_HT40MINUS;
                                        if (freqs[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
@@ -3031,6 +3048,14 @@ static int nl80211_get_freqlist_cb(struct nl_msg *msg, void *arg)
                                                e->flags |= IWINFO_FREQ_NO_10MHZ;
                                        if (freqs[NL80211_FREQUENCY_ATTR_NO_HE])
                                                e->flags |= IWINFO_FREQ_NO_HE;
+                                       if (freqs[NL80211_FREQUENCY_ATTR_NO_IR] &&
+                                           !freqs[NL80211_FREQUENCY_ATTR_RADAR])
+                                               e->flags |= IWINFO_FREQ_NO_IR;
+                                       if (freqs[NL80211_FREQUENCY_ATTR_INDOOR_ONLY])
+                                               e->flags |= IWINFO_FREQ_INDOOR_ONLY;
+
+                                       /* keep backwards compatibility */
+                                       e->restricted = (e->flags & IWINFO_FREQ_NO_IR) ? 1 : 0;
 
                                        e++;
                                        arr->count++;
@@ -3445,7 +3470,7 @@ static int nl80211_get_mbssid_support(const char *ifname, int *buf)
 
 static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const char *ifname)
 {
-       char *phy, compat[64], path[PATH_MAX];
+       char *phy, path[PATH_MAX];
 
        /* Try to determine the phy name from the given interface */
        phy = nl80211_ifname2phy(ifname);
@@ -3453,62 +3478,10 @@ static int nl80211_hardware_id_from_fdt(struct iwinfo_hardware_id *id, const cha
        snprintf(path, sizeof(path), "/sys/class/%s/%s/device/of_node/compatible",
                 phy ? "ieee80211" : "net", phy ? phy : ifname);
 
-       if (nl80211_readstr(path, compat, sizeof(compat)) <= 0)
+       if (nl80211_readstr(path, id->compatible, sizeof(id->compatible)) <= 0)
                return -1;
 
-       if (!strcmp(compat, "qca,ar9130-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0029;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9130;
-       } else if (!strcmp(compat, "qca,ar9330-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0030;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9330;
-       } else if (!strcmp(compat, "qca,ar9340-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0030;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9340;
-       } else if (!strcmp(compat, "qca,qca9530-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0033;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9530;
-       } else if (!strcmp(compat, "qca,qca9550-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0033;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9550;
-       } else if (!strcmp(compat, "qca,qca9560-wmac")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x0033;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x9560;
-       } else if (!strcmp(compat, "qcom,ipq4019-wifi")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x003c;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x4019;
-       } else if (!strcmp(compat, "qcom,ipq8074-wifi")) {
-               id->vendor_id = 0x168c;
-               id->device_id = 0x8074;
-               id->subsystem_vendor_id = 0x168c;
-               id->subsystem_device_id = 0x8074;
-       } else if (!strcmp(compat, "mediatek,mt7622-wmac")) {
-               id->vendor_id = 0x14c3;
-               id->device_id = 0x7622;
-               id->subsystem_vendor_id = 0x14c3;
-               id->subsystem_device_id = 0x7622;
-       } else if (!strcmp(compat, "mediatek,mt7986-wmac")) {
-               id->vendor_id = 0x14c3;
-               id->device_id = 0x7986;
-               id->subsystem_vendor_id = 0x14c3;
-               id->subsystem_device_id = 0x7986;
-       }
-
-       return (id->vendor_id && id->device_id) ? 0 : -1;
+       return 0;
 }
 
 
@@ -3542,16 +3515,13 @@ static int nl80211_get_hardware_id(const char *ifname, char *buf)
                        *lookup[i].dest = strtoul(num, NULL, 16);
        }
 
-       /* Failed to obtain hardware IDs, try FDT */
-       if (id->vendor_id == 0 && id->device_id == 0 &&
-           id->subsystem_vendor_id == 0 && id->subsystem_device_id == 0)
-               if (!nl80211_hardware_id_from_fdt(id, ifname))
-                       return 0;
-
-       /* Failed to obtain hardware IDs, search board config */
+       /* Failed to obtain hardware PCI/USB IDs... */
        if (id->vendor_id == 0 && id->device_id == 0 &&
            id->subsystem_vendor_id == 0 && id->subsystem_device_id == 0)
-               return iwinfo_hardware_id_from_mtd(id);
+               /* ... first fallback to FDT ... */
+               if (nl80211_hardware_id_from_fdt(id, ifname) == -1)
+                       /* ... then board config */
+                       return iwinfo_hardware_id_from_mtd(id);
 
        return 0;
 }