SAMA5D4 Xplained Ultra Board BSP
guowenxue
2019-08-19 2e7235d10c6dbff81960282e1a1e2e798f9b8db8
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/******************************************************************************
 *
 * Copyright(c) 2009-2010  Realtek Corporation.
 *
 *****************************************************************************/
 
#include <drv_types.h>
 
#ifdef CONFIG_IOCTL_CFG80211
 
#include <rtw_wifi_regd.h>
 
static struct country_code_to_enum_rd allCountries[] = {
    {COUNTRY_CODE_USER, "RD"},
};
 
/*
 * REG_RULE(freq start, freq end, bandwidth, max gain, eirp, reg_flags)
 */
 
/*
 *Only these channels all allow active
 *scan on all world regulatory domains
 */
 
/* 2G chan 01 - chan 11 */
#define RTW_2GHZ_CH01_11    \
    REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
 
/*
 *We enable active scan on these a case
 *by case basis by regulatory domain
 */
 
/* 2G chan 12 - chan 13, PASSIV SCAN */
#define RTW_2GHZ_CH12_13    \
    REG_RULE(2467-10, 2472+10, 40, 0, 20,    \
         NL80211_RRF_PASSIVE_SCAN)
 
/* 2G chan 14, PASSIVS SCAN, NO OFDM (B only) */
#define RTW_2GHZ_CH14    \
    REG_RULE(2484-10, 2484+10, 40, 0, 20,    \
         NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_OFDM)
 
/* 5G chan 36 - chan 64 */
#define RTW_5GHZ_5150_5350    \
    REG_RULE(5150-10, 5350+10, 40, 0, 30,    \
         NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
 
/* 5G chan 100 - chan 165 */
#define RTW_5GHZ_5470_5850    \
    REG_RULE(5470-10, 5850+10, 40, 0, 30, \
         NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
 
/* 5G chan 149 - chan 165 */
#define RTW_5GHZ_5725_5850    \
    REG_RULE(5725-10, 5850+10, 40, 0, 30, \
         NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
 
/* 5G chan 36 - chan 165 */
#define RTW_5GHZ_5150_5850    \
    REG_RULE(5150-10, 5850+10, 40, 0, 30,    \
         NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS)
 
static const struct ieee80211_regdomain rtw_regdom_rd = {
    .n_reg_rules = 3,
    .alpha2 = "99",
    .reg_rules = {
        RTW_2GHZ_CH01_11,
        RTW_2GHZ_CH12_13,
        RTW_5GHZ_5150_5850,
    }
};
 
static const struct ieee80211_regdomain rtw_regdom_11 = {
    .n_reg_rules = 1,
    .alpha2 = "99",
    .reg_rules = {
        RTW_2GHZ_CH01_11,
    }
};
 
static const struct ieee80211_regdomain rtw_regdom_12_13 = {
    .n_reg_rules = 2,
    .alpha2 = "99",
    .reg_rules = {
        RTW_2GHZ_CH01_11,
        RTW_2GHZ_CH12_13,
    }
};
 
static const struct ieee80211_regdomain rtw_regdom_no_midband = {
    .n_reg_rules = 3,
    .alpha2 = "99",
    .reg_rules = {
        RTW_2GHZ_CH01_11,
        RTW_5GHZ_5150_5350,
        RTW_5GHZ_5725_5850,
    }
};
 
static const struct ieee80211_regdomain rtw_regdom_60_64 = {
    .n_reg_rules = 3,
    .alpha2 = "99",
    .reg_rules = {
        RTW_2GHZ_CH01_11,
        RTW_2GHZ_CH12_13,
        RTW_5GHZ_5725_5850,
    }
};
 
static const struct ieee80211_regdomain rtw_regdom_14_60_64 = {
    .n_reg_rules = 4,
    .alpha2 = "99",
    .reg_rules = {
        RTW_2GHZ_CH01_11,
        RTW_2GHZ_CH12_13,
        RTW_2GHZ_CH14,
        RTW_5GHZ_5725_5850,
    }
};
 
static const struct ieee80211_regdomain rtw_regdom_14 = {
    .n_reg_rules = 3,
    .alpha2 = "99",
    .reg_rules = {
        RTW_2GHZ_CH01_11,
        RTW_2GHZ_CH12_13,
        RTW_2GHZ_CH14,
    }
};
 
#if 0
static struct rtw_regulatory *rtw_regd;
#endif
 
static bool _rtw_is_radar_freq(u16 center_freq)
{
    return center_freq >= 5260 && center_freq <= 5700;
}
 
#if 0 /* not_yet */
static void _rtw_reg_apply_beaconing_flags(struct wiphy *wiphy,
        enum nl80211_reg_initiator initiator)
{
    enum nl80211_band band;
    struct ieee80211_supported_band *sband;
    const struct ieee80211_reg_rule *reg_rule;
    struct ieee80211_channel *ch;
    unsigned int i;
    u32 bandwidth = 0;
    int r;
 
    for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
 
        if (!wiphy->bands[band])
            continue;
 
        sband = wiphy->bands[band];
 
        for (i = 0; i < sband->n_channels; i++) {
            ch = &sband->channels[i];
            if (_rtw_is_radar_freq(ch->center_freq) ||
                (ch->flags & IEEE80211_CHAN_RADAR))
                continue;
            if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
                r = freq_reg_info(wiphy, ch->center_freq,
                          bandwidth, &reg_rule);
                if (r)
                    continue;
 
                /*
                 *If 11d had a rule for this channel ensure
                 *we enable adhoc/beaconing if it allows us to
                 *use it. Note that we would have disabled it
                 *by applying our static world regdomain by
                 *default during init, prior to calling our
                 *regulatory_hint().
                 */
 
                if (!(reg_rule->flags & NL80211_RRF_NO_IBSS))
                    ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
                if (!
                    (reg_rule->flags &
                     NL80211_RRF_PASSIVE_SCAN))
                    ch->flags &=
                        ~IEEE80211_CHAN_PASSIVE_SCAN;
            } else {
                if (ch->beacon_found)
                    ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
                        IEEE80211_CHAN_PASSIVE_SCAN);
            }
        }
    }
}
 
