1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
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 #define WILC_RESP_MSG_TYPE_CONFIG_REPLY 'R'
48 #define WILC_RESP_MSG_TYPE_STATUS_INFO 'I'
49 #define WILC_RESP_MSG_TYPE_NETWORK_INFO 'N'
50 #define WILC_RESP_MSG_TYPE_SCAN_COMPLETE 'S'
52 /********************************************
54 * Configuration Functions
56 ********************************************/
58 static int wilc_wlan_cfg_set_byte(u8 *frame, u32 offset, u16 id, u8 val8)
60 if ((offset + 4) >= WILC_MAX_CFG_FRAME_SIZE)
63 put_unaligned_le16(id, &frame[offset]);
64 put_unaligned_le16(1, &frame[offset + 2]);
65 frame[offset + 4] = val8;
69 static int wilc_wlan_cfg_set_hword(u8 *frame, u32 offset, u16 id, u16 val16)
71 if ((offset + 5) >= WILC_MAX_CFG_FRAME_SIZE)
74 put_unaligned_le16(id, &frame[offset]);
75 put_unaligned_le16(2, &frame[offset + 2]);
76 put_unaligned_le16(val16, &frame[offset + 4]);
81 static int wilc_wlan_cfg_set_word(u8 *frame, u32 offset, u16 id, u32 val32)
83 if ((offset + 7) >= WILC_MAX_CFG_FRAME_SIZE)
86 put_unaligned_le16(id, &frame[offset]);
87 put_unaligned_le16(4, &frame[offset + 2]);
88 put_unaligned_le32(val32, &frame[offset + 4]);
93 static int wilc_wlan_cfg_set_str(u8 *frame, u32 offset, u16 id, u8 *str,
96 if ((offset + size + 4) >= WILC_MAX_CFG_FRAME_SIZE)
99 put_unaligned_le16(id, &frame[offset]);
100 put_unaligned_le16(size, &frame[offset + 2]);
101 if (str && size != 0)
102 memcpy(&frame[offset + 4], str, size);
107 static int wilc_wlan_cfg_set_bin(u8 *frame, u32 offset, u16 id, u8 *b, u32 size)
112 if ((offset + size + 5) >= WILC_MAX_CFG_FRAME_SIZE)
115 put_unaligned_le16(id, &frame[offset]);
116 put_unaligned_le16(size, &frame[offset + 2]);
118 if ((b) && size != 0) {
119 memcpy(&frame[offset + 4], b, size);
120 for (i = 0; i < size; i++)
121 checksum += frame[offset + i + 4];
124 frame[offset + size + 4] = checksum;
129 /********************************************
131 * Configuration Response Functions
133 ********************************************/
135 #define GET_WID_TYPE(wid) (((wid) >> 12) & 0x7)
136 static void wilc_wlan_parse_response_frame(struct wilc *wl, u8 *info, int size)
143 wid = get_unaligned_le16(info);
145 switch (GET_WID_TYPE(wid)) {
148 if (wl->cfg.b[i].id == WID_NIL)
151 if (wl->cfg.b[i].id == wid) {
152 wl->cfg.b[i].val = info[4];
162 struct wilc_cfg_hword *hw = &wl->cfg.hw[i];
164 if (hw->id == WID_NIL)
168 hw->val = get_unaligned_le16(&info[4]);
178 struct wilc_cfg_word *w = &wl->cfg.w[i];
180 if (w->id == WID_NIL)
184 w->val = get_unaligned_le32(&info[4]);
194 if (wl->cfg.s[i].id == WID_NIL)
197 if (wl->cfg.s[i].id == wid) {
198 memcpy(wl->cfg.s[i].str, &info[2],
215 static void wilc_wlan_parse_info_frame(struct wilc *wl, u8 *info)
219 wid = get_unaligned_le16(info);
223 if (len == 1 && wid == WID_STATUS) {
227 if (wl->cfg.b[i].id == WID_NIL)
230 if (wl->cfg.b[i].id == wid) {
231 wl->cfg.b[i].val = info[3];
239 /********************************************
241 * Configuration Exported Functions
243 ********************************************/
245 int wilc_wlan_cfg_set_wid(u8 *frame, u32 offset, u16 id, u8 *buf, int size)
247 u8 type = (id >> 12) & 0xf;
253 ret = wilc_wlan_cfg_set_byte(frame, offset, id, *buf);
258 ret = wilc_wlan_cfg_set_hword(frame, offset, id,
264 ret = wilc_wlan_cfg_set_word(frame, offset, id,
269 ret = wilc_wlan_cfg_set_str(frame, offset, id, buf, size);
273 ret = wilc_wlan_cfg_set_bin(frame, offset, id, buf, size);
280 int wilc_wlan_cfg_get_wid(u8 *frame, u32 offset, u16 id)
282 if ((offset + 2) >= WILC_MAX_CFG_FRAME_SIZE)
285 put_unaligned_le16(id, &frame[offset]);
290 int wilc_wlan_cfg_get_val(struct wilc *wl, u16 wid, u8 *buffer,
293 u32 type = (wid >> 12) & 0xf;
297 if (type == CFG_BYTE_CMD) {
299 if (wl->cfg.b[i].id == WID_NIL)
302 if (wl->cfg.b[i].id == wid) {
303 memcpy(buffer, &wl->cfg.b[i].val, 1);
309 } else if (type == CFG_HWORD_CMD) {
311 if (wl->cfg.hw[i].id == WID_NIL)
314 if (wl->cfg.hw[i].id == wid) {
315 memcpy(buffer, &wl->cfg.hw[i].val, 2);
321 } else if (type == CFG_WORD_CMD) {
323 if (wl->cfg.w[i].id == WID_NIL)
326 if (wl->cfg.w[i].id == wid) {
327 memcpy(buffer, &wl->cfg.w[i].val, 4);
333 } else if (type == CFG_STR_CMD) {
335 u32 id = wl->cfg.s[i].id;
341 u16 size = get_unaligned_le16(wl->cfg.s[i].str);
343 if (buffer_size >= size) {
344 memcpy(buffer, &wl->cfg.s[i].str[2],
356 void wilc_wlan_cfg_indicate_rx(struct wilc *wilc, u8 *frame, int size,
357 struct wilc_cfg_rsp *rsp)
363 msg_id = frame[1]; /* seq no */
369 case WILC_RESP_MSG_TYPE_CONFIG_REPLY:
370 wilc_wlan_parse_response_frame(wilc, frame, size);
371 rsp->type = WILC_CFG_RSP;
372 rsp->seq_no = msg_id;
375 case WILC_RESP_MSG_TYPE_STATUS_INFO:
376 wilc_wlan_parse_info_frame(wilc, frame);
377 rsp->type = WILC_CFG_RSP_STATUS;
378 rsp->seq_no = msg_id;
379 /* call host interface info parse as well */
380 wilc_gnrl_async_info_received(wilc, frame - 4, size + 4);
383 case WILC_RESP_MSG_TYPE_NETWORK_INFO:
384 wilc_network_info_received(wilc, frame - 4, size + 4);
387 case WILC_RESP_MSG_TYPE_SCAN_COMPLETE:
388 wilc_scan_complete_received(wilc, frame - 4, size + 4);
392 rsp->seq_no = msg_id;
397 int wilc_wlan_cfg_init(struct wilc *wl)
399 struct wilc_cfg_str_vals *str_vals;
402 wl->cfg.b = kmemdup(g_cfg_byte, sizeof(g_cfg_byte), GFP_KERNEL);
406 wl->cfg.hw = kmemdup(g_cfg_hword, sizeof(g_cfg_hword), GFP_KERNEL);
410 wl->cfg.w = kmemdup(g_cfg_word, sizeof(g_cfg_word), GFP_KERNEL);
414 wl->cfg.s = kmemdup(g_cfg_str, sizeof(g_cfg_str), GFP_KERNEL);
418 str_vals = kzalloc(sizeof(*str_vals), GFP_KERNEL);
422 wl->cfg.str_vals = str_vals;
423 /* store the string cfg parameters */
424 wl->cfg.s[i].id = WID_FIRMWARE_VERSION;
425 wl->cfg.s[i].str = str_vals->firmware_version;
427 wl->cfg.s[i].id = WID_MAC_ADDR;
428 wl->cfg.s[i].str = str_vals->mac_address;
430 wl->cfg.s[i].id = WID_ASSOC_RES_INFO;
431 wl->cfg.s[i].str = str_vals->assoc_rsp;
433 wl->cfg.s[i].id = WID_NIL;
434 wl->cfg.s[i].str = NULL;
448 void wilc_wlan_cfg_deinit(struct wilc *wl)
454 kfree(wl->cfg.str_vals);