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