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 <linux/bitfield.h>
8 #include "wlan_if.h"
9 #include "wlan.h"
10 #include "wlan_cfg.h"
11 #include "netdev.h"
12 
13 enum cfg_cmd_type {
14 	CFG_BYTE_CMD	= 0,
15 	CFG_HWORD_CMD	= 1,
16 	CFG_WORD_CMD	= 2,
17 	CFG_STR_CMD	= 3,
18 	CFG_BIN_CMD	= 4
19 };
20 
21 static const struct wilc_cfg_byte g_cfg_byte[] = {
22 	{WID_STATUS, 0},
23 	{WID_RSSI, 0},
24 	{WID_LINKSPEED, 0},
25 	{WID_TX_POWER, 0},
26 	{WID_WOWLAN_TRIGGER, 0},
27 	{WID_NIL, 0}
28 };
29 
30 static const struct wilc_cfg_hword g_cfg_hword[] = {
31 	{WID_NIL, 0}
32 };
33 
34 static const struct wilc_cfg_word g_cfg_word[] = {
35 	{WID_FAILED_COUNT, 0},
36 	{WID_RECEIVED_FRAGMENT_COUNT, 0},
37 	{WID_SUCCESS_FRAME_COUNT, 0},
38 	{WID_GET_INACTIVE_TIME, 0},
39 	{WID_NIL, 0}
40 
41 };
42 
43 static const struct wilc_cfg_str g_cfg_str[] = {
44 	{WID_FIRMWARE_VERSION, NULL},
45 	{WID_MAC_ADDR, NULL},
46 	{WID_ASSOC_RES_INFO, NULL},
47 	{WID_NIL, NULL}
48 };
49 
50 #define WILC_RESP_MSG_TYPE_CONFIG_REPLY		'R'
51 #define WILC_RESP_MSG_TYPE_STATUS_INFO		'I'
52 #define WILC_RESP_MSG_TYPE_NETWORK_INFO		'N'
53 #define WILC_RESP_MSG_TYPE_SCAN_COMPLETE	'S'
54 
55 /********************************************
56  *
57  *      Configuration Functions
58  *
59  ********************************************/
60 
wilc_wlan_cfg_set_byte(u8 * frame,u32 offset,u16 id,u8 val8)61 static int wilc_wlan_cfg_set_byte(u8 *frame, u32 offset, u16 id, u8 val8)
62 {
63 	if ((offset + 4) >= WILC_MAX_CFG_FRAME_SIZE)
64 		return 0;
65 
66 	put_unaligned_le16(id, &frame[offset]);
67 	put_unaligned_le16(1, &frame[offset + 2]);
68 	frame[offset + 4] = val8;
69 	return 5;
70 }
71 
wilc_wlan_cfg_set_hword(u8 * frame,u32 offset,u16 id,u16 val16)72 static int wilc_wlan_cfg_set_hword(u8 *frame, u32 offset, u16 id, u16 val16)
73 {
74 	if ((offset + 5) >= WILC_MAX_CFG_FRAME_SIZE)
75 		return 0;
76 
77 	put_unaligned_le16(id, &frame[offset]);
78 	put_unaligned_le16(2, &frame[offset + 2]);
79 	put_unaligned_le16(val16, &frame[offset + 4]);
80 
81 	return 6;
82 }
83 
wilc_wlan_cfg_set_word(u8 * frame,u32 offset,u16 id,u32 val32)84 static int wilc_wlan_cfg_set_word(u8 *frame, u32 offset, u16 id, u32 val32)
85 {
86 	if ((offset + 7) >= WILC_MAX_CFG_FRAME_SIZE)
87 		return 0;
88 
89 	put_unaligned_le16(id, &frame[offset]);
90 	put_unaligned_le16(4, &frame[offset + 2]);
91 	put_unaligned_le32(val32, &frame[offset + 4]);
92 
93 	return 8;
94 }
95 
wilc_wlan_cfg_set_str(u8 * frame,u32 offset,u16 id,u8 * str,u32 size)96 static int wilc_wlan_cfg_set_str(u8 *frame, u32 offset, u16 id, u8 *str,
97 				 u32 size)
98 {
99 	if ((offset + size + 4) >= WILC_MAX_CFG_FRAME_SIZE)
100 		return 0;
101 
102 	put_unaligned_le16(id, &frame[offset]);
103 	put_unaligned_le16(size, &frame[offset + 2]);
104 	if (str && size != 0)
105 		memcpy(&frame[offset + 4], str, size);
106 
107 	return (size + 4);
108 }
109 
wilc_wlan_cfg_set_bin(u8 * frame,u32 offset,u16 id,u8 * b,u32 size)110 static int wilc_wlan_cfg_set_bin(u8 *frame, u32 offset, u16 id, u8 *b, u32 size)
111 {
112 	u32 i;
113 	u8 checksum = 0;
114 
115 	if ((offset + size + 5) >= WILC_MAX_CFG_FRAME_SIZE)
116 		return 0;
117 
118 	put_unaligned_le16(id, &frame[offset]);
119 	put_unaligned_le16(size, &frame[offset + 2]);
120 
121 	if ((b) && size != 0) {
122 		memcpy(&frame[offset + 4], b, size);
123 		for (i = 0; i < size; i++)
124 			checksum += frame[offset + i + 4];
125 	}
126 
127 	frame[offset + size + 4] = checksum;
128 
129 	return (size + 5);
130 }
131 
132 /********************************************
133  *
134  *      Configuration Response Functions
135  *
136  ********************************************/
137 
wilc_wlan_parse_response_frame(struct wilc * wl,u8 * info,int size)138 static void wilc_wlan_parse_response_frame(struct wilc *wl, u8 *info, int size)
139 {
140 	u16 wid;
141 	u32 len = 0, i = 0;
142 	struct wilc_cfg *cfg = &wl->cfg;
143 
144 	while (size > 0) {
145 		i = 0;
146 		wid = get_unaligned_le16(info);
147 
148 		switch (FIELD_GET(WILC_WID_TYPE, wid)) {
149 		case WID_CHAR:
150 			while (cfg->b[i].id != WID_NIL && cfg->b[i].id != wid)
151 				i++;
152 
153 			if (cfg->b[i].id == wid)
154 				cfg->b[i].val = info[4];
155 
156 			len = 3;
157 			break;
158 
159 		case WID_SHORT:
160 			while (cfg->hw[i].id != WID_NIL && cfg->hw[i].id != wid)
161 				i++;
162 
163 			if (cfg->hw[i].id == wid)
164 				cfg->hw[i].val = get_unaligned_le16(&info[4]);
165 
166 			len = 4;
167 			break;
168 
169 		case WID_INT:
170 			while (cfg->w[i].id != WID_NIL && cfg->w[i].id != wid)
171 				i++;
172 
173 			if (cfg->w[i].id == wid)
174 				cfg->w[i].val = get_unaligned_le32(&info[4]);
175 
176 			len = 6;
177 			break;
178 
179 		case WID_STR:
180 			while (cfg->s[i].id != WID_NIL && cfg->s[i].id != wid)
181 				i++;
182 
183 			if (cfg->s[i].id == wid)
184 				memcpy(cfg->s[i].str, &info[2],
185 				       get_unaligned_le16(&info[2]) + 2);
186 
187 			len = 2 + get_unaligned_le16(&info[2]);
188 			break;
189 
190 		default:
191 			break;
192 		}
193 		size -= (2 + len);
194 		info += (2 + len);
195 	}
196 }
197 
wilc_wlan_parse_info_frame(struct wilc * wl,u8 * info)198 static void wilc_wlan_parse_info_frame(struct wilc *wl, u8 *info)
199 {
200 	u32 wid, len;
201 
202 	wid = get_unaligned_le16(info);
203 
204 	len = info[2];
205 
206 	if (len == 1 && wid == WID_STATUS) {
207 		int i = 0;
208 
209 		while (wl->cfg.b[i].id != WID_NIL &&
210 		       wl->cfg.b[i].id != wid)
211 			i++;
212 
213 		if (wl->cfg.b[i].id == wid)
214 			wl->cfg.b[i].val = info[3];
215 	}
216 }
217 
218 /********************************************
219  *
220  *      Configuration Exported Functions
221  *
222  ********************************************/
223 
wilc_wlan_cfg_set_wid(u8 * frame,u32 offset,u16 id,u8 * buf,int size)224 int wilc_wlan_cfg_set_wid(u8 *frame, u32 offset, u16 id, u8 *buf, int size)
225 {
226 	u8 type = FIELD_GET(WILC_WID_TYPE, id);
227 	int ret = 0;
228 
229 	switch (type) {
230 	case CFG_BYTE_CMD:
231 		if (size >= 1)
232 			ret = wilc_wlan_cfg_set_byte(frame, offset, id, *buf);
233 		break;
234 
235 	case CFG_HWORD_CMD:
236 		if (size >= 2)
237 			ret = wilc_wlan_cfg_set_hword(frame, offset, id,
238 						      *((u16 *)buf));
239 		break;
240 
241 	case CFG_WORD_CMD:
242 		if (size >= 4)
243 			ret = wilc_wlan_cfg_set_word(frame, offset, id,
244 						     *((u32 *)buf));
245 		break;
246 
247 	case CFG_STR_CMD:
248 		ret = wilc_wlan_cfg_set_str(frame, offset, id, buf, size);
249 		break;
250 
251 	case CFG_BIN_CMD:
252 		ret = wilc_wlan_cfg_set_bin(frame, offset, id, buf, size);
253 		break;
254 	}
255 
256 	return ret;
257 }
258 
wilc_wlan_cfg_get_wid(u8 * frame,u32 offset,u16 id)259 int wilc_wlan_cfg_get_wid(u8 *frame, u32 offset, u16 id)
260 {
261 	if ((offset + 2) >= WILC_MAX_CFG_FRAME_SIZE)
262 		return 0;
263 
264 	put_unaligned_le16(id, &frame[offset]);
265 
266 	return 2;
267 }
268 
wilc_wlan_cfg_get_val(struct wilc * wl,u16 wid,u8 * buffer,u32 buffer_size)269 int wilc_wlan_cfg_get_val(struct wilc *wl, u16 wid, u8 *buffer,
270 			  u32 buffer_size)
271 {
272 	u8 type = FIELD_GET(WILC_WID_TYPE, wid);
273 	int i, ret = 0;
274 	struct wilc_cfg *cfg = &wl->cfg;
275 
276 	i = 0;
277 	if (type == CFG_BYTE_CMD) {
278 		while (cfg->b[i].id != WID_NIL && cfg->b[i].id != wid)
279 			i++;
280 
281 		if (cfg->b[i].id == wid) {
282 			memcpy(buffer, &cfg->b[i].val, 1);
283 			ret = 1;
284 		}
285 	} else if (type == CFG_HWORD_CMD) {
286 		while (cfg->hw[i].id != WID_NIL && cfg->hw[i].id != wid)
287 			i++;
288 
289 		if (cfg->hw[i].id == wid) {
290 			memcpy(buffer, &cfg->hw[i].val, 2);
291 			ret = 2;
292 		}
293 	} else if (type == CFG_WORD_CMD) {
294 		while (cfg->w[i].id != WID_NIL && cfg->w[i].id != wid)
295 			i++;
296 
297 		if (cfg->w[i].id == wid) {
298 			memcpy(buffer, &cfg->w[i].val, 4);
299 			ret = 4;
300 		}
301 	} else if (type == CFG_STR_CMD) {
302 		while (cfg->s[i].id != WID_NIL && cfg->s[i].id != wid)
303 			i++;
304 
305 		if (cfg->s[i].id == wid) {
306 			u16 size = get_unaligned_le16(cfg->s[i].str);
307 
308 			if (buffer_size >= size) {
309 				memcpy(buffer, &cfg->s[i].str[2], size);
310 				ret = size;
311 			}
312 		}
313 	}
314 	return ret;
315 }
316 
wilc_wlan_cfg_indicate_rx(struct wilc * wilc,u8 * frame,int size,struct wilc_cfg_rsp * rsp)317 void wilc_wlan_cfg_indicate_rx(struct wilc *wilc, u8 *frame, int size,
318 			       struct wilc_cfg_rsp *rsp)
319 {
320 	u8 msg_type;
321 	u8 msg_id;
322 
323 	msg_type = frame[0];
324 	msg_id = frame[1];      /* seq no */
325 	frame += 4;
326 	size -= 4;
327 	rsp->type = 0;
328 
329 	switch (msg_type) {
330 	case WILC_RESP_MSG_TYPE_CONFIG_REPLY:
331 		wilc_wlan_parse_response_frame(wilc, frame, size);
332 		rsp->type = WILC_CFG_RSP;
333 		rsp->seq_no = msg_id;
334 		break;
335 
336 	case WILC_RESP_MSG_TYPE_STATUS_INFO:
337 		wilc_wlan_parse_info_frame(wilc, frame);
338 		rsp->type = WILC_CFG_RSP_STATUS;
339 		rsp->seq_no = msg_id;
340 		/* call host interface info parse as well */
341 		wilc_gnrl_async_info_received(wilc, frame - 4, size + 4);
342 		break;
343 
344 	case WILC_RESP_MSG_TYPE_NETWORK_INFO:
345 		wilc_network_info_received(wilc, frame - 4, size + 4);
346 		break;
347 
348 	case WILC_RESP_MSG_TYPE_SCAN_COMPLETE:
349 		wilc_scan_complete_received(wilc, frame - 4, size + 4);
350 		break;
351 
352 	default:
353 		rsp->seq_no = msg_id;
354 		break;
355 	}
356 }
357 
wilc_wlan_cfg_init(struct wilc * wl)358 int wilc_wlan_cfg_init(struct wilc *wl)
359 {
360 	struct wilc_cfg_str_vals *str_vals;
361 	int i = 0;
362 
363 	wl->cfg.b = kmemdup(g_cfg_byte, sizeof(g_cfg_byte), GFP_KERNEL);
364 	if (!wl->cfg.b)
365 		return -ENOMEM;
366 
367 	wl->cfg.hw = kmemdup(g_cfg_hword, sizeof(g_cfg_hword), GFP_KERNEL);
368 	if (!wl->cfg.hw)
369 		goto out_b;
370 
371 	wl->cfg.w = kmemdup(g_cfg_word, sizeof(g_cfg_word), GFP_KERNEL);
372 	if (!wl->cfg.w)
373 		goto out_hw;
374 
375 	wl->cfg.s = kmemdup(g_cfg_str, sizeof(g_cfg_str), GFP_KERNEL);
376 	if (!wl->cfg.s)
377 		goto out_w;
378 
379 	str_vals = kzalloc(sizeof(*str_vals), GFP_KERNEL);
380 	if (!str_vals)
381 		goto out_s;
382 
383 	wl->cfg.str_vals = str_vals;
384 	/* store the string cfg parameters */
385 	wl->cfg.s[i].id = WID_FIRMWARE_VERSION;
386 	wl->cfg.s[i].str = str_vals->firmware_version;
387 	i++;
388 	wl->cfg.s[i].id = WID_MAC_ADDR;
389 	wl->cfg.s[i].str = str_vals->mac_address;
390 	i++;
391 	wl->cfg.s[i].id = WID_ASSOC_RES_INFO;
392 	wl->cfg.s[i].str = str_vals->assoc_rsp;
393 	i++;
394 	wl->cfg.s[i].id = WID_NIL;
395 	wl->cfg.s[i].str = NULL;
396 	return 0;
397 
398 out_s:
399 	kfree(wl->cfg.s);
400 out_w:
401 	kfree(wl->cfg.w);
402 out_hw:
403 	kfree(wl->cfg.hw);
404 out_b:
405 	kfree(wl->cfg.b);
406 	return -ENOMEM;
407 }
408 
wilc_wlan_cfg_deinit(struct wilc * wl)409 void wilc_wlan_cfg_deinit(struct wilc *wl)
410 {
411 	kfree(wl->cfg.b);
412 	kfree(wl->cfg.hw);
413 	kfree(wl->cfg.w);
414 	kfree(wl->cfg.s);
415 	kfree(wl->cfg.str_vals);
416 }
417