| 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, 0, NULL}, |
| 45 |
{WID_MAC_ADDR, 0, NULL}, |
| 46 |
{WID_ASSOC_RES_INFO, 0, NULL}, |
| 47 |
{WID_NIL, 0, 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
len = 3; |
| 151 |
if (len + 2 > size) |
| 152 |
return; |
| 153 |
|
| 154 |
while (cfg->b[i].id != WID_NIL && cfg->b[i].id != wid) |
| 155 |
i++; |
| 156 |
|
| 157 |
if (cfg->b[i].id == wid) |
| 158 |
cfg->b[i].val = info[4]; |
| 159 |
|
| 160 |
break; |
| 161 |
|
| 162 |
case WID_SHORT: |
| 163 |
len = 4; |
| 164 |
if (len + 2 > size) |
| 165 |
return; |
| 166 |
|
| 167 |
while (cfg->hw[i].id != WID_NIL && cfg->hw[i].id != wid) |
| 168 |
i++; |
| 169 |
|
| 170 |
if (cfg->hw[i].id == wid) |
| 171 |
cfg->hw[i].val = get_unaligned_le16(&info[4]); |
| 172 |
|
| 173 |
break; |
| 174 |
|
| 175 |
case WID_INT: |
| 176 |
len = 6; |
| 177 |
if (len + 2 > size) |
| 178 |
return; |
| 179 |
|
| 180 |
while (cfg->w[i].id != WID_NIL && cfg->w[i].id != wid) |
| 181 |
i++; |
| 182 |
|
| 183 |
if (cfg->w[i].id == wid) |
| 184 |
cfg->w[i].val = get_unaligned_le32(&info[4]); |
| 185 |
|
| 186 |
break; |
| 187 |
|
| 188 |
case WID_STR: |
| 189 |
len = 2 + get_unaligned_le16(&info[2]); |
| 190 |
|
| 191 |
while (cfg->s[i].id != WID_NIL && cfg->s[i].id != wid) |
| 192 |
i++; |
| 193 |
|
| 194 |
if (cfg->s[i].id == wid) { |
| 195 |
if (len > cfg->s[i].len || (len + 2 > size)) |
| 196 |
return; |
| 197 |
|
| 198 |
memcpy(cfg->s[i].str, &info[2], |
| 199 |
len); |
| 200 |
} |
| 201 |
|
| 202 |
break; |
| 203 |
|
| 204 |
default: |
| 205 |
break; |
| 206 |
} |
| 207 |
size -= (2 + len); |
| 208 |
info += (2 + len); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
static void wilc_wlan_parse_info_frame(struct wilc *wl, u8 *info) |
| 213 |
{ |
| 214 |
u32 wid, len; |
| 215 |
|
| 216 |
wid = get_unaligned_le16(info); |
| 217 |
|
| 218 |
len = info[2]; |
| 219 |
|
| 220 |
if (len == 1 && wid == WID_STATUS) { |
| 221 |
int i = 0; |
| 222 |
|
| 223 |
while (wl->cfg.b[i].id != WID_NIL && |
| 224 |
wl->cfg.b[i].id != wid) |
| 225 |
i++; |
| 226 |
|
| 227 |
if (wl->cfg.b[i].id == wid) |
| 228 |
wl->cfg.b[i].val = info[3]; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/******************************************** |
| 233 |
* |
| 234 |
* Configuration Exported Functions |
| 235 |
* |
| 236 |
********************************************/ |
| 237 |
|
| 238 |
int wilc_wlan_cfg_set_wid(u8 *frame, u32 offset, u16 id, u8 *buf, int size) |
| 239 |
{ |
| 240 |
u8 type = FIELD_GET(WILC_WID_TYPE, id); |
| 241 |
int ret = 0; |
| 242 |
|
| 243 |
switch (type) { |
| 244 |
case CFG_BYTE_CMD: |
| 245 |
if (size >= 1) |
| 246 |
ret = wilc_wlan_cfg_set_byte(frame, offset, id, *buf); |
| 247 |
break; |
| 248 |
|
| 249 |
case CFG_HWORD_CMD: |
| 250 |
if (size >= 2) |
| 251 |
ret = wilc_wlan_cfg_set_hword(frame, offset, id, |
| 252 |
*((u16 *)buf)); |
| 253 |
break; |
| 254 |
|
| 255 |
case CFG_WORD_CMD: |
| 256 |
if (size >= 4) |
| 257 |
ret = wilc_wlan_cfg_set_word(frame, offset, id, |
| 258 |
*((u32 *)buf)); |
| 259 |
break; |
| 260 |
|
| 261 |
case CFG_STR_CMD: |
| 262 |
ret = wilc_wlan_cfg_set_str(frame, offset, id, buf, size); |
| 263 |
break; |
| 264 |
|
| 265 |
case CFG_BIN_CMD: |
| 266 |
ret = wilc_wlan_cfg_set_bin(frame, offset, id, buf, size); |
| 267 |
break; |
| 268 |
} |
| 269 |
|
| 270 |
return ret; |
| 271 |
} |
| 272 |
|
| 273 |
int wilc_wlan_cfg_get_wid(u8 *frame, u32 offset, u16 id) |
| 274 |
{ |
| 275 |
if ((offset + 2) >= WILC_MAX_CFG_FRAME_SIZE) |
| 276 |
return 0; |
| 277 |
|
| 278 |
put_unaligned_le16(id, &frame[offset]); |
| 279 |
|
| 280 |
return 2; |
| 281 |
} |
| 282 |
|
| 283 |
int wilc_wlan_cfg_get_val(struct wilc *wl, u16 wid, u8 *buffer, |
| 284 |
u32 buffer_size) |
| 285 |
{ |
| 286 |
u8 type = FIELD_GET(WILC_WID_TYPE, wid); |
| 287 |
int i, ret = 0; |
| 288 |
struct wilc_cfg *cfg = &wl->cfg; |
| 289 |
|
| 290 |
i = 0; |
| 291 |
if (type == CFG_BYTE_CMD) { |
| 292 |
while (cfg->b[i].id != WID_NIL && cfg->b[i].id != wid) |
| 293 |
i++; |
| 294 |
|
| 295 |
if (cfg->b[i].id == wid) { |
| 296 |
memcpy(buffer, &cfg->b[i].val, 1); |
| 297 |
ret = 1; |
| 298 |
} |
| 299 |
} else if (type == CFG_HWORD_CMD) { |
| 300 |
while (cfg->hw[i].id != WID_NIL && cfg->hw[i].id != wid) |
| 301 |
i++; |
| 302 |
|
| 303 |
if (cfg->hw[i].id == wid) { |
| 304 |
memcpy(buffer, &cfg->hw[i].val, 2); |
| 305 |
ret = 2; |
| 306 |
} |
| 307 |
} else if (type == CFG_WORD_CMD) { |
| 308 |
while (cfg->w[i].id != WID_NIL && cfg->w[i].id != wid) |
| 309 |
i++; |
| 310 |
|
| 311 |
if (cfg->w[i].id == wid) { |
| 312 |
memcpy(buffer, &cfg->w[i].val, 4); |
| 313 |
ret = 4; |
| 314 |
} |
| 315 |
} else if (type == CFG_STR_CMD) { |
| 316 |
while (cfg->s[i].id != WID_NIL && cfg->s[i].id != wid) |
| 317 |
i++; |
| 318 |
|
| 319 |
if (cfg->s[i].id == wid) { |
| 320 |
u16 size = get_unaligned_le16(cfg->s[i].str); |
| 321 |
|
| 322 |
if (buffer_size >= size) { |
| 323 |
memcpy(buffer, &cfg->s[i].str[2], size); |
| 324 |
ret = size; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
return ret; |
| 329 |
} |
| 330 |
|
| 331 |
void wilc_wlan_cfg_indicate_rx(struct wilc *wilc, u8 *frame, int size, |
| 332 |
struct wilc_cfg_rsp *rsp) |
| 333 |
{ |
| 334 |
u8 msg_type; |
| 335 |
u8 msg_id; |
| 336 |
|
| 337 |
msg_type = frame[0]; |
| 338 |
msg_id = frame[1]; /* seq no */ |
| 339 |
frame += 4; |
| 340 |
size -= 4; |
| 341 |
rsp->type = 0; |
| 342 |
|
| 343 |
switch (msg_type) { |
| 344 |
case WILC_RESP_MSG_TYPE_CONFIG_REPLY: |
| 345 |
wilc_wlan_parse_response_frame(wilc, frame, size); |
| 346 |
rsp->type = WILC_CFG_RSP; |
| 347 |
rsp->seq_no = msg_id; |
| 348 |
break; |
| 349 |
|
| 350 |
case WILC_RESP_MSG_TYPE_STATUS_INFO: |
| 351 |
wilc_wlan_parse_info_frame(wilc, frame); |
| 352 |
rsp->type = WILC_CFG_RSP_STATUS; |
| 353 |
rsp->seq_no = msg_id; |
| 354 |
/* call host interface info parse as well */ |
| 355 |
wilc_gnrl_async_info_received(wilc, frame - 4, size + 4); |
| 356 |
break; |
| 357 |
|
| 358 |
case WILC_RESP_MSG_TYPE_NETWORK_INFO: |
| 359 |
wilc_network_info_received(wilc, frame - 4, size + 4); |
| 360 |
break; |
| 361 |
|
| 362 |
case WILC_RESP_MSG_TYPE_SCAN_COMPLETE: |
| 363 |
wilc_scan_complete_received(wilc, frame - 4, size + 4); |
| 364 |
break; |
| 365 |
|
| 366 |
default: |
| 367 |
rsp->seq_no = msg_id; |
| 368 |
break; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
int wilc_wlan_cfg_init(struct wilc *wl) |
| 373 |
{ |
| 374 |
struct wilc_cfg_str_vals *str_vals; |
| 375 |
int i = 0; |
| 376 |
|
| 377 |
wl->cfg.b = kmemdup(g_cfg_byte, sizeof(g_cfg_byte), GFP_KERNEL); |
| 378 |
if (!wl->cfg.b) |
| 379 |
return -ENOMEM; |
| 380 |
|
| 381 |
wl->cfg.hw = kmemdup(g_cfg_hword, sizeof(g_cfg_hword), GFP_KERNEL); |
| 382 |
if (!wl->cfg.hw) |
| 383 |
goto out_b; |
| 384 |
|
| 385 |
wl->cfg.w = kmemdup(g_cfg_word, sizeof(g_cfg_word), GFP_KERNEL); |
| 386 |
if (!wl->cfg.w) |
| 387 |
goto out_hw; |
| 388 |
|
| 389 |
wl->cfg.s = kmemdup(g_cfg_str, sizeof(g_cfg_str), GFP_KERNEL); |
| 390 |
if (!wl->cfg.s) |
| 391 |
goto out_w; |
| 392 |
|
| 393 |
str_vals = kzalloc_obj(*str_vals); |
| 394 |
if (!str_vals) |
| 395 |
goto out_s; |
| 396 |
|
| 397 |
wl->cfg.str_vals = str_vals; |
| 398 |
/* store the string cfg parameters */ |
| 399 |
wl->cfg.s[i].id = WID_FIRMWARE_VERSION; |
| 400 |
wl->cfg.s[i].str = str_vals->firmware_version; |
| 401 |
wl->cfg.s[i].len = sizeof(str_vals->firmware_version); |
| 402 |
i++; |
| 403 |
wl->cfg.s[i].id = WID_MAC_ADDR; |
| 404 |
wl->cfg.s[i].str = str_vals->mac_address; |
| 405 |
wl->cfg.s[i].len = sizeof(str_vals->mac_address); |
| 406 |
i++; |
| 407 |
wl->cfg.s[i].id = WID_ASSOC_RES_INFO; |
| 408 |
wl->cfg.s[i].str = str_vals->assoc_rsp; |
| 409 |
wl->cfg.s[i].len = sizeof(str_vals->assoc_rsp); |
| 410 |
i++; |
| 411 |
wl->cfg.s[i].id = WID_NIL; |
| 412 |
wl->cfg.s[i].str = NULL; |
| 413 |
return 0; |
| 414 |
|
| 415 |
out_s: |
| 416 |
kfree(wl->cfg.s); |
| 417 |
out_w: |
| 418 |
kfree(wl->cfg.w); |
| 419 |
out_hw: |
| 420 |
kfree(wl->cfg.hw); |
| 421 |
out_b: |
| 422 |
kfree(wl->cfg.b); |
| 423 |
return -ENOMEM; |
| 424 |
} |
| 425 |
|
| 426 |
void wilc_wlan_cfg_deinit(struct wilc *wl) |
| 427 |
{ |
| 428 |
kfree(wl->cfg.b); |
| 429 |
kfree(wl->cfg.hw); |
| 430 |
kfree(wl->cfg.w); |
| 431 |
kfree(wl->cfg.s); |
| 432 |
kfree(wl->cfg.str_vals); |
| 433 |
} |