hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/******************************************************************************
 *
 * Copyright(c) 2007 - 2022 Realtek Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 *****************************************************************************/
#define _RTW_CHSET_C_
 
#include <drv_types.h>
 
const char *const _rtw_ch_type_str[] = {
   [RTW_CHT_DIS]        = "DIS",
   [RTW_CHT_NO_IR]        = "NO_IR",
   [RTW_CHT_DFS]        = "DFS",
   [RTW_CHT_NO_HT40U]    = "NO_40M+",
   [RTW_CHT_NO_HT40L]    = "NO_40M-",
   [RTW_CHT_NO_80MHZ]    = "NO_80M",
   [RTW_CHT_NO_160MHZ]    = "NO_160M",
   [RTW_CHT_NUM]        = "UNKNOWN",
};
 
enum rtw_ch_type get_ch_type_from_str(const char *str, size_t str_len)
{
   u8 i;
 
   for (i = 0; i < RTW_CHT_NUM; i++)
       if (str_len == strlen(rtw_ch_type_str(i))
           && strncmp(str, rtw_ch_type_str(i), str_len) == 0)
           return i;
   return RTW_CHT_NUM;
}
 
char *rtw_get_ch_flags_str(char *buf, u8 flags, char delim)
{
   char *pos = buf;
   char d_str[2] = {delim, '\0'};
   int i;
 
   for (i = 0; i < RTW_CHT_NUM; i++) {
       if (!(flags & BIT(i)))
           continue;
       pos += snprintf(pos, RTW_CH_FLAGS_STR_LEN - (pos - buf), "%s%s"
           , pos == buf ? "" : d_str, rtw_ch_type_str(i));
       if (pos >= buf + RTW_CH_FLAGS_STR_LEN - 1)
           break;
   }
   if (pos == buf)
       *buf = '\0';
 
   return buf;
}
 
int rtw_chset_init(struct rtw_chset *chset, u8 band_bmp)
{
   u8 ch_num = 0;
   int band, i;
 
   _rtw_memset(chset->chs, 0, sizeof(RT_CHANNEL_INFO) * MAX_CHANNEL_NUM);
 
   for (band = 0; band < BAND_MAX; band++) {
       u8 center_ch_num;
       u8 (*center_chs)(u8, u8);
 
       if (!(band_bmp & band_to_band_cap(band)))
           continue;
 
       center_ch_num = center_chs_num_of_band[band](CHANNEL_WIDTH_20);
       center_chs = center_chs_of_band[band];
 
       chset->chs_of_band[band] = &chset->chs[ch_num];
       chset->chs_len_of_band[band] = 0;
 
       for (i = 0; i < center_ch_num; i++) {
           chset->chs[ch_num].band = band;
           chset->chs[ch_num].ChannelNum = center_chs(CHANNEL_WIDTH_20, i);
           chset->chs_len_of_band[band]++;;
           ch_num++;
       }
   }
 
   chset->chs_len = ch_num;
 
   return _SUCCESS;
}
 
#if CONFIG_ALLOW_FUNC_2G_5G_ONLY
/*
 * Search enabled channel with the @param ch in given @param ch_set
 * @ch_set: the given channel set
 * @ch: the given channel number
 *
 * return the index of channel_num in channel_set, -1 if not found
 */
RTW_FUNC_2G_5G_ONLY static int _rtw_chset_search_ch(const struct rtw_chset *chset, u32 ch, bool include_dis)
{
   int i;
 
   if (ch == 0)
       return -1;
 
   for (i = 0; i < chset->chs_len; i++) {
       if (ch == chset->chs[i].ChannelNum
           && (include_dis || !(chset->chs[i].flags & RTW_CHF_DIS)))
           return i;
   }
 
   return -1;
}
 
RTW_FUNC_2G_5G_ONLY int rtw_chset_search_ch(const struct rtw_chset *chset, u32 ch)
{
   return _rtw_chset_search_ch(chset, ch, false);
}
#endif
 
/*
 * Search enabled channel with the @param ch of @param band in given @param ch_set
 * @ch_set: the given channel set
 * @band: the given band
 * @ch: the given channel number
 *
 * return the index of channel_num in channel_set, -1 if not found
 */
static int _rtw_chset_search_bch(const struct rtw_chset *chset, enum band_type band, u32 ch, bool include_dis)
{
   int i;
 
   if (ch == 0)
       return -1;
 
   for (i = 0; i < chset->chs_len; i++) {
       if (band == chset->chs[i].band && ch == chset->chs[i].ChannelNum
           && (include_dis || !(chset->chs[i].flags & RTW_CHF_DIS)))
           return i;
   }
 
   return -1;
}
 
int rtw_chset_search_bch(const struct rtw_chset *chset, enum band_type band, u32 ch)
{
   return _rtw_chset_search_bch(chset, band, ch, false);
}
 
