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