/* Allows active scan scan on Ch 12 and 13 */
static void _rtw_reg_apply_active_scan_flags(struct wiphy *wiphy,
        enum nl80211_reg_initiator
        initiator)
{
    struct ieee80211_supported_band *sband;
    struct ieee80211_channel *ch;
    const struct ieee80211_reg_rule *reg_rule;
    u32 bandwidth = 0;
    int r;
 
    if (!wiphy->bands[NL80211_BAND_2GHZ])
        return;
    sband = wiphy->bands[NL80211_BAND_2GHZ];
 
    /*
     * If no country IE has been received always enable active scan
     * on these channels. This is only done for specific regulatory SKUs
     */
    if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
        ch = &sband->channels[11];    /* CH 12 */
        if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
            ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
        ch = &sband->channels[12];    /* CH 13 */
        if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
            ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
        return;
    }
 
    /*
     * If a country IE has been received check its rule for this
     * channel first before enabling active scan. The passive scan
     * would have been enforced by the initial processing of our
     * custom regulatory domain.
     */
 
    ch = &sband->channels[11];    /* CH 12 */
    r = freq_reg_info(wiphy, ch->center_freq, bandwidth, &reg_rule);
    if (!r) {
        if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
            if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
                ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
    }
 
    ch = &sband->channels[12];    /* CH 13 */
    r = freq_reg_info(wiphy, ch->center_freq, bandwidth, &reg_rule);
    if (!r) {
        if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
            if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
                ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
    }
}
#endif
 
/*
 * Always apply Radar/DFS rules on
 * freq range 5260 MHz - 5700 MHz
 */