RT_CHANNEL_INFO *rtw_chset_get_chinfo_by_bch(struct rtw_chset *chset, enum band_type band, u32 ch, bool include_dis)
{
   int i = _rtw_chset_search_bch(chset, band, ch, include_dis);
 
   return i >= 0 ? &chset->chs[i] : NULL;
}
 
/*
 * Check if the @param ch, bw, offset is valid for the given @param ch_set
 * @ch_set: the given channel set
 * @ch: the given channel number
 * @bw: the given bandwidth
 * @offset: the given channel offset
 *
 * return valid (1) or not (0)
 */
static u8 _rtw_chset_is_bchbw_valid(const struct rtw_chset *chset, enum band_type band, u8 ch, u8 bw, u8 offset
   , bool allow_primary_passive, bool allow_passive)
{
   u8 cch;
   u8 *op_chs;
   u8 op_ch_num;
   u8 valid = 0;
   int i;
   int ch_idx;
 
   cch = rtw_get_center_ch_by_band(band, ch, bw, offset);
 
   if (!rtw_get_op_chs_by_bcch_bw(band, cch, bw, &op_chs, &op_ch_num))
       goto exit;
 
   for (i = 0; i < op_ch_num; i++) {
       if (0)
           RTW_INFO("%u,%u,%u,%u - cch:%u, bw:%u, op_ch:%u\n", band, ch, bw, offset, cch, bw, *(op_chs + i));
       ch_idx = rtw_chset_search_bch(chset, band, *(op_chs + i));
       if (ch_idx == -1)
           break;
       if (!allow_passive && chset->chs[ch_idx].flags & RTW_CHF_NO_IR) {
           /* all sub chs are passive is not allowed and one of sub ch is NO_IR */
           if (!allow_primary_passive) /* even primary ch is not allow to be NO_IR */
               break;
           if (chset->chs[ch_idx].ChannelNum != ch) /* allow primary ch NO_IR, but this is not primary ch */
               break;
       }
       if (bw >= CHANNEL_WIDTH_40) {
           if ((chset->chs[ch_idx].flags & RTW_CHF_NO_HT40U) && i % 2 == 0)
               break;
           if ((chset->chs[ch_idx].flags & RTW_CHF_NO_HT40L) && i % 2 == 1)
               break;
       }
       if (bw >= CHANNEL_WIDTH_80 && (chset->chs[ch_idx].flags & RTW_CHF_NO_80MHZ))
           break;
       if (bw >= CHANNEL_WIDTH_160 && (chset->chs[ch_idx].flags & RTW_CHF_NO_160MHZ))
           break;
   }
 
   if (op_ch_num != 0 && i == op_ch_num)
       valid = 1;
 
exit:
   return valid;
}
 
#if CONFIG_ALLOW_FUNC_2G_5G_ONLY
RTW_FUNC_2G_5G_ONLY u8 rtw_chset_is_chbw_valid(const struct rtw_chset *chset, u8 ch, u8 bw, u8 offset, bool allow_primary_passive, bool allow_passive)
{
   return _rtw_chset_is_bchbw_valid(chset, rtw_is_2g_ch(ch) ? BAND_ON_24G : BAND_ON_5G, ch, bw, offset, allow_primary_passive, allow_passive);
}
#endif
 
u8 rtw_chset_is_bchbw_valid(const struct rtw_chset *chset, enum band_type band, u8 ch, u8 bw, u8 offset
   , bool allow_primary_passive, bool allow_passive)
{
   return _rtw_chset_is_bchbw_valid(chset, band, ch, bw, offset, allow_primary_passive, allow_passive);
}
 
/**
 * rtw_chset_sync_bchbw - obey g_ch, adjust g_bw, g_offset, bw, offset to fit in channel plan
 * @ch_set: channel plan to check
 * @req_ch: pointer of the request ch, may be modified further
 * @req_bw: pointer of the request bw, may be modified further
 * @req_offset: pointer of the request offset, may be modified further
 * @g_ch: pointer of the ongoing group ch
 * @g_bw: pointer of the ongoing group bw, may be modified further
 * @g_offset: pointer of the ongoing group offset, may be modified further
 * @allow_primary_passive: if allow passive primary ch when deciding chbw
 * @allow_passive: if allow passive ch (not primary) when deciding chbw
 */
void rtw_chset_sync_bchbw(const struct rtw_chset *chset, enum band_type *req_band, u8 *req_ch, u8 *req_bw, u8 *req_offset
   , enum band_type *g_band, u8 *g_ch, u8 *g_bw, u8 *g_offset, bool allow_primary_passive, bool allow_passive)
{
   enum band_type r_band;
   u8 r_ch, r_bw, r_offset;
   enum band_type u_band;
   u8 u_ch, u_bw, u_offset;
   u8 cur_bw = *req_bw;
 
   while (1) {
       r_band = *req_band;
       r_ch = *req_ch;
       r_bw = cur_bw;
       r_offset = *req_offset;
       u_band = *g_band;
       u_ch = *g_ch;
       u_bw = *g_bw;
       u_offset = *g_offset;
 
       rtw_sync_bchbw(&r_band, &r_ch, &r_bw, &r_offset, &u_band, &u_ch, &u_bw, &u_offset);
 
       if (rtw_chset_is_bchbw_valid(chset, r_band, r_ch, r_bw, r_offset, allow_primary_passive, allow_passive))
           break;
       if (cur_bw == CHANNEL_WIDTH_20) {
           rtw_warn_on(1);
           break;
       }
       cur_bw--;
   };
 
   *req_band = r_band;
   *req_ch = r_ch;
   *req_bw = r_bw;
   *req_offset = r_offset;
   *g_band = u_band;
   *g_ch = u_ch;
   *g_bw = u_bw;
   *g_offset = u_offset;
}
 
