drm/rockchip: Don't change hdmi reference clock rate
[drm/drm-misc.git] / drivers / net / wireless / microchip / wilc1000 / wlan_cfg.c
blob131388886acbfa6af83b9a208b18596ab45e7d0b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4 * All rights reserved.
5 */
7 #include <linux/bitfield.h>
8 #include "wlan_if.h"
9 #include "wlan.h"
10 #include "wlan_cfg.h"
11 #include "netdev.h"
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
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}
30 static const struct wilc_cfg_hword g_cfg_hword[] = {
31 {WID_NIL, 0}
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}
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}
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'
55 /********************************************
57 * Configuration Functions
59 ********************************************/
61 static int wilc_wlan_cfg_set_byte(u8 *frame, u32 offset, u16 id, u8 val8)
63 if ((offset + 4) >= WILC_MAX_CFG_FRAME_SIZE)
64 return 0;
66 put_unaligned_le16(id, &frame[offset]);
67 put_unaligned_le16(1, &frame[offset + 2]);
68 frame[offset + 4] = val8;
69 return 5;
72 static int wilc_wlan_cfg_set_hword(u8 *frame, u32 offset, u16 id, u16 val16)
74 if ((offset + 5) >= WILC_MAX_CFG_FRAME_SIZE)
75 return 0;
77 put_unaligned_le16(id, &frame[offset]);
78 put_unaligned_le16(2, &frame[offset + 2]);
79 put_unaligned_le16(val16, &frame[offset + 4]);
81 return 6;
84 static int wilc_wlan_cfg_set_word(u8 *frame, u32 offset, u16 id, u32 val32)
86 if ((offset + 7) >= WILC_MAX_CFG_FRAME_SIZE)
87 return 0;
89 put_unaligned_le16(id, &frame[offset]);
90 put_unaligned_le16(4, &frame[offset + 2]);
91 put_unaligned_le32(val32, &frame[offset + 4]);
93 return 8;
96 static int wilc_wlan_cfg_set_str(u8 *frame, u32 offset, u16 id, u8 *str,
97 u32 size)
99 if ((offset + size + 4) >= WILC_MAX_CFG_FRAME_SIZE)
100 return 0;
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);
107 return (size + 4);
110 static int wilc_wlan_cfg_set_bin(u8 *frame, u32 offset, u16 id, u8 *b, u32 size)
112 u32 i;
113 u8 checksum = 0;
115 if ((offset + size + 5) >= WILC_MAX_CFG_FRAME_SIZE)
116 return 0;
118 put_unaligned_le16(id, &frame[offset]);
119 put_unaligned_le16(size, &frame[offset + 2]);
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];
127 frame[offset + size + 4] = checksum;
129 return (size + 5);
132 /********************************************
134 * Configuration Response Functions
136 ********************************************/
138 static void wilc_wlan_parse_response_frame(struct wilc *wl, u8 *info, int size)
140 u16 wid;
141 u32 len = 0, i = 0;
142 struct wilc_cfg *cfg = &wl->cfg;
144 while (size > 0) {
145 i = 0;
146 wid = get_unaligned_le16(info);
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++;
153 if (cfg->b[i].id == wid)
154 cfg->b[i].val = info[4];
156 len = 3;
157 break;
159 case WID_SHORT:
160 while (cfg->hw[i].id != WID_NIL && cfg->hw[i].id != wid)
161 i++;
163 if (cfg->hw[i].id == wid)
164 cfg->hw[i].val = get_unaligned_le16(&info[4]);
166 len = 4;
167 break;
169 case WID_INT:
170 while (cfg->w[i].id != WID_NIL && cfg->w[i].id != wid)
171 i++;
173 if (cfg->w[i].id == wid)
174 cfg->w[i].val = get_unaligned_le32(&info[4]);
176 len = 6;
177 break;
179 case WID_STR:
180 while (cfg->s[i].id != WID_NIL && cfg->s[i].id != wid)
181 i++;
183 if (cfg->s[i].id == wid)
184 memcpy(cfg->s[i].str, &info[2],
185 get_unaligned_le16(&info[2]) + 2);
187 len = 2 + get_unaligned_le16(&info[2]);
188 break;
190 default:
191 break;
193 size -= (2 + len);
194 info += (2 + len);
198 static void wilc_wlan_parse_info_frame(struct wilc *wl, u8 *info)
200 u32 wid, len;
202 wid = get_unaligned_le16(info);
204 len = info[2];
206 if (len == 1 && wid == WID_STATUS) {
207 int i = 0;
209 while (wl->cfg.b[i].id != WID_NIL &&
210 wl->cfg.b[i].id != wid)
211 i++;
213 if (wl->cfg.b[i].id == wid)
214 wl->cfg.b[i].val = info[3];
218 /********************************************
220 * Configuration Exported Functions
222 ********************************************/
224 int wilc_wlan_cfg_set_wid(u8 *frame, u32 offset, u16 id, u8 *buf, int size)
226 u8 type = FIELD_GET(WILC_WID_TYPE, id);
227 int ret = 0;
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;
235 case CFG_HWORD_CMD:
236 if (size >= 2)
237 ret = wilc_wlan_cfg_set_hword(frame, offset, id,
238 *((u16 *)buf));
239 break;
241 case CFG_WORD_CMD:
242 if (size >= 4)
243 ret = wilc_wlan_cfg_set_word(frame, offset, id,
244 *((u32 *)buf));
245 break;
247 case CFG_STR_CMD:
248 ret = wilc_wlan_cfg_set_str(frame, offset, id, buf, size);
249 break;
251 case CFG_BIN_CMD:
252 ret = wilc_wlan_cfg_set_bin(frame, offset, id, buf, size);
253 break;
256 return ret;
259 int wilc_wlan_cfg_get_wid(u8 *frame, u32 offset, u16 id)
261 if ((offset + 2) >= WILC_MAX_CFG_FRAME_SIZE)
262 return 0;
264 put_unaligned_le16(id, &frame[offset]);
266 return 2;
269 int wilc_wlan_cfg_get_val(struct wilc *wl, u16 wid, u8 *buffer,
270 u32 buffer_size)
272 u8 type = FIELD_GET(WILC_WID_TYPE, wid);
273 int i, ret = 0;
274 struct wilc_cfg *cfg = &wl->cfg;
276 i = 0;
277 if (type == CFG_BYTE_CMD) {
278 while (cfg->b[i].id != WID_NIL && cfg->b[i].id != wid)
279 i++;
281 if (cfg->b[i].id == wid) {
282 memcpy(buffer, &cfg->b[i].val, 1);
283 ret = 1;
285 } else if (type == CFG_HWORD_CMD) {
286 while (cfg->hw[i].id != WID_NIL && cfg->hw[i].id != wid)
287 i++;
289 if (cfg->hw[i].id == wid) {
290 memcpy(buffer, &cfg->hw[i].val, 2);
291 ret = 2;
293 } else if (type == CFG_WORD_CMD) {
294 while (cfg->w[i].id != WID_NIL && cfg->w[i].id != wid)
295 i++;
297 if (cfg->w[i].id == wid) {
298 memcpy(buffer, &cfg->w[i].val, 4);
299 ret = 4;
301 } else if (type == CFG_STR_CMD) {
302 while (cfg->s[i].id != WID_NIL && cfg->s[i].id != wid)
303 i++;
305 if (cfg->s[i].id == wid) {
306 u16 size = get_unaligned_le16(cfg->s[i].str);
308 if (buffer_size >= size) {
309 memcpy(buffer, &cfg->s[i].str[2], size);
310 ret = size;
314 return ret;
317 void wilc_wlan_cfg_indicate_rx(struct wilc *wilc, u8 *frame, int size,
318 struct wilc_cfg_rsp *rsp)
320 u8 msg_type;
321 u8 msg_id;
323 msg_type = frame[0];
324 msg_id = frame[1]; /* seq no */
325 frame += 4;
326 size -= 4;
327 rsp->type = 0;
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;
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;
344 case WILC_RESP_MSG_TYPE_NETWORK_INFO:
345 wilc_network_info_received(wilc, frame - 4, size + 4);
346 break;
348 case WILC_RESP_MSG_TYPE_SCAN_COMPLETE:
349 wilc_scan_complete_received(wilc, frame - 4, size + 4);
350 break;
352 default:
353 rsp->seq_no = msg_id;
354 break;
358 int wilc_wlan_cfg_init(struct wilc *wl)
360 struct wilc_cfg_str_vals *str_vals;
361 int i = 0;
363 wl->cfg.b = kmemdup(g_cfg_byte, sizeof(g_cfg_byte), GFP_KERNEL);
364 if (!wl->cfg.b)
365 return -ENOMEM;
367 wl->cfg.hw = kmemdup(g_cfg_hword, sizeof(g_cfg_hword), GFP_KERNEL);
368 if (!wl->cfg.hw)
369 goto out_b;
371 wl->cfg.w = kmemdup(g_cfg_word, sizeof(g_cfg_word), GFP_KERNEL);
372 if (!wl->cfg.w)
373 goto out_hw;
375 wl->cfg.s = kmemdup(g_cfg_str, sizeof(g_cfg_str), GFP_KERNEL);
376 if (!wl->cfg.s)
377 goto out_w;
379 str_vals = kzalloc(sizeof(*str_vals), GFP_KERNEL);
380 if (!str_vals)
381 goto out_s;
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;
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;
409 void wilc_wlan_cfg_deinit(struct wilc *wl)
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);