forked from FreedomBen/rtl8188ce-linux-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.c
1949 lines (1705 loc) · 56 KB
/
core.c
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/* Copyright( c ) 2009-2012 Realtek Corporation.*/
#include "wifi.h"
#include "core.h"
#include "cam.h"
#include "base.h"
#include "ps.h"
#include "pwrseqcmd.h"
#include "btcoexist/rtl_btc.h"
#include <linux/firmware.h>
#include <linux/export.h>
#include <net/cfg80211.h>
u8 channel5g[CHANNEL_MAX_NUMBER_5G] = {
36, 38, 40, 42, 44, 46, 48, /* Band 1 */
52, 54, 56, 58, 60, 62, 64, /* Band 2 */
100, 102, 104, 106, 108, 110, 112, /* Band 3 */
116, 118, 120, 122, 124, 126, 128, /* Band 3 */
132, 134, 136, 138, 140, 142, 144, /* Band 3 */
149, 151, 153, 155, 157, 159, 161, /* Band 4 */
165, 167, 169, 171, 173, 175, 177 /* Band 4 */
};
EXPORT_SYMBOL( channel5g );
u8 channel5g_80m[CHANNEL_MAX_NUMBER_5G_80M] = {
42, 58, 106, 122, 138, 155, 171
};
EXPORT_SYMBOL( channel5g_80m );
void rtl_addr_delay( u32 addr )
{
if ( addr == 0xfe )
mdelay( 50 );
else if ( addr == 0xfd )
msleep( 5 );
else if ( addr == 0xfc )
msleep( 1 );
else if ( addr == 0xfb )
usleep_range( 50, 100 );
else if ( addr == 0xfa )
usleep_range( 5, 10 );
else if ( addr == 0xf9 )
usleep_range( 1, 2 );
}
EXPORT_SYMBOL( rtl_addr_delay );
void rtl_rfreg_delay( struct ieee80211_hw *hw, enum radio_path rfpath, u32 addr,
u32 mask, u32 data )
{
if ( addr >= 0xf9 && addr <= 0xfe ) {
rtl_addr_delay( addr );
} else {
rtl_set_rfreg( hw, rfpath, addr, mask, data );
udelay( 1 );
}
}
EXPORT_SYMBOL( rtl_rfreg_delay );
void rtl_bb_delay( struct ieee80211_hw *hw, u32 addr, u32 data )
{
if ( addr >= 0xf9 && addr <= 0xfe ) {
rtl_addr_delay( addr );
} else {
rtl_set_bbreg( hw, addr, MASKDWORD, data );
udelay( 1 );
}
}
EXPORT_SYMBOL( rtl_bb_delay );
static void rtl_fw_do_work( const struct firmware *firmware, void *context,
bool is_wow )
{
struct ieee80211_hw *hw = context;
struct rtl_priv *rtlpriv = rtl_priv( hw );
int err;
RT_TRACE( rtlpriv, COMP_ERR, DBG_LOUD,
"Firmware callback routine entered!\n" );
complete( &rtlpriv->firmware_loading_complete );
if ( !firmware ) {
if ( rtlpriv->cfg->alt_fw_name ) {
err = request_firmware( &firmware,
rtlpriv->cfg->alt_fw_name,
rtlpriv->io.dev );
pr_info( "Loading alternative firmware %s\n",
rtlpriv->cfg->alt_fw_name );
if ( !err )
goto found_alt;
}
pr_err( "Selected firmware is not available\n" );
rtlpriv->max_fw_size = 0;
return;
}
found_alt:
if ( firmware->size > rtlpriv->max_fw_size ) {
pr_err( "Firmware is too big!\n" );
release_firmware( firmware );
return;
}
if ( !is_wow ) {
memcpy( rtlpriv->rtlhal.pfirmware, firmware->data,
firmware->size );
rtlpriv->rtlhal.fwsize = firmware->size;
} else {
memcpy( rtlpriv->rtlhal.wowlan_firmware, firmware->data,
firmware->size );
rtlpriv->rtlhal.wowlan_fwsize = firmware->size;
}
release_firmware( firmware );
}
void rtl_fw_cb( const struct firmware *firmware, void *context )
{
rtl_fw_do_work( firmware, context, false );
}
EXPORT_SYMBOL( rtl_fw_cb );
void rtl_wowlan_fw_cb( const struct firmware *firmware, void *context )
{
rtl_fw_do_work( firmware, context, true );
}
EXPORT_SYMBOL( rtl_wowlan_fw_cb );
/*mutex for start & stop is must here. */
static int rtl_op_start( struct ieee80211_hw *hw )
{
int err = 0;
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_hal *rtlhal = rtl_hal( rtl_priv( hw ) );
if ( !is_hal_stop( rtlhal ) )
return 0;
if ( !test_bit( RTL_STATUS_INTERFACE_START, &rtlpriv->status ) )
return 0;
mutex_lock( &rtlpriv->locks.conf_mutex );
err = rtlpriv->intf_ops->adapter_start( hw );
if ( !err )
rtl_watch_dog_timer_callback( &rtlpriv->works.watchdog_timer );
mutex_unlock( &rtlpriv->locks.conf_mutex );
return err;
}
static void rtl_op_stop( struct ieee80211_hw *hw )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_mac *mac = rtl_mac( rtl_priv( hw ) );
struct rtl_hal *rtlhal = rtl_hal( rtl_priv( hw ) );
struct rtl_ps_ctl *ppsc = rtl_psc( rtl_priv( hw ) );
bool support_remote_wakeup = false;
if ( is_hal_stop( rtlhal ) )
return;
rtlpriv->cfg->ops->get_hw_reg( hw, HAL_DEF_WOWLAN,
( u8 * )( &support_remote_wakeup ) );
/* here is must, because adhoc do stop and start,
* but stop with RFOFF may cause something wrong,
* like adhoc TP
*/
if ( unlikely( ppsc->rfpwr_state == ERFOFF ) )
rtl_ips_nic_on( hw );
mutex_lock( &rtlpriv->locks.conf_mutex );
/* if wowlan supported, DON'T clear connected info */
if ( !( support_remote_wakeup &&
rtlhal->enter_pnp_sleep ) ) {
mac->link_state = MAC80211_NOLINK;
eth_zero_addr( mac->bssid );
mac->vendor = PEER_UNKNOWN;
/* reset sec info */
rtl_cam_reset_sec_info( hw );
rtl_deinit_deferred_work( hw, false );
}
rtlpriv->intf_ops->adapter_stop( hw );
mutex_unlock( &rtlpriv->locks.conf_mutex );
}
static void rtl_op_tx( struct ieee80211_hw *hw,
struct ieee80211_tx_control *control,
struct sk_buff *skb )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_hal *rtlhal = rtl_hal( rtl_priv( hw ) );
struct rtl_ps_ctl *ppsc = rtl_psc( rtl_priv( hw ) );
struct rtl_tcb_desc tcb_desc;
memset( &tcb_desc, 0, sizeof( struct rtl_tcb_desc ) );
if ( unlikely( is_hal_stop( rtlhal ) || ppsc->rfpwr_state != ERFON ) )
goto err_free;
if ( !test_bit( RTL_STATUS_INTERFACE_START, &rtlpriv->status ) )
goto err_free;
if ( !rtlpriv->intf_ops->waitq_insert( hw, control->sta, skb ) )
rtlpriv->intf_ops->adapter_tx( hw, control->sta, skb, &tcb_desc );
return;
err_free:
dev_kfree_skb_any( skb );
}
static int rtl_op_add_interface( struct ieee80211_hw *hw,
struct ieee80211_vif *vif )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_mac *mac = rtl_mac( rtl_priv( hw ) );
int err = 0;
u8 retry_limit = 0x30;
if ( mac->vif ) {
RT_TRACE( rtlpriv, COMP_ERR, DBG_WARNING,
"vif has been set!! mac->vif = 0x%p\n", mac->vif );
return -EOPNOTSUPP;
}
vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
rtl_ips_nic_on( hw );
mutex_lock( &rtlpriv->locks.conf_mutex );
switch ( ieee80211_vif_type_p2p( vif ) ) {
case NL80211_IFTYPE_P2P_CLIENT:
mac->p2p = P2P_ROLE_CLIENT;
/*fall through*/
case NL80211_IFTYPE_STATION:
if ( mac->beacon_enabled == 1 ) {
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"NL80211_IFTYPE_STATION\n" );
mac->beacon_enabled = 0;
rtlpriv->cfg->ops->update_interrupt_mask( hw, 0,
rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS] );
}
break;
case NL80211_IFTYPE_ADHOC:
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"NL80211_IFTYPE_ADHOC\n" );
mac->link_state = MAC80211_LINKED;
rtlpriv->cfg->ops->set_bcn_reg( hw );
if ( rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G )
mac->basic_rates = 0xfff;
else
mac->basic_rates = 0xff0;
rtlpriv->cfg->ops->set_hw_reg( hw, HW_VAR_BASIC_RATE,
( u8 * )( &mac->basic_rates ) );
retry_limit = 0x07;
break;
case NL80211_IFTYPE_P2P_GO:
mac->p2p = P2P_ROLE_GO;
/*fall through*/
case NL80211_IFTYPE_AP:
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"NL80211_IFTYPE_AP\n" );
mac->link_state = MAC80211_LINKED;
rtlpriv->cfg->ops->set_bcn_reg( hw );
if ( rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G )
mac->basic_rates = 0xfff;
else
mac->basic_rates = 0xff0;
rtlpriv->cfg->ops->set_hw_reg( hw, HW_VAR_BASIC_RATE,
( u8 * )( &mac->basic_rates ) );
retry_limit = 0x07;
break;
case NL80211_IFTYPE_MESH_POINT:
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"NL80211_IFTYPE_MESH_POINT\n" );
mac->link_state = MAC80211_LINKED;
rtlpriv->cfg->ops->set_bcn_reg( hw );
if ( rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G )
mac->basic_rates = 0xfff;
else
mac->basic_rates = 0xff0;
rtlpriv->cfg->ops->set_hw_reg( hw, HW_VAR_BASIC_RATE,
( u8 * )( &mac->basic_rates ) );
retry_limit = 0x07;
break;
default:
pr_err( "operation mode %d is not supported!\n",
vif->type );
err = -EOPNOTSUPP;
goto out;
}
if ( mac->p2p ) {
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"p2p role %x\n", vif->type );
mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
rtlpriv->cfg->ops->set_hw_reg( hw, HW_VAR_BASIC_RATE,
( u8 * )( &mac->basic_rates ) );
}
mac->vif = vif;
mac->opmode = vif->type;
rtlpriv->cfg->ops->set_network_type( hw, vif->type );
memcpy( mac->mac_addr, vif->addr, ETH_ALEN );
rtlpriv->cfg->ops->set_hw_reg( hw, HW_VAR_ETHER_ADDR, mac->mac_addr );
mac->retry_long = retry_limit;
mac->retry_short = retry_limit;
rtlpriv->cfg->ops->set_hw_reg( hw, HW_VAR_RETRY_LIMIT,
( u8 * )( &retry_limit ) );
out:
mutex_unlock( &rtlpriv->locks.conf_mutex );
return err;
}
static void rtl_op_remove_interface( struct ieee80211_hw *hw,
struct ieee80211_vif *vif )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_mac *mac = rtl_mac( rtl_priv( hw ) );
mutex_lock( &rtlpriv->locks.conf_mutex );
/* Free beacon resources */
if ( vif->type == NL80211_IFTYPE_AP ||
vif->type == NL80211_IFTYPE_ADHOC ||
vif->type == NL80211_IFTYPE_MESH_POINT ) {
if ( mac->beacon_enabled == 1 ) {
mac->beacon_enabled = 0;
rtlpriv->cfg->ops->update_interrupt_mask( hw, 0,
rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS] );
}
}
/*
*Note: We assume NL80211_IFTYPE_UNSPECIFIED as
*NO LINK for our hardware.
*/
mac->p2p = 0;
mac->vif = NULL;
mac->link_state = MAC80211_NOLINK;
eth_zero_addr( mac->bssid );
mac->vendor = PEER_UNKNOWN;
mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
rtlpriv->cfg->ops->set_network_type( hw, mac->opmode );
mutex_unlock( &rtlpriv->locks.conf_mutex );
}
static int rtl_op_change_interface( struct ieee80211_hw *hw,
struct ieee80211_vif *vif,
enum nl80211_iftype new_type, bool p2p )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
int ret;
rtl_op_remove_interface( hw, vif );
vif->type = new_type;
vif->p2p = p2p;
ret = rtl_op_add_interface( hw, vif );
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"p2p %x\n", p2p );
return ret;
}
#ifdef CONFIG_PM
static u16 crc16_ccitt( u8 data, u16 crc )
{
u8 shift_in, data_bit, crc_bit11, crc_bit4, crc_bit15;
u8 i;
u16 result;
for ( i = 0; i < 8; i++ ) {
crc_bit15 = ( ( crc & BIT( 15 ) ) ? 1 : 0 );
data_bit = ( data & ( BIT( 0 ) << i ) ? 1 : 0 );
shift_in = crc_bit15 ^ data_bit;
result = crc << 1;
if ( shift_in == 0 )
result &= ( ~BIT( 0 ) );
else
result |= BIT( 0 );
crc_bit11 = ( ( crc & BIT( 11 ) ) ? 1 : 0 ) ^ shift_in;
if ( crc_bit11 == 0 )
result &= ( ~BIT( 12 ) );
else
result |= BIT( 12 );
crc_bit4 = ( ( crc & BIT( 4 ) ) ? 1 : 0 ) ^ shift_in;
if ( crc_bit4 == 0 )
result &= ( ~BIT( 5 ) );
else
result |= BIT( 5 );
crc = result;
}
return crc;
}
static u16 _calculate_wol_pattern_crc( u8 *pattern, u16 len )
{
u16 crc = 0xffff;
u32 i;
for ( i = 0; i < len; i++ )
crc = crc16_ccitt( pattern[i], crc );
crc = ~crc;
return crc;
}
static void _rtl_add_wowlan_patterns( struct ieee80211_hw *hw,
struct cfg80211_wowlan *wow )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_mac *mac = &rtlpriv->mac80211;
struct cfg80211_pkt_pattern *patterns = wow->patterns;
struct rtl_wow_pattern rtl_pattern;
const u8 *pattern_os, *mask_os;
u8 mask[MAX_WOL_BIT_MASK_SIZE] = {0};
u8 content[MAX_WOL_PATTERN_SIZE] = {0};
u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
u8 multicast_addr1[2] = {0x33, 0x33};
u8 multicast_addr2[3] = {0x01, 0x00, 0x5e};
u8 i, mask_len;
u16 j, len;
for ( i = 0; i < wow->n_patterns; i++ ) {
memset( &rtl_pattern, 0, sizeof( struct rtl_wow_pattern ) );
memset( mask, 0, MAX_WOL_BIT_MASK_SIZE );
if ( patterns[i].pattern_len < 0 ||
patterns[i].pattern_len > MAX_WOL_PATTERN_SIZE ) {
RT_TRACE( rtlpriv, COMP_POWER, DBG_WARNING,
"Pattern[%d] is too long\n", i );
continue;
}
pattern_os = patterns[i].pattern;
mask_len = DIV_ROUND_UP( patterns[i].pattern_len, 8 );
mask_os = patterns[i].mask;
RT_PRINT_DATA( rtlpriv, COMP_POWER, DBG_TRACE,
"pattern content\n", pattern_os,
patterns[i].pattern_len );
RT_PRINT_DATA( rtlpriv, COMP_POWER, DBG_TRACE,
"mask content\n", mask_os, mask_len );
/* 1. unicast? multicast? or broadcast? */
if ( memcmp( pattern_os, broadcast_addr, 6 ) == 0 )
rtl_pattern.type = BROADCAST_PATTERN;
else if ( memcmp( pattern_os, multicast_addr1, 2 ) == 0 ||
memcmp( pattern_os, multicast_addr2, 3 ) == 0 )
rtl_pattern.type = MULTICAST_PATTERN;
else if ( memcmp( pattern_os, mac->mac_addr, 6 ) == 0 )
rtl_pattern.type = UNICAST_PATTERN;
else
rtl_pattern.type = UNKNOWN_TYPE;
/* 2. translate mask_from_os to mask_for_hw */
/******************************************************************************
* pattern from OS uses 'ethenet frame', like this:
| 6 | 6 | 2 | 20 | Variable | 4 |
|--------+--------+------+-----------+------------+-----|
| 802.3 Mac Header | IP Header | TCP Packet | FCS |
| DA | SA | Type |
* BUT, packet catched by our HW is in '802.11 frame', begin from LLC,
| 24 or 30 | 6 | 2 | 20 | Variable | 4 |
|-------------------+--------+------+-----------+------------+-----|
| 802.11 MAC Header | LLC | IP Header | TCP Packet | FCS |
| Others | Tpye |
* Therefore, we need translate mask_from_OS to mask_to_hw.
* We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
* because new mask[0~5] means 'SA', but our HW packet begins from LLC,
* bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
******************************************************************************/
/* Shift 6 bits */
for ( j = 0; j < mask_len - 1; j++ ) {
mask[j] = mask_os[j] >> 6;
mask[j] |= ( mask_os[j + 1] & 0x3F ) << 2;
}
mask[j] = ( mask_os[j] >> 6 ) & 0x3F;
/* Set bit 0-5 to zero */
mask[0] &= 0xC0;
RT_PRINT_DATA( rtlpriv, COMP_POWER, DBG_TRACE,
"mask to hw\n", mask, mask_len );
for ( j = 0; j < ( MAX_WOL_BIT_MASK_SIZE + 1 ) / 4; j++ ) {
rtl_pattern.mask[j] = mask[j * 4];
rtl_pattern.mask[j] |= ( mask[j * 4 + 1] << 8 );
rtl_pattern.mask[j] |= ( mask[j * 4 + 2] << 16 );
rtl_pattern.mask[j] |= ( mask[j * 4 + 3] << 24 );
}
/* To get the wake up pattern from the mask.
* We do not count first 12 bits which means
* DA[6] and SA[6] in the pattern to match HW design.
*/
len = 0;
for ( j = 12; j < patterns[i].pattern_len; j++ ) {
if ( ( mask_os[j / 8] >> ( j % 8 ) ) & 0x01 ) {
content[len] = pattern_os[j];
len++;
}
}
RT_PRINT_DATA( rtlpriv, COMP_POWER, DBG_TRACE,
"pattern to hw\n", content, len );
/* 3. calculate crc */
rtl_pattern.crc = _calculate_wol_pattern_crc( content, len );
RT_TRACE( rtlpriv, COMP_POWER, DBG_TRACE,
"CRC_Remainder = 0x%x\n", rtl_pattern.crc );
/* 4. write crc & mask_for_hw to hw */
rtlpriv->cfg->ops->add_wowlan_pattern( hw, &rtl_pattern, i );
}
rtl_write_byte( rtlpriv, 0x698, wow->n_patterns );
}
static int rtl_op_suspend( struct ieee80211_hw *hw,
struct cfg80211_wowlan *wow )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_hal *rtlhal = rtl_hal( rtlpriv );
struct rtl_ps_ctl *ppsc = rtl_psc( rtl_priv( hw ) );
RT_TRACE( rtlpriv, COMP_POWER, DBG_DMESG, "\n" );
if ( WARN_ON( !wow ) )
return -EINVAL;
/* to resolve s4 can not wake up*/
rtlhal->last_suspend_sec = ktime_get_real_seconds();
if ( ( ppsc->wo_wlan_mode & WAKE_ON_PATTERN_MATCH ) && wow->n_patterns )
_rtl_add_wowlan_patterns( hw, wow );
rtlhal->driver_is_goingto_unload = true;
rtlhal->enter_pnp_sleep = true;
rtl_lps_leave( hw );
rtl_op_stop( hw );
device_set_wakeup_enable( wiphy_dev( hw->wiphy ), true );
return 0;
}
static int rtl_op_resume( struct ieee80211_hw *hw )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_hal *rtlhal = rtl_hal( rtlpriv );
struct rtl_mac *mac = rtl_mac( rtl_priv( hw ) );
time64_t now;
RT_TRACE( rtlpriv, COMP_POWER, DBG_DMESG, "\n" );
rtlhal->driver_is_goingto_unload = false;
rtlhal->enter_pnp_sleep = false;
rtlhal->wake_from_pnp_sleep = true;
/* to resovle s4 can not wake up*/
now = ktime_get_real_seconds();
if ( now - rtlhal->last_suspend_sec < 5 )
return -1;
rtl_op_start( hw );
device_set_wakeup_enable( wiphy_dev( hw->wiphy ), false );
ieee80211_resume_disconnect( mac->vif );
rtlhal->wake_from_pnp_sleep = false;
return 0;
}
#endif
static int rtl_op_config( struct ieee80211_hw *hw, u32 changed )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_phy *rtlphy = &( rtlpriv->phy );
struct rtl_mac *mac = rtl_mac( rtl_priv( hw ) );
struct rtl_ps_ctl *ppsc = rtl_psc( rtl_priv( hw ) );
struct ieee80211_conf *conf = &hw->conf;
if ( mac->skip_scan )
return 1;
mutex_lock( &rtlpriv->locks.conf_mutex );
if ( changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL ) { /* BIT( 2 )*/
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n" );
}
/*For IPS */
if ( changed & IEEE80211_CONF_CHANGE_IDLE ) {
if ( hw->conf.flags & IEEE80211_CONF_IDLE )
rtl_ips_nic_off( hw );
else
rtl_ips_nic_on( hw );
} else {
/*
*although rfoff may not cause by ips, but we will
*check the reason in set_rf_power_state function
*/
if ( unlikely( ppsc->rfpwr_state == ERFOFF ) )
rtl_ips_nic_on( hw );
}
/*For LPS */
if ( ( changed & IEEE80211_CONF_CHANGE_PS ) &&
rtlpriv->psc.swctrl_lps && !rtlpriv->psc.fwctrl_lps ) {
cancel_delayed_work( &rtlpriv->works.ps_work );
cancel_delayed_work( &rtlpriv->works.ps_rfon_wq );
if ( conf->flags & IEEE80211_CONF_PS ) {
rtlpriv->psc.sw_ps_enabled = true;
/* sleep here is must, or we may recv the beacon and
* cause mac80211 into wrong ps state, this will cause
* power save nullfunc send fail, and further cause
* pkt loss, So sleep must quickly but not immediatly
* because that will cause nullfunc send by mac80211
* fail, and cause pkt loss, we have tested that 5mA
* is worked very well */
if ( !rtlpriv->psc.multi_buffered )
queue_delayed_work( rtlpriv->works.rtl_wq,
&rtlpriv->works.ps_work,
MSECS( 5 ) );
} else {
rtl_swlps_rf_awake( hw );
rtlpriv->psc.sw_ps_enabled = false;
}
}
if ( changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS ) {
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
hw->conf.long_frame_max_tx_count );
/* brought up everything changes ( changed == ~0 ) indicates first
* open, so use our default value instead of that of wiphy.
*/
if ( changed != ~0 ) {
mac->retry_long = hw->conf.long_frame_max_tx_count;
mac->retry_short = hw->conf.long_frame_max_tx_count;
rtlpriv->cfg->ops->set_hw_reg( hw, HW_VAR_RETRY_LIMIT,
( u8 * )( &hw->conf.long_frame_max_tx_count ) );
}
}
if ( changed & IEEE80211_CONF_CHANGE_CHANNEL &&
!rtlpriv->proximity.proxim_on ) {
struct ieee80211_channel *channel = hw->conf.chandef.chan;
enum nl80211_chan_width width = hw->conf.chandef.width;
enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
u8 wide_chan = ( u8 ) channel->hw_value;
/* channel_type is for 20&40M */
if ( width < NL80211_CHAN_WIDTH_80 )
channel_type =
cfg80211_get_chandef_type( &hw->conf.chandef );
if ( mac->act_scanning )
mac->n_channels++;
if ( rtlpriv->dm.supp_phymode_switch &&
mac->link_state < MAC80211_LINKED &&
!mac->act_scanning ) {
if ( rtlpriv->cfg->ops->chk_switch_dmdp )
rtlpriv->cfg->ops->chk_switch_dmdp( hw );
}
/*
*because we should back channel to
*current_network.chan in in scanning,
*So if set_chan == current_network.chan
*we should set it.
*because mac80211 tell us wrong bw40
*info for cisco1253 bw20, so we modify
*it here based on UPPER & LOWER
*/
if ( width >= NL80211_CHAN_WIDTH_80 ) {
if ( width == NL80211_CHAN_WIDTH_80 ) {
u32 center = hw->conf.chandef.center_freq1;
u32 primary =
( u32 )hw->conf.chandef.chan->center_freq;
rtlphy->current_chan_bw =
HT_CHANNEL_WIDTH_80;
mac->bw_80 = true;
mac->bw_40 = true;
if ( center > primary ) {
mac->cur_80_prime_sc =
PRIME_CHNL_OFFSET_LOWER;
if ( center - primary == 10 ) {
mac->cur_40_prime_sc =
PRIME_CHNL_OFFSET_UPPER;
wide_chan += 2;
} else if ( center - primary == 30 ) {
mac->cur_40_prime_sc =
PRIME_CHNL_OFFSET_LOWER;
wide_chan += 6;
}
} else {
mac->cur_80_prime_sc =
PRIME_CHNL_OFFSET_UPPER;
if ( primary - center == 10 ) {
mac->cur_40_prime_sc =
PRIME_CHNL_OFFSET_LOWER;
wide_chan -= 2;
} else if ( primary - center == 30 ) {
mac->cur_40_prime_sc =
PRIME_CHNL_OFFSET_UPPER;
wide_chan -= 6;
}
}
}
} else {
switch ( channel_type ) {
case NL80211_CHAN_HT20:
case NL80211_CHAN_NO_HT:
/* SC */
mac->cur_40_prime_sc =
PRIME_CHNL_OFFSET_DONT_CARE;
rtlphy->current_chan_bw =
HT_CHANNEL_WIDTH_20;
mac->bw_40 = false;
mac->bw_80 = false;
break;
case NL80211_CHAN_HT40MINUS:
/* SC */
mac->cur_40_prime_sc =
PRIME_CHNL_OFFSET_UPPER;
rtlphy->current_chan_bw =
HT_CHANNEL_WIDTH_20_40;
mac->bw_40 = true;
mac->bw_80 = false;
/*wide channel */
wide_chan -= 2;
break;
case NL80211_CHAN_HT40PLUS:
/* SC */
mac->cur_40_prime_sc =
PRIME_CHNL_OFFSET_LOWER;
rtlphy->current_chan_bw =
HT_CHANNEL_WIDTH_20_40;
mac->bw_40 = true;
mac->bw_80 = false;
/*wide channel */
wide_chan += 2;
break;
default:
mac->bw_40 = false;
mac->bw_80 = false;
pr_err( "switch case %#x not processed\n",
channel_type );
break;
}
}
if ( wide_chan <= 0 )
wide_chan = 1;
/* In scanning, when before we offchannel we may send a ps=1
* null to AP, and then we may send a ps = 0 null to AP quickly,
* but first null may have caused AP to put lots of packet to
* hw tx buffer. These packets must be tx'd before we go off
* channel so we must delay more time to let AP flush these
* packets before going offchannel, or dis-association or
* delete BA will be caused by AP
*/
if ( rtlpriv->mac80211.offchan_delay ) {
rtlpriv->mac80211.offchan_delay = false;
mdelay( 50 );
}
rtlphy->current_channel = wide_chan;
rtlpriv->cfg->ops->switch_channel( hw );
rtlpriv->cfg->ops->set_channel_access( hw );
rtlpriv->cfg->ops->set_bw_mode( hw, channel_type );
}
mutex_unlock( &rtlpriv->locks.conf_mutex );
return 0;
}
static void rtl_op_configure_filter( struct ieee80211_hw *hw,
unsigned int changed_flags,
unsigned int *new_flags, u64 multicast )
{
bool update_rcr = false;
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_mac *mac = rtl_mac( rtl_priv( hw ) );
*new_flags &= RTL_SUPPORTED_FILTERS;
if ( 0 == changed_flags )
return;
/*TODO: we disable broadcase now, so enable here */
if ( changed_flags & FIF_ALLMULTI ) {
if ( *new_flags & FIF_ALLMULTI ) {
mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
rtlpriv->cfg->maps[MAC_RCR_AB];
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"Enable receive multicast frame\n" );
} else {
mac->rx_conf &= ~( rtlpriv->cfg->maps[MAC_RCR_AM] |
rtlpriv->cfg->maps[MAC_RCR_AB] );
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"Disable receive multicast frame\n" );
}
update_rcr = true;
}
if ( changed_flags & FIF_FCSFAIL ) {
if ( *new_flags & FIF_FCSFAIL ) {
mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"Enable receive FCS error frame\n" );
} else {
mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"Disable receive FCS error frame\n" );
}
if ( !update_rcr )
update_rcr = true;
}
/* if ssid not set to hw don't check bssid
* here just used for linked scanning, & linked
* and nolink check bssid is set in set network_type
*/
if ( changed_flags & FIF_BCN_PRBRESP_PROMISC &&
mac->link_state >= MAC80211_LINKED ) {
if ( mac->opmode != NL80211_IFTYPE_AP &&
mac->opmode != NL80211_IFTYPE_MESH_POINT ) {
if ( *new_flags & FIF_BCN_PRBRESP_PROMISC )
rtlpriv->cfg->ops->set_chk_bssid( hw, false );
else
rtlpriv->cfg->ops->set_chk_bssid( hw, true );
if ( update_rcr )
update_rcr = false;
}
}
if ( changed_flags & FIF_CONTROL ) {
if ( *new_flags & FIF_CONTROL ) {
mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"Enable receive control frame.\n" );
} else {
mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"Disable receive control frame.\n" );
}
if ( !update_rcr )
update_rcr = true;
}
if ( changed_flags & FIF_OTHER_BSS ) {
if ( *new_flags & FIF_OTHER_BSS ) {
mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"Enable receive other BSS's frame.\n" );
} else {
mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_LOUD,
"Disable receive other BSS's frame.\n" );
}
if ( !update_rcr )
update_rcr = true;
}
if ( update_rcr )
rtlpriv->cfg->ops->set_hw_reg( hw, HW_VAR_RCR,
( u8 * )( &mac->rx_conf ) );
}
static int rtl_op_sta_add( struct ieee80211_hw *hw,
struct ieee80211_vif *vif,
struct ieee80211_sta *sta )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_hal *rtlhal = rtl_hal( rtl_priv( hw ) );
struct rtl_mac *mac = rtl_mac( rtl_priv( hw ) );
struct rtl_sta_info *sta_entry;
if ( sta ) {
sta_entry = ( struct rtl_sta_info * )sta->drv_priv;
spin_lock_bh( &rtlpriv->locks.entry_list_lock );
list_add_tail( &sta_entry->list, &rtlpriv->entry_list );
spin_unlock_bh( &rtlpriv->locks.entry_list_lock );
if ( rtlhal->current_bandtype == BAND_ON_2_4G ) {
sta_entry->wireless_mode = WIRELESS_MODE_G;
if ( sta->supp_rates[0] <= 0xf )
sta_entry->wireless_mode = WIRELESS_MODE_B;
if ( sta->ht_cap.ht_supported )
sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
if ( vif->type == NL80211_IFTYPE_ADHOC )
sta_entry->wireless_mode = WIRELESS_MODE_G;
} else if ( rtlhal->current_bandtype == BAND_ON_5G ) {
sta_entry->wireless_mode = WIRELESS_MODE_A;
if ( sta->ht_cap.ht_supported )
sta_entry->wireless_mode = WIRELESS_MODE_N_5G;
if ( sta->vht_cap.vht_supported )
sta_entry->wireless_mode = WIRELESS_MODE_AC_5G;
if ( vif->type == NL80211_IFTYPE_ADHOC )
sta_entry->wireless_mode = WIRELESS_MODE_A;
}
/*disable cck rate for p2p*/
if ( mac->p2p )
sta->supp_rates[0] &= 0xfffffff0;
memcpy( sta_entry->mac_addr, sta->addr, ETH_ALEN );
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_DMESG,
"Add sta addr is %pM\n", sta->addr );
rtlpriv->cfg->ops->update_rate_tbl( hw, sta, 0, true );
}
return 0;
}
static int rtl_op_sta_remove( struct ieee80211_hw *hw,
struct ieee80211_vif *vif,
struct ieee80211_sta *sta )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_sta_info *sta_entry;
if ( sta ) {
RT_TRACE( rtlpriv, COMP_MAC80211, DBG_DMESG,
"Remove sta addr is %pM\n", sta->addr );
sta_entry = ( struct rtl_sta_info * )sta->drv_priv;
sta_entry->wireless_mode = 0;
sta_entry->ratr_index = 0;
spin_lock_bh( &rtlpriv->locks.entry_list_lock );
list_del( &sta_entry->list );
spin_unlock_bh( &rtlpriv->locks.entry_list_lock );
}
return 0;
}
static int _rtl_get_hal_qnum( u16 queue )
{
int qnum;
switch ( queue ) {
case 0:
qnum = AC3_VO;
break;
case 1:
qnum = AC2_VI;
break;
case 2:
qnum = AC0_BE;
break;
case 3:
qnum = AC1_BK;
break;
default:
qnum = AC0_BE;
break;
}
return qnum;
}
/*
*for mac80211 VO = 0, VI = 1, BE = 2, BK = 3
*for rtl819x BE = 0, BK = 1, VI = 2, VO = 3
*/
static int rtl_op_conf_tx( struct ieee80211_hw *hw,
struct ieee80211_vif *vif, u16 queue,
const struct ieee80211_tx_queue_params *param )
{
struct rtl_priv *rtlpriv = rtl_priv( hw );
struct rtl_mac *mac = rtl_mac( rtl_priv( hw ) );
int aci;
if ( queue >= AC_MAX ) {
RT_TRACE( rtlpriv, COMP_ERR, DBG_WARNING,
"queue number %d is incorrect!\n", queue );
return -EINVAL;
}
aci = _rtl_get_hal_qnum( queue );
mac->ac[aci].aifs = param->aifs;
mac->ac[aci].cw_min = cpu_to_le16( param->cw_min );
mac->ac[aci].cw_max = cpu_to_le16( param->cw_max );
mac->ac[aci].tx_op = cpu_to_le16( param->txop );