2 * Copyright (c) 2007-2008 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 /* Module Name : init.c */
20 /* This module contains init functions. */
25 /************************************************************************/
27 #include "../hal/hpreg.h"
29 extern const u8_t zcUpToAc[8];
31 u16_t zcIndextoRateBG[16] = {1000, 2000, 5500, 11000, 0, 0, 0, 0, 48000,
32 24000, 12000, 6000, 54000, 36000, 18000, 9000};
33 u32_t zcIndextoRateN20L[16] = {6500, 13000, 19500, 26000, 39000, 52000, 58500,
34 65000, 13000, 26000, 39000, 52000, 78000, 104000,
36 u32_t zcIndextoRateN20S[16] = {7200, 14400, 21700, 28900, 43300, 57800, 65000,
37 72200, 14400, 28900, 43300, 57800, 86700, 115600,
39 u32_t zcIndextoRateN40L[16] = {13500, 27000, 40500, 54000, 81000, 108000, 121500,
40 135000, 27000, 54000, 81000, 108000, 162000, 216000,
42 u32_t zcIndextoRateN40S[16] = {15000, 30000, 45000, 60000, 90000, 120000, 135000,
43 150000, 30000, 60000, 90000, 120000, 180000, 240000,
46 /************************************************************************/
48 /* FUNCTION DESCRIPTION zfTxGenWlanHeader */
49 /* Generate WLAN MAC header and LLC header. */
52 /* dev : device pointer */
53 /* buf : buffer pointer */
54 /* id : Index of TxD */
55 /* port : WLAN port */
58 /* length of removed Ethernet header */
61 /* Stephen ZyDAS Technology Corporation 2005.5 */
63 /************************************************************************/
64 u16_t zfTxGenWlanHeader(zdev_t* dev, zbuf_t* buf, u16_t* header, u16_t seq,
65 u8_t flag, u16_t plusLen, u16_t minusLen, u16_t port,
66 u16_t* da, u16_t* sa, u8_t up, u16_t *micLen,
67 u16_t* snap, u16_t snapLen, struct aggControl *aggControl)
82 u8_t encExemptionActionType;
83 u16_t rateProbingFlag = 0;
84 u8_t tkipFrameOffset = 0;
86 #ifdef ZM_ENABLE_IBSS_WPA2PSK
93 zmw_get_wlan_dev(dev);
95 /* Generate WLAN header */
97 header[4] = 0x0008 | (flag<<8);
101 if (wd->wlanMode == ZM_MODE_INFRASTRUCTURE)
106 /*Sometimes we wake up to tx/rx but AP still think we are sleeping, so still need to set this bit*/
107 if ( zfPowerSavingMgrIsSleeping(dev) || wd->sta.psMgr.tempWakeUp == 1 )
112 /* Address 1 = BSSID */
113 header[6] = wd->sta.bssid[0];
114 header[7] = wd->sta.bssid[1];
115 header[8] = wd->sta.bssid[2];
121 else if (wd->wlanMode == ZM_MODE_PSEUDO)
127 /* Address 3 = 00:00:00:00:00:00 */
132 /* PSEUDO test : WDS */
135 /* ToDS and FromDS bit */
146 else if (wd->wlanMode == ZM_MODE_IBSS)
152 /* Address 3 = BSSID */
153 header[12] = wd->sta.bssid[0];
154 header[13] = wd->sta.bssid[1];
155 header[14] = wd->sta.bssid[2];
157 #ifdef ZM_ENABLE_IBSS_WPA2PSK
158 zmw_enter_critical_section(dev);
159 res = zfStaFindOppositeByMACAddr(dev, da, &peerIdx);
160 if(res == 0) // Find opposite in our OppositeInfo Structure !
164 zmw_leave_critical_section(dev);
167 else if (wd->wlanMode == ZM_MODE_AP)
184 if (port < ZM_MAX_AP_SUPPORT)
187 header[14] += (vap<<8);
193 /* ToDS and FromDS bit */
196 wdsPortId = port - 0x20;
199 header[6] = wd->ap.wds.macAddr[wdsPortId][0];
200 header[7] = wd->ap.wds.macAddr[wdsPortId][1];
201 header[8] = wd->ap.wds.macAddr[wdsPortId][2];
213 } /* else if (wd->wlanMode == ZM_MODE_AP) */
216 header[9] = wd->macAddr[0];
217 header[10] = wd->macAddr[1];
218 #ifdef ZM_VAPMODE_MULTILE_SSID
219 header[11] = wd->macAddr[2]; //Multiple SSID
221 header[11] = wd->macAddr[2] + (vap<<8); //VAP
224 if ( (wd->wlanMode == ZM_MODE_IBSS) && (wd->XLinkMode) )
231 /* Sequence Control */
235 if (wd->wlanMode == ZM_MODE_AP)
237 zfApGetStaTxRateAndQosType(dev, da, &phyCtrl, &qosType, &rateProbingFlag);
238 mt = (u16_t)(phyCtrl & 0x3);
239 mcs = (u16_t)((phyCtrl >> 16) & 0x3f);
241 //zfApGetStaQosType(dev, da, &qosType);
243 /* if DA == WME STA */
258 if (header[6] == 0x8000)
269 if (wd->wlanMode == ZM_MODE_AP) {
270 /* Todo: rate control here for qos field */
274 zfStaGetTxRate(dev, da, &phyCtrl, &rateProbingFlag);
275 mt = (u16_t)(phyCtrl & 0x3);
276 mcs = (u16_t)((phyCtrl >> 16) & 0x3f);
279 if (wd->txMCS != 0xff)
282 phyCtrl = ((u32_t)wd->txMCS<<16) + wd->txMT;
287 if (wd->enableAggregation)
289 /* force enable aggregation */
290 if (wd->enableAggregation==2 && !(header[6]&0x1))
299 /* if wd->enableAggregation=1 => force disable */
300 /* if wd->enableAggregation=0 => auto */
303 #ifdef ZM_ENABLE_AGGREGATION
305 * aggregation control
311 if (wd->wlanMode == ZM_MODE_AP) {
312 if (aggControl && mt == 2) {
313 if (wd->enableAggregation==0 && !(header[6]&0x1))
328 len = zfwBufGetSize(dev, buf);
330 /* Generate control setting */
331 /* Backoff, Non-Burst and hardware duration */
335 if ((header[6] & 0x1) == 0x1)
337 /* multicast frame : Set NO-ACK bit */
344 // Enable RTS according to MPDU Lengths ( not MSDU Lengths )
345 if (len >= wd->rtsThreshold)
355 if (wd->wlanMode == ZM_MODE_AP)
361 /* Check whether this is a multicast frame */
362 if ((header[6] & 0x1) == 0x1)
364 /* multicast frame */
365 if (wd->ap.encryMode[vap] == ZM_TKIP)
369 if(wd->ap.iv16[vap] == 0)
374 b1 = (u8_t) (wd->ap.iv16[vap] >> 8);
375 b2 = (b1 | 0x20) & 0x7f;
376 header[hlen] = ((u16_t)b2 << 8) + b1;
377 b1 = (u8_t) wd->ap.iv16[vap];
378 b2 = 0x20 | (wd->ap.bcKeyIndex[vap] << 6);
379 header[hlen+1] = ((u16_t)b2 << 8) + b1;
380 header[hlen+2] = (u16_t) wd->ap.iv32[vap];
381 header[hlen+3] = (u16_t) (wd->ap.iv32[vap] >> 16);
387 /* set hardware MIC */
388 if ( (!(seq & 0xf))&&(!(flag & 0x4)) )
398 else if (wd->ap.encryMode[vap] == ZM_AES)
402 if(wd->ap.iv16[vap] == 0)
407 b1 = (u8_t) wd->ap.iv16[vap];
408 b2 = (u8_t) (wd->ap.iv16[vap] >> 8);
409 header[hlen] = ((u16_t)b2 << 8) + b1;
410 header[hlen+1] = 0x2000 | (wd->ap.bcKeyIndex[vap] << 14);
411 header[hlen+2] = (u16_t) (wd->ap.iv32[vap]);
412 header[hlen+3] = (u16_t) (wd->ap.iv32[vap] >> 16);
415 icvLen = 8; /* MIC */
420 #ifdef ZM_ENABLE_CENC
421 else if (wd->ap.encryMode[vap] == ZM_CENC)
425 wd->ap.txiv[vap][0]++;
427 if (wd->ap.txiv[vap][0] == 0)
429 wd->ap.txiv[vap][1]++;
432 if (wd->ap.txiv[vap][1] == 0)
434 wd->ap.txiv[vap][2]++;
437 if (wd->ap.txiv[vap][2] == 0)
439 wd->ap.txiv[vap][3]++;
442 if (wd->ap.txiv[vap][3] == 0)
444 wd->ap.txiv[vap][0] = 0;
445 wd->ap.txiv[vap][1] = 0;
446 wd->ap.txiv[vap][2] = 0;
449 header[hlen] = (wd->ap.bcKeyIndex[vap] & 0x0001); /* For Key Id and reserved field */
450 header[hlen+1] = (u16_t)wd->ap.txiv[vap][0];
451 header[hlen+2] = (u16_t)(wd->ap.txiv[vap][0] >> 16);
452 header[hlen+3] = (u16_t)wd->ap.txiv[vap][1];
453 header[hlen+4] = (u16_t)(wd->ap.txiv[vap][1] >> 16);
454 header[hlen+5] = (u16_t)wd->ap.txiv[vap][2];
455 header[hlen+6] = (u16_t)(wd->ap.txiv[vap][2] >> 16);
456 header[hlen+7] = (u16_t)wd->ap.txiv[vap][3];
457 header[hlen+8] = (u16_t)(wd->ap.txiv[vap][3] >> 16);
460 icvLen = 16; /* MIC */
465 #endif //ZM_ENABLE_CENC
469 /* Get STA's encryption type */
470 zfApGetStaEncryType(dev, da, &encryType);
472 if (encryType == ZM_TKIP)
474 /* Get iv16 and iv32 */
475 zfApGetStaWpaIv(dev, da, &iv16, &iv32);
483 b1 = (u8_t) (iv16 >> 8);
484 b2 = (b1 | 0x20) & 0x7f;
485 header[hlen] = ((u16_t)b2 << 8) + b1;
488 header[hlen+1] = ((u16_t)b2 << 8) + b1;
489 header[hlen+2] = (u16_t) iv32;
490 header[hlen+3] = (u16_t) (iv32 >> 16);
496 /* set hardware MIC */
497 if ( (!(seq & 0xf))&&(!(flag & 0x4)) )
507 /* Set iv16 and iv32 */
508 zfApSetStaWpaIv(dev, da, iv16, iv32);
510 else if (encryType == ZM_AES)
512 /* Get iv16 and iv32 */
513 zfApGetStaWpaIv(dev, da, &iv16, &iv32);
522 b2 = (u8_t) (iv16 >> 8);
523 header[hlen] = ((u16_t)b2 << 8) + b1;
524 header[hlen+1] = 0x2000;
525 header[hlen+2] = (u16_t) (iv32);
526 header[hlen+3] = (u16_t) (iv32 >> 16);
529 icvLen = 8; /* MIC */
534 /* Set iv16 and iv32 */
535 zfApSetStaWpaIv(dev, da, iv16, iv32);
537 #ifdef ZM_ENABLE_CENC
538 else if (encryType == ZM_CENC)
544 zfApGetStaCencIvAndKeyIdx(dev, da, txiv, &keyIdx);
548 if (txiv[0] == 0 || txiv[0] == 1)
570 header[hlen] = (keyIdx & 0x0001); /* For Key Id and reserved field */
571 header[hlen+1] = (u16_t)txiv[0];
572 header[hlen+2] = (u16_t)(txiv[0] >> 16);
573 header[hlen+3] = (u16_t)txiv[1];
574 header[hlen+4] = (u16_t)(txiv[1] >> 16);
575 header[hlen+5] = (u16_t)txiv[2];
576 header[hlen+6] = (u16_t)(txiv[2] >> 16);
577 header[hlen+7] = (u16_t)txiv[3];
578 header[hlen+8] = (u16_t)(txiv[3] >> 16);
581 icvLen = 16; /* MIC */
587 zfApSetStaCencIv(dev, da, txiv);
589 #endif //ZM_ENABLE_CENC
592 /* protection mode */
593 if (wd->ap.protectionMode == 1)
595 /* Enable Self-CTS */
605 if ((wd->ap.encryMode[vap] == ZM_WEP64) ||
606 (wd->ap.encryMode[vap] == ZM_WEP128) ||
607 (wd->ap.encryMode[vap] == ZM_WEP256))
610 header[hlen] = 0x0; //IV
611 header[hlen+1] = wd->ap.bcKeyIndex[vap] << 14; //IV with Keyid--CWYang(m)
621 /* TODO : Fixed rate to 54M */
622 phyCtrl = 0xc0001; //PHY control L
624 /* WDS port checking */
625 wdsPort = port - 0x20;
626 if (wdsPort >= ZM_MAX_WDS_SUPPORT)
633 switch (wd->ap.wds.encryMode[wdsPort])
639 header[hlen] = 0x0; //IV
640 header[hlen+1] = wd->ap.bcKeyIndex[vap] << 14; //IV with Keyid
649 if ( wd->sta.iv16 == 0 )
654 b1 = (u8_t) (wd->sta.iv16 >> 8);
655 b2 = (b1 | 0x20) & 0x7f;
656 header[hlen] = ((u16_t)b2 << 8) + b1;
657 b1 = (u8_t) wd->sta.iv16;
659 header[hlen+1] = ((u16_t)b2 << 8) + b1;
660 header[hlen+2] = (u16_t) wd->sta.iv32;
661 header[hlen+3] = (u16_t) (wd->sta.iv32 >> 16);
667 /* set hardware MIC */
668 if ( (!(seq & 0xf))&&(!(flag & 0x4)) )
681 if ( wd->sta.iv16 == 0 )
686 b1 = (u8_t) wd->sta.iv16;
687 b2 = (u8_t) (wd->sta.iv16 >> 8);
688 header[hlen] = ((u16_t)b2 << 8) + b1;
689 header[hlen+1] = 0x2000;
690 header[hlen+2] = (u16_t) (wd->sta.iv32);
691 header[hlen+3] = (u16_t) (wd->sta.iv32 >> 16);
693 macCtrl |= 0xc0; /* Set to AES in control setting */
694 icvLen = 8; /* MIC */
696 header[4] |= 0x4000; /* Set WEP bit in wlan header */
697 hlen += 4; /* plus IV length */
703 else /* wd->wlanMode != ZM_MODE_AP */
705 encExemptionActionType = zfwGetPktEncExemptionActionType(dev, buf);
707 if ( wd->wlanMode == ZM_MODE_INFRASTRUCTURE )
711 if (wd->sta.wmeConnected != 0)
722 if ( encExemptionActionType == ZM_ENCRYPTION_EXEMPT_NO_EXEMPTION )
724 if ( wd->sta.authMode < ZM_AUTH_MODE_WPA )
726 if ( wd->sta.wepStatus == ZM_ENCRYPTION_WEP_ENABLED )
728 if ( (wd->sta.encryMode == ZM_WEP64)||
729 (wd->sta.encryMode == ZM_WEP128)||
730 (wd->sta.encryMode == ZM_WEP256) )
733 header[hlen] = 0x0; //IV
734 header[hlen+1] = 0x0; //IV
735 header[hlen+1] |= (((u16_t) wd->sta.keyId) << 14);
739 /* For Software WEP */
740 if ((wd->sta.SWEncryptEnable & ZM_SW_WEP_ENCRY_EN) != 0)
749 if (wd->sta.SWEncryMode[wd->sta.keyId] == ZM_WEP64)
753 else if (wd->sta.SWEncryMode[wd->sta.keyId] == ZM_WEP128)
757 else if (wd->sta.SWEncryMode[wd->sta.keyId] == ZM_WEP256)
762 zfWEPEncrypt(dev, buf, (u8_t*) snap, snapLen, minusLen, keyLen,
763 wd->sta.wepKey[wd->sta.keyId], iv);
774 if ( wd->sta.wpaState >= ZM_STA_WPA_STATE_PK_OK )
777 if ( wd->sta.iv16 == 0 )
782 /* set encryption mode */
783 if ( wd->sta.encryMode == ZM_TKIP )
785 b1 = (u8_t) (wd->sta.iv16 >> 8);
786 b2 = (b1 | 0x20) & 0x7f;
787 header[hlen] = ((u16_t)b2 << 8) + b1;
788 b1 = (u8_t) wd->sta.iv16;
791 // header[hlen+1] = (((u16_t) wd->sta.keyId) << 14) | (((u16_t)b2 << 8) + b1);
792 // STA in infrastructure mode should use keyId = 0 to transmit unicast !
793 header[hlen+1] = (((u16_t)b2 << 8) + b1);
794 header[hlen+2] = (u16_t) wd->sta.iv32;
795 header[hlen+3] = (u16_t) (wd->sta.iv32 >> 16);
797 /* If software encryption enable */
798 if ((wd->sta.SWEncryptEnable & ZM_SW_TKIP_ENCRY_EN) == 0)
801 /* TKIP same to WEP */
805 /* set hardware MIC */
806 if ( (!(seq & 0xf))&&(!(flag & 0x4)) )
820 /* TODO: Remove the criticial section here. */
821 zmw_declare_for_critical_section();
823 zmw_enter_critical_section(dev);
825 zfCalTxMic(dev, buf, (u8_t *)snap, snapLen, minusLen, da, sa, up, mic);
827 offset = zfwBufGetSize(dev, buf);
829 /* Append MIC to the buffer */
830 zfCopyToIntTxBuffer(dev, buf, mic, offset, 8);
831 zfwBufSetSize(dev, buf, offset+8);
832 zmw_leave_critical_section(dev);
834 /* TKIP Key Mixing */
835 zfTkipPhase1KeyMix(wd->sta.iv32, &wd->sta.txSeed);
836 zfTkipPhase2KeyMix(wd->sta.iv16, &wd->sta.txSeed);
837 zfTkipGetseeds(wd->sta.iv16, RC4Key, &wd->sta.txSeed);
840 zfTKIPEncrypt(dev, buf, (u8_t *)snap, snapLen, minusLen, 16, RC4Key, &icv);
849 else if ( wd->sta.encryMode == ZM_AES )
851 b1 = (u8_t) wd->sta.iv16;
852 b2 = (u8_t) (wd->sta.iv16 >> 8);
853 header[hlen] = ((u16_t)b2 << 8) + b1;
854 // header[hlen+1] = (((u16_t) wd->sta.keyId) << 14) | (0x2000);
855 // STA in infrastructure mode should use keyId = 0 to transmit unicast !
856 header[hlen+1] = 0x2000;
857 header[hlen+2] = (u16_t) (wd->sta.iv32);
858 header[hlen+3] = (u16_t) (wd->sta.iv32 >> 16);
861 icvLen = 8; /* MIC */
866 #ifdef ZM_ENABLE_CENC
867 else if ( wd->sta.encryMode == ZM_CENC )
869 /* Accumlate the PN sequence */
870 wd->sta.txiv[0] += 2;
872 if (wd->sta.txiv[0] == 0 || wd->sta.txiv[0] == 1)
877 if (wd->sta.txiv[1] == 0)
882 if (wd->sta.txiv[2] == 0)
887 if (wd->sta.txiv[3] == 0)
894 header[hlen] = (wd->sta.cencKeyId & 0x0001); /* For Key Id and reserved field */
895 header[hlen+1] = (u16_t) wd->sta.txiv[0];
896 header[hlen+2] = (u16_t) (wd->sta.txiv[0] >> 16);
897 header[hlen+3] = (u16_t) wd->sta.txiv[1];
898 header[hlen+4] = (u16_t) (wd->sta.txiv[1] >> 16);
899 header[hlen+5] = (u16_t) wd->sta.txiv[2];
900 header[hlen+6] = (u16_t) (wd->sta.txiv[2] >> 16);
901 header[hlen+7] = (u16_t) wd->sta.txiv[3];
902 header[hlen+8] = (u16_t) (wd->sta.txiv[3] >> 16);
905 icvLen = 16; /* MIC */
910 #endif //ZM_ENABLE_CENC
913 } // if ( encExemptionActionType == ZM_ENCRYPTION_EXEMPT_NO_EXEMPTION )
914 } /* if ( wd->wlanMode != ZM_MODE_INFRASTRUCTURE ) */
916 if ( wd->wlanMode == ZM_MODE_IBSS )
918 if ( encExemptionActionType == ZM_ENCRYPTION_EXEMPT_NO_EXEMPTION )
920 #ifdef ZM_ENABLE_IBSS_WPA2PSK
921 if( wd->sta.oppositeInfo[userIdx].wpaState >= ZM_STA_WPA_STATE_PK_OK || wd->sta.wpaState >= ZM_STA_WPA_STATE_PK_OK)
927 isUnicast = 0 ; // Not unicast , is broadcast
930 if( wd->sta.ibssWpa2Psk == 1 )
931 { /* The IV order is not the same between unicast and broadcast ! */
934 iv16 = &wd->sta.oppositeInfo[userIdx].iv16;
935 iv32 = &wd->sta.oppositeInfo[userIdx].iv32;
939 iv16 = &wd->sta.iv16;
940 iv32 = &wd->sta.iv32;
945 iv16 = &wd->sta.iv16;
946 iv32 = &wd->sta.iv32;
955 if ( wd->sta.oppositeInfo[userIdx].encryMode == ZM_AES || wd->sta.encryMode == ZM_AES)
957 //printk("Station encryption mode is AES-CCMP\n") ;
959 b2 = (u8_t) ((*iv16) >> 8);
960 header[hlen] = ((u16_t)b2 << 8) + b1;
964 header[hlen+1] = 0x2000;
968 header[hlen+1] = 0x2000 | (((u16_t) wd->sta.keyId) << 14);
971 header[hlen+2] = (u16_t) (*iv32);
972 header[hlen+3] = (u16_t) ((*iv32) >> 16);
974 icvLen = 8; /* MIC */
980 else if ( wd->sta.wepStatus == ZM_ENCRYPTION_WEP_ENABLED)
982 if ( (wd->sta.encryMode == ZM_WEP64)||
983 (wd->sta.encryMode == ZM_WEP128)||
984 (wd->sta.encryMode == ZM_WEP256) )
987 header[hlen] = 0x0; //IV
988 header[hlen+1] = 0x0; //IV
989 header[hlen+1] |= (((u16_t) wd->sta.keyId) << 14);
996 /* ----- 20070405 add by Mxzeng ----- */
997 if( wd->sta.wpaState >= ZM_STA_WPA_STATE_PK_OK )
1003 isUnicast = 0 ; // Not unicast , is broadcast
1007 if ( wd->sta.iv16 == 0 )
1012 if ( wd->sta.encryMode == ZM_AES )
1014 //printk("Station encryption mode is AES-CCMP\n") ;
1015 b1 = (u8_t) wd->sta.iv16;
1016 b2 = (u8_t) (wd->sta.iv16 >> 8);
1017 header[hlen] = ((u16_t)b2 << 8) + b1;
1021 header[hlen+1] = 0x2000;
1025 header[hlen+1] = 0x2000 | (((u16_t) wd->sta.keyId) << 14);
1028 header[hlen+2] = (u16_t) (wd->sta.iv32);
1029 header[hlen+3] = (u16_t) (wd->sta.iv32 >> 16);
1031 icvLen = 8; /* MIC */
1034 header[4] |= 0x4000;
1037 else if ( wd->sta.wepStatus == ZM_ENCRYPTION_WEP_ENABLED)
1039 if ( (wd->sta.encryMode == ZM_WEP64)||
1040 (wd->sta.encryMode == ZM_WEP128)||
1041 (wd->sta.encryMode == ZM_WEP256) )
1043 header[4] |= 0x4000;
1044 header[hlen] = 0x0; //IV
1045 header[hlen+1] = 0x0; //IV
1046 header[hlen+1] |= (((u16_t) wd->sta.keyId) << 14);
1053 } // End if ( encExemptionActionType == ZM_ENCRYPTION_EXEMPT_NO_EXEMPTION )
1054 } // End if ( wd->wlanMode == ZM_MODE_IBSS )
1055 else if ( wd->wlanMode == ZM_MODE_PSEUDO )
1057 switch (wd->sta.encryMode)
1062 header[4] |= 0x4000;
1063 header[hlen] = 0x0; //IV
1064 header[hlen+1] = 0x0; //IV
1073 if ( wd->sta.iv16 == 0 )
1078 b1 = (u8_t) (wd->sta.iv16 >> 8);
1079 b2 = (b1 | 0x20) & 0x7f;
1080 header[hlen] = ((u16_t)b2 << 8) + b1;
1081 b1 = (u8_t) wd->sta.iv16;
1083 header[hlen+1] = ((u16_t)b2 << 8) + b1;
1084 header[hlen+2] = (u16_t) wd->sta.iv32;
1085 header[hlen+3] = (u16_t) (wd->sta.iv32 >> 16);
1091 /* set hardware MIC */
1092 if ( (!(seq & 0xf))&&(!(flag & 0x4)) )
1099 header[4] |= 0x4000;
1101 }/* end of PSEUDO TKIP */
1107 if ( wd->sta.iv16 == 0 )
1112 b1 = (u8_t) wd->sta.iv16;
1113 b2 = (u8_t) (wd->sta.iv16 >> 8);
1114 header[hlen] = ((u16_t)b2 << 8) + b1;
1115 header[hlen+1] = 0x2000;
1116 header[hlen+2] = (u16_t) (wd->sta.iv32);
1117 header[hlen+3] = (u16_t) (wd->sta.iv32 >> 16);
1119 icvLen = 8; /* MIC */
1120 header[4] |= 0x4000;
1122 }/* end of PSEUDO AES */
1125 #ifdef ZM_ENABLE_CENC
1127 /* Accumlate the PN sequence */
1128 wd->sta.txiv[0] += 2;
1130 if (wd->sta.txiv[0] == 0 || wd->sta.txiv[0] == 1)
1135 if (wd->sta.txiv[1] == 0)
1140 if (wd->sta.txiv[2] == 0)
1145 if (wd->sta.txiv[3] == 0)
1147 wd->sta.txiv[0] = 0;
1148 wd->sta.txiv[1] = 0;
1149 wd->sta.txiv[2] = 0;
1153 header[hlen+1] = (u16_t) wd->sta.txiv[0];
1154 header[hlen+2] = (u16_t) (wd->sta.txiv[0] >> 16);
1155 header[hlen+3] = (u16_t) wd->sta.txiv[1];
1156 header[hlen+4] = (u16_t) (wd->sta.txiv[1] >> 16);
1157 header[hlen+5] = (u16_t) wd->sta.txiv[2];
1158 header[hlen+6] = (u16_t) (wd->sta.txiv[2] >> 16);
1159 header[hlen+7] = (u16_t) wd->sta.txiv[3];
1160 header[hlen+8] = (u16_t) (wd->sta.txiv[3] >> 16);
1163 icvLen = 16; /* MIC */
1165 header[4] |= 0x4000;
1168 #endif //ZM_ENABLE_CENC
1169 }/* end of switch */
1172 /* Generate control setting */
1174 /* protection mode */
1175 if (wd->enableProtectionMode)
1177 if (wd->enableProtectionMode==2)
1179 /* Force enable protection: self cts */
1183 /* if wd->enableProtectionMode=1 => force disable */
1184 /* if wd->enableProtectionMode=0 => auto */
1189 /* protection mode */
1190 if (wd->sta.bProtectionMode == TRUE)
1192 /* Enable Self-CTS */
1200 if (wd->txMCS != 0xff)
1203 phyCtrl = ((u32_t)wd->txMCS<<16) + wd->txMT;
1211 /* HT PT: 0 Mixed mode 1 Green field */
1212 if (wd->sta.preambleTypeHT == ZM_PREAMBLE_TYPE_GREEN_FIELD)
1214 phyCtrl |= 0x4; /* Bit 2 */
1218 if (wd->sta.htCtrlBandwidth == ZM_BANDWIDTH_40MHZ)
1220 phyCtrl |= (0x80<<16); /* BIT 23 */
1224 if (wd->sta.htCtrlSTBC<=0x3)
1226 phyCtrl |= (wd->sta.htCtrlSTBC<<28); /* BIT 23 */
1230 if(wd->sta.htCtrlSG)
1232 phyCtrl |= (0x8000<<16); /* BIT 31 */
1236 if ( ((mcs >=0x8) && (mcs<=0xf)) || (wd->sta.htCtrlSTBC) )
1238 phyCtrl |= 0x1800; /* BIT 11 12 */
1244 //bug that cause OFDM rate become duplicate legacy rate
1246 if (wd->sta.htCtrlBandwidth == ZM_BANDWIDTH_40MHZ)
1248 phyCtrl |= (0x80<<16); /* BIT 23 */
1249 mt = 3; /* duplicate legacy */
1256 /* CCK PT: Legcy Preamble: 1 long preamble 2 short preamble */
1257 if (wd->preambleTypeInUsed == ZM_PREAMBLE_TYPE_SHORT)
1259 //phyCtrl |= 0x4; /* BIT 2 */
1264 if (wd->sta.defaultTA)
1273 //Get CurrentTxRate -- CWYang(+)
1274 if ((mt == 0) || (mt == 1)) //B,G Rate
1278 wd->CurrentTxRateKbps = zcIndextoRateBG[mcs];
1285 if (wd->sta.htCtrlBandwidth == ZM_BANDWIDTH_40MHZ)
1287 if((phyCtrl & 0x80000000) != 0)
1289 /* Short GI 40 MHz MIMO Rate */
1290 wd->CurrentTxRateKbps = zcIndextoRateN40S[mcs];
1294 /* Long GI 40 MHz MIMO Rate */
1295 wd->CurrentTxRateKbps = zcIndextoRateN40L[mcs];
1300 if((phyCtrl & 0x80000000) != 0)
1302 /* Short GI 20 MHz MIMO Rate */
1303 wd->CurrentTxRateKbps = zcIndextoRateN20S[mcs];
1307 /* Long GI 20 MHz MIMO Rate */
1308 wd->CurrentTxRateKbps = zcIndextoRateN20L[mcs];
1314 //802.11 header(include IV) = (hlen<<1)-8
1315 //ethernet frame = len
1316 //snap + mic = plusLen
1317 //ethernet header = minusLen
1320 //length=802.11 header+snap+(ethernet frame-ethernet header)+mic+icv+crc32
1321 header[0] = ((hlen<<1)-8)+plusLen+(len-minusLen)+icvLen+4; //Length
1323 // header[0] : MPDU Lengths
1324 if ((header[6] & 0x1) != 0x1) // Unicast Frame
1326 if (header[0] >= wd->rtsThreshold)
1333 if ( wd->sta.encryMode == ZM_TKIP )
1334 tkipFrameOffset = 8;
1336 if( wd->sta.EnableHT != 1 )
1337 { // Aggregation should not be fragmented !
1338 if ( header[0] > ( wd->fragThreshold + tkipFrameOffset ) )
1340 return 0; // Need to be fragmented ! !
1344 //if ( wd->sta.encryMode == ZM_TKIP )
1346 // zm_debug_msg1("ctrl length = ", header[0]);
1350 if (rateProbingFlag != 0)
1354 header[1] = macCtrl;
1356 header[2] = (u16_t) ((phyCtrl&0xffff) | 0x700 | (zcUpToAc[up&0x7]<<13));
1358 header[3] = (u16_t) ((phyCtrl>>16) | 0x700);
1360 if (wd->enableAggregation)
1362 /* force enable aggregation */
1363 if (wd->enableAggregation==2 && !(header[6]&0x1))
1365 if (((header[2] & 0x3) == 2))
1367 /* Enable aggregation */
1371 /* if wd->enableAggregation=1 => force disable */
1372 /* if wd->enableAggregation=0 => auto */
1375 #ifdef ZM_ENABLE_AGGREGATION
1376 if (wd->addbaComplete) {
1377 #ifdef ZM_BYPASS_AGGR_SCHEDULING
1378 if (!(header[6]&0x1) && !rateProbingFlag && (wd->enableAggregation != 1))
1380 if (((header[2] & 0x3) == 2))
1382 /* Unicast frame with HT rate => Enable aggregation */
1383 /* We only support software encryption in single packet mode */
1384 if ((wd->sta.SWEncryptEnable & ZM_SW_TKIP_ENCRY_EN) == 0 &&
1385 (wd->sta.SWEncryptEnable & ZM_SW_WEP_ENCRY_EN) == 0)
1387 /* Set aggregation group bits per AC */
1388 header[1] |= (0x20 | (zcUpToAc[up&0x7]<<10));
1390 //if (wd->sta.currentFrequency < 3000)
1392 /* issue: -PB42 Enable RTS/CTS to prevent OWL Tx hang up */
1393 /* If this is Owl Ap, enable RTS/CTS protect */
1394 if ( (wd->sta.athOwlAp == 1) || (wd->sta.RTSInAGGMode == TRUE) )
1396 header[1] &= 0xfffc;
1400 /* Enable RIFS : workaround 854T RTS/CTS */
1401 /* Bit13 : TI enable RIFS */
1402 //header[1] |= 0x2000;
1409 * aggregation ampduIndication control
1411 if (aggControl && aggControl->aggEnabled) {
1412 if (wd->enableAggregation==0 && !(header[6]&0x1))
1414 if (((header[2] & 0x3) == 2))
1416 /* Enable aggregation */
1418 if (ZM_AGG_LAST_MPDU == aggControl->ampduIndication)
1419 header[1] |= 0x4000;
1422 zm_debug_msg1("no aggr, header[2]&0x3 = ",header[2] & 0x3)
1423 aggControl->aggEnabled = 0;
1427 zm_debug_msg1("no aggr, wd->enableAggregation = ", wd->enableAggregation);
1428 zm_debug_msg1("no aggr, !header[6]&0x1 = ",!(header[6]&0x1));
1429 aggControl->aggEnabled = 0;
1434 #ifdef ZM_AGGR_BIT_ON
1435 if (!(header[6]&0x1) && !rateProbingFlag)
1437 if (((header[2] & 0x3) == 2))
1439 /* Unicast frame with HT rate => Enable aggregation */
1440 /* Set aggregation group bits per AC */
1441 header[1] |= (0x20 | (zcUpToAc[up&0x7]<<10));
1443 //if (wd->sta.currentFrequency < 3000)
1445 /* Enable RTS/CTS to prevent OWL Tx hang up */
1446 header[1] &= 0xfffc;
1459 u16_t zfTxGenMmHeader(zdev_t* dev, u8_t frameType, u16_t* dst,
1460 u16_t* header, u16_t len, zbuf_t* buf, u16_t vap, u8_t encrypt)
1463 u8_t hlen = 32; // MAC ctrl + PHY ctrl + 802.11 MM header
1465 zmw_get_wlan_dev(dev);
1467 zmw_declare_for_critical_section();
1469 /* Generate control setting */
1470 //bodyLen = zfwBufGetSize(dev, buf);
1471 header[0] = 24+len+4; //Length
1472 if ((dst[0] & 0x1) != 0) //Broadcast, multicast frames
1474 header[1] = 0xc; //MAC control, backoff + noack
1478 header[1] = 0x8; //MAC control, backoff + (ack)
1480 /* Dualband Management frame tx Rate */
1481 if (wd->wlanMode == ZM_MODE_AP)
1483 if (wd->frequency < 3000)
1486 header[2] = 0x0f00; //PHY control L
1487 header[3] = 0x0000; //PHY control H
1492 header[2] = 0x0f01; //PHY control L
1493 header[3] = 0x000B; //PHY control H
1498 if (wd->sta.currentFrequency < 3000)
1501 header[2] = 0x0f00; //PHY control L
1502 header[3] = 0x0001; //PHY control H
1507 header[2] = 0x0f01; //PHY control L
1508 header[3] = 0x000B; //PHY control H
1511 /* Generate WLAN header */
1513 header[4+0] = frameType;
1517 if (wd->wlanMode == ZM_MODE_INFRASTRUCTURE)
1519 if ( frameType == ZM_WLAN_FRAME_TYPE_PROBEREQ )
1521 header[4+8] = 0xFFFF;
1522 header[4+9] = 0xFFFF;
1523 header[4+10] = 0xFFFF;
1525 else if ( frameType == ZM_WLAN_FRAME_TYPE_BA ) {
1530 header[4+8] = wd->sta.bssid[0];
1531 header[4+9] = wd->sta.bssid[1];
1532 header[4+10] = wd->sta.bssid[2];
1535 else if (wd->wlanMode == ZM_MODE_PSEUDO)
1537 /* Address 3 = 00:00:00:00:00:00 */
1542 else if (wd->wlanMode == ZM_MODE_IBSS)
1544 header[4+8] = wd->sta.bssid[0];
1545 header[4+9] = wd->sta.bssid[1];
1546 header[4+10] = wd->sta.bssid[2];
1548 if ( frameType == ZM_WLAN_FRAME_TYPE_ATIM )
1550 /* put ATIM to queue 5th */
1551 //header[2] |= (ZM_BIT_13|ZM_BIT_14);
1552 header[2] |= ZM_BIT_15;
1555 else if (wd->wlanMode == ZM_MODE_AP)
1557 /* Address 3 = BSSID */
1558 header[4+8] = wd->macAddr[0];
1559 header[4+9] = wd->macAddr[1];
1560 #ifdef ZM_VAPMODE_MULTILE_SSID
1561 header[4+10] = wd->macAddr[2]; //Multiple SSID
1563 header[4+10] = wd->macAddr[2] + (vap<<8); //VAP
1565 //if in scan, must set address 3 to broadcast because of some ap would care this
1566 //if ((wd->heartBeatNotification & ZM_BSSID_LIST_SCAN)
1567 // == ZM_BSSID_LIST_SCAN)
1568 //if FrameType is Probe Request, Address3 should be boradcast
1569 if (frameType == ZM_WLAN_FRAME_TYPE_PROBEREQ)
1571 header[4+8] = 0xFFFF;
1572 header[4+9] = 0xFFFF;
1573 header[4+10] = 0xFFFF;
1577 /* Address 1 = DA */
1578 header[4+2] = dst[0];
1579 header[4+3] = dst[1];
1580 header[4+4] = dst[2];
1582 /* Address 2 = SA */
1583 header[4+5] = wd->macAddr[0];
1584 header[4+6] = wd->macAddr[1];
1585 if (wd->wlanMode == ZM_MODE_AP)
1587 #ifdef ZM_VAPMODE_MULTILE_SSID
1588 header[4+7] = wd->macAddr[2]; //Multiple SSID
1590 header[4+7] = wd->macAddr[2] + (vap<<8); //VAP
1595 header[4+7] = wd->macAddr[2];
1598 /* Sequence Control */
1599 zmw_enter_critical_section(dev);
1600 header[4+11] = ((wd->mmseq++)<<4);
1601 zmw_leave_critical_section(dev);
1603 if( frameType == ZM_WLAN_FRAME_TYPE_QOS_NULL )
1613 if ( wd->sta.wepStatus == ZM_ENCRYPTION_WEP_ENABLED )
1615 if ( (wd->sta.encryMode == ZM_WEP64)||
1616 (wd->sta.encryMode == ZM_WEP128)||
1617 (wd->sta.encryMode == ZM_WEP256) )
1619 header[4] |= 0x4000;
1620 header[16] = 0x0; //IV
1621 header[17] = 0x0; //IV
1622 header[17] |= (((u16_t) wd->sta.keyId) << 14);
1625 header[0] += 8; // icvLen = 4;
1626 header[1] |= 0x40; // enable encryption on macCtrl
1631 // Enable HW duration
1632 if ( frameType != ZM_WLAN_FRAME_TYPE_PSPOLL )
1640 void zfInitMacApMode(zdev_t* dev)
1644 zmw_get_wlan_dev(dev);
1646 zfHpEnableBeacon(dev, ZM_MODE_AP, (wd->beaconInterval/wd->ap.vapNumber), 1, 0);
1649 zfHpSetApStaMode(dev, ZM_HAL_80211_MODE_AP);
1653 if (wd->ap.vapNumber >= 2)
1655 for (i=1; i<ZM_MAX_AP_SUPPORT; i++)
1657 if (((wd->ap.apBitmap >> i) & 0x1) != 0)
1660 mac[0] = wd->macAddr[0];
1661 mac[1] = wd->macAddr[1];
1662 #ifdef ZM_VAPMODE_MULTILE_SSID
1663 mac[2] = wd->macAddr[2]; //Multiple SSID
1665 mac[2] = wd->macAddr[2] + (i<<8); //VAP
1667 zfHpSetMacAddress(dev, mac, i);
1673 /* basic rate setting */
1674 zfHpSetBasicRateSet(dev, wd->bRateBasic, wd->gRateBasic);
1676 /* Set TxQs CWMIN, CWMAX, AIFS and TXO to WME AP default. */
1677 zfUpdateDefaultQosParameter(dev, 1);
1682 u16_t zfChGetNextChannel(zdev_t* dev, u16_t frequency, u8_t* pbPassive)
1687 zmw_get_wlan_dev(dev);
1689 /* Avoid NULL value */
1690 if ( pbPassive == NULL )
1692 pbPassive = &bPassive;
1695 for( i=0; i<wd->regulationTable.allowChannelCnt; i++ )
1697 if ( wd->regulationTable.allowChannel[i].channel == frequency )
1699 if ( i == (wd->regulationTable.allowChannelCnt-1) )
1708 if ( wd->regulationTable.allowChannel[i].channelFlags
1709 & ZM_REG_FLAG_CHANNEL_PASSIVE )
1718 return wd->regulationTable.allowChannel[i].channel;
1725 u16_t zfChGetFirstChannel(zdev_t* dev, u8_t* pbPassive)
1729 zmw_get_wlan_dev(dev);
1731 /* Avoid NULL value */
1732 if ( pbPassive == NULL )
1734 pbPassive = &bPassive;
1737 if ( wd->regulationTable.allowChannel[0].channelFlags & ZM_REG_FLAG_CHANNEL_PASSIVE )
1746 return wd->regulationTable.allowChannel[0].channel;
1749 u16_t zfChGetFirst2GhzChannel(zdev_t* dev)
1753 zmw_get_wlan_dev(dev);
1755 for( i=0; i<wd->regulationTable.allowChannelCnt; i++ )
1757 if ( wd->regulationTable.allowChannel[i].channel < 3000 )
1759 /* find the first 2Ghz channel */
1760 return wd->regulationTable.allowChannel[i].channel;
1764 /* Can not find any 2Ghz channel */
1768 u16_t zfChGetFirst5GhzChannel(zdev_t* dev)
1772 zmw_get_wlan_dev(dev);
1774 for( i=0; i<wd->regulationTable.allowChannelCnt; i++ )
1776 if ( wd->regulationTable.allowChannel[i].channel > 3000 )
1778 /* find the first 5Ghz channel */
1779 return wd->regulationTable.allowChannel[i].channel;
1783 /* Can not find any 5Ghz channel */
1787 u16_t zfChGetLastChannel(zdev_t* dev, u8_t* pbPassive)
1792 zmw_get_wlan_dev(dev);
1794 ChannelIndex = wd->regulationTable.allowChannelCnt-1;
1796 /* Avoid NULL value */
1797 if ( pbPassive == NULL )
1799 pbPassive = &bPassive;
1802 if ( wd->regulationTable.allowChannel[ChannelIndex].channelFlags
1803 & ZM_REG_FLAG_CHANNEL_PASSIVE )
1812 return wd->regulationTable.allowChannel[ChannelIndex].channel;
1815 u16_t zfChGetLast5GhzChannel(zdev_t* dev)
1818 u16_t last5Ghzfrequency;
1820 zmw_get_wlan_dev(dev);
1822 last5Ghzfrequency = 0;
1823 for( i=0; i<wd->regulationTable.allowChannelCnt; i++ )
1825 if ( wd->regulationTable.allowChannel[i].channel > 3000 )
1827 last5Ghzfrequency = wd->regulationTable.allowChannel[i].channel;
1831 return last5Ghzfrequency;
1834 /* freqBand = 0 => auto check */
1835 /* = 1 => 2.4 GHz band */
1836 /* = 2 => 5 GHz band */
1837 u16_t zfChNumToFreq(zdev_t* dev, u8_t ch, u8_t freqBand)
1839 u16_t freq = 0xffff;
1841 if ( freqBand == 0 )
1844 { /* adapter is at 5 GHz band */
1853 if ( freqBand == 2 )
1854 { /* the channel belongs to 5 GHz band */
1855 if ( (ch >= 184)&&(ch <= 196) )
1865 { /* the channel belongs to 2.4 GHz band */
1872 freq = ZM_CH_G_1 + (ch-1)*5;
1879 u8_t zfChFreqToNum(u16_t freq, u8_t* pbIs5GBand)
1884 /* to avoid NULL value */
1885 if ( pbIs5GBand == NULL )
1887 pbIs5GBand = &Is5GBand;
1890 *pbIs5GBand = FALSE;
1892 if ( freq == ZM_CH_G_14 )
1896 else if ( freq < 4000 )
1898 ch = (freq - ZM_CH_G_1) / 5 + 1;
1900 else if ( freq < 5000 )
1902 ch = (freq - 4000) / 5;
1907 ch = (freq - 5000) / 5;