1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
7 #include "wilc_wlan_if.h"
9 #include "wilc_wlan_cfg.h"
10 #include "wilc_wfi_netdevice.h"
20 static const struct wilc_cfg_byte g_cfg_byte[] = {
27 static const struct wilc_cfg_hword g_cfg_hword[] = {
31 static const struct wilc_cfg_word g_cfg_word[] = {
32 {WID_FAILED_COUNT, 0},
33 {WID_RECEIVED_FRAGMENT_COUNT, 0},
34 {WID_SUCCESS_FRAME_COUNT, 0},
35 {WID_GET_INACTIVE_TIME, 0},
40 static const struct wilc_cfg_str g_cfg_str[] = {
41 {WID_FIRMWARE_VERSION, NULL},
43 {WID_ASSOC_RES_INFO, NULL},
47 /********************************************
49 * Configuration Functions
51 ********************************************/
53 static int wilc_wlan_cfg_set_byte(u8 *frame, u32 offset, u16 id, u8 val8)
55 if ((offset + 4) >= WILC_MAX_CFG_FRAME_SIZE)
58 put_unaligned_le16(id, &frame[offset]);
59 put_unaligned_le16(1, &frame[offset + 2]);
60 frame[offset + 4] = val8;
64 static int wilc_wlan_cfg_set_hword(u8 *frame, u32 offset, u16 id, u16 val16)
66 if ((offset + 5) >= WILC_MAX_CFG_FRAME_SIZE)
69 put_unaligned_le16(id, &frame[offset]);
70 put_unaligned_le16(2, &frame[offset + 2]);
71 put_unaligned_le16(val16, &frame[offset + 4]);
76 static int wilc_wlan_cfg_set_word(u8 *frame, u32 offset, u16 id, u32 val32)
78 if ((offset + 7) >= WILC_MAX_CFG_FRAME_SIZE)
81 put_unaligned_le16(id, &frame[offset]);
82 put_unaligned_le16(4, &frame[offset + 2]);
83 put_unaligned_le32(val32, &frame[offset + 4]);
88 static int wilc_wlan_cfg_set_str(u8 *frame, u32 offset, u16 id, u8 *str,
91 if ((offset + size + 4) >= WILC_MAX_CFG_FRAME_SIZE)
94 put_unaligned_le16(id, &frame[offset]);
95 put_unaligned_le16(size, &frame[offset + 2]);
97 memcpy(&frame[offset + 4], str, size);
102 static int wilc_wlan_cfg_set_bin(u8 *frame, u32 offset, u16 id, u8 *b, u32 size)
107 if ((offset + size + 5) >= WILC_MAX_CFG_FRAME_SIZE)
110 put_unaligned_le16(id, &frame[offset]);
111 put_unaligned_le16(size, &frame[offset + 2]);
113 if ((b) && size != 0) {
114 memcpy(&frame[offset + 4], b, size);
115 for (i = 0; i < size; i++)
116 checksum += frame[offset + i + 4];
119 frame[offset + size + 4] = checksum;
124 /********************************************
126 * Configuration Response Functions
128 ********************************************/
130 #define GET_WID_TYPE(wid) (((wid) >> 12) & 0x7)
131 static void wilc_wlan_parse_response_frame(struct wilc *wl, u8 *info, int size)
138 wid = get_unaligned_le16(info);
140 switch (GET_WID_TYPE(wid)) {
143 if (wl->cfg.b[i].id == WID_NIL)
146 if (wl->cfg.b[i].id == wid) {
147 wl->cfg.b[i].val = info[4];
157 struct wilc_cfg_hword *hw = &wl->cfg.hw[i];
159 if (hw->id == WID_NIL)
163 hw->val = get_unaligned_le16(&info[4]);
173 struct wilc_cfg_word *w = &wl->cfg.w[i];
175 if (w->id == WID_NIL)
179 w->val = get_unaligned_le32(&info[4]);
189 if (wl->cfg.s[i].id == WID_NIL)
192 if (wl->cfg.s[i].id == wid) {
193 memcpy(wl->cfg.s[i].str, &info[2],
210 static void wilc_wlan_parse_info_frame(struct wilc *wl, u8 *info)
214 wid = get_unaligned_le16(info);
218 if (len == 1 && wid == WID_STATUS) {
222 if (wl->cfg.b[i].id == WID_NIL)
225 if (wl->cfg.b[i].id == wid) {
226 wl->cfg.b[i].val = info[3];
234 /********************************************
236 * Configuration Exported Functions
238 ********************************************/
240 int wilc_wlan_cfg_set_wid(u8 *frame, u32 offset, u16 id, u8 *buf, int size)
242 u8 type = (id >> 12) & 0xf;
248 ret = wilc_wlan_cfg_set_byte(frame, offset, id, *buf);
253 ret = wilc_wlan_cfg_set_hword(frame, offset, id,
259 ret = wilc_wlan_cfg_set_word(frame, offset, id,
264 ret = wilc_wlan_cfg_set_str(frame, offset, id, buf, size);
268 ret = wilc_wlan_cfg_set_bin(frame, offset, id, buf, size);
275 int wilc_wlan_cfg_get_wid(u8 *frame, u32 offset, u16 id)
277 if ((offset + 2) >= WILC_MAX_CFG_FRAME_SIZE)
280 put_unaligned_le16(id, &frame[offset]);
285 int wilc_wlan_cfg_get_val(struct wilc *wl, u16 wid, u8 *buffer,
288 u32 type = (wid >> 12) & 0xf;
292 if (type == CFG_BYTE_CMD) {
294 if (wl->cfg.b[i].id == WID_NIL)
297 if (wl->cfg.b[i].id == wid) {
298 memcpy(buffer, &wl->cfg.b[i].val, 1);
304 } else if (type == CFG_HWORD_CMD) {
306 if (wl->cfg.hw[i].id == WID_NIL)
309 if (wl->cfg.hw[i].id == wid) {
310 memcpy(buffer, &wl->cfg.hw[i].val, 2);
316 } else if (type == CFG_WORD_CMD) {
318 if (wl->cfg.w[i].id == WID_NIL)
321 if (wl->cfg.w[i].id == wid) {
322 memcpy(buffer, &wl->cfg.w[i].val, 4);
328 } else if (type == CFG_STR_CMD) {
330 u32 id = wl->cfg.s[i].id;
336 u16 size = get_unaligned_le16(wl->cfg.s[i].str);
338 if (buffer_size >= size) {
339 memcpy(buffer, &wl->cfg.s[i].str[2],
351 void wilc_wlan_cfg_indicate_rx(struct wilc *wilc, u8 *frame, int size,
352 struct wilc_cfg_rsp *rsp)
358 msg_id = frame[1]; /* seq no */
364 * The valid types of response messages are
366 * 'I' (Information), and
367 * 'N' (Network Information)
372 wilc_wlan_parse_response_frame(wilc, frame, size);
373 rsp->type = WILC_CFG_RSP;
374 rsp->seq_no = msg_id;
378 wilc_wlan_parse_info_frame(wilc, frame);
379 rsp->type = WILC_CFG_RSP_STATUS;
380 rsp->seq_no = msg_id;
381 /*call host interface info parse as well*/
382 wilc_gnrl_async_info_received(wilc, frame - 4, size + 4);
386 wilc_network_info_received(wilc, frame - 4, size + 4);
390 wilc_scan_complete_received(wilc, frame - 4, size + 4);
394 rsp->seq_no = msg_id;
399 int wilc_wlan_cfg_init(struct wilc *wl)
401 struct wilc_cfg_str_vals *str_vals;
404 wl->cfg.b = kmemdup(g_cfg_byte, sizeof(g_cfg_byte), GFP_KERNEL);
408 wl->cfg.hw = kmemdup(g_cfg_hword, sizeof(g_cfg_hword), GFP_KERNEL);
412 wl->cfg.w = kmemdup(g_cfg_word, sizeof(g_cfg_word), GFP_KERNEL);
416 wl->cfg.s = kmemdup(g_cfg_str, sizeof(g_cfg_str), GFP_KERNEL);
420 str_vals = kzalloc(sizeof(*str_vals), GFP_KERNEL);
424 wl->cfg.str_vals = str_vals;
425 /* store the string cfg parameters */
426 wl->cfg.s[i].id = WID_FIRMWARE_VERSION;
427 wl->cfg.s[i].str = str_vals->firmware_version;
429 wl->cfg.s[i].id = WID_MAC_ADDR;
430 wl->cfg.s[i].str = str_vals->mac_address;
432 wl->cfg.s[i].id = WID_ASSOC_RES_INFO;
433 wl->cfg.s[i].str = str_vals->assoc_rsp;
435 wl->cfg.s[i].id = WID_NIL;
436 wl->cfg.s[i].str = NULL;
450 void wilc_wlan_cfg_deinit(struct wilc *wl)
456 kfree(wl->cfg.str_vals);