kill dentry_update_name_case()
[sfrench/cifs-2.6.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                                         (u16)((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         u16 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         u32 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         u8 rssi, signal, noise;
870         u8 link_speed;
871         u32 transmitted_frame_count, received_fragment_count;
872         u32 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(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(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(TYPE_DATA);
1125
1126                         } else if (priv->wpa.pairwise_suite ==
1127                                    IW_AUTH_CIPHER_CCMP) {
1128                                 pp->auth_type =
1129                                     cpu_to_le16(TYPE_DATA);
1130                         }
1131                 }
1132         } else {
1133                 if (eth_proto == ETH_P_PAE)
1134                         pp->auth_type = cpu_to_le16(TYPE_AUTH);
1135                 else
1136                         pp->auth_type = cpu_to_le16(TYPE_DATA);
1137         }
1138
1139         /* header value set */
1140         pp->header.size =
1141             cpu_to_le16((sizeof(*pp) - sizeof(pp->header.size) + skb_len));
1142         pp->header.event = cpu_to_le16(HIF_DATA_REQ);
1143
1144         /* tx request */
1145         result = ks_wlan_hw_tx(priv, pp, hif_align_size(sizeof(*pp) + skb_len),
1146                                send_packet_complete, skb);
1147
1148         /* MIC FAILURE REPORT check */
1149         if (eth_proto == ETH_P_PAE &&
1150             priv->wpa.mic_failure.failure > 0) {
1151                 if (keyinfo & WPA_KEY_INFO_ERROR &&
1152                     keyinfo & WPA_KEY_INFO_REQUEST) {
1153                         netdev_err(priv->net_dev,
1154                                    "MIC ERROR Report SET : %04X\n", keyinfo);
1155                         hostif_sme_enqueue(priv, SME_MIC_FAILURE_REQUEST);
1156                 }
1157                 if (priv->wpa.mic_failure.failure == 2)
1158                         priv->wpa.mic_failure.stop = 1;
1159         }
1160
1161         return result;
1162
1163 err_kfree:
1164         kfree(pp);
1165 err_kfree_skb:
1166         dev_kfree_skb(skb);
1167
1168         return ret;
1169 }
1170
1171 static inline void ps_confirm_wait_inc(struct ks_wlan_private *priv)
1172 {
1173         if (atomic_read(&priv->psstatus.status) > PS_ACTIVE_SET)
1174                 atomic_inc(&priv->psstatus.confirm_wait);
1175 }
1176
1177 static inline void send_request_to_device(struct ks_wlan_private *priv,
1178                                           void *data, size_t size)
1179 {
1180         ps_confirm_wait_inc(priv);
1181         ks_wlan_hw_tx(priv, data, size, NULL, NULL);
1182 }
1183
1184 static void hostif_mib_get_request(struct ks_wlan_private *priv,
1185                                    u32 mib_attribute)
1186 {
1187         struct hostif_mib_get_request *pp;
1188
1189         pp = hostif_generic_request(sizeof(*pp), HIF_MIB_GET_REQ);
1190         if (!pp)
1191                 return;
1192
1193         pp->mib_attribute = cpu_to_le32(mib_attribute);
1194
1195         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1196 }
1197
1198 static void hostif_mib_set_request(struct ks_wlan_private *priv,
1199                                    enum mib_attribute attr,
1200                                    enum mib_data_type type,
1201                                    void *data, size_t size)
1202 {
1203         struct hostif_mib_set_request_t *pp;
1204
1205         if (priv->dev_state < DEVICE_STATE_BOOT)
1206                 return;
1207
1208         pp = hostif_generic_request(sizeof(*pp), HIF_MIB_SET_REQ);
1209         if (!pp)
1210                 return;
1211
1212         pp->mib_attribute = cpu_to_le32(attr);
1213         pp->mib_value.size = cpu_to_le16(size);
1214         pp->mib_value.type = cpu_to_le16(type);
1215         memcpy(&pp->mib_value.body, data, size);
1216
1217         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp) + size));
1218 }
1219
1220 static inline void hostif_mib_set_request_int(struct ks_wlan_private *priv,
1221                                               enum mib_attribute attr, int val)
1222 {
1223         __le32 v = cpu_to_le32(val);
1224         size_t size = sizeof(v);
1225
1226         hostif_mib_set_request(priv, attr, MIB_VALUE_TYPE_INT, &v, size);
1227 }
1228
1229 static inline void hostif_mib_set_request_bool(struct ks_wlan_private *priv,
1230                                                enum mib_attribute attr,
1231                                                bool val)
1232 {
1233         __le32 v = cpu_to_le32(val);
1234         size_t size = sizeof(v);
1235
1236         hostif_mib_set_request(priv, attr, MIB_VALUE_TYPE_BOOL, &v, size);
1237 }
1238
1239 static inline void hostif_mib_set_request_ostring(struct ks_wlan_private *priv,
1240                                                   enum mib_attribute attr,
1241                                                   void *data, size_t size)
1242 {
1243         hostif_mib_set_request(priv, attr, MIB_VALUE_TYPE_OSTRING, data, size);
1244 }
1245
1246 static
1247 void hostif_start_request(struct ks_wlan_private *priv, unsigned char mode)
1248 {
1249         struct hostif_start_request *pp;
1250
1251         pp = hostif_generic_request(sizeof(*pp), HIF_START_REQ);
1252         if (!pp)
1253                 return;
1254
1255         pp->mode = cpu_to_le16(mode);
1256
1257         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1258
1259         priv->aplist.size = 0;
1260         priv->scan_ind_count = 0;
1261 }
1262
1263 static __le16 ks_wlan_cap(struct ks_wlan_private *priv)
1264 {
1265         u16 capability = 0x0000;
1266
1267         if (priv->reg.preamble == SHORT_PREAMBLE)
1268                 capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
1269
1270         capability &= ~(WLAN_CAPABILITY_PBCC);  /* pbcc not support */
1271
1272         if (priv->reg.phy_type != D_11B_ONLY_MODE) {
1273                 capability |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
1274                 capability &= ~(WLAN_CAPABILITY_DSSS_OFDM);
1275         }
1276
1277         return cpu_to_le16(capability);
1278 }
1279
1280 static void init_request(struct ks_wlan_private *priv,
1281                          struct hostif_request *req)
1282 {
1283         req->phy_type = cpu_to_le16(priv->reg.phy_type);
1284         req->cts_mode = cpu_to_le16(priv->reg.cts_mode);
1285         req->scan_type = cpu_to_le16(priv->reg.scan_type);
1286         req->rate_set.size = priv->reg.rate_set.size;
1287         req->capability = ks_wlan_cap(priv);
1288         memcpy(&req->rate_set.body[0], &priv->reg.rate_set.body[0],
1289                priv->reg.rate_set.size);
1290 }
1291
1292 static
1293 void hostif_ps_adhoc_set_request(struct ks_wlan_private *priv)
1294 {
1295         struct hostif_ps_adhoc_set_request *pp;
1296
1297         pp = hostif_generic_request(sizeof(*pp), HIF_PS_ADH_SET_REQ);
1298         if (!pp)
1299                 return;
1300
1301         init_request(priv, &pp->request);
1302         pp->channel = cpu_to_le16(priv->reg.channel);
1303
1304         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1305 }
1306
1307 static
1308 void hostif_infrastructure_set_request(struct ks_wlan_private *priv, int event)
1309 {
1310         struct hostif_infrastructure_set_request *pp;
1311
1312         pp = hostif_generic_request(sizeof(*pp), event);
1313         if (!pp)
1314                 return;
1315
1316         init_request(priv, &pp->request);
1317         pp->ssid.size = priv->reg.ssid.size;
1318         memcpy(&pp->ssid.body[0], &priv->reg.ssid.body[0], priv->reg.ssid.size);
1319         pp->beacon_lost_count =
1320             cpu_to_le16(priv->reg.beacon_lost_count);
1321         pp->auth_type = cpu_to_le16(priv->reg.authenticate_type);
1322
1323         pp->channel_list.body[0] = 1;
1324         pp->channel_list.body[1] = 8;
1325         pp->channel_list.body[2] = 2;
1326         pp->channel_list.body[3] = 9;
1327         pp->channel_list.body[4] = 3;
1328         pp->channel_list.body[5] = 10;
1329         pp->channel_list.body[6] = 4;
1330         pp->channel_list.body[7] = 11;
1331         pp->channel_list.body[8] = 5;
1332         pp->channel_list.body[9] = 12;
1333         pp->channel_list.body[10] = 6;
1334         pp->channel_list.body[11] = 13;
1335         pp->channel_list.body[12] = 7;
1336         if (priv->reg.phy_type == D_11G_ONLY_MODE) {
1337                 pp->channel_list.size = 13;
1338         } else {
1339                 pp->channel_list.body[13] = 14;
1340                 pp->channel_list.size = 14;
1341         }
1342
1343         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1344 }
1345
1346 static
1347 void hostif_adhoc_set_request(struct ks_wlan_private *priv)
1348 {
1349         struct hostif_adhoc_set_request *pp;
1350
1351         pp = hostif_generic_request(sizeof(*pp), HIF_ADH_SET_REQ);
1352         if (!pp)
1353                 return;
1354
1355         init_request(priv, &pp->request);
1356         pp->channel = cpu_to_le16(priv->reg.channel);
1357         pp->ssid.size = priv->reg.ssid.size;
1358         memcpy(&pp->ssid.body[0], &priv->reg.ssid.body[0], priv->reg.ssid.size);
1359
1360         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1361 }
1362
1363 static
1364 void hostif_adhoc_set2_request(struct ks_wlan_private *priv)
1365 {
1366         struct hostif_adhoc_set2_request *pp;
1367
1368         pp = hostif_generic_request(sizeof(*pp), HIF_ADH_SET_REQ);
1369         if (!pp)
1370                 return;
1371
1372         init_request(priv, &pp->request);
1373         pp->ssid.size = priv->reg.ssid.size;
1374         memcpy(&pp->ssid.body[0], &priv->reg.ssid.body[0], priv->reg.ssid.size);
1375
1376         pp->channel_list.body[0] = priv->reg.channel;
1377         pp->channel_list.size = 1;
1378         memcpy(pp->bssid, priv->reg.bssid, ETH_ALEN);
1379
1380         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1381 }
1382
1383 static
1384 void hostif_stop_request(struct ks_wlan_private *priv)
1385 {
1386         struct hostif_stop_request *pp;
1387
1388         pp = hostif_generic_request(sizeof(*pp), HIF_STOP_REQ);
1389         if (!pp)
1390                 return;
1391
1392         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1393 }
1394
1395 static
1396 void hostif_phy_information_request(struct ks_wlan_private *priv)
1397 {
1398         struct hostif_phy_information_request *pp;
1399
1400         pp = hostif_generic_request(sizeof(*pp), HIF_PHY_INFO_REQ);
1401         if (!pp)
1402                 return;
1403
1404         if (priv->reg.phy_info_timer) {
1405                 pp->type = cpu_to_le16(TIME_TYPE);
1406                 pp->time = cpu_to_le16(priv->reg.phy_info_timer);
1407         } else {
1408                 pp->type = cpu_to_le16(NORMAL_TYPE);
1409                 pp->time = cpu_to_le16(0);
1410         }
1411
1412         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1413 }
1414
1415 static
1416 void hostif_power_mgmt_request(struct ks_wlan_private *priv,
1417                                u32 mode, u32 wake_up, u32 receive_dtims)
1418 {
1419         struct hostif_power_mgmt_request *pp;
1420
1421         pp = hostif_generic_request(sizeof(*pp), HIF_POWER_MGMT_REQ);
1422         if (!pp)
1423                 return;
1424
1425         pp->mode = cpu_to_le32(mode);
1426         pp->wake_up = cpu_to_le32(wake_up);
1427         pp->receive_dtims = cpu_to_le32(receive_dtims);
1428
1429         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1430 }
1431
1432 static
1433 void hostif_sleep_request(struct ks_wlan_private *priv,
1434                           enum sleep_mode_type mode)
1435 {
1436         struct hostif_sleep_request *pp;
1437
1438         if (mode == SLP_SLEEP) {
1439                 pp = hostif_generic_request(sizeof(*pp), HIF_SLEEP_REQ);
1440                 if (!pp)
1441                         return;
1442
1443                 send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1444         } else if (mode == SLP_ACTIVE) {
1445                 atomic_set(&priv->sleepstatus.wakeup_request, 1);
1446                 queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
1447         } else {
1448                 netdev_err(priv->net_dev, "invalid mode %ld\n", (long)mode);
1449                 return;
1450         }
1451 }
1452
1453 static
1454 void hostif_bss_scan_request(struct ks_wlan_private *priv,
1455                              unsigned long scan_type, u8 *scan_ssid,
1456                              u8 scan_ssid_len)
1457 {
1458         struct hostif_bss_scan_request *pp;
1459
1460         pp = hostif_generic_request(sizeof(*pp), HIF_SCAN_REQ);
1461         if (!pp)
1462                 return;
1463
1464         pp->scan_type = scan_type;
1465
1466         pp->ch_time_min = cpu_to_le32(110);     /* default value */
1467         pp->ch_time_max = cpu_to_le32(130);     /* default value */
1468         pp->channel_list.body[0] = 1;
1469         pp->channel_list.body[1] = 8;
1470         pp->channel_list.body[2] = 2;
1471         pp->channel_list.body[3] = 9;
1472         pp->channel_list.body[4] = 3;
1473         pp->channel_list.body[5] = 10;
1474         pp->channel_list.body[6] = 4;
1475         pp->channel_list.body[7] = 11;
1476         pp->channel_list.body[8] = 5;
1477         pp->channel_list.body[9] = 12;
1478         pp->channel_list.body[10] = 6;
1479         pp->channel_list.body[11] = 13;
1480         pp->channel_list.body[12] = 7;
1481         if (priv->reg.phy_type == D_11G_ONLY_MODE) {
1482                 pp->channel_list.size = 13;
1483         } else {
1484                 pp->channel_list.body[13] = 14;
1485                 pp->channel_list.size = 14;
1486         }
1487         pp->ssid.size = 0;
1488
1489         /* specified SSID SCAN */
1490         if (scan_ssid_len > 0 && scan_ssid_len <= 32) {
1491                 pp->ssid.size = scan_ssid_len;
1492                 memcpy(&pp->ssid.body[0], scan_ssid, scan_ssid_len);
1493         }
1494
1495         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1496
1497         priv->aplist.size = 0;
1498         priv->scan_ind_count = 0;
1499 }
1500
1501 static
1502 void hostif_mic_failure_request(struct ks_wlan_private *priv,
1503                                 u16 failure_count, u16 timer)
1504 {
1505         struct hostif_mic_failure_request *pp;
1506
1507         pp = hostif_generic_request(sizeof(*pp), HIF_MIC_FAILURE_REQ);
1508         if (!pp)
1509                 return;
1510
1511         pp->failure_count = cpu_to_le16(failure_count);
1512         pp->timer = cpu_to_le16(timer);
1513
1514         send_request_to_device(priv, pp, hif_align_size(sizeof(*pp)));
1515 }
1516
1517 /* Device I/O Receive indicate */
1518 static void devio_rec_ind(struct ks_wlan_private *priv, unsigned char *p,
1519                           unsigned int size)
1520 {
1521         if (!priv->is_device_open)
1522                 return;
1523
1524         spin_lock(&priv->dev_read_lock);
1525         priv->dev_data[atomic_read(&priv->rec_count)] = p;
1526         priv->dev_size[atomic_read(&priv->rec_count)] = size;
1527
1528         if (atomic_read(&priv->event_count) != DEVICE_STOCK_COUNT) {
1529                 /* rx event count inc */
1530                 atomic_inc(&priv->event_count);
1531         }
1532         atomic_inc(&priv->rec_count);
1533         if (atomic_read(&priv->rec_count) == DEVICE_STOCK_COUNT)
1534                 atomic_set(&priv->rec_count, 0);
1535
1536         wake_up_interruptible_all(&priv->devread_wait);
1537
1538         spin_unlock(&priv->dev_read_lock);
1539 }
1540
1541 void hostif_receive(struct ks_wlan_private *priv, unsigned char *p,
1542                     unsigned int size)
1543 {
1544         devio_rec_ind(priv, p, size);
1545
1546         priv->rxp = p;
1547         priv->rx_size = size;
1548
1549         if (get_word(priv) == priv->rx_size)
1550                 hostif_event_check(priv);
1551 }
1552
1553 static void hostif_sme_set_wep(struct ks_wlan_private *priv, int type)
1554 {
1555         switch (type) {
1556         case SME_WEP_INDEX_REQUEST:
1557                 hostif_mib_set_request_int(priv, DOT11_WEP_DEFAULT_KEY_ID,
1558                                            priv->reg.wep_index);
1559                 break;
1560         case SME_WEP_KEY1_REQUEST:
1561                 if (priv->wpa.wpa_enabled)
1562                         return;
1563                 hostif_mib_set_request_ostring(priv,
1564                                                DOT11_WEP_DEFAULT_KEY_VALUE1,
1565                                                &priv->reg.wep_key[0].val[0],
1566                                                priv->reg.wep_key[0].size);
1567                 break;
1568         case SME_WEP_KEY2_REQUEST:
1569                 if (priv->wpa.wpa_enabled)
1570                         return;
1571                 hostif_mib_set_request_ostring(priv,
1572                                                DOT11_WEP_DEFAULT_KEY_VALUE2,
1573                                                &priv->reg.wep_key[1].val[0],
1574                                                priv->reg.wep_key[1].size);
1575                 break;
1576         case SME_WEP_KEY3_REQUEST:
1577                 if (priv->wpa.wpa_enabled)
1578                         return;
1579                 hostif_mib_set_request_ostring(priv,
1580                                                DOT11_WEP_DEFAULT_KEY_VALUE3,
1581                                                &priv->reg.wep_key[2].val[0],
1582                                                priv->reg.wep_key[2].size);
1583                 break;
1584         case SME_WEP_KEY4_REQUEST:
1585                 if (priv->wpa.wpa_enabled)
1586                         return;
1587                 hostif_mib_set_request_ostring(priv,
1588                                                DOT11_WEP_DEFAULT_KEY_VALUE4,
1589                                                &priv->reg.wep_key[3].val[0],
1590                                                priv->reg.wep_key[3].size);
1591                 break;
1592         case SME_WEP_FLAG_REQUEST:
1593                 hostif_mib_set_request_bool(priv, DOT11_PRIVACY_INVOKED,
1594                                             priv->reg.privacy_invoked);
1595                 break;
1596         }
1597 }
1598
1599 struct wpa_suite {
1600         __le16 size;
1601         unsigned char suite[4][CIPHER_ID_LEN];
1602 } __packed;
1603
1604 struct rsn_mode {
1605         __le32 rsn_mode;
1606         __le16 rsn_capability;
1607 } __packed;
1608
1609 static void hostif_sme_set_rsn(struct ks_wlan_private *priv, int type)
1610 {
1611         struct wpa_suite wpa_suite;
1612         struct rsn_mode rsn_mode;
1613         size_t size;
1614         u32 mode;
1615         const u8 *buf = NULL;
1616
1617         memset(&wpa_suite, 0, sizeof(wpa_suite));
1618
1619         switch (type) {
1620         case SME_RSN_UCAST_REQUEST:
1621                 wpa_suite.size = cpu_to_le16(1);
1622                 switch (priv->wpa.pairwise_suite) {
1623                 case IW_AUTH_CIPHER_NONE:
1624                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1625                                 CIPHER_ID_WPA2_NONE : CIPHER_ID_WPA_NONE;
1626                         break;
1627                 case IW_AUTH_CIPHER_WEP40:
1628                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1629                                 CIPHER_ID_WPA2_WEP40 : CIPHER_ID_WPA_WEP40;
1630                         break;
1631                 case IW_AUTH_CIPHER_TKIP:
1632                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1633                                 CIPHER_ID_WPA2_TKIP : CIPHER_ID_WPA_TKIP;
1634                         break;
1635                 case IW_AUTH_CIPHER_CCMP:
1636                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1637                                 CIPHER_ID_WPA2_CCMP : CIPHER_ID_WPA_CCMP;
1638                         break;
1639                 case IW_AUTH_CIPHER_WEP104:
1640                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1641                                 CIPHER_ID_WPA2_WEP104 : CIPHER_ID_WPA_WEP104;
1642                         break;
1643                 }
1644
1645                 if (buf)
1646                         memcpy(&wpa_suite.suite[0][0], buf, CIPHER_ID_LEN);
1647                 size = sizeof(wpa_suite.size) +
1648                        (CIPHER_ID_LEN * le16_to_cpu(wpa_suite.size));
1649                 hostif_mib_set_request_ostring(priv,
1650                                                DOT11_RSN_CONFIG_UNICAST_CIPHER,
1651                                                &wpa_suite, size);
1652                 break;
1653         case SME_RSN_MCAST_REQUEST:
1654                 switch (priv->wpa.group_suite) {
1655                 case IW_AUTH_CIPHER_NONE:
1656                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1657                                 CIPHER_ID_WPA2_NONE : CIPHER_ID_WPA_NONE;
1658                         break;
1659                 case IW_AUTH_CIPHER_WEP40:
1660                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1661                                 CIPHER_ID_WPA2_WEP40 : CIPHER_ID_WPA_WEP40;
1662                         break;
1663                 case IW_AUTH_CIPHER_TKIP:
1664                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1665                                 CIPHER_ID_WPA2_TKIP : CIPHER_ID_WPA_TKIP;
1666                         break;
1667                 case IW_AUTH_CIPHER_CCMP:
1668                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1669                                 CIPHER_ID_WPA2_CCMP : CIPHER_ID_WPA_CCMP;
1670                         break;
1671                 case IW_AUTH_CIPHER_WEP104:
1672                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1673                                 CIPHER_ID_WPA2_WEP104 : CIPHER_ID_WPA_WEP104;
1674                         break;
1675                 }
1676                 if (buf)
1677                         memcpy(&wpa_suite.suite[0][0], buf, CIPHER_ID_LEN);
1678                 hostif_mib_set_request_ostring(priv,
1679                                                DOT11_RSN_CONFIG_MULTICAST_CIPHER,
1680                                                &wpa_suite.suite[0][0],
1681                                                CIPHER_ID_LEN);
1682                 break;
1683         case SME_RSN_AUTH_REQUEST:
1684                 wpa_suite.size = cpu_to_le16(1);
1685                 switch (priv->wpa.key_mgmt_suite) {
1686                 case IW_AUTH_KEY_MGMT_802_1X:
1687                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1688                                 KEY_MGMT_ID_WPA2_1X : KEY_MGMT_ID_WPA_1X;
1689                         break;
1690                 case IW_AUTH_KEY_MGMT_PSK:
1691                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1692                                 KEY_MGMT_ID_WPA2_PSK : KEY_MGMT_ID_WPA_PSK;
1693                         break;
1694                 case 0:
1695                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1696                                 KEY_MGMT_ID_WPA2_NONE : KEY_MGMT_ID_WPA_NONE;
1697                         break;
1698                 case 4:
1699                         buf = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1700                                 KEY_MGMT_ID_WPA2_WPANONE :
1701                                 KEY_MGMT_ID_WPA_WPANONE;
1702                         break;
1703                 }
1704
1705                 if (buf)
1706                         memcpy(&wpa_suite.suite[0][0], buf, KEY_MGMT_ID_LEN);
1707                 size = sizeof(wpa_suite.size) +
1708                        (KEY_MGMT_ID_LEN * le16_to_cpu(wpa_suite.size));
1709                 hostif_mib_set_request_ostring(priv,
1710                                                DOT11_RSN_CONFIG_AUTH_SUITE,
1711                                                &wpa_suite, size);
1712                 break;
1713         case SME_RSN_ENABLED_REQUEST:
1714                 hostif_mib_set_request_bool(priv, DOT11_RSN_ENABLED,
1715                                             priv->wpa.rsn_enabled);
1716                 break;
1717         case SME_RSN_MODE_REQUEST:
1718                 mode = (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA2) ?
1719                         RSN_MODE_WPA2 :
1720                         (priv->wpa.version == IW_AUTH_WPA_VERSION_WPA) ?
1721                          RSN_MODE_WPA : RSN_MODE_NONE;
1722                 rsn_mode.rsn_mode = cpu_to_le32(mode);
1723                 rsn_mode.rsn_capability = cpu_to_le16(0);
1724                 hostif_mib_set_request_ostring(priv, LOCAL_RSN_MODE,
1725                                                &rsn_mode, sizeof(rsn_mode));
1726                 break;
1727         }
1728 }
1729
1730 static
1731 void hostif_sme_mode_setup(struct ks_wlan_private *priv)
1732 {
1733         unsigned char rate_size;
1734         unsigned char rate_octet[RATE_SET_MAX_SIZE];
1735         int i = 0;
1736
1737         /* rate setting if rate segging is auto for changing phy_type (#94) */
1738         if (priv->reg.tx_rate == TX_RATE_FULL_AUTO) {
1739                 if (priv->reg.phy_type == D_11B_ONLY_MODE) {
1740                         priv->reg.rate_set.body[3] = TX_RATE_11M;
1741                         priv->reg.rate_set.body[2] = TX_RATE_5M;
1742                         priv->reg.rate_set.body[1] = TX_RATE_2M | BASIC_RATE;
1743                         priv->reg.rate_set.body[0] = TX_RATE_1M | BASIC_RATE;
1744                         priv->reg.rate_set.size = 4;
1745                 } else {        /* D_11G_ONLY_MODE or D_11BG_COMPATIBLE_MODE */
1746                         priv->reg.rate_set.body[11] = TX_RATE_54M;
1747                         priv->reg.rate_set.body[10] = TX_RATE_48M;
1748                         priv->reg.rate_set.body[9] = TX_RATE_36M;
1749                         priv->reg.rate_set.body[8] = TX_RATE_18M;
1750                         priv->reg.rate_set.body[7] = TX_RATE_9M;
1751                         priv->reg.rate_set.body[6] = TX_RATE_24M | BASIC_RATE;
1752                         priv->reg.rate_set.body[5] = TX_RATE_12M | BASIC_RATE;
1753                         priv->reg.rate_set.body[4] = TX_RATE_6M | BASIC_RATE;
1754                         priv->reg.rate_set.body[3] = TX_RATE_11M | BASIC_RATE;
1755                         priv->reg.rate_set.body[2] = TX_RATE_5M | BASIC_RATE;
1756                         priv->reg.rate_set.body[1] = TX_RATE_2M | BASIC_RATE;
1757                         priv->reg.rate_set.body[0] = TX_RATE_1M | BASIC_RATE;
1758                         priv->reg.rate_set.size = 12;
1759                 }
1760         }
1761
1762         /* rate mask by phy setting */
1763         if (priv->reg.phy_type == D_11B_ONLY_MODE) {
1764                 for (i = 0; i < priv->reg.rate_set.size; i++) {
1765                         if (!is_11b_rate(priv->reg.rate_set.body[i]))
1766                                 break;
1767
1768                         if ((priv->reg.rate_set.body[i] & RATE_MASK) >= TX_RATE_5M) {
1769                                 rate_octet[i] = priv->reg.rate_set.body[i] &
1770                                                 RATE_MASK;
1771                         } else {
1772                                 rate_octet[i] = priv->reg.rate_set.body[i];
1773                         }
1774                 }
1775
1776         } else {        /* D_11G_ONLY_MODE or D_11BG_COMPATIBLE_MODE */
1777                 for (i = 0; i < priv->reg.rate_set.size; i++) {
1778                         if (!is_11bg_rate(priv->reg.rate_set.body[i]))
1779                                 break;
1780
1781                         if (is_ofdm_ext_rate(priv->reg.rate_set.body[i])) {
1782                                 rate_octet[i] = priv->reg.rate_set.body[i] &
1783                                                 RATE_MASK;
1784                         } else {
1785                                 rate_octet[i] = priv->reg.rate_set.body[i];
1786                         }
1787                 }
1788         }
1789         rate_size = i;
1790         if (rate_size == 0) {
1791                 if (priv->reg.phy_type == D_11G_ONLY_MODE)
1792                         rate_octet[0] = TX_RATE_6M | BASIC_RATE;
1793                 else
1794                         rate_octet[0] = TX_RATE_2M | BASIC_RATE;
1795                 rate_size = 1;
1796         }
1797
1798         /* rate set update */
1799         priv->reg.rate_set.size = rate_size;
1800         memcpy(&priv->reg.rate_set.body[0], &rate_octet[0], rate_size);
1801
1802         switch (priv->reg.operation_mode) {
1803         case MODE_PSEUDO_ADHOC:
1804                 hostif_ps_adhoc_set_request(priv);
1805                 break;
1806         case MODE_INFRASTRUCTURE:
1807                 if (!is_valid_ether_addr((u8 *)priv->reg.bssid)) {
1808                         hostif_infrastructure_set_request(priv,
1809                                                           HIF_INFRA_SET_REQ);
1810                 } else {
1811                         hostif_infrastructure_set_request(priv,
1812                                                           HIF_INFRA_SET2_REQ);
1813                         netdev_dbg(priv->net_dev,
1814                                    "Infra bssid = %pM\n", priv->reg.bssid);
1815                 }
1816                 break;
1817         case MODE_ADHOC:
1818                 if (!is_valid_ether_addr((u8 *)priv->reg.bssid)) {
1819                         hostif_adhoc_set_request(priv);
1820                 } else {
1821                         hostif_adhoc_set2_request(priv);
1822                         netdev_dbg(priv->net_dev,
1823                                    "Adhoc bssid = %pM\n", priv->reg.bssid);
1824                 }
1825                 break;
1826         default:
1827                 break;
1828         }
1829 }
1830
1831 static
1832 void hostif_sme_multicast_set(struct ks_wlan_private *priv)
1833 {
1834         struct net_device *dev = priv->net_dev;
1835         int mc_count;
1836         struct netdev_hw_addr *ha;
1837         char set_address[NIC_MAX_MCAST_LIST * ETH_ALEN];
1838         int i = 0;
1839
1840         spin_lock(&priv->multicast_spin);
1841
1842         memset(set_address, 0, NIC_MAX_MCAST_LIST * ETH_ALEN);
1843
1844         if (dev->flags & IFF_PROMISC) {
1845                 hostif_mib_set_request_bool(priv, LOCAL_MULTICAST_FILTER,
1846                                             MCAST_FILTER_PROMISC);
1847                 goto spin_unlock;
1848         }
1849
1850         if ((netdev_mc_count(dev) > NIC_MAX_MCAST_LIST) ||
1851             (dev->flags & IFF_ALLMULTI)) {
1852                 hostif_mib_set_request_bool(priv, LOCAL_MULTICAST_FILTER,
1853                                             MCAST_FILTER_MCASTALL);
1854                 goto spin_unlock;
1855         }
1856
1857         if (priv->sme_i.sme_flag & SME_MULTICAST) {
1858                 mc_count = netdev_mc_count(dev);
1859                 netdev_for_each_mc_addr(ha, dev) {
1860                         ether_addr_copy(&set_address[i * ETH_ALEN], ha->addr);
1861                         i++;
1862                 }
1863                 priv->sme_i.sme_flag &= ~SME_MULTICAST;
1864                 hostif_mib_set_request_ostring(priv, LOCAL_MULTICAST_ADDRESS,
1865                                                &set_address[0],
1866                                                ETH_ALEN * mc_count);
1867         } else {
1868                 priv->sme_i.sme_flag |= SME_MULTICAST;
1869                 hostif_mib_set_request_bool(priv, LOCAL_MULTICAST_FILTER,
1870                                             MCAST_FILTER_MCAST);
1871         }
1872
1873 spin_unlock:
1874         spin_unlock(&priv->multicast_spin);
1875 }
1876
1877 static void hostif_sme_power_mgmt_set(struct ks_wlan_private *priv)
1878 {
1879         u32 mode, wake_up, receive_dtims;
1880
1881         if (priv->reg.power_mgmt != POWER_MGMT_SAVE1 &&
1882             priv->reg.power_mgmt != POWER_MGMT_SAVE2) {
1883                 mode = POWER_ACTIVE;
1884                 wake_up = 0;
1885                 receive_dtims = 0;
1886         } else {
1887                 mode = (priv->reg.operation_mode == MODE_INFRASTRUCTURE) ?
1888                         POWER_SAVE : POWER_ACTIVE;
1889                 wake_up = 0;
1890                 receive_dtims = (priv->reg.operation_mode == MODE_INFRASTRUCTURE &&
1891                                  priv->reg.power_mgmt == POWER_MGMT_SAVE2);
1892         }
1893
1894         hostif_power_mgmt_request(priv, mode, wake_up, receive_dtims);
1895 }
1896
1897 static void hostif_sme_sleep_set(struct ks_wlan_private *priv)
1898 {
1899         if (priv->sleep_mode != SLP_SLEEP &&
1900             priv->sleep_mode != SLP_ACTIVE)
1901                 return;
1902
1903         hostif_sleep_request(priv, priv->sleep_mode);
1904 }
1905
1906 static
1907 void hostif_sme_set_key(struct ks_wlan_private *priv, int type)
1908 {
1909         switch (type) {
1910         case SME_SET_FLAG:
1911                 hostif_mib_set_request_bool(priv, DOT11_PRIVACY_INVOKED,
1912                                             priv->reg.privacy_invoked);
1913                 break;
1914         case SME_SET_TXKEY:
1915                 hostif_mib_set_request_int(priv, DOT11_WEP_DEFAULT_KEY_ID,
1916                                            priv->wpa.txkey);
1917                 break;
1918         case SME_SET_KEY1:
1919                 hostif_mib_set_request_ostring(priv,
1920                                                DOT11_WEP_DEFAULT_KEY_VALUE1,
1921                                                &priv->wpa.key[0].key_val[0],
1922                                                priv->wpa.key[0].key_len);
1923                 break;
1924         case SME_SET_KEY2:
1925                 hostif_mib_set_request_ostring(priv,
1926                                                DOT11_WEP_DEFAULT_KEY_VALUE2,
1927                                                &priv->wpa.key[1].key_val[0],
1928                                                priv->wpa.key[1].key_len);
1929                 break;
1930         case SME_SET_KEY3:
1931                 hostif_mib_set_request_ostring(priv,
1932                                                DOT11_WEP_DEFAULT_KEY_VALUE3,
1933                                                &priv->wpa.key[2].key_val[0],
1934                                                priv->wpa.key[2].key_len);
1935                 break;
1936         case SME_SET_KEY4:
1937                 hostif_mib_set_request_ostring(priv,
1938                                                DOT11_WEP_DEFAULT_KEY_VALUE4,
1939                                                &priv->wpa.key[3].key_val[0],
1940                                                priv->wpa.key[3].key_len);
1941                 break;
1942         case SME_SET_PMK_TSC:
1943                 hostif_mib_set_request_ostring(priv, DOT11_PMK_TSC,
1944                                                &priv->wpa.key[0].rx_seq[0],
1945                                                WPA_RX_SEQ_LEN);
1946                 break;
1947         case SME_SET_GMK1_TSC:
1948                 hostif_mib_set_request_ostring(priv, DOT11_GMK1_TSC,
1949                                                &priv->wpa.key[1].rx_seq[0],
1950                                                WPA_RX_SEQ_LEN);
1951                 break;
1952         case SME_SET_GMK2_TSC:
1953                 hostif_mib_set_request_ostring(priv, DOT11_GMK2_TSC,
1954                                                &priv->wpa.key[2].rx_seq[0],
1955                                                WPA_RX_SEQ_LEN);
1956                 break;
1957         }
1958 }
1959
1960 static
1961 void hostif_sme_set_pmksa(struct ks_wlan_private *priv)
1962 {
1963         struct pmk_cache {
1964                 __le16 size;
1965                 struct {
1966                         u8 bssid[ETH_ALEN];
1967                         u8 pmkid[IW_PMKID_LEN];
1968                 } __packed list[PMK_LIST_MAX];
1969         } __packed pmkcache;
1970         struct pmk *pmk;
1971         size_t size;
1972         int i = 0;
1973
1974         list_for_each_entry(pmk, &priv->pmklist.head, list) {
1975                 if (i >= PMK_LIST_MAX)
1976                         break;
1977                 ether_addr_copy(pmkcache.list[i].bssid, pmk->bssid);
1978                 memcpy(pmkcache.list[i].pmkid, pmk->pmkid, IW_PMKID_LEN);
1979                 i++;
1980         }
1981         pmkcache.size = cpu_to_le16(priv->pmklist.size);
1982         size = sizeof(priv->pmklist.size) +
1983                ((ETH_ALEN + IW_PMKID_LEN) * priv->pmklist.size);
1984         hostif_mib_set_request_ostring(priv, LOCAL_PMK, &pmkcache, size);
1985 }
1986
1987 /* execute sme */
1988 static void hostif_sme_execute(struct ks_wlan_private *priv, int event)
1989 {
1990         u16 failure;
1991
1992         switch (event) {
1993         case SME_START:
1994                 if (priv->dev_state == DEVICE_STATE_BOOT)
1995                         hostif_mib_get_request(priv, DOT11_MAC_ADDRESS);
1996                 break;
1997         case SME_MULTICAST_REQUEST:
1998                 hostif_sme_multicast_set(priv);
1999                 break;
2000         case SME_MACADDRESS_SET_REQUEST:
2001                 hostif_mib_set_request_ostring(priv, LOCAL_CURRENTADDRESS,
2002                                                &priv->eth_addr[0], ETH_ALEN);
2003                 break;
2004         case SME_BSS_SCAN_REQUEST:
2005                 hostif_bss_scan_request(priv, priv->reg.scan_type,
2006                                         priv->scan_ssid, priv->scan_ssid_len);
2007                 break;
2008         case SME_POW_MNGMT_REQUEST:
2009                 hostif_sme_power_mgmt_set(priv);
2010                 break;
2011         case SME_PHY_INFO_REQUEST:
2012                 hostif_phy_information_request(priv);
2013                 break;
2014         case SME_MIC_FAILURE_REQUEST:
2015                 failure = priv->wpa.mic_failure.failure;
2016                 if (failure != 1 && failure != 2) {
2017                         netdev_err(priv->net_dev,
2018                                    "SME_MIC_FAILURE_REQUEST: failure count=%u error?\n",
2019                                    failure);
2020                         return;
2021                 }
2022                 hostif_mic_failure_request(priv, failure - 1, (failure == 1) ?
2023                                             0 : priv->wpa.mic_failure.counter);
2024                 break;
2025         case SME_MIC_FAILURE_CONFIRM:
2026                 if (priv->wpa.mic_failure.failure == 2) {
2027                         if (priv->wpa.mic_failure.stop)
2028                                 priv->wpa.mic_failure.stop = 0;
2029                         priv->wpa.mic_failure.failure = 0;
2030                         hostif_start_request(priv, priv->reg.operation_mode);
2031                 }
2032                 break;
2033         case SME_GET_MAC_ADDRESS:
2034                 if (priv->dev_state == DEVICE_STATE_BOOT)
2035                         hostif_mib_get_request(priv, DOT11_PRODUCT_VERSION);
2036                 break;
2037         case SME_GET_PRODUCT_VERSION:
2038                 if (priv->dev_state == DEVICE_STATE_BOOT)
2039                         priv->dev_state = DEVICE_STATE_PREINIT;
2040                 break;
2041         case SME_STOP_REQUEST:
2042                 hostif_stop_request(priv);
2043                 break;
2044         case SME_RTS_THRESHOLD_REQUEST:
2045                 hostif_mib_set_request_int(priv, DOT11_RTS_THRESHOLD,
2046                                            priv->reg.rts);
2047                 break;
2048         case SME_FRAGMENTATION_THRESHOLD_REQUEST:
2049                 hostif_mib_set_request_int(priv, DOT11_FRAGMENTATION_THRESHOLD,
2050                                            priv->reg.fragment);
2051                 break;
2052         case SME_WEP_INDEX_REQUEST:
2053         case SME_WEP_KEY1_REQUEST:
2054         case SME_WEP_KEY2_REQUEST:
2055         case SME_WEP_KEY3_REQUEST:
2056         case SME_WEP_KEY4_REQUEST:
2057         case SME_WEP_FLAG_REQUEST:
2058                 hostif_sme_set_wep(priv, event);
2059                 break;
2060         case SME_RSN_UCAST_REQUEST:
2061         case SME_RSN_MCAST_REQUEST:
2062         case SME_RSN_AUTH_REQUEST:
2063         case SME_RSN_ENABLED_REQUEST:
2064         case SME_RSN_MODE_REQUEST:
2065                 hostif_sme_set_rsn(priv, event);
2066                 break;
2067         case SME_SET_FLAG:
2068         case SME_SET_TXKEY:
2069         case SME_SET_KEY1:
2070         case SME_SET_KEY2:
2071         case SME_SET_KEY3:
2072         case SME_SET_KEY4:
2073         case SME_SET_PMK_TSC:
2074         case SME_SET_GMK1_TSC:
2075         case SME_SET_GMK2_TSC:
2076                 hostif_sme_set_key(priv, event);
2077                 break;
2078         case SME_SET_PMKSA:
2079                 hostif_sme_set_pmksa(priv);
2080                 break;
2081         case SME_WPS_ENABLE_REQUEST:
2082                 hostif_mib_set_request_int(priv, LOCAL_WPS_ENABLE,
2083                                            priv->wps.wps_enabled);
2084                 break;
2085         case SME_WPS_PROBE_REQUEST:
2086                 hostif_mib_set_request_ostring(priv, LOCAL_WPS_PROBE_REQ,
2087                                                priv->wps.ie, priv->wps.ielen);
2088                 break;
2089         case SME_MODE_SET_REQUEST:
2090                 hostif_sme_mode_setup(priv);
2091                 break;
2092         case SME_SET_GAIN:
2093                 hostif_mib_set_request_ostring(priv, LOCAL_GAIN,
2094                                                &priv->gain, sizeof(priv->gain));
2095                 break;
2096         case SME_GET_GAIN:
2097                 hostif_mib_get_request(priv, LOCAL_GAIN);
2098                 break;
2099         case SME_GET_EEPROM_CKSUM:
2100                 priv->eeprom_checksum = EEPROM_FW_NOT_SUPPORT;  /* initialize */
2101                 hostif_mib_get_request(priv, LOCAL_EEPROM_SUM);
2102                 break;
2103         case SME_START_REQUEST:
2104                 hostif_start_request(priv, priv->reg.operation_mode);
2105                 break;
2106         case SME_START_CONFIRM:
2107                 /* for power save */
2108                 atomic_set(&priv->psstatus.snooze_guard, 0);
2109                 atomic_set(&priv->psstatus.confirm_wait, 0);
2110                 if (priv->dev_state == DEVICE_STATE_PREINIT)
2111                         priv->dev_state = DEVICE_STATE_INIT;
2112                 /* wake_up_interruptible_all(&priv->confirm_wait); */
2113                 complete(&priv->confirm_wait);
2114                 break;
2115         case SME_SLEEP_REQUEST:
2116                 hostif_sme_sleep_set(priv);
2117                 break;
2118         case SME_SET_REGION:
2119                 hostif_mib_set_request_int(priv, LOCAL_REGION, priv->region);
2120                 break;
2121         case SME_MULTICAST_CONFIRM:
2122         case SME_BSS_SCAN_CONFIRM:
2123         case SME_POW_MNGMT_CONFIRM:
2124         case SME_PHY_INFO_CONFIRM:
2125         case SME_STOP_CONFIRM:
2126         case SME_RTS_THRESHOLD_CONFIRM:
2127         case SME_FRAGMENTATION_THRESHOLD_CONFIRM:
2128         case SME_WEP_INDEX_CONFIRM:
2129         case SME_WEP_KEY1_CONFIRM:
2130         case SME_WEP_KEY2_CONFIRM:
2131         case SME_WEP_KEY3_CONFIRM:
2132         case SME_WEP_KEY4_CONFIRM:
2133         case SME_WEP_FLAG_CONFIRM:
2134         case SME_RSN_UCAST_CONFIRM:
2135         case SME_RSN_MCAST_CONFIRM:
2136         case SME_RSN_AUTH_CONFIRM:
2137         case SME_RSN_ENABLED_CONFIRM:
2138         case SME_RSN_MODE_CONFIRM:
2139         case SME_MODE_SET_CONFIRM:
2140         case SME_TERMINATE:
2141         default:
2142                 break;
2143         }
2144 }
2145
2146 static
2147 void hostif_sme_task(unsigned long dev)
2148 {
2149         struct ks_wlan_private *priv = (struct ks_wlan_private *)dev;
2150
2151         if (priv->dev_state < DEVICE_STATE_BOOT)
2152                 return;
2153
2154         if (cnt_smeqbody(priv) <= 0)
2155                 return;
2156
2157         hostif_sme_execute(priv, priv->sme_i.event_buff[priv->sme_i.qhead]);
2158         inc_smeqhead(priv);
2159         if (cnt_smeqbody(priv) > 0)
2160                 tasklet_schedule(&priv->sme_task);
2161 }
2162
2163 /* send to Station Management Entity module */
2164 void hostif_sme_enqueue(struct ks_wlan_private *priv, u16 event)
2165 {
2166         /* enqueue sme event */
2167         if (cnt_smeqbody(priv) < (SME_EVENT_BUFF_SIZE - 1)) {
2168                 priv->sme_i.event_buff[priv->sme_i.qtail] = event;
2169                 inc_smeqtail(priv);
2170         } else {
2171                 /* in case of buffer overflow */
2172                 netdev_err(priv->net_dev, "sme queue buffer overflow\n");
2173         }
2174
2175         tasklet_schedule(&priv->sme_task);
2176 }
2177
2178 static inline void hostif_aplist_init(struct ks_wlan_private *priv)
2179 {
2180         size_t size = LOCAL_APLIST_MAX * sizeof(struct local_ap);
2181
2182         priv->aplist.size = 0;
2183         memset(&priv->aplist.ap[0], 0, size);
2184 }
2185
2186 static inline void hostif_status_init(struct ks_wlan_private *priv)
2187 {
2188         priv->infra_status = 0;
2189         priv->current_rate = 4;
2190         priv->connect_status = DISCONNECT_STATUS;
2191 }
2192
2193 static inline void hostif_sme_init(struct ks_wlan_private *priv)
2194 {
2195         priv->sme_i.sme_status = SME_IDLE;
2196         priv->sme_i.qhead = 0;
2197         priv->sme_i.qtail = 0;
2198         spin_lock_init(&priv->sme_i.sme_spin);
2199         priv->sme_i.sme_flag = 0;
2200         tasklet_init(&priv->sme_task, hostif_sme_task, (unsigned long)priv);
2201 }
2202
2203 static inline void hostif_wpa_init(struct ks_wlan_private *priv)
2204 {
2205         memset(&priv->wpa, 0, sizeof(priv->wpa));
2206         priv->wpa.rsn_enabled = false;
2207         priv->wpa.mic_failure.failure = 0;
2208         priv->wpa.mic_failure.last_failure_time = 0;
2209         priv->wpa.mic_failure.stop = 0;
2210 }
2211
2212 static inline void hostif_power_save_init(struct ks_wlan_private *priv)
2213 {
2214         atomic_set(&priv->psstatus.status, PS_NONE);
2215         atomic_set(&priv->psstatus.confirm_wait, 0);
2216         atomic_set(&priv->psstatus.snooze_guard, 0);
2217         init_completion(&priv->psstatus.wakeup_wait);
2218         INIT_WORK(&priv->wakeup_work, ks_wlan_hw_wakeup_task);
2219 }
2220
2221 static inline void hostif_pmklist_init(struct ks_wlan_private *priv)
2222 {
2223         int i;
2224
2225         memset(&priv->pmklist, 0, sizeof(priv->pmklist));
2226         INIT_LIST_HEAD(&priv->pmklist.head);
2227         for (i = 0; i < PMK_LIST_MAX; i++)
2228                 INIT_LIST_HEAD(&priv->pmklist.pmk[i].list);
2229 }
2230
2231 static inline void hostif_counters_init(struct ks_wlan_private *priv)
2232 {
2233         priv->dev_count = 0;
2234         atomic_set(&priv->event_count, 0);
2235         atomic_set(&priv->rec_count, 0);
2236 }
2237
2238 int hostif_init(struct ks_wlan_private *priv)
2239 {
2240         hostif_aplist_init(priv);
2241         hostif_status_init(priv);
2242
2243         spin_lock_init(&priv->multicast_spin);
2244         spin_lock_init(&priv->dev_read_lock);
2245         init_waitqueue_head(&priv->devread_wait);
2246
2247         hostif_counters_init(priv);
2248         hostif_power_save_init(priv);
2249         hostif_wpa_init(priv);
2250         hostif_pmklist_init(priv);
2251         hostif_sme_init(priv);
2252
2253         return 0;
2254 }
2255
2256 void hostif_exit(struct ks_wlan_private *priv)
2257 {
2258         tasklet_kill(&priv->sme_task);
2259 }