• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RTL8188E_CMD_C_
21 
22 #include <osdep_service.h>
23 #include <drv_types.h>
24 #include <recv_osdep.h>
25 #include <mlme_osdep.h>
26 #include <rtw_ioctl_set.h>
27 
28 #include <rtl8188e_hal.h>
29 
30 #define RTL88E_MAX_H2C_BOX_NUMS		4
31 #define RTL88E_MAX_CMD_LEN		7
32 #define RTL88E_MESSAGE_BOX_SIZE		4
33 #define RTL88E_EX_MESSAGE_BOX_SIZE	4
34 
_is_fw_read_cmd_down(struct adapter * adapt,u8 msgbox_num)35 static u8 _is_fw_read_cmd_down(struct adapter *adapt, u8 msgbox_num)
36 {
37 	u8 read_down = false;
38 	int	retry_cnts = 100;
39 
40 	u8 valid;
41 
42 	do {
43 		valid = usb_read8(adapt, REG_HMETFR) & BIT(msgbox_num);
44 		if (0 == valid)
45 			read_down = true;
46 	} while ((!read_down) && (retry_cnts--));
47 
48 	return read_down;
49 }
50 
51 /*****************************************
52 * H2C Msg format :
53 * 0x1DF - 0x1D0
54 *| 31 - 8	| 7-5	 4 - 0	|
55 *| h2c_msg	|Class_ID CMD_ID	|
56 *
57 * Extend 0x1FF - 0x1F0
58 *|31 - 0	  |
59 *|ext_msg|
60 ******************************************/
FillH2CCmd_88E(struct adapter * adapt,u8 ElementID,u32 CmdLen,u8 * pCmdBuffer)61 static s32 FillH2CCmd_88E(struct adapter *adapt, u8 ElementID, u32 CmdLen, u8 *pCmdBuffer)
62 {
63 	u8 bcmd_down = false;
64 	s32 retry_cnts = 100;
65 	u8 h2c_box_num;
66 	u32 msgbox_addr;
67 	u32 msgbox_ex_addr;
68 	struct hal_data_8188e *haldata = GET_HAL_DATA(adapt);
69 	u8 cmd_idx, ext_cmd_len;
70 	u32 h2c_cmd = 0;
71 	u32 h2c_cmd_ex = 0;
72 	s32 ret = _FAIL;
73 
74 
75 	if (!adapt->bFWReady) {
76 		DBG_88E("FillH2CCmd_88E(): return H2C cmd because fw is not ready\n");
77 		return ret;
78 	}
79 
80 	if (!pCmdBuffer)
81 		goto exit;
82 	if (CmdLen > RTL88E_MAX_CMD_LEN)
83 		goto exit;
84 	if (adapt->bSurpriseRemoved)
85 		goto exit;
86 
87 	/* pay attention to if  race condition happened in  H2C cmd setting. */
88 	do {
89 		h2c_box_num = haldata->LastHMEBoxNum;
90 
91 		if (!_is_fw_read_cmd_down(adapt, h2c_box_num)) {
92 			DBG_88E(" fw read cmd failed...\n");
93 			goto exit;
94 		}
95 
96 		*(u8 *)(&h2c_cmd) = ElementID;
97 
98 		if (CmdLen <= 3) {
99 			memcpy((u8 *)(&h2c_cmd)+1, pCmdBuffer, CmdLen);
100 		} else {
101 			memcpy((u8 *)(&h2c_cmd)+1, pCmdBuffer, 3);
102 			ext_cmd_len = CmdLen-3;
103 			memcpy((u8 *)(&h2c_cmd_ex), pCmdBuffer+3, ext_cmd_len);
104 
105 			/* Write Ext command */
106 			msgbox_ex_addr = REG_HMEBOX_EXT_0 + (h2c_box_num * RTL88E_EX_MESSAGE_BOX_SIZE);
107 			for (cmd_idx = 0; cmd_idx < ext_cmd_len; cmd_idx++) {
108 				usb_write8(adapt, msgbox_ex_addr+cmd_idx, *((u8 *)(&h2c_cmd_ex)+cmd_idx));
109 			}
110 		}
111 		/*  Write command */
112 		msgbox_addr = REG_HMEBOX_0 + (h2c_box_num * RTL88E_MESSAGE_BOX_SIZE);
113 		for (cmd_idx = 0; cmd_idx < RTL88E_MESSAGE_BOX_SIZE; cmd_idx++) {
114 			usb_write8(adapt, msgbox_addr+cmd_idx, *((u8 *)(&h2c_cmd)+cmd_idx));
115 		}
116 		bcmd_down = true;
117 
118 		haldata->LastHMEBoxNum = (h2c_box_num+1) % RTL88E_MAX_H2C_BOX_NUMS;
119 
120 	} while ((!bcmd_down) && (retry_cnts--));
121 
122 	ret = _SUCCESS;
123 
124 exit:
125 
126 
127 	return ret;
128 }
129 
130 /* bitmap[0:27] = tx_rate_bitmap */
131 /* bitmap[28:31]= Rate Adaptive id */
132 /* arg[0:4] = macid */
133 /* arg[5] = Short GI */
rtl8188e_Add_RateATid(struct adapter * pAdapter,u32 bitmap,u8 arg,u8 rssi_level)134 void rtl8188e_Add_RateATid(struct adapter *pAdapter, u32 bitmap, u8 arg, u8 rssi_level)
135 {
136 	struct hal_data_8188e *haldata = GET_HAL_DATA(pAdapter);
137 
138 	u8 macid, init_rate, raid, shortGIrate = false;
139 
140 	macid = arg&0x1f;
141 
142 	raid = (bitmap>>28) & 0x0f;
143 	bitmap &= 0x0fffffff;
144 
145 	if (rssi_level != DM_RATR_STA_INIT)
146 		bitmap = ODM_Get_Rate_Bitmap(&haldata->odmpriv, macid, bitmap, rssi_level);
147 
148 	bitmap |= ((raid<<28)&0xf0000000);
149 
150 	init_rate = get_highest_rate_idx(bitmap&0x0fffffff)&0x3f;
151 
152 	shortGIrate = (arg & BIT(5)) ? true : false;
153 
154 	if (shortGIrate)
155 		init_rate |= BIT(6);
156 
157 	raid = (bitmap>>28) & 0x0f;
158 
159 	bitmap &= 0x0fffffff;
160 
161 	DBG_88E("%s=> mac_id:%d, raid:%d, ra_bitmap=0x%x, shortGIrate=0x%02x\n",
162 		__func__, macid, raid, bitmap, shortGIrate);
163 
164 	ODM_RA_UpdateRateInfo_8188E(&(haldata->odmpriv), macid, raid, bitmap, shortGIrate);
165 }
166 
rtl8188e_set_FwPwrMode_cmd(struct adapter * adapt,u8 Mode)167 void rtl8188e_set_FwPwrMode_cmd(struct adapter *adapt, u8 Mode)
168 {
169 	struct setpwrmode_parm H2CSetPwrMode;
170 	struct pwrctrl_priv *pwrpriv = &adapt->pwrctrlpriv;
171 	u8 RLBM = 0; /*  0:Min, 1:Max, 2:User define */
172 
173 	DBG_88E("%s: Mode=%d SmartPS=%d UAPSD=%d\n", __func__,
174 		Mode, pwrpriv->smart_ps, adapt->registrypriv.uapsd_enable);
175 
176 	switch (Mode) {
177 	case PS_MODE_ACTIVE:
178 		H2CSetPwrMode.Mode = 0;
179 		break;
180 	case PS_MODE_MIN:
181 		H2CSetPwrMode.Mode = 1;
182 		break;
183 	case PS_MODE_MAX:
184 		RLBM = 1;
185 		H2CSetPwrMode.Mode = 1;
186 		break;
187 	case PS_MODE_DTIM:
188 		RLBM = 2;
189 		H2CSetPwrMode.Mode = 1;
190 		break;
191 	case PS_MODE_UAPSD_WMM:
192 		H2CSetPwrMode.Mode = 2;
193 		break;
194 	default:
195 		H2CSetPwrMode.Mode = 0;
196 		break;
197 	}
198 
199 	H2CSetPwrMode.SmartPS_RLBM = (((pwrpriv->smart_ps<<4)&0xf0) | (RLBM & 0x0f));
200 
201 	H2CSetPwrMode.AwakeInterval = 1;
202 
203 	H2CSetPwrMode.bAllQueueUAPSD = adapt->registrypriv.uapsd_enable;
204 
205 	if (Mode > 0)
206 		H2CSetPwrMode.PwrState = 0x00;/*  AllON(0x0C), RFON(0x04), RFOFF(0x00) */
207 	else
208 		H2CSetPwrMode.PwrState = 0x0C;/*  AllON(0x0C), RFON(0x04), RFOFF(0x00) */
209 
210 	FillH2CCmd_88E(adapt, H2C_PS_PWR_MODE, sizeof(H2CSetPwrMode), (u8 *)&H2CSetPwrMode);
211 
212 }
213 
rtl8188e_set_FwMediaStatus_cmd(struct adapter * adapt,__le16 mstatus_rpt)214 void rtl8188e_set_FwMediaStatus_cmd(struct adapter *adapt, __le16 mstatus_rpt)
215 {
216 	u8 opmode, macid;
217 	u16 mst_rpt = le16_to_cpu(mstatus_rpt);
218 	opmode = (u8)mst_rpt;
219 	macid = (u8)(mst_rpt >> 8);
220 
221 	DBG_88E("### %s: MStatus=%x MACID=%d\n", __func__, opmode, macid);
222 	FillH2CCmd_88E(adapt, H2C_COM_MEDIA_STATUS_RPT, sizeof(mst_rpt), (u8 *)&mst_rpt);
223 }
224 
ConstructBeacon(struct adapter * adapt,u8 * pframe,u32 * pLength)225 static void ConstructBeacon(struct adapter *adapt, u8 *pframe, u32 *pLength)
226 {
227 	struct rtw_ieee80211_hdr	*pwlanhdr;
228 	__le16 *fctrl;
229 	u32 rate_len, pktlen;
230 	struct mlme_ext_priv *pmlmeext = &(adapt->mlmeextpriv);
231 	struct mlme_ext_info	*pmlmeinfo = &(pmlmeext->mlmext_info);
232 	struct wlan_bssid_ex		*cur_network = &(pmlmeinfo->network);
233 	u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
234 
235 	pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
236 
237 	fctrl = &(pwlanhdr->frame_ctl);
238 	*(fctrl) = 0;
239 
240 	memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN);
241 	memcpy(pwlanhdr->addr2, myid(&(adapt->eeprompriv)), ETH_ALEN);
242 	memcpy(pwlanhdr->addr3, cur_network->MacAddress, ETH_ALEN);
243 
244 	SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/);
245 	SetFrameSubType(pframe, WIFI_BEACON);
246 
247 	pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
248 	pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
249 
250 	/* timestamp will be inserted by hardware */
251 	pframe += 8;
252 	pktlen += 8;
253 
254 	/*  beacon interval: 2 bytes */
255 	memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2);
256 
257 	pframe += 2;
258 	pktlen += 2;
259 
260 	/*  capability info: 2 bytes */
261 	memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2);
262 
263 	pframe += 2;
264 	pktlen += 2;
265 
266 	if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
267 		pktlen += cur_network->IELength - sizeof(struct ndis_802_11_fixed_ie);
268 		memcpy(pframe, cur_network->IEs+sizeof(struct ndis_802_11_fixed_ie), pktlen);
269 
270 		goto _ConstructBeacon;
271 	}
272 
273 	/* below for ad-hoc mode */
274 
275 	/*  SSID */
276 	pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pktlen);
277 
278 	/*  supported rates... */
279 	rate_len = rtw_get_rateset_len(cur_network->SupportedRates);
280 	pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, min_t(u32, rate_len, 8), cur_network->SupportedRates, &pktlen);
281 
282 	/*  DS parameter set */
283 	pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&(cur_network->Configuration.DSConfig), &pktlen);
284 
285 	if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
286 		u32 ATIMWindow;
287 		/*  IBSS Parameter Set... */
288 		ATIMWindow = 0;
289 		pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pktlen);
290 	}
291 
292 	/* todo: ERP IE */
293 
294 	/*  EXTERNDED SUPPORTED RATE */
295 	if (rate_len > 8)
296 		pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pktlen);
297 
298 	/* todo:HT for adhoc */
299 
300 _ConstructBeacon:
301 
302 	if ((pktlen + TXDESC_SIZE) > 512) {
303 		DBG_88E("beacon frame too large\n");
304 		return;
305 	}
306 
307 	*pLength = pktlen;
308 }
309 
ConstructPSPoll(struct adapter * adapt,u8 * pframe,u32 * pLength)310 static void ConstructPSPoll(struct adapter *adapt, u8 *pframe, u32 *pLength)
311 {
312 	struct rtw_ieee80211_hdr	*pwlanhdr;
313 	struct mlme_ext_priv *pmlmeext = &(adapt->mlmeextpriv);
314 	struct mlme_ext_info	*pmlmeinfo = &(pmlmeext->mlmext_info);
315 	__le16 *fctrl;
316 	struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
317 
318 	pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
319 
320 	/*  Frame control. */
321 	fctrl = &(pwlanhdr->frame_ctl);
322 	*(fctrl) = 0;
323 	SetPwrMgt(fctrl);
324 	SetFrameSubType(pframe, WIFI_PSPOLL);
325 
326 	/*  AID. */
327 	SetDuration(pframe, (pmlmeinfo->aid | 0xc000));
328 
329 	/*  BSSID. */
330 	memcpy(pwlanhdr->addr1, pnetwork->MacAddress, ETH_ALEN);
331 
332 	/*  TA. */
333 	memcpy(pwlanhdr->addr2, myid(&(adapt->eeprompriv)), ETH_ALEN);
334 
335 	*pLength = 16;
336 }
337 
ConstructNullFunctionData(struct adapter * adapt,u8 * pframe,u32 * pLength,u8 * StaAddr,u8 bQoS,u8 AC,u8 bEosp,u8 bForcePowerSave)338 static void ConstructNullFunctionData(struct adapter *adapt, u8 *pframe,
339 	u32 *pLength,
340 	u8 *StaAddr,
341 	u8 bQoS,
342 	u8 AC,
343 	u8 bEosp,
344 	u8 bForcePowerSave)
345 {
346 	struct rtw_ieee80211_hdr	*pwlanhdr;
347 	__le16 *fctrl;
348 	u32 pktlen;
349 	struct mlme_priv *pmlmepriv = &adapt->mlmepriv;
350 	struct wlan_network		*cur_network = &pmlmepriv->cur_network;
351 	struct mlme_ext_priv *pmlmeext = &(adapt->mlmeextpriv);
352 	struct mlme_ext_info	*pmlmeinfo = &(pmlmeext->mlmext_info);
353 	struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
354 
355 	pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
356 
357 	fctrl = &pwlanhdr->frame_ctl;
358 	*(fctrl) = 0;
359 	if (bForcePowerSave)
360 		SetPwrMgt(fctrl);
361 
362 	switch (cur_network->network.InfrastructureMode) {
363 	case Ndis802_11Infrastructure:
364 		SetToDs(fctrl);
365 		memcpy(pwlanhdr->addr1, pnetwork->MacAddress, ETH_ALEN);
366 		memcpy(pwlanhdr->addr2, myid(&(adapt->eeprompriv)), ETH_ALEN);
367 		memcpy(pwlanhdr->addr3, StaAddr, ETH_ALEN);
368 		break;
369 	case Ndis802_11APMode:
370 		SetFrDs(fctrl);
371 		memcpy(pwlanhdr->addr1, StaAddr, ETH_ALEN);
372 		memcpy(pwlanhdr->addr2, pnetwork->MacAddress, ETH_ALEN);
373 		memcpy(pwlanhdr->addr3, myid(&(adapt->eeprompriv)), ETH_ALEN);
374 		break;
375 	case Ndis802_11IBSS:
376 	default:
377 		memcpy(pwlanhdr->addr1, StaAddr, ETH_ALEN);
378 		memcpy(pwlanhdr->addr2, myid(&(adapt->eeprompriv)), ETH_ALEN);
379 		memcpy(pwlanhdr->addr3, pnetwork->MacAddress, ETH_ALEN);
380 		break;
381 	}
382 
383 	SetSeqNum(pwlanhdr, 0);
384 
385 	if (bQoS) {
386 		struct rtw_ieee80211_hdr_3addr_qos *pwlanqoshdr;
387 
388 		SetFrameSubType(pframe, WIFI_QOS_DATA_NULL);
389 
390 		pwlanqoshdr = (struct rtw_ieee80211_hdr_3addr_qos *)pframe;
391 		SetPriority(&pwlanqoshdr->qc, AC);
392 		SetEOSP(&pwlanqoshdr->qc, bEosp);
393 
394 		pktlen = sizeof(struct rtw_ieee80211_hdr_3addr_qos);
395 	} else {
396 		SetFrameSubType(pframe, WIFI_DATA_NULL);
397 
398 		pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
399 	}
400 
401 	*pLength = pktlen;
402 }
403 
ConstructProbeRsp(struct adapter * adapt,u8 * pframe,u32 * pLength,u8 * StaAddr,bool bHideSSID)404 static void ConstructProbeRsp(struct adapter *adapt, u8 *pframe, u32 *pLength, u8 *StaAddr, bool bHideSSID)
405 {
406 	struct rtw_ieee80211_hdr	*pwlanhdr;
407 	__le16 *fctrl;
408 	u8 *mac, *bssid;
409 	u32 pktlen;
410 	struct mlme_ext_priv *pmlmeext = &(adapt->mlmeextpriv);
411 	struct mlme_ext_info	*pmlmeinfo = &(pmlmeext->mlmext_info);
412 	struct wlan_bssid_ex	*cur_network = &(pmlmeinfo->network);
413 
414 	pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
415 
416 	mac = myid(&(adapt->eeprompriv));
417 	bssid = cur_network->MacAddress;
418 
419 	fctrl = &(pwlanhdr->frame_ctl);
420 	*(fctrl) = 0;
421 	memcpy(pwlanhdr->addr1, StaAddr, ETH_ALEN);
422 	memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
423 	memcpy(pwlanhdr->addr3, bssid, ETH_ALEN);
424 
425 	SetSeqNum(pwlanhdr, 0);
426 	SetFrameSubType(fctrl, WIFI_PROBERSP);
427 
428 	pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
429 	pframe += pktlen;
430 
431 	if (cur_network->IELength > MAX_IE_SZ)
432 		return;
433 
434 	memcpy(pframe, cur_network->IEs, cur_network->IELength);
435 	pframe += cur_network->IELength;
436 	pktlen += cur_network->IELength;
437 
438 	*pLength = pktlen;
439 }
440 
441 /*  */
442 /*  Description: Fill the reserved packets that FW will use to RSVD page. */
443 /*			Now we just send 4 types packet to rsvd page. */
444 /*			(1)Beacon, (2)Ps-poll, (3)Null data, (4)ProbeRsp. */
445 /*	Input: */
446 /*	    bDLFinished - false: At the first time we will send all the packets as a large packet to Hw, */
447 /*						so we need to set the packet length to total length. */
448 /*			      true: At the second time, we should send the first packet (default:beacon) */
449 /*						to Hw again and set the length in descriptor to the real beacon length. */
450 /*  2009.10.15 by tynli. */
SetFwRsvdPagePkt(struct adapter * adapt,bool bDLFinished)451 static void SetFwRsvdPagePkt(struct adapter *adapt, bool bDLFinished)
452 {
453 	struct hal_data_8188e *haldata;
454 	struct xmit_frame	*pmgntframe;
455 	struct pkt_attrib	*pattrib;
456 	struct xmit_priv *pxmitpriv;
457 	struct mlme_ext_priv *pmlmeext;
458 	struct mlme_ext_info	*pmlmeinfo;
459 	u32 BeaconLength = 0, ProbeRspLength = 0, PSPollLength;
460 	u32 NullDataLength, QosNullLength;
461 	u8 *ReservedPagePacket;
462 	u8 PageNum, PageNeed, TxDescLen;
463 	u16 BufIndex;
464 	u32 TotalPacketLen;
465 	struct rsvdpage_loc RsvdPageLoc;
466 	struct wlan_bssid_ex *pnetwork;
467 
468 	DBG_88E("%s\n", __func__);
469 	ReservedPagePacket = kzalloc(1000, GFP_KERNEL);
470 	if (!ReservedPagePacket) {
471 		DBG_88E("%s: alloc ReservedPagePacket fail!\n", __func__);
472 		return;
473 	}
474 
475 	haldata = GET_HAL_DATA(adapt);
476 	pxmitpriv = &adapt->xmitpriv;
477 	pmlmeext = &adapt->mlmeextpriv;
478 	pmlmeinfo = &pmlmeext->mlmext_info;
479 	pnetwork = &(pmlmeinfo->network);
480 
481 	TxDescLen = TXDESC_SIZE;
482 	PageNum = 0;
483 
484 	/* 3 (1) beacon * 2 pages */
485 	BufIndex = TXDESC_OFFSET;
486 	ConstructBeacon(adapt, &ReservedPagePacket[BufIndex], &BeaconLength);
487 
488 	/*  When we count the first page size, we need to reserve description size for the RSVD */
489 	/*  packet, it will be filled in front of the packet in TXPKTBUF. */
490 	PageNeed = (u8)PageNum_128(TxDescLen + BeaconLength);
491 	/*  To reserved 2 pages for beacon buffer. 2010.06.24. */
492 	if (PageNeed == 1)
493 		PageNeed += 1;
494 	PageNum += PageNeed;
495 	haldata->FwRsvdPageStartOffset = PageNum;
496 
497 	BufIndex += PageNeed*128;
498 
499 	/* 3 (2) ps-poll *1 page */
500 	RsvdPageLoc.LocPsPoll = PageNum;
501 	ConstructPSPoll(adapt, &ReservedPagePacket[BufIndex], &PSPollLength);
502 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex-TxDescLen], PSPollLength, true, false);
503 
504 	PageNeed = (u8)PageNum_128(TxDescLen + PSPollLength);
505 	PageNum += PageNeed;
506 
507 	BufIndex += PageNeed*128;
508 
509 	/* 3 (3) null data * 1 page */
510 	RsvdPageLoc.LocNullData = PageNum;
511 	ConstructNullFunctionData(adapt, &ReservedPagePacket[BufIndex], &NullDataLength, pnetwork->MacAddress, false, 0, 0, false);
512 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex-TxDescLen], NullDataLength, false, false);
513 
514 	PageNeed = (u8)PageNum_128(TxDescLen + NullDataLength);
515 	PageNum += PageNeed;
516 
517 	BufIndex += PageNeed*128;
518 
519 	/* 3 (4) probe response * 1page */
520 	RsvdPageLoc.LocProbeRsp = PageNum;
521 	ConstructProbeRsp(adapt, &ReservedPagePacket[BufIndex], &ProbeRspLength, pnetwork->MacAddress, false);
522 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex-TxDescLen], ProbeRspLength, false, false);
523 
524 	PageNeed = (u8)PageNum_128(TxDescLen + ProbeRspLength);
525 	PageNum += PageNeed;
526 
527 	BufIndex += PageNeed*128;
528 
529 	/* 3 (5) Qos null data */
530 	RsvdPageLoc.LocQosNull = PageNum;
531 	ConstructNullFunctionData(adapt, &ReservedPagePacket[BufIndex],
532 				  &QosNullLength, pnetwork->MacAddress, true, 0, 0, false);
533 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex-TxDescLen], QosNullLength, false, false);
534 
535 	PageNeed = (u8)PageNum_128(TxDescLen + QosNullLength);
536 	PageNum += PageNeed;
537 
538 	TotalPacketLen = BufIndex + QosNullLength;
539 	pmgntframe = alloc_mgtxmitframe(pxmitpriv);
540 	if (!pmgntframe)
541 		goto exit;
542 
543 	/*  update attribute */
544 	pattrib = &pmgntframe->attrib;
545 	update_mgntframe_attrib(adapt, pattrib);
546 	pattrib->qsel = 0x10;
547 	pattrib->last_txcmdsz = TotalPacketLen - TXDESC_OFFSET;
548 	pattrib->pktlen = pattrib->last_txcmdsz;
549 	memcpy(pmgntframe->buf_addr, ReservedPagePacket, TotalPacketLen);
550 
551 	rtw_hal_mgnt_xmit(adapt, pmgntframe);
552 
553 	DBG_88E("%s: Set RSVD page location to Fw\n", __func__);
554 	FillH2CCmd_88E(adapt, H2C_COM_RSVD_PAGE, sizeof(RsvdPageLoc), (u8 *)&RsvdPageLoc);
555 
556 exit:
557 	kfree(ReservedPagePacket);
558 }
559 
rtl8188e_set_FwJoinBssReport_cmd(struct adapter * adapt,u8 mstatus)560 void rtl8188e_set_FwJoinBssReport_cmd(struct adapter *adapt, u8 mstatus)
561 {
562 	struct hal_data_8188e *haldata = GET_HAL_DATA(adapt);
563 	struct mlme_ext_priv *pmlmeext = &(adapt->mlmeextpriv);
564 	struct mlme_ext_info	*pmlmeinfo = &(pmlmeext->mlmext_info);
565 	bool	bSendBeacon = false;
566 	bool	bcn_valid = false;
567 	u8 DLBcnCount = 0;
568 	u32 poll = 0;
569 
570 
571 	DBG_88E("%s mstatus(%x)\n", __func__, mstatus);
572 
573 	if (mstatus == 1) {
574 		/*  We should set AID, correct TSF, HW seq enable before set JoinBssReport to Fw in 88/92C. */
575 		/*  Suggested by filen. Added by tynli. */
576 		usb_write16(adapt, REG_BCN_PSR_RPT, (0xC000|pmlmeinfo->aid));
577 		/*  Do not set TSF again here or vWiFi beacon DMA INT will not work. */
578 
579 		/* Set REG_CR bit 8. DMA beacon by SW. */
580 		haldata->RegCR_1 |= BIT(0);
581 		usb_write8(adapt,  REG_CR+1, haldata->RegCR_1);
582 
583 		/*  Disable Hw protection for a time which revserd for Hw sending beacon. */
584 		/*  Fix download reserved page packet fail that access collision with the protection time. */
585 		/*  2010.05.11. Added by tynli. */
586 		usb_write8(adapt, REG_BCN_CTRL, usb_read8(adapt, REG_BCN_CTRL)&(~BIT(3)));
587 		usb_write8(adapt, REG_BCN_CTRL, usb_read8(adapt, REG_BCN_CTRL) | BIT(4));
588 
589 		if (haldata->RegFwHwTxQCtrl & BIT(6)) {
590 			DBG_88E("HalDownloadRSVDPage(): There is an Adapter is sending beacon.\n");
591 			bSendBeacon = true;
592 		}
593 
594 		/*  Set FWHW_TXQ_CTRL 0x422[6]=0 to tell Hw the packet is not a real beacon frame. */
595 		usb_write8(adapt, REG_FWHW_TXQ_CTRL+2, (haldata->RegFwHwTxQCtrl&(~BIT(6))));
596 		haldata->RegFwHwTxQCtrl &= (~BIT(6));
597 
598 		/*  Clear beacon valid check bit. */
599 		rtw_hal_set_hwreg(adapt, HW_VAR_BCN_VALID, NULL);
600 		DLBcnCount = 0;
601 		poll = 0;
602 		do {
603 			/*  download rsvd page. */
604 			SetFwRsvdPagePkt(adapt, false);
605 			DLBcnCount++;
606 			do {
607 				yield();
608 				/* mdelay(10); */
609 				/*  check rsvd page download OK. */
610 				rtw_hal_get_hwreg(adapt, HW_VAR_BCN_VALID, (u8 *)(&bcn_valid));
611 				poll++;
612 			} while (!bcn_valid && (poll%10) != 0 && !adapt->bSurpriseRemoved && !adapt->bDriverStopped);
613 		} while (!bcn_valid && DLBcnCount <= 100 && !adapt->bSurpriseRemoved && !adapt->bDriverStopped);
614 
615 		if (adapt->bSurpriseRemoved || adapt->bDriverStopped)
616 			;
617 		else if (!bcn_valid)
618 			DBG_88E("%s: 1 Download RSVD page failed! DLBcnCount:%u, poll:%u\n", __func__, DLBcnCount, poll);
619 		else
620 			DBG_88E("%s: 1 Download RSVD success! DLBcnCount:%u, poll:%u\n", __func__, DLBcnCount, poll);
621 		/*  */
622 		/*  We just can send the reserved page twice during the time that Tx thread is stopped (e.g. pnpsetpower) */
623 		/*  because we need to free the Tx BCN Desc which is used by the first reserved page packet. */
624 		/*  At run time, we cannot get the Tx Desc until it is released in TxHandleInterrupt() so we will return */
625 		/*  the beacon TCB in the following code. 2011.11.23. by tynli. */
626 		/*  */
627 
628 		/*  Enable Bcn */
629 		usb_write8(adapt, REG_BCN_CTRL, usb_read8(adapt, REG_BCN_CTRL) | BIT(3));
630 		usb_write8(adapt, REG_BCN_CTRL, usb_read8(adapt, REG_BCN_CTRL)&(~BIT(4)));
631 
632 		/*  To make sure that if there exists an adapter which would like to send beacon. */
633 		/*  If exists, the origianl value of 0x422[6] will be 1, we should check this to */
634 		/*  prevent from setting 0x422[6] to 0 after download reserved page, or it will cause */
635 		/*  the beacon cannot be sent by HW. */
636 		/*  2010.06.23. Added by tynli. */
637 		if (bSendBeacon) {
638 			usb_write8(adapt, REG_FWHW_TXQ_CTRL+2, (haldata->RegFwHwTxQCtrl | BIT(6)));
639 			haldata->RegFwHwTxQCtrl |= BIT(6);
640 		}
641 
642 		/*  Update RSVD page location H2C to Fw. */
643 		if (bcn_valid) {
644 			rtw_hal_set_hwreg(adapt, HW_VAR_BCN_VALID, NULL);
645 			DBG_88E("Set RSVD page location to Fw.\n");
646 		}
647 
648 		/*  Do not enable HW DMA BCN or it will cause Pcie interface hang by timing issue. 2011.11.24. by tynli. */
649 		/*  Clear CR[8] or beacon packet will not be send to TxBuf anymore. */
650 		haldata->RegCR_1 &= (~BIT(0));
651 		usb_write8(adapt,  REG_CR+1, haldata->RegCR_1);
652 	}
653 }
654