treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / wireless / mediatek / mt76 / util.c
blob23d1e1da78b209a5d6562350bc83ae58318654ee
1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 */
6 #include <linux/module.h>
7 #include "mt76.h"
9 bool __mt76_poll(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
10 int timeout)
12 u32 cur;
14 timeout /= 10;
15 do {
16 cur = dev->bus->rr(dev, offset) & mask;
17 if (cur == val)
18 return true;
20 udelay(10);
21 } while (timeout-- > 0);
23 return false;
25 EXPORT_SYMBOL_GPL(__mt76_poll);
27 bool __mt76_poll_msec(struct mt76_dev *dev, u32 offset, u32 mask, u32 val,
28 int timeout)
30 u32 cur;
32 timeout /= 10;
33 do {
34 cur = dev->bus->rr(dev, offset) & mask;
35 if (cur == val)
36 return true;
38 usleep_range(10000, 20000);
39 } while (timeout-- > 0);
41 return false;
43 EXPORT_SYMBOL_GPL(__mt76_poll_msec);
45 int mt76_wcid_alloc(unsigned long *mask, int size)
47 int i, idx = 0, cur;
49 for (i = 0; i < size / BITS_PER_LONG; i++) {
50 idx = ffs(~mask[i]);
51 if (!idx)
52 continue;
54 idx--;
55 cur = i * BITS_PER_LONG + idx;
56 if (cur >= size)
57 break;
59 mask[i] |= BIT(idx);
60 return cur;
63 return -1;
65 EXPORT_SYMBOL_GPL(mt76_wcid_alloc);
67 int mt76_get_min_avg_rssi(struct mt76_dev *dev)
69 struct mt76_wcid *wcid;
70 int i, j, min_rssi = 0;
71 s8 cur_rssi;
73 local_bh_disable();
74 rcu_read_lock();
76 for (i = 0; i < ARRAY_SIZE(dev->wcid_mask); i++) {
77 unsigned long mask = dev->wcid_mask[i];
79 if (!mask)
80 continue;
82 for (j = i * BITS_PER_LONG; mask; j++, mask >>= 1) {
83 if (!(mask & 1))
84 continue;
86 wcid = rcu_dereference(dev->wcid[j]);
87 if (!wcid)
88 continue;
90 spin_lock(&dev->rx_lock);
91 if (wcid->inactive_count++ < 5)
92 cur_rssi = -ewma_signal_read(&wcid->rssi);
93 else
94 cur_rssi = 0;
95 spin_unlock(&dev->rx_lock);
97 if (cur_rssi < min_rssi)
98 min_rssi = cur_rssi;
102 rcu_read_unlock();
103 local_bh_enable();
105 return min_rssi;
107 EXPORT_SYMBOL_GPL(mt76_get_min_avg_rssi);
109 MODULE_LICENSE("Dual BSD/GPL");