• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4  * All rights reserved.
5  */
6 
7 #include "wilc_wfi_netdevice.h"
8 
9 #define WILC_HIF_SCAN_TIMEOUT_MS                5000
10 #define WILC_HIF_CONNECT_TIMEOUT_MS             9500
11 
12 #define WILC_FALSE_FRMWR_CHANNEL		100
13 #define WILC_MAX_RATES_SUPPORTED		12
14 
15 #define WILC_SCAN_WID_LIST_SIZE		6
16 
17 struct wilc_rcvd_mac_info {
18 	u8 status;
19 };
20 
21 struct wilc_set_multicast {
22 	u32 enabled;
23 	u32 cnt;
24 	u8 *mc_list;
25 };
26 
27 struct wilc_del_all_sta {
28 	u8 assoc_sta;
29 	u8 mac[WILC_MAX_NUM_STA][ETH_ALEN];
30 };
31 
32 struct wilc_op_mode {
33 	__le32 mode;
34 };
35 
36 struct wilc_reg_frame {
37 	bool reg;
38 	u8 reg_id;
39 	__le16 frame_type;
40 } __packed;
41 
42 struct wilc_drv_handler {
43 	__le32 handler;
44 	u8 mode;
45 } __packed;
46 
47 struct wilc_wep_key {
48 	u8 index;
49 	u8 key_len;
50 	u8 key[0];
51 } __packed;
52 
53 struct wilc_sta_wpa_ptk {
54 	u8 mac_addr[ETH_ALEN];
55 	u8 key_len;
56 	u8 key[0];
57 } __packed;
58 
59 struct wilc_ap_wpa_ptk {
60 	u8 mac_addr[ETH_ALEN];
61 	u8 index;
62 	u8 key_len;
63 	u8 key[0];
64 } __packed;
65 
66 struct wilc_gtk_key {
67 	u8 mac_addr[ETH_ALEN];
68 	u8 rsc[8];
69 	u8 index;
70 	u8 key_len;
71 	u8 key[0];
72 } __packed;
73 
74 union wilc_message_body {
75 	struct wilc_rcvd_net_info net_info;
76 	struct wilc_rcvd_mac_info mac_info;
77 	struct wilc_set_multicast mc_info;
78 	struct wilc_remain_ch remain_on_ch;
79 	char *data;
80 };
81 
82 struct host_if_msg {
83 	union wilc_message_body body;
84 	struct wilc_vif *vif;
85 	struct work_struct work;
86 	void (*fn)(struct work_struct *ws);
87 	struct completion work_comp;
88 	bool is_sync;
89 };
90 
91 struct wilc_noa_opp_enable {
92 	u8 ct_window;
93 	u8 cnt;
94 	__le32 duration;
95 	__le32 interval;
96 	__le32 start_time;
97 } __packed;
98 
99 struct wilc_noa_opp_disable {
100 	u8 cnt;
101 	__le32 duration;
102 	__le32 interval;
103 	__le32 start_time;
104 } __packed;
105 
106 struct wilc_join_bss_param {
107 	char ssid[IEEE80211_MAX_SSID_LEN];
108 	u8 ssid_terminator;
109 	u8 bss_type;
110 	u8 ch;
111 	__le16 cap_info;
112 	u8 sa[ETH_ALEN];
113 	u8 bssid[ETH_ALEN];
114 	__le16 beacon_period;
115 	u8 dtim_period;
116 	u8 supp_rates[WILC_MAX_RATES_SUPPORTED + 1];
117 	u8 wmm_cap;
118 	u8 uapsd_cap;
119 	u8 ht_capable;
120 	u8 rsn_found;
121 	u8 rsn_grp_policy;
122 	u8 mode_802_11i;
123 	u8 p_suites[3];
124 	u8 akm_suites[3];
125 	u8 rsn_cap[2];
126 	u8 noa_enabled;
127 	__le32 tsf_lo;
128 	u8 idx;
129 	u8 opp_enabled;
130 	union {
131 		struct wilc_noa_opp_disable opp_dis;
132 		struct wilc_noa_opp_enable opp_en;
133 	};
134 } __packed;
135 
136 /* 'msg' should be free by the caller for syc */
137 static struct host_if_msg*
wilc_alloc_work(struct wilc_vif * vif,void (* work_fun)(struct work_struct *),bool is_sync)138 wilc_alloc_work(struct wilc_vif *vif, void (*work_fun)(struct work_struct *),
139 		bool is_sync)
140 {
141 	struct host_if_msg *msg;
142 
143 	if (!work_fun)
144 		return ERR_PTR(-EINVAL);
145 
146 	msg = kzalloc(sizeof(*msg), GFP_ATOMIC);
147 	if (!msg)
148 		return ERR_PTR(-ENOMEM);
149 	msg->fn = work_fun;
150 	msg->vif = vif;
151 	msg->is_sync = is_sync;
152 	if (is_sync)
153 		init_completion(&msg->work_comp);
154 
155 	return msg;
156 }
157 
wilc_enqueue_work(struct host_if_msg * msg)158 static int wilc_enqueue_work(struct host_if_msg *msg)
159 {
160 	INIT_WORK(&msg->work, msg->fn);
161 
162 	if (!msg->vif || !msg->vif->wilc || !msg->vif->wilc->hif_workqueue)
163 		return -EINVAL;
164 
165 	if (!queue_work(msg->vif->wilc->hif_workqueue, &msg->work))
166 		return -EINVAL;
167 
168 	return 0;
169 }
170 
171 /* The idx starts from 0 to (NUM_CONCURRENT_IFC - 1), but 0 index used as
172  * special purpose in wilc device, so we add 1 to the index to starts from 1.
173  * As a result, the returned index will be 1 to NUM_CONCURRENT_IFC.
174  */
wilc_get_vif_idx(struct wilc_vif * vif)175 int wilc_get_vif_idx(struct wilc_vif *vif)
176 {
177 	return vif->idx + 1;
178 }
179 
180 /* We need to minus 1 from idx which is from wilc device to get real index
181  * of wilc->vif[], because we add 1 when pass to wilc device in the function
182  * wilc_get_vif_idx.
183  * As a result, the index should be between 0 and (NUM_CONCURRENT_IFC - 1).
184  */
wilc_get_vif_from_idx(struct wilc * wilc,int idx)185 static struct wilc_vif *wilc_get_vif_from_idx(struct wilc *wilc, int idx)
186 {
187 	int index = idx - 1;
188 
189 	if (index < 0 || index >= WILC_NUM_CONCURRENT_IFC)
190 		return NULL;
191 
192 	return wilc->vif[index];
193 }
194 
handle_scan_done(struct wilc_vif * vif,enum scan_event evt)195 static int handle_scan_done(struct wilc_vif *vif, enum scan_event evt)
196 {
197 	int result = 0;
198 	u8 abort_running_scan;
199 	struct wid wid;
200 	struct host_if_drv *hif_drv = vif->hif_drv;
201 	struct wilc_user_scan_req *scan_req;
202 
203 	if (evt == SCAN_EVENT_ABORTED) {
204 		abort_running_scan = 1;
205 		wid.id = WID_ABORT_RUNNING_SCAN;
206 		wid.type = WID_CHAR;
207 		wid.val = (s8 *)&abort_running_scan;
208 		wid.size = sizeof(char);
209 
210 		result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
211 		if (result) {
212 			netdev_err(vif->ndev, "Failed to set abort running\n");
213 			result = -EFAULT;
214 		}
215 	}
216 
217 	if (!hif_drv) {
218 		netdev_err(vif->ndev, "%s: hif driver is NULL\n", __func__);
219 		return result;
220 	}
221 
222 	scan_req = &hif_drv->usr_scan_req;
223 	if (scan_req->scan_result) {
224 		scan_req->scan_result(evt, NULL, scan_req->arg);
225 		scan_req->scan_result = NULL;
226 	}
227 
228 	return result;
229 }
230 
wilc_scan(struct wilc_vif * vif,u8 scan_source,u8 scan_type,u8 * ch_freq_list,u8 ch_list_len,void (* scan_result_fn)(enum scan_event,struct wilc_rcvd_net_info *,void *),void * user_arg,struct cfg80211_scan_request * request)231 int wilc_scan(struct wilc_vif *vif, u8 scan_source, u8 scan_type,
232 	      u8 *ch_freq_list, u8 ch_list_len,
233 	      void (*scan_result_fn)(enum scan_event,
234 				     struct wilc_rcvd_net_info *, void *),
235 	      void *user_arg, struct cfg80211_scan_request *request)
236 {
237 	int result = 0;
238 	struct wid wid_list[WILC_SCAN_WID_LIST_SIZE];
239 	u32 index = 0;
240 	u32 i, scan_timeout;
241 	u8 *buffer;
242 	u8 valuesize = 0;
243 	u8 *search_ssid_vals = NULL;
244 	struct host_if_drv *hif_drv = vif->hif_drv;
245 
246 	if (hif_drv->hif_state >= HOST_IF_SCANNING &&
247 	    hif_drv->hif_state < HOST_IF_CONNECTED) {
248 		netdev_err(vif->ndev, "Already scan\n");
249 		result = -EBUSY;
250 		goto error;
251 	}
252 
253 	if (vif->connecting) {
254 		netdev_err(vif->ndev, "Don't do obss scan\n");
255 		result = -EBUSY;
256 		goto error;
257 	}
258 
259 	hif_drv->usr_scan_req.ch_cnt = 0;
260 
261 	if (request->n_ssids) {
262 		for (i = 0; i < request->n_ssids; i++)
263 			valuesize += ((request->ssids[i].ssid_len) + 1);
264 		search_ssid_vals = kmalloc(valuesize + 1, GFP_KERNEL);
265 		if (search_ssid_vals) {
266 			wid_list[index].id = WID_SSID_PROBE_REQ;
267 			wid_list[index].type = WID_STR;
268 			wid_list[index].val = search_ssid_vals;
269 			buffer = wid_list[index].val;
270 
271 			*buffer++ = request->n_ssids;
272 
273 			for (i = 0; i < request->n_ssids; i++) {
274 				*buffer++ = request->ssids[i].ssid_len;
275 				memcpy(buffer, request->ssids[i].ssid,
276 				       request->ssids[i].ssid_len);
277 				buffer += request->ssids[i].ssid_len;
278 			}
279 			wid_list[index].size = (s32)(valuesize + 1);
280 			index++;
281 		}
282 	}
283 
284 	wid_list[index].id = WID_INFO_ELEMENT_PROBE;
285 	wid_list[index].type = WID_BIN_DATA;
286 	wid_list[index].val = (s8 *)request->ie;
287 	wid_list[index].size = request->ie_len;
288 	index++;
289 
290 	wid_list[index].id = WID_SCAN_TYPE;
291 	wid_list[index].type = WID_CHAR;
292 	wid_list[index].size = sizeof(char);
293 	wid_list[index].val = (s8 *)&scan_type;
294 	index++;
295 
296 	if (scan_type == WILC_FW_PASSIVE_SCAN && request->duration) {
297 		wid_list[index].id = WID_PASSIVE_SCAN_TIME;
298 		wid_list[index].type = WID_SHORT;
299 		wid_list[index].size = sizeof(u16);
300 		wid_list[index].val = (s8 *)&request->duration;
301 		index++;
302 
303 		scan_timeout = (request->duration * ch_list_len) + 500;
304 	} else {
305 		scan_timeout = WILC_HIF_SCAN_TIMEOUT_MS;
306 	}
307 
308 	wid_list[index].id = WID_SCAN_CHANNEL_LIST;
309 	wid_list[index].type = WID_BIN_DATA;
310 
311 	if (ch_freq_list && ch_list_len > 0) {
312 		for (i = 0; i < ch_list_len; i++) {
313 			if (ch_freq_list[i] > 0)
314 				ch_freq_list[i] -= 1;
315 		}
316 	}
317 
318 	wid_list[index].val = ch_freq_list;
319 	wid_list[index].size = ch_list_len;
320 	index++;
321 
322 	wid_list[index].id = WID_START_SCAN_REQ;
323 	wid_list[index].type = WID_CHAR;
324 	wid_list[index].size = sizeof(char);
325 	wid_list[index].val = (s8 *)&scan_source;
326 	index++;
327 
328 	hif_drv->usr_scan_req.scan_result = scan_result_fn;
329 	hif_drv->usr_scan_req.arg = user_arg;
330 
331 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, wid_list, index);
332 	if (result) {
333 		netdev_err(vif->ndev, "Failed to send scan parameters\n");
334 		goto error;
335 	}
336 
337 	hif_drv->scan_timer_vif = vif;
338 	mod_timer(&hif_drv->scan_timer,
339 		  jiffies + msecs_to_jiffies(scan_timeout));
340 
341 error:
342 
343 	kfree(search_ssid_vals);
344 
345 	return result;
346 }
347 
wilc_send_connect_wid(struct wilc_vif * vif)348 static int wilc_send_connect_wid(struct wilc_vif *vif)
349 {
350 	int result = 0;
351 	struct wid wid_list[4];
352 	u32 wid_cnt = 0;
353 	struct host_if_drv *hif_drv = vif->hif_drv;
354 	struct wilc_conn_info *conn_attr = &hif_drv->conn_info;
355 	struct wilc_join_bss_param *bss_param = conn_attr->param;
356 
357 	wid_list[wid_cnt].id = WID_INFO_ELEMENT_ASSOCIATE;
358 	wid_list[wid_cnt].type = WID_BIN_DATA;
359 	wid_list[wid_cnt].val = conn_attr->req_ies;
360 	wid_list[wid_cnt].size = conn_attr->req_ies_len;
361 	wid_cnt++;
362 
363 	wid_list[wid_cnt].id = WID_11I_MODE;
364 	wid_list[wid_cnt].type = WID_CHAR;
365 	wid_list[wid_cnt].size = sizeof(char);
366 	wid_list[wid_cnt].val = (s8 *)&conn_attr->security;
367 	wid_cnt++;
368 
369 	wid_list[wid_cnt].id = WID_AUTH_TYPE;
370 	wid_list[wid_cnt].type = WID_CHAR;
371 	wid_list[wid_cnt].size = sizeof(char);
372 	wid_list[wid_cnt].val = (s8 *)&conn_attr->auth_type;
373 	wid_cnt++;
374 
375 	wid_list[wid_cnt].id = WID_JOIN_REQ_EXTENDED;
376 	wid_list[wid_cnt].type = WID_STR;
377 	wid_list[wid_cnt].size = sizeof(*bss_param);
378 	wid_list[wid_cnt].val = (u8 *)bss_param;
379 	wid_cnt++;
380 
381 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, wid_list, wid_cnt);
382 	if (result) {
383 		netdev_err(vif->ndev, "failed to send config packet\n");
384 		goto error;
385 	} else {
386 		hif_drv->hif_state = HOST_IF_WAITING_CONN_RESP;
387 	}
388 
389 	return 0;
390 
391 error:
392 
393 	kfree(conn_attr->req_ies);
394 	conn_attr->req_ies = NULL;
395 
396 	return result;
397 }
398 
handle_connect_timeout(struct work_struct * work)399 static void handle_connect_timeout(struct work_struct *work)
400 {
401 	struct host_if_msg *msg = container_of(work, struct host_if_msg, work);
402 	struct wilc_vif *vif = msg->vif;
403 	int result;
404 	struct wid wid;
405 	u16 dummy_reason_code = 0;
406 	struct host_if_drv *hif_drv = vif->hif_drv;
407 
408 	if (!hif_drv) {
409 		netdev_err(vif->ndev, "%s: hif driver is NULL\n", __func__);
410 		goto out;
411 	}
412 
413 	hif_drv->hif_state = HOST_IF_IDLE;
414 
415 	if (hif_drv->conn_info.conn_result) {
416 		hif_drv->conn_info.conn_result(CONN_DISCONN_EVENT_CONN_RESP,
417 					       WILC_MAC_STATUS_DISCONNECTED,
418 					       hif_drv->conn_info.arg);
419 
420 	} else {
421 		netdev_err(vif->ndev, "%s: conn_result is NULL\n", __func__);
422 	}
423 
424 	wid.id = WID_DISCONNECT;
425 	wid.type = WID_CHAR;
426 	wid.val = (s8 *)&dummy_reason_code;
427 	wid.size = sizeof(char);
428 
429 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
430 	if (result)
431 		netdev_err(vif->ndev, "Failed to send disconnect\n");
432 
433 	hif_drv->conn_info.req_ies_len = 0;
434 	kfree(hif_drv->conn_info.req_ies);
435 	hif_drv->conn_info.req_ies = NULL;
436 
437 out:
438 	kfree(msg);
439 }
440 
wilc_parse_join_bss_param(struct cfg80211_bss * bss,struct cfg80211_crypto_settings * crypto)441 void *wilc_parse_join_bss_param(struct cfg80211_bss *bss,
442 				struct cfg80211_crypto_settings *crypto)
443 {
444 	const u8 *ies_data, *tim_elm, *ssid_elm, *rates_ie, *supp_rates_ie;
445 	const u8 *ht_ie, *wpa_ie, *wmm_ie, *rsn_ie;
446 	struct ieee80211_p2p_noa_attr noa_attr;
447 	const struct cfg80211_bss_ies *ies;
448 	struct wilc_join_bss_param *param;
449 	u8 rates_len = 0, ies_len;
450 	int ret;
451 
452 	param = kzalloc(sizeof(*param), GFP_KERNEL);
453 	if (!param)
454 		return NULL;
455 
456 	rcu_read_lock();
457 	ies = rcu_dereference(bss->ies);
458 	ies_data = kmemdup(ies->data, ies->len, GFP_ATOMIC);
459 	if (!ies_data) {
460 		rcu_read_unlock();
461 		kfree(param);
462 		return NULL;
463 	}
464 	ies_len = ies->len;
465 	rcu_read_unlock();
466 
467 	param->beacon_period = cpu_to_le16(bss->beacon_interval);
468 	param->cap_info = cpu_to_le16(bss->capability);
469 	param->bss_type = WILC_FW_BSS_TYPE_INFRA;
470 	param->ch = ieee80211_frequency_to_channel(bss->channel->center_freq);
471 	ether_addr_copy(param->bssid, bss->bssid);
472 
473 	ssid_elm = cfg80211_find_ie(WLAN_EID_SSID, ies_data, ies_len);
474 	if (ssid_elm) {
475 		if (ssid_elm[1] <= IEEE80211_MAX_SSID_LEN)
476 			memcpy(param->ssid, ssid_elm + 2, ssid_elm[1]);
477 	}
478 
479 	tim_elm = cfg80211_find_ie(WLAN_EID_TIM, ies_data, ies_len);
480 	if (tim_elm && tim_elm[1] >= 2)
481 		param->dtim_period = tim_elm[3];
482 
483 	memset(param->p_suites, 0xFF, 3);
484 	memset(param->akm_suites, 0xFF, 3);
485 
486 	rates_ie = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies_data, ies_len);
487 	if (rates_ie) {
488 		rates_len = rates_ie[1];
489 		if (rates_len > WILC_MAX_RATES_SUPPORTED)
490 			rates_len = WILC_MAX_RATES_SUPPORTED;
491 		param->supp_rates[0] = rates_len;
492 		memcpy(&param->supp_rates[1], rates_ie + 2, rates_len);
493 	}
494 
495 	if (rates_len < WILC_MAX_RATES_SUPPORTED) {
496 		supp_rates_ie = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES,
497 						 ies_data, ies_len);
498 		if (supp_rates_ie) {
499 			u8 ext_rates = supp_rates_ie[1];
500 
501 			if (ext_rates > (WILC_MAX_RATES_SUPPORTED - rates_len))
502 				param->supp_rates[0] = WILC_MAX_RATES_SUPPORTED;
503 			else
504 				param->supp_rates[0] += ext_rates;
505 
506 			memcpy(&param->supp_rates[rates_len + 1],
507 			       supp_rates_ie + 2,
508 			       (param->supp_rates[0] - rates_len));
509 		}
510 	}
511 
512 	ht_ie = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies_data, ies_len);
513 	if (ht_ie)
514 		param->ht_capable = true;
515 
516 	ret = cfg80211_get_p2p_attr(ies_data, ies_len,
517 				    IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
518 				    (u8 *)&noa_attr, sizeof(noa_attr));
519 	if (ret > 0) {
520 		param->tsf_lo = cpu_to_le32(ies->tsf);
521 		param->noa_enabled = 1;
522 		param->idx = noa_attr.index;
523 		if (noa_attr.oppps_ctwindow & IEEE80211_P2P_OPPPS_ENABLE_BIT) {
524 			param->opp_enabled = 1;
525 			param->opp_en.ct_window = noa_attr.oppps_ctwindow;
526 			param->opp_en.cnt = noa_attr.desc[0].count;
527 			param->opp_en.duration = noa_attr.desc[0].duration;
528 			param->opp_en.interval = noa_attr.desc[0].interval;
529 			param->opp_en.start_time = noa_attr.desc[0].start_time;
530 		} else {
531 			param->opp_enabled = 0;
532 			param->opp_dis.cnt = noa_attr.desc[0].count;
533 			param->opp_dis.duration = noa_attr.desc[0].duration;
534 			param->opp_dis.interval = noa_attr.desc[0].interval;
535 			param->opp_dis.start_time = noa_attr.desc[0].start_time;
536 		}
537 	}
538 	wmm_ie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
539 					 WLAN_OUI_TYPE_MICROSOFT_WMM,
540 					 ies_data, ies_len);
541 	if (wmm_ie) {
542 		struct ieee80211_wmm_param_ie *ie;
543 
544 		ie = (struct ieee80211_wmm_param_ie *)wmm_ie;
545 		if ((ie->oui_subtype == 0 || ie->oui_subtype == 1) &&
546 		    ie->version == 1) {
547 			param->wmm_cap = true;
548 			if (ie->qos_info & BIT(7))
549 				param->uapsd_cap = true;
550 		}
551 	}
552 
553 	wpa_ie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
554 					 WLAN_OUI_TYPE_MICROSOFT_WPA,
555 					 ies_data, ies_len);
556 	if (wpa_ie) {
557 		param->mode_802_11i = 1;
558 		param->rsn_found = true;
559 	}
560 
561 	rsn_ie = cfg80211_find_ie(WLAN_EID_RSN, ies_data, ies_len);
562 	if (rsn_ie) {
563 		int offset = 8;
564 
565 		param->mode_802_11i = 2;
566 		param->rsn_found = true;
567 		//extract RSN capabilities
568 		offset += (rsn_ie[offset] * 4) + 2;
569 		offset += (rsn_ie[offset] * 4) + 2;
570 		memcpy(param->rsn_cap, &rsn_ie[offset], 2);
571 	}
572 
573 	if (param->rsn_found) {
574 		int i;
575 
576 		param->rsn_grp_policy = crypto->cipher_group & 0xFF;
577 		for (i = 0; i < crypto->n_ciphers_pairwise && i < 3; i++)
578 			param->p_suites[i] = crypto->ciphers_pairwise[i] & 0xFF;
579 
580 		for (i = 0; i < crypto->n_akm_suites && i < 3; i++)
581 			param->akm_suites[i] = crypto->akm_suites[i] & 0xFF;
582 	}
583 
584 	kfree(ies_data);
585 	return (void *)param;
586 }
587 
handle_rcvd_ntwrk_info(struct work_struct * work)588 static void handle_rcvd_ntwrk_info(struct work_struct *work)
589 {
590 	struct host_if_msg *msg = container_of(work, struct host_if_msg, work);
591 	struct wilc_rcvd_net_info *rcvd_info = &msg->body.net_info;
592 	struct wilc_user_scan_req *scan_req = &msg->vif->hif_drv->usr_scan_req;
593 	const u8 *ch_elm;
594 	u8 *ies;
595 	int ies_len;
596 	size_t offset;
597 
598 	if (ieee80211_is_probe_resp(rcvd_info->mgmt->frame_control))
599 		offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
600 	else if (ieee80211_is_beacon(rcvd_info->mgmt->frame_control))
601 		offset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
602 	else
603 		goto done;
604 
605 	ies = rcvd_info->mgmt->u.beacon.variable;
606 	ies_len = rcvd_info->frame_len - offset;
607 	if (ies_len <= 0)
608 		goto done;
609 
610 	ch_elm = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ies, ies_len);
611 	if (ch_elm && ch_elm[1] > 0)
612 		rcvd_info->ch = ch_elm[2];
613 
614 	if (scan_req->scan_result)
615 		scan_req->scan_result(SCAN_EVENT_NETWORK_FOUND, rcvd_info,
616 				      scan_req->arg);
617 
618 done:
619 	kfree(rcvd_info->mgmt);
620 	kfree(msg);
621 }
622 
host_int_get_assoc_res_info(struct wilc_vif * vif,u8 * assoc_resp_info,u32 max_assoc_resp_info_len,u32 * rcvd_assoc_resp_info_len)623 static void host_int_get_assoc_res_info(struct wilc_vif *vif,
624 					u8 *assoc_resp_info,
625 					u32 max_assoc_resp_info_len,
626 					u32 *rcvd_assoc_resp_info_len)
627 {
628 	int result;
629 	struct wid wid;
630 
631 	wid.id = WID_ASSOC_RES_INFO;
632 	wid.type = WID_STR;
633 	wid.val = assoc_resp_info;
634 	wid.size = max_assoc_resp_info_len;
635 
636 	result = wilc_send_config_pkt(vif, WILC_GET_CFG, &wid, 1);
637 	if (result) {
638 		*rcvd_assoc_resp_info_len = 0;
639 		netdev_err(vif->ndev, "Failed to send association response\n");
640 		return;
641 	}
642 
643 	*rcvd_assoc_resp_info_len = wid.size;
644 }
645 
wilc_parse_assoc_resp_info(u8 * buffer,u32 buffer_len,struct wilc_conn_info * ret_conn_info)646 static s32 wilc_parse_assoc_resp_info(u8 *buffer, u32 buffer_len,
647 				      struct wilc_conn_info *ret_conn_info)
648 {
649 	u8 *ies;
650 	u16 ies_len;
651 	struct assoc_resp *res = (struct assoc_resp *)buffer;
652 
653 	ret_conn_info->status = le16_to_cpu(res->status_code);
654 	if (ret_conn_info->status == WLAN_STATUS_SUCCESS) {
655 		ies = &buffer[sizeof(*res)];
656 		ies_len = buffer_len - sizeof(*res);
657 
658 		ret_conn_info->resp_ies = kmemdup(ies, ies_len, GFP_KERNEL);
659 		if (!ret_conn_info->resp_ies)
660 			return -ENOMEM;
661 
662 		ret_conn_info->resp_ies_len = ies_len;
663 	}
664 
665 	return 0;
666 }
667 
host_int_parse_assoc_resp_info(struct wilc_vif * vif,u8 mac_status)668 static inline void host_int_parse_assoc_resp_info(struct wilc_vif *vif,
669 						  u8 mac_status)
670 {
671 	struct host_if_drv *hif_drv = vif->hif_drv;
672 	struct wilc_conn_info *conn_info = &hif_drv->conn_info;
673 
674 	if (mac_status == WILC_MAC_STATUS_CONNECTED) {
675 		u32 assoc_resp_info_len;
676 
677 		memset(hif_drv->assoc_resp, 0, WILC_MAX_ASSOC_RESP_FRAME_SIZE);
678 
679 		host_int_get_assoc_res_info(vif, hif_drv->assoc_resp,
680 					    WILC_MAX_ASSOC_RESP_FRAME_SIZE,
681 					    &assoc_resp_info_len);
682 
683 		if (assoc_resp_info_len != 0) {
684 			s32 err = 0;
685 
686 			err = wilc_parse_assoc_resp_info(hif_drv->assoc_resp,
687 							 assoc_resp_info_len,
688 							 conn_info);
689 			if (err)
690 				netdev_err(vif->ndev,
691 					   "wilc_parse_assoc_resp_info() returned error %d\n",
692 					   err);
693 		}
694 	}
695 
696 	del_timer(&hif_drv->connect_timer);
697 	conn_info->conn_result(CONN_DISCONN_EVENT_CONN_RESP, mac_status,
698 			       hif_drv->conn_info.arg);
699 
700 	if (mac_status == WILC_MAC_STATUS_CONNECTED &&
701 	    conn_info->status == WLAN_STATUS_SUCCESS) {
702 		ether_addr_copy(hif_drv->assoc_bssid, conn_info->bssid);
703 		hif_drv->hif_state = HOST_IF_CONNECTED;
704 	} else {
705 		hif_drv->hif_state = HOST_IF_IDLE;
706 	}
707 
708 	kfree(conn_info->resp_ies);
709 	conn_info->resp_ies = NULL;
710 	conn_info->resp_ies_len = 0;
711 
712 	kfree(conn_info->req_ies);
713 	conn_info->req_ies = NULL;
714 	conn_info->req_ies_len = 0;
715 }
716 
host_int_handle_disconnect(struct wilc_vif * vif)717 static inline void host_int_handle_disconnect(struct wilc_vif *vif)
718 {
719 	struct host_if_drv *hif_drv = vif->hif_drv;
720 
721 	if (hif_drv->usr_scan_req.scan_result) {
722 		del_timer(&hif_drv->scan_timer);
723 		handle_scan_done(vif, SCAN_EVENT_ABORTED);
724 	}
725 
726 	if (hif_drv->conn_info.conn_result)
727 		hif_drv->conn_info.conn_result(CONN_DISCONN_EVENT_DISCONN_NOTIF,
728 					       0, hif_drv->conn_info.arg);
729 	else
730 		netdev_err(vif->ndev, "%s: conn_result is NULL\n", __func__);
731 
732 	eth_zero_addr(hif_drv->assoc_bssid);
733 
734 	hif_drv->conn_info.req_ies_len = 0;
735 	kfree(hif_drv->conn_info.req_ies);
736 	hif_drv->conn_info.req_ies = NULL;
737 	hif_drv->hif_state = HOST_IF_IDLE;
738 }
739 
handle_rcvd_gnrl_async_info(struct work_struct * work)740 static void handle_rcvd_gnrl_async_info(struct work_struct *work)
741 {
742 	struct host_if_msg *msg = container_of(work, struct host_if_msg, work);
743 	struct wilc_vif *vif = msg->vif;
744 	struct wilc_rcvd_mac_info *mac_info = &msg->body.mac_info;
745 	struct host_if_drv *hif_drv = vif->hif_drv;
746 
747 	if (!hif_drv) {
748 		netdev_err(vif->ndev, "%s: hif driver is NULL\n", __func__);
749 		goto free_msg;
750 	}
751 
752 	if (!hif_drv->conn_info.conn_result) {
753 		netdev_err(vif->ndev, "%s: conn_result is NULL\n", __func__);
754 		goto free_msg;
755 	}
756 
757 	if (hif_drv->hif_state == HOST_IF_WAITING_CONN_RESP) {
758 		host_int_parse_assoc_resp_info(vif, mac_info->status);
759 	} else if (mac_info->status == WILC_MAC_STATUS_DISCONNECTED) {
760 		if (hif_drv->hif_state == HOST_IF_CONNECTED) {
761 			host_int_handle_disconnect(vif);
762 		} else if (hif_drv->usr_scan_req.scan_result) {
763 			del_timer(&hif_drv->scan_timer);
764 			handle_scan_done(vif, SCAN_EVENT_ABORTED);
765 		}
766 	}
767 
768 free_msg:
769 	kfree(msg);
770 }
771 
wilc_disconnect(struct wilc_vif * vif)772 int wilc_disconnect(struct wilc_vif *vif)
773 {
774 	struct wid wid;
775 	struct host_if_drv *hif_drv = vif->hif_drv;
776 	struct wilc_user_scan_req *scan_req;
777 	struct wilc_conn_info *conn_info;
778 	int result;
779 	u16 dummy_reason_code = 0;
780 
781 	wid.id = WID_DISCONNECT;
782 	wid.type = WID_CHAR;
783 	wid.val = (s8 *)&dummy_reason_code;
784 	wid.size = sizeof(char);
785 
786 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
787 	if (result) {
788 		netdev_err(vif->ndev, "Failed to send disconnect\n");
789 		return result;
790 	}
791 
792 	scan_req = &hif_drv->usr_scan_req;
793 	conn_info = &hif_drv->conn_info;
794 
795 	if (scan_req->scan_result) {
796 		del_timer(&hif_drv->scan_timer);
797 		scan_req->scan_result(SCAN_EVENT_ABORTED, NULL, scan_req->arg);
798 		scan_req->scan_result = NULL;
799 	}
800 
801 	if (conn_info->conn_result) {
802 		if (hif_drv->hif_state == HOST_IF_WAITING_CONN_RESP)
803 			del_timer(&hif_drv->connect_timer);
804 
805 		conn_info->conn_result(CONN_DISCONN_EVENT_DISCONN_NOTIF, 0,
806 				       conn_info->arg);
807 	} else {
808 		netdev_err(vif->ndev, "%s: conn_result is NULL\n", __func__);
809 	}
810 
811 	hif_drv->hif_state = HOST_IF_IDLE;
812 
813 	eth_zero_addr(hif_drv->assoc_bssid);
814 
815 	conn_info->req_ies_len = 0;
816 	kfree(conn_info->req_ies);
817 	conn_info->req_ies = NULL;
818 
819 	return 0;
820 }
821 
wilc_get_statistics(struct wilc_vif * vif,struct rf_info * stats)822 int wilc_get_statistics(struct wilc_vif *vif, struct rf_info *stats)
823 {
824 	struct wid wid_list[5];
825 	u32 wid_cnt = 0, result;
826 
827 	wid_list[wid_cnt].id = WID_LINKSPEED;
828 	wid_list[wid_cnt].type = WID_CHAR;
829 	wid_list[wid_cnt].size = sizeof(char);
830 	wid_list[wid_cnt].val = (s8 *)&stats->link_speed;
831 	wid_cnt++;
832 
833 	wid_list[wid_cnt].id = WID_RSSI;
834 	wid_list[wid_cnt].type = WID_CHAR;
835 	wid_list[wid_cnt].size = sizeof(char);
836 	wid_list[wid_cnt].val = (s8 *)&stats->rssi;
837 	wid_cnt++;
838 
839 	wid_list[wid_cnt].id = WID_SUCCESS_FRAME_COUNT;
840 	wid_list[wid_cnt].type = WID_INT;
841 	wid_list[wid_cnt].size = sizeof(u32);
842 	wid_list[wid_cnt].val = (s8 *)&stats->tx_cnt;
843 	wid_cnt++;
844 
845 	wid_list[wid_cnt].id = WID_RECEIVED_FRAGMENT_COUNT;
846 	wid_list[wid_cnt].type = WID_INT;
847 	wid_list[wid_cnt].size = sizeof(u32);
848 	wid_list[wid_cnt].val = (s8 *)&stats->rx_cnt;
849 	wid_cnt++;
850 
851 	wid_list[wid_cnt].id = WID_FAILED_COUNT;
852 	wid_list[wid_cnt].type = WID_INT;
853 	wid_list[wid_cnt].size = sizeof(u32);
854 	wid_list[wid_cnt].val = (s8 *)&stats->tx_fail_cnt;
855 	wid_cnt++;
856 
857 	result = wilc_send_config_pkt(vif, WILC_GET_CFG, wid_list, wid_cnt);
858 	if (result) {
859 		netdev_err(vif->ndev, "Failed to send scan parameters\n");
860 		return result;
861 	}
862 
863 	if (stats->link_speed > TCP_ACK_FILTER_LINK_SPEED_THRESH &&
864 	    stats->link_speed != DEFAULT_LINK_SPEED)
865 		wilc_enable_tcp_ack_filter(vif, true);
866 	else if (stats->link_speed != DEFAULT_LINK_SPEED)
867 		wilc_enable_tcp_ack_filter(vif, false);
868 
869 	return result;
870 }
871 
handle_get_statistics(struct work_struct * work)872 static void handle_get_statistics(struct work_struct *work)
873 {
874 	struct host_if_msg *msg = container_of(work, struct host_if_msg, work);
875 	struct wilc_vif *vif = msg->vif;
876 	struct rf_info *stats = (struct rf_info *)msg->body.data;
877 
878 	wilc_get_statistics(vif, stats);
879 
880 	kfree(msg);
881 }
882 
wilc_hif_pack_sta_param(u8 * cur_byte,const u8 * mac,struct station_parameters * params)883 static void wilc_hif_pack_sta_param(u8 *cur_byte, const u8 *mac,
884 				    struct station_parameters *params)
885 {
886 	ether_addr_copy(cur_byte, mac);
887 	cur_byte += ETH_ALEN;
888 
889 	put_unaligned_le16(params->aid, cur_byte);
890 	cur_byte += 2;
891 
892 	*cur_byte++ = params->supported_rates_len;
893 	if (params->supported_rates_len > 0)
894 		memcpy(cur_byte, params->supported_rates,
895 		       params->supported_rates_len);
896 	cur_byte += params->supported_rates_len;
897 
898 	if (params->ht_capa) {
899 		*cur_byte++ = true;
900 		memcpy(cur_byte, &params->ht_capa,
901 		       sizeof(struct ieee80211_ht_cap));
902 	} else {
903 		*cur_byte++ = false;
904 	}
905 	cur_byte += sizeof(struct ieee80211_ht_cap);
906 
907 	put_unaligned_le16(params->sta_flags_mask, cur_byte);
908 	cur_byte += 2;
909 	put_unaligned_le16(params->sta_flags_set, cur_byte);
910 }
911 
handle_remain_on_chan(struct wilc_vif * vif,struct wilc_remain_ch * hif_remain_ch)912 static int handle_remain_on_chan(struct wilc_vif *vif,
913 				 struct wilc_remain_ch *hif_remain_ch)
914 {
915 	int result;
916 	u8 remain_on_chan_flag;
917 	struct wid wid;
918 	struct host_if_drv *hif_drv = vif->hif_drv;
919 
920 	if (hif_drv->usr_scan_req.scan_result)
921 		return -EBUSY;
922 
923 	if (hif_drv->hif_state == HOST_IF_WAITING_CONN_RESP)
924 		return -EBUSY;
925 
926 	if (vif->connecting)
927 		return -EBUSY;
928 
929 	remain_on_chan_flag = true;
930 	wid.id = WID_REMAIN_ON_CHAN;
931 	wid.type = WID_STR;
932 	wid.size = 2;
933 	wid.val = kmalloc(wid.size, GFP_KERNEL);
934 	if (!wid.val)
935 		return -ENOMEM;
936 
937 	wid.val[0] = remain_on_chan_flag;
938 	wid.val[1] = (s8)hif_remain_ch->ch;
939 
940 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
941 	kfree(wid.val);
942 	if (result)
943 		return -EBUSY;
944 
945 	hif_drv->remain_on_ch.arg = hif_remain_ch->arg;
946 	hif_drv->remain_on_ch.expired = hif_remain_ch->expired;
947 	hif_drv->remain_on_ch.ch = hif_remain_ch->ch;
948 	hif_drv->remain_on_ch.cookie = hif_remain_ch->cookie;
949 	hif_drv->remain_on_ch_timer_vif = vif;
950 
951 	return 0;
952 }
953 
wilc_handle_roc_expired(struct wilc_vif * vif,u64 cookie)954 static int wilc_handle_roc_expired(struct wilc_vif *vif, u64 cookie)
955 {
956 	u8 remain_on_chan_flag;
957 	struct wid wid;
958 	int result;
959 	struct host_if_drv *hif_drv = vif->hif_drv;
960 	struct wilc_priv *priv = wdev_priv(vif->ndev->ieee80211_ptr);
961 
962 	if (priv->p2p_listen_state) {
963 		remain_on_chan_flag = false;
964 		wid.id = WID_REMAIN_ON_CHAN;
965 		wid.type = WID_STR;
966 		wid.size = 2;
967 
968 		wid.val = kmalloc(wid.size, GFP_KERNEL);
969 		if (!wid.val)
970 			return -ENOMEM;
971 
972 		wid.val[0] = remain_on_chan_flag;
973 		wid.val[1] = WILC_FALSE_FRMWR_CHANNEL;
974 
975 		result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
976 		kfree(wid.val);
977 		if (result != 0) {
978 			netdev_err(vif->ndev, "Failed to set remain channel\n");
979 			return -EINVAL;
980 		}
981 
982 		if (hif_drv->remain_on_ch.expired) {
983 			hif_drv->remain_on_ch.expired(hif_drv->remain_on_ch.arg,
984 						      cookie);
985 		}
986 	} else {
987 		netdev_dbg(vif->ndev, "Not in listen state\n");
988 	}
989 
990 	return 0;
991 }
992 
wilc_handle_listen_state_expired(struct work_struct * work)993 static void wilc_handle_listen_state_expired(struct work_struct *work)
994 {
995 	struct host_if_msg *msg = container_of(work, struct host_if_msg, work);
996 
997 	wilc_handle_roc_expired(msg->vif, msg->body.remain_on_ch.cookie);
998 	kfree(msg);
999 }
1000 
listen_timer_cb(struct timer_list * t)1001 static void listen_timer_cb(struct timer_list *t)
1002 {
1003 	struct host_if_drv *hif_drv = from_timer(hif_drv, t,
1004 						      remain_on_ch_timer);
1005 	struct wilc_vif *vif = hif_drv->remain_on_ch_timer_vif;
1006 	int result;
1007 	struct host_if_msg *msg;
1008 
1009 	del_timer(&vif->hif_drv->remain_on_ch_timer);
1010 
1011 	msg = wilc_alloc_work(vif, wilc_handle_listen_state_expired, false);
1012 	if (IS_ERR(msg))
1013 		return;
1014 
1015 	msg->body.remain_on_ch.cookie = vif->hif_drv->remain_on_ch.cookie;
1016 
1017 	result = wilc_enqueue_work(msg);
1018 	if (result) {
1019 		netdev_err(vif->ndev, "%s: enqueue work failed\n", __func__);
1020 		kfree(msg);
1021 	}
1022 }
1023 
handle_set_mcast_filter(struct work_struct * work)1024 static void handle_set_mcast_filter(struct work_struct *work)
1025 {
1026 	struct host_if_msg *msg = container_of(work, struct host_if_msg, work);
1027 	struct wilc_vif *vif = msg->vif;
1028 	struct wilc_set_multicast *set_mc = &msg->body.mc_info;
1029 	int result;
1030 	struct wid wid;
1031 	u8 *cur_byte;
1032 
1033 	wid.id = WID_SETUP_MULTICAST_FILTER;
1034 	wid.type = WID_BIN;
1035 	wid.size = sizeof(struct wilc_set_multicast) + (set_mc->cnt * ETH_ALEN);
1036 	wid.val = kmalloc(wid.size, GFP_KERNEL);
1037 	if (!wid.val)
1038 		goto error;
1039 
1040 	cur_byte = wid.val;
1041 	put_unaligned_le32(set_mc->enabled, cur_byte);
1042 	cur_byte += 4;
1043 
1044 	put_unaligned_le32(set_mc->cnt, cur_byte);
1045 	cur_byte += 4;
1046 
1047 	if (set_mc->cnt > 0 && set_mc->mc_list)
1048 		memcpy(cur_byte, set_mc->mc_list, set_mc->cnt * ETH_ALEN);
1049 
1050 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1051 	if (result)
1052 		netdev_err(vif->ndev, "Failed to send setup multicast\n");
1053 
1054 error:
1055 	kfree(set_mc->mc_list);
1056 	kfree(wid.val);
1057 	kfree(msg);
1058 }
1059 
handle_scan_timer(struct work_struct * work)1060 static void handle_scan_timer(struct work_struct *work)
1061 {
1062 	struct host_if_msg *msg = container_of(work, struct host_if_msg, work);
1063 
1064 	handle_scan_done(msg->vif, SCAN_EVENT_ABORTED);
1065 	kfree(msg);
1066 }
1067 
handle_scan_complete(struct work_struct * work)1068 static void handle_scan_complete(struct work_struct *work)
1069 {
1070 	struct host_if_msg *msg = container_of(work, struct host_if_msg, work);
1071 
1072 	del_timer(&msg->vif->hif_drv->scan_timer);
1073 
1074 	handle_scan_done(msg->vif, SCAN_EVENT_DONE);
1075 
1076 	kfree(msg);
1077 }
1078 
timer_scan_cb(struct timer_list * t)1079 static void timer_scan_cb(struct timer_list *t)
1080 {
1081 	struct host_if_drv *hif_drv = from_timer(hif_drv, t, scan_timer);
1082 	struct wilc_vif *vif = hif_drv->scan_timer_vif;
1083 	struct host_if_msg *msg;
1084 	int result;
1085 
1086 	msg = wilc_alloc_work(vif, handle_scan_timer, false);
1087 	if (IS_ERR(msg))
1088 		return;
1089 
1090 	result = wilc_enqueue_work(msg);
1091 	if (result)
1092 		kfree(msg);
1093 }
1094 
timer_connect_cb(struct timer_list * t)1095 static void timer_connect_cb(struct timer_list *t)
1096 {
1097 	struct host_if_drv *hif_drv = from_timer(hif_drv, t,
1098 						      connect_timer);
1099 	struct wilc_vif *vif = hif_drv->connect_timer_vif;
1100 	struct host_if_msg *msg;
1101 	int result;
1102 
1103 	msg = wilc_alloc_work(vif, handle_connect_timeout, false);
1104 	if (IS_ERR(msg))
1105 		return;
1106 
1107 	result = wilc_enqueue_work(msg);
1108 	if (result)
1109 		kfree(msg);
1110 }
1111 
wilc_remove_wep_key(struct wilc_vif * vif,u8 index)1112 int wilc_remove_wep_key(struct wilc_vif *vif, u8 index)
1113 {
1114 	struct wid wid;
1115 	int result;
1116 
1117 	wid.id = WID_REMOVE_WEP_KEY;
1118 	wid.type = WID_STR;
1119 	wid.size = sizeof(char);
1120 	wid.val = &index;
1121 
1122 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1123 	if (result)
1124 		netdev_err(vif->ndev,
1125 			   "Failed to send remove wep key config packet\n");
1126 	return result;
1127 }
1128 
wilc_set_wep_default_keyid(struct wilc_vif * vif,u8 index)1129 int wilc_set_wep_default_keyid(struct wilc_vif *vif, u8 index)
1130 {
1131 	struct wid wid;
1132 	int result;
1133 
1134 	wid.id = WID_KEY_ID;
1135 	wid.type = WID_CHAR;
1136 	wid.size = sizeof(char);
1137 	wid.val = &index;
1138 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1139 	if (result)
1140 		netdev_err(vif->ndev,
1141 			   "Failed to send wep default key config packet\n");
1142 
1143 	return result;
1144 }
1145 
wilc_add_wep_key_bss_sta(struct wilc_vif * vif,const u8 * key,u8 len,u8 index)1146 int wilc_add_wep_key_bss_sta(struct wilc_vif *vif, const u8 *key, u8 len,
1147 			     u8 index)
1148 {
1149 	struct wid wid;
1150 	int result;
1151 	struct wilc_wep_key *wep_key;
1152 
1153 	wid.id = WID_ADD_WEP_KEY;
1154 	wid.type = WID_STR;
1155 	wid.size = sizeof(*wep_key) + len;
1156 	wep_key = kzalloc(wid.size, GFP_KERNEL);
1157 	if (!wep_key)
1158 		return -ENOMEM;
1159 
1160 	wid.val = (u8 *)wep_key;
1161 
1162 	wep_key->index = index;
1163 	wep_key->key_len = len;
1164 	memcpy(wep_key->key, key, len);
1165 
1166 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1167 	if (result)
1168 		netdev_err(vif->ndev,
1169 			   "Failed to add wep key config packet\n");
1170 
1171 	kfree(wep_key);
1172 	return result;
1173 }
1174 
wilc_add_wep_key_bss_ap(struct wilc_vif * vif,const u8 * key,u8 len,u8 index,u8 mode,enum authtype auth_type)1175 int wilc_add_wep_key_bss_ap(struct wilc_vif *vif, const u8 *key, u8 len,
1176 			    u8 index, u8 mode, enum authtype auth_type)
1177 {
1178 	struct wid wid_list[3];
1179 	int result;
1180 	struct wilc_wep_key *wep_key;
1181 
1182 	wid_list[0].id = WID_11I_MODE;
1183 	wid_list[0].type = WID_CHAR;
1184 	wid_list[0].size = sizeof(char);
1185 	wid_list[0].val = &mode;
1186 
1187 	wid_list[1].id = WID_AUTH_TYPE;
1188 	wid_list[1].type = WID_CHAR;
1189 	wid_list[1].size = sizeof(char);
1190 	wid_list[1].val = (s8 *)&auth_type;
1191 
1192 	wid_list[2].id = WID_WEP_KEY_VALUE;
1193 	wid_list[2].type = WID_STR;
1194 	wid_list[2].size = sizeof(*wep_key) + len;
1195 	wep_key = kzalloc(wid_list[2].size, GFP_KERNEL);
1196 	if (!wep_key)
1197 		return -ENOMEM;
1198 
1199 	wid_list[2].val = (u8 *)wep_key;
1200 
1201 	wep_key->index = index;
1202 	wep_key->key_len = len;
1203 	memcpy(wep_key->key, key, len);
1204 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, wid_list,
1205 				      ARRAY_SIZE(wid_list));
1206 	if (result)
1207 		netdev_err(vif->ndev,
1208 			   "Failed to add wep ap key config packet\n");
1209 
1210 	kfree(wep_key);
1211 	return result;
1212 }
1213 
wilc_add_ptk(struct wilc_vif * vif,const u8 * ptk,u8 ptk_key_len,const u8 * mac_addr,const u8 * rx_mic,const u8 * tx_mic,u8 mode,u8 cipher_mode,u8 index)1214 int wilc_add_ptk(struct wilc_vif *vif, const u8 *ptk, u8 ptk_key_len,
1215 		 const u8 *mac_addr, const u8 *rx_mic, const u8 *tx_mic,
1216 		 u8 mode, u8 cipher_mode, u8 index)
1217 {
1218 	int result = 0;
1219 	u8 t_key_len  = ptk_key_len + WILC_RX_MIC_KEY_LEN + WILC_TX_MIC_KEY_LEN;
1220 
1221 	if (mode == WILC_AP_MODE) {
1222 		struct wid wid_list[2];
1223 		struct wilc_ap_wpa_ptk *key_buf;
1224 
1225 		wid_list[0].id = WID_11I_MODE;
1226 		wid_list[0].type = WID_CHAR;
1227 		wid_list[0].size = sizeof(char);
1228 		wid_list[0].val = (s8 *)&cipher_mode;
1229 
1230 		key_buf = kzalloc(sizeof(*key_buf) + t_key_len, GFP_KERNEL);
1231 		if (!key_buf)
1232 			return -ENOMEM;
1233 
1234 		ether_addr_copy(key_buf->mac_addr, mac_addr);
1235 		key_buf->index = index;
1236 		key_buf->key_len = t_key_len;
1237 		memcpy(&key_buf->key[0], ptk, ptk_key_len);
1238 
1239 		if (rx_mic)
1240 			memcpy(&key_buf->key[ptk_key_len], rx_mic,
1241 			       WILC_RX_MIC_KEY_LEN);
1242 
1243 		if (tx_mic)
1244 			memcpy(&key_buf->key[ptk_key_len + WILC_RX_MIC_KEY_LEN],
1245 			       tx_mic, WILC_TX_MIC_KEY_LEN);
1246 
1247 		wid_list[1].id = WID_ADD_PTK;
1248 		wid_list[1].type = WID_STR;
1249 		wid_list[1].size = sizeof(*key_buf) + t_key_len;
1250 		wid_list[1].val = (u8 *)key_buf;
1251 		result = wilc_send_config_pkt(vif, WILC_SET_CFG, wid_list,
1252 					      ARRAY_SIZE(wid_list));
1253 		kfree(key_buf);
1254 	} else if (mode == WILC_STATION_MODE) {
1255 		struct wid wid;
1256 		struct wilc_sta_wpa_ptk *key_buf;
1257 
1258 		key_buf = kzalloc(sizeof(*key_buf) + t_key_len, GFP_KERNEL);
1259 		if (!key_buf)
1260 			return -ENOMEM;
1261 
1262 		ether_addr_copy(key_buf->mac_addr, mac_addr);
1263 		key_buf->key_len = t_key_len;
1264 		memcpy(&key_buf->key[0], ptk, ptk_key_len);
1265 
1266 		if (rx_mic)
1267 			memcpy(&key_buf->key[ptk_key_len], rx_mic,
1268 			       WILC_RX_MIC_KEY_LEN);
1269 
1270 		if (tx_mic)
1271 			memcpy(&key_buf->key[ptk_key_len + WILC_RX_MIC_KEY_LEN],
1272 			       tx_mic, WILC_TX_MIC_KEY_LEN);
1273 
1274 		wid.id = WID_ADD_PTK;
1275 		wid.type = WID_STR;
1276 		wid.size = sizeof(*key_buf) + t_key_len;
1277 		wid.val = (s8 *)key_buf;
1278 		result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1279 		kfree(key_buf);
1280 	}
1281 
1282 	return result;
1283 }
1284 
wilc_add_rx_gtk(struct wilc_vif * vif,const u8 * rx_gtk,u8 gtk_key_len,u8 index,u32 key_rsc_len,const u8 * key_rsc,const u8 * rx_mic,const u8 * tx_mic,u8 mode,u8 cipher_mode)1285 int wilc_add_rx_gtk(struct wilc_vif *vif, const u8 *rx_gtk, u8 gtk_key_len,
1286 		    u8 index, u32 key_rsc_len, const u8 *key_rsc,
1287 		    const u8 *rx_mic, const u8 *tx_mic, u8 mode,
1288 		    u8 cipher_mode)
1289 {
1290 	int result = 0;
1291 	struct wilc_gtk_key *gtk_key;
1292 	int t_key_len = gtk_key_len + WILC_RX_MIC_KEY_LEN + WILC_TX_MIC_KEY_LEN;
1293 
1294 	gtk_key = kzalloc(sizeof(*gtk_key) + t_key_len, GFP_KERNEL);
1295 	if (!gtk_key)
1296 		return -ENOMEM;
1297 
1298 	/* fill bssid value only in station mode */
1299 	if (mode == WILC_STATION_MODE &&
1300 	    vif->hif_drv->hif_state == HOST_IF_CONNECTED)
1301 		memcpy(gtk_key->mac_addr, vif->hif_drv->assoc_bssid, ETH_ALEN);
1302 
1303 	if (key_rsc)
1304 		memcpy(gtk_key->rsc, key_rsc, 8);
1305 	gtk_key->index = index;
1306 	gtk_key->key_len = t_key_len;
1307 	memcpy(&gtk_key->key[0], rx_gtk, gtk_key_len);
1308 
1309 	if (rx_mic)
1310 		memcpy(&gtk_key->key[gtk_key_len], rx_mic, WILC_RX_MIC_KEY_LEN);
1311 
1312 	if (tx_mic)
1313 		memcpy(&gtk_key->key[gtk_key_len + WILC_RX_MIC_KEY_LEN],
1314 		       tx_mic, WILC_TX_MIC_KEY_LEN);
1315 
1316 	if (mode == WILC_AP_MODE) {
1317 		struct wid wid_list[2];
1318 
1319 		wid_list[0].id = WID_11I_MODE;
1320 		wid_list[0].type = WID_CHAR;
1321 		wid_list[0].size = sizeof(char);
1322 		wid_list[0].val = (s8 *)&cipher_mode;
1323 
1324 		wid_list[1].id = WID_ADD_RX_GTK;
1325 		wid_list[1].type = WID_STR;
1326 		wid_list[1].size = sizeof(*gtk_key) + t_key_len;
1327 		wid_list[1].val = (u8 *)gtk_key;
1328 
1329 		result = wilc_send_config_pkt(vif, WILC_SET_CFG, wid_list,
1330 					      ARRAY_SIZE(wid_list));
1331 	} else if (mode == WILC_STATION_MODE) {
1332 		struct wid wid;
1333 
1334 		wid.id = WID_ADD_RX_GTK;
1335 		wid.type = WID_STR;
1336 		wid.size = sizeof(*gtk_key) + t_key_len;
1337 		wid.val = (u8 *)gtk_key;
1338 		result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1339 	}
1340 
1341 	kfree(gtk_key);
1342 	return result;
1343 }
1344 
wilc_set_pmkid_info(struct wilc_vif * vif,struct wilc_pmkid_attr * pmkid)1345 int wilc_set_pmkid_info(struct wilc_vif *vif, struct wilc_pmkid_attr *pmkid)
1346 {
1347 	struct wid wid;
1348 
1349 	wid.id = WID_PMKID_INFO;
1350 	wid.type = WID_STR;
1351 	wid.size = (pmkid->numpmkid * sizeof(struct wilc_pmkid)) + 1;
1352 	wid.val = (u8 *)pmkid;
1353 
1354 	return wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1355 }
1356 
wilc_get_mac_address(struct wilc_vif * vif,u8 * mac_addr)1357 int wilc_get_mac_address(struct wilc_vif *vif, u8 *mac_addr)
1358 {
1359 	int result;
1360 	struct wid wid;
1361 
1362 	wid.id = WID_MAC_ADDR;
1363 	wid.type = WID_STR;
1364 	wid.size = ETH_ALEN;
1365 	wid.val = mac_addr;
1366 
1367 	result = wilc_send_config_pkt(vif, WILC_GET_CFG, &wid, 1);
1368 	if (result)
1369 		netdev_err(vif->ndev, "Failed to get mac address\n");
1370 
1371 	return result;
1372 }
1373 
wilc_set_join_req(struct wilc_vif * vif,u8 * bssid,const u8 * ies,size_t ies_len)1374 int wilc_set_join_req(struct wilc_vif *vif, u8 *bssid, const u8 *ies,
1375 		      size_t ies_len)
1376 {
1377 	int result;
1378 	struct host_if_drv *hif_drv = vif->hif_drv;
1379 	struct wilc_conn_info *conn_info = &hif_drv->conn_info;
1380 
1381 	if (bssid)
1382 		ether_addr_copy(conn_info->bssid, bssid);
1383 
1384 	if (ies) {
1385 		conn_info->req_ies_len = ies_len;
1386 		conn_info->req_ies = kmemdup(ies, ies_len, GFP_KERNEL);
1387 		if (!conn_info->req_ies)
1388 			return -ENOMEM;
1389 	}
1390 
1391 	result = wilc_send_connect_wid(vif);
1392 	if (result)
1393 		goto free_ies;
1394 
1395 	hif_drv->connect_timer_vif = vif;
1396 	mod_timer(&hif_drv->connect_timer,
1397 		  jiffies + msecs_to_jiffies(WILC_HIF_CONNECT_TIMEOUT_MS));
1398 
1399 	return 0;
1400 
1401 free_ies:
1402 	kfree(conn_info->req_ies);
1403 
1404 	return result;
1405 }
1406 
wilc_set_mac_chnl_num(struct wilc_vif * vif,u8 channel)1407 int wilc_set_mac_chnl_num(struct wilc_vif *vif, u8 channel)
1408 {
1409 	struct wid wid;
1410 	int result;
1411 
1412 	wid.id = WID_CURRENT_CHANNEL;
1413 	wid.type = WID_CHAR;
1414 	wid.size = sizeof(char);
1415 	wid.val = &channel;
1416 
1417 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1418 	if (result)
1419 		netdev_err(vif->ndev, "Failed to set channel\n");
1420 
1421 	return result;
1422 }
1423 
wilc_set_operation_mode(struct wilc_vif * vif,int index,u8 mode,u8 ifc_id)1424 int wilc_set_operation_mode(struct wilc_vif *vif, int index, u8 mode,
1425 			    u8 ifc_id)
1426 {
1427 	struct wid wid;
1428 	int result;
1429 	struct wilc_drv_handler drv;
1430 
1431 	wid.id = WID_SET_OPERATION_MODE;
1432 	wid.type = WID_STR;
1433 	wid.size = sizeof(drv);
1434 	wid.val = (u8 *)&drv;
1435 
1436 	drv.handler = cpu_to_le32(index);
1437 	drv.mode = (ifc_id | (mode << 1));
1438 
1439 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1440 	if (result)
1441 		netdev_err(vif->ndev, "Failed to set driver handler\n");
1442 
1443 	return result;
1444 }
1445 
wilc_get_inactive_time(struct wilc_vif * vif,const u8 * mac,u32 * out_val)1446 s32 wilc_get_inactive_time(struct wilc_vif *vif, const u8 *mac, u32 *out_val)
1447 {
1448 	struct wid wid;
1449 	s32 result;
1450 
1451 	wid.id = WID_SET_STA_MAC_INACTIVE_TIME;
1452 	wid.type = WID_STR;
1453 	wid.size = ETH_ALEN;
1454 	wid.val = kzalloc(wid.size, GFP_KERNEL);
1455 	if (!wid.val)
1456 		return -ENOMEM;
1457 
1458 	ether_addr_copy(wid.val, mac);
1459 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1460 	kfree(wid.val);
1461 	if (result) {
1462 		netdev_err(vif->ndev, "Failed to set inactive mac\n");
1463 		return result;
1464 	}
1465 
1466 	wid.id = WID_GET_INACTIVE_TIME;
1467 	wid.type = WID_INT;
1468 	wid.val = (s8 *)out_val;
1469 	wid.size = sizeof(u32);
1470 	result = wilc_send_config_pkt(vif, WILC_GET_CFG, &wid, 1);
1471 	if (result)
1472 		netdev_err(vif->ndev, "Failed to get inactive time\n");
1473 
1474 	return result;
1475 }
1476 
wilc_get_rssi(struct wilc_vif * vif,s8 * rssi_level)1477 int wilc_get_rssi(struct wilc_vif *vif, s8 *rssi_level)
1478 {
1479 	struct wid wid;
1480 	int result;
1481 
1482 	if (!rssi_level) {
1483 		netdev_err(vif->ndev, "%s: RSSI level is NULL\n", __func__);
1484 		return -EFAULT;
1485 	}
1486 
1487 	wid.id = WID_RSSI;
1488 	wid.type = WID_CHAR;
1489 	wid.size = sizeof(char);
1490 	wid.val = rssi_level;
1491 	result = wilc_send_config_pkt(vif, WILC_GET_CFG, &wid, 1);
1492 	if (result)
1493 		netdev_err(vif->ndev, "Failed to get RSSI value\n");
1494 
1495 	return result;
1496 }
1497 
wilc_get_stats_async(struct wilc_vif * vif,struct rf_info * stats)1498 static int wilc_get_stats_async(struct wilc_vif *vif, struct rf_info *stats)
1499 {
1500 	int result;
1501 	struct host_if_msg *msg;
1502 
1503 	msg = wilc_alloc_work(vif, handle_get_statistics, false);
1504 	if (IS_ERR(msg))
1505 		return PTR_ERR(msg);
1506 
1507 	msg->body.data = (char *)stats;
1508 
1509 	result = wilc_enqueue_work(msg);
1510 	if (result) {
1511 		netdev_err(vif->ndev, "%s: enqueue work failed\n", __func__);
1512 		kfree(msg);
1513 		return result;
1514 	}
1515 
1516 	return result;
1517 }
1518 
wilc_hif_set_cfg(struct wilc_vif * vif,struct cfg_param_attr * param)1519 int wilc_hif_set_cfg(struct wilc_vif *vif, struct cfg_param_attr *param)
1520 {
1521 	struct wid wid_list[4];
1522 	int i = 0;
1523 
1524 	if (param->flag & WILC_CFG_PARAM_RETRY_SHORT) {
1525 		wid_list[i].id = WID_SHORT_RETRY_LIMIT;
1526 		wid_list[i].val = (s8 *)&param->short_retry_limit;
1527 		wid_list[i].type = WID_SHORT;
1528 		wid_list[i].size = sizeof(u16);
1529 		i++;
1530 	}
1531 	if (param->flag & WILC_CFG_PARAM_RETRY_LONG) {
1532 		wid_list[i].id = WID_LONG_RETRY_LIMIT;
1533 		wid_list[i].val = (s8 *)&param->long_retry_limit;
1534 		wid_list[i].type = WID_SHORT;
1535 		wid_list[i].size = sizeof(u16);
1536 		i++;
1537 	}
1538 	if (param->flag & WILC_CFG_PARAM_FRAG_THRESHOLD) {
1539 		wid_list[i].id = WID_FRAG_THRESHOLD;
1540 		wid_list[i].val = (s8 *)&param->frag_threshold;
1541 		wid_list[i].type = WID_SHORT;
1542 		wid_list[i].size = sizeof(u16);
1543 		i++;
1544 	}
1545 	if (param->flag & WILC_CFG_PARAM_RTS_THRESHOLD) {
1546 		wid_list[i].id = WID_RTS_THRESHOLD;
1547 		wid_list[i].val = (s8 *)&param->rts_threshold;
1548 		wid_list[i].type = WID_SHORT;
1549 		wid_list[i].size = sizeof(u16);
1550 		i++;
1551 	}
1552 
1553 	return wilc_send_config_pkt(vif, WILC_SET_CFG, wid_list, i);
1554 }
1555 
get_periodic_rssi(struct timer_list * t)1556 static void get_periodic_rssi(struct timer_list *t)
1557 {
1558 	struct wilc_vif *vif = from_timer(vif, t, periodic_rssi);
1559 
1560 	if (!vif->hif_drv) {
1561 		netdev_err(vif->ndev, "%s: hif driver is NULL", __func__);
1562 		return;
1563 	}
1564 
1565 	if (vif->hif_drv->hif_state == HOST_IF_CONNECTED)
1566 		wilc_get_stats_async(vif, &vif->periodic_stat);
1567 
1568 	mod_timer(&vif->periodic_rssi, jiffies + msecs_to_jiffies(5000));
1569 }
1570 
wilc_init(struct net_device * dev,struct host_if_drv ** hif_drv_handler)1571 int wilc_init(struct net_device *dev, struct host_if_drv **hif_drv_handler)
1572 {
1573 	struct host_if_drv *hif_drv;
1574 	struct wilc_vif *vif = netdev_priv(dev);
1575 	struct wilc *wilc = vif->wilc;
1576 
1577 	hif_drv  = kzalloc(sizeof(*hif_drv), GFP_KERNEL);
1578 	if (!hif_drv)
1579 		return -ENOMEM;
1580 
1581 	*hif_drv_handler = hif_drv;
1582 
1583 	vif->hif_drv = hif_drv;
1584 
1585 	if (wilc->clients_count == 0)
1586 		mutex_init(&wilc->deinit_lock);
1587 
1588 	timer_setup(&vif->periodic_rssi, get_periodic_rssi, 0);
1589 	mod_timer(&vif->periodic_rssi, jiffies + msecs_to_jiffies(5000));
1590 
1591 	timer_setup(&hif_drv->scan_timer, timer_scan_cb, 0);
1592 	timer_setup(&hif_drv->connect_timer, timer_connect_cb, 0);
1593 	timer_setup(&hif_drv->remain_on_ch_timer, listen_timer_cb, 0);
1594 
1595 	hif_drv->hif_state = HOST_IF_IDLE;
1596 
1597 	hif_drv->p2p_timeout = 0;
1598 
1599 	wilc->clients_count++;
1600 
1601 	return 0;
1602 }
1603 
wilc_deinit(struct wilc_vif * vif)1604 int wilc_deinit(struct wilc_vif *vif)
1605 {
1606 	int result = 0;
1607 	struct host_if_drv *hif_drv = vif->hif_drv;
1608 
1609 	if (!hif_drv) {
1610 		netdev_err(vif->ndev, "%s: hif driver is NULL", __func__);
1611 		return -EFAULT;
1612 	}
1613 
1614 	mutex_lock(&vif->wilc->deinit_lock);
1615 
1616 	del_timer_sync(&hif_drv->scan_timer);
1617 	del_timer_sync(&hif_drv->connect_timer);
1618 	del_timer_sync(&vif->periodic_rssi);
1619 	del_timer_sync(&hif_drv->remain_on_ch_timer);
1620 
1621 	if (hif_drv->usr_scan_req.scan_result) {
1622 		hif_drv->usr_scan_req.scan_result(SCAN_EVENT_ABORTED, NULL,
1623 						  hif_drv->usr_scan_req.arg);
1624 		hif_drv->usr_scan_req.scan_result = NULL;
1625 	}
1626 
1627 	hif_drv->hif_state = HOST_IF_IDLE;
1628 
1629 	kfree(hif_drv);
1630 	vif->hif_drv = NULL;
1631 	vif->wilc->clients_count--;
1632 	mutex_unlock(&vif->wilc->deinit_lock);
1633 	return result;
1634 }
1635 
wilc_network_info_received(struct wilc * wilc,u8 * buffer,u32 length)1636 void wilc_network_info_received(struct wilc *wilc, u8 *buffer, u32 length)
1637 {
1638 	int result;
1639 	struct host_if_msg *msg;
1640 	int id;
1641 	struct host_if_drv *hif_drv;
1642 	struct wilc_vif *vif;
1643 
1644 	id = get_unaligned_le32(&buffer[length - 4]);
1645 	vif = wilc_get_vif_from_idx(wilc, id);
1646 	if (!vif)
1647 		return;
1648 	hif_drv = vif->hif_drv;
1649 
1650 	if (!hif_drv) {
1651 		netdev_err(vif->ndev, "driver not init[%p]\n", hif_drv);
1652 		return;
1653 	}
1654 
1655 	msg = wilc_alloc_work(vif, handle_rcvd_ntwrk_info, false);
1656 	if (IS_ERR(msg))
1657 		return;
1658 
1659 	msg->body.net_info.frame_len = get_unaligned_le16(&buffer[6]) - 1;
1660 	msg->body.net_info.rssi = buffer[8];
1661 	msg->body.net_info.mgmt = kmemdup(&buffer[9],
1662 					  msg->body.net_info.frame_len,
1663 					  GFP_KERNEL);
1664 	if (!msg->body.net_info.mgmt) {
1665 		kfree(msg);
1666 		return;
1667 	}
1668 
1669 	result = wilc_enqueue_work(msg);
1670 	if (result) {
1671 		netdev_err(vif->ndev, "%s: enqueue work failed\n", __func__);
1672 		kfree(msg->body.net_info.mgmt);
1673 		kfree(msg);
1674 	}
1675 }
1676 
wilc_gnrl_async_info_received(struct wilc * wilc,u8 * buffer,u32 length)1677 void wilc_gnrl_async_info_received(struct wilc *wilc, u8 *buffer, u32 length)
1678 {
1679 	int result;
1680 	struct host_if_msg *msg;
1681 	int id;
1682 	struct host_if_drv *hif_drv;
1683 	struct wilc_vif *vif;
1684 
1685 	mutex_lock(&wilc->deinit_lock);
1686 
1687 	id = get_unaligned_le32(&buffer[length - 4]);
1688 	vif = wilc_get_vif_from_idx(wilc, id);
1689 	if (!vif) {
1690 		mutex_unlock(&wilc->deinit_lock);
1691 		return;
1692 	}
1693 
1694 	hif_drv = vif->hif_drv;
1695 
1696 	if (!hif_drv) {
1697 		mutex_unlock(&wilc->deinit_lock);
1698 		return;
1699 	}
1700 
1701 	if (!hif_drv->conn_info.conn_result) {
1702 		netdev_err(vif->ndev, "%s: conn_result is NULL\n", __func__);
1703 		mutex_unlock(&wilc->deinit_lock);
1704 		return;
1705 	}
1706 
1707 	msg = wilc_alloc_work(vif, handle_rcvd_gnrl_async_info, false);
1708 	if (IS_ERR(msg)) {
1709 		mutex_unlock(&wilc->deinit_lock);
1710 		return;
1711 	}
1712 
1713 	msg->body.mac_info.status = buffer[7];
1714 	result = wilc_enqueue_work(msg);
1715 	if (result) {
1716 		netdev_err(vif->ndev, "%s: enqueue work failed\n", __func__);
1717 		kfree(msg);
1718 	}
1719 
1720 	mutex_unlock(&wilc->deinit_lock);
1721 }
1722 
wilc_scan_complete_received(struct wilc * wilc,u8 * buffer,u32 length)1723 void wilc_scan_complete_received(struct wilc *wilc, u8 *buffer, u32 length)
1724 {
1725 	int result;
1726 	int id;
1727 	struct host_if_drv *hif_drv;
1728 	struct wilc_vif *vif;
1729 
1730 	id = get_unaligned_le32(&buffer[length - 4]);
1731 	vif = wilc_get_vif_from_idx(wilc, id);
1732 	if (!vif)
1733 		return;
1734 	hif_drv = vif->hif_drv;
1735 
1736 	if (!hif_drv)
1737 		return;
1738 
1739 	if (hif_drv->usr_scan_req.scan_result) {
1740 		struct host_if_msg *msg;
1741 
1742 		msg = wilc_alloc_work(vif, handle_scan_complete, false);
1743 		if (IS_ERR(msg))
1744 			return;
1745 
1746 		result = wilc_enqueue_work(msg);
1747 		if (result) {
1748 			netdev_err(vif->ndev, "%s: enqueue work failed\n",
1749 				   __func__);
1750 			kfree(msg);
1751 		}
1752 	}
1753 }
1754 
wilc_remain_on_channel(struct wilc_vif * vif,u64 cookie,u32 duration,u16 chan,void (* expired)(void *,u64),void * user_arg)1755 int wilc_remain_on_channel(struct wilc_vif *vif, u64 cookie,
1756 			   u32 duration, u16 chan,
1757 			   void (*expired)(void *, u64),
1758 			   void *user_arg)
1759 {
1760 	struct wilc_remain_ch roc;
1761 	int result;
1762 
1763 	roc.ch = chan;
1764 	roc.expired = expired;
1765 	roc.arg = user_arg;
1766 	roc.duration = duration;
1767 	roc.cookie = cookie;
1768 	result = handle_remain_on_chan(vif, &roc);
1769 	if (result)
1770 		netdev_err(vif->ndev, "%s: failed to set remain on channel\n",
1771 			   __func__);
1772 
1773 	return result;
1774 }
1775 
wilc_listen_state_expired(struct wilc_vif * vif,u64 cookie)1776 int wilc_listen_state_expired(struct wilc_vif *vif, u64 cookie)
1777 {
1778 	if (!vif->hif_drv) {
1779 		netdev_err(vif->ndev, "%s: hif driver is NULL", __func__);
1780 		return -EFAULT;
1781 	}
1782 
1783 	del_timer(&vif->hif_drv->remain_on_ch_timer);
1784 
1785 	return wilc_handle_roc_expired(vif, cookie);
1786 }
1787 
wilc_frame_register(struct wilc_vif * vif,u16 frame_type,bool reg)1788 void wilc_frame_register(struct wilc_vif *vif, u16 frame_type, bool reg)
1789 {
1790 	struct wid wid;
1791 	int result;
1792 	struct wilc_reg_frame reg_frame;
1793 
1794 	wid.id = WID_REGISTER_FRAME;
1795 	wid.type = WID_STR;
1796 	wid.size = sizeof(reg_frame);
1797 	wid.val = (u8 *)&reg_frame;
1798 
1799 	memset(&reg_frame, 0x0, sizeof(reg_frame));
1800 	reg_frame.reg = reg;
1801 
1802 	switch (frame_type) {
1803 	case IEEE80211_STYPE_ACTION:
1804 		reg_frame.reg_id = WILC_FW_ACTION_FRM_IDX;
1805 		break;
1806 
1807 	case IEEE80211_STYPE_PROBE_REQ:
1808 		reg_frame.reg_id = WILC_FW_PROBE_REQ_IDX;
1809 		break;
1810 
1811 	default:
1812 		break;
1813 	}
1814 	reg_frame.frame_type = cpu_to_le16(frame_type);
1815 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1816 	if (result)
1817 		netdev_err(vif->ndev, "Failed to frame register\n");
1818 }
1819 
wilc_add_beacon(struct wilc_vif * vif,u32 interval,u32 dtim_period,struct cfg80211_beacon_data * params)1820 int wilc_add_beacon(struct wilc_vif *vif, u32 interval, u32 dtim_period,
1821 		    struct cfg80211_beacon_data *params)
1822 {
1823 	struct wid wid;
1824 	int result;
1825 	u8 *cur_byte;
1826 
1827 	wid.id = WID_ADD_BEACON;
1828 	wid.type = WID_BIN;
1829 	wid.size = params->head_len + params->tail_len + 16;
1830 	wid.val = kzalloc(wid.size, GFP_KERNEL);
1831 	if (!wid.val)
1832 		return -ENOMEM;
1833 
1834 	cur_byte = wid.val;
1835 	put_unaligned_le32(interval, cur_byte);
1836 	cur_byte += 4;
1837 	put_unaligned_le32(dtim_period, cur_byte);
1838 	cur_byte += 4;
1839 	put_unaligned_le32(params->head_len, cur_byte);
1840 	cur_byte += 4;
1841 
1842 	if (params->head_len > 0)
1843 		memcpy(cur_byte, params->head, params->head_len);
1844 	cur_byte += params->head_len;
1845 
1846 	put_unaligned_le32(params->tail_len, cur_byte);
1847 	cur_byte += 4;
1848 
1849 	if (params->tail_len > 0)
1850 		memcpy(cur_byte, params->tail, params->tail_len);
1851 
1852 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1853 	if (result)
1854 		netdev_err(vif->ndev, "Failed to send add beacon\n");
1855 
1856 	kfree(wid.val);
1857 
1858 	return result;
1859 }
1860 
wilc_del_beacon(struct wilc_vif * vif)1861 int wilc_del_beacon(struct wilc_vif *vif)
1862 {
1863 	int result;
1864 	struct wid wid;
1865 	u8 del_beacon = 0;
1866 
1867 	wid.id = WID_DEL_BEACON;
1868 	wid.type = WID_CHAR;
1869 	wid.size = sizeof(char);
1870 	wid.val = &del_beacon;
1871 
1872 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1873 	if (result)
1874 		netdev_err(vif->ndev, "Failed to send delete beacon\n");
1875 
1876 	return result;
1877 }
1878 
wilc_add_station(struct wilc_vif * vif,const u8 * mac,struct station_parameters * params)1879 int wilc_add_station(struct wilc_vif *vif, const u8 *mac,
1880 		     struct station_parameters *params)
1881 {
1882 	struct wid wid;
1883 	int result;
1884 	u8 *cur_byte;
1885 
1886 	wid.id = WID_ADD_STA;
1887 	wid.type = WID_BIN;
1888 	wid.size = WILC_ADD_STA_LENGTH + params->supported_rates_len;
1889 	wid.val = kmalloc(wid.size, GFP_KERNEL);
1890 	if (!wid.val)
1891 		return -ENOMEM;
1892 
1893 	cur_byte = wid.val;
1894 	wilc_hif_pack_sta_param(cur_byte, mac, params);
1895 
1896 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1897 	if (result != 0)
1898 		netdev_err(vif->ndev, "Failed to send add station\n");
1899 
1900 	kfree(wid.val);
1901 
1902 	return result;
1903 }
1904 
wilc_del_station(struct wilc_vif * vif,const u8 * mac_addr)1905 int wilc_del_station(struct wilc_vif *vif, const u8 *mac_addr)
1906 {
1907 	struct wid wid;
1908 	int result;
1909 
1910 	wid.id = WID_REMOVE_STA;
1911 	wid.type = WID_BIN;
1912 	wid.size = ETH_ALEN;
1913 	wid.val = kzalloc(wid.size, GFP_KERNEL);
1914 	if (!wid.val)
1915 		return -ENOMEM;
1916 
1917 	if (!mac_addr)
1918 		eth_broadcast_addr(wid.val);
1919 	else
1920 		ether_addr_copy(wid.val, mac_addr);
1921 
1922 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1923 	if (result)
1924 		netdev_err(vif->ndev, "Failed to del station\n");
1925 
1926 	kfree(wid.val);
1927 
1928 	return result;
1929 }
1930 
wilc_del_allstation(struct wilc_vif * vif,u8 mac_addr[][ETH_ALEN])1931 int wilc_del_allstation(struct wilc_vif *vif, u8 mac_addr[][ETH_ALEN])
1932 {
1933 	struct wid wid;
1934 	int result;
1935 	int i;
1936 	u8 assoc_sta = 0;
1937 	struct wilc_del_all_sta del_sta;
1938 
1939 	memset(&del_sta, 0x0, sizeof(del_sta));
1940 	for (i = 0; i < WILC_MAX_NUM_STA; i++) {
1941 		if (!is_zero_ether_addr(mac_addr[i])) {
1942 			assoc_sta++;
1943 			ether_addr_copy(del_sta.mac[i], mac_addr[i]);
1944 		}
1945 	}
1946 
1947 	if (!assoc_sta)
1948 		return 0;
1949 
1950 	del_sta.assoc_sta = assoc_sta;
1951 
1952 	wid.id = WID_DEL_ALL_STA;
1953 	wid.type = WID_STR;
1954 	wid.size = (assoc_sta * ETH_ALEN) + 1;
1955 	wid.val = (u8 *)&del_sta;
1956 
1957 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1958 	if (result)
1959 		netdev_err(vif->ndev, "Failed to send delete all station\n");
1960 
1961 	return result;
1962 }
1963 
wilc_edit_station(struct wilc_vif * vif,const u8 * mac,struct station_parameters * params)1964 int wilc_edit_station(struct wilc_vif *vif, const u8 *mac,
1965 		      struct station_parameters *params)
1966 {
1967 	struct wid wid;
1968 	int result;
1969 	u8 *cur_byte;
1970 
1971 	wid.id = WID_EDIT_STA;
1972 	wid.type = WID_BIN;
1973 	wid.size = WILC_ADD_STA_LENGTH + params->supported_rates_len;
1974 	wid.val = kmalloc(wid.size, GFP_KERNEL);
1975 	if (!wid.val)
1976 		return -ENOMEM;
1977 
1978 	cur_byte = wid.val;
1979 	wilc_hif_pack_sta_param(cur_byte, mac, params);
1980 
1981 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
1982 	if (result)
1983 		netdev_err(vif->ndev, "Failed to send edit station\n");
1984 
1985 	kfree(wid.val);
1986 	return result;
1987 }
1988 
wilc_set_power_mgmt(struct wilc_vif * vif,bool enabled,u32 timeout)1989 int wilc_set_power_mgmt(struct wilc_vif *vif, bool enabled, u32 timeout)
1990 {
1991 	struct wid wid;
1992 	int result;
1993 	s8 power_mode;
1994 
1995 	if (enabled)
1996 		power_mode = WILC_FW_MIN_FAST_PS;
1997 	else
1998 		power_mode = WILC_FW_NO_POWERSAVE;
1999 
2000 	wid.id = WID_POWER_MANAGEMENT;
2001 	wid.val = &power_mode;
2002 	wid.size = sizeof(char);
2003 	result = wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
2004 	if (result)
2005 		netdev_err(vif->ndev, "Failed to send power management\n");
2006 
2007 	return result;
2008 }
2009 
wilc_setup_multicast_filter(struct wilc_vif * vif,u32 enabled,u32 count,u8 * mc_list)2010 int wilc_setup_multicast_filter(struct wilc_vif *vif, u32 enabled, u32 count,
2011 				u8 *mc_list)
2012 {
2013 	int result;
2014 	struct host_if_msg *msg;
2015 
2016 	msg = wilc_alloc_work(vif, handle_set_mcast_filter, false);
2017 	if (IS_ERR(msg))
2018 		return PTR_ERR(msg);
2019 
2020 	msg->body.mc_info.enabled = enabled;
2021 	msg->body.mc_info.cnt = count;
2022 	msg->body.mc_info.mc_list = mc_list;
2023 
2024 	result = wilc_enqueue_work(msg);
2025 	if (result) {
2026 		netdev_err(vif->ndev, "%s: enqueue work failed\n", __func__);
2027 		kfree(msg);
2028 	}
2029 	return result;
2030 }
2031 
wilc_set_tx_power(struct wilc_vif * vif,u8 tx_power)2032 int wilc_set_tx_power(struct wilc_vif *vif, u8 tx_power)
2033 {
2034 	struct wid wid;
2035 
2036 	wid.id = WID_TX_POWER;
2037 	wid.type = WID_CHAR;
2038 	wid.val = &tx_power;
2039 	wid.size = sizeof(char);
2040 
2041 	return wilc_send_config_pkt(vif, WILC_SET_CFG, &wid, 1);
2042 }
2043 
wilc_get_tx_power(struct wilc_vif * vif,u8 * tx_power)2044 int wilc_get_tx_power(struct wilc_vif *vif, u8 *tx_power)
2045 {
2046 	struct wid wid;
2047 
2048 	wid.id = WID_TX_POWER;
2049 	wid.type = WID_CHAR;
2050 	wid.val = tx_power;
2051 	wid.size = sizeof(char);
2052 
2053 	return wilc_send_config_pkt(vif, WILC_GET_CFG, &wid, 1);
2054 }
2055