static void _rtw_reg_apply_radar_flags(struct wiphy *wiphy)
{
    struct ieee80211_supported_band *sband;
    struct ieee80211_channel *ch;
    unsigned int i;
 
    if (!wiphy->bands[NL80211_BAND_5GHZ])
        return;
 
    sband = wiphy->bands[NL80211_BAND_5GHZ];
 
    for (i = 0; i < sband->n_channels; i++) {
        ch = &sband->channels[i];
        if (!_rtw_is_radar_freq(ch->center_freq))
            continue;
#ifdef CONFIG_DFS
        if (!(ch->flags & IEEE80211_CHAN_DISABLED)
            #if defined(CONFIG_DFS_MASTER)
            && rtw_odm_dfs_domain_unknown(wiphy_to_adapter(wiphy))
            #endif
        ) {
            ch->flags |= IEEE80211_CHAN_RADAR;
            #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0))
            ch->flags |= (IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_PASSIVE_SCAN);
            #else
            ch->flags |= IEEE80211_CHAN_NO_IR;
            #endif
        }
#endif /* CONFIG_DFS */
 
#if 0
        /*
         * We always enable radar detection/DFS on this
         * frequency range. Additionally we also apply on
         * this frequency range:
         * - If STA mode does not yet have DFS supports disable
         *  active scanning
         * - If adhoc mode does not support DFS yet then disable
         *  adhoc in the frequency.
         * - If AP mode does not yet support radar detection/DFS
         *  do not allow AP mode
         */
        if (!(ch->flags & IEEE80211_CHAN_DISABLED))
            ch->flags |= IEEE80211_CHAN_RADAR |
                     IEEE80211_CHAN_NO_IBSS |
                     IEEE80211_CHAN_PASSIVE_SCAN;
#endif
    }
}
 
static void _rtw_reg_apply_flags(struct wiphy *wiphy)
{
#if 1                /* by channel plan */
    _adapter *padapter = wiphy_to_adapter(wiphy);
    u8 channel_plan = padapter->mlmepriv.ChannelPlan;
    struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
    RT_CHANNEL_INFO *channel_set = pmlmeext->channel_set;
    u8 max_chan_nums = pmlmeext->max_chan_nums;
 
    struct ieee80211_supported_band *sband;
    struct ieee80211_channel *ch;
    unsigned int i, j;
    u16 channel;
    u32 freq;
 
    /* all channels disable */
    for (i = 0; i < NUM_NL80211_BANDS; i++) {
        sband = wiphy->bands[i];
 
        if (sband) {
            for (j = 0; j < sband->n_channels; j++) {
                ch = &sband->channels[j];
 
                if (ch)
                    ch->flags = IEEE80211_CHAN_DISABLED;
            }
        }
    }
 
    /* channels apply by channel plans. */
    for (i = 0; i < max_chan_nums; i++) {
        channel = channel_set[i].ChannelNum;
        freq = rtw_ch2freq(channel);
 
        ch = ieee80211_get_channel(wiphy, freq);
        if (ch) {
            if (channel_set[i].ScanType == SCAN_PASSIVE
                #if defined(CONFIG_DFS_MASTER)
                && rtw_odm_dfs_domain_unknown(wiphy_to_adapter(wiphy))
                #endif
            ) {
                #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0))
                ch->flags = (IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_PASSIVE_SCAN);
                #else
                ch->flags = IEEE80211_CHAN_NO_IR;
                #endif
            } else
                ch->flags = 0;
        }
    }
 
#else
    struct ieee80211_supported_band *sband;
    struct ieee80211_channel *ch;
    unsigned int i, j;
    u16 channels[37] = {
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 36, 40, 44, 48, 52, 56,
        60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
        149, 153,
        157, 161, 165
    };
    u16 channel;
    u32 freq;
 
    for (i = 0; i < NUM_NL80211_BANDS; i++) {
        sband = wiphy->bands[i];
 
        if (sband)
            for (j = 0; j < sband->n_channels; j++) {
                ch = &sband->channels[j];
 
                if (ch)
                    ch->flags = IEEE80211_CHAN_DISABLED;
            }
    }
 
    for (i = 0; i < 37; i++) {
        channel = channels[i];
        freq = rtw_ch2freq(channel);
 
        ch = ieee80211_get_channel(wiphy, freq);
        if (ch) {
            if (channel <= 11)
                ch->flags = 0;
            else
                ch->flags = 0;    /* IEEE80211_CHAN_PASSIVE_SCAN; */
        }
        /* printk("%s: freq %d(%d) flag 0x%02X\n", __func__, freq, channel, ch->flags); */
    }
#endif
}
 
static void _rtw_reg_apply_world_flags(struct wiphy *wiphy,
                       enum nl80211_reg_initiator initiator,
                       struct rtw_regulatory *reg)
{
    /* _rtw_reg_apply_beaconing_flags(wiphy, initiator); */
    /* _rtw_reg_apply_active_scan_flags(wiphy, initiator); */
    return;
}
 
static int _rtw_reg_notifier_apply(struct wiphy *wiphy,
                   struct regulatory_request *request,
                   struct rtw_regulatory *reg)
{
 
