d4ce9860d4c230258043d7df82de9e91a7d51930
[platform/kernel/linux-starfive.git] / drivers / staging / ks7010 / ks_hostif.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *   Driver for KeyStream wireless LAN cards.
4  *
5  *   Copyright (C) 2005-2008 KeyStream Corp.
6  *   Copyright (C) 2009 Renesas Technology Corp.
7  */
8
9 #include <linux/circ_buf.h>
10 #include <linux/if_arp.h>
11 #include <net/iw_handler.h>
12 #include <uapi/linux/llc.h>
13 #include "eap_packet.h"
14 #include "ks_wlan.h"
15 #include "michael_mic.h"
16 #include "ks_hostif.h"
17
18 static inline void inc_smeqhead(struct ks_wlan_private *priv)
19 {
20         priv->sme_i.qhead = (priv->sme_i.qhead + 1) % SME_EVENT_BUFF_SIZE;
21 }
22
23 static inline void inc_smeqtail(struct ks_wlan_private *priv)
24 {
25         priv->sme_i.qtail = (priv->sme_i.qtail + 1) % SME_EVENT_BUFF_SIZE;
26 }
27
28 static inline unsigned int cnt_smeqbody(struct ks_wlan_private *priv)
29 {
30         return CIRC_CNT_TO_END(priv->sme_i.qhead, priv->sme_i.qtail,
31                                SME_EVENT_BUFF_SIZE);
32 }
33
34 static inline u8 get_byte(struct ks_wlan_private *priv)
35 {
36         u8 data;
37
38         data = *(priv->rxp)++;
39         /* length check in advance ! */
40         --(priv->rx_size);
41         return data;
42 }
43
44 static inline u16 get_word(struct ks_wlan_private *priv)
45 {
46         u16 data;
47
48         data = (get_byte(priv) & 0xff);
49         data |= ((get_byte(priv) << 8) & 0xff00);
50         return data;
51 }
52
53 static inline u32 get_dword(struct ks_wlan_private *priv)
54 {
55         u32 data;
56
57         data = (get_byte(priv) & 0xff);
58         data |= ((get_byte(priv) << 8) & 0x0000ff00);
59         data |= ((get_byte(priv) << 16) & 0x00ff0000);
60         data |= ((get_byte(priv) << 24) & 0xff000000);
61         return data;
62 }
63
64 static void ks_wlan_hw_wakeup_task(struct work_struct *work)
65 {
66         struct ks_wlan_private *priv;
67         int ps_status;
68         long time_left;
69
70         priv = container_of(work, struct ks_wlan_private, wakeup_work);
71         ps_status = atomic_read(&priv->psstatus.status);
72
73         if (ps_status == PS_SNOOZE) {
74                 ks_wlan_hw_wakeup_request(priv);
75                 time_left = wait_for_completion_interruptible_timeout(
76                                 &priv->psstatus.wakeup_wait,
77                                 msecs_to_jiffies(20));
78                 if (time_left <= 0) {
79                         netdev_dbg(priv->net_dev, "wake up timeout or interrupted !!!\n");
80                         schedule_work(&priv->wakeup_work);
81                         return;
82                 }
83         }
84
85         /* power save */
86         if (atomic_read(&priv->sme_task.count) > 0)
87                 tasklet_enable(&priv->sme_task);
88 }
89
90 static void ks_wlan_do_power_save(struct ks_wlan_private *priv)
91 {
92         if (is_connect_status(priv->connect_status))
93                 hostif_sme_enqueue(priv, SME_POW_MNGMT_REQUEST);
94         else
95                 priv->dev_state = DEVICE_STATE_READY;
96 }
97
98 static
99 int get_current_ap(struct ks_wlan_private *priv, struct link_ap_info *ap_info)
100 {
101         struct local_ap *ap;
102         union iwreq_data wrqu;
103         struct net_device *netdev = priv->net_dev;
104         u8 size;
105
106         ap = &priv->current_ap;
107
108         if (is_disconnect_status(priv->connect_status)) {
109                 memset(ap, 0, sizeof(struct local_ap));
110                 return -EPERM;
111         }
112
113         ether_addr_copy(ap->bssid, ap_info->bssid);
114         memcpy(ap->ssid.body, priv->reg.ssid.body,
115                priv->reg.ssid.size);
116         ap->ssid.size = priv->reg.ssid.size;
117         memcpy(ap->rate_set.body, ap_info->rate_set.body,
118                ap_info->rate_set.size);
119         ap->rate_set.size = ap_info->rate_set.size;
120         if (ap_info->ext_rate_set.size != 0) {
121                 memcpy(&ap->rate_set.body[ap->rate_set.size],
122                        ap_info->ext_rate_set.body,
123                        ap_info->ext_rate_set.size);
124                 ap->rate_set.size += ap_info->ext_rate_set.size;
125         }
126         ap->channel = ap_info->ds_parameter.channel;
127         ap->rssi = ap_info->rssi;
128         ap->sq = ap_info->sq;
129         ap->noise = ap_info->noise;
130         ap->capability = le16_to_cpu(ap_info->capability);
131         size = (ap_info->rsn.size <= RSN_IE_BODY_MAX) ?
132                 ap_info->rsn.size : RSN_IE_BODY_MAX;
133         if ((ap_info->rsn_mode & RSN_MODE_WPA2) &&
134             (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2)) {
135                 ap->rsn_ie.id = RSN_INFO_ELEM_ID;
136                 ap->rsn_ie.size = size;
137                 memcpy(ap->rsn_ie.body, ap_info->rsn.body, size);
138         } else if ((ap_info->rsn_mode & RSN_MODE_WPA) &&
139                    (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA)) {
140                 ap->wpa_ie.id = WPA_INFO_ELEM_ID;
141                 ap->wpa_ie.size = size;
142                 memcpy(ap->wpa_ie.body, ap_info->rsn.body, size);
143         } else {
144                 ap->rsn_ie.id = 0;
145                 ap->rsn_ie.size = 0;
146                 ap->wpa_ie.id = 0;
147                 ap->wpa_ie.size = 0;
148         }
149
150         wrqu.data.length = 0;
151         wrqu.data.flags = 0;
152         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
153         if (is_connect_status(priv->connect_status)) {
154                 ether_addr_copy(wrqu.ap_addr.sa_data, priv->current_ap.bssid);
155                 netdev_dbg(priv->net_dev,
156                            "IWEVENT: connect bssid=%pM\n",
157                            wrqu.ap_addr.sa_data);
158                 wireless_send_event(netdev, SIOCGIWAP, &wrqu, NULL);
159         }
160         netdev_dbg(priv->net_dev, "Link AP\n"
161                    "- bssid=%02X:%02X:%02X:%02X:%02X:%02X\n"
162                    "- essid=%s\n"
163                    "- rate_set=%02X,%02X,%02X,%02X,%02X,%02X,%02X,%02X\n"
164                    "- channel=%d\n"
165                    "- rssi=%d\n"
166                    "- sq=%d\n"
167                    "- capability=%04X\n"
168                    "- rsn.mode=%d\n"
169                    "- rsn.size=%d\n"
170                    "- ext_rate_set_size=%d\n"
171                    "- rate_set_size=%d\n",
172                    ap->bssid[0], ap->bssid[1], ap->bssid[2],
173                    ap->bssid[3], ap->bssid[4], ap->bssid[5],
174                    &(ap->ssid.body[0]),
175                    ap->rate_set.body[0], ap->rate_set.body[1],
176                    ap->rate_set.body[2], ap->rate_set.body[3],
177                    ap->rate_set.body[4], ap->rate_set.body[5],
178                    ap->rate_set.body[6], ap->rate_set.body[7],
179                    ap->channel, ap->rssi, ap->sq, ap->capability,
180                    ap_info->rsn_mode, ap_info->rsn.size,
181                    ap_info->ext_rate_set.size, ap_info->rate_set.size);
182
183         return 0;
184 }
185
186 static u8 read_ie(unsigned char *bp, u8 max, u8 *body)
187 {
188         u8 size = (*(bp + 1) <= max) ? *(bp + 1) : max;
189
190         memcpy(body, bp + 2, size);
191         return size;
192 }
193
194
195 static
196 int get_ap_information(struct ks_wlan_private *priv, struct ap_info *ap_info,
197                        struct local_ap *ap)
198 {
199         unsigned char *bp;
200         int bsize, offset;
201
202         memset(ap, 0, sizeof(struct local_ap));
203
204         ether_addr_copy(ap->bssid, ap_info->bssid);
205         ap->rssi = ap_info->rssi;
206         ap->sq = ap_info->sq;
207         ap->noise = ap_info->noise;
208         ap->capability = le16_to_cpu(ap_info->capability);
209         ap->channel = ap_info->ch_info;
210
211         bp = ap_info->body;
212         bsize = le16_to_cpu(ap_info->body_size);
213         offset = 0;
214
215         while (bsize > offset) {
216                 switch (*bp) { /* Information Element ID */
217                 case WLAN_EID_SSID:
218                         ap->ssid.size = read_ie(bp, IEEE80211_MAX_SSID_LEN,
219                                                 ap->ssid.body);
220                         break;
221                 case WLAN_EID_SUPP_RATES:
222                 case WLAN_EID_EXT_SUPP_RATES:
223                         if ((*(bp + 1) + ap->rate_set.size) <=
224                             RATE_SET_MAX_SIZE) {
225                                 memcpy(&ap->rate_set.body[ap->rate_set.size],
226                                        bp + 2, *(bp + 1));
227                                 ap->rate_set.size += *(bp + 1);
228                         } else {
229                                 memcpy(&ap->rate_set.body[ap->rate_set.size],
230                                        bp + 2,
231                                        RATE_SET_MAX_SIZE - ap->rate_set.size);
232                                 ap->rate_set.size +=
233                                     (RATE_SET_MAX_SIZE - ap->rate_set.size);
234                         }
235                         break;
236                 case WLAN_EID_RSN:
237                         ap->rsn_ie.id = *bp;
238                         ap->rsn_ie.size = read_ie(bp, RSN_IE_BODY_MAX,
239                                                   ap->rsn_ie.body);
240                         break;
241                 case WLAN_EID_VENDOR_SPECIFIC: /* WPA */
242                         /* WPA OUI check */
243                         if (memcmp(bp + 2, CIPHER_ID_WPA_WEP40, 4) == 0) {
244                                 ap->wpa_ie.id = *bp;
245                                 ap->wpa_ie.size = read_ie(bp, RSN_IE_BODY_MAX,
246                                                           ap->wpa_ie.body);
247                         }
248                         break;
249                 case WLAN_EID_DS_PARAMS:
250                 case WLAN_EID_FH_PARAMS:
251                 case WLAN_EID_CF_PARAMS:
252                 case WLAN_EID_TIM:
253                 case WLAN_EID_IBSS_PARAMS:
254                 case WLAN_EID_COUNTRY:
255                 case WLAN_EID_ERP_INFO:
256                         break;
257                 default:
258                         netdev_err(priv->net_dev,
259                                    "unknown Element ID=%d\n", *bp);
260                         break;
261                 }
262
263                 offset += 2;    /* id & size field */
264                 offset += *(bp + 1);    /* +size offset */
265                 bp += (*(bp + 1) + 2);  /* pointer update */
266         }
267
268         return 0;
269 }
270
271 static
272 int hostif_data_indication_wpa(struct ks_wlan_private *priv,
273                                unsigned short auth_type)
274 {
275         struct ether_hdr *eth_hdr;
276         unsigned short eth_proto;
277         unsigned char recv_mic[8];
278         char buf[128];
279         unsigned long now;
280         struct mic_failure *mic_failure;
281         struct michael_mic michael_mic;
282         union iwreq_data wrqu;
283         unsigned int key_index = auth_type - 1;
284         struct wpa_key *key = &priv->wpa.key[key_index];
285
286         eth_hdr = (struct ether_hdr *)(priv->rxp);
287         eth_proto = ntohs(eth_hdr->h_proto);
288
289         if (eth_hdr->h_dest_snap != eth_hdr->h_source_snap) {
290                 netdev_err(priv->net_dev, "invalid data format\n");
291                 priv->nstats.rx_errors++;
292                 return -EINVAL;
293         }
294         if (((auth_type == TYPE_PMK1 &&
295               priv->wpa.pairwise_suite == IW_AUTH_CIPHER_TKIP) ||
296              (auth_type == TYPE_GMK1 &&
297               priv->wpa.group_suite == IW_AUTH_CIPHER_TKIP) ||
298              (auth_type == TYPE_GMK2 &&
299               priv->wpa.group_suite == IW_AUTH_CIPHER_TKIP)) &&
300             key->key_len) {
301                 netdev_dbg(priv->net_dev, "TKIP: protocol=%04X: size=%u\n",
302                            eth_proto, priv->rx_size);
303                 /* MIC save */
304                 memcpy(&recv_mic[0], (priv->rxp) + ((priv->rx_size) - 8), 8);
305                 priv->rx_size = priv->rx_size - 8;
306                 if (auth_type > 0 && auth_type < 4) {   /* auth_type check */
307                         michael_mic_function(&michael_mic, key->rx_mic_key,
308                                              priv->rxp, priv->rx_size,
309                                              0, michael_mic.result);
310                 }
311                 if (memcmp(michael_mic.result, recv_mic, 8) != 0) {
312                         now = jiffies;
313                         mic_failure = &priv->wpa.mic_failure;
314                         /* MIC FAILURE */
315                         if (mic_failure->last_failure_time &&
316                             (now - mic_failure->last_failure_time) / HZ >= 60) {
317                                 mic_failure->failure = 0;
318                         }
319                         netdev_err(priv->net_dev, "MIC FAILURE\n");
320                         if (mic_failure->failure == 0) {
321                                 mic_failure->failure = 1;
322                                 mic_failure->counter = 0;
323                         } else if (mic_failure->failure == 1) {
324                                 mic_failure->failure = 2;
325                                 mic_failure->counter =
326                                         (uint16_t)((now - mic_failure->last_failure_time) / HZ);
327                                 /*  range 1-60 */
328                                 if (!mic_failure->counter)
329                                         mic_failure->counter = 1;
330                         }
331                         priv->wpa.mic_failure.last_failure_time = now;
332
333                         /*  needed parameters: count, keyid, key type, TSC */
334                         sprintf(buf,
335                                 "MLME-MICHAELMICFAILURE.indication(keyid=%d %scast addr=%pM)",
336                                 key_index,
337                                 eth_hdr->h_dest[0] & 0x01 ? "broad" : "uni",
338                                 eth_hdr->h_source);
339                         memset(&wrqu, 0, sizeof(wrqu));
340                         wrqu.data.length = strlen(buf);
341                         wireless_send_event(priv->net_dev, IWEVCUSTOM, &wrqu,
342                                             buf);
343                         return -EINVAL;
344                 }
345         }
346         return 0;
347 }
348
349 static
350 void hostif_data_indication(struct ks_wlan_private *priv)
351 {
352         unsigned int rx_ind_size;       /* indicate data size */
353         struct sk_buff *skb;
354         u16 auth_type;
355         unsigned char temp[256];
356         struct ether_hdr *eth_hdr;
357         unsigned short eth_proto;
358         struct ieee802_1x_hdr *aa1x_hdr;
359         size_t size;
360         int ret;
361
362         /* min length check */
363         if (priv->rx_size <= ETH_HLEN) {
364                 priv->nstats.rx_errors++;
365                 return;
366         }
367
368         auth_type = get_word(priv);     /* AuthType */
369         get_word(priv); /* Reserve Area */
370
371         eth_hdr = (struct ether_hdr *)(priv->rxp);
372         eth_proto = ntohs(eth_hdr->h_proto);
373
374         /* source address check */
375         if (ether_addr_equal(&priv->eth_addr[0], eth_hdr->h_source)) {
376                 netdev_err(priv->net_dev, "invalid : source is own mac address !!\n");
377                 netdev_err(priv->net_dev,
378                            "eth_hdrernet->h_dest=%02X:%02X:%02X:%02X:%02X:%02X\n",
379                            eth_hdr->h_source[0], eth_hdr->h_source[1],
380                            eth_hdr->h_source[2], eth_hdr->h_source[3],
381                            eth_hdr->h_source[4], eth_hdr->h_source[5]);
382                 priv->nstats.rx_errors++;
383                 return;
384         }
385
386         /*  for WPA */
387         if (auth_type != TYPE_DATA && priv->wpa.rsn_enabled) {
388                 ret = hostif_data_indication_wpa(priv, auth_type);
389                 if (ret)
390                         return;
391         }
392
393         if ((priv->connect_status & FORCE_DISCONNECT) ||
394             priv->wpa.mic_failure.failure == 2) {
395                 return;
396         }
397
398         /* check 13th byte at rx data */
399         switch (*(priv->rxp + 12)) {
400         case LLC_SAP_SNAP:
401                 rx_ind_size = priv->rx_size - 6;
402                 skb = dev_alloc_skb(rx_ind_size);
403                 if (!skb) {
404                         priv->nstats.rx_dropped++;
405                         return;
406                 }
407                 netdev_dbg(priv->net_dev, "SNAP, rx_ind_size = %d\n",
408                            rx_ind_size);
409
410                 size = ETH_ALEN * 2;
411                 skb_put_data(skb, priv->rxp, size);
412
413                 /* (SNAP+UI..) skip */
414
415                 size = rx_ind_size - (ETH_ALEN * 2);
416                 skb_put_data(skb, &eth_hdr->h_proto, size);
417
418                 aa1x_hdr = (struct ieee802_1x_hdr *)(priv->rxp + ETHER_HDR_SIZE);
419                 break;
420         case LLC_SAP_NETBEUI:
421                 rx_ind_size = (priv->rx_size + 2);
422                 skb = dev_alloc_skb(rx_ind_size);
423                 if (!skb) {
424                         priv->nstats.rx_dropped++;
425                         return;
426                 }
427                 netdev_dbg(priv->net_dev, "NETBEUI/NetBIOS rx_ind_size=%d\n",
428                            rx_ind_size);
429
430                 /* 8802/FDDI MAC copy */
431                 skb_put_data(skb, priv->rxp, 12);
432
433                 /* NETBEUI size add */
434                 temp[0] = (((rx_ind_size - 12) >> 8) & 0xff);
435                 temp[1] = ((rx_ind_size - 12) & 0xff);
436                 skb_put_data(skb, temp, 2);
437
438                 /* copy after Type */
439                 skb_put_data(skb, priv->rxp + 12, rx_ind_size - 14);
440
441                 aa1x_hdr = (struct ieee802_1x_hdr *)(priv->rxp + 14);
442                 break;
443         default:        /* other rx data */
444                 netdev_err(priv->net_dev, "invalid data format\n");
445                 priv->nstats.rx_errors++;
446                 return;
447         }
448
449         if (aa1x_hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
450             priv->wpa.rsn_enabled)
451                 atomic_set(&priv->psstatus.snooze_guard, 1);
452
453         /* rx indication */
454         skb->dev = priv->net_dev;
455         skb->protocol = eth_type_trans(skb, skb->dev);
456         priv->nstats.rx_packets++;
457         priv->nstats.rx_bytes += rx_ind_size;
458         netif_rx(skb);
459 }
460
461 static
462 void hostif_mib_get_confirm(struct ks_wlan_private *priv)
463 {
464         struct net_device *dev = priv->net_dev;
465         u32 mib_status;
466         u32 mib_attribute;
467         u16 mib_val_size;
468         u16 mib_val_type;
469
470         mib_status = get_dword(priv);
471         mib_attribute = get_dword(priv);
472         mib_val_size = get_word(priv);
473         mib_val_type = get_word(priv);
474
475         if (mib_status) {
476                 netdev_err(priv->net_dev, "attribute=%08X, status=%08X\n",
477                            mib_attribute, mib_status);
478                 return;
479         }
480
481         switch (mib_attribute) {
482         case DOT11_MAC_ADDRESS:
483                 hostif_sme_enqueue(priv, SME_GET_MAC_ADDRESS);
484                 ether_addr_copy(priv->eth_addr, priv->rxp);
485                 priv->mac_address_valid = true;
486                 ether_addr_copy(dev->dev_addr, priv->eth_addr);
487                 netdev_info(dev, "MAC ADDRESS = %pM\n", priv->eth_addr);
488                 break;
489         case DOT11_PRODUCT_VERSION:
490                 priv->version_size = priv->rx_size;
491                 memcpy(priv->firmware_version, priv->rxp, priv->rx_size);
492                 priv->firmware_version[priv->rx_size] = '\0';
493                 netdev_info(dev, "firmware ver. = %s\n",
494                             priv->firmware_version);
495                 hostif_sme_enqueue(priv, SME_GET_PRODUCT_VERSION);
496                 /* wake_up_interruptible_all(&priv->confirm_wait); */
497                 complete(&priv->confirm_wait);
498                 break;
499         case LOCAL_GAIN:
500                 memcpy(&priv->gain, priv->rxp, sizeof(priv->gain));
501                 netdev_dbg(priv->net_dev, "tx_mode=%d, rx_mode=%d, tx_gain=%d, rx_gain=%d\n",
502                            priv->gain.tx_mode, priv->gain.rx_mode,
503                            priv->gain.tx_gain, priv->gain.rx_gain);
504                 break;
505         case LOCAL_EEPROM_SUM:
506                 memcpy(&priv->eeprom_sum, priv->rxp, sizeof(priv->eeprom_sum));
507                 if (priv->eeprom_sum.type != 0 &&
508                     priv->eeprom_sum.type != 1) {
509                         netdev_err(dev, "LOCAL_EEPROM_SUM error!\n");
510                         return;
511                 }
512                 priv->eeprom_checksum = (priv->eeprom_sum.type == 0) ?
513                                          EEPROM_CHECKSUM_NONE :
514                                          (priv->eeprom_sum.result == 0) ?
515                                          EEPROM_NG : EEPROM_OK;
516                 break;
517         default:
518                 netdev_err(priv->net_dev, "mib_attribute=%08x\n",
519                            (unsigned int)mib_attribute);
520                 break;
521         }
522 }
523
524 static
525 void hostif_mib_set_confirm(struct ks_wlan_private *priv)
526 {
527         u32 mib_status;
528         u32 mib_attribute;
529
530         mib_status = get_dword(priv);
531         mib_attribute = get_dword(priv);
532
533         if (mib_status) {
534                 /* in case of error */
535                 netdev_err(priv->net_dev, "error :: attribute=%08X, status=%08X\n",
536                            mib_attribute, mib_status);
537         }
538
539         switch (mib_attribute) {
540         case DOT11_RTS_THRESHOLD:
541                 hostif_sme_enqueue(priv, SME_RTS_THRESHOLD_CONFIRM);
542                 break;
543         case DOT11_FRAGMENTATION_THRESHOLD:
544                 hostif_sme_enqueue(priv, SME_FRAGMENTATION_THRESHOLD_CONFIRM);
545                 break;
546         case DOT11_WEP_DEFAULT_KEY_ID:
547                 if (!priv->wpa.wpa_enabled)
548                         hostif_sme_enqueue(priv, SME_WEP_INDEX_CONFIRM);
549                 break;
550         case DOT11_WEP_DEFAULT_KEY_VALUE1:
551                 if (priv->wpa.rsn_enabled)
552                         hostif_sme_enqueue(priv, SME_SET_PMK_TSC);
553                 else
554                         hostif_sme_enqueue(priv, SME_WEP_KEY1_CONFIRM);
555                 break;
556         case DOT11_WEP_DEFAULT_KEY_VALUE2:
557                 if (priv->wpa.rsn_enabled)
558                         hostif_sme_enqueue(priv, SME_SET_GMK1_TSC);
559                 else
560                         hostif_sme_enqueue(priv, SME_WEP_KEY2_CONFIRM);
561                 break;
562         case DOT11_WEP_DEFAULT_KEY_VALUE3:
563                 if (priv->wpa.rsn_enabled)
564                         hostif_sme_enqueue(priv, SME_SET_GMK2_TSC);
565                 else
566                         hostif_sme_enqueue(priv, SME_WEP_KEY3_CONFIRM);
567                 break;
568         case DOT11_WEP_DEFAULT_KEY_VALUE4:
569                 if (!priv->wpa.rsn_enabled)
570                         hostif_sme_enqueue(priv, SME_WEP_KEY4_CONFIRM);
571                 break;
572         case DOT11_PRIVACY_INVOKED:
573                 if (!priv->wpa.rsn_enabled)
574                         hostif_sme_enqueue(priv, SME_WEP_FLAG_CONFIRM);
575                 break;
576         case DOT11_RSN_ENABLED:
577                 hostif_sme_enqueue(priv, SME_RSN_ENABLED_CONFIRM);
578                 break;
579         case LOCAL_RSN_MODE:
580                 hostif_sme_enqueue(priv, SME_RSN_MODE_CONFIRM);
581                 break;
582         case LOCAL_MULTICAST_ADDRESS:
583                 hostif_sme_enqueue(priv, SME_MULTICAST_REQUEST);
584                 break;
585         case LOCAL_MULTICAST_FILTER:
586                 hostif_sme_enqueue(priv, SME_MULTICAST_CONFIRM);
587                 break;
588         case LOCAL_CURRENTADDRESS:
589                 priv->mac_address_valid = true;
590                 break;
591         case DOT11_RSN_CONFIG_MULTICAST_CIPHER:
592                 hostif_sme_enqueue(priv, SME_RSN_MCAST_CONFIRM);
593                 break;
594         case DOT11_RSN_CONFIG_UNICAST_CIPHER:
595                 hostif_sme_enqueue(priv, SME_RSN_UCAST_CONFIRM);
596                 break;
597         case DOT11_RSN_CONFIG_AUTH_SUITE:
598                 hostif_sme_enqueue(priv, SME_RSN_AUTH_CONFIRM);
599                 break;
600         case DOT11_GMK1_TSC:
601                 if (atomic_read(&priv->psstatus.snooze_guard))
602                         atomic_set(&priv->psstatus.snooze_guard, 0);
603                 break;
604         case DOT11_GMK2_TSC:
605                 if (atomic_read(&priv->psstatus.snooze_guard))
606                         atomic_set(&priv->psstatus.snooze_guard, 0);
607                 break;
608         case DOT11_PMK_TSC:
609         case LOCAL_PMK:
610         case LOCAL_GAIN:
611         case LOCAL_WPS_ENABLE:
612         case LOCAL_WPS_PROBE_REQ:
613         case LOCAL_REGION:
614         default:
615                 break;
616         }
617 }
618
619 static
620 void hostif_power_mgmt_confirm(struct ks_wlan_private *priv)
621 {
622         if (priv->reg.power_mgmt > POWER_MGMT_ACTIVE &&
623             priv->reg.operation_mode == MODE_INFRASTRUCTURE) {
624                 atomic_set(&priv->psstatus.confirm_wait, 0);
625                 priv->dev_state = DEVICE_STATE_SLEEP;
626                 ks_wlan_hw_power_save(priv);
627         } else {
628                 priv->dev_state = DEVICE_STATE_READY;
629         }
630 }
631
632 static
633 void hostif_sleep_confirm(struct ks_wlan_private *priv)
634 {
635         atomic_set(&priv->sleepstatus.doze_request, 1);
636         queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
637 }
638
639 static
640 void hostif_start_confirm(struct ks_wlan_private *priv)
641 {
642         union iwreq_data wrqu;
643
644         wrqu.data.length = 0;
645         wrqu.data.flags = 0;
646         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
647         if (is_connect_status(priv->connect_status)) {
648                 eth_zero_addr(wrqu.ap_addr.sa_data);
649                 wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
650         }
651         netdev_dbg(priv->net_dev, " scan_ind_count=%d\n", priv->scan_ind_count);
652         hostif_sme_enqueue(priv, SME_START_CONFIRM);
653 }
654
655 static
656 void hostif_connect_indication(struct ks_wlan_private *priv)
657 {
658         unsigned short connect_code;
659         unsigned int tmp = 0;
660         unsigned int old_status = priv->connect_status;
661         struct net_device *netdev = priv->net_dev;
662         union iwreq_data wrqu0;
663
664         connect_code = get_word(priv);
665
666         switch (connect_code) {
667         case RESULT_CONNECT:
668                 if (!(priv->connect_status & FORCE_DISCONNECT))
669                         netif_carrier_on(netdev);
670                 tmp = FORCE_DISCONNECT & priv->connect_status;
671                 priv->connect_status = tmp + CONNECT_STATUS;
672                 break;
673         case RESULT_DISCONNECT:
674                 netif_carrier_off(netdev);
675                 tmp = FORCE_DISCONNECT & priv->connect_status;
676                 priv->connect_status = tmp + DISCONNECT_STATUS;
677                 break;
678         default:
679                 netdev_dbg(priv->net_dev, "unknown connect_code=%d :: scan_ind_count=%d\n",
680                            connect_code, priv->scan_ind_count);
681                 netif_carrier_off(netdev);
682                 tmp = FORCE_DISCONNECT & priv->connect_status;
683                 priv->connect_status = tmp + DISCONNECT_STATUS;
684                 break;
685         }
686
687         get_current_ap(priv, (struct link_ap_info *)priv->rxp);
688         if (is_connect_status(priv->connect_status) &&
689             is_disconnect_status(old_status)) {
690                 /* for power save */
691                 atomic_set(&priv->psstatus.snooze_guard, 0);
692                 atomic_set(&priv->psstatus.confirm_wait, 0);
693         }
694         ks_wlan_do_power_save(priv);
695
696         wrqu0.data.length = 0;
697         wrqu0.data.flags = 0;
698         wrqu0.ap_addr.sa_family = ARPHRD_ETHER;
699         if (is_disconnect_status(priv->connect_status) &&
700             is_connect_status(old_status)) {
701                 eth_zero_addr(wrqu0.ap_addr.sa_data);
702                 netdev_dbg(priv->net_dev, "disconnect :: scan_ind_count=%d\n",
703                            priv->scan_ind_count);
704                 wireless_send_event(netdev, SIOCGIWAP, &wrqu0, NULL);
705         }
706         priv->scan_ind_count = 0;
707 }
708
709 static
710 void hostif_scan_indication(struct ks_wlan_private *priv)
711 {
712         int i;
713         struct ap_info *ap_info;
714
715         netdev_dbg(priv->net_dev,
716                    "scan_ind_count = %d\n", priv->scan_ind_count);
717         ap_info = (struct ap_info *)(priv->rxp);
718
719         if (priv->scan_ind_count) {
720                 /* bssid check */
721                 for (i = 0; i < priv->aplist.size; i++) {
722                         u8 *bssid = priv->aplist.ap[i].bssid;
723
724                         if (ether_addr_equal(ap_info->bssid, bssid))
725                                 continue;
726
727                         if (ap_info->frame_type == IEEE80211_STYPE_PROBE_RESP)
728                                 get_ap_information(priv, ap_info,
729                                                    &priv->aplist.ap[i]);
730                         return;
731                 }
732         }
733         priv->scan_ind_count++;
734         if (priv->scan_ind_count < LOCAL_APLIST_MAX + 1) {
735                 netdev_dbg(priv->net_dev, " scan_ind_count=%d :: aplist.size=%d\n",
736                         priv->scan_ind_count, priv->aplist.size);
737                 get_ap_information(priv, (struct ap_info *)(priv->rxp),
738                                    &(priv->aplist.ap[priv->scan_ind_count - 1]));
739                 priv->aplist.size = priv->scan_ind_count;
740         } else {
741                 netdev_dbg(priv->net_dev, " count over :: scan_ind_count=%d\n",
742                            priv->scan_ind_count);
743         }
744 }
745
746 static
747 void hostif_stop_confirm(struct ks_wlan_private *priv)
748 {
749         unsigned int tmp = 0;
750         unsigned int old_status = priv->connect_status;
751         struct net_device *netdev = priv->net_dev;
752         union iwreq_data wrqu0;
753
754         if (priv->dev_state == DEVICE_STATE_SLEEP)
755                 priv->dev_state = DEVICE_STATE_READY;
756
757         /* disconnect indication */
758         if (is_connect_status(priv->connect_status)) {
759                 netif_carrier_off(netdev);
760                 tmp = FORCE_DISCONNECT & priv->connect_status;
761                 priv->connect_status = tmp | DISCONNECT_STATUS;
762                 netdev_info(netdev, "IWEVENT: disconnect\n");
763
764                 wrqu0.data.length = 0;
765                 wrqu0.data.flags = 0;
766                 wrqu0.ap_addr.sa_family = ARPHRD_ETHER;
767                 if (is_disconnect_status(priv->connect_status) &&
768                     is_connect_status(old_status)) {
769                         eth_zero_addr(wrqu0.ap_addr.sa_data);
770                         netdev_info(netdev, "IWEVENT: disconnect\n");
771                         wireless_send_event(netdev, SIOCGIWAP, &wrqu0, NULL);
772                 }
773                 priv->scan_ind_count = 0;
774         }
775
776         hostif_sme_enqueue(priv, SME_STOP_CONFIRM);
777 }
778
779 static
780 void hostif_ps_adhoc_set_confirm(struct ks_wlan_private *priv)
781 {
782         priv->infra_status = 0; /* infrastructure mode cancel */
783         hostif_sme_enqueue(priv, SME_MODE_SET_CONFIRM);
784 }
785
786 static
787 void hostif_infrastructure_set_confirm(struct ks_wlan_private *priv)
788 {
789         u16 result_code;
790
791         result_code = get_word(priv);
792         priv->infra_status = 1; /* infrastructure mode set */
793         hostif_sme_enqueue(priv, SME_MODE_SET_CONFIRM);
794 }
795
796 static
797 void hostif_adhoc_set_confirm(struct ks_wlan_private *priv)
798 {
799         priv->infra_status = 1; /* infrastructure mode set */
800         hostif_sme_enqueue(priv, SME_MODE_SET_CONFIRM);
801 }
802
803 static
804 void hostif_associate_indication(struct ks_wlan_private *priv)
805 {
806         struct association_request *assoc_req;
807         struct association_response *assoc_resp;
808         unsigned char *pb;
809         union iwreq_data wrqu;
810         char buf[IW_CUSTOM_MAX];
811         char *pbuf = &buf[0];
812         int i;
813
814         static const char associnfo_leader0[] = "ASSOCINFO(ReqIEs=";
815         static const char associnfo_leader1[] = " RespIEs=";
816
817         assoc_req = (struct association_request *)(priv->rxp);
818         assoc_resp = (struct association_response *)(assoc_req + 1);
819         pb = (unsigned char *)(assoc_resp + 1);
820
821         memset(&wrqu, 0, sizeof(wrqu));
822         memcpy(pbuf, associnfo_leader0, sizeof(associnfo_leader0) - 1);
823         wrqu.data.length += sizeof(associnfo_leader0) - 1;
824         pbuf += sizeof(associnfo_leader0) - 1;
825
826         for (i = 0; i < le16_to_cpu(assoc_req->req_ies_size); i++)
827                 pbuf += sprintf(pbuf, "%02x", *(pb + i));
828         wrqu.data.length += (le16_to_cpu(assoc_req->req_ies_size)) * 2;
829
830         memcpy(pbuf, associnfo_leader1, sizeof(associnfo_leader1) - 1);
831         wrqu.data.length += sizeof(associnfo_leader1) - 1;
832         pbuf += sizeof(associnfo_leader1) - 1;
833
834         pb += le16_to_cpu(assoc_req->req_ies_size);
835         for (i = 0; i < le16_to_cpu(assoc_resp->resp_ies_size); i++)
836                 pbuf += sprintf(pbuf, "%02x", *(pb + i));
837         wrqu.data.length += (le16_to_cpu(assoc_resp->resp_ies_size)) * 2;
838
839         pbuf += sprintf(pbuf, ")");
840         wrqu.data.length += 1;
841
842         wireless_send_event(priv->net_dev, IWEVCUSTOM, &wrqu, buf);
843 }
844
845 static
846 void hostif_bss_scan_confirm(struct ks_wlan_private *priv)
847 {
848         unsigned int result_code;
849         struct net_device *dev = priv->net_dev;
850         union iwreq_data wrqu;
851
852         result_code = get_dword(priv);
853         netdev_dbg(priv->net_dev, "result=%d :: scan_ind_count=%d\n",
854                    result_code, priv->scan_ind_count);
855
856         priv->sme_i.sme_flag &= ~SME_AP_SCAN;
857         hostif_sme_enqueue(priv, SME_BSS_SCAN_CONFIRM);
858
859         wrqu.data.length = 0;
860         wrqu.data.flags = 0;
861         wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
862         priv->scan_ind_count = 0;
863 }
864
865 static
866 void hostif_phy_information_confirm(struct ks_wlan_private *priv)
867 {
868         struct iw_statistics *wstats = &priv->wstats;
869         unsigned char rssi, signal, noise;
870         unsigned char link_speed;
871         unsigned int transmitted_frame_count, received_fragment_count;
872         unsigned int failed_count, fcs_error_count;
873
874         rssi = get_byte(priv);
875         signal = get_byte(priv);
876         noise = get_byte(priv);
877         link_speed = get_byte(priv);
878         transmitted_frame_count = get_dword(priv);
879         received_fragment_count = get_dword(priv);
880         failed_count = get_dword(priv);
881         fcs_error_count = get_dword(priv);
882
883         netdev_dbg(priv->net_dev, "phyinfo confirm rssi=%d signal=%d\n",
884                    rssi, signal);
885         priv->current_rate = (link_speed & RATE_MASK);
886         wstats->qual.qual = signal;
887         wstats->qual.level = 256 - rssi;
888         wstats->qual.noise = 0; /* invalid noise value */
889         wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
890
891         netdev_dbg(priv->net_dev, "\n    rssi=%u\n"
892                    "    signal=%u\n"
893                    "    link_speed=%ux500Kbps\n"
894                    "    transmitted_frame_count=%u\n"
895                    "    received_fragment_count=%u\n"
896                    "    failed_count=%u\n"
897                    "    fcs_error_count=%u\n",
898                    rssi, signal, link_speed, transmitted_frame_count,
899                    received_fragment_count, failed_count, fcs_error_count);
900         /* wake_up_interruptible_all(&priv->confirm_wait); */
901         complete(&priv->confirm_wait);
902 }
903
904 static
905 void hostif_mic_failure_confirm(struct ks_wlan_private *priv)
906 {
907         netdev_dbg(priv->net_dev, "mic_failure=%u\n",
908                    priv->wpa.mic_failure.failure);
909         hostif_sme_enqueue(priv, SME_MIC_FAILURE_CONFIRM);
910 }
911
912 static
913 void hostif_event_check(struct ks_wlan_private *priv)
914 {
915         u16 event;
916
917         event = get_word(priv);
918         switch (event) {
919         case HIF_DATA_IND:
920                 hostif_data_indication(priv);
921                 break;
922         case HIF_MIB_GET_CONF:
923                 hostif_mib_get_confirm(priv);
924                 break;
925         case HIF_MIB_SET_CONF:
926                 hostif_mib_set_confirm(priv);
927                 break;
928         case HIF_POWER_MGMT_CONF:
929                 hostif_power_mgmt_confirm(priv);
930                 break;
931         case HIF_SLEEP_CONF:
932                 hostif_sleep_confirm(priv);
933                 break;
934         case HIF_START_CONF:
935                 hostif_start_confirm(priv);
936                 break;
937         case HIF_CONNECT_IND:
938                 hostif_connect_indication(priv);
939                 break;
940         case HIF_STOP_CONF:
941                 hostif_stop_confirm(priv);
942                 break;
943         case HIF_PS_ADH_SET_CONF:
944                 hostif_ps_adhoc_set_confirm(priv);
945                 break;
946         case HIF_INFRA_SET_CONF:
947         case HIF_INFRA_SET2_CONF:
948                 hostif_infrastructure_set_confirm(priv);
949                 break;
950         case HIF_ADH_SET_CONF:
951         case HIF_ADH_SET2_CONF:
952                 hostif_adhoc_set_confirm(priv);
953                 break;
954         case HIF_ASSOC_INFO_IND:
955                 hostif_associate_indication(priv);
956                 break;
957         case HIF_MIC_FAILURE_CONF:
958                 hostif_mic_failure_confirm(priv);
959                 break;
960         case HIF_SCAN_CONF:
961                 hostif_bss_scan_confirm(priv);
962                 break;
963         case HIF_PHY_INFO_CONF:
964         case HIF_PHY_INFO_IND:
965                 hostif_phy_information_confirm(priv);
966                 break;
967         case HIF_SCAN_IND:
968                 hostif_scan_indication(priv);
969                 break;
970         case HIF_AP_SET_CONF:
971         default:
972                 netdev_err(priv->net_dev, "undefined event[%04X]\n", event);
973                 /* wake_up_all(&priv->confirm_wait); */
974                 complete(&priv->confirm_wait);
975                 break;
976         }
977
978         /* add event to hostt buffer */
979         priv->hostt.buff[priv->hostt.qtail] = event;
980         priv->hostt.qtail = (priv->hostt.qtail + 1) % SME_EVENT_BUFF_SIZE;
981 }
982
983 /* allocate size bytes, set header size and event */
984 static void *hostif_generic_request(size_t size, int event)
985 {
986         struct hostif_hdr *p;
987
988         p = kzalloc(hif_align_size(size), GFP_ATOMIC);
989         if (!p)
990                 return NULL;
991
992         p->size = cpu_to_le16((u16)(size - sizeof(p->size)));
993         p->event = cpu_to_le16(event);
994
995         return p;
996 }
997
998 int hostif_data_request(struct ks_wlan_private *priv, struct sk_buff *skb)
999 {
1000         unsigned int skb_len = 0;
1001         unsigned char *buffer = NULL;
1002         unsigned int length = 0;
1003         struct hostif_data_request *pp;
1004         unsigned char *p;
1005         int result = 0;
1006         unsigned short eth_proto;
1007         struct ether_hdr *eth_hdr;
1008         struct michael_mic michael_mic;
1009         unsigned short keyinfo = 0;
1010         struct ieee802_1x_hdr *aa1x_hdr;
1011         struct wpa_eapol_key *eap_key;
1012         struct ethhdr *eth;
1013         size_t size;
1014         int ret;
1015
1016         skb_len = skb->len;
1017         if (skb_len > ETH_FRAME_LEN) {
1018                 netdev_err(priv->net_dev, "bad length skb_len=%d\n", skb_len);
1019                 ret = -EOVERFLOW;
1020                 goto err_kfree_skb;
1021         }
1022
1023         if (is_disconnect_status(priv->connect_status) ||
1024             (priv->connect_status & FORCE_DISCONNECT) ||
1025             priv->wpa.mic_failure.stop) {
1026                 if (netif_queue_stopped(priv->net_dev))
1027                         netif_wake_queue(priv->net_dev);
1028                 if (skb)
1029                         dev_kfree_skb(skb);
1030
1031                 return 0;
1032         }
1033
1034         /* power save wakeup */
1035         if (atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
1036                 if (!netif_queue_stopped(priv->net_dev))
1037                         netif_stop_queue(priv->net_dev);
1038         }
1039
1040         size = sizeof(*pp) + 6 + skb_len + 8;
1041         pp = kmalloc(hif_align_size(size), GFP_ATOMIC);
1042         if (!pp) {
1043                 ret = -ENOMEM;
1044                 goto err_kfree_skb;
1045         }
1046
1047         p = (unsigned char *)pp->data;
1048
1049         buffer = skb->data;
1050         length = skb->len;
1051
1052         /* skb check */
1053         eth = (struct ethhdr *)skb->data;
1054         if (!ether_addr_equal(&priv->eth_addr[0], eth->h_source)) {
1055                 netdev_err(priv->net_dev,
1056                            "Invalid mac address: ethernet->h_source=%pM\n",
1057                            eth->h_source);
1058                 ret = -ENXIO;
1059                 goto err_kfree;
1060         }
1061
1062         /* dest and src MAC address copy */
1063         size = ETH_ALEN * 2;
1064         memcpy(p, buffer, size);
1065         p += size;
1066         buffer += size;
1067         length -= size;
1068
1069         /* EtherType/Length check */
1070         if (*(buffer + 1) + (*buffer << 8) > 1500) {
1071                 /* ProtocolEAP = *(buffer+1) + (*buffer << 8); */
1072                 /* SAP/CTL/OUI(6 byte) add */
1073                 *p++ = 0xAA;    /* DSAP */
1074                 *p++ = 0xAA;    /* SSAP */
1075                 *p++ = 0x03;    /* CTL */
1076                 *p++ = 0x00;    /* OUI ("000000") */
1077                 *p++ = 0x00;    /* OUI ("000000") */
1078                 *p++ = 0x00;    /* OUI ("000000") */
1079                 skb_len += 6;
1080         } else {
1081                 /* Length(2 byte) delete */
1082                 buffer += 2;
1083                 length -= 2;
1084                 skb_len -= 2;
1085         }
1086
1087         /* pp->data copy */
1088         memcpy(p, buffer, length);
1089
1090         p += length;
1091
1092         /* for WPA */
1093         eth_hdr = (struct ether_hdr *)&pp->data[0];
1094         eth_proto = ntohs(eth_hdr->h_proto);
1095
1096         /* for MIC FAILURE REPORT check */
1097         if (eth_proto == ETH_P_PAE &&
1098             priv->wpa.mic_failure.failure > 0) {
1099                 aa1x_hdr = (struct ieee802_1x_hdr *)(eth_hdr + 1);
1100                 if (aa1x_hdr->type == IEEE802_1X_TYPE_EAPOL_KEY) {
1101                         eap_key = (struct wpa_eapol_key *)(aa1x_hdr + 1);
1102                         keyinfo = ntohs(eap_key->key_info);
1103                 }
1104         }
1105
1106         if (priv->wpa.rsn_enabled && priv->wpa.key[0].key_len) {
1107                 /* no encryption */
1108                 if (eth_proto == ETH_P_PAE &&
1109                     priv->wpa.key[1].key_len == 0 &&
1110                     priv->wpa.key[2].key_len == 0 &&
1111                     priv->wpa.key[3].key_len == 0) {
1112                         pp->auth_type = cpu_to_le16((uint16_t)TYPE_AUTH);
1113                 } else {
1114                         if (priv->wpa.pairwise_suite == IW_AUTH_CIPHER_TKIP) {
1115                                 michael_mic_function(&michael_mic,
1116                                                      priv->wpa.key[0].tx_mic_key,
1117                                                      &pp->data[0], skb_len,
1118                                                      0, michael_mic.result);
1119                                 memcpy(p, michael_mic.result, 8);
1120                                 length += 8;
1121                                 skb_len += 8;
1122                                 p += 8;
1123                                 pp->auth_type =
1124                                     cpu_to_le16((uint16_t)TYPE_DATA);
1125
1126                         } else if (priv->wpa.pairwise_suite ==
1127                                    IW_AUTH_CIPHER_CCMP) {
1128                                 pp->auth_type =
1129                                     cpu_to_le16((uint16_t)TYPE_DATA);
1130                         }
1131                 }
1132         } else {
1133                 if (eth_proto == ETH_P_PAE)
1134                         pp->auth_type = cpu_to_le16((uint16_t)TYPE_AUTH);
1135                 else
1136                         pp->auth_type = cpu_to_le16((uint16_t)TYPE_DATA);
1137         }
1138
1139         /* header value set */
1140         pp->header.size =
1141             cpu_to_le16((uint16_t)
1142                         (sizeof(*pp) - sizeof(pp->header.size) + skb_len));
1143         pp->header.event = cpu_to_le16((uint16_t)HIF_DATA_REQ);
1144
1145         /* tx request */
1146         result = ks_wlan_hw_tx(priv, pp, hif_align_size(sizeof(*pp) + skb_len),
1147                                send_packet_complete, skb);
1148
1149         /* MIC FAILURE REPORT check */
1150         if (eth_proto == ETH_P_PAE &&
1151             priv->wpa.mic_failure.failure > 0) {
1152                 if (keyinfo & WPA_KEY_INFO_ERROR &&
1153                     keyinfo & WPA_KEY_INFO_REQUEST) {
1154                         netdev_err(priv->net_dev,
1155                                    "MIC ERROR Report SET : %04X\n", keyinfo);
1156                         hostif_sme_enqueue(priv, SME_MIC_FAILURE_REQUEST);
1157                 }
1158                 if (priv->wpa.mic_failure.failure == 2)
1159                         priv->wpa.mic_failure.stop = 1;
1160         }
1161
1162         return result;
1163
1164 err_kfree:
1165         kfree(pp);
1166 err_kfree_skb:
1167         dev_kfree_skb(skb);
1168
1169         return ret;
1170 }
1171
1172 static inline void ps_confirm_wait_inc(struct ks_wlan_private *priv)
1173 {
1174         if (atomic_read(&priv->psstatus.status) > PS_ACTIVE_SET)
1175                 atomic_inc(&priv->psstatus.confirm_wait);
1176 }
1177
1178 static inline void send_request_to_device(struct ks_wlan_private *priv,
1179                                           void *data, size_t size)
1180 {
1181         ps_confirm_wait_inc(priv);
1182         ks_wlan_hw_tx(priv, data, size, NULL, NULL);
1183 }
1184
1185 static
1186 void hostif_mib_get_request(struct ks_wlan_private *priv,
1187                             unsigned long mib_attribute)
1188 {
1189         struct hostif_mib_get_request *pp;
1190
1191         pp = hostif_generic_request(sizeof(*pp), HIF_MIB_GET_REQ);
1192         if (!pp)
1193                 return;
1194
1195         pp->mib_attribute = cpu_to_le32((uint32_t)mib_attribute);
1196
1197         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1198 }
1199
1200 static void hostif_mib_set_request(struct ks_wlan_private *priv,
1201                                    enum mib_attribute attr,
1202                                    enum mib_data_type type,
1203                                    void *data, size_t size)
1204 {
1205         struct hostif_mib_set_request_t *pp;
1206
1207         if (priv->dev_state < DEVICE_STATE_BOOT)
1208                 return;
1209
1210         pp = hostif_generic_request(sizeof(*pp), HIF_MIB_SET_REQ);
1211         if (!pp)
1212                 return;
1213
1214         pp->mib_attribute = cpu_to_le32(attr);
1215         pp->mib_value.size = cpu_to_le16((u16)size);
1216         pp->mib_value.type = cpu_to_le16(type);
1217         memcpy(&pp->mib_value.body, data, size);
1218
1219         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp) + size));
1220 }
1221
1222 static inline void hostif_mib_set_request_int(struct ks_wlan_private *priv,
1223                                               enum mib_attribute attr, int val)
1224 {
1225         __le32 v = cpu_to_le32((u32)val);
1226         size_t size = sizeof(v);
1227
1228         hostif_mib_set_request(priv, attr, MIB_VALUE_TYPE_INT, &v, size);
1229 }
1230
1231 static inline void hostif_mib_set_request_bool(struct ks_wlan_private *priv,
1232                                                enum mib_attribute attr,
1233                                                bool val)
1234 {
1235         __le32 v = cpu_to_le32((u32)val);
1236         size_t size = sizeof(v);
1237
1238         hostif_mib_set_request(priv, attr, MIB_VALUE_TYPE_BOOL, &v, size);
1239 }
1240
1241 static inline void hostif_mib_set_request_ostring(struct ks_wlan_private *priv,
1242                                                   enum mib_attribute attr,
1243                                                   void *data, size_t size)
1244 {
1245         hostif_mib_set_request(priv, attr, MIB_VALUE_TYPE_OSTRING, data, size);
1246 }
1247
1248 static
1249 void hostif_start_request(struct ks_wlan_private *priv, unsigned char mode)
1250 {
1251         struct hostif_start_request *pp;
1252
1253         pp = hostif_generic_request(sizeof(*pp), HIF_START_REQ);
1254         if (!pp)
1255                 return;
1256
1257         pp->mode = cpu_to_le16((uint16_t)mode);
1258
1259         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1260
1261         priv->aplist.size = 0;
1262         priv->scan_ind_count = 0;
1263 }
1264
1265 static __le16 ks_wlan_cap(struct ks_wlan_private *priv)
1266 {
1267         u16 capability = 0x0000;
1268
1269         if (priv->reg.preamble == SHORT_PREAMBLE)
1270                 capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
1271
1272         capability &= ~(WLAN_CAPABILITY_PBCC);  /* pbcc not support */
1273
1274         if (priv->reg.phy_type != D_11B_ONLY_MODE) {
1275                 capability |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
1276                 capability &= ~(WLAN_CAPABILITY_DSSS_OFDM);
1277         }
1278
1279         return cpu_to_le16((uint16_t)capability);
1280 }
1281
1282 static void init_request(struct ks_wlan_private *priv,
1283                          struct hostif_request *req)
1284 {
1285         req->phy_type = cpu_to_le16((uint16_t)(priv->reg.phy_type));
1286         req->cts_mode = cpu_to_le16((uint16_t)(priv->reg.cts_mode));
1287         req->scan_type = cpu_to_le16((uint16_t)(priv->reg.scan_type));
1288         req->rate_set.size = priv->reg.rate_set.size;
1289         req->capability = ks_wlan_cap(priv);
1290         memcpy(&req->rate_set.body[0], &priv->reg.rate_set.body[0],
1291                priv->reg.rate_set.size);
1292 }
1293
1294 static
1295 void hostif_ps_adhoc_set_request(struct ks_wlan_private *priv)
1296 {
1297         struct hostif_ps_adhoc_set_request *pp;
1298
1299         pp = hostif_generic_request(sizeof(*pp), HIF_PS_ADH_SET_REQ);
1300         if (!pp)
1301                 return;
1302
1303         init_request(priv, &pp->request);
1304         pp->channel = cpu_to_le16((uint16_t)(priv->reg.channel));
1305
1306         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1307 }
1308
1309 static
1310 void hostif_infrastructure_set_request(struct ks_wlan_private *priv, int event)
1311 {
1312         struct hostif_infrastructure_set_request *pp;
1313
1314         pp = hostif_generic_request(sizeof(*pp), event);
1315         if (!pp)
1316                 return;
1317
1318         init_request(priv, &pp->request);
1319         pp->ssid.size = priv->reg.ssid.size;
1320         memcpy(&pp->ssid.body[0], &priv->reg.ssid.body[0], priv->reg.ssid.size);
1321         pp->beacon_lost_count =
1322             cpu_to_le16((uint16_t)(priv->reg.beacon_lost_count));
1323         pp->auth_type = cpu_to_le16((uint16_t)(priv->reg.authenticate_type));
1324
1325         pp->channel_list.body[0] = 1;
1326         pp->channel_list.body[1] = 8;
1327         pp->channel_list.body[2] = 2;
1328         pp->channel_list.body[3] = 9;
1329         pp->channel_list.body[4] = 3;
1330         pp->channel_list.body[5] = 10;
1331         pp->channel_list.body[6] = 4;
1332         pp->channel_list.body[7] = 11;
1333         pp->channel_list.body[8] = 5;
1334         pp->channel_list.body[9] = 12;
1335         pp->channel_list.body[10] = 6;
1336         pp->channel_list.body[11] = 13;
1337         pp->channel_list.body[12] = 7;
1338         if (priv->reg.phy_type == D_11G_ONLY_MODE) {
1339                 pp->channel_list.size = 13;
1340         } else {
1341                 pp->channel_list.body[13] = 14;
1342                 pp->channel_list.size = 14;
1343         }
1344
1345         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1346 }
1347
1348 static
1349 void hostif_adhoc_set_request(struct ks_wlan_private *priv)
1350 {
1351         struct hostif_adhoc_set_request *pp;
1352
1353         pp = hostif_generic_request(sizeof(*pp), HIF_ADH_SET_REQ);
1354         if (!pp)
1355                 return;
1356
1357         init_request(priv, &pp->request);
1358         pp->channel = cpu_to_le16((uint16_t)(priv->reg.channel));
1359         pp->ssid.size = priv->reg.ssid.size;
1360         memcpy(&pp->ssid.body[0], &priv->reg.ssid.body[0], priv->reg.ssid.size);
1361
1362         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1363 }
1364
1365 static
1366 void hostif_adhoc_set2_request(struct ks_wlan_private *priv)
1367 {
1368         struct hostif_adhoc_set2_request *pp;
1369
1370         pp = hostif_generic_request(sizeof(*pp), HIF_ADH_SET_REQ);
1371         if (!pp)
1372                 return;
1373
1374         init_request(priv, &pp->request);
1375         pp->ssid.size = priv->reg.ssid.size;
1376         memcpy(&pp->ssid.body[0], &priv->reg.ssid.body[0], priv->reg.ssid.size);
1377
1378         pp->channel_list.body[0] = priv->reg.channel;
1379         pp->channel_list.size = 1;
1380         memcpy(pp->bssid, priv->reg.bssid, ETH_ALEN);
1381
1382         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1383 }
1384
1385 static
1386 void hostif_stop_request(struct ks_wlan_private *priv)
1387 {
1388         struct hostif_stop_request *pp;
1389
1390         pp = hostif_generic_request(sizeof(*pp), HIF_STOP_REQ);
1391         if (!pp)
1392                 return;
1393
1394         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1395 }
1396
1397 static
1398 void hostif_phy_information_request(struct ks_wlan_private *priv)
1399 {
1400         struct hostif_phy_information_request *pp;
1401
1402         pp = hostif_generic_request(sizeof(*pp), HIF_PHY_INFO_REQ);
1403         if (!pp)
1404                 return;
1405
1406         if (priv->reg.phy_info_timer) {
1407                 pp->type = cpu_to_le16((uint16_t)TIME_TYPE);
1408                 pp->time = cpu_to_le16((uint16_t)(priv->reg.phy_info_timer));
1409         } else {
1410                 pp->type = cpu_to_le16((uint16_t)NORMAL_TYPE);
1411                 pp->time = cpu_to_le16((uint16_t)0);
1412         }
1413
1414         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1415 }
1416
1417 static
1418 void hostif_power_mgmt_request(struct ks_wlan_private *priv,
1419                                u32 mode, u32 wake_up, u32 receive_dtims)
1420 {
1421         struct hostif_power_mgmt_request *pp;
1422
1423         pp = hostif_generic_request(sizeof(*pp), HIF_POWER_MGMT_REQ);
1424         if (!pp)
1425                 return;
1426
1427         pp->mode = cpu_to_le32(mode);
1428         pp->wake_up = cpu_to_le32(wake_up);
1429         pp->receive_dtims = cpu_to_le32(receive_dtims);
1430
1431         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1432 }
1433
1434 static
1435 void hostif_sleep_request(struct ks_wlan_private *priv,
1436                           enum sleep_mode_type mode)
1437 {
1438         struct hostif_sleep_request *pp;
1439
1440         if (mode == SLP_SLEEP) {
1441                 pp = hostif_generic_request(sizeof(*pp), HIF_SLEEP_REQ);
1442                 if (!pp)
1443                         return;
1444
1445                 send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1446         } else if (mode == SLP_ACTIVE) {
1447                 atomic_set(&priv->sleepstatus.wakeup_request, 1);
1448                 queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
1449         } else {
1450                 netdev_err(priv->net_dev, "invalid mode %ld\n", (long)mode);
1451                 return;
1452         }
1453 }
1454
1455 static
1456 void hostif_bss_scan_request(struct ks_wlan_private *priv,
1457                              unsigned long scan_type, uint8_t *scan_ssid,
1458                              uint8_t scan_ssid_len)
1459 {
1460         struct hostif_bss_scan_request *pp;
1461
1462         pp = hostif_generic_request(sizeof(*pp), HIF_SCAN_REQ);
1463         if (!pp)
1464                 return;
1465
1466         pp->scan_type = scan_type;
1467
1468         pp->ch_time_min = cpu_to_le32((uint32_t)110);   /* default value */
1469         pp->ch_time_max = cpu_to_le32((uint32_t)130);   /* default value */
1470         pp->channel_list.body[0] = 1;
1471         pp->channel_list.body[1] = 8;
1472         pp->channel_list.body[2] = 2;
1473         pp->channel_list.body[3] = 9;
1474         pp->channel_list.body[4] = 3;
1475         pp->channel_list.body[5] = 10;
1476         pp->channel_list.body[6] = 4;
1477         pp->channel_list.body[7] = 11;
1478         pp->channel_list.body[8] = 5;
1479         pp->channel_list.body[9] = 12;
1480         pp->channel_list.body[10] = 6;
1481         pp->channel_list.body[11] = 13;
1482         pp->channel_list.body[12] = 7;
1483         if (priv->reg.phy_type == D_11G_ONLY_MODE) {
1484                 pp->channel_list.size = 13;
1485         } else {
1486                 pp->channel_list.body[13] = 14;
1487                 pp->channel_list.size = 14;
1488         }
1489         pp->ssid.size = 0;
1490
1491         /* specified SSID SCAN */
1492         if (scan_ssid_len > 0 && scan_ssid_len <= 32) {
1493                 pp->ssid.size = scan_ssid_len;
1494                 memcpy(&pp->ssid.body[0], scan_ssid, scan_ssid_len);
1495         }
1496
1497         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1498
1499         priv->aplist.size = 0;
1500         priv->scan_ind_count = 0;
1501 }
1502
1503 static
1504 void hostif_mic_failure_request(struct ks_wlan_private *priv,
1505                                 unsigned short failure_count,
1506                                 unsigned short timer)
1507 {
1508         struct hostif_mic_failure_request *pp;
1509
1510         pp = hostif_generic_request(sizeof(*pp), HIF_MIC_FAILURE_REQ);
1511         if (!pp)
1512                 return;
1513
1514         pp->failure_count = cpu_to_le16((uint16_t)failure_count);
1515         pp->timer = cpu_to_le16((uint16_t)timer);
1516
1517         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1518 }
1519
1520 /* Device I/O Receive indicate */
1521 static void devio_rec_ind(struct ks_wlan_private *priv, unsigned char *p,
1522                           unsigned int size)
1523 {
1524         if (!priv->is_device_open)
1525                 return;
1526
1527         spin_lock(&priv->dev_read_lock);
1528         priv->dev_data[atomic_read(&priv->rec_count)] = p;
1529         priv->dev_size[atomic_read(&priv->rec_count)] = size;
1530
1531         if (atomic_read(&priv->event_count) != DEVICE_STOCK_COUNT) {
1532                 /* rx event count inc */
1533                 atomic_inc(&priv->event_count);
1534         }
1535         atomic_inc(&priv->rec_count);
1536         if (atomic_read(&priv->rec_count) == DEVICE_STOCK_COUNT)
1537                 atomic_set(&priv->rec_count, 0);
1538
1539         wake_up_interruptible_all(&priv->devread_wait);
1540
1541         spin_unlock(&priv->dev_read_lock);
1542 }
1543
1544 void hostif_receive(struct ks_wlan_private *priv, unsigned char *p,
1545                     unsigned int size)
1546 {
1547         devio_rec_ind(priv, p, size);
1548
1549         priv->rxp = p;
1550         priv->rx_size = size;
1551
1552         if (get_word(priv) == priv->rx_size)
1553                 hostif_event_check(priv);
1554 }
1555
1556 static void hostif_sme_set_wep(struct ks_wlan_private *priv, int type)
1557 {
1558         switch (type) {
1559         case SME_WEP_INDEX_REQUEST:
1560                 hostif_mib_set_request_int(priv, DOT11_WEP_DEFAULT_KEY_ID,
1561                                            priv->reg.wep_index);
1562                 break;
1563         case SME_WEP_KEY1_REQUEST:
1564                 if (priv->wpa.wpa_enabled)
1565                         return;
1566                 hostif_mib_set_request_ostring(priv,
1567                                                DOT11_WEP_DEFAULT_KEY_VALUE1,
1568                                                &priv->reg.wep_key[0].val[0],
1569                                                priv->reg.wep_key[0].size);
1570                 break;
1571         case SME_WEP_KEY2_REQUEST:
1572                 if (priv->wpa.wpa_enabled)
1573                         return;
1574                 hostif_mib_set_request_ostring(priv,
1575                                                DOT11_WEP_DEFAULT_KEY_VALUE2,
1576                                                &priv->reg.wep_key[1].val[0],
1577                                                priv->reg.wep_key[1].size);
1578                 break;
1579         case SME_WEP_KEY3_REQUEST:
1580                 if (priv->wpa.wpa_enabled)
1581                         return;
1582                 hostif_mib_set_request_ostring(priv,
1583                                                DOT11_WEP_DEFAULT_KEY_VALUE3,
1584                                                &priv->reg.wep_key[2].val[0],
1585                                                priv->reg.wep_key[2].size);
1586                 break;
1587         case SME_WEP_KEY4_REQUEST:
1588                 if (priv->wpa.wpa_enabled)
1589                         return;
1590                 hostif_mib_set_request_ostring(priv,
1591                                                DOT11_WEP_DEFAULT_KEY_VALUE4,
1592                                                &priv->reg.wep_key[3].val[0],
1593                                                priv->reg.wep_key[3].size);
1594                 break;
1595         case SME_WEP_FLAG_REQUEST:
1596                 hostif_mib_set_request_bool(priv, DOT11_PRIVACY_INVOKED,
1597                                             priv->reg.privacy_invoked);
1598                 break;
1599         }
1600 }
1601
1602 struct wpa_suite {
1603         __le16 size;
1604         unsigned char suite[4][CIPHER_ID_LEN];
1605 } __packed;
1606
1607 struct rsn_mode {
1608         __le32 rsn_mode;
1609         __le16 rsn_capability;
1610 } __packed;
1611
1612 static void hostif_sme_set_rsn(struct ks_wlan_private *priv, int type)
1613 {
1614         struct wpa_suite wpa_suite;
1615         struct rsn_mode rsn_mode;
1616         size_t size;
1617         u32 mode;
1618         const u8 *buf = NULL;
1619
1620         memset(&wpa_suite, 0, sizeof(wpa_suite));
1621
1622         switch (type) {
1623         case SME_RSN_UCAST_REQUEST:
1624                 wpa_suite.size = cpu_to_le16((uint16_t)1);
1625                 switch (priv->wpa.pairwise_suite) {
1626                 case IW_AUTH_CIPHER_NONE:
1627                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1628                                 CIPHER_ID_WPA2_NONE : CIPHER_ID_WPA_NONE;
1629                         break;
1630                 case IW_AUTH_CIPHER_WEP40:
1631                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1632                                 CIPHER_ID_WPA2_WEP40 : CIPHER_ID_WPA_WEP40;
1633                         break;
1634                 case IW_AUTH_CIPHER_TKIP:
1635                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1636                                 CIPHER_ID_WPA2_TKIP : CIPHER_ID_WPA_TKIP;
1637                         break;
1638                 case IW_AUTH_CIPHER_CCMP:
1639                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1640                                 CIPHER_ID_WPA2_CCMP : CIPHER_ID_WPA_CCMP;
1641                         break;
1642                 case IW_AUTH_CIPHER_WEP104:
1643                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1644                                 CIPHER_ID_WPA2_WEP104 : CIPHER_ID_WPA_WEP104;
1645                         break;
1646                 }
1647
1648                 if (buf)
1649                         memcpy(&wpa_suite.suite[0][0], buf, CIPHER_ID_LEN);
1650                 size = sizeof(wpa_suite.size) +
1651                        (CIPHER_ID_LEN * le16_to_cpu(wpa_suite.size));
1652                 hostif_mib_set_request_ostring(priv,
1653                                                DOT11_RSN_CONFIG_UNICAST_CIPHER,
1654                                                &wpa_suite, size);
1655                 break;
1656         case SME_RSN_MCAST_REQUEST:
1657                 switch (priv->wpa.group_suite) {
1658                 case IW_AUTH_CIPHER_NONE:
1659                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1660                                 CIPHER_ID_WPA2_NONE : CIPHER_ID_WPA_NONE;
1661                         break;
1662                 case IW_AUTH_CIPHER_WEP40:
1663                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1664                                 CIPHER_ID_WPA2_WEP40 : CIPHER_ID_WPA_WEP40;
1665                         break;
1666                 case IW_AUTH_CIPHER_TKIP:
1667                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1668                                 CIPHER_ID_WPA2_TKIP : CIPHER_ID_WPA_TKIP;
1669                         break;
1670                 case IW_AUTH_CIPHER_CCMP:
1671                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1672                                 CIPHER_ID_WPA2_CCMP : CIPHER_ID_WPA_CCMP;
1673                         break;
1674                 case IW_AUTH_CIPHER_WEP104:
1675                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1676                                 CIPHER_ID_WPA2_WEP104 : CIPHER_ID_WPA_WEP104;
1677                         break;
1678                 }
1679                 if (buf)
1680                         memcpy(&wpa_suite.suite[0][0], buf, CIPHER_ID_LEN);
1681                 hostif_mib_set_request_ostring(priv,
1682                                                DOT11_RSN_CONFIG_MULTICAST_CIPHER,
1683                                                &wpa_suite.suite[0][0],
1684                                                CIPHER_ID_LEN);
1685                 break;
1686         case SME_RSN_AUTH_REQUEST:
1687                 wpa_suite.size = cpu_to_le16((uint16_t)1);
1688                 switch (priv->wpa.key_mgmt_suite) {
1689                 case IW_AUTH_KEY_MGMT_802_1X:
1690                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1691                                 KEY_MGMT_ID_WPA2_1X : KEY_MGMT_ID_WPA_1X;
1692                         break;
1693                 case IW_AUTH_KEY_MGMT_PSK:
1694                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1695                                 KEY_MGMT_ID_WPA2_PSK : KEY_MGMT_ID_WPA_PSK;
1696                         break;
1697                 case 0:
1698                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1699                                 KEY_MGMT_ID_WPA2_NONE : KEY_MGMT_ID_WPA_NONE;
1700                         break;
1701                 case 4:
1702                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1703                                 KEY_MGMT_ID_WPA2_WPANONE :
1704                                 KEY_MGMT_ID_WPA_WPANONE;
1705                         break;
1706                 }
1707
1708                 if (buf)
1709                         memcpy(&wpa_suite.suite[0][0], buf, KEY_MGMT_ID_LEN);
1710                 size = sizeof(wpa_suite.size) +
1711                        (KEY_MGMT_ID_LEN * le16_to_cpu(wpa_suite.size));
1712                 hostif_mib_set_request_ostring(priv,
1713                                                DOT11_RSN_CONFIG_AUTH_SUITE,
1714                                                &wpa_suite, size);
1715                 break;
1716         case SME_RSN_ENABLED_REQUEST:
1717                 hostif_mib_set_request_bool(priv, DOT11_RSN_ENABLED,
1718                                             priv->wpa.rsn_enabled);
1719                 break;
1720         case SME_RSN_MODE_REQUEST:
1721                 mode = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1722                         RSN_MODE_WPA2 :
1723                         (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA) ?
1724                          RSN_MODE_WPA : RSN_MODE_NONE;
1725                 rsn_mode.rsn_mode = cpu_to_le32(mode);
1726                 rsn_mode.rsn_capability = cpu_to_le16((uint16_t)0);
1727                 hostif_mib_set_request_ostring(priv, LOCAL_RSN_MODE,
1728                                                &rsn_mode, sizeof(rsn_mode));
1729                 break;
1730         }
1731 }
1732
1733 static
1734 void hostif_sme_mode_setup(struct ks_wlan_private *priv)
1735 {
1736         unsigned char rate_size;
1737         unsigned char rate_octet[RATE_SET_MAX_SIZE];
1738         int i = 0;
1739
1740         /* rate setting if rate segging is auto for changing phy_type (#94) */
1741         if (priv->reg.tx_rate == TX_RATE_FULL_AUTO) {
1742                 if (priv->reg.phy_type == D_11B_ONLY_MODE) {
1743                         priv->reg.rate_set.body[3] = TX_RATE_11M;
1744                         priv->reg.rate_set.body[2] = TX_RATE_5M;
1745                         priv->reg.rate_set.body[1] = TX_RATE_2M | BASIC_RATE;
1746                         priv->reg.rate_set.body[0] = TX_RATE_1M | BASIC_RATE;
1747                         priv->reg.rate_set.size = 4;
1748                 } else {        /* D_11G_ONLY_MODE or D_11BG_COMPATIBLE_MODE */
1749                         priv->reg.rate_set.body[11] = TX_RATE_54M;
1750                         priv->reg.rate_set.body[10] = TX_RATE_48M;
1751                         priv->reg.rate_set.body[9] = TX_RATE_36M;
1752                         priv->reg.rate_set.body[8] = TX_RATE_18M;
1753                         priv->reg.rate_set.body[7] = TX_RATE_9M;
1754                         priv->reg.rate_set.body[6] = TX_RATE_24M | BASIC_RATE;
1755                         priv->reg.rate_set.body[5] = TX_RATE_12M | BASIC_RATE;
1756                         priv->reg.rate_set.body[4] = TX_RATE_6M | BASIC_RATE;
1757                         priv->reg.rate_set.body[3] = TX_RATE_11M | BASIC_RATE;
1758                         priv->reg.rate_set.body[2] = TX_RATE_5M | BASIC_RATE;
1759                         priv->reg.rate_set.body[1] = TX_RATE_2M | BASIC_RATE;
1760                         priv->reg.rate_set.body[0] = TX_RATE_1M | BASIC_RATE;
1761                         priv->reg.rate_set.size = 12;
1762                 }
1763         }
1764
1765         /* rate mask by phy setting */
1766         if (priv->reg.phy_type == D_11B_ONLY_MODE) {
1767                 for (i = 0; i < priv->reg.rate_set.size; i++) {
1768                         if (!is_11b_rate(priv->reg.rate_set.body[i]))
1769                                 break;
1770
1771                         if ((priv->reg.rate_set.body[i] & RATE_MASK) >= TX_RATE_5M) {
1772                                 rate_octet[i] = priv->reg.rate_set.body[i] &
1773                                                 RATE_MASK;
1774                         } else {
1775                                 rate_octet[i] = priv->reg.rate_set.body[i];
1776                         }
1777                 }
1778
1779         } else {        /* D_11G_ONLY_MODE or D_11BG_COMPATIBLE_MODE */
1780                 for (i = 0; i < priv->reg.rate_set.size; i++) {
1781                         if (!is_11bg_rate(priv->reg.rate_set.body[i]))
1782                                 break;
1783
1784                         if (is_ofdm_ext_rate(priv->reg.rate_set.body[i])) {
1785                                 rate_octet[i] = priv->reg.rate_set.body[i] &
1786                                                 RATE_MASK;
1787                         } else {
1788                                 rate_octet[i] = priv->reg.rate_set.body[i];
1789                         }
1790                 }
1791         }
1792         rate_size = i;
1793         if (rate_size == 0) {
1794                 if (priv->reg.phy_type == D_11G_ONLY_MODE)
1795                         rate_octet[0] = TX_RATE_6M | BASIC_RATE;
1796                 else
1797                         rate_octet[0] = TX_RATE_2M | BASIC_RATE;
1798                 rate_size = 1;
1799         }
1800
1801         /* rate set update */
1802         priv->reg.rate_set.size = rate_size;
1803         memcpy(&priv->reg.rate_set.body[0], &rate_octet[0], rate_size);
1804
1805         switch (priv->reg.operation_mode) {
1806         case MODE_PSEUDO_ADHOC:
1807                 hostif_ps_adhoc_set_request(priv);
1808                 break;
1809         case MODE_INFRASTRUCTURE:
1810                 if (!is_valid_ether_addr((u8 *)priv->reg.bssid)) {
1811                         hostif_infrastructure_set_request(priv,
1812                                                           HIF_INFRA_SET_REQ);
1813                 } else {
1814                         hostif_infrastructure_set_request(priv,
1815                                                           HIF_INFRA_SET2_REQ);
1816                         netdev_dbg(priv->net_dev,
1817                                    "Infra bssid = %pM\n", priv->reg.bssid);
1818                 }
1819                 break;
1820         case MODE_ADHOC:
1821                 if (!is_valid_ether_addr((u8 *)priv->reg.bssid)) {
1822                         hostif_adhoc_set_request(priv);
1823                 } else {
1824                         hostif_adhoc_set2_request(priv);
1825                         netdev_dbg(priv->net_dev,
1826                                    "Adhoc bssid = %pM\n", priv->reg.bssid);
1827                 }
1828                 break;
1829         default:
1830                 break;
1831         }
1832 }
1833
1834 static
1835 void hostif_sme_multicast_set(struct ks_wlan_private *priv)
1836 {
1837         struct net_device *dev = priv->net_dev;
1838         int mc_count;
1839         struct netdev_hw_addr *ha;
1840         char set_address[NIC_MAX_MCAST_LIST * ETH_ALEN];
1841         int i = 0;
1842
1843         spin_lock(&priv->multicast_spin);
1844
1845         memset(set_address, 0, NIC_MAX_MCAST_LIST * ETH_ALEN);
1846
1847         if (dev->flags & IFF_PROMISC) {
1848                 hostif_mib_set_request_bool(priv, LOCAL_MULTICAST_FILTER,
1849                                             MCAST_FILTER_PROMISC);
1850                 goto spin_unlock;
1851         }
1852
1853         if ((netdev_mc_count(dev) > NIC_MAX_MCAST_LIST) ||
1854             (dev->flags & IFF_ALLMULTI)) {
1855                 hostif_mib_set_request_bool(priv, LOCAL_MULTICAST_FILTER,
1856                                             MCAST_FILTER_MCASTALL);
1857                 goto spin_unlock;
1858         }
1859
1860         if (priv->sme_i.sme_flag & SME_MULTICAST) {
1861                 mc_count = netdev_mc_count(dev);
1862                 netdev_for_each_mc_addr(ha, dev) {
1863                         ether_addr_copy(&set_address[i * ETH_ALEN], ha->addr);
1864                         i++;
1865                 }
1866                 priv->sme_i.sme_flag &= ~SME_MULTICAST;
1867                 hostif_mib_set_request_ostring(priv, LOCAL_MULTICAST_ADDRESS,
1868                                                &set_address[0],
1869                                                ETH_ALEN * mc_count);
1870         } else {
1871                 priv->sme_i.sme_flag |= SME_MULTICAST;
1872                 hostif_mib_set_request_bool(priv, LOCAL_MULTICAST_FILTER,
1873                                             MCAST_FILTER_MCAST);
1874         }
1875
1876 spin_unlock:
1877         spin_unlock(&priv->multicast_spin);
1878 }
1879
1880 static void hostif_sme_power_mgmt_set(struct ks_wlan_private *priv)
1881 {
1882         u32 mode, wake_up, receive_dtims;
1883
1884         if (priv->reg.power_mgmt != POWER_MGMT_SAVE1 &&
1885             priv->reg.power_mgmt != POWER_MGMT_SAVE2) {
1886                 mode = POWER_ACTIVE;
1887                 wake_up = 0;
1888                 receive_dtims = 0;
1889         } else {
1890                 mode = (priv->reg.operation_mode == MODE_INFRASTRUCTURE) ?
1891                         POWER_SAVE : POWER_ACTIVE;
1892                 wake_up = 0;
1893                 receive_dtims = (priv->reg.operation_mode == MODE_INFRASTRUCTURE &&
1894                                  priv->reg.power_mgmt == POWER_MGMT_SAVE2);
1895         }
1896
1897         hostif_power_mgmt_request(priv, mode, wake_up, receive_dtims);
1898 }
1899
1900 static void hostif_sme_sleep_set(struct ks_wlan_private *priv)
1901 {
1902         if (priv->sleep_mode != SLP_SLEEP &&
1903             priv->sleep_mode != SLP_ACTIVE)
1904                 return;
1905
1906         hostif_sleep_request(priv, priv->sleep_mode);
1907 }
1908
1909 static
1910 void hostif_sme_set_key(struct ks_wlan_private *priv, int type)
1911 {
1912         switch (type) {
1913         case SME_SET_FLAG:
1914                 hostif_mib_set_request_bool(priv, DOT11_PRIVACY_INVOKED,
1915                                             priv->reg.privacy_invoked);
1916                 break;
1917         case SME_SET_TXKEY:
1918                 hostif_mib_set_request_int(priv, DOT11_WEP_DEFAULT_KEY_ID,
1919                                            priv->wpa.txkey);
1920                 break;
1921         case SME_SET_KEY1:
1922                 hostif_mib_set_request_ostring(priv,
1923                                                DOT11_WEP_DEFAULT_KEY_VALUE1,
1924                                                &priv->wpa.key[0].key_val[0],
1925                                                priv->wpa.key[0].key_len);
1926                 break;
1927         case SME_SET_KEY2:
1928                 hostif_mib_set_request_ostring(priv,
1929                                                DOT11_WEP_DEFAULT_KEY_VALUE2,
1930                                                &priv->wpa.key[1].key_val[0],
1931                                                priv->wpa.key[1].key_len);
1932                 break;
1933         case SME_SET_KEY3:
1934                 hostif_mib_set_request_ostring(priv,
1935                                                DOT11_WEP_DEFAULT_KEY_VALUE3,
1936                                                &priv->wpa.key[2].key_val[0],
1937                                                priv->wpa.key[2].key_len);
1938                 break;
1939         case SME_SET_KEY4:
1940                 hostif_mib_set_request_ostring(priv,
1941                                                DOT11_WEP_DEFAULT_KEY_VALUE4,
1942                                                &priv->wpa.key[3].key_val[0],
1943                                                priv->wpa.key[3].key_len);
1944                 break;
1945         case SME_SET_PMK_TSC:
1946                 hostif_mib_set_request_ostring(priv, DOT11_PMK_TSC,
1947                                                &priv->wpa.key[0].rx_seq[0],
1948                                                WPA_RX_SEQ_LEN);
1949                 break;
1950         case SME_SET_GMK1_TSC:
1951                 hostif_mib_set_request_ostring(priv, DOT11_GMK1_TSC,
1952                                                &priv->wpa.key[1].rx_seq[0],
1953                                                WPA_RX_SEQ_LEN);
1954                 break;
1955         case SME_SET_GMK2_TSC:
1956                 hostif_mib_set_request_ostring(priv, DOT11_GMK2_TSC,
1957                                                &priv->wpa.key[2].rx_seq[0],
1958                                                WPA_RX_SEQ_LEN);
1959                 break;
1960         }
1961 }
1962
1963 static
1964 void hostif_sme_set_pmksa(struct ks_wlan_private *priv)
1965 {
1966         struct pmk_cache {
1967                 __le16 size;
1968                 struct {
1969                         u8 bssid[ETH_ALEN];
1970                         u8 pmkid[IW_PMKID_LEN];
1971                 } __packed list[PMK_LIST_MAX];
1972         } __packed pmkcache;
1973         struct pmk *pmk;
1974         size_t size;
1975         int i = 0;
1976
1977         list_for_each_entry(pmk, &priv->pmklist.head, list) {
1978                 if (i >= PMK_LIST_MAX)
1979                         break;
1980                 ether_addr_copy(pmkcache.list[i].bssid, pmk->bssid);
1981                 memcpy(pmkcache.list[i].pmkid, pmk->pmkid, IW_PMKID_LEN);
1982                 i++;
1983         }
1984         pmkcache.size = cpu_to_le16((uint16_t)(priv->pmklist.size));
1985         size = sizeof(priv->pmklist.size) +
1986                ((ETH_ALEN + IW_PMKID_LEN) * priv->pmklist.size);
1987         hostif_mib_set_request_ostring(priv, LOCAL_PMK, &pmkcache, size);
1988 }
1989
1990 /* execute sme */
1991 static void hostif_sme_execute(struct ks_wlan_private *priv, int event)
1992 {
1993         u16 failure;
1994
1995         switch (event) {
1996         case SME_START:
1997                 if (priv->dev_state == DEVICE_STATE_BOOT)
1998                         hostif_mib_get_request(priv, DOT11_MAC_ADDRESS);
1999                 break;
2000         case SME_MULTICAST_REQUEST:
2001                 hostif_sme_multicast_set(priv);
2002                 break;
2003         case SME_MACADDRESS_SET_REQUEST:
2004                 hostif_mib_set_request_ostring(priv, LOCAL_CURRENTADDRESS,
2005                                                &priv->eth_addr[0], ETH_ALEN);
2006                 break;
2007         case SME_BSS_SCAN_REQUEST:
2008                 hostif_bss_scan_request(priv, priv->reg.scan_type,
2009                                         priv->scan_ssid, priv->scan_ssid_len);
2010                 break;
2011         case SME_POW_MNGMT_REQUEST:
2012                 hostif_sme_power_mgmt_set(priv);
2013                 break;
2014         case SME_PHY_INFO_REQUEST:
2015                 hostif_phy_information_request(priv);
2016                 break;
2017         case SME_MIC_FAILURE_REQUEST:
2018                 failure = priv->wpa.mic_failure.failure;
2019                 if (failure != 1 && failure != 2) {
2020                         netdev_err(priv->net_dev,
2021                                    "SME_MIC_FAILURE_REQUEST: failure count=%u error?\n",
2022                                    failure);
2023                         return;
2024                 }
2025                 hostif_mic_failure_request(priv, failure - 1, (failure == 1) ?
2026                                             0 : priv->wpa.mic_failure.counter);
2027                 break;
2028         case SME_MIC_FAILURE_CONFIRM:
2029                 if (priv->wpa.mic_failure.failure == 2) {
2030                         if (priv->wpa.mic_failure.stop)
2031                                 priv->wpa.mic_failure.stop = 0;
2032                         priv->wpa.mic_failure.failure = 0;
2033                         hostif_start_request(priv, priv->reg.operation_mode);
2034                 }
2035                 break;
2036         case SME_GET_MAC_ADDRESS:
2037                 if (priv->dev_state == DEVICE_STATE_BOOT)
2038                         hostif_mib_get_request(priv, DOT11_PRODUCT_VERSION);
2039                 break;
2040         case SME_GET_PRODUCT_VERSION:
2041                 if (priv->dev_state == DEVICE_STATE_BOOT)
2042                         priv->dev_state = DEVICE_STATE_PREINIT;
2043                 break;
2044         case SME_STOP_REQUEST:
2045                 hostif_stop_request(priv);
2046                 break;
2047         case SME_RTS_THRESHOLD_REQUEST:
2048                 hostif_mib_set_request_int(priv, DOT11_RTS_THRESHOLD,
2049                                            priv->reg.rts);
2050                 break;
2051         case SME_FRAGMENTATION_THRESHOLD_REQUEST:
2052                 hostif_mib_set_request_int(priv, DOT11_FRAGMENTATION_THRESHOLD,
2053                                            priv->reg.fragment);
2054                 break;
2055         case SME_WEP_INDEX_REQUEST:
2056         case SME_WEP_KEY1_REQUEST:
2057         case SME_WEP_KEY2_REQUEST:
2058         case SME_WEP_KEY3_REQUEST:
2059         case SME_WEP_KEY4_REQUEST:
2060         case SME_WEP_FLAG_REQUEST:
2061                 hostif_sme_set_wep(priv, event);
2062                 break;
2063         case SME_RSN_UCAST_REQUEST:
2064         case SME_RSN_MCAST_REQUEST:
2065         case SME_RSN_AUTH_REQUEST:
2066         case SME_RSN_ENABLED_REQUEST:
2067         case SME_RSN_MODE_REQUEST:
2068                 hostif_sme_set_rsn(priv, event);
2069                 break;
2070         case SME_SET_FLAG:
2071         case SME_SET_TXKEY:
2072         case SME_SET_KEY1:
2073         case SME_SET_KEY2:
2074         case SME_SET_KEY3:
2075         case SME_SET_KEY4:
2076         case SME_SET_PMK_TSC:
2077         case SME_SET_GMK1_TSC:
2078         case SME_SET_GMK2_TSC:
2079                 hostif_sme_set_key(priv, event);
2080                 break;
2081         case SME_SET_PMKSA:
2082                 hostif_sme_set_pmksa(priv);
2083                 break;
2084         case SME_WPS_ENABLE_REQUEST:
2085                 hostif_mib_set_request_int(priv, LOCAL_WPS_ENABLE,
2086                                            priv->wps.wps_enabled);
2087                 break;
2088         case SME_WPS_PROBE_REQUEST:
2089                 hostif_mib_set_request_ostring(priv, LOCAL_WPS_PROBE_REQ,
2090                                                priv->wps.ie, priv->wps.ielen);
2091                 break;
2092         case SME_MODE_SET_REQUEST:
2093                 hostif_sme_mode_setup(priv);
2094                 break;
2095         case SME_SET_GAIN:
2096                 hostif_mib_set_request_ostring(priv, LOCAL_GAIN,
2097                                                &priv->gain, sizeof(priv->gain));
2098                 break;
2099         case SME_GET_GAIN:
2100                 hostif_mib_get_request(priv, LOCAL_GAIN);
2101                 break;
2102         case SME_GET_EEPROM_CKSUM:
2103                 priv->eeprom_checksum = EEPROM_FW_NOT_SUPPORT;  /* initialize */
2104                 hostif_mib_get_request(priv, LOCAL_EEPROM_SUM);
2105                 break;
2106         case SME_START_REQUEST:
2107                 hostif_start_request(priv, priv->reg.operation_mode);
2108                 break;
2109         case SME_START_CONFIRM:
2110                 /* for power save */
2111                 atomic_set(&priv->psstatus.snooze_guard, 0);
2112                 atomic_set(&priv->psstatus.confirm_wait, 0);
2113                 if (priv->dev_state == DEVICE_STATE_PREINIT)
2114                         priv->dev_state = DEVICE_STATE_INIT;
2115                 /* wake_up_interruptible_all(&priv->confirm_wait); */
2116                 complete(&priv->confirm_wait);
2117                 break;
2118         case SME_SLEEP_REQUEST:
2119                 hostif_sme_sleep_set(priv);
2120                 break;
2121         case SME_SET_REGION:
2122                 hostif_mib_set_request_int(priv, LOCAL_REGION, priv->region);
2123                 break;
2124         case SME_MULTICAST_CONFIRM:
2125         case SME_BSS_SCAN_CONFIRM:
2126         case SME_POW_MNGMT_CONFIRM:
2127         case SME_PHY_INFO_CONFIRM:
2128         case SME_STOP_CONFIRM:
2129         case SME_RTS_THRESHOLD_CONFIRM:
2130         case SME_FRAGMENTATION_THRESHOLD_CONFIRM:
2131         case SME_WEP_INDEX_CONFIRM:
2132         case SME_WEP_KEY1_CONFIRM:
2133         case SME_WEP_KEY2_CONFIRM:
2134         case SME_WEP_KEY3_CONFIRM:
2135         case SME_WEP_KEY4_CONFIRM:
2136         case SME_WEP_FLAG_CONFIRM:
2137         case SME_RSN_UCAST_CONFIRM:
2138         case SME_RSN_MCAST_CONFIRM:
2139         case SME_RSN_AUTH_CONFIRM:
2140         case SME_RSN_ENABLED_CONFIRM:
2141         case SME_RSN_MODE_CONFIRM:
2142         case SME_MODE_SET_CONFIRM:
2143         case SME_TERMINATE:
2144         default:
2145                 break;
2146         }
2147 }
2148
2149 static
2150 void hostif_sme_task(unsigned long dev)
2151 {
2152         struct ks_wlan_private *priv = (struct ks_wlan_private *)dev;
2153
2154         if (priv->dev_state < DEVICE_STATE_BOOT)
2155                 return;
2156
2157         if (cnt_smeqbody(priv) <= 0)
2158                 return;
2159
2160         hostif_sme_execute(priv, priv->sme_i.event_buff[priv->sme_i.qhead]);
2161         inc_smeqhead(priv);
2162         if (cnt_smeqbody(priv) > 0)
2163                 tasklet_schedule(&priv->sme_task);
2164 }
2165
2166 /* send to Station Management Entity module */
2167 void hostif_sme_enqueue(struct ks_wlan_private *priv, u16 event)
2168 {
2169         /* enqueue sme event */
2170         if (cnt_smeqbody(priv) < (SME_EVENT_BUFF_SIZE - 1)) {
2171                 priv->sme_i.event_buff[priv->sme_i.qtail] = event;
2172                 inc_smeqtail(priv);
2173         } else {
2174                 /* in case of buffer overflow */
2175                 netdev_err(priv->net_dev, "sme queue buffer overflow\n");
2176         }
2177
2178         tasklet_schedule(&priv->sme_task);
2179 }
2180
2181 static inline void hostif_aplist_init(struct ks_wlan_private *priv)
2182 {
2183         size_t size = LOCAL_APLIST_MAX * sizeof(struct local_ap);
2184
2185         priv->aplist.size = 0;
2186         memset(&priv->aplist.ap[0], 0, size);
2187 }
2188
2189 static inline void hostif_status_init(struct ks_wlan_private *priv)
2190 {
2191         priv->infra_status = 0;
2192         priv->current_rate = 4;
2193         priv->connect_status = DISCONNECT_STATUS;
2194 }
2195
2196 static inline void hostif_sme_init(struct ks_wlan_private *priv)
2197 {
2198         priv->sme_i.sme_status = SME_IDLE;
2199         priv->sme_i.qhead = 0;
2200         priv->sme_i.qtail = 0;
2201         spin_lock_init(&priv->sme_i.sme_spin);
2202         priv->sme_i.sme_flag = 0;
2203         tasklet_init(&priv->sme_task, hostif_sme_task, (unsigned long)priv);
2204 }
2205
2206 static inline void hostif_wpa_init(struct ks_wlan_private *priv)
2207 {
2208         memset(&priv->wpa, 0, sizeof(priv->wpa));
2209         priv->wpa.rsn_enabled = false;
2210         priv->wpa.mic_failure.failure = 0;
2211         priv->wpa.mic_failure.last_failure_time = 0;
2212         priv->wpa.mic_failure.stop = 0;
2213 }
2214
2215 static inline void hostif_power_save_init(struct ks_wlan_private *priv)
2216 {
2217         atomic_set(&priv->psstatus.status, PS_NONE);
2218         atomic_set(&priv->psstatus.confirm_wait, 0);
2219         atomic_set(&priv->psstatus.snooze_guard, 0);
2220         init_completion(&priv->psstatus.wakeup_wait);
2221         INIT_WORK(&priv->wakeup_work, ks_wlan_hw_wakeup_task);
2222 }
2223
2224 static inline void hostif_pmklist_init(struct ks_wlan_private *priv)
2225 {
2226         int i;
2227
2228         memset(&priv->pmklist, 0, sizeof(priv->pmklist));
2229         INIT_LIST_HEAD(&priv->pmklist.head);
2230         for (i = 0; i < PMK_LIST_MAX; i++)
2231                 INIT_LIST_HEAD(&priv->pmklist.pmk[i].list);
2232 }
2233
2234 static inline void hostif_counters_init(struct ks_wlan_private *priv)
2235 {
2236         priv->dev_count = 0;
2237         atomic_set(&priv->event_count, 0);
2238         atomic_set(&priv->rec_count, 0);
2239 }
2240
2241 int hostif_init(struct ks_wlan_private *priv)
2242 {
2243         hostif_aplist_init(priv);
2244         hostif_status_init(priv);
2245
2246         spin_lock_init(&priv->multicast_spin);
2247         spin_lock_init(&priv->dev_read_lock);
2248         init_waitqueue_head(&priv->devread_wait);
2249
2250         hostif_counters_init(priv);
2251         hostif_power_save_init(priv);
2252         hostif_wpa_init(priv);
2253         hostif_pmklist_init(priv);
2254         hostif_sme_init(priv);
2255
2256         return 0;
2257 }
2258
2259 void hostif_exit(struct ks_wlan_private *priv)
2260 {
2261         tasklet_kill(&priv->sme_task);
2262 }