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