    /* Hard code flags */
    _rtw_reg_apply_flags(wiphy);
 
    /* We always apply this */
    _rtw_reg_apply_radar_flags(wiphy);
 
    switch (request->initiator) {
    case NL80211_REGDOM_SET_BY_DRIVER:
        RTW_INFO("%s: %s\n", __func__, "NL80211_REGDOM_SET_BY_DRIVER");
        _rtw_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER,
                       reg);
        break;
    case NL80211_REGDOM_SET_BY_CORE:
        RTW_INFO("%s: %s\n", __func__,
             "NL80211_REGDOM_SET_BY_CORE to DRV");
        _rtw_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER,
                       reg);
        break;
    case NL80211_REGDOM_SET_BY_USER:
        RTW_INFO("%s: %s\n", __func__,
             "NL80211_REGDOM_SET_BY_USER to DRV");
        _rtw_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER,
                       reg);
        break;
    case NL80211_REGDOM_SET_BY_COUNTRY_IE:
        RTW_INFO("%s: %s\n", __func__,
             "NL80211_REGDOM_SET_BY_COUNTRY_IE");
        _rtw_reg_apply_world_flags(wiphy, request->initiator, reg);
        break;
    }
 
    return 0;
}
 
static const struct ieee80211_regdomain *_rtw_regdomain_select(struct
        rtw_regulatory
        *reg)
{
#if 0
    switch (reg->country_code) {
    case COUNTRY_CODE_USER:
    default:
        return &rtw_regdom_rd;
    }
#else
    return &rtw_regdom_rd;
#endif
}
 
void _rtw_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
{
    struct rtw_regulatory *reg = NULL;
 
    RTW_INFO("%s\n", __func__);
 
    _rtw_reg_notifier_apply(wiphy, request, reg);
}
 
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
int rtw_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
#else
void rtw_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
#endif
{
    _rtw_reg_notifier(wiphy, request);
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
    return 0;
#endif
}
 
void rtw_reg_notify_by_driver(_adapter *adapter)
{
    if ((adapter->rtw_wdev != NULL) && (adapter->rtw_wdev->wiphy)) {
        struct regulatory_request request;
        request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
        rtw_reg_notifier(adapter->rtw_wdev->wiphy, &request);
    }
}
 
static void _rtw_regd_init_wiphy(struct rtw_regulatory *reg, struct wiphy *wiphy)
{
    const struct ieee80211_regdomain *regd;
 
    wiphy->reg_notifier = rtw_reg_notifier;
 
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0))
    wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
    wiphy->flags &= ~WIPHY_FLAG_STRICT_REGULATORY;
    wiphy->flags &= ~WIPHY_FLAG_DISABLE_BEACON_HINTS;
#else
    wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
    wiphy->regulatory_flags &= ~REGULATORY_STRICT_REG;
    wiphy->regulatory_flags &= ~REGULATORY_DISABLE_BEACON_HINTS;
#endif
 
    regd = _rtw_regdomain_select(reg);
    wiphy_apply_custom_regulatory(wiphy, regd);
 
    /* Hard code flags */
    _rtw_reg_apply_flags(wiphy);
    _rtw_reg_apply_radar_flags(wiphy);
    _rtw_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
}
 
static struct country_code_to_enum_rd *_rtw_regd_find_country(u16 countrycode)
{
    int i;
 
    for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
        if (allCountries[i].countrycode == countrycode)
            return &allCountries[i];
    }
    return NULL;
}
 
int rtw_regd_init(_adapter *padapter)
{
    struct wiphy *wiphy = padapter->rtw_wdev->wiphy;
 
#if 0
    if (rtw_regd == NULL) {
        rtw_regd = (struct rtw_regulatory *)
               rtw_malloc(sizeof(struct rtw_regulatory));
 
        rtw_regd->alpha2[0] = '9';
        rtw_regd->alpha2[1] = '9';
 
        rtw_regd->country_code = COUNTRY_CODE_USER;
    }
 
    RTW_INFO("%s: Country alpha2 being used: %c%c\n",
         __func__, rtw_regd->alpha2[0], rtw_regd->alpha2[1]);
#endif
 
    _rtw_regd_init_wiphy(NULL, wiphy);
 
    return 0;
}
#endif /* CONFIG_IOCTL_CFG80211 */