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