forked from gtxaspec/wq9001-wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_drv.c
executable file
·4507 lines (3886 loc) · 141 KB
/
wifi_drv.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
#include <linux/module.h>
#include <net/cfg80211.h>
#include <linux/skbuff.h>
#include <linux/workqueue.h> /* work_struct */
#include <linux/semaphore.h>
#include <linux/version.h>
#include <linux/netdevice.h>
#include <linux/wireless.h>
#include <linux/if_arp.h>
#include <net/iw_handler.h>
#include <linux/proc_fs.h>
#include "usb.h"
#include "wq_ipc.h"
#include "wuqi_cfg80211_port.h"
#include "wifi_drv.h"
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/etherdevice.h>
#define WIPHY_NAME "phy0"
#define NDEV_NAME "hawk%d"
#ifdef TASKNAME_PRINT
#define iot_printf(fmt, ...) \
do { \
pr_cont("[%d][%s] ",current->pid,current->comm); \
pr_cont(fmt, ##__VA_ARGS__); \
} while (0)
#else
#define iot_printf printk
#endif
extern int wq_get_desc_size(void);
//For netdevic tx packet headroom/tailroom configuration
#define TXPKT_MAC_HEADROOM_NEEDED (32 + wq_get_desc_size()) //sizeof(DESC) + 8(IV) + 24(3->11 header, 36-12)
#define TXPKT_MAC_TAILROOM_NEEDED 12 //8(MIC) + 4(ICV)
#define SSID_MAX_LEN 32
#define WQ_MEM_GFP in_interrupt() ? GFP_ATOMIC : GFP_KERNEL
static struct wuqi_vif_context *g_vif_ctx = NULL;
static struct wuqi_vif_context *wuqi_create_context(void *wq_ipc);
static struct wuqi_vif_context *wuqi_create_context_vif(const char *name, struct wiphy *wiphy, enum nl80211_iftype type, uint8_t mac[6]);
#if (LINUX_VERSION_CODE > KERNEL_VERSION(3,0,8))
static struct wireless_dev *wuqi_create_dummy_ndev(const char *name,struct wiphy *wiphy);
#else
static struct net_device *wuqi_create_dummy_ndev(const char *name,struct wiphy *wiphy);
#endif
static int wuqi_free_dummy_ndev(struct net_device *ndev);
extern void oss_start(void);
extern void oss_stop(void);
extern void wproto_start(void);
extern void wdrv_start(void *);
extern void wdrv_stop(void);
extern void wq_set_fw_dbg(uint32_t fw_dbg);
extern int rtnl_is_locked(void);
extern void rtnl_lock(void);
extern void rtnl_unlock(void);
extern void wuqi_set_shortslottime_flag(void *vap_param);
extern void wuqi_vap_change_mode(void *, uint8_t);
extern void wuqi_send_probereq(void *vap_params, uint8_t *addr, uint8_t *ssid, size_t ssidlen);
extern void wuqi_dhcp_debug_msg(struct ethhdr *eh);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,20,0)
extern void wuqi_config_chan_power(void *vap_params, uint32_t *freq, int *pwr, uint16_t ch_nbr);
#else
extern void wuqi_config_chan_power(void *vap_params, uint16_t *freq, int *pwr, uint16_t ch_nbr);
#endif
extern void wq_mp_tone_notify(uint8_t enable, uint16_t freq, uint8_t num, uint16_t amp);
extern int wq_mp_freq_dev_notify(uint8_t set, uint16_t *freq_dev);
extern int wq_mp_power_notify(uint8_t set, uint8_t rate_idx, int8_t *power);
extern int wq_mp_mac_notify(uint8_t set, uint8_t *mac_addr);
extern int wq_mp_crc_err_notify(uint32_t *crc_err);
extern int wq_mp_power_ctrl_notify(uint8_t *power_ctrl);
extern int wq_mp_cali_efuse_notify(int set, uint8_t *cali_efuse, uint8_t cal_efuse_len);
extern int wq_mp_dpd_ctrl_notify(uint8_t *dpd_ctrl);
extern int wq_mp_digit_gain_notify(uint8_t mode, uint16_t freq);
extern int wq_mp_status_notify(uint8_t status);
extern int wq_mp_kfree_cali_notify(char *ver, char *cont);
extern int wuqi_config_channel_dpd(void *vap_param, int chan_freq, int chan_info, uint8_t sw_ch_cali);
extern void wuqi_user_chan_power(void *vap_params, int *freq, uint8_t *power, int ch_nbr);
extern bool wuqi_chan_power_valid(void *vap_params, uint16_t freq);
extern int wq_mp_cali_remain_times_notify(uint8_t *remain_times);
extern int wq_rate_pwr_notify(int8_t *pwr_list);
extern int wq_rate_pwr_srrc_notify(int8_t *pwr_list);
extern void wuqi_chan_freq_set(void *vap_params, u32 first_freq, u32 last_freq);
extern int wq_mp_socinfo_notify(uint8_t *soc_info);
extern int wq_mp_gain_notify(uint8_t gain_info);
extern int wq_mp_gain_ctrl_notify(uint8_t gain_ctrl_info);
extern int wq_mp_get_dpdgain_notify(uint8_t *dpd_gain_info);
extern int wq_mp_set_gain_notify(uint32_t *ref_gain);
#ifndef WQ_BUS_TEST
extern uint32_t WQ_MAC_READ(uint32_t addr);
extern void WQ_MAC_WRITE(uint32_t addr, uint32_t val);
#endif
extern void wq_proc_tx_monitor(long tx_count, int phy_perf_flag, int pkt_len, int tx_ms, int tx_us);
extern void vap_attach_to_new_ic(void *vap_param);
extern int wuqi_change_channel_dpd();
#if WQ_USB_RX_TYPE == WQ_USB_RX_CPU
extern int submit_rxurb_limit_h;
extern int submit_rxurb_limit_l;
extern int rx_skb_count_limit;
extern int rx_done_count_wk_limit;
#endif
#if LINUX_VERSION_CODE == KERNEL_VERSION(4,4,192)
extern bool wq_wifi_detach_flag;
extern bool wq_wifi_detach_end_flag;
#endif
int rx_start = 0;
uint32_t rxcount = 0;
int8_t init_rate_pwr_list[12] = {
0, //OFDM_MCS0
0, //OFDM_MCS1
0, //OFDM_MCS2
0, //OFDM_MCS3
0, //OFDM_MCS4
0, //OFDM_MCS5
0, //OFDM_MCS6
0, //OFDM_MCS7
0, //DSSS_1M
0, //DSSS_2M
0, //DSSS_5M
0, //DSSS_11M
};
int inject_other_types = 0;
u16 wq_dbg_flag = WQ_DEBUG_ALL;
bool wq_drv_rmmod_flag = false;
module_param(wq_dbg_flag, short, S_IRUGO);
MODULE_PARM_DESC(wq_dbg_flag, "dbg_flag");
char *country;
module_param(country, charp, S_IRUGO);
MODULE_PARM_DESC(country, "country code");
int sgi_rx_enable=1, sgi_tx_enable=1;
module_param(sgi_rx_enable, int, S_IRUGO);
module_param(sgi_tx_enable, int, S_IRUGO);
u8 monitor_tx_flag = 0;
static struct net_device * g_wlan1_mon_ndev=NULL;
static struct net *g_wlan1_mon_net=NULL;
int send_keepalive_flag =1;
struct log_2_file log2file;
char *cache_log_buf1;
char *cache_log_buf2;
struct wuqi_mptest_context{
uint32_t rx_ce_start_cnt;
uint32_t rx_ce_stop_cnt;
uint32_t rx_crc_err_cnt;
int rx_reset_flag;
uint8_t mac_addr[ETH_ALEN];
struct wuqi_mptest_efuse_ctx efuse_ctx;
};
//statistic pkt of krn_drv, drv_fw and fw ack
atomic_t krn_drv_pkt_cnt ;
atomic_t drv_fw_pkt_cnt;
atomic_t fw_ack_cnt;
atomic_t drop_pkt_cnt;
struct wuqi_mptest_context mp_ctxt;
struct wuqi_vif_context {
struct wiphy *wiphy;
struct net_device *ndev;
uint32_t vif_ctx_state;
uint32_t if_up;
struct semaphore sem;
struct work_struct ws_connect;
struct work_struct ws_set_bitrate;
struct work_struct ws_set_txq_params;
struct ieee80211_txq_params txq_params;
u8 connecting_bssid[ETH_ALEN];
char connecting_ssid[SSID_MAX_LEN];
struct work_struct ws_disconnect;
u16 disconnect_reason_code;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,20,0)
u32 scanreq_channels_conf[WUQI_MAX_CHANNELS];
#else
u16 scanreq_channels_conf[WUQI_MAX_CHANNELS];
#endif
int scanreq_channels_pwr[WUQI_MAX_CHANNELS];
u16 scanreq_n_channels;
struct work_struct ws_scan;
struct delayed_work ws_scan_delayed;
struct cfg80211_scan_request *scan_request;
void *vap;
u16 mgmt_rx_reg;
u8 mac_addr[ETH_ALEN];
u8 bssid[ETH_ALEN];
/*ap params*/
char ap_ssid_len;
char ap_ssid[IMAX_SSID_LEN];
int dtim_period;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
enum nl80211_hidden_ssid hidden_ssid;
#endif
u32 wpa_versions;
u32 cipher_group;
int n_ciphers_pairwise;
enum nl80211_auth_type auth_type;
bool privacy;
struct wuqi_cfg80211_chan chan;
struct wuqi_mac_rateset rateset;
struct wuqi_cfg80211_beacon bcn;
char password[IMAX_PASSWORD_LEN];
/* statistics */
struct iw_statistics iwstats;
};
struct wuqi_wiphy_priv_context {
void *wq_ipc;
struct wuqi_vif_context *vif_ctx;
};
struct wuqi_ndev_priv_context {
struct wuqi_vif_context *vif_ctx;
struct wireless_dev wdev;
};
static struct wuqi_vif_context *g_vif_ctx0=NULL, *g_vif_ctx1 = NULL;
/* helper function that will retrieve main context from "priv" data of the wiphy */
static struct wuqi_wiphy_priv_context *
wiphy_get_vif_context(struct wiphy *wiphy) { return (struct wuqi_wiphy_priv_context *) wiphy_priv(wiphy); }
/* helper function that will retrieve main context from "priv" data of the network device */
static struct wuqi_ndev_priv_context *
ndev_get_vif_context(struct net_device *ndev) { return (struct wuqi_ndev_priv_context *) netdev_priv(ndev); }
static const u32 wuqi_cipher_suites[] = {
WLAN_CIPHER_SUITE_WEP40,
WLAN_CIPHER_SUITE_WEP104,
WLAN_CIPHER_SUITE_TKIP,
WLAN_CIPHER_SUITE_CCMP,
/* Keep as last entry: */
WLAN_CIPHER_SUITE_AES_CMAC
};
static const struct ieee80211_txrx_stypes
wuqi_txrx_stypes[NUM_NL80211_IFTYPES] = {
[NL80211_IFTYPE_STATION] = {
.tx = 0xffff,
.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
BIT(IEEE80211_STYPE_DISASSOC >> 4) |
BIT(IEEE80211_STYPE_AUTH >> 4) |
BIT(IEEE80211_STYPE_DEAUTH >> 4) |
BIT(IEEE80211_STYPE_ACTION >> 4)
},
[NL80211_IFTYPE_AP] = {
.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
BIT(IEEE80211_STYPE_DISASSOC >> 4) |
BIT(IEEE80211_STYPE_AUTH >> 4) |
BIT(IEEE80211_STYPE_DEAUTH >> 4) |
BIT(IEEE80211_STYPE_ACTION >> 4)
},
[NL80211_IFTYPE_P2P_CLIENT] = {
.tx = 0xffff,
.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
},
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
[NL80211_IFTYPE_P2P_DEVICE] = {
.tx = 0xffff,
.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
},
#endif
[NL80211_IFTYPE_P2P_GO] = {
.tx = 0xffff,
.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
BIT(IEEE80211_STYPE_DISASSOC >> 4) |
BIT(IEEE80211_STYPE_AUTH >> 4) |
BIT(IEEE80211_STYPE_DEAUTH >> 4) |
BIT(IEEE80211_STYPE_ACTION >> 4)
}
};
void wq_free_skb(struct sk_buff *skb)
{
dev_kfree_skb_any(skb);
}
u32 *wq_mem_alloc(int len, int zero)
{
gfp_t gfp;
u32 *addr_mem = NULL;
gfp = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
if (zero)
addr_mem = kzalloc(len, gfp);
else
addr_mem = kmalloc(len, gfp);
return addr_mem;
}
struct sk_buff *wq_alloc_skb(unsigned int length)
{
return __dev_alloc_skb(length, WQ_MEM_GFP);
}
int wuqi_tx_mgmt_status(void *vap_ctx_param, char *buf, int size)
{
struct wuqi_vif_context *vif_ctx = (struct wuqi_vif_context *)vap_ctx_param;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
struct wuqi_ndev_priv_context *ndev_data = ndev_get_vif_context(vif_ctx->ndev);
cfg80211_mgmt_tx_status(&ndev_data->wdev, 0, buf, size, 1, WQ_MEM_GFP);
#else
cfg80211_mgmt_tx_status(vif_ctx->ndev, 0, buf, size, 1, WQ_MEM_GFP);
#endif
iot_printf("[%s:%d]\n",__func__,__LINE__);
return 0;
}
#if 0
int wuqi_rx_mgmt(void *vap_ctx_param, char *buf, int size)
{
struct wuqi_vif_context *vif_ctx = (struct wuqi_vif_context *)vap_ctx_param;
struct wuqi_ndev_priv_context *ndev_data = ndev_get_vif_context(vif_ctx->ndev);
struct wuqi_chan_info channel_info;
iot_printf("[%s:%d]\n",__func__,__LINE__);
dump_bytes("wuqi_rx_mgmt",buf,size);
wuqi_get_channel_params(vif_ctx->vap, vif_ctx->vif_ctx_state, &channel_info);
cfg80211_rx_mgmt(&ndev_data->wdev,channel_info.center_freq,0,buf,size,0);
return 0;
}
#else
int wuqi_rx_mgmt(void *vap_ctx_param, char *buf, int size)
{
struct sk_buff *skb_for_route=NULL;
u8 rtap_hdr[] = {
0x00, 0x00, /* radiotap version */
0x0e, 0x00, /* radiotap length */
0x00, 0x00, 0x00, 0x00, /* bmap: flags, tx and rx flags */
0x8, /* F_FRAG (fragment if required) */
0x00, /* padding */
0x00, 0x00, /* RX and TX flags to indicate that */
0x00, 0x00, /* this is the injected frame directly */
};
iot_printf("[%s:%d]\n",__func__,__LINE__);
if(!g_wlan1_mon_ndev)
return 0;
skb_for_route=wq_alloc_skb(1500);
skb_for_route->dev = g_wlan1_mon_ndev;
memcpy(skb_for_route->data,rtap_hdr,sizeof(rtap_hdr));
memcpy(skb_for_route->data+sizeof(rtap_hdr),buf,size);
skb_for_route->len=sizeof(rtap_hdr)+size;
skb_reset_mac_header(skb_for_route);
skb_for_route->ip_summed = CHECKSUM_UNNECESSARY;
skb_for_route->pkt_type = PACKET_OTHERHOST;
skb_for_route->protocol = htons(ETH_P_802_2);
memset(skb_for_route->cb, 0, sizeof(skb_for_route->cb));
dump_bytes("skb_for_route->data",skb_for_route->data,skb_for_route->len);
netif_receive_skb(skb_for_route);
return 0;
}
int wuqi_rx_monitor(void *vap_ctx_param, void *buf, int size, uint32_t flags)
{
struct wuqi_vif_context *vif_ctx = (struct wuqi_vif_context *)vap_ctx_param;
struct sk_buff *skb=NULL;
if (flags & M_AMSDU)
skb = wq_alloc_skb(SKB_MAX_LEN);
else
skb = wq_alloc_skb(1800);
if(skb == NULL) {
iot_printf("wuqi_rx_monitor : no buff\n");
return 0;
}
skb->dev = vif_ctx->ndev;
memcpy(skb->data, buf, size);
skb->len=size;
skb_reset_mac_header(skb);
memset(skb->cb, 0, sizeof(skb->cb));
//dump_bytes("skb->data", skb->data, skb->len);
netif_receive_skb(skb);
return 0;
}
#endif
int wuqi_mic_fail_notify(void *vap_ctx_param, char *mac, int key_id)
{
struct wuqi_vif_context *vif_ctx = (struct wuqi_vif_context *)vap_ctx_param;
iot_printf("[%s:%d]\n",__func__,__LINE__);
cfg80211_michael_mic_failure(vif_ctx->ndev,mac,NL80211_KEYTYPE_PAIRWISE ,key_id, NULL, WQ_MEM_GFP);
return 0;
}
int wuqi_new_sta_notify(void *vap_ctx_param, char *mac, char *ies, int ies_len)
{
struct wuqi_vif_context *vif_ctx = (struct wuqi_vif_context *)vap_ctx_param;
struct station_info sinfo={0};
iot_printf("%s:%d\n",__func__,__LINE__);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
sinfo.assoc_req_ies = ies;
sinfo.assoc_req_ies_len = ies_len;
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0) && LINUX_VERSION_CODE < KERNEL_VERSION(4,0,0))
sinfo.filled |= STATION_INFO_ASSOC_REQ_IES;
#endif
cfg80211_new_sta(vif_ctx->ndev,mac, &sinfo, WQ_MEM_GFP);
return 0;
}
int wuqi_del_sta_notify(void *vap_ctx_param, char *mac)
{
struct wuqi_vif_context *vif_ctx = (struct wuqi_vif_context *)vap_ctx_param;
iot_printf("%s:%d\n",__func__,__LINE__);
cfg80211_del_sta(vif_ctx->ndev, mac, WQ_MEM_GFP);
return 0;
}
#define NUM_OF_CHANNEL_OF_MICRO_SCAN 4
void build_scan_channel_list(struct wuqi_vif_context *vif_ctx, u16 *chan_list, u16 *n_chan)
{
int i;
*n_chan = 0;
for (i=0; i<NUM_OF_CHANNEL_OF_MICRO_SCAN; i++) {
if (vif_ctx->scanreq_n_channels > 0)
{
chan_list[i] = vif_ctx->scanreq_channels_conf[vif_ctx->scanreq_n_channels-1];
(*n_chan)++;
vif_ctx->scanreq_n_channels--;
}
else
break;
//iot_printf("build_scan_channel_list %d:%d\n", *n_chan, chan_list[i]);
}
}
static void wuqi_scan_routine(struct work_struct *w)
{
struct wuqi_vif_context *vif_ctx = container_of(w, struct wuqi_vif_context, ws_scan);
u16 chan_list[WUQI_MAX_CHANNELS];
u16 n_chan;
iot_printf("[wuqi_scan_routine:%p]\n", vif_ctx->vap);
if(vif_ctx->vap){
if(vif_ctx->scan_request == NULL)
{
iot_printf("in disconnecting, already report scanning done!\n");
return;
}
build_scan_channel_list(vif_ctx, chan_list, &n_chan);
wuqi_scan_req(vif_ctx->vap, vif_ctx->scan_request->ssids->ssid,
vif_ctx->scan_request->ssids->ssid_len,
chan_list, n_chan);
}
else{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
static struct cfg80211_scan_info info = {
/* if scan was aborted by user(calling cfg80211_ops->abort_scan) or by any driver/hardware issue - field should be set to "true"*/
.aborted = false,
};
#endif
msleep(100);
/* inform with dummy BSS */
//inform_dummy_bss(vif_ctx);
if(down_interruptible(&vif_ctx->sem)) {
return;
}
/* finish scan */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
cfg80211_scan_done(vif_ctx->scan_request, &info);
#else
cfg80211_scan_done(vif_ctx->scan_request, false);
#endif
vif_ctx->scan_request = NULL;
up(&vif_ctx->sem);
}
}
static void wuqi_scan_delay_routine(struct work_struct *w)
{
struct wuqi_vif_context *vif_ctx = container_of(w, struct wuqi_vif_context, ws_scan_delayed.work);
u16 chan_list[WUQI_MAX_CHANNELS];
u16 n_chan;
if((vif_ctx != NULL) && (vif_ctx->vap != NULL) &&
(vif_ctx->scan_request != NULL)){
//iot_printf("wuqi_scan_delay_routine:%p\n", vif_ctx->vap);
build_scan_channel_list(vif_ctx, chan_list, &n_chan);
wuqi_scan_req(vif_ctx->vap, vif_ctx->scan_request->ssids->ssid,
vif_ctx->scan_request->ssids->ssid_len,
chan_list, n_chan);
}
else{
iot_printf("wuqi_scan_delay_routine: fatal error\n");
}
}
void update_scan_cancel_info(void *vif_ctx_cxt)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
static struct cfg80211_scan_info info = {
/* if scan was aborted by user(calling cfg80211_ops->abort_scan) or by any driver/hardware issue - field should be set to "true"*/
.aborted = false,
};
#endif
struct wuqi_vif_context *vif_ctx = vif_ctx_cxt;
if (vif_ctx->vif_ctx_state & VIF_STATE_SCANNING)
{
iot_printf("In scanning, report scanning cancel!\n");
/* finish scan */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
cfg80211_scan_done(vif_ctx->scan_request, &info);
#else
cfg80211_scan_done(vif_ctx->scan_request, false);
#endif
vif_ctx->vif_ctx_state &= (~VIF_STATE_SCANNING);
vif_ctx->scanreq_n_channels = 0;
vif_ctx->scan_request = NULL;
}
}
void update_bss_info(void *vif_ctx_cxt, char *bssid, char *ie, int ie_len, int chan, uint16_t bcn_interval, uint16_t cap, int last, int signal)
{
struct cfg80211_bss *bss = NULL;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)
struct cfg80211_inform_bss data = {
.scan_width = NL80211_BSS_CHAN_WIDTH_20,
.signal = signal*100,
};
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
static struct cfg80211_scan_info info = {
/* if scan was aborted by user(calling cfg80211_ops->abort_scan) or by any driver/hardware issue - field should be set to "true"*/
.aborted = false,
};
#endif
struct wuqi_vif_context *vif_ctx;
vif_ctx = vif_ctx_cxt;
if (last == 1)
{
iot_printf("call cfg80211_scan_done %p\n", vif_ctx->scan_request);
if (vif_ctx->scan_request != NULL)
{
if (vif_ctx->vif_ctx_state & VIF_STATE_SCANNING)
{
/* finish scan */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
cfg80211_scan_done(vif_ctx->scan_request, &info);
#else
cfg80211_scan_done(vif_ctx->scan_request, false);
#endif
vif_ctx->vif_ctx_state &= (~VIF_STATE_SCANNING);
}
else
{
iot_printf("Error : vif_ctx_state not in VIF_STATE_SCANNING\n");
}
vif_ctx->scan_request = NULL;
}
return;
}
if ((chan >= 1) && (chan <= 14))
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)
data.chan = &vif_ctx->wiphy->bands[NL80211_BAND_2GHZ]->channels[chan-1];
//iot_printf("update_bss_info %02x:%02x:%02x:%02x:%02x:%02x, ie_len=%d\n", (u_char)bssid[0], (u_char)bssid[1], (u_char)bssid[2], (u_char)bssid[3], (u_char)bssid[4], (u_char)bssid[5], ie_len);
//for (i=0; i<ie_len; i++)
// iot_printf("%02x ", ie[i]);
/* also it posible to use cfg80211_inform_bss() instead of cfg80211_inform_bss_data() */
bss = cfg80211_inform_bss_data(vif_ctx->wiphy, &data, CFG80211_BSS_FTYPE_UNKNOWN, bssid, 0, cap, bcn_interval,
ie, ie_len, WQ_MEM_GFP);
#else
//channel = vif_ctx->wiphy->bands[NL80211_BAND_2GHZ]->channels[chan-1];
//iot_printf("update_bss_info %02x:%02x:%02x:%02x:%02x:%02x, ie_len=%d\n", (u_char)bssid[0], (u_char)bssid[1], (u_char)bssid[2], (u_char)bssid[3], (u_char)bssid[4], (u_char)bssid[5], ie_len);
//for (i=0; i<ie_len; i++)
// iot_printf("%02x ", ie[i]);
/* also it posible to use cfg80211_inform_bss() instead of cfg80211_inform_bss_data() */
bss = cfg80211_inform_bss(vif_ctx->wiphy, &(vif_ctx->wiphy->bands[NL80211_BAND_2GHZ]->channels[chan-1]), bssid, 0, cap, bcn_interval,
ie, ie_len, signal*100, WQ_MEM_GFP);
#endif
/* free, cfg80211_inform_bss_data() returning cfg80211_bss structure refcounter of which should be decremented if its not used. */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
cfg80211_put_bss(vif_ctx->wiphy, bss);
#else
cfg80211_put_bss(bss);
#endif
}
}
void update_connection_info(void *vif_ctx_ctx, uint8_t *bssid, uint8_t *essid, uint8_t essid_len)
{
struct wuqi_vif_context *vif_ctx;
vif_ctx = vif_ctx_ctx;
iot_printf("in update_connection_info, call cfg80211_connect_result bssid=%02x-%02x-%02x-%02x-%02x-%02x\n", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
if (vif_ctx->vif_ctx_state & VIF_STATE_CONNECTING)
{
vif_ctx->vif_ctx_state &= (~VIF_STATE_CONNECTING);
vif_ctx->vif_ctx_state |= VIF_STATE_CONNECTED;
memcpy(vif_ctx->bssid,bssid,ETH_ALEN);
vif_ctx->ap_ssid_len=essid_len;
memcpy(vif_ctx->ap_ssid,essid,essid_len);
cfg80211_connect_result(vif_ctx->ndev, bssid, NULL, 0, NULL, 0, 0, WQ_MEM_GFP);
netif_carrier_on(vif_ctx->ndev);
netif_wake_queue(vif_ctx->ndev);
}
else
{
iot_printf("Error : 1. vif_ctx_state not in VIF_STATE_CONNECTING\n");
}
}
void update_connection_fail_info(void *vif_ctx_ctx)
{
struct wuqi_vif_context *vif_ctx;
vif_ctx = vif_ctx_ctx;
iot_printf("in update_connection_fail_info, call cfg80211_connect_result\n");
if (vif_ctx->vif_ctx_state & VIF_STATE_CONNECTING)
{
vif_ctx->vif_ctx_state &= (~VIF_STATE_CONNECTING);
cfg80211_connect_result(vif_ctx->ndev, NULL, NULL, 0, NULL, 0, WLAN_STATUS_UNSPECIFIED_FAILURE, WQ_MEM_GFP);
}
else
{
iot_printf("Error : 2. vif_ctx_state not in VIF_STATE_CONNECTING\n");
}
}
void update_disconnection_info(void *vif_ctx_ctx)
{
struct wuqi_vif_context *vif_ctx;
vif_ctx = vif_ctx_ctx;
iot_printf("call cfg80211_disconnected\n");
if (vif_ctx->vif_ctx_state & VIF_STATE_CONNECTED)
{
update_scan_cancel_info(vif_ctx_ctx);
vif_ctx->vif_ctx_state &= (~VIF_STATE_CONNECTED);
memset(vif_ctx->bssid,0,ETH_ALEN);
vif_ctx->ap_ssid_len=0;
memset(vif_ctx->ap_ssid,0,IMAX_SSID_LEN);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)
cfg80211_disconnected(vif_ctx->ndev, vif_ctx->disconnect_reason_code, NULL, 0, true, WQ_MEM_GFP);
#else
cfg80211_disconnected(vif_ctx->ndev, vif_ctx->disconnect_reason_code, NULL, 0, WQ_MEM_GFP);
#endif
netif_carrier_off(vif_ctx->ndev);
netif_stop_queue(vif_ctx->ndev);
}
else
{
iot_printf("Error : vif_ctx_state not in VIF_STATE_CONNECTED\n");
}
vif_ctx->disconnect_reason_code = 0;
}
bool wq_vif_state_is_idle(void *vif_ctx_ctx)
{
return (((struct wuqi_vif_context*)vif_ctx_ctx)->vif_ctx_state) == VIF_STATE_IDLE;
}
void wq_update_rssi_nf(void *vap_ctx_param, uint8_t rssi, uint8_t nf)
{
struct wuqi_vif_context *vif_ctx = (struct wuqi_vif_context *)vap_ctx_param;
//agv_rssi-0.5 if last_rssi<agv_rssi, agv_rssi+0.5 if last_rssi>agv_rssi
if ((int8_t) rssi > (int8_t) vif_ctx->iwstats.qual.level) {
vif_ctx->iwstats.qual.level=((int8_t) vif_ctx->iwstats.qual.level*7+(int8_t) rssi+4)/8;
}
if ((int8_t) rssi < (int8_t) vif_ctx->iwstats.qual.level) {
vif_ctx->iwstats.qual.level=((int8_t) vif_ctx->iwstats.qual.level*7+(int8_t) rssi-4)/8;
}
//iot_printf("agv:%d, last:%d\n",(int8_t) vif_ctx->iwstats.qual.level, (int8_t) rssi);
vif_ctx->iwstats.qual.noise=(vif_ctx->iwstats.qual.noise*7+nf+4)/8;
vif_ctx->iwstats.qual.updated=IW_QUAL_LEVEL_UPDATED|IW_QUAL_NOISE_UPDATED|IW_QUAL_DBM;
}
void wq_net_if_input(void *vap_ctx_param, struct sk_buff *skb, uint8_t rssi, uint8_t nf)
{
struct wuqi_vif_context *vif_ctx = (struct wuqi_vif_context *)vap_ctx_param;
struct wuqi_ndev_priv_context * ndev_data;
skb->dev = vif_ctx->ndev;
ndev_data = ndev_get_vif_context(skb->dev);
if (((vif_ctx->vif_ctx_state & VIF_STATE_CONNECTED) == VIF_STATE_CONNECTED)
|| ((vif_ctx->vif_ctx_state & VIF_STATE_AP_CONNECTED) == VIF_STATE_AP_CONNECTED)
|| ndev_data->wdev.iftype == NL80211_IFTYPE_MONITOR)
{
if (vif_ctx->ndev->flags & IFF_UP)
{
vif_ctx->ndev->stats.rx_packets++;
vif_ctx->ndev->stats.rx_bytes += (skb->len - 14);//payload only
skb->protocol = eth_type_trans(skb, skb->dev);
netif_rx_ni(skb);
}
else
{
dev_kfree_skb_any(skb);
}
}
else
{
dev_kfree_skb_any(skb);
}
}
extern void inject_packets(void *vap_ptr,int types);
static void set_bitrate_routine(struct work_struct *w)
{
struct wuqi_vif_context *vif_ctx = container_of(w, struct wuqi_vif_context, ws_set_bitrate);
iot_printf("[set_bitrate_routine:%p] inject_other_types:0x%x\n",
vif_ctx->vap,inject_other_types);
if(inject_other_types){
inject_packets(vif_ctx->vap,inject_other_types);
inject_other_types=0;
}else{
inject_packets(vif_ctx->vap,0xff);
}
}
static void set_txq_params_routine(struct work_struct *w)
{
struct wuqi_vif_context *vif_ctx = container_of(w, struct wuqi_vif_context, ws_set_txq_params);
iot_printf("[set_txq_params_routine] vif_ctx->vap:%p\n",vif_ctx->vap);
wuqi_uptate_txq_params(vif_ctx->vap);
}
static void wuqi_connect_routine(struct work_struct *w)
{
struct wuqi_vif_context *vif_ctx = container_of(w, struct wuqi_vif_context, ws_connect);
int ret;
iot_printf("[wuqi_connect_routine:%p]\n", vif_ctx->vap);
if(down_interruptible(&vif_ctx->sem)) {
iot_printf("down_interruptible failed\n");
return;
}
if(vif_ctx->vap){
extern int setmlme_assoc_sta(void *vap, const uint8_t mac[6], int ssid_len, const uint8_t ssid[32]);
//uint8_t mac[6] = {0x00, 0x48, 0x41, 0x57, 0x4b, 0x46};
uint8_t ssid[33] = {};
int ssid_len = strlen(vif_ctx->connecting_ssid);
strcpy(ssid, vif_ctx->connecting_ssid);
if(memcmp(vif_ctx->connecting_bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0)
{
ret = setmlme_assoc_sta(vif_ctx->vap, vif_ctx->connecting_bssid, ssid_len, ssid);
}
else
{
ret = setmlme_assoc_sta(vif_ctx->vap, NULL, ssid_len, ssid);
}
if (ret != 0)
{
cfg80211_connect_result(vif_ctx->ndev, NULL, NULL, 0, NULL, 0, WLAN_STATUS_UNSPECIFIED_FAILURE, WQ_MEM_GFP);
}
}
else{
//cfg80211_connect_timeout(vif_ctx->ndev, NULL, NULL, 0, GFP_KERNEL, NL80211_TIMEOUT_SCAN);
cfg80211_connect_result(vif_ctx->ndev, NULL, NULL, 0, NULL, 0, WLAN_STATUS_UNSPECIFIED_FAILURE, WQ_MEM_GFP);
}
vif_ctx->connecting_ssid[0] = 0;
up(&vif_ctx->sem);
}
extern int setmlme_disconnect(void *vap, const uint8_t mac[6], int reason);
static void wuqi_disconnect_routine(struct work_struct *w)
{
struct wuqi_vif_context *vif_ctx = container_of(w, struct wuqi_vif_context, ws_disconnect);
//uint8_t mac[6] = {0x00, 0x48, 0x41, 0x57, 0x4b, 0x46};
iot_printf("[wuqi_disconnect_routine] vif_ctx->vap:%p\n",vif_ctx->vap);
if(down_interruptible(&vif_ctx->sem)) {
iot_printf("down_interruptible failed\n");
return;
}
if(vif_ctx->vap)
setmlme_disconnect(vif_ctx->vap, NULL, 0);
//cfg80211_disconnected(vif_ctx->ndev, vif_ctx->disconnect_reason_code, NULL, 0, true, GFP_KERNEL);
//vif_ctx->disconnect_reason_code = 0;
up(&vif_ctx->sem);
}
/* callback that called by the kernel when user decided to scan.
* This callback should initiate scan routine(through work_struct) and exit with 0 if everything ok.
* Scan routine should be finished with cfg80211_scan_done() call. */
#if (LINUX_VERSION_CODE > KERNEL_VERSION(3,0,8))
static int wuqi_cfg80211_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
#else
static int wuqi_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
struct cfg80211_scan_request *request)
#endif
{
//struct wuqi_vif_context *vif_ctx = wiphy_get_vif_context(wiphy)->vif_ctx;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
struct wuqi_vif_context *vif_ctx = ndev_get_vif_context(request->wdev->netdev)->vif_ctx;
#else
struct wuqi_vif_context *vif_ctx = ndev_get_vif_context(ndev)->vif_ctx;
#endif
u32 scan_first_freq;
u32 scan_last_freq;
if (wq_drv_rmmod_flag){
iot_printf("%s: rmmod is being executed, return!\n", __func__);
return -ERESTARTSYS;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,20,0)
memset(vif_ctx->scanreq_channels_conf, 0, WUQI_MAX_CHANNELS*sizeof(u32));
#else
memset(vif_ctx->scanreq_channels_conf, 0, WUQI_MAX_CHANNELS*sizeof(u16));
#endif
// iot_printf("wuqi_cfg80211_scan n_channels=%d, %p %s\n", vif_ctx->scanreq_n_channels, request->wdev->netdev, request->wdev->netdev->name);
if(!vif_ctx->ndev){
iot_printf("%s Error!, ndev is NULL\n",__func__);
return -ERESTARTSYS;
}
if(down_interruptible(&vif_ctx->sem)) {
return -ERESTARTSYS;
}
if (vif_ctx->scan_request != NULL) {
up(&vif_ctx->sem);
return -EBUSY;
}
vif_ctx->vif_ctx_state |= VIF_STATE_SCANNING;
vif_ctx->scan_request = request;
if(request->ie_len) {
wuqi_vap_config_appie(vif_ctx->vap, IEEE80211_ADDIE_PROBE_REQ, request->ie, request->ie_len);
}
//Scan requested channels
if ((request->n_channels > 0) && (request->n_channels < WUQI_MAX_CHANNELS)) {
u8 i, ch_cnt = 0;
vif_ctx->scanreq_n_channels = request->n_channels;
//iot_printf("wuqi_cfg80211_scan: n_channels=%d\n", vif_ctx->scanreq_n_channels);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
for (i = 0; i < vif_ctx->scanreq_n_channels; i++) {
vif_ctx->scanreq_channels_conf[vif_ctx->scanreq_n_channels-i-1] = request->channels[i]->center_freq;
vif_ctx->scanreq_channels_pwr[vif_ctx->scanreq_n_channels-i-1] = request->channels[i]->max_reg_power;
}
#else
for (i = 0; i < vif_ctx->scanreq_n_channels; i++) {
vif_ctx->scanreq_channels_conf[vif_ctx->scanreq_n_channels-i-1] = request->channels[i]->center_freq;
vif_ctx->scanreq_channels_pwr[vif_ctx->scanreq_n_channels-i-1] = request->channels[i]->max_power;
}
#endif
/* update reg_power */
wuqi_config_chan_power(vif_ctx->vap, vif_ctx->scanreq_channels_conf, vif_ctx->scanreq_channels_pwr, vif_ctx->scanreq_n_channels);
/* since power may be configured by user, remove channels without power configured */
for (i = 0; i < request->n_channels; i++) {
if (wuqi_chan_power_valid(vif_ctx->vap, request->channels[i]->center_freq)) {
ch_cnt++;
}
}
vif_ctx->scanreq_n_channels = ch_cnt;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
for (i = 0; i < request->n_channels; i++) {
if (wuqi_chan_power_valid(vif_ctx->vap, request->channels[i]->center_freq)) {
vif_ctx->scanreq_channels_conf[ch_cnt - 1] = request->channels[i]->center_freq;
vif_ctx->scanreq_channels_pwr[ch_cnt - 1] = request->channels[i]->max_reg_power;
ch_cnt--;
}
}
#else
for (i = 0; i < request->n_channels; i++) {
if (wuqi_chan_power_valid(vif_ctx->vap, request->channels[i]->center_freq)) {
vif_ctx->scanreq_channels_conf[ch_cnt - 1] = request->channels[i]->center_freq;
vif_ctx->scanreq_channels_pwr[ch_cnt - 1] = request->channels[i]->max_power;
ch_cnt--;
}
}
#endif
scan_first_freq = vif_ctx->scanreq_channels_conf[vif_ctx->scanreq_n_channels - 1];