• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #include <linux/etherdevice.h>
8 #include <drv_types.h>
9 #include <rtw_debug.h>
10 #include <hal_btcoex.h>
11 #include <linux/jiffies.h>
12 
rtw_init_mlme_priv(struct adapter * padapter)13 int	rtw_init_mlme_priv(struct adapter *padapter)
14 {
15 	int	i;
16 	u8 *pbuf;
17 	struct wlan_network	*pnetwork;
18 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
19 	int	res = _SUCCESS;
20 
21 	pmlmepriv->nic_hdl = (u8 *)padapter;
22 
23 	pmlmepriv->pscanned = NULL;
24 	pmlmepriv->fw_state = WIFI_STATION_STATE; /*  Must sync with rtw_wdev_alloc() */
25 	/*  wdev->iftype = NL80211_IFTYPE_STATION */
26 	pmlmepriv->cur_network.network.infrastructure_mode = Ndis802_11AutoUnknown;
27 	pmlmepriv->scan_mode = SCAN_ACTIVE;/*  1: active, 0: passive. Maybe someday we should rename this varable to "active_mode" (Jeff) */
28 
29 	spin_lock_init(&pmlmepriv->lock);
30 	INIT_LIST_HEAD(&pmlmepriv->free_bss_pool.queue);
31 	spin_lock_init(&pmlmepriv->free_bss_pool.lock);
32 	INIT_LIST_HEAD(&pmlmepriv->scanned_queue.queue);
33 	spin_lock_init(&pmlmepriv->scanned_queue.lock);
34 
35 	memset(&pmlmepriv->assoc_ssid, 0, sizeof(struct ndis_802_11_ssid));
36 
37 	pbuf = vzalloc(array_size(MAX_BSS_CNT, sizeof(struct wlan_network)));
38 
39 	if (!pbuf) {
40 		res = _FAIL;
41 		goto exit;
42 	}
43 	pmlmepriv->free_bss_buf = pbuf;
44 
45 	pnetwork = (struct wlan_network *)pbuf;
46 
47 	for (i = 0; i < MAX_BSS_CNT; i++) {
48 		INIT_LIST_HEAD(&pnetwork->list);
49 
50 		list_add_tail(&pnetwork->list, &pmlmepriv->free_bss_pool.queue);
51 
52 		pnetwork++;
53 	}
54 
55 	/* allocate DMA-able/Non-Page memory for cmd_buf and rsp_buf */
56 
57 	rtw_clear_scan_deny(padapter);
58 
59 	#define RTW_ROAM_SCAN_RESULT_EXP_MS 5000
60 	#define RTW_ROAM_RSSI_DIFF_TH 10
61 	#define RTW_ROAM_SCAN_INTERVAL_MS 10000
62 
63 	pmlmepriv->roam_flags = 0
64 		| RTW_ROAM_ON_EXPIRED
65 		| RTW_ROAM_ON_RESUME
66 		;
67 
68 	pmlmepriv->roam_scanr_exp_ms = RTW_ROAM_SCAN_RESULT_EXP_MS;
69 	pmlmepriv->roam_rssi_diff_th = RTW_ROAM_RSSI_DIFF_TH;
70 	pmlmepriv->roam_scan_int_ms = RTW_ROAM_SCAN_INTERVAL_MS;
71 
72 	rtw_init_mlme_timer(padapter);
73 
74 exit:
75 
76 	return res;
77 }
78 
rtw_free_mlme_ie_data(u8 ** ppie,u32 * plen)79 static void rtw_free_mlme_ie_data(u8 **ppie, u32 *plen)
80 {
81 	if (*ppie) {
82 		kfree(*ppie);
83 		*plen = 0;
84 		*ppie = NULL;
85 	}
86 }
87 
rtw_free_mlme_priv_ie_data(struct mlme_priv * pmlmepriv)88 void rtw_free_mlme_priv_ie_data(struct mlme_priv *pmlmepriv)
89 {
90 	rtw_buf_free(&pmlmepriv->assoc_req, &pmlmepriv->assoc_req_len);
91 	rtw_buf_free(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len);
92 	rtw_free_mlme_ie_data(&pmlmepriv->wps_beacon_ie, &pmlmepriv->wps_beacon_ie_len);
93 	rtw_free_mlme_ie_data(&pmlmepriv->wps_probe_req_ie, &pmlmepriv->wps_probe_req_ie_len);
94 	rtw_free_mlme_ie_data(&pmlmepriv->wps_probe_resp_ie, &pmlmepriv->wps_probe_resp_ie_len);
95 	rtw_free_mlme_ie_data(&pmlmepriv->wps_assoc_resp_ie, &pmlmepriv->wps_assoc_resp_ie_len);
96 
97 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_beacon_ie, &pmlmepriv->p2p_beacon_ie_len);
98 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_probe_req_ie, &pmlmepriv->p2p_probe_req_ie_len);
99 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_probe_resp_ie, &pmlmepriv->p2p_probe_resp_ie_len);
100 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_go_probe_resp_ie, &pmlmepriv->p2p_go_probe_resp_ie_len);
101 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_assoc_req_ie, &pmlmepriv->p2p_assoc_req_ie_len);
102 }
103 
_rtw_free_mlme_priv(struct mlme_priv * pmlmepriv)104 void _rtw_free_mlme_priv(struct mlme_priv *pmlmepriv)
105 {
106 	if (pmlmepriv) {
107 		rtw_free_mlme_priv_ie_data(pmlmepriv);
108 		vfree(pmlmepriv->free_bss_buf);
109 	}
110 }
111 
112 /*
113 struct	wlan_network *_rtw_dequeue_network(struct __queue *queue)
114 {
115 	_irqL irqL;
116 
117 	struct wlan_network *pnetwork;
118 
119 	spin_lock_bh(&queue->lock);
120 
121 	if (list_empty(&queue->queue))
122 
123 		pnetwork = NULL;
124 
125 	else
126 	{
127 		pnetwork = container_of(get_next(&queue->queue), struct wlan_network, list);
128 
129 		list_del_init(&(pnetwork->list));
130 	}
131 
132 	spin_unlock_bh(&queue->lock);
133 
134 	return pnetwork;
135 }
136 */
137 
rtw_alloc_network(struct mlme_priv * pmlmepriv)138 struct	wlan_network *rtw_alloc_network(struct	mlme_priv *pmlmepriv)
139 {
140 	struct	wlan_network	*pnetwork;
141 	struct __queue *free_queue = &pmlmepriv->free_bss_pool;
142 	struct list_head *plist = NULL;
143 
144 	spin_lock_bh(&free_queue->lock);
145 
146 	if (list_empty(&free_queue->queue)) {
147 		pnetwork = NULL;
148 		goto exit;
149 	}
150 	plist = get_next(&(free_queue->queue));
151 
152 	pnetwork = container_of(plist, struct wlan_network, list);
153 
154 	list_del_init(&pnetwork->list);
155 
156 	pnetwork->network_type = 0;
157 	pnetwork->fixed = false;
158 	pnetwork->last_scanned = jiffies;
159 	pnetwork->aid = 0;
160 	pnetwork->join_res = 0;
161 
162 exit:
163 	spin_unlock_bh(&free_queue->lock);
164 
165 	return pnetwork;
166 }
167 
_rtw_free_network(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork,u8 isfreeall)168 void _rtw_free_network(struct	mlme_priv *pmlmepriv, struct wlan_network *pnetwork, u8 isfreeall)
169 {
170 	unsigned int delta_time;
171 	u32 lifetime = SCANQUEUE_LIFETIME;
172 /* 	_irqL irqL; */
173 	struct __queue *free_queue = &(pmlmepriv->free_bss_pool);
174 
175 	if (!pnetwork)
176 		return;
177 
178 	if (pnetwork->fixed)
179 		return;
180 
181 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) ||
182 		(check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true))
183 		lifetime = 1;
184 
185 	if (!isfreeall) {
186 		delta_time = jiffies_to_msecs(jiffies - pnetwork->last_scanned);
187 		if (delta_time < lifetime)/*  unit:msec */
188 			return;
189 	}
190 
191 	spin_lock_bh(&free_queue->lock);
192 
193 	list_del_init(&(pnetwork->list));
194 
195 	list_add_tail(&(pnetwork->list), &(free_queue->queue));
196 
197 	spin_unlock_bh(&free_queue->lock);
198 }
199 
_rtw_free_network_nolock(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork)200 void _rtw_free_network_nolock(struct	mlme_priv *pmlmepriv, struct wlan_network *pnetwork)
201 {
202 
203 	struct __queue *free_queue = &(pmlmepriv->free_bss_pool);
204 
205 	if (!pnetwork)
206 		return;
207 
208 	if (pnetwork->fixed)
209 		return;
210 
211 	/* spin_lock_irqsave(&free_queue->lock, irqL); */
212 
213 	list_del_init(&(pnetwork->list));
214 
215 	list_add_tail(&(pnetwork->list), get_list_head(free_queue));
216 
217 	/* spin_unlock_irqrestore(&free_queue->lock, irqL); */
218 }
219 
220 /*
221 	return the wlan_network with the matching addr
222 
223 	Shall be called under atomic context... to avoid possible racing condition...
224 */
_rtw_find_network(struct __queue * scanned_queue,u8 * addr)225 struct wlan_network *_rtw_find_network(struct __queue *scanned_queue, u8 *addr)
226 {
227 	struct list_head	*phead, *plist;
228 	struct	wlan_network *pnetwork = NULL;
229 
230 	if (is_zero_ether_addr(addr)) {
231 		pnetwork = NULL;
232 		goto exit;
233 	}
234 
235 	/* spin_lock_bh(&scanned_queue->lock); */
236 
237 	phead = get_list_head(scanned_queue);
238 	list_for_each(plist, phead) {
239 		pnetwork = list_entry(plist, struct wlan_network, list);
240 
241 		if (!memcmp(addr, pnetwork->network.mac_address, ETH_ALEN))
242 			break;
243 	}
244 
245 	if (plist == phead)
246 		pnetwork = NULL;
247 
248 	/* spin_unlock_bh(&scanned_queue->lock); */
249 
250 exit:
251 	return pnetwork;
252 }
253 
rtw_free_network_queue(struct adapter * padapter,u8 isfreeall)254 void rtw_free_network_queue(struct adapter *padapter, u8 isfreeall)
255 {
256 	struct list_head *phead, *plist, *tmp;
257 	struct wlan_network *pnetwork;
258 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
259 	struct __queue *scanned_queue = &pmlmepriv->scanned_queue;
260 
261 	spin_lock_bh(&scanned_queue->lock);
262 
263 	phead = get_list_head(scanned_queue);
264 	list_for_each_safe(plist, tmp, phead) {
265 
266 		pnetwork = list_entry(plist, struct wlan_network, list);
267 
268 		_rtw_free_network(pmlmepriv, pnetwork, isfreeall);
269 
270 	}
271 
272 	spin_unlock_bh(&scanned_queue->lock);
273 }
274 
rtw_if_up(struct adapter * padapter)275 signed int rtw_if_up(struct adapter *padapter)
276 {
277 	signed int res;
278 
279 	if (padapter->bDriverStopped || padapter->bSurpriseRemoved ||
280 		(check_fwstate(&padapter->mlmepriv, _FW_LINKED) == false))
281 		res = false;
282 	else
283 		res =  true;
284 
285 	return res;
286 }
287 
rtw_generate_random_ibss(u8 * pibss)288 void rtw_generate_random_ibss(u8 *pibss)
289 {
290 	unsigned long curtime = jiffies;
291 
292 	pibss[0] = 0x02;  /* in ad-hoc mode bit1 must set to 1 */
293 	pibss[1] = 0x11;
294 	pibss[2] = 0x87;
295 	pibss[3] = (u8)(curtime & 0xff) ;/* p[0]; */
296 	pibss[4] = (u8)((curtime>>8) & 0xff) ;/* p[1]; */
297 	pibss[5] = (u8)((curtime>>16) & 0xff) ;/* p[2]; */
298 }
299 
rtw_get_capability_from_ie(u8 * ie)300 u8 *rtw_get_capability_from_ie(u8 *ie)
301 {
302 	return ie + 8 + 2;
303 }
304 
rtw_get_capability(struct wlan_bssid_ex * bss)305 u16 rtw_get_capability(struct wlan_bssid_ex *bss)
306 {
307 	__le16	val;
308 
309 	memcpy((u8 *)&val, rtw_get_capability_from_ie(bss->ies), 2);
310 
311 	return le16_to_cpu(val);
312 }
313 
rtw_get_beacon_interval_from_ie(u8 * ie)314 u8 *rtw_get_beacon_interval_from_ie(u8 *ie)
315 {
316 	return ie + 8;
317 }
318 
rtw_free_mlme_priv(struct mlme_priv * pmlmepriv)319 void rtw_free_mlme_priv(struct mlme_priv *pmlmepriv)
320 {
321 	_rtw_free_mlme_priv(pmlmepriv);
322 }
323 
324 /*
325 static struct	wlan_network *rtw_dequeue_network(struct __queue *queue)
326 {
327 	struct wlan_network *pnetwork;
328 
329 	pnetwork = _rtw_dequeue_network(queue);
330 	return pnetwork;
331 }
332 */
333 
334 void rtw_free_network_nolock(struct adapter *padapter, struct wlan_network *pnetwork);
rtw_free_network_nolock(struct adapter * padapter,struct wlan_network * pnetwork)335 void rtw_free_network_nolock(struct adapter *padapter, struct wlan_network *pnetwork)
336 {
337 	_rtw_free_network_nolock(&(padapter->mlmepriv), pnetwork);
338 	rtw_cfg80211_unlink_bss(padapter, pnetwork);
339 }
340 
341 /*
342 	return the wlan_network with the matching addr
343 
344 	Shall be called under atomic context... to avoid possible racing condition...
345 */
rtw_find_network(struct __queue * scanned_queue,u8 * addr)346 struct	wlan_network *rtw_find_network(struct __queue *scanned_queue, u8 *addr)
347 {
348 	struct	wlan_network *pnetwork = _rtw_find_network(scanned_queue, addr);
349 
350 	return pnetwork;
351 }
352 
rtw_is_same_ibss(struct adapter * adapter,struct wlan_network * pnetwork)353 int rtw_is_same_ibss(struct adapter *adapter, struct wlan_network *pnetwork)
354 {
355 	int ret = true;
356 	struct security_priv *psecuritypriv = &adapter->securitypriv;
357 
358 	if ((psecuritypriv->dot11PrivacyAlgrthm != _NO_PRIVACY_) &&
359 		    (pnetwork->network.privacy == 0))
360 		ret = false;
361 	else if ((psecuritypriv->dot11PrivacyAlgrthm == _NO_PRIVACY_) &&
362 		 (pnetwork->network.privacy == 1))
363 		ret = false;
364 	else
365 		ret = true;
366 
367 	return ret;
368 
369 }
370 
is_same_ess(struct wlan_bssid_ex * a,struct wlan_bssid_ex * b)371 inline int is_same_ess(struct wlan_bssid_ex *a, struct wlan_bssid_ex *b)
372 {
373 	return (a->ssid.ssid_length == b->ssid.ssid_length)
374 		&&  !memcmp(a->ssid.ssid, b->ssid.ssid, a->ssid.ssid_length);
375 }
376 
is_same_network(struct wlan_bssid_ex * src,struct wlan_bssid_ex * dst,u8 feature)377 int is_same_network(struct wlan_bssid_ex *src, struct wlan_bssid_ex *dst, u8 feature)
378 {
379 	u16 s_cap, d_cap;
380 	__le16 tmps, tmpd;
381 
382 	if (rtw_bug_check(dst, src, &s_cap, &d_cap) == false)
383 		return false;
384 
385 	memcpy((u8 *)&tmps, rtw_get_capability_from_ie(src->ies), 2);
386 	memcpy((u8 *)&tmpd, rtw_get_capability_from_ie(dst->ies), 2);
387 
388 	s_cap = le16_to_cpu(tmps);
389 	d_cap = le16_to_cpu(tmpd);
390 
391 	return (src->ssid.ssid_length == dst->ssid.ssid_length) &&
392 		/* 	(src->configuration.ds_config == dst->configuration.ds_config) && */
393 			((!memcmp(src->mac_address, dst->mac_address, ETH_ALEN))) &&
394 			((!memcmp(src->ssid.ssid, dst->ssid.ssid, src->ssid.ssid_length))) &&
395 			((s_cap & WLAN_CAPABILITY_IBSS) ==
396 			(d_cap & WLAN_CAPABILITY_IBSS)) &&
397 			((s_cap & WLAN_CAPABILITY_ESS) ==
398 			(d_cap & WLAN_CAPABILITY_ESS));
399 
400 }
401 
_rtw_find_same_network(struct __queue * scanned_queue,struct wlan_network * network)402 struct wlan_network *_rtw_find_same_network(struct __queue *scanned_queue, struct wlan_network *network)
403 {
404 	struct list_head *phead, *plist;
405 	struct wlan_network *found = NULL;
406 
407 	phead = get_list_head(scanned_queue);
408 	list_for_each(plist, phead) {
409 		found = list_entry(plist, struct wlan_network, list);
410 
411 		if (is_same_network(&network->network, &found->network, 0))
412 			break;
413 	}
414 
415 	if (plist == phead)
416 		found = NULL;
417 
418 	return found;
419 }
420 
rtw_get_oldest_wlan_network(struct __queue * scanned_queue)421 struct	wlan_network	*rtw_get_oldest_wlan_network(struct __queue *scanned_queue)
422 {
423 	struct list_head	*plist, *phead;
424 
425 	struct	wlan_network	*pwlan = NULL;
426 	struct	wlan_network	*oldest = NULL;
427 
428 	phead = get_list_head(scanned_queue);
429 
430 	list_for_each(plist, phead) {
431 
432 		pwlan = list_entry(plist, struct wlan_network, list);
433 
434 		if (!pwlan->fixed) {
435 			if (!oldest || time_after(oldest->last_scanned, pwlan->last_scanned))
436 				oldest = pwlan;
437 		}
438 	}
439 	return oldest;
440 
441 }
442 
update_network(struct wlan_bssid_ex * dst,struct wlan_bssid_ex * src,struct adapter * padapter,bool update_ie)443 void update_network(struct wlan_bssid_ex *dst, struct wlan_bssid_ex *src,
444 	struct adapter *padapter, bool update_ie)
445 {
446 	long rssi_ori = dst->rssi;
447 
448 	u8 sq_smp = src->phy_info.signal_quality;
449 
450 	u8 ss_final;
451 	u8 sq_final;
452 	long rssi_final;
453 
454 	/* The rule below is 1/5 for sample value, 4/5 for history value */
455 	if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) && is_same_network(&(padapter->mlmepriv.cur_network.network), src, 0)) {
456 		/* Take the recvpriv's value for the connected AP*/
457 		ss_final = padapter->recvpriv.signal_strength;
458 		sq_final = padapter->recvpriv.signal_qual;
459 		/* the rssi value here is undecorated, and will be used for antenna diversity */
460 		if (sq_smp != 101) /* from the right channel */
461 			rssi_final = (src->rssi+dst->rssi*4)/5;
462 		else
463 			rssi_final = rssi_ori;
464 	} else {
465 		if (sq_smp != 101) { /* from the right channel */
466 			ss_final = ((u32)(src->phy_info.signal_strength)+(u32)(dst->phy_info.signal_strength)*4)/5;
467 			sq_final = ((u32)(src->phy_info.signal_quality)+(u32)(dst->phy_info.signal_quality)*4)/5;
468 			rssi_final = (src->rssi+dst->rssi*4)/5;
469 		} else {
470 			/* bss info not receiving from the right channel, use the original RX signal infos */
471 			ss_final = dst->phy_info.signal_strength;
472 			sq_final = dst->phy_info.signal_quality;
473 			rssi_final = dst->rssi;
474 		}
475 
476 	}
477 
478 	if (update_ie) {
479 		dst->reserved[0] = src->reserved[0];
480 		dst->reserved[1] = src->reserved[1];
481 		memcpy((u8 *)dst, (u8 *)src, get_wlan_bssid_ex_sz(src));
482 	}
483 
484 	dst->phy_info.signal_strength = ss_final;
485 	dst->phy_info.signal_quality = sq_final;
486 	dst->rssi = rssi_final;
487 }
488 
update_current_network(struct adapter * adapter,struct wlan_bssid_ex * pnetwork)489 static void update_current_network(struct adapter *adapter, struct wlan_bssid_ex *pnetwork)
490 {
491 	struct	mlme_priv *pmlmepriv = &(adapter->mlmepriv);
492 
493 	rtw_bug_check(&(pmlmepriv->cur_network.network),
494 		&(pmlmepriv->cur_network.network),
495 		&(pmlmepriv->cur_network.network),
496 		&(pmlmepriv->cur_network.network));
497 
498 	if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) && (is_same_network(&(pmlmepriv->cur_network.network), pnetwork, 0))) {
499 		/* if (pmlmepriv->cur_network.network.ie_length<= pnetwork->ie_length) */
500 		{
501 			update_network(&(pmlmepriv->cur_network.network), pnetwork, adapter, true);
502 			rtw_update_protection(adapter, (pmlmepriv->cur_network.network.ies) + sizeof(struct ndis_802_11_fix_ie),
503 									pmlmepriv->cur_network.network.ie_length);
504 		}
505 	}
506 }
507 
508 /*
509 Caller must hold pmlmepriv->lock first.
510 */
rtw_update_scanned_network(struct adapter * adapter,struct wlan_bssid_ex * target)511 void rtw_update_scanned_network(struct adapter *adapter, struct wlan_bssid_ex *target)
512 {
513 	struct list_head	*plist, *phead;
514 	u32 bssid_ex_sz;
515 	struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
516 	struct __queue	*queue	= &(pmlmepriv->scanned_queue);
517 	struct wlan_network	*pnetwork = NULL;
518 	struct wlan_network	*oldest = NULL;
519 	int target_find = 0;
520 	u8 feature = 0;
521 
522 	spin_lock_bh(&queue->lock);
523 	phead = get_list_head(queue);
524 	list_for_each(plist, phead) {
525 		pnetwork = list_entry(plist, struct wlan_network, list);
526 
527 		rtw_bug_check(pnetwork, pnetwork, pnetwork, pnetwork);
528 
529 		if (is_same_network(&(pnetwork->network), target, feature)) {
530 			target_find = 1;
531 			break;
532 		}
533 
534 		if (rtw_roam_flags(adapter)) {
535 			/* TODO: don't select network in the same ess as oldest if it's new enough*/
536 		}
537 
538 		if (!oldest || time_after(oldest->last_scanned, pnetwork->last_scanned))
539 			oldest = pnetwork;
540 
541 	}
542 
543 	/* If we didn't find a match, then get a new network slot to initialize
544 	 * with this beacon's information */
545 	/* if (phead == plist) { */
546 	if (!target_find) {
547 		if (list_empty(&pmlmepriv->free_bss_pool.queue)) {
548 			/* If there are no more slots, expire the oldest */
549 			/* list_del_init(&oldest->list); */
550 			pnetwork = oldest;
551 			if (!pnetwork)
552 				goto exit;
553 
554 			memcpy(&(pnetwork->network), target,  get_wlan_bssid_ex_sz(target));
555 			/*  variable initialize */
556 			pnetwork->fixed = false;
557 			pnetwork->last_scanned = jiffies;
558 
559 			pnetwork->network_type = 0;
560 			pnetwork->aid = 0;
561 			pnetwork->join_res = 0;
562 
563 			/* bss info not receiving from the right channel */
564 			if (pnetwork->network.phy_info.signal_quality == 101)
565 				pnetwork->network.phy_info.signal_quality = 0;
566 		} else {
567 			/* Otherwise just pull from the free list */
568 
569 			pnetwork = rtw_alloc_network(pmlmepriv); /*  will update scan_time */
570 
571 			if (!pnetwork)
572 				goto exit;
573 
574 			bssid_ex_sz = get_wlan_bssid_ex_sz(target);
575 			target->length = bssid_ex_sz;
576 			memcpy(&(pnetwork->network), target, bssid_ex_sz);
577 
578 			pnetwork->last_scanned = jiffies;
579 
580 			/* bss info not receiving from the right channel */
581 			if (pnetwork->network.phy_info.signal_quality == 101)
582 				pnetwork->network.phy_info.signal_quality = 0;
583 
584 			list_add_tail(&(pnetwork->list), &(queue->queue));
585 
586 		}
587 	} else {
588 		/* we have an entry and we are going to update it. But this entry may
589 		 * be already expired. In this case we do the same as we found a new
590 		 * net and call the new_net handler
591 		 */
592 		bool update_ie = true;
593 
594 		pnetwork->last_scanned = jiffies;
595 
596 		/* target.reserved[0]== 1, means that scanned network is a bcn frame. */
597 		if (pnetwork->network.ie_length > target->ie_length && target->reserved[0] == 1)
598 			update_ie = false;
599 
600 		/*  probe resp(3) > beacon(1) > probe req(2) */
601 		if (target->reserved[0] != 2 &&
602 		    target->reserved[0] >= pnetwork->network.reserved[0]) {
603 			update_ie = true;
604 		} else {
605 			update_ie = false;
606 		}
607 
608 		update_network(&(pnetwork->network), target, adapter, update_ie);
609 	}
610 
611 exit:
612 	spin_unlock_bh(&queue->lock);
613 }
614 
615 void rtw_add_network(struct adapter *adapter, struct wlan_bssid_ex *pnetwork);
rtw_add_network(struct adapter * adapter,struct wlan_bssid_ex * pnetwork)616 void rtw_add_network(struct adapter *adapter, struct wlan_bssid_ex *pnetwork)
617 {
618 	/* struct __queue	*queue	= &(pmlmepriv->scanned_queue); */
619 
620 	/* spin_lock_bh(&queue->lock); */
621 
622 	update_current_network(adapter, pnetwork);
623 
624 	rtw_update_scanned_network(adapter, pnetwork);
625 
626 	/* spin_unlock_bh(&queue->lock); */
627 }
628 
629 /* select the desired network based on the capability of the (i)bss. */
630 /*  check items: (1) security */
631 /* 			   (2) network_type */
632 /* 			   (3) WMM */
633 /* 			   (4) HT */
634 /*                      (5) others */
635 int rtw_is_desired_network(struct adapter *adapter, struct wlan_network *pnetwork);
rtw_is_desired_network(struct adapter * adapter,struct wlan_network * pnetwork)636 int rtw_is_desired_network(struct adapter *adapter, struct wlan_network *pnetwork)
637 {
638 	struct security_priv *psecuritypriv = &adapter->securitypriv;
639 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
640 	u32 desired_encmode;
641 	u32 privacy;
642 
643 	/* u8 wps_ie[512]; */
644 	uint wps_ielen;
645 
646 	int bselected = true;
647 
648 	desired_encmode = psecuritypriv->ndisencryptstatus;
649 	privacy = pnetwork->network.privacy;
650 
651 	if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) {
652 		if (rtw_get_wps_ie(pnetwork->network.ies+_FIXED_IE_LENGTH_, pnetwork->network.ie_length-_FIXED_IE_LENGTH_, NULL, &wps_ielen))
653 			return true;
654 		else
655 			return false;
656 
657 	}
658 	if (adapter->registrypriv.wifi_spec == 1) { /* for  correct flow of 8021X  to do.... */
659 		u8 *p = NULL;
660 		uint ie_len = 0;
661 
662 		if ((desired_encmode == Ndis802_11EncryptionDisabled) && (privacy != 0))
663 			bselected = false;
664 
665 		if (psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPA2PSK) {
666 			p = rtw_get_ie(pnetwork->network.ies + _BEACON_IE_OFFSET_, WLAN_EID_RSN, &ie_len, (pnetwork->network.ie_length - _BEACON_IE_OFFSET_));
667 			if (p && ie_len > 0)
668 				bselected = true;
669 			else
670 				bselected = false;
671 		}
672 	}
673 
674 	if ((desired_encmode != Ndis802_11EncryptionDisabled) && (privacy == 0))
675 		bselected = false;
676 
677 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true) {
678 		if (pnetwork->network.infrastructure_mode != pmlmepriv->cur_network.network.infrastructure_mode)
679 			bselected = false;
680 	}
681 
682 	return bselected;
683 }
684 
685 /* TODO: Perry : For Power Management */
rtw_atimdone_event_callback(struct adapter * adapter,u8 * pbuf)686 void rtw_atimdone_event_callback(struct adapter	*adapter, u8 *pbuf)
687 {
688 }
689 
rtw_survey_event_callback(struct adapter * adapter,u8 * pbuf)690 void rtw_survey_event_callback(struct adapter	*adapter, u8 *pbuf)
691 {
692 	u32 len;
693 	struct wlan_bssid_ex *pnetwork;
694 	struct	mlme_priv *pmlmepriv = &(adapter->mlmepriv);
695 
696 	pnetwork = (struct wlan_bssid_ex *)pbuf;
697 
698 	len = get_wlan_bssid_ex_sz(pnetwork);
699 	if (len > (sizeof(struct wlan_bssid_ex)))
700 		return;
701 
702 	spin_lock_bh(&pmlmepriv->lock);
703 
704 	/*  update IBSS_network 's timestamp */
705 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) == true) {
706 		if (!memcmp(&(pmlmepriv->cur_network.network.mac_address), pnetwork->mac_address, ETH_ALEN)) {
707 			struct wlan_network *ibss_wlan = NULL;
708 
709 			memcpy(pmlmepriv->cur_network.network.ies, pnetwork->ies, 8);
710 			spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
711 			ibss_wlan = rtw_find_network(&pmlmepriv->scanned_queue,  pnetwork->mac_address);
712 			if (ibss_wlan) {
713 				memcpy(ibss_wlan->network.ies, pnetwork->ies, 8);
714 				spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
715 				goto exit;
716 			}
717 			spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
718 		}
719 	}
720 
721 	/*  lock pmlmepriv->lock when you accessing network_q */
722 	if ((check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) == false) {
723 		if (pnetwork->ssid.ssid[0] == 0)
724 			pnetwork->ssid.ssid_length = 0;
725 		rtw_add_network(adapter, pnetwork);
726 	}
727 
728 exit:
729 
730 	spin_unlock_bh(&pmlmepriv->lock);
731 }
732 
rtw_surveydone_event_callback(struct adapter * adapter,u8 * pbuf)733 void rtw_surveydone_event_callback(struct adapter	*adapter, u8 *pbuf)
734 {
735 	struct	mlme_priv *pmlmepriv = &(adapter->mlmepriv);
736 
737 	spin_lock_bh(&pmlmepriv->lock);
738 	if (pmlmepriv->wps_probe_req_ie) {
739 		pmlmepriv->wps_probe_req_ie_len = 0;
740 		kfree(pmlmepriv->wps_probe_req_ie);
741 		pmlmepriv->wps_probe_req_ie = NULL;
742 	}
743 
744 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)) {
745 		spin_unlock_bh(&pmlmepriv->lock);
746 		del_timer_sync(&pmlmepriv->scan_to_timer);
747 		spin_lock_bh(&pmlmepriv->lock);
748 		_clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
749 	}
750 
751 	rtw_set_signal_stat_timer(&adapter->recvpriv);
752 
753 	if (pmlmepriv->to_join) {
754 		if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true)) {
755 			if (check_fwstate(pmlmepriv, _FW_LINKED) == false) {
756 				set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
757 
758 				if (rtw_select_and_join_from_scanned_queue(pmlmepriv) == _SUCCESS) {
759 					_set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
760 				} else {
761 					u8 ret = _SUCCESS;
762 					struct wlan_bssid_ex    *pdev_network = &(adapter->registrypriv.dev_network);
763 					u8 *pibss = adapter->registrypriv.dev_network.mac_address;
764 
765 					/* pmlmepriv->fw_state ^= _FW_UNDER_SURVEY;because don't set assoc_timer */
766 					_clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
767 
768 					memcpy(&pdev_network->ssid, &pmlmepriv->assoc_ssid, sizeof(struct ndis_802_11_ssid));
769 
770 					rtw_update_registrypriv_dev_network(adapter);
771 					rtw_generate_random_ibss(pibss);
772 
773 					pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;
774 
775 					pmlmepriv->to_join = false;
776 
777 					ret = rtw_createbss_cmd(adapter);
778 					if (ret != _SUCCESS)
779 						goto unlock;
780 				}
781 			}
782 		} else {
783 			int s_ret;
784 
785 			set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
786 			pmlmepriv->to_join = false;
787 			s_ret = rtw_select_and_join_from_scanned_queue(pmlmepriv);
788 			if (s_ret == _SUCCESS) {
789 				_set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
790 			} else if (s_ret == 2) {/* there is no need to wait for join */
791 				_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
792 				rtw_indicate_connect(adapter);
793 			} else {
794 				if (rtw_to_roam(adapter) != 0) {
795 					if (rtw_dec_to_roam(adapter) == 0
796 						|| _SUCCESS != rtw_sitesurvey_cmd(adapter, &pmlmepriv->assoc_ssid, 1, NULL, 0)
797 					) {
798 						rtw_set_to_roam(adapter, 0);
799 						rtw_free_assoc_resources(adapter, 1);
800 						rtw_indicate_disconnect(adapter);
801 					} else {
802 						pmlmepriv->to_join = true;
803 					}
804 				} else
805 					rtw_indicate_disconnect(adapter);
806 
807 				_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
808 			}
809 		}
810 	} else {
811 		if (rtw_chk_roam_flags(adapter, RTW_ROAM_ACTIVE)) {
812 			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)
813 				&& check_fwstate(pmlmepriv, _FW_LINKED)) {
814 				if (rtw_select_roaming_candidate(pmlmepriv) == _SUCCESS) {
815 					receive_disconnect(adapter, pmlmepriv->cur_network.network.mac_address
816 						, WLAN_REASON_ACTIVE_ROAM);
817 				}
818 			}
819 		}
820 	}
821 
822 unlock:
823 	spin_unlock_bh(&pmlmepriv->lock);
824 
825 	rtw_os_xmit_schedule(adapter);
826 
827 	rtw_cfg80211_surveydone_event_callback(adapter);
828 
829 	rtw_indicate_scan_done(adapter, false);
830 }
831 
rtw_dummy_event_callback(struct adapter * adapter,u8 * pbuf)832 void rtw_dummy_event_callback(struct adapter *adapter, u8 *pbuf)
833 {
834 }
835 
rtw_fwdbg_event_callback(struct adapter * adapter,u8 * pbuf)836 void rtw_fwdbg_event_callback(struct adapter *adapter, u8 *pbuf)
837 {
838 }
839 
free_scanqueue(struct mlme_priv * pmlmepriv)840 static void free_scanqueue(struct	mlme_priv *pmlmepriv)
841 {
842 	struct __queue *free_queue = &pmlmepriv->free_bss_pool;
843 	struct __queue *scan_queue = &pmlmepriv->scanned_queue;
844 	struct list_head	*plist, *phead, *ptemp;
845 
846 	spin_lock_bh(&scan_queue->lock);
847 	spin_lock_bh(&free_queue->lock);
848 
849 	phead = get_list_head(scan_queue);
850 	plist = get_next(phead);
851 
852 	while (plist != phead) {
853 		ptemp = get_next(plist);
854 		list_del_init(plist);
855 		list_add_tail(plist, &free_queue->queue);
856 		plist = ptemp;
857 	}
858 
859 	spin_unlock_bh(&free_queue->lock);
860 	spin_unlock_bh(&scan_queue->lock);
861 }
862 
rtw_reset_rx_info(struct debug_priv * pdbgpriv)863 static void rtw_reset_rx_info(struct debug_priv *pdbgpriv)
864 {
865 	pdbgpriv->dbg_rx_ampdu_drop_count = 0;
866 	pdbgpriv->dbg_rx_ampdu_forced_indicate_count = 0;
867 	pdbgpriv->dbg_rx_ampdu_loss_count = 0;
868 	pdbgpriv->dbg_rx_dup_mgt_frame_drop_count = 0;
869 	pdbgpriv->dbg_rx_ampdu_window_shift_cnt = 0;
870 }
871 
find_network(struct adapter * adapter)872 static void find_network(struct adapter *adapter)
873 {
874 	struct wlan_network *pwlan = NULL;
875 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
876 	struct wlan_network *tgt_network = &pmlmepriv->cur_network;
877 
878 	pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.mac_address);
879 	if (pwlan)
880 		pwlan->fixed = false;
881 
882 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) &&
883 	    (adapter->stapriv.asoc_sta_count == 1))
884 		rtw_free_network_nolock(adapter, pwlan);
885 }
886 
887 /*
888 *rtw_free_assoc_resources: the caller has to lock pmlmepriv->lock
889 */
rtw_free_assoc_resources(struct adapter * adapter,int lock_scanned_queue)890 void rtw_free_assoc_resources(struct adapter *adapter, int lock_scanned_queue)
891 {
892 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
893 	struct wlan_network *tgt_network = &pmlmepriv->cur_network;
894 	struct dvobj_priv *psdpriv = adapter->dvobj;
895 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
896 
897 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE|WIFI_AP_STATE)) {
898 		struct sta_info *psta;
899 
900 		psta = rtw_get_stainfo(&adapter->stapriv, tgt_network->network.mac_address);
901 		rtw_free_stainfo(adapter,  psta);
902 	}
903 
904 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE|WIFI_AP_STATE)) {
905 		struct sta_info *psta;
906 
907 		rtw_free_all_stainfo(adapter);
908 
909 		psta = rtw_get_bcmc_stainfo(adapter);
910 		rtw_free_stainfo(adapter, psta);
911 
912 		rtw_init_bcmc_stainfo(adapter);
913 	}
914 
915 	find_network(adapter);
916 
917 	if (lock_scanned_queue)
918 		adapter->securitypriv.key_mask = 0;
919 
920 	rtw_reset_rx_info(pdbgpriv);
921 }
922 
923 /*
924 *rtw_indicate_connect: the caller has to lock pmlmepriv->lock
925 */
rtw_indicate_connect(struct adapter * padapter)926 void rtw_indicate_connect(struct adapter *padapter)
927 {
928 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
929 
930 	pmlmepriv->to_join = false;
931 
932 	if (!check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
933 
934 		set_fwstate(pmlmepriv, _FW_LINKED);
935 
936 		rtw_os_indicate_connect(padapter);
937 	}
938 
939 	rtw_set_to_roam(padapter, 0);
940 	rtw_set_scan_deny(padapter, 3000);
941 
942 }
943 
944 /*
945 *rtw_indicate_disconnect: the caller has to lock pmlmepriv->lock
946 */
rtw_indicate_disconnect(struct adapter * padapter)947 void rtw_indicate_disconnect(struct adapter *padapter)
948 {
949 	struct	mlme_priv *pmlmepriv = &padapter->mlmepriv;
950 
951 	_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING|WIFI_UNDER_WPS);
952 
953 	if (rtw_to_roam(padapter) > 0)
954 		_clr_fwstate_(pmlmepriv, _FW_LINKED);
955 
956 	if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)
957 		|| (rtw_to_roam(padapter) <= 0)
958 	) {
959 		rtw_os_indicate_disconnect(padapter);
960 
961 		/* set ips_deny_time to avoid enter IPS before LPS leave */
962 		rtw_set_ips_deny(padapter, 3000);
963 
964 		_clr_fwstate_(pmlmepriv, _FW_LINKED);
965 
966 		rtw_clear_scan_deny(padapter);
967 	}
968 
969 	rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_DISCONNECT, 1);
970 }
971 
rtw_indicate_scan_done(struct adapter * padapter,bool aborted)972 inline void rtw_indicate_scan_done(struct adapter *padapter, bool aborted)
973 {
974 	rtw_os_indicate_scan_done(padapter, aborted);
975 
976 	if (is_primary_adapter(padapter) &&
977 	    (!adapter_to_pwrctl(padapter)->bInSuspend) &&
978 	    (!check_fwstate(&padapter->mlmepriv,
979 			    WIFI_ASOC_STATE|WIFI_UNDER_LINKING))) {
980 		rtw_set_ips_deny(padapter, 0);
981 		_set_timer(&padapter->mlmepriv.dynamic_chk_timer, 1);
982 	}
983 }
984 
rtw_scan_abort(struct adapter * adapter)985 void rtw_scan_abort(struct adapter *adapter)
986 {
987 	unsigned long start;
988 	struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
989 	struct mlme_ext_priv *pmlmeext = &(adapter->mlmeextpriv);
990 
991 	start = jiffies;
992 	pmlmeext->scan_abort = true;
993 	while (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)
994 		&& jiffies_to_msecs(start) <= 200) {
995 
996 		if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
997 			break;
998 
999 		msleep(20);
1000 	}
1001 
1002 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY))
1003 		rtw_indicate_scan_done(adapter, true);
1004 
1005 	pmlmeext->scan_abort = false;
1006 }
1007 
rtw_joinbss_update_stainfo(struct adapter * padapter,struct wlan_network * pnetwork)1008 static struct sta_info *rtw_joinbss_update_stainfo(struct adapter *padapter, struct wlan_network *pnetwork)
1009 {
1010 	int i;
1011 	struct sta_info *bmc_sta, *psta = NULL;
1012 	struct recv_reorder_ctrl *preorder_ctrl;
1013 	struct sta_priv *pstapriv = &padapter->stapriv;
1014 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1015 
1016 	psta = rtw_get_stainfo(pstapriv, pnetwork->network.mac_address);
1017 	if (!psta)
1018 		psta = rtw_alloc_stainfo(pstapriv, pnetwork->network.mac_address);
1019 
1020 	if (psta) { /* update ptarget_sta */
1021 
1022 		psta->aid  = pnetwork->join_res;
1023 
1024 		update_sta_info(padapter, psta);
1025 
1026 		/* update station supportRate */
1027 		psta->bssratelen = rtw_get_rateset_len(pnetwork->network.supported_rates);
1028 		memcpy(psta->bssrateset, pnetwork->network.supported_rates, psta->bssratelen);
1029 		rtw_hal_update_sta_rate_mask(padapter, psta);
1030 
1031 		psta->wireless_mode = pmlmeext->cur_wireless_mode;
1032 		psta->raid = networktype_to_raid_ex(padapter, psta);
1033 
1034 		/* sta mode */
1035 		rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, true);
1036 
1037 		/* security related */
1038 		if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) {
1039 			padapter->securitypriv.binstallGrpkey = false;
1040 			padapter->securitypriv.busetkipkey = false;
1041 			padapter->securitypriv.bgrpkey_handshake = false;
1042 
1043 			psta->ieee8021x_blocked = true;
1044 			psta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
1045 
1046 			memset((u8 *)&psta->dot118021x_UncstKey, 0, sizeof(union Keytype));
1047 
1048 			memset((u8 *)&psta->dot11tkiprxmickey, 0, sizeof(union Keytype));
1049 			memset((u8 *)&psta->dot11tkiptxmickey, 0, sizeof(union Keytype));
1050 
1051 			memset((u8 *)&psta->dot11txpn, 0, sizeof(union pn48));
1052 			psta->dot11txpn.val = psta->dot11txpn.val + 1;
1053 			memset((u8 *)&psta->dot11wtxpn, 0, sizeof(union pn48));
1054 			memset((u8 *)&psta->dot11rxpn, 0, sizeof(union pn48));
1055 		}
1056 
1057 		/* 	Commented by Albert 2012/07/21 */
1058 		/* 	When doing the WPS, the wps_ie_len won't equal to 0 */
1059 		/* 	And the Wi-Fi driver shouldn't allow the data packet to be transmitted. */
1060 		if (padapter->securitypriv.wps_ie_len != 0) {
1061 			psta->ieee8021x_blocked = true;
1062 			padapter->securitypriv.wps_ie_len = 0;
1063 		}
1064 
1065 		/* for A-MPDU Rx reordering buffer control for bmc_sta & sta_info */
1066 		/* if A-MPDU Rx is enabled, resetting  rx_ordering_ctrl wstart_b(indicate_seq) to default value = 0xffff */
1067 		/* todo: check if AP can send A-MPDU packets */
1068 		for (i = 0; i < 16 ; i++) {
1069 			/* preorder_ctrl = &precvpriv->recvreorder_ctrl[i]; */
1070 			preorder_ctrl = &psta->recvreorder_ctrl[i];
1071 			preorder_ctrl->enable = false;
1072 			preorder_ctrl->indicate_seq = 0xffff;
1073 			preorder_ctrl->wend_b = 0xffff;
1074 			preorder_ctrl->wsize_b = 64;/* max_ampdu_sz;ex. 32(kbytes) -> wsize_b =32 */
1075 		}
1076 
1077 		bmc_sta = rtw_get_bcmc_stainfo(padapter);
1078 		if (bmc_sta) {
1079 			for (i = 0; i < 16 ; i++) {
1080 				/* preorder_ctrl = &precvpriv->recvreorder_ctrl[i]; */
1081 				preorder_ctrl = &bmc_sta->recvreorder_ctrl[i];
1082 				preorder_ctrl->enable = false;
1083 				preorder_ctrl->indicate_seq = 0xffff;
1084 				preorder_ctrl->wend_b = 0xffff;
1085 				preorder_ctrl->wsize_b = 64;/* max_ampdu_sz;ex. 32(kbytes) -> wsize_b =32 */
1086 			}
1087 		}
1088 	}
1089 
1090 	return psta;
1091 
1092 }
1093 
1094 /* pnetwork : returns from rtw_joinbss_event_callback */
1095 /* ptarget_wlan: found from scanned_queue */
rtw_joinbss_update_network(struct adapter * padapter,struct wlan_network * ptarget_wlan,struct wlan_network * pnetwork)1096 static void rtw_joinbss_update_network(struct adapter *padapter, struct wlan_network *ptarget_wlan, struct wlan_network  *pnetwork)
1097 {
1098 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1099 	struct wlan_network  *cur_network = &(pmlmepriv->cur_network);
1100 
1101 	/*  why not use ptarget_wlan?? */
1102 	memcpy(&cur_network->network, &pnetwork->network, pnetwork->network.length);
1103 	/*  some ies in pnetwork is wrong, so we should use ptarget_wlan ies */
1104 	cur_network->network.ie_length = ptarget_wlan->network.ie_length;
1105 	memcpy(&cur_network->network.ies[0], &ptarget_wlan->network.ies[0], MAX_IE_SZ);
1106 
1107 	cur_network->aid = pnetwork->join_res;
1108 
1109 	rtw_set_signal_stat_timer(&padapter->recvpriv);
1110 
1111 	padapter->recvpriv.signal_strength = ptarget_wlan->network.phy_info.signal_strength;
1112 	padapter->recvpriv.signal_qual = ptarget_wlan->network.phy_info.signal_quality;
1113 	/* the ptarget_wlan->network.rssi is raw data, we use ptarget_wlan->network.phy_info.signal_strength instead (has scaled) */
1114 	padapter->recvpriv.rssi = translate_percentage_to_dbm(ptarget_wlan->network.phy_info.signal_strength);
1115 
1116 	rtw_set_signal_stat_timer(&padapter->recvpriv);
1117 
1118 	/* update fw_state will clr _FW_UNDER_LINKING here indirectly */
1119 	switch (pnetwork->network.infrastructure_mode) {
1120 	case Ndis802_11Infrastructure:
1121 
1122 			if (pmlmepriv->fw_state&WIFI_UNDER_WPS)
1123 				pmlmepriv->fw_state = WIFI_STATION_STATE|WIFI_UNDER_WPS;
1124 			else
1125 				pmlmepriv->fw_state = WIFI_STATION_STATE;
1126 
1127 			break;
1128 	case Ndis802_11IBSS:
1129 			pmlmepriv->fw_state = WIFI_ADHOC_STATE;
1130 			break;
1131 	default:
1132 			pmlmepriv->fw_state = WIFI_NULL_STATE;
1133 			break;
1134 	}
1135 
1136 	rtw_update_protection(padapter, (cur_network->network.ies) + sizeof(struct ndis_802_11_fix_ie),
1137 									(cur_network->network.ie_length));
1138 
1139 	rtw_update_ht_cap(padapter, cur_network->network.ies, cur_network->network.ie_length, (u8) cur_network->network.configuration.ds_config);
1140 }
1141 
1142 /* Notes: the function could be > passive_level (the same context as Rx tasklet) */
1143 /* pnetwork : returns from rtw_joinbss_event_callback */
1144 /* ptarget_wlan: found from scanned_queue */
1145 /* if join_res > 0, for (fw_state ==WIFI_STATION_STATE), we check if  "ptarget_sta" & "ptarget_wlan" exist. */
1146 /* if join_res > 0, for (fw_state ==WIFI_ADHOC_STATE), we only check if "ptarget_wlan" exist. */
1147 /* if join_res > 0, update "cur_network->network" from "pnetwork->network" if (ptarget_wlan != NULL). */
1148 /*  */
1149 /* define REJOIN */
rtw_joinbss_event_prehandle(struct adapter * adapter,u8 * pbuf)1150 void rtw_joinbss_event_prehandle(struct adapter *adapter, u8 *pbuf)
1151 {
1152 	static u8 __maybe_unused retry;
1153 	struct sta_info *ptarget_sta = NULL, *pcur_sta = NULL;
1154 	struct	sta_priv *pstapriv = &adapter->stapriv;
1155 	struct	mlme_priv *pmlmepriv = &(adapter->mlmepriv);
1156 	struct wlan_network	*pnetwork	= (struct wlan_network *)pbuf;
1157 	struct wlan_network	*cur_network = &(pmlmepriv->cur_network);
1158 	struct wlan_network	*pcur_wlan = NULL, *ptarget_wlan = NULL;
1159 	unsigned int		the_same_macaddr = false;
1160 
1161 	rtw_get_encrypt_decrypt_from_registrypriv(adapter);
1162 
1163 	the_same_macaddr = !memcmp(pnetwork->network.mac_address, cur_network->network.mac_address, ETH_ALEN);
1164 
1165 	pnetwork->network.length = get_wlan_bssid_ex_sz(&pnetwork->network);
1166 	if (pnetwork->network.length > sizeof(struct wlan_bssid_ex))
1167 		return;
1168 
1169 	spin_lock_bh(&pmlmepriv->lock);
1170 
1171 	pmlmepriv->LinkDetectInfo.TrafficTransitionCount = 0;
1172 	pmlmepriv->LinkDetectInfo.LowPowerTransitionCount = 0;
1173 
1174 	if (pnetwork->join_res > 0) {
1175 		spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1176 		retry = 0;
1177 		if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
1178 			/* s1. find ptarget_wlan */
1179 			if (check_fwstate(pmlmepriv, _FW_LINKED)) {
1180 				if (the_same_macaddr) {
1181 					ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.mac_address);
1182 				} else {
1183 					pcur_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.mac_address);
1184 					if (pcur_wlan)
1185 						pcur_wlan->fixed = false;
1186 
1187 					pcur_sta = rtw_get_stainfo(pstapriv, cur_network->network.mac_address);
1188 					if (pcur_sta)
1189 						rtw_free_stainfo(adapter,  pcur_sta);
1190 
1191 					ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, pnetwork->network.mac_address);
1192 					if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
1193 						if (ptarget_wlan)
1194 							ptarget_wlan->fixed = true;
1195 					}
1196 				}
1197 
1198 			} else {
1199 				ptarget_wlan = _rtw_find_same_network(&pmlmepriv->scanned_queue, pnetwork);
1200 				if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
1201 					if (ptarget_wlan)
1202 						ptarget_wlan->fixed = true;
1203 				}
1204 			}
1205 
1206 			/* s2. update cur_network */
1207 			if (ptarget_wlan) {
1208 				rtw_joinbss_update_network(adapter, ptarget_wlan, pnetwork);
1209 			} else {
1210 				netdev_dbg(adapter->pnetdev,
1211 					   "Can't find ptarget_wlan when joinbss_event callback\n");
1212 				spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1213 				goto ignore_joinbss_callback;
1214 			}
1215 
1216 			/* s3. find ptarget_sta & update ptarget_sta after update cur_network only for station mode */
1217 			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
1218 				ptarget_sta = rtw_joinbss_update_stainfo(adapter, pnetwork);
1219 				if (!ptarget_sta) {
1220 					spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1221 					goto ignore_joinbss_callback;
1222 				}
1223 			}
1224 
1225 			/* s4. indicate connect */
1226 			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
1227 				pmlmepriv->cur_network_scanned = ptarget_wlan;
1228 				rtw_indicate_connect(adapter);
1229 			}
1230 
1231 			spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1232 
1233 			spin_unlock_bh(&pmlmepriv->lock);
1234 			/* s5. Cancel assoc_timer */
1235 			del_timer_sync(&pmlmepriv->assoc_timer);
1236 			spin_lock_bh(&pmlmepriv->lock);
1237 		} else {
1238 			spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1239 		}
1240 	} else if (pnetwork->join_res == -4) {
1241 		rtw_reset_securitypriv(adapter);
1242 		_set_timer(&pmlmepriv->assoc_timer, 1);
1243 
1244 		/* rtw_free_assoc_resources(adapter, 1); */
1245 
1246 		if ((check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) == true)
1247 			_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1248 
1249 	} else {/* if join_res < 0 (join fails), then try again */
1250 
1251 		#ifdef REJOIN
1252 		res = _FAIL;
1253 		if (retry < 2)
1254 			res = rtw_select_and_join_from_scanned_queue(pmlmepriv);
1255 
1256 		if (res == _SUCCESS) {
1257 			/* extend time of assoc_timer */
1258 			_set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
1259 			retry++;
1260 		} else if (res == 2) {/* there is no need to wait for join */
1261 			_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1262 			rtw_indicate_connect(adapter);
1263 		} else {
1264 		#endif
1265 
1266 			_set_timer(&pmlmepriv->assoc_timer, 1);
1267 			/* rtw_free_assoc_resources(adapter, 1); */
1268 			_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1269 
1270 		#ifdef REJOIN
1271 			retry = 0;
1272 		}
1273 		#endif
1274 	}
1275 
1276 ignore_joinbss_callback:
1277 
1278 	spin_unlock_bh(&pmlmepriv->lock);
1279 }
1280 
rtw_joinbss_event_callback(struct adapter * adapter,u8 * pbuf)1281 void rtw_joinbss_event_callback(struct adapter *adapter, u8 *pbuf)
1282 {
1283 	struct wlan_network	*pnetwork	= (struct wlan_network *)pbuf;
1284 
1285 	mlmeext_joinbss_event_callback(adapter, pnetwork->join_res);
1286 
1287 	rtw_os_xmit_schedule(adapter);
1288 }
1289 
1290 /* FOR STA, AP , AD-HOC mode */
rtw_sta_media_status_rpt(struct adapter * adapter,struct sta_info * psta,u32 mstatus)1291 void rtw_sta_media_status_rpt(struct adapter *adapter, struct sta_info *psta, u32 mstatus)
1292 {
1293 	u16 media_status_rpt;
1294 
1295 	if (!psta)
1296 		return;
1297 
1298 	media_status_rpt = (u16)((psta->mac_id<<8)|mstatus); /*   MACID|OPMODE:1 connect */
1299 	rtw_hal_set_hwreg(adapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status_rpt);
1300 }
1301 
rtw_stassoc_event_callback(struct adapter * adapter,u8 * pbuf)1302 void rtw_stassoc_event_callback(struct adapter *adapter, u8 *pbuf)
1303 {
1304 	struct sta_info *psta;
1305 	struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
1306 	struct stassoc_event	*pstassoc	= (struct stassoc_event *)pbuf;
1307 	struct wlan_network	*cur_network = &(pmlmepriv->cur_network);
1308 	struct wlan_network	*ptarget_wlan = NULL;
1309 
1310 	if (rtw_access_ctrl(adapter, pstassoc->macaddr) == false)
1311 		return;
1312 
1313 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1314 		psta = rtw_get_stainfo(&adapter->stapriv, pstassoc->macaddr);
1315 		if (psta) {
1316 			u8 *passoc_req = NULL;
1317 			u32 assoc_req_len = 0;
1318 
1319 			rtw_sta_media_status_rpt(adapter, psta, 1);
1320 
1321 			ap_sta_info_defer_update(adapter, psta);
1322 
1323 			/* report to upper layer */
1324 			spin_lock_bh(&psta->lock);
1325 			if (psta->passoc_req && psta->assoc_req_len > 0) {
1326 				passoc_req = rtw_zmalloc(psta->assoc_req_len);
1327 				if (passoc_req) {
1328 					assoc_req_len = psta->assoc_req_len;
1329 					memcpy(passoc_req, psta->passoc_req, assoc_req_len);
1330 
1331 					kfree(psta->passoc_req);
1332 					psta->passoc_req = NULL;
1333 					psta->assoc_req_len = 0;
1334 				}
1335 			}
1336 			spin_unlock_bh(&psta->lock);
1337 
1338 			if (passoc_req && assoc_req_len > 0) {
1339 				rtw_cfg80211_indicate_sta_assoc(adapter, passoc_req, assoc_req_len);
1340 
1341 				kfree(passoc_req);
1342 			}
1343 		}
1344 		return;
1345 	}
1346 
1347 	/* for AD-HOC mode */
1348 	psta = rtw_get_stainfo(&adapter->stapriv, pstassoc->macaddr);
1349 	if (psta) {
1350 		/* the sta have been in sta_info_queue => do nothing */
1351 
1352 		return; /* between drv has received this event before and  fw have not yet to set key to CAM_ENTRY) */
1353 	}
1354 
1355 	psta = rtw_alloc_stainfo(&adapter->stapriv, pstassoc->macaddr);
1356 	if (!psta)
1357 		return;
1358 
1359 	/* to do : init sta_info variable */
1360 	psta->qos_option = 0;
1361 	psta->mac_id = (uint)pstassoc->cam_id;
1362 	/* psta->aid = (uint)pstassoc->cam_id; */
1363 
1364 	/* for ad-hoc mode */
1365 	rtw_hal_set_odm_var(adapter, HAL_ODM_STA_INFO, psta, true);
1366 
1367 	rtw_sta_media_status_rpt(adapter, psta, 1);
1368 
1369 	if (adapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X)
1370 		psta->dot118021XPrivacy = adapter->securitypriv.dot11PrivacyAlgrthm;
1371 
1372 	psta->ieee8021x_blocked = false;
1373 
1374 	spin_lock_bh(&pmlmepriv->lock);
1375 
1376 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) ||
1377 		(check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true)) {
1378 		if (adapter->stapriv.asoc_sta_count == 2) {
1379 			spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1380 			ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.mac_address);
1381 			pmlmepriv->cur_network_scanned = ptarget_wlan;
1382 			if (ptarget_wlan)
1383 				ptarget_wlan->fixed = true;
1384 			spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1385 			/*  a sta + bc/mc_stainfo (not Ibss_stainfo) */
1386 			rtw_indicate_connect(adapter);
1387 		}
1388 	}
1389 
1390 	spin_unlock_bh(&pmlmepriv->lock);
1391 
1392 	mlmeext_sta_add_event_callback(adapter, psta);
1393 }
1394 
rtw_stadel_event_callback(struct adapter * adapter,u8 * pbuf)1395 void rtw_stadel_event_callback(struct adapter *adapter, u8 *pbuf)
1396 {
1397 	int mac_id = (-1);
1398 	struct sta_info *psta;
1399 	struct wlan_network *pwlan = NULL;
1400 	struct wlan_bssid_ex    *pdev_network = NULL;
1401 	u8 *pibss = NULL;
1402 	struct	mlme_priv *pmlmepriv = &(adapter->mlmepriv);
1403 	struct	stadel_event *pstadel	= (struct stadel_event *)pbuf;
1404 	struct wlan_network *tgt_network = &(pmlmepriv->cur_network);
1405 	struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
1406 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1407 
1408 	psta = rtw_get_stainfo(&adapter->stapriv, pstadel->macaddr);
1409 	if (psta)
1410 		mac_id = psta->mac_id;
1411 	else
1412 		mac_id = pstadel->mac_id;
1413 
1414 	if (mac_id >= 0) {
1415 		u16 media_status;
1416 
1417 		media_status = (mac_id<<8)|0; /*   MACID|OPMODE:0 means disconnect */
1418 		/* for STA, AP, ADHOC mode, report disconnect stauts to FW */
1419 		rtw_hal_set_hwreg(adapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status);
1420 	}
1421 
1422 	/* if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) */
1423 	if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
1424 		return;
1425 
1426 	mlmeext_sta_del_event_callback(adapter);
1427 
1428 	spin_lock_bh(&pmlmepriv->lock);
1429 
1430 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1431 		u16 reason = *((unsigned short *)(pstadel->rsvd));
1432 		bool roam = false;
1433 		struct wlan_network *roam_target = NULL;
1434 
1435 		if (adapter->registrypriv.wifi_spec == 1) {
1436 			roam = false;
1437 		} else if (reason == WLAN_REASON_EXPIRATION_CHK && rtw_chk_roam_flags(adapter, RTW_ROAM_ON_EXPIRED)) {
1438 			roam = true;
1439 		} else if (reason == WLAN_REASON_ACTIVE_ROAM && rtw_chk_roam_flags(adapter, RTW_ROAM_ACTIVE)) {
1440 			roam = true;
1441 			roam_target = pmlmepriv->roam_network;
1442 		}
1443 
1444 		if (roam) {
1445 			if (rtw_to_roam(adapter) > 0)
1446 				rtw_dec_to_roam(adapter); /* this stadel_event is caused by roaming, decrease to_roam */
1447 			else if (rtw_to_roam(adapter) == 0)
1448 				rtw_set_to_roam(adapter, adapter->registrypriv.max_roaming_times);
1449 		} else {
1450 			rtw_set_to_roam(adapter, 0);
1451 		}
1452 
1453 		rtw_free_uc_swdec_pending_queue(adapter);
1454 
1455 		rtw_free_assoc_resources(adapter, 1);
1456 		rtw_indicate_disconnect(adapter);
1457 
1458 		spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1459 		/*  remove the network entry in scanned_queue */
1460 		pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.mac_address);
1461 		if (pwlan) {
1462 			pwlan->fixed = false;
1463 			rtw_free_network_nolock(adapter, pwlan);
1464 		}
1465 		spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1466 
1467 		_rtw_roaming(adapter, roam_target);
1468 	}
1469 
1470 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) ||
1471 	      check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1472 
1473 		rtw_free_stainfo(adapter,  psta);
1474 
1475 		if (adapter->stapriv.asoc_sta_count == 1) {/* a sta + bc/mc_stainfo (not Ibss_stainfo) */
1476 			u8 ret = _SUCCESS;
1477 			/* rtw_indicate_disconnect(adapter);removed@20091105 */
1478 			spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1479 			/* free old ibss network */
1480 			/* pwlan = rtw_find_network(&pmlmepriv->scanned_queue, pstadel->macaddr); */
1481 			pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.mac_address);
1482 			if (pwlan) {
1483 				pwlan->fixed = false;
1484 				rtw_free_network_nolock(adapter, pwlan);
1485 			}
1486 			spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1487 			/* re-create ibss */
1488 			pdev_network = &(adapter->registrypriv.dev_network);
1489 			pibss = adapter->registrypriv.dev_network.mac_address;
1490 
1491 			memcpy(pdev_network, &tgt_network->network, get_wlan_bssid_ex_sz(&tgt_network->network));
1492 
1493 			memcpy(&pdev_network->ssid, &pmlmepriv->assoc_ssid, sizeof(struct ndis_802_11_ssid));
1494 
1495 			rtw_update_registrypriv_dev_network(adapter);
1496 
1497 			rtw_generate_random_ibss(pibss);
1498 
1499 			if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1500 				set_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE);
1501 				_clr_fwstate_(pmlmepriv, WIFI_ADHOC_STATE);
1502 			}
1503 
1504 			ret = rtw_createbss_cmd(adapter);
1505 			if (ret != _SUCCESS)
1506 				goto unlock;
1507 		}
1508 
1509 	}
1510 
1511 unlock:
1512 	spin_unlock_bh(&pmlmepriv->lock);
1513 }
1514 
rtw_cpwm_event_callback(struct adapter * padapter,u8 * pbuf)1515 void rtw_cpwm_event_callback(struct adapter *padapter, u8 *pbuf)
1516 {
1517 	struct reportpwrstate_parm *preportpwrstate;
1518 
1519 	preportpwrstate = (struct reportpwrstate_parm *)pbuf;
1520 	preportpwrstate->state |= (u8)(adapter_to_pwrctl(padapter)->cpwm_tog + 0x80);
1521 	cpwm_int_hdl(padapter, preportpwrstate);
1522 }
1523 
rtw_wmm_event_callback(struct adapter * padapter,u8 * pbuf)1524 void rtw_wmm_event_callback(struct adapter *padapter, u8 *pbuf)
1525 {
1526 	WMMOnAssocRsp(padapter);
1527 }
1528 
1529 /*
1530 * _rtw_join_timeout_handler - Timeout/failure handler for CMD JoinBss
1531 * @adapter: pointer to struct adapter structure
1532 */
_rtw_join_timeout_handler(struct timer_list * t)1533 void _rtw_join_timeout_handler(struct timer_list *t)
1534 {
1535 	struct adapter *adapter = from_timer(adapter, t,
1536 						  mlmepriv.assoc_timer);
1537 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
1538 
1539 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
1540 		return;
1541 
1542 	spin_lock_bh(&pmlmepriv->lock);
1543 
1544 	if (rtw_to_roam(adapter) > 0) { /* join timeout caused by roaming */
1545 		while (1) {
1546 			rtw_dec_to_roam(adapter);
1547 			if (rtw_to_roam(adapter) != 0) { /* try another */
1548 				int do_join_r;
1549 
1550 				do_join_r = rtw_do_join(adapter);
1551 				if (do_join_r != _SUCCESS) {
1552 					continue;
1553 				}
1554 				break;
1555 			} else {
1556 				rtw_indicate_disconnect(adapter);
1557 				break;
1558 			}
1559 		}
1560 
1561 	} else {
1562 		rtw_indicate_disconnect(adapter);
1563 		free_scanqueue(pmlmepriv);/*  */
1564 
1565 		/* indicate disconnect for the case that join_timeout and check_fwstate != FW_LINKED */
1566 		rtw_cfg80211_indicate_disconnect(adapter);
1567 
1568 	}
1569 
1570 	spin_unlock_bh(&pmlmepriv->lock);
1571 }
1572 
1573 /*
1574 * rtw_scan_timeout_handler - Timeout/Failure handler for CMD SiteSurvey
1575 * @adapter: pointer to struct adapter structure
1576 */
rtw_scan_timeout_handler(struct timer_list * t)1577 void rtw_scan_timeout_handler(struct timer_list *t)
1578 {
1579 	struct adapter *adapter = from_timer(adapter, t,
1580 						  mlmepriv.scan_to_timer);
1581 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
1582 
1583 	spin_lock_bh(&pmlmepriv->lock);
1584 
1585 	_clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
1586 
1587 	spin_unlock_bh(&pmlmepriv->lock);
1588 
1589 	rtw_indicate_scan_done(adapter, true);
1590 }
1591 
rtw_mlme_reset_auto_scan_int(struct adapter * adapter)1592 void rtw_mlme_reset_auto_scan_int(struct adapter *adapter)
1593 {
1594 	struct mlme_priv *mlme = &adapter->mlmepriv;
1595 	struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
1596 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1597 
1598 	if (pmlmeinfo->VHT_enable) /* disable auto scan when connect to 11AC AP */
1599 		mlme->auto_scan_int_ms = 0;
1600 	else if (adapter->registrypriv.wifi_spec && is_client_associated_to_ap(adapter) == true)
1601 		mlme->auto_scan_int_ms = 60*1000;
1602 	else if (rtw_chk_roam_flags(adapter, RTW_ROAM_ACTIVE)) {
1603 		if (check_fwstate(mlme, WIFI_STATION_STATE) && check_fwstate(mlme, _FW_LINKED))
1604 			mlme->auto_scan_int_ms = mlme->roam_scan_int_ms;
1605 	} else
1606 		mlme->auto_scan_int_ms = 0; /* disabled */
1607 }
1608 
rtw_auto_scan_handler(struct adapter * padapter)1609 static void rtw_auto_scan_handler(struct adapter *padapter)
1610 {
1611 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1612 
1613 	rtw_mlme_reset_auto_scan_int(padapter);
1614 
1615 	if (pmlmepriv->auto_scan_int_ms != 0
1616 		&& jiffies_to_msecs(jiffies - pmlmepriv->scan_start_time) > pmlmepriv->auto_scan_int_ms) {
1617 
1618 		if (!padapter->registrypriv.wifi_spec) {
1619 			if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY | _FW_UNDER_LINKING) == true)
1620 				goto exit;
1621 
1622 			if (pmlmepriv->LinkDetectInfo.bBusyTraffic)
1623 				goto exit;
1624 		}
1625 
1626 		rtw_set_802_11_bssid_list_scan(padapter, NULL, 0);
1627 	}
1628 
1629 exit:
1630 	return;
1631 }
1632 
rtw_dynamic_check_timer_handler(struct adapter * adapter)1633 void rtw_dynamic_check_timer_handler(struct adapter *adapter)
1634 {
1635 	if (!adapter)
1636 		return;
1637 
1638 	if (!adapter->hw_init_completed)
1639 		return;
1640 
1641 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
1642 		return;
1643 
1644 	if (adapter->net_closed)
1645 		return;
1646 
1647 	if ((adapter_to_pwrctl(adapter)->fw_current_in_ps_mode)
1648 		&& !(hal_btcoex_IsBtControlLps(adapter))
1649 		) {
1650 		u8 bEnterPS;
1651 
1652 		linked_status_chk(adapter);
1653 
1654 		bEnterPS = traffic_status_watchdog(adapter, 1);
1655 		if (bEnterPS) {
1656 			/* rtw_lps_ctrl_wk_cmd(adapter, LPS_CTRL_ENTER, 1); */
1657 			rtw_hal_dm_watchdog_in_lps(adapter);
1658 		} else {
1659 			/* call rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1) in traffic_status_watchdog() */
1660 		}
1661 
1662 	} else {
1663 		if (is_primary_adapter(adapter))
1664 			rtw_dynamic_chk_wk_cmd(adapter);
1665 	}
1666 
1667 	/* auto site survey */
1668 	rtw_auto_scan_handler(adapter);
1669 }
1670 
rtw_is_scan_deny(struct adapter * adapter)1671 inline bool rtw_is_scan_deny(struct adapter *adapter)
1672 {
1673 	struct mlme_priv *mlmepriv = &adapter->mlmepriv;
1674 
1675 	return (atomic_read(&mlmepriv->set_scan_deny) != 0) ? true : false;
1676 }
1677 
rtw_clear_scan_deny(struct adapter * adapter)1678 inline void rtw_clear_scan_deny(struct adapter *adapter)
1679 {
1680 	struct mlme_priv *mlmepriv = &adapter->mlmepriv;
1681 
1682 	atomic_set(&mlmepriv->set_scan_deny, 0);
1683 }
1684 
rtw_set_scan_deny(struct adapter * adapter,u32 ms)1685 void rtw_set_scan_deny(struct adapter *adapter, u32 ms)
1686 {
1687 	struct mlme_priv *mlmepriv = &adapter->mlmepriv;
1688 
1689 	atomic_set(&mlmepriv->set_scan_deny, 1);
1690 	_set_timer(&mlmepriv->set_scan_deny_timer, ms);
1691 }
1692 
1693 /*
1694 * Select a new roaming candidate from the original @param candidate and @param competitor
1695 * @return true: candidate is updated
1696 * @return false: candidate is not updated
1697 */
rtw_check_roaming_candidate(struct mlme_priv * mlme,struct wlan_network ** candidate,struct wlan_network * competitor)1698 static int rtw_check_roaming_candidate(struct mlme_priv *mlme
1699 	, struct wlan_network **candidate, struct wlan_network *competitor)
1700 {
1701 	int updated = false;
1702 	struct adapter *adapter = container_of(mlme, struct adapter, mlmepriv);
1703 
1704 	if (is_same_ess(&competitor->network, &mlme->cur_network.network) == false)
1705 		goto exit;
1706 
1707 	if (rtw_is_desired_network(adapter, competitor) == false)
1708 		goto exit;
1709 
1710 	/* got specific addr to roam */
1711 	if (!is_zero_mac_addr(mlme->roam_tgt_addr)) {
1712 		if (!memcmp(mlme->roam_tgt_addr, competitor->network.mac_address, ETH_ALEN))
1713 			goto update;
1714 		else
1715 			goto exit;
1716 	}
1717 	if (jiffies_to_msecs(jiffies - competitor->last_scanned) >= mlme->roam_scanr_exp_ms)
1718 		goto exit;
1719 
1720 	if (competitor->network.rssi - mlme->cur_network_scanned->network.rssi < mlme->roam_rssi_diff_th)
1721 		goto exit;
1722 
1723 	if (*candidate && (*candidate)->network.rssi >= competitor->network.rssi)
1724 		goto exit;
1725 
1726 update:
1727 	*candidate = competitor;
1728 	updated = true;
1729 
1730 exit:
1731 	return updated;
1732 }
1733 
rtw_select_roaming_candidate(struct mlme_priv * mlme)1734 int rtw_select_roaming_candidate(struct mlme_priv *mlme)
1735 {
1736 	int ret = _FAIL;
1737 	struct list_head	*phead;
1738 	struct __queue	*queue	= &(mlme->scanned_queue);
1739 	struct	wlan_network	*pnetwork = NULL;
1740 	struct	wlan_network	*candidate = NULL;
1741 
1742 	if (!mlme->cur_network_scanned) {
1743 		rtw_warn_on(1);
1744 		return ret;
1745 	}
1746 
1747 	spin_lock_bh(&(mlme->scanned_queue.lock));
1748 	phead = get_list_head(queue);
1749 
1750 	list_for_each(mlme->pscanned, phead) {
1751 
1752 		pnetwork = list_entry(mlme->pscanned, struct wlan_network,
1753 				      list);
1754 
1755 		rtw_check_roaming_candidate(mlme, &candidate, pnetwork);
1756 
1757 	}
1758 
1759 	if (!candidate) {
1760 		ret = _FAIL;
1761 		goto exit;
1762 	} else {
1763 		mlme->roam_network = candidate;
1764 
1765 		if (!memcmp(candidate->network.mac_address, mlme->roam_tgt_addr, ETH_ALEN))
1766 			eth_zero_addr(mlme->roam_tgt_addr);
1767 	}
1768 
1769 	ret = _SUCCESS;
1770 exit:
1771 	spin_unlock_bh(&(mlme->scanned_queue.lock));
1772 
1773 	return ret;
1774 }
1775 
1776 /*
1777 * Select a new join candidate from the original @param candidate and @param competitor
1778 * @return true: candidate is updated
1779 * @return false: candidate is not updated
1780 */
rtw_check_join_candidate(struct mlme_priv * mlme,struct wlan_network ** candidate,struct wlan_network * competitor)1781 static int rtw_check_join_candidate(struct mlme_priv *mlme
1782 	, struct wlan_network **candidate, struct wlan_network *competitor)
1783 {
1784 	int updated = false;
1785 	struct adapter *adapter = container_of(mlme, struct adapter, mlmepriv);
1786 
1787 	/* check bssid, if needed */
1788 	if (mlme->assoc_by_bssid) {
1789 		if (memcmp(competitor->network.mac_address, mlme->assoc_bssid, ETH_ALEN))
1790 			goto exit;
1791 	}
1792 
1793 	/* check ssid, if needed */
1794 	if (mlme->assoc_ssid.ssid[0] && mlme->assoc_ssid.ssid_length) {
1795 		if (competitor->network.ssid.ssid_length != mlme->assoc_ssid.ssid_length
1796 			|| memcmp(competitor->network.ssid.ssid, mlme->assoc_ssid.ssid, mlme->assoc_ssid.ssid_length)
1797 		)
1798 			goto exit;
1799 	}
1800 
1801 	if (rtw_is_desired_network(adapter, competitor)  == false)
1802 		goto exit;
1803 
1804 	if (rtw_to_roam(adapter) > 0) {
1805 		if (jiffies_to_msecs(jiffies - competitor->last_scanned) >= mlme->roam_scanr_exp_ms
1806 			|| is_same_ess(&competitor->network, &mlme->cur_network.network) == false
1807 		)
1808 			goto exit;
1809 	}
1810 
1811 	if (!*candidate || (*candidate)->network.rssi < competitor->network.rssi) {
1812 		*candidate = competitor;
1813 		updated = true;
1814 	}
1815 
1816 exit:
1817 	return updated;
1818 }
1819 
1820 /*
1821 Calling context:
1822 The caller of the sub-routine will be in critical section...
1823 The caller must hold the following spinlock
1824 pmlmepriv->lock
1825 */
1826 
rtw_select_and_join_from_scanned_queue(struct mlme_priv * pmlmepriv)1827 int rtw_select_and_join_from_scanned_queue(struct mlme_priv *pmlmepriv)
1828 {
1829 	int ret;
1830 	struct list_head	*phead;
1831 	struct adapter *adapter;
1832 	struct __queue	*queue	= &(pmlmepriv->scanned_queue);
1833 	struct	wlan_network	*pnetwork = NULL;
1834 	struct	wlan_network	*candidate = NULL;
1835 
1836 	adapter = (struct adapter *)pmlmepriv->nic_hdl;
1837 
1838 	spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1839 
1840 	if (pmlmepriv->roam_network) {
1841 		candidate = pmlmepriv->roam_network;
1842 		pmlmepriv->roam_network = NULL;
1843 		goto candidate_exist;
1844 	}
1845 
1846 	phead = get_list_head(queue);
1847 	list_for_each(pmlmepriv->pscanned, phead) {
1848 
1849 		pnetwork = list_entry(pmlmepriv->pscanned,
1850 				      struct wlan_network, list);
1851 
1852 		rtw_check_join_candidate(pmlmepriv, &candidate, pnetwork);
1853 
1854 	}
1855 
1856 	if (!candidate) {
1857 		ret = _FAIL;
1858 		goto exit;
1859 	} else {
1860 		goto candidate_exist;
1861 	}
1862 
1863 candidate_exist:
1864 
1865 	/*  check for situation of  _FW_LINKED */
1866 	if (check_fwstate(pmlmepriv, _FW_LINKED) == true) {
1867 		rtw_disassoc_cmd(adapter, 0, true);
1868 		rtw_indicate_disconnect(adapter);
1869 		rtw_free_assoc_resources(adapter, 0);
1870 	}
1871 
1872 	set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
1873 	ret = rtw_joinbss_cmd(adapter, candidate);
1874 
1875 exit:
1876 	spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1877 	return ret;
1878 }
1879 
rtw_set_auth(struct adapter * adapter,struct security_priv * psecuritypriv)1880 signed int rtw_set_auth(struct adapter *adapter, struct security_priv *psecuritypriv)
1881 {
1882 	struct	cmd_obj *pcmd;
1883 	struct	setauth_parm *psetauthparm;
1884 	struct	cmd_priv *pcmdpriv = &(adapter->cmdpriv);
1885 	signed int		res = _SUCCESS;
1886 
1887 	pcmd = rtw_zmalloc(sizeof(struct cmd_obj));
1888 	if (!pcmd) {
1889 		res = _FAIL;  /* try again */
1890 		goto exit;
1891 	}
1892 
1893 	psetauthparm = rtw_zmalloc(sizeof(struct setauth_parm));
1894 	if (!psetauthparm) {
1895 		kfree(pcmd);
1896 		res = _FAIL;
1897 		goto exit;
1898 	}
1899 
1900 	psetauthparm->mode = (unsigned char)psecuritypriv->dot11AuthAlgrthm;
1901 
1902 	pcmd->cmdcode = _SetAuth_CMD_;
1903 	pcmd->parmbuf = (unsigned char *)psetauthparm;
1904 	pcmd->cmdsz =  (sizeof(struct setauth_parm));
1905 	pcmd->rsp = NULL;
1906 	pcmd->rspsz = 0;
1907 
1908 	INIT_LIST_HEAD(&pcmd->list);
1909 
1910 	res = rtw_enqueue_cmd(pcmdpriv, pcmd);
1911 
1912 exit:
1913 	return res;
1914 }
1915 
rtw_set_key(struct adapter * adapter,struct security_priv * psecuritypriv,signed int keyid,u8 set_tx,bool enqueue)1916 signed int rtw_set_key(struct adapter *adapter, struct security_priv *psecuritypriv, signed int keyid, u8 set_tx, bool enqueue)
1917 {
1918 	u8 keylen;
1919 	struct cmd_obj		*pcmd;
1920 	struct setkey_parm	*psetkeyparm;
1921 	struct cmd_priv 	*pcmdpriv = &(adapter->cmdpriv);
1922 	signed int	res = _SUCCESS;
1923 
1924 	psetkeyparm = rtw_zmalloc(sizeof(struct setkey_parm));
1925 	if (!psetkeyparm) {
1926 		res = _FAIL;
1927 		goto exit;
1928 	}
1929 
1930 	if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X)
1931 		psetkeyparm->algorithm = (unsigned char)psecuritypriv->dot118021XGrpPrivacy;
1932 	else
1933 		psetkeyparm->algorithm = (u8)psecuritypriv->dot11PrivacyAlgrthm;
1934 
1935 	psetkeyparm->keyid = (u8)keyid;/* 0~3 */
1936 	psetkeyparm->set_tx = set_tx;
1937 	if (is_wep_enc(psetkeyparm->algorithm))
1938 		adapter->securitypriv.key_mask |= BIT(psetkeyparm->keyid);
1939 
1940 	switch (psetkeyparm->algorithm) {
1941 
1942 	case _WEP40_:
1943 		keylen = 5;
1944 		memcpy(&(psetkeyparm->key[0]), &(psecuritypriv->dot11DefKey[keyid].skey[0]), keylen);
1945 		break;
1946 	case _WEP104_:
1947 		keylen = 13;
1948 		memcpy(&(psetkeyparm->key[0]), &(psecuritypriv->dot11DefKey[keyid].skey[0]), keylen);
1949 		break;
1950 	case _TKIP_:
1951 		keylen = 16;
1952 		memcpy(&psetkeyparm->key, &psecuritypriv->dot118021XGrpKey[keyid], keylen);
1953 		psetkeyparm->grpkey = 1;
1954 		break;
1955 	case _AES_:
1956 		keylen = 16;
1957 		memcpy(&psetkeyparm->key, &psecuritypriv->dot118021XGrpKey[keyid], keylen);
1958 		psetkeyparm->grpkey = 1;
1959 		break;
1960 	default:
1961 		res = _FAIL;
1962 		kfree(psetkeyparm);
1963 		goto exit;
1964 	}
1965 
1966 	if (enqueue) {
1967 		pcmd = rtw_zmalloc(sizeof(struct cmd_obj));
1968 		if (!pcmd) {
1969 			kfree(psetkeyparm);
1970 			res = _FAIL;  /* try again */
1971 			goto exit;
1972 		}
1973 
1974 		pcmd->cmdcode = _SetKey_CMD_;
1975 		pcmd->parmbuf = (u8 *)psetkeyparm;
1976 		pcmd->cmdsz =  (sizeof(struct setkey_parm));
1977 		pcmd->rsp = NULL;
1978 		pcmd->rspsz = 0;
1979 
1980 		INIT_LIST_HEAD(&pcmd->list);
1981 
1982 		res = rtw_enqueue_cmd(pcmdpriv, pcmd);
1983 	} else {
1984 		setkey_hdl(adapter, (u8 *)psetkeyparm);
1985 		kfree(psetkeyparm);
1986 	}
1987 exit:
1988 	return res;
1989 }
1990 
1991 /* adjust ies for rtw_joinbss_cmd in WMM */
rtw_restruct_wmm_ie(struct adapter * adapter,u8 * in_ie,u8 * out_ie,uint in_len,uint initial_out_len)1992 int rtw_restruct_wmm_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_len, uint initial_out_len)
1993 {
1994 	unsigned	int ielength = 0;
1995 	unsigned int i, j;
1996 
1997 	i = 12; /* after the fixed IE */
1998 	while (i < in_len) {
1999 		ielength = initial_out_len;
2000 
2001 		if (in_ie[i] == 0xDD && in_ie[i+2] == 0x00 && in_ie[i+3] == 0x50  && in_ie[i+4] == 0xF2 && in_ie[i+5] == 0x02 && i+5 < in_len) { /* WMM element ID and OUI */
2002 			for (j = i; j < i + 9; j++) {
2003 				out_ie[ielength] = in_ie[j];
2004 				ielength++;
2005 			}
2006 			out_ie[initial_out_len + 1] = 0x07;
2007 			out_ie[initial_out_len + 6] = 0x00;
2008 			out_ie[initial_out_len + 8] = 0x00;
2009 
2010 			break;
2011 		}
2012 
2013 		i += (in_ie[i+1]+2); /*  to the next IE element */
2014 	}
2015 
2016 	return ielength;
2017 
2018 }
2019 
2020 /*  */
2021 /*  Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */
2022 /*  Added by Annie, 2006-05-07. */
2023 /*  */
2024 /*  Search by BSSID, */
2025 /*  Return Value: */
2026 /* 		-1		:if there is no pre-auth key in the  table */
2027 /* 		>= 0		:if there is pre-auth key, and   return the entry id */
2028 /*  */
2029 /*  */
2030 
SecIsInPMKIDList(struct adapter * Adapter,u8 * bssid)2031 static int SecIsInPMKIDList(struct adapter *Adapter, u8 *bssid)
2032 {
2033 	struct security_priv *p = &Adapter->securitypriv;
2034 	int i;
2035 
2036 	for (i = 0; i < NUM_PMKID_CACHE; i++)
2037 		if ((p->PMKIDList[i].bUsed) &&
2038 				(!memcmp(p->PMKIDList[i].Bssid, bssid, ETH_ALEN)))
2039 			return i;
2040 	return -1;
2041 }
2042 
2043 /*  */
2044 /*  Check the RSN IE length */
2045 /*  If the RSN IE length <= 20, the RSN IE didn't include the PMKID information */
2046 /*  0-11th element in the array are the fixed IE */
2047 /*  12th element in the array is the IE */
2048 /*  13th element in the array is the IE length */
2049 /*  */
2050 
rtw_append_pmkid(struct adapter * Adapter,int iEntry,u8 * ie,uint ie_len)2051 static int rtw_append_pmkid(struct adapter *Adapter, int iEntry, u8 *ie, uint ie_len)
2052 {
2053 	struct security_priv *psecuritypriv = &Adapter->securitypriv;
2054 
2055 	if (ie[13] <= 20) {
2056 		/*  The RSN IE didn't include the PMK ID, append the PMK information */
2057 		ie[ie_len] = 1;
2058 		ie_len++;
2059 		ie[ie_len] = 0;	/* PMKID count = 0x0100 */
2060 		ie_len++;
2061 		memcpy(&ie[ie_len], &psecuritypriv->PMKIDList[iEntry].PMKID, 16);
2062 		ie_len += 16;
2063 		ie[13] += 18;/* PMKID length = 2+16 */
2064 	}
2065 	return ie_len;
2066 }
2067 
rtw_restruct_sec_ie(struct adapter * adapter,u8 * in_ie,u8 * out_ie,uint in_len)2068 signed int rtw_restruct_sec_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_len)
2069 {
2070 	u8 authmode = 0x0;
2071 	uint	ielength;
2072 	int iEntry;
2073 
2074 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
2075 	struct security_priv *psecuritypriv = &adapter->securitypriv;
2076 	uint	ndisauthmode = psecuritypriv->ndisauthtype;
2077 
2078 	/* copy fixed ie only */
2079 	memcpy(out_ie, in_ie, 12);
2080 	ielength = 12;
2081 	if ((ndisauthmode == Ndis802_11AuthModeWPA) || (ndisauthmode == Ndis802_11AuthModeWPAPSK))
2082 		authmode = WLAN_EID_VENDOR_SPECIFIC;
2083 	if ((ndisauthmode == Ndis802_11AuthModeWPA2) || (ndisauthmode == Ndis802_11AuthModeWPA2PSK))
2084 		authmode = WLAN_EID_RSN;
2085 
2086 	if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) {
2087 		memcpy(out_ie+ielength, psecuritypriv->wps_ie, psecuritypriv->wps_ie_len);
2088 
2089 		ielength += psecuritypriv->wps_ie_len;
2090 	} else if ((authmode == WLAN_EID_VENDOR_SPECIFIC) || (authmode == WLAN_EID_RSN)) {
2091 		/* copy RSN or SSN */
2092 		memcpy(&out_ie[ielength], &psecuritypriv->supplicant_ie[0], psecuritypriv->supplicant_ie[1]+2);
2093 		/* debug for CONFIG_IEEE80211W
2094 		{
2095 			int jj;
2096 			printk("supplicant_ie_length =%d &&&&&&&&&&&&&&&&&&&\n", psecuritypriv->supplicant_ie[1]+2);
2097 			for (jj = 0; jj < psecuritypriv->supplicant_ie[1]+2; jj++)
2098 				printk(" %02x ", psecuritypriv->supplicant_ie[jj]);
2099 			printk("\n");
2100 		}*/
2101 		ielength += psecuritypriv->supplicant_ie[1]+2;
2102 		rtw_report_sec_ie(adapter, authmode, psecuritypriv->supplicant_ie);
2103 	}
2104 
2105 	iEntry = SecIsInPMKIDList(adapter, pmlmepriv->assoc_bssid);
2106 	if (iEntry < 0) {
2107 		return ielength;
2108 	} else {
2109 		if (authmode == WLAN_EID_RSN)
2110 			ielength = rtw_append_pmkid(adapter, iEntry, out_ie, ielength);
2111 	}
2112 	return ielength;
2113 }
2114 
rtw_init_registrypriv_dev_network(struct adapter * adapter)2115 void rtw_init_registrypriv_dev_network(struct adapter *adapter)
2116 {
2117 	struct registry_priv *pregistrypriv = &adapter->registrypriv;
2118 	struct eeprom_priv *peepriv = &adapter->eeprompriv;
2119 	struct wlan_bssid_ex    *pdev_network = &pregistrypriv->dev_network;
2120 	u8 *myhwaddr = myid(peepriv);
2121 
2122 	memcpy(pdev_network->mac_address, myhwaddr, ETH_ALEN);
2123 
2124 	memcpy(&pdev_network->ssid, &pregistrypriv->ssid, sizeof(struct ndis_802_11_ssid));
2125 
2126 	pdev_network->configuration.length = sizeof(struct ndis_802_11_conf);
2127 	pdev_network->configuration.beacon_period = 100;
2128 }
2129 
rtw_update_registrypriv_dev_network(struct adapter * adapter)2130 void rtw_update_registrypriv_dev_network(struct adapter *adapter)
2131 {
2132 	int sz = 0;
2133 	struct registry_priv *pregistrypriv = &adapter->registrypriv;
2134 	struct wlan_bssid_ex    *pdev_network = &pregistrypriv->dev_network;
2135 	struct	security_priv *psecuritypriv = &adapter->securitypriv;
2136 	struct	wlan_network	*cur_network = &adapter->mlmepriv.cur_network;
2137 	/* struct	xmit_priv *pxmitpriv = &adapter->xmitpriv; */
2138 
2139 	pdev_network->privacy = (psecuritypriv->dot11PrivacyAlgrthm > 0 ? 1 : 0) ; /*  adhoc no 802.1x */
2140 
2141 	pdev_network->rssi = 0;
2142 
2143 	switch (pregistrypriv->wireless_mode) {
2144 	case WIRELESS_11B:
2145 		pdev_network->network_type_in_use = (Ndis802_11DS);
2146 		break;
2147 	case WIRELESS_11G:
2148 	case WIRELESS_11BG:
2149 	case WIRELESS_11_24N:
2150 	case WIRELESS_11G_24N:
2151 	case WIRELESS_11BG_24N:
2152 		pdev_network->network_type_in_use = (Ndis802_11OFDM24);
2153 		break;
2154 	default:
2155 		/*  TODO */
2156 		break;
2157 	}
2158 
2159 	pdev_network->configuration.ds_config = (pregistrypriv->channel);
2160 
2161 	if (cur_network->network.infrastructure_mode == Ndis802_11IBSS)
2162 		pdev_network->configuration.atim_window = (0);
2163 
2164 	pdev_network->infrastructure_mode = (cur_network->network.infrastructure_mode);
2165 
2166 	/*  1. Supported rates */
2167 	/*  2. IE */
2168 
2169 	/* rtw_set_supported_rate(pdev_network->supported_rates, pregistrypriv->wireless_mode) ;  will be called in rtw_generate_ie */
2170 	sz = rtw_generate_ie(pregistrypriv);
2171 
2172 	pdev_network->ie_length = sz;
2173 
2174 	pdev_network->length = get_wlan_bssid_ex_sz((struct wlan_bssid_ex  *)pdev_network);
2175 
2176 	/* notes: translate ie_length & length after assign the length to cmdsz in createbss_cmd(); */
2177 	/* pdev_network->ie_length = cpu_to_le32(sz); */
2178 }
2179 
rtw_get_encrypt_decrypt_from_registrypriv(struct adapter * adapter)2180 void rtw_get_encrypt_decrypt_from_registrypriv(struct adapter *adapter)
2181 {
2182 }
2183 
2184 /* the function is at passive_level */
rtw_joinbss_reset(struct adapter * padapter)2185 void rtw_joinbss_reset(struct adapter *padapter)
2186 {
2187 	u8 threshold;
2188 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2189 
2190 	struct ht_priv 	*phtpriv = &pmlmepriv->htpriv;
2191 
2192 	/* todo: if you want to do something io/reg/hw setting before join_bss, please add code here */
2193 
2194 	pmlmepriv->num_FortyMHzIntolerant = 0;
2195 
2196 	pmlmepriv->num_sta_no_ht = 0;
2197 
2198 	phtpriv->ampdu_enable = false;/* reset to disabled */
2199 
2200 	/*  TH = 1 => means that invalidate usb rx aggregation */
2201 	/*  TH = 0 => means that validate usb rx aggregation, use init value. */
2202 	if (phtpriv->ht_option) {
2203 		if (padapter->registrypriv.wifi_spec == 1)
2204 			threshold = 1;
2205 		else
2206 			threshold = 0;
2207 		rtw_hal_set_hwreg(padapter, HW_VAR_RXDMA_AGG_PG_TH, (u8 *)(&threshold));
2208 	} else {
2209 		threshold = 1;
2210 		rtw_hal_set_hwreg(padapter, HW_VAR_RXDMA_AGG_PG_TH, (u8 *)(&threshold));
2211 	}
2212 }
2213 
rtw_ht_use_default_setting(struct adapter * padapter)2214 void rtw_ht_use_default_setting(struct adapter *padapter)
2215 {
2216 	struct mlme_priv 	*pmlmepriv = &padapter->mlmepriv;
2217 	struct ht_priv 	*phtpriv = &pmlmepriv->htpriv;
2218 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
2219 	bool		bHwLDPCSupport = false, bHwSTBCSupport = false;
2220 	bool		bHwSupportBeamformer = false, bHwSupportBeamformee = false;
2221 
2222 	if (pregistrypriv->wifi_spec)
2223 		phtpriv->bss_coexist = 1;
2224 	else
2225 		phtpriv->bss_coexist = 0;
2226 
2227 	phtpriv->sgi_40m = TEST_FLAG(pregistrypriv->short_gi, BIT1) ? true : false;
2228 	phtpriv->sgi_20m = TEST_FLAG(pregistrypriv->short_gi, BIT0) ? true : false;
2229 
2230 	/*  LDPC support */
2231 	rtw_hal_get_def_var(padapter, HAL_DEF_RX_LDPC, (u8 *)&bHwLDPCSupport);
2232 	CLEAR_FLAGS(phtpriv->ldpc_cap);
2233 	if (bHwLDPCSupport) {
2234 		if (TEST_FLAG(pregistrypriv->ldpc_cap, BIT4))
2235 			SET_FLAG(phtpriv->ldpc_cap, LDPC_HT_ENABLE_RX);
2236 	}
2237 	rtw_hal_get_def_var(padapter, HAL_DEF_TX_LDPC, (u8 *)&bHwLDPCSupport);
2238 	if (bHwLDPCSupport) {
2239 		if (TEST_FLAG(pregistrypriv->ldpc_cap, BIT5))
2240 			SET_FLAG(phtpriv->ldpc_cap, LDPC_HT_ENABLE_TX);
2241 	}
2242 
2243 	/*  STBC */
2244 	rtw_hal_get_def_var(padapter, HAL_DEF_TX_STBC, (u8 *)&bHwSTBCSupport);
2245 	CLEAR_FLAGS(phtpriv->stbc_cap);
2246 	if (bHwSTBCSupport) {
2247 		if (TEST_FLAG(pregistrypriv->stbc_cap, BIT5))
2248 			SET_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_TX);
2249 	}
2250 	rtw_hal_get_def_var(padapter, HAL_DEF_RX_STBC, (u8 *)&bHwSTBCSupport);
2251 	if (bHwSTBCSupport) {
2252 		if (TEST_FLAG(pregistrypriv->stbc_cap, BIT4))
2253 			SET_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_RX);
2254 	}
2255 
2256 	/*  Beamforming setting */
2257 	rtw_hal_get_def_var(padapter, HAL_DEF_EXPLICIT_BEAMFORMER, (u8 *)&bHwSupportBeamformer);
2258 	rtw_hal_get_def_var(padapter, HAL_DEF_EXPLICIT_BEAMFORMEE, (u8 *)&bHwSupportBeamformee);
2259 	CLEAR_FLAGS(phtpriv->beamform_cap);
2260 	if (TEST_FLAG(pregistrypriv->beamform_cap, BIT4) && bHwSupportBeamformer)
2261 		SET_FLAG(phtpriv->beamform_cap, BEAMFORMING_HT_BEAMFORMER_ENABLE);
2262 
2263 	if (TEST_FLAG(pregistrypriv->beamform_cap, BIT5) && bHwSupportBeamformee)
2264 		SET_FLAG(phtpriv->beamform_cap, BEAMFORMING_HT_BEAMFORMEE_ENABLE);
2265 }
2266 
rtw_build_wmm_ie_ht(struct adapter * padapter,u8 * out_ie,uint * pout_len)2267 void rtw_build_wmm_ie_ht(struct adapter *padapter, u8 *out_ie, uint *pout_len)
2268 {
2269 	unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01, 0x00};
2270 	int out_len;
2271 
2272 	if (padapter->mlmepriv.qospriv.qos_option == 0) {
2273 		out_len = *pout_len;
2274 		rtw_set_ie(out_ie+out_len, WLAN_EID_VENDOR_SPECIFIC,
2275 			   _WMM_IE_Length_, WMM_IE, pout_len);
2276 
2277 		padapter->mlmepriv.qospriv.qos_option = 1;
2278 	}
2279 }
2280 
2281 /* the function is >= passive_level */
rtw_restructure_ht_ie(struct adapter * padapter,u8 * in_ie,u8 * out_ie,uint in_len,uint * pout_len,u8 channel)2282 unsigned int rtw_restructure_ht_ie(struct adapter *padapter, u8 *in_ie, u8 *out_ie, uint in_len, uint *pout_len, u8 channel)
2283 {
2284 	u32 ielen, out_len;
2285 	enum ieee80211_max_ampdu_length_exp max_rx_ampdu_factor;
2286 	unsigned char *p;
2287 	struct ieee80211_ht_cap ht_capie;
2288 	u8 cbw40_enable = 0, stbc_rx_enable = 0, operation_bw = 0;
2289 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
2290 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2291 	struct ht_priv 	*phtpriv = &pmlmepriv->htpriv;
2292 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2293 
2294 	phtpriv->ht_option = false;
2295 
2296 	out_len = *pout_len;
2297 
2298 	memset(&ht_capie, 0, sizeof(struct ieee80211_ht_cap));
2299 
2300 	ht_capie.cap_info = cpu_to_le16(IEEE80211_HT_CAP_DSSSCCK40);
2301 
2302 	if (phtpriv->sgi_20m)
2303 		ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_SGI_20);
2304 
2305 	/* Get HT BW */
2306 	if (!in_ie) {
2307 		/* TDLS: TODO 20/40 issue */
2308 		if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
2309 			operation_bw = padapter->mlmeextpriv.cur_bwmode;
2310 			if (operation_bw > CHANNEL_WIDTH_40)
2311 				operation_bw = CHANNEL_WIDTH_40;
2312 		} else
2313 			/* TDLS: TODO 40? */
2314 			operation_bw = CHANNEL_WIDTH_40;
2315 	} else {
2316 		p = rtw_get_ie(in_ie, WLAN_EID_HT_OPERATION, &ielen, in_len);
2317 		if (p && (ielen == sizeof(struct ieee80211_ht_addt_info))) {
2318 			struct HT_info_element *pht_info = (struct HT_info_element *)(p+2);
2319 
2320 			if (pht_info->infos[0] & BIT(2)) {
2321 				switch (pht_info->infos[0] & 0x3) {
2322 				case 1:
2323 				case 3:
2324 					operation_bw = CHANNEL_WIDTH_40;
2325 					break;
2326 				default:
2327 					operation_bw = CHANNEL_WIDTH_20;
2328 					break;
2329 				}
2330 			} else {
2331 				operation_bw = CHANNEL_WIDTH_20;
2332 			}
2333 		}
2334 	}
2335 
2336 	/* to disable 40M Hz support while gd_bw_40MHz_en = 0 */
2337 	if (channel > 14) {
2338 		if ((pregistrypriv->bw_mode & 0xf0) > 0)
2339 			cbw40_enable = 1;
2340 	} else {
2341 		if ((pregistrypriv->bw_mode & 0x0f) > 0)
2342 			cbw40_enable = 1;
2343 	}
2344 
2345 	if ((cbw40_enable == 1) && (operation_bw == CHANNEL_WIDTH_40)) {
2346 		ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH);
2347 		if (phtpriv->sgi_40m)
2348 			ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_SGI_40);
2349 	}
2350 
2351 	if (TEST_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_TX))
2352 		ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_TX_STBC);
2353 
2354 	/* todo: disable SM power save mode */
2355 	ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_SM_PS);
2356 
2357 	if (TEST_FLAG(phtpriv->stbc_cap, STBC_HT_ENABLE_RX)) {
2358 		if ((channel <= 14 && pregistrypriv->rx_stbc == 0x1) ||	/* enable for 2.4GHz */
2359 			(pregistrypriv->wifi_spec == 1))
2360 			stbc_rx_enable = 1;
2361 	}
2362 
2363 	/* fill default supported_mcs_set */
2364 	memcpy(&ht_capie.mcs, pmlmeext->default_supported_mcs_set, 16);
2365 
2366 	/* update default supported_mcs_set */
2367 	if (stbc_rx_enable)
2368 		ht_capie.cap_info |= cpu_to_le16(IEEE80211_HT_CAP_RX_STBC_1R);/* RX STBC One spatial stream */
2369 
2370 	set_mcs_rate_by_mask(ht_capie.mcs.rx_mask, MCS_RATE_1R);
2371 
2372 	{
2373 		u32 rx_packet_offset, max_recvbuf_sz;
2374 
2375 		rtw_hal_get_def_var(padapter, HAL_DEF_RX_PACKET_OFFSET, &rx_packet_offset);
2376 		rtw_hal_get_def_var(padapter, HAL_DEF_MAX_RECVBUF_SZ, &max_recvbuf_sz);
2377 	}
2378 
2379 	if (padapter->driver_rx_ampdu_factor != 0xFF)
2380 		max_rx_ampdu_factor =
2381 		  (enum ieee80211_max_ampdu_length_exp)padapter->driver_rx_ampdu_factor;
2382 	else
2383 		rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR,
2384 				    &max_rx_ampdu_factor);
2385 
2386 	/* rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR, &max_rx_ampdu_factor); */
2387 	ht_capie.ampdu_params_info = (max_rx_ampdu_factor&0x03);
2388 
2389 	if (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)
2390 		ht_capie.ampdu_params_info |= (IEEE80211_HT_CAP_AMPDU_DENSITY&(0x07<<2));
2391 	else
2392 		ht_capie.ampdu_params_info |= (IEEE80211_HT_CAP_AMPDU_DENSITY&0x00);
2393 
2394 	rtw_set_ie(out_ie+out_len, WLAN_EID_HT_CAPABILITY,
2395 		   sizeof(struct ieee80211_ht_cap), (unsigned char *)&ht_capie, pout_len);
2396 
2397 	phtpriv->ht_option = true;
2398 
2399 	if (in_ie) {
2400 		p = rtw_get_ie(in_ie, WLAN_EID_HT_OPERATION, &ielen, in_len);
2401 		if (p && (ielen == sizeof(struct ieee80211_ht_addt_info))) {
2402 			out_len = *pout_len;
2403 			rtw_set_ie(out_ie+out_len, WLAN_EID_HT_OPERATION, ielen, p+2, pout_len);
2404 		}
2405 	}
2406 
2407 	return phtpriv->ht_option;
2408 
2409 }
2410 
2411 /* the function is > passive_level (in critical_section) */
rtw_update_ht_cap(struct adapter * padapter,u8 * pie,uint ie_len,u8 channel)2412 void rtw_update_ht_cap(struct adapter *padapter, u8 *pie, uint ie_len, u8 channel)
2413 {
2414 	u8 *p, max_ampdu_sz;
2415 	int len;
2416 	/* struct sta_info *bmc_sta, *psta; */
2417 	struct ieee80211_ht_cap *pht_capie;
2418 	/* struct recv_reorder_ctrl *preorder_ctrl; */
2419 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2420 	struct ht_priv 	*phtpriv = &pmlmepriv->htpriv;
2421 	/* struct recv_priv *precvpriv = &padapter->recvpriv; */
2422 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
2423 	/* struct wlan_network *pcur_network = &(pmlmepriv->cur_network);; */
2424 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2425 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
2426 	u8 cbw40_enable = 0;
2427 
2428 	if (!phtpriv->ht_option)
2429 		return;
2430 
2431 	if ((!pmlmeinfo->HT_info_enable) || (!pmlmeinfo->HT_caps_enable))
2432 		return;
2433 
2434 	/* maybe needs check if ap supports rx ampdu. */
2435 	if (!(phtpriv->ampdu_enable) && pregistrypriv->ampdu_enable == 1) {
2436 		phtpriv->ampdu_enable = true;
2437 	}
2438 
2439 	/* check Max Rx A-MPDU Size */
2440 	len = 0;
2441 	p = rtw_get_ie(pie+sizeof(struct ndis_802_11_fix_ie), WLAN_EID_HT_CAPABILITY, &len, ie_len-sizeof(struct ndis_802_11_fix_ie));
2442 	if (p && len > 0) {
2443 		pht_capie = (struct ieee80211_ht_cap *)(p+2);
2444 		max_ampdu_sz = (pht_capie->ampdu_params_info & IEEE80211_HT_CAP_AMPDU_FACTOR);
2445 		max_ampdu_sz = 1 << (max_ampdu_sz+3); /*  max_ampdu_sz (kbytes); */
2446 
2447 		phtpriv->rx_ampdu_maxlen = max_ampdu_sz;
2448 
2449 	}
2450 
2451 	len = 0;
2452 	p = rtw_get_ie(pie+sizeof(struct ndis_802_11_fix_ie), WLAN_EID_HT_OPERATION, &len, ie_len-sizeof(struct ndis_802_11_fix_ie));
2453 	if (p && len > 0) {
2454 		/* todo: */
2455 	}
2456 
2457 	if (channel > 14) {
2458 		if ((pregistrypriv->bw_mode & 0xf0) > 0)
2459 			cbw40_enable = 1;
2460 	} else {
2461 		if ((pregistrypriv->bw_mode & 0x0f) > 0)
2462 			cbw40_enable = 1;
2463 	}
2464 
2465 	/* update cur_bwmode & cur_ch_offset */
2466 	if ((cbw40_enable) &&
2467 	    (le16_to_cpu(pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info) &
2468 	      BIT(1)) && (pmlmeinfo->HT_info.infos[0] & BIT(2))) {
2469 		int i;
2470 
2471 		/* update the MCS set */
2472 		for (i = 0; i < 16; i++)
2473 			pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate[i] &= pmlmeext->default_supported_mcs_set[i];
2474 
2475 		/* update the MCS rates */
2476 		set_mcs_rate_by_mask(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_RATE_1R);
2477 
2478 		/* switch to the 40M Hz mode according to the AP */
2479 		/* pmlmeext->cur_bwmode = CHANNEL_WIDTH_40; */
2480 		switch ((pmlmeinfo->HT_info.infos[0] & 0x3)) {
2481 		case EXTCHNL_OFFSET_UPPER:
2482 			pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
2483 			break;
2484 
2485 		case EXTCHNL_OFFSET_LOWER:
2486 			pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
2487 			break;
2488 
2489 		default:
2490 			pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
2491 			break;
2492 		}
2493 	}
2494 
2495 	/*  */
2496 	/*  Config SM Power Save setting */
2497 	/*  */
2498 	pmlmeinfo->SM_PS =
2499 		(le16_to_cpu(pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info) &
2500 		 0x0C) >> 2;
2501 
2502 	/*  */
2503 	/*  Config current HT Protection mode. */
2504 	/*  */
2505 	pmlmeinfo->HT_protection = pmlmeinfo->HT_info.infos[1] & 0x3;
2506 }
2507 
rtw_issue_addbareq_cmd(struct adapter * padapter,struct xmit_frame * pxmitframe)2508 void rtw_issue_addbareq_cmd(struct adapter *padapter, struct xmit_frame *pxmitframe)
2509 {
2510 	u8 issued;
2511 	int priority;
2512 	struct sta_info *psta;
2513 	struct ht_priv *phtpriv;
2514 	struct pkt_attrib *pattrib = &pxmitframe->attrib;
2515 	s32 bmcst = is_multicast_ether_addr(pattrib->ra);
2516 
2517 	/* if (bmcst || (padapter->mlmepriv.LinkDetectInfo.bTxBusyTraffic == false)) */
2518 	if (bmcst || (padapter->mlmepriv.LinkDetectInfo.NumTxOkInPeriod < 100))
2519 		return;
2520 
2521 	priority = pattrib->priority;
2522 
2523 	psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
2524 	if (pattrib->psta != psta)
2525 		return;
2526 
2527 	if (!psta)
2528 		return;
2529 
2530 	if (!(psta->state & _FW_LINKED))
2531 		return;
2532 
2533 	phtpriv = &psta->htpriv;
2534 
2535 	if (phtpriv->ht_option && phtpriv->ampdu_enable) {
2536 		issued = (phtpriv->agg_enable_bitmap>>priority)&0x1;
2537 		issued |= (phtpriv->candidate_tid_bitmap>>priority)&0x1;
2538 
2539 		if (issued == 0) {
2540 			psta->htpriv.candidate_tid_bitmap |= BIT((u8)priority);
2541 			rtw_addbareq_cmd(padapter, (u8) priority, pattrib->ra);
2542 		}
2543 	}
2544 
2545 }
2546 
rtw_append_exented_cap(struct adapter * padapter,u8 * out_ie,uint * pout_len)2547 void rtw_append_exented_cap(struct adapter *padapter, u8 *out_ie, uint *pout_len)
2548 {
2549 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2550 	struct ht_priv 	*phtpriv = &pmlmepriv->htpriv;
2551 	u8 cap_content[8] = {0};
2552 
2553 	if (phtpriv->bss_coexist)
2554 		SET_EXT_CAPABILITY_ELE_BSS_COEXIST(cap_content, 1);
2555 
2556 	rtw_set_ie(out_ie + *pout_len, WLAN_EID_EXT_CAPABILITY, 8, cap_content, pout_len);
2557 }
2558 
rtw_set_to_roam(struct adapter * adapter,u8 to_roam)2559 inline void rtw_set_to_roam(struct adapter *adapter, u8 to_roam)
2560 {
2561 	if (to_roam == 0)
2562 		adapter->mlmepriv.to_join = false;
2563 	adapter->mlmepriv.to_roam = to_roam;
2564 }
2565 
rtw_dec_to_roam(struct adapter * adapter)2566 inline u8 rtw_dec_to_roam(struct adapter *adapter)
2567 {
2568 	adapter->mlmepriv.to_roam--;
2569 	return adapter->mlmepriv.to_roam;
2570 }
2571 
rtw_to_roam(struct adapter * adapter)2572 inline u8 rtw_to_roam(struct adapter *adapter)
2573 {
2574 	return adapter->mlmepriv.to_roam;
2575 }
2576 
rtw_roaming(struct adapter * padapter,struct wlan_network * tgt_network)2577 void rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network)
2578 {
2579 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2580 
2581 	spin_lock_bh(&pmlmepriv->lock);
2582 	_rtw_roaming(padapter, tgt_network);
2583 	spin_unlock_bh(&pmlmepriv->lock);
2584 }
_rtw_roaming(struct adapter * padapter,struct wlan_network * tgt_network)2585 void _rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network)
2586 {
2587 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2588 	struct wlan_network *cur_network = &pmlmepriv->cur_network;
2589 
2590 	if (rtw_to_roam(padapter) > 0) {
2591 		memcpy(&pmlmepriv->assoc_ssid, &cur_network->network.ssid, sizeof(struct ndis_802_11_ssid));
2592 
2593 		pmlmepriv->assoc_by_bssid = false;
2594 
2595 		while (rtw_do_join(padapter) != _SUCCESS) {
2596 			rtw_dec_to_roam(padapter);
2597 			if (rtw_to_roam(padapter) <= 0) {
2598 				rtw_indicate_disconnect(padapter);
2599 				break;
2600 			}
2601 		}
2602 	}
2603 }
2604 
rtw_linked_check(struct adapter * padapter)2605 signed int rtw_linked_check(struct adapter *padapter)
2606 {
2607 	if ((check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE) == true) ||
2608 			(check_fwstate(&padapter->mlmepriv, WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE) == true)) {
2609 		if (padapter->stapriv.asoc_sta_count > 2)
2610 			return true;
2611 	} else {	/* Station mode */
2612 		if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) == true)
2613 			return true;
2614 	}
2615 	return false;
2616 }
2617