#if CONFIG_ALLOW_FUNC_2G_5G_ONLY
RTW_FUNC_2G_5G_ONLY void rtw_chset_sync_chbw(const struct rtw_chset *chset, u8 *req_ch, u8 *req_bw, u8 *req_offset
   , u8 *g_ch, u8 *g_bw, u8 *g_offset, bool allow_primary_passive, bool allow_passive)
{
   enum band_type band = rtw_is_2g_ch(*g_ch) ? BAND_ON_24G : BAND_ON_5G; /* follow g_ch's band */
 
   rtw_chset_sync_bchbw(chset, &band, req_ch, req_bw, req_offset, &band, g_ch, g_bw, g_offset, allow_primary_passive, allow_passive);
}
#endif
 
u8 *rtw_chset_set_spt_chs_ie(struct rtw_chset *chset, u8 *buf_pos, uint *buf_len)
{
   u8 i = 0;
   u8 fch = 0, lch = 0, ch;
   u8 *cont = buf_pos + 2;
   RT_CHANNEL_INFO *chinfo;
 
   while (i < chset->chs_len) {
       chinfo = &chset->chs[i++];
 
       if (chinfo->flags & RTW_CHF_DIS)
           continue;
 
       #if CONFIG_IEEE80211_BAND_6GHZ
       /* don't appnd 6G chs now, how to distinguish 2G/5G chs with 6G? */
       if (chinfo->band == BAND_ON_6G)
           continue;
       #endif
 
       ch = chinfo->ChannelNum;
       if (fch == 0) {
           fch = ch;
           lch = ch;
           continue;
       }
 
       if (lch + 1 != ch) {
           *cont = fch;
           *(cont + 1) = lch - fch + 1;
           cont += 2;
           fch = ch;
       }
       lch = ch;
   }
   if (fch) {
       /* last subband */
       *cont = fch;
       *(cont + 1) = lch - fch + 1;
       cont += 2;
   }
 
   if (cont > buf_pos + 2) {
       *buf_pos = WLAN_EID_SUPPORTED_CHANNELS;
       *(buf_pos + 1) = cont - buf_pos - 2;
       *buf_len += cont - buf_pos;
       return cont;
   }
   return buf_pos;
}
 
#ifdef CONFIG_PROC_DEBUG
void dump_chinfos(void *sel, const RT_CHANNEL_INFO *chinfos, u8 chinfo_num)
{
   u32 bhint_sec;
   char bhint_buf[8];
   u16 non_ocp_sec;
   char non_ocp_buf[8];
   char flags_buf[RTW_CH_FLAGS_STR_LEN];
   u8 enable_ch_num = 0;
   u8 i;
 
   RTW_PRINT_SEL(sel, "%-3s %-4s %-5s %-4s flags\n", "ch", "freq", "bhint", "nocp");
 
   for (i = 0; i < chinfo_num; i++) {
       if (chinfos[i].flags & RTW_CHF_DIS)
           continue;
       enable_ch_num++;
 
       bhint_sec = 0;
       if (CH_IS_BCN_HINT(&chinfos[i])) {
           bhint_sec = rtw_systime_to_ms(chinfos[i].bcn_hint_end_time - rtw_get_current_time()) / 1000;
           if (bhint_sec > 99999)
               bhint_sec = 99999;
       }
       snprintf(bhint_buf, 8, "%d", bhint_sec);
 
       non_ocp_sec = 0;
       #ifdef CONFIG_DFS_MASTER
       if (CH_IS_NON_OCP(&chinfos[i]))
           non_ocp_sec = rtw_systime_to_ms(chinfos[i].non_ocp_end_time - rtw_get_current_time()) / 1000;
       #endif
       snprintf(non_ocp_buf, 8, "%d", non_ocp_sec);
 
       RTW_PRINT_SEL(sel, "%3u %4u %5s %4s %s\n"
           , chinfos[i].ChannelNum, rtw_bch2freq(chinfos[i].band, chinfos[i].ChannelNum)
           , bhint_buf, non_ocp_buf, rtw_get_ch_flags_str(flags_buf, chinfos[i].flags, ' ')
       );
   }
 
   RTW_PRINT_SEL(sel, "total ch number:%d\n", enable_ch_num);
}
#endif /* CONFIG_PROC_DEBUG */