GNU Linux-libre 4.14.294-gnu1
[releases.git] / drivers / staging / wilc1000 / wilc_wlan_cfg.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* ////////////////////////////////////////////////////////////////////////// */
3 /*  */
4 /* Copyright (c) Atmel Corporation.  All rights reserved. */
5 /*  */
6 /* Module Name:  wilc_wlan_cfg.c */
7 /*  */
8 /*  */
9 /* ///////////////////////////////////////////////////////////////////////// */
10
11 #include <linux/string.h>
12 #include "wilc_wlan_if.h"
13 #include "wilc_wlan.h"
14 #include "wilc_wlan_cfg.h"
15 #include "coreconfigurator.h"
16
17 /********************************************
18  *
19  *      Global Data
20  *
21  ********************************************/
22 enum cfg_cmd_type {
23         CFG_BYTE_CMD    = 0,
24         CFG_HWORD_CMD   = 1,
25         CFG_WORD_CMD    = 2,
26         CFG_STR_CMD     = 3,
27         CFG_BIN_CMD     = 4
28 };
29
30 struct wilc_mac_cfg {
31         int mac_status;
32         u8 mac_address[7];
33         u8 ip_address[5];
34         u8 bssid[7];
35         u8 ssid[34];
36         u8 firmware_version[129];
37         u8 supp_rate[24];
38         u8 wep_key[28];
39         u8 i_psk[66];
40         u8 hw_product_version[33];
41         u8 phyversion[17];
42         u8 supp_username[21];
43         u8 supp_password[64];
44         u8 assoc_req[256];
45         u8 assoc_rsp[256];
46         u8 firmware_info[8];
47         u8 scan_result[256];
48         u8 scan_result1[256];
49 };
50
51 static struct wilc_mac_cfg g_mac;
52
53 static struct wilc_cfg_byte g_cfg_byte[] = {
54         {WID_BSS_TYPE, 0},
55         {WID_CURRENT_TX_RATE, 0},
56         {WID_CURRENT_CHANNEL, 0},
57         {WID_PREAMBLE, 0},
58         {WID_11G_OPERATING_MODE, 0},
59         {WID_STATUS, 0},
60         {WID_SCAN_TYPE, 0},
61         {WID_KEY_ID, 0},
62         {WID_QOS_ENABLE, 0},
63         {WID_POWER_MANAGEMENT, 0},
64         {WID_11I_MODE, 0},
65         {WID_AUTH_TYPE, 0},
66         {WID_SITE_SURVEY, 0},
67         {WID_LISTEN_INTERVAL, 0},
68         {WID_DTIM_PERIOD, 0},
69         {WID_ACK_POLICY, 0},
70         {WID_BCAST_SSID, 0},
71         {WID_REKEY_POLICY, 0},
72         {WID_SHORT_SLOT_ALLOWED, 0},
73         {WID_START_SCAN_REQ, 0},
74         {WID_RSSI, 0},
75         {WID_LINKSPEED, 0},
76         {WID_AUTO_RX_SENSITIVITY, 0},
77         {WID_DATAFLOW_CONTROL, 0},
78         {WID_SCAN_FILTER, 0},
79         {WID_11N_PROT_MECH, 0},
80         {WID_11N_ERP_PROT_TYPE, 0},
81         {WID_11N_ENABLE, 0},
82         {WID_11N_OPERATING_MODE, 0},
83         {WID_11N_OBSS_NONHT_DETECTION, 0},
84         {WID_11N_HT_PROT_TYPE, 0},
85         {WID_11N_RIFS_PROT_ENABLE, 0},
86         {WID_11N_SMPS_MODE, 0},
87         {WID_11N_CURRENT_TX_MCS, 0},
88         {WID_11N_SHORT_GI_ENABLE, 0},
89         {WID_RIFS_MODE, 0},
90         {WID_TX_ABORT_CONFIG, 0},
91         {WID_11N_IMMEDIATE_BA_ENABLED, 0},
92         {WID_11N_TXOP_PROT_DISABLE, 0},
93         {WID_NIL, 0}
94 };
95
96 static struct wilc_cfg_hword g_cfg_hword[] = {
97         {WID_LINK_LOSS_THRESHOLD, 0},
98         {WID_RTS_THRESHOLD, 0},
99         {WID_FRAG_THRESHOLD, 0},
100         {WID_SHORT_RETRY_LIMIT, 0},
101         {WID_LONG_RETRY_LIMIT, 0},
102         {WID_BEACON_INTERVAL, 0},
103         {WID_RX_SENSE, 0},
104         {WID_ACTIVE_SCAN_TIME, 0},
105         {WID_PASSIVE_SCAN_TIME, 0},
106         {WID_SITE_SURVEY_SCAN_TIME, 0},
107         {WID_JOIN_START_TIMEOUT, 0},
108         {WID_AUTH_TIMEOUT, 0},
109         {WID_ASOC_TIMEOUT, 0},
110         {WID_11I_PROTOCOL_TIMEOUT, 0},
111         {WID_EAPOL_RESPONSE_TIMEOUT, 0},
112         {WID_11N_SIG_QUAL_VAL, 0},
113         {WID_CCA_THRESHOLD, 0},
114         {WID_NIL, 0}
115 };
116
117 static struct wilc_cfg_word g_cfg_word[] = {
118         {WID_FAILED_COUNT, 0},
119         {WID_RETRY_COUNT, 0},
120         {WID_MULTIPLE_RETRY_COUNT, 0},
121         {WID_FRAME_DUPLICATE_COUNT, 0},
122         {WID_ACK_FAILURE_COUNT, 0},
123         {WID_RECEIVED_FRAGMENT_COUNT, 0},
124         {WID_MCAST_RECEIVED_FRAME_COUNT, 0},
125         {WID_FCS_ERROR_COUNT, 0},
126         {WID_SUCCESS_FRAME_COUNT, 0},
127         {WID_TX_FRAGMENT_COUNT, 0},
128         {WID_TX_MULTICAST_FRAME_COUNT, 0},
129         {WID_RTS_SUCCESS_COUNT, 0},
130         {WID_RTS_FAILURE_COUNT, 0},
131         {WID_WEP_UNDECRYPTABLE_COUNT, 0},
132         {WID_REKEY_PERIOD, 0},
133         {WID_REKEY_PACKET_COUNT, 0},
134         {WID_HW_RX_COUNT, 0},
135         {WID_GET_INACTIVE_TIME, 0},
136         {WID_NIL, 0}
137
138 };
139
140 static struct wilc_cfg_str g_cfg_str[] = {
141         {WID_SSID, g_mac.ssid}, /* 33 + 1 bytes */
142         {WID_FIRMWARE_VERSION, g_mac.firmware_version},
143         {WID_OPERATIONAL_RATE_SET, g_mac.supp_rate},
144         {WID_BSSID, g_mac.bssid},       /* 6 bytes */
145         {WID_WEP_KEY_VALUE, g_mac.wep_key},     /* 27 bytes */
146         {WID_11I_PSK, g_mac.i_psk},     /* 65 bytes */
147         {WID_HARDWARE_VERSION, g_mac.hw_product_version},
148         {WID_MAC_ADDR, g_mac.mac_address},
149         {WID_PHY_VERSION, g_mac.phyversion},
150         {WID_SUPP_USERNAME, g_mac.supp_username},
151         {WID_SUPP_PASSWORD, g_mac.supp_password},
152         {WID_SITE_SURVEY_RESULTS, g_mac.scan_result},
153         {WID_SITE_SURVEY_RESULTS, g_mac.scan_result1},
154         {WID_ASSOC_REQ_INFO, g_mac.assoc_req},
155         {WID_ASSOC_RES_INFO, g_mac.assoc_rsp},
156         {WID_FIRMWARE_INFO, g_mac.firmware_version},
157         {WID_IP_ADDRESS, g_mac.ip_address},
158         {WID_NIL, NULL}
159 };
160
161 /********************************************
162  *
163  *      Configuration Functions
164  *
165  ********************************************/
166
167 static int wilc_wlan_cfg_set_byte(u8 *frame, u32 offset, u16 id, u8 val8)
168 {
169         u8 *buf;
170
171         if ((offset + 4) >= MAX_CFG_FRAME_SIZE)
172                 return 0;
173
174         buf = &frame[offset];
175
176         buf[0] = (u8)id;
177         buf[1] = (u8)(id >> 8);
178         buf[2] = 1;
179         buf[3] = 0;
180         buf[4] = val8;
181         return 5;
182 }
183
184 static int wilc_wlan_cfg_set_hword(u8 *frame, u32 offset, u16 id, u16 val16)
185 {
186         u8 *buf;
187
188         if ((offset + 5) >= MAX_CFG_FRAME_SIZE)
189                 return 0;
190
191         buf = &frame[offset];
192
193         buf[0] = (u8)id;
194         buf[1] = (u8)(id >> 8);
195         buf[2] = 2;
196         buf[3] = 0;
197         buf[4] = (u8)val16;
198         buf[5] = (u8)(val16 >> 8);
199
200         return 6;
201 }
202
203 static int wilc_wlan_cfg_set_word(u8 *frame, u32 offset, u16 id, u32 val32)
204 {
205         u8 *buf;
206
207         if ((offset + 7) >= MAX_CFG_FRAME_SIZE)
208                 return 0;
209
210         buf = &frame[offset];
211
212         buf[0] = (u8)id;
213         buf[1] = (u8)(id >> 8);
214         buf[2] = 4;
215         buf[3] = 0;
216         buf[4] = (u8)val32;
217         buf[5] = (u8)(val32 >> 8);
218         buf[6] = (u8)(val32 >> 16);
219         buf[7] = (u8)(val32 >> 24);
220
221         return 8;
222 }
223
224 static int wilc_wlan_cfg_set_str(u8 *frame, u32 offset, u16 id, u8 *str, u32 size)
225 {
226         u8 *buf;
227
228         if ((offset + size + 4) >= MAX_CFG_FRAME_SIZE)
229                 return 0;
230
231         buf = &frame[offset];
232
233         buf[0] = (u8)id;
234         buf[1] = (u8)(id >> 8);
235         buf[2] = (u8)size;
236         buf[3] = (u8)(size >> 8);
237
238         if ((str) && (size != 0))
239                 memcpy(&buf[4], str, size);
240
241         return (size + 4);
242 }
243
244 static int wilc_wlan_cfg_set_bin(u8 *frame, u32 offset, u16 id, u8 *b, u32 size)
245 {
246         u8 *buf;
247         u32 i;
248         u8 checksum = 0;
249
250         if ((offset + size + 5) >= MAX_CFG_FRAME_SIZE)
251                 return 0;
252
253         buf = &frame[offset];
254         buf[0] = (u8)id;
255         buf[1] = (u8)(id >> 8);
256         buf[2] = (u8)size;
257         buf[3] = (u8)(size >> 8);
258
259         if ((b) && (size != 0)) {
260                 memcpy(&buf[4], b, size);
261                 for (i = 0; i < size; i++)
262                         checksum += buf[i + 4];
263         }
264
265         buf[size + 4] = checksum;
266
267         return (size + 5);
268 }
269
270 /********************************************
271  *
272  *      Configuration Response Functions
273  *
274  ********************************************/
275
276 static void wilc_wlan_parse_response_frame(u8 *info, int size)
277 {
278         u32 wid, len = 0, i = 0;
279
280         while (size > 0) {
281                 i = 0;
282                 wid = info[0] | (info[1] << 8);
283                 wid = cpu_to_le32(wid);
284
285                 switch ((wid >> 12) & 0x7) {
286                 case WID_CHAR:
287                         do {
288                                 if (g_cfg_byte[i].id == WID_NIL)
289                                         break;
290
291                                 if (g_cfg_byte[i].id == wid) {
292                                         g_cfg_byte[i].val = info[4];
293                                         break;
294                                 }
295                                 i++;
296                         } while (1);
297                         len = 3;
298                         break;
299
300                 case WID_SHORT:
301                         do {
302                                 if (g_cfg_hword[i].id == WID_NIL)
303                                         break;
304
305                                 if (g_cfg_hword[i].id == wid) {
306                                         g_cfg_hword[i].val =
307                                                 cpu_to_le16(info[4] |
308                                                             (info[5] << 8));
309                                         break;
310                                 }
311                                 i++;
312                         } while (1);
313                         len = 4;
314                         break;
315
316                 case WID_INT:
317                         do {
318                                 if (g_cfg_word[i].id == WID_NIL)
319                                         break;
320
321                                 if (g_cfg_word[i].id == wid) {
322                                         g_cfg_word[i].val =
323                                                 cpu_to_le32(info[4] |
324                                                             (info[5] << 8) |
325                                                             (info[6] << 16) |
326                                                             (info[7] << 24));
327                                         break;
328                                 }
329                                 i++;
330                         } while (1);
331                         len = 6;
332                         break;
333
334                 case WID_STR:
335                         do {
336                                 if (g_cfg_str[i].id == WID_NIL)
337                                         break;
338
339                                 if (g_cfg_str[i].id == wid) {
340                                         if (wid == WID_SITE_SURVEY_RESULTS) {
341                                                 static int toggle;
342
343                                                 i += toggle;
344                                                 toggle ^= 1;
345                                         }
346                                         memcpy(g_cfg_str[i].str, &info[2],
347                                                (info[2] + 2));
348                                         break;
349                                 }
350                                 i++;
351                         } while (1);
352                         len = 2 + info[2];
353                         break;
354
355                 default:
356                         break;
357                 }
358                 size -= (2 + len);
359                 info += (2 + len);
360         }
361 }
362
363 static int wilc_wlan_parse_info_frame(u8 *info, int size)
364 {
365         struct wilc_mac_cfg *pd = &g_mac;
366         u32 wid, len;
367         int type = WILC_CFG_RSP_STATUS;
368
369         wid = info[0] | (info[1] << 8);
370
371         len = info[2];
372
373         if ((len == 1) && (wid == WID_STATUS)) {
374                 pd->mac_status = info[3];
375                 type = WILC_CFG_RSP_STATUS;
376         }
377
378         return type;
379 }
380
381 /********************************************
382  *
383  *      Configuration Exported Functions
384  *
385  ********************************************/
386
387 int wilc_wlan_cfg_set_wid(u8 *frame, u32 offset, u16 id, u8 *buf, int size)
388 {
389         u8 type = (id >> 12) & 0xf;
390         int ret = 0;
391
392         switch (type) {
393         case CFG_BYTE_CMD:
394                 if (size >= 1)
395                         ret = wilc_wlan_cfg_set_byte(frame, offset, id, *buf);
396                 break;
397
398         case CFG_HWORD_CMD:
399                 if (size >= 2)
400                         ret = wilc_wlan_cfg_set_hword(frame, offset, id,
401                                                       *((u16 *)buf));
402                 break;
403
404         case CFG_WORD_CMD:
405                 if (size >= 4)
406                         ret = wilc_wlan_cfg_set_word(frame, offset, id,
407                                                      *((u32 *)buf));
408                 break;
409
410         case CFG_STR_CMD:
411                 ret = wilc_wlan_cfg_set_str(frame, offset, id, buf, size);
412                 break;
413
414         case CFG_BIN_CMD:
415                 ret = wilc_wlan_cfg_set_bin(frame, offset, id, buf, size);
416                 break;
417         }
418
419         return ret;
420 }
421
422 int wilc_wlan_cfg_get_wid(u8 *frame, u32 offset, u16 id)
423 {
424         u8 *buf;
425
426         if ((offset + 2) >= MAX_CFG_FRAME_SIZE)
427                 return 0;
428
429         buf = &frame[offset];
430
431         buf[0] = (u8)id;
432         buf[1] = (u8)(id >> 8);
433
434         return 2;
435 }
436
437 int wilc_wlan_cfg_get_wid_value(u16 wid, u8 *buffer, u32 buffer_size)
438 {
439         u32 type = (wid >> 12) & 0xf;
440         int i, ret = 0;
441
442         if (wid == WID_STATUS) {
443                 *((u32 *)buffer) = g_mac.mac_status;
444                 return 4;
445         }
446
447         i = 0;
448         if (type == CFG_BYTE_CMD) {
449                 do {
450                         if (g_cfg_byte[i].id == WID_NIL)
451                                 break;
452
453                         if (g_cfg_byte[i].id == wid) {
454                                 memcpy(buffer,  &g_cfg_byte[i].val, 1);
455                                 ret = 1;
456                                 break;
457                         }
458                         i++;
459                 } while (1);
460         } else if (type == CFG_HWORD_CMD) {
461                 do {
462                         if (g_cfg_hword[i].id == WID_NIL)
463                                 break;
464
465                         if (g_cfg_hword[i].id == wid) {
466                                 memcpy(buffer,  &g_cfg_hword[i].val, 2);
467                                 ret = 2;
468                                 break;
469                         }
470                         i++;
471                 } while (1);
472         } else if (type == CFG_WORD_CMD) {
473                 do {
474                         if (g_cfg_word[i].id == WID_NIL)
475                                 break;
476
477                         if (g_cfg_word[i].id == wid) {
478                                 memcpy(buffer,  &g_cfg_word[i].val, 4);
479                                 ret = 4;
480                                 break;
481                         }
482                         i++;
483                 } while (1);
484         } else if (type == CFG_STR_CMD) {
485                 do {
486                         if (g_cfg_str[i].id == WID_NIL)
487                                 break;
488
489                         if (g_cfg_str[i].id == wid) {
490                                 u32 size = g_cfg_str[i].str[0] |
491                                                 (g_cfg_str[i].str[1] << 8);
492
493                                 if (buffer_size >= size) {
494                                         if (g_cfg_str[i].id == WID_SITE_SURVEY_RESULTS) {
495                                                 static int toggle;
496
497                                                 i += toggle;
498                                                 toggle ^= 1;
499
500                                         }
501                                         memcpy(buffer,  &g_cfg_str[i].str[2],
502                                                size);
503                                         ret = size;
504                                 }
505                                 break;
506                         }
507                         i++;
508                 } while (1);
509         }
510
511         return ret;
512 }
513
514 int wilc_wlan_cfg_indicate_rx(struct wilc *wilc, u8 *frame, int size,
515                               struct wilc_cfg_rsp *rsp)
516 {
517         int ret = 1;
518         u8 msg_type;
519         u8 msg_id;
520
521         msg_type = frame[0];
522         msg_id = frame[1];      /* seq no */
523         frame += 4;
524         size -= 4;
525
526         /**
527          *      The  valid types of response messages are 'R' (Response), 'I' (Information), and 'N' (Network Information)
528          **/
529
530         switch (msg_type) {
531         case 'R':
532                 wilc_wlan_parse_response_frame(frame, size);
533                 rsp->type = WILC_CFG_RSP;
534                 rsp->seq_no = msg_id;
535                 break;
536
537         case 'I':
538                 rsp->type = wilc_wlan_parse_info_frame(frame, size);
539                 rsp->seq_no = msg_id;
540                 /*call host interface info parse as well*/
541                 wilc_gnrl_async_info_received(wilc, frame - 4, size + 4);
542                 break;
543
544         case 'N':
545                 wilc_network_info_received(wilc, frame - 4, size + 4);
546                 rsp->type = 0;
547                 break;
548
549         case 'S':
550                 wilc_scan_complete_received(wilc, frame - 4, size + 4);
551                 break;
552
553         default:
554                 rsp->type = 0;
555                 rsp->seq_no = msg_id;
556                 ret = 0;
557                 break;
558         }
559
560         return ret;
561 }
562
563 int wilc_wlan_cfg_init(void)
564 {
565         memset((void *)&g_mac, 0, sizeof(struct wilc_mac_cfg));
566         return 1;
567 }