staging: ks7010: ks_wlan_net: mark expected switch fall-throughs
[sfrench/cifs-2.6.git] / drivers / staging / ks7010 / ks_wlan_net.c
1 /*
2  *   Driver for KeyStream 11b/g wireless LAN
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 <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/compiler.h>
15 #include <linux/init.h>
16 #include <linux/ioport.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/if_arp.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/delay.h>
22 #include <linux/completion.h>
23 #include <linux/mii.h>
24 #include <linux/pci.h>
25 #include <linux/ctype.h>
26 #include <linux/timer.h>
27 #include <linux/atomic.h>
28 #include <linux/io.h>
29 #include <linux/uaccess.h>
30
31 static int wep_on_off;
32 #define WEP_OFF         0
33 #define WEP_ON_64BIT    1
34 #define WEP_ON_128BIT   2
35
36 #include "ks_wlan.h"
37 #include "ks_hostif.h"
38 #include "ks_wlan_ioctl.h"
39
40 /* Include Wireless Extension definition and check version */
41 #include <linux/wireless.h>
42 #define WIRELESS_SPY    /* enable iwspy support */
43 #include <net/iw_handler.h>     /* New driver API */
44
45 /* Frequency list (map channels to frequencies) */
46 static const long frequency_list[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
47         2447, 2452, 2457, 2462, 2467, 2472, 2484
48 };
49
50 /* A few details needed for WEP (Wireless Equivalent Privacy) */
51 #define MAX_KEY_SIZE 13 /* 128 (?) bits */
52 #define MIN_KEY_SIZE  5 /* 40 bits RC4 - WEP */
53 struct wep_key {
54         u16 len;
55         u8 key[16];     /* 40-bit and 104-bit keys */
56 };
57
58 /* Backward compatibility */
59 #ifndef IW_ENCODE_NOKEY
60 #define IW_ENCODE_NOKEY 0x0800  /* Key is write only, so not present */
61 #define IW_ENCODE_MODE  (IW_ENCODE_DISABLED | IW_ENCODE_RESTRICTED | IW_ENCODE_OPEN)
62 #endif /* IW_ENCODE_NOKEY */
63
64 /* List of Wireless Handlers (new API) */
65 static const struct iw_handler_def ks_wlan_handler_def;
66
67 #define KSC_OPNOTSUPP   /* Operation Not Support */
68
69 /*
70  *      function prototypes
71  */
72 static int ks_wlan_open(struct net_device *dev);
73 static void ks_wlan_tx_timeout(struct net_device *dev);
74 static int ks_wlan_start_xmit(struct sk_buff *skb, struct net_device *dev);
75 static int ks_wlan_close(struct net_device *dev);
76 static void ks_wlan_set_multicast_list(struct net_device *dev);
77 static struct net_device_stats *ks_wlan_get_stats(struct net_device *dev);
78 static int ks_wlan_set_mac_address(struct net_device *dev, void *addr);
79 static int ks_wlan_netdev_ioctl(struct net_device *dev, struct ifreq *rq,
80                                 int cmd);
81
82 static atomic_t update_phyinfo;
83 static struct timer_list update_phyinfo_timer;
84 static
85 int ks_wlan_update_phy_information(struct ks_wlan_private *priv)
86 {
87         struct iw_statistics *wstats = &priv->wstats;
88
89         DPRINTK(4, "in_interrupt = %ld\n", in_interrupt());
90
91         if (priv->dev_state < DEVICE_STATE_READY)
92                 return -EBUSY;  /* not finished initialize */
93
94         if (atomic_read(&update_phyinfo))
95                 return -EPERM;
96
97         /* The status */
98         wstats->status = priv->reg.operation_mode;      /* Operation mode */
99
100         /* Signal quality and co. But where is the noise level ??? */
101         hostif_sme_enqueue(priv, SME_PHY_INFO_REQUEST);
102
103         /* interruptible_sleep_on_timeout(&priv->confirm_wait, HZ/2); */
104         if (!wait_for_completion_interruptible_timeout
105             (&priv->confirm_wait, HZ / 2)) {
106                 DPRINTK(1, "wait time out!!\n");
107         }
108
109         atomic_inc(&update_phyinfo);
110         update_phyinfo_timer.expires = jiffies + HZ;    /* 1sec */
111         add_timer(&update_phyinfo_timer);
112
113         return 0;
114 }
115
116 static
117 void ks_wlan_update_phyinfo_timeout(unsigned long ptr)
118 {
119         DPRINTK(4, "in_interrupt = %ld\n", in_interrupt());
120         atomic_set(&update_phyinfo, 0);
121 }
122
123 int ks_wlan_setup_parameter(struct ks_wlan_private *priv,
124                             unsigned int commit_flag)
125 {
126         DPRINTK(2, "\n");
127
128         hostif_sme_enqueue(priv, SME_STOP_REQUEST);
129
130         if (commit_flag & SME_RTS)
131                 hostif_sme_enqueue(priv, SME_RTS_THRESHOLD_REQUEST);
132         if (commit_flag & SME_FRAG)
133                 hostif_sme_enqueue(priv, SME_FRAGMENTATION_THRESHOLD_REQUEST);
134
135         if (commit_flag & SME_WEP_INDEX)
136                 hostif_sme_enqueue(priv, SME_WEP_INDEX_REQUEST);
137         if (commit_flag & SME_WEP_VAL1)
138                 hostif_sme_enqueue(priv, SME_WEP_KEY1_REQUEST);
139         if (commit_flag & SME_WEP_VAL2)
140                 hostif_sme_enqueue(priv, SME_WEP_KEY2_REQUEST);
141         if (commit_flag & SME_WEP_VAL3)
142                 hostif_sme_enqueue(priv, SME_WEP_KEY3_REQUEST);
143         if (commit_flag & SME_WEP_VAL4)
144                 hostif_sme_enqueue(priv, SME_WEP_KEY4_REQUEST);
145         if (commit_flag & SME_WEP_FLAG)
146                 hostif_sme_enqueue(priv, SME_WEP_FLAG_REQUEST);
147
148         if (commit_flag & SME_RSN) {
149                 hostif_sme_enqueue(priv, SME_RSN_ENABLED_REQUEST);
150                 hostif_sme_enqueue(priv, SME_RSN_MODE_REQUEST);
151         }
152         if (commit_flag & SME_RSN_MULTICAST)
153                 hostif_sme_enqueue(priv, SME_RSN_MCAST_REQUEST);
154         if (commit_flag & SME_RSN_UNICAST)
155                 hostif_sme_enqueue(priv, SME_RSN_UCAST_REQUEST);
156         if (commit_flag & SME_RSN_AUTH)
157                 hostif_sme_enqueue(priv, SME_RSN_AUTH_REQUEST);
158
159         hostif_sme_enqueue(priv, SME_MODE_SET_REQUEST);
160
161         hostif_sme_enqueue(priv, SME_START_REQUEST);
162
163         return 0;
164 }
165
166 /*
167  * Initial Wireless Extension code for Ks_Wlannet driver by :
168  *      Jean Tourrilhes <jt@hpl.hp.com> - HPL - 17 November 00
169  * Conversion to new driver API by :
170  *      Jean Tourrilhes <jt@hpl.hp.com> - HPL - 26 March 02
171  * Javier also did a good amount of work here, adding some new extensions
172  * and fixing my code. Let's just say that without him this code just
173  * would not work at all... - Jean II
174  */
175
176 static int ks_wlan_get_name(struct net_device *dev,
177                             struct iw_request_info *info, char *cwrq,
178                             char *extra)
179 {
180         struct ks_wlan_private *priv = netdev_priv(dev);
181
182         if (priv->sleep_mode == SLP_SLEEP)
183                 return -EPERM;
184
185         /* for SLEEP MODE */
186         if (priv->dev_state < DEVICE_STATE_READY)
187                 strcpy(cwrq, "NOT READY!");
188         else if (priv->reg.phy_type == D_11B_ONLY_MODE)
189                 strcpy(cwrq, "IEEE 802.11b");
190         else if (priv->reg.phy_type == D_11G_ONLY_MODE)
191                 strcpy(cwrq, "IEEE 802.11g");
192         else
193                 strcpy(cwrq, "IEEE 802.11b/g");
194
195         return 0;
196 }
197
198 static int ks_wlan_set_freq(struct net_device *dev,
199                             struct iw_request_info *info, struct iw_freq *fwrq,
200                             char *extra)
201 {
202         struct ks_wlan_private *priv = netdev_priv(dev);
203         int channel;
204
205         if (priv->sleep_mode == SLP_SLEEP)
206                 return -EPERM;
207
208         /* for SLEEP MODE */
209         /* If setting by frequency, convert to a channel */
210         if ((fwrq->e == 1) &&
211             (fwrq->m >= (int)2.412e8) && (fwrq->m <= (int)2.487e8)) {
212                 int f = fwrq->m / 100000;
213                 int c = 0;
214
215                 while ((c < 14) && (f != frequency_list[c]))
216                         c++;
217                 /* Hack to fall through... */
218                 fwrq->e = 0;
219                 fwrq->m = c + 1;
220         }
221         /* Setting by channel number */
222         if ((fwrq->m > 1000) || (fwrq->e > 0))
223                 return -EOPNOTSUPP;
224
225         channel = fwrq->m;
226         /* We should do a better check than that,
227          * based on the card capability !!!
228          */
229         if ((channel < 1) || (channel > 14)) {
230                 netdev_dbg(dev, "%s: New channel value of %d is invalid!\n",
231                            dev->name, fwrq->m);
232                 return -EINVAL;
233         }
234
235         /* Yes ! We can set it !!! */
236         priv->reg.channel = (u8)(channel);
237         priv->need_commit |= SME_MODE_SET;
238
239         return -EINPROGRESS;    /* Call commit handler */
240 }
241
242 static int ks_wlan_get_freq(struct net_device *dev,
243                             struct iw_request_info *info, struct iw_freq *fwrq,
244                             char *extra)
245 {
246         struct ks_wlan_private *priv = netdev_priv(dev);
247         int f;
248
249         if (priv->sleep_mode == SLP_SLEEP)
250                 return -EPERM;
251
252         /* for SLEEP MODE */
253         if (is_connect_status(priv->connect_status))
254                 f = (int)priv->current_ap.channel;
255         else
256                 f = (int)priv->reg.channel;
257
258         fwrq->m = frequency_list[f - 1] * 100000;
259         fwrq->e = 1;
260
261         return 0;
262 }
263
264 static int ks_wlan_set_essid(struct net_device *dev,
265                              struct iw_request_info *info,
266                              struct iw_point *dwrq, char *extra)
267 {
268         struct ks_wlan_private *priv = netdev_priv(dev);
269         size_t len;
270
271         DPRINTK(2, " %d\n", dwrq->flags);
272
273         if (priv->sleep_mode == SLP_SLEEP)
274                 return -EPERM;
275
276         /* for SLEEP MODE */
277         /* Check if we asked for `any' */
278         if (!dwrq->flags) {
279                 /* Just send an empty SSID list */
280                 memset(priv->reg.ssid.body, 0, sizeof(priv->reg.ssid.body));
281                 priv->reg.ssid.size = 0;
282         } else {
283 #if 1
284                 len = dwrq->length;
285                 /* iwconfig uses nul termination in SSID.. */
286                 if (len > 0 && extra[len - 1] == '\0')
287                         len--;
288
289                 /* Check the size of the string */
290                 if (len > IW_ESSID_MAX_SIZE)
291                         return -EINVAL;
292
293 #else
294                 /* Check the size of the string */
295                 if (dwrq->length > IW_ESSID_MAX_SIZE + 1)
296                         return -E2BIG;
297
298 #endif
299
300                 /* Set the SSID */
301                 memset(priv->reg.ssid.body, 0, sizeof(priv->reg.ssid.body));
302
303 #if 1
304                 memcpy(priv->reg.ssid.body, extra, len);
305                 priv->reg.ssid.size = len;
306 #else
307                 memcpy(priv->reg.ssid.body, extra, dwrq->length);
308                 priv->reg.ssid.size = dwrq->length;
309 #endif
310         }
311         /* Write it to the card */
312         priv->need_commit |= SME_MODE_SET;
313
314 //      return  -EINPROGRESS;   /* Call commit handler */
315         ks_wlan_setup_parameter(priv, priv->need_commit);
316         priv->need_commit = 0;
317         return 0;
318 }
319
320 static int ks_wlan_get_essid(struct net_device *dev,
321                              struct iw_request_info *info,
322                              struct iw_point *dwrq, char *extra)
323 {
324         struct ks_wlan_private *priv = netdev_priv(dev);
325
326         if (priv->sleep_mode == SLP_SLEEP)
327                 return -EPERM;
328
329         /* for SLEEP MODE */
330         /* Note : if dwrq->flags != 0, we should
331          * get the relevant SSID from the SSID list...
332          */
333         if (priv->reg.ssid.size != 0) {
334                 /* Get the current SSID */
335                 memcpy(extra, priv->reg.ssid.body, priv->reg.ssid.size);
336
337                 /* If none, we may want to get the one that was set */
338
339                 /* Push it out ! */
340                 dwrq->length = priv->reg.ssid.size;
341                 dwrq->flags = 1;        /* active */
342         } else {
343                 dwrq->length = 0;
344                 dwrq->flags = 0;        /* ANY */
345         }
346
347         return 0;
348 }
349
350 static int ks_wlan_set_wap(struct net_device *dev, struct iw_request_info *info,
351                            struct sockaddr *ap_addr, char *extra)
352 {
353         struct ks_wlan_private *priv = netdev_priv(dev);
354
355         DPRINTK(2, "\n");
356
357         if (priv->sleep_mode == SLP_SLEEP)
358                 return -EPERM;
359
360         /* for SLEEP MODE */
361         if (priv->reg.operation_mode == MODE_ADHOC ||
362             priv->reg.operation_mode == MODE_INFRASTRUCTURE) {
363                 memcpy(priv->reg.bssid, &ap_addr->sa_data, ETH_ALEN);
364
365                 if (is_valid_ether_addr((u8 *)priv->reg.bssid))
366                         priv->need_commit |= SME_MODE_SET;
367
368         } else {
369                 eth_zero_addr(priv->reg.bssid);
370                 return -EOPNOTSUPP;
371         }
372
373         DPRINTK(2, "bssid = %pM\n", priv->reg.bssid);
374
375         /* Write it to the card */
376         if (priv->need_commit) {
377                 priv->need_commit |= SME_MODE_SET;
378                 return -EINPROGRESS;    /* Call commit handler */
379         }
380         return 0;
381 }
382
383 static int ks_wlan_get_wap(struct net_device *dev, struct iw_request_info *info,
384                            struct sockaddr *awrq, char *extra)
385 {
386         struct ks_wlan_private *priv = netdev_priv(dev);
387
388         if (priv->sleep_mode == SLP_SLEEP)
389                 return -EPERM;
390
391         /* for SLEEP MODE */
392         if (is_connect_status(priv->connect_status))
393                 memcpy(awrq->sa_data, priv->current_ap.bssid, ETH_ALEN);
394         else
395                 eth_zero_addr(awrq->sa_data);
396
397         awrq->sa_family = ARPHRD_ETHER;
398
399         return 0;
400 }
401
402 static int ks_wlan_set_nick(struct net_device *dev,
403                             struct iw_request_info *info, struct iw_point *dwrq,
404                             char *extra)
405 {
406         struct ks_wlan_private *priv = netdev_priv(dev);
407
408         if (priv->sleep_mode == SLP_SLEEP)
409                 return -EPERM;
410
411         /* for SLEEP MODE */
412         /* Check the size of the string */
413         if (dwrq->length > 16 + 1)
414                 return -E2BIG;
415
416         memset(priv->nick, 0, sizeof(priv->nick));
417         memcpy(priv->nick, extra, dwrq->length);
418
419         return -EINPROGRESS;    /* Call commit handler */
420 }
421
422 static int ks_wlan_get_nick(struct net_device *dev,
423                             struct iw_request_info *info, struct iw_point *dwrq,
424                             char *extra)
425 {
426         struct ks_wlan_private *priv = netdev_priv(dev);
427
428         if (priv->sleep_mode == SLP_SLEEP)
429                 return -EPERM;
430
431         /* for SLEEP MODE */
432         strncpy(extra, priv->nick, 16);
433         extra[16] = '\0';
434         dwrq->length = strlen(extra) + 1;
435
436         return 0;
437 }
438
439 static int ks_wlan_set_rate(struct net_device *dev,
440                             struct iw_request_info *info, struct iw_param *vwrq,
441                             char *extra)
442 {
443         struct ks_wlan_private *priv = netdev_priv(dev);
444         int i = 0;
445
446         if (priv->sleep_mode == SLP_SLEEP)
447                 return -EPERM;
448
449         /* for SLEEP MODE */
450         if (priv->reg.phy_type == D_11B_ONLY_MODE) {
451                 if (vwrq->fixed == 1) {
452                         switch (vwrq->value) {
453                         case 11000000:
454                         case 5500000:
455                                 priv->reg.rate_set.body[0] =
456                                     (uint8_t)(vwrq->value / 500000);
457                                 break;
458                         case 2000000:
459                         case 1000000:
460                                 priv->reg.rate_set.body[0] =
461                                     ((uint8_t)(vwrq->value / 500000)) |
462                                     BASIC_RATE;
463                                 break;
464                         default:
465                                 return -EINVAL;
466                         }
467                         priv->reg.tx_rate = TX_RATE_FIXED;
468                         priv->reg.rate_set.size = 1;
469                 } else {        /* vwrq->fixed == 0 */
470                         if (vwrq->value > 0) {
471                                 switch (vwrq->value) {
472                                 case 11000000:
473                                         priv->reg.rate_set.body[3] =
474                                             TX_RATE_11M;
475                                         i++;
476                                         /* fall through */
477                                 case 5500000:
478                                         priv->reg.rate_set.body[2] = TX_RATE_5M;
479                                         i++;
480                                         /* fall through */
481                                 case 2000000:
482                                         priv->reg.rate_set.body[1] =
483                                             TX_RATE_2M | BASIC_RATE;
484                                         i++;
485                                         /* fall through */
486                                 case 1000000:
487                                         priv->reg.rate_set.body[0] =
488                                             TX_RATE_1M | BASIC_RATE;
489                                         i++;
490                                         break;
491                                 default:
492                                         return -EINVAL;
493                                 }
494                                 priv->reg.tx_rate = TX_RATE_MANUAL_AUTO;
495                                 priv->reg.rate_set.size = i;
496                         } else {
497                                 priv->reg.rate_set.body[3] = TX_RATE_11M;
498                                 priv->reg.rate_set.body[2] = TX_RATE_5M;
499                                 priv->reg.rate_set.body[1] =
500                                     TX_RATE_2M | BASIC_RATE;
501                                 priv->reg.rate_set.body[0] =
502                                     TX_RATE_1M | BASIC_RATE;
503                                 priv->reg.tx_rate = TX_RATE_FULL_AUTO;
504                                 priv->reg.rate_set.size = 4;
505                         }
506                 }
507         } else {        /* D_11B_ONLY_MODE or  D_11BG_COMPATIBLE_MODE */
508                 if (vwrq->fixed == 1) {
509                         switch (vwrq->value) {
510                         case 54000000:
511                         case 48000000:
512                         case 36000000:
513                         case 18000000:
514                         case 9000000:
515                                 priv->reg.rate_set.body[0] =
516                                     (uint8_t)(vwrq->value / 500000);
517                                 break;
518                         case 24000000:
519                         case 12000000:
520                         case 11000000:
521                         case 6000000:
522                         case 5500000:
523                         case 2000000:
524                         case 1000000:
525                                 priv->reg.rate_set.body[0] =
526                                     ((uint8_t)(vwrq->value / 500000)) |
527                                     BASIC_RATE;
528                                 break;
529                         default:
530                                 return -EINVAL;
531                         }
532                         priv->reg.tx_rate = TX_RATE_FIXED;
533                         priv->reg.rate_set.size = 1;
534                 } else {        /* vwrq->fixed == 0 */
535                         if (vwrq->value > 0) {
536                                 switch (vwrq->value) {
537                                 case 54000000:
538                                         priv->reg.rate_set.body[11] =
539                                             TX_RATE_54M;
540                                         i++;
541                                         /* fall through */
542                                 case 48000000:
543                                         priv->reg.rate_set.body[10] =
544                                             TX_RATE_48M;
545                                         i++;
546                                         /* fall through */
547                                 case 36000000:
548                                         priv->reg.rate_set.body[9] =
549                                             TX_RATE_36M;
550                                         i++;
551                                         /* fall through */
552                                 case 24000000:
553                                 case 18000000:
554                                 case 12000000:
555                                 case 11000000:
556                                 case 9000000:
557                                 case 6000000:
558                                         if (vwrq->value == 24000000) {
559                                                 priv->reg.rate_set.body[8] =
560                                                     TX_RATE_18M;
561                                                 i++;
562                                                 priv->reg.rate_set.body[7] =
563                                                     TX_RATE_9M;
564                                                 i++;
565                                                 priv->reg.rate_set.body[6] =
566                                                     TX_RATE_24M | BASIC_RATE;
567                                                 i++;
568                                                 priv->reg.rate_set.body[5] =
569                                                     TX_RATE_12M | BASIC_RATE;
570                                                 i++;
571                                                 priv->reg.rate_set.body[4] =
572                                                     TX_RATE_6M | BASIC_RATE;
573                                                 i++;
574                                                 priv->reg.rate_set.body[3] =
575                                                     TX_RATE_11M | BASIC_RATE;
576                                                 i++;
577                                         } else if (vwrq->value == 18000000) {
578                                                 priv->reg.rate_set.body[7] =
579                                                     TX_RATE_18M;
580                                                 i++;
581                                                 priv->reg.rate_set.body[6] =
582                                                     TX_RATE_9M;
583                                                 i++;
584                                                 priv->reg.rate_set.body[5] =
585                                                     TX_RATE_12M | BASIC_RATE;
586                                                 i++;
587                                                 priv->reg.rate_set.body[4] =
588                                                     TX_RATE_6M | BASIC_RATE;
589                                                 i++;
590                                                 priv->reg.rate_set.body[3] =
591                                                     TX_RATE_11M | BASIC_RATE;
592                                                 i++;
593                                         } else if (vwrq->value == 12000000) {
594                                                 priv->reg.rate_set.body[6] =
595                                                     TX_RATE_9M;
596                                                 i++;
597                                                 priv->reg.rate_set.body[5] =
598                                                     TX_RATE_12M | BASIC_RATE;
599                                                 i++;
600                                                 priv->reg.rate_set.body[4] =
601                                                     TX_RATE_6M | BASIC_RATE;
602                                                 i++;
603                                                 priv->reg.rate_set.body[3] =
604                                                     TX_RATE_11M | BASIC_RATE;
605                                                 i++;
606                                         } else if (vwrq->value == 11000000) {
607                                                 priv->reg.rate_set.body[5] =
608                                                     TX_RATE_9M;
609                                                 i++;
610                                                 priv->reg.rate_set.body[4] =
611                                                     TX_RATE_6M | BASIC_RATE;
612                                                 i++;
613                                                 priv->reg.rate_set.body[3] =
614                                                     TX_RATE_11M | BASIC_RATE;
615                                                 i++;
616                                         } else if (vwrq->value == 9000000) {
617                                                 priv->reg.rate_set.body[4] =
618                                                     TX_RATE_9M;
619                                                 i++;
620                                                 priv->reg.rate_set.body[3] =
621                                                     TX_RATE_6M | BASIC_RATE;
622                                                 i++;
623                                         } else {        /* vwrq->value == 6000000 */
624                                                 priv->reg.rate_set.body[3] =
625                                                     TX_RATE_6M | BASIC_RATE;
626                                                 i++;
627                                         }
628                                         /* fall through */
629                                 case 5500000:
630                                         priv->reg.rate_set.body[2] =
631                                             TX_RATE_5M | BASIC_RATE;
632                                         i++;
633                                         /* fall through */
634                                 case 2000000:
635                                         priv->reg.rate_set.body[1] =
636                                             TX_RATE_2M | BASIC_RATE;
637                                         i++;
638                                         /* fall through */
639                                 case 1000000:
640                                         priv->reg.rate_set.body[0] =
641                                             TX_RATE_1M | BASIC_RATE;
642                                         i++;
643                                         break;
644                                 default:
645                                         return -EINVAL;
646                                 }
647                                 priv->reg.tx_rate = TX_RATE_MANUAL_AUTO;
648                                 priv->reg.rate_set.size = i;
649                         } else {
650                                 priv->reg.rate_set.body[11] = TX_RATE_54M;
651                                 priv->reg.rate_set.body[10] = TX_RATE_48M;
652                                 priv->reg.rate_set.body[9] = TX_RATE_36M;
653                                 priv->reg.rate_set.body[8] = TX_RATE_18M;
654                                 priv->reg.rate_set.body[7] = TX_RATE_9M;
655                                 priv->reg.rate_set.body[6] =
656                                     TX_RATE_24M | BASIC_RATE;
657                                 priv->reg.rate_set.body[5] =
658                                     TX_RATE_12M | BASIC_RATE;
659                                 priv->reg.rate_set.body[4] =
660                                     TX_RATE_6M | BASIC_RATE;
661                                 priv->reg.rate_set.body[3] =
662                                     TX_RATE_11M | BASIC_RATE;
663                                 priv->reg.rate_set.body[2] =
664                                     TX_RATE_5M | BASIC_RATE;
665                                 priv->reg.rate_set.body[1] =
666                                     TX_RATE_2M | BASIC_RATE;
667                                 priv->reg.rate_set.body[0] =
668                                     TX_RATE_1M | BASIC_RATE;
669                                 priv->reg.tx_rate = TX_RATE_FULL_AUTO;
670                                 priv->reg.rate_set.size = 12;
671                         }
672                 }
673         }
674
675         priv->need_commit |= SME_MODE_SET;
676
677         return -EINPROGRESS;    /* Call commit handler */
678 }
679
680 static int ks_wlan_get_rate(struct net_device *dev,
681                             struct iw_request_info *info, struct iw_param *vwrq,
682                             char *extra)
683 {
684         struct ks_wlan_private *priv = netdev_priv(dev);
685
686         DPRINTK(2, "in_interrupt = %ld update_phyinfo = %d\n",
687                 in_interrupt(), atomic_read(&update_phyinfo));
688
689         if (priv->sleep_mode == SLP_SLEEP)
690                 return -EPERM;
691
692         /* for SLEEP MODE */
693         if (!atomic_read(&update_phyinfo))
694                 ks_wlan_update_phy_information(priv);
695
696         vwrq->value = ((priv->current_rate) & RATE_MASK) * 500000;
697         if (priv->reg.tx_rate == TX_RATE_FIXED)
698                 vwrq->fixed = 1;
699         else
700                 vwrq->fixed = 0;
701
702         return 0;
703 }
704
705 static int ks_wlan_set_rts(struct net_device *dev, struct iw_request_info *info,
706                            struct iw_param *vwrq, char *extra)
707 {
708         struct ks_wlan_private *priv = netdev_priv(dev);
709         int rthr = vwrq->value;
710
711         if (priv->sleep_mode == SLP_SLEEP)
712                 return -EPERM;
713
714         /* for SLEEP MODE */
715         if (vwrq->disabled)
716                 rthr = 2347;
717         if ((rthr < 0) || (rthr > 2347))
718                 return -EINVAL;
719
720         priv->reg.rts = rthr;
721         priv->need_commit |= SME_RTS;
722
723         return -EINPROGRESS;    /* Call commit handler */
724 }
725
726 static int ks_wlan_get_rts(struct net_device *dev, struct iw_request_info *info,
727                            struct iw_param *vwrq, char *extra)
728 {
729         struct ks_wlan_private *priv = netdev_priv(dev);
730
731         if (priv->sleep_mode == SLP_SLEEP)
732                 return -EPERM;
733
734         /* for SLEEP MODE */
735         vwrq->value = priv->reg.rts;
736         vwrq->disabled = (vwrq->value >= 2347);
737         vwrq->fixed = 1;
738
739         return 0;
740 }
741
742 static int ks_wlan_set_frag(struct net_device *dev,
743                             struct iw_request_info *info, struct iw_param *vwrq,
744                             char *extra)
745 {
746         struct ks_wlan_private *priv = netdev_priv(dev);
747         int fthr = vwrq->value;
748
749         if (priv->sleep_mode == SLP_SLEEP)
750                 return -EPERM;
751
752         /* for SLEEP MODE */
753         if (vwrq->disabled)
754                 fthr = 2346;
755         if ((fthr < 256) || (fthr > 2346))
756                 return -EINVAL;
757
758         fthr &= ~0x1;   /* Get an even value - is it really needed ??? */
759         priv->reg.fragment = fthr;
760         priv->need_commit |= SME_FRAG;
761
762         return -EINPROGRESS;    /* Call commit handler */
763 }
764
765 static int ks_wlan_get_frag(struct net_device *dev,
766                             struct iw_request_info *info, struct iw_param *vwrq,
767                             char *extra)
768 {
769         struct ks_wlan_private *priv = netdev_priv(dev);
770
771         if (priv->sleep_mode == SLP_SLEEP)
772                 return -EPERM;
773
774         /* for SLEEP MODE */
775         vwrq->value = priv->reg.fragment;
776         vwrq->disabled = (vwrq->value >= 2346);
777         vwrq->fixed = 1;
778
779         return 0;
780 }
781
782 static int ks_wlan_set_mode(struct net_device *dev,
783                             struct iw_request_info *info, __u32 *uwrq,
784                             char *extra)
785 {
786         struct ks_wlan_private *priv = netdev_priv(dev);
787
788         DPRINTK(2, "mode=%d\n", *uwrq);
789
790         if (priv->sleep_mode == SLP_SLEEP)
791                 return -EPERM;
792
793         /* for SLEEP MODE */
794         switch (*uwrq) {
795         case IW_MODE_ADHOC:
796                 priv->reg.operation_mode = MODE_ADHOC;
797                 priv->need_commit |= SME_MODE_SET;
798                 break;
799         case IW_MODE_INFRA:
800                 priv->reg.operation_mode = MODE_INFRASTRUCTURE;
801                 priv->need_commit |= SME_MODE_SET;
802                 break;
803         case IW_MODE_AUTO:
804         case IW_MODE_MASTER:
805         case IW_MODE_REPEAT:
806         case IW_MODE_SECOND:
807         case IW_MODE_MONITOR:
808         default:
809                 return -EINVAL;
810         }
811
812         return -EINPROGRESS;    /* Call commit handler */
813 }
814
815 static int ks_wlan_get_mode(struct net_device *dev,
816                             struct iw_request_info *info, __u32 *uwrq,
817                             char *extra)
818 {
819         struct ks_wlan_private *priv = netdev_priv(dev);
820
821         if (priv->sleep_mode == SLP_SLEEP)
822                 return -EPERM;
823
824         /* for SLEEP MODE */
825         /* If not managed, assume it's ad-hoc */
826         switch (priv->reg.operation_mode) {
827         case MODE_INFRASTRUCTURE:
828                 *uwrq = IW_MODE_INFRA;
829                 break;
830         case MODE_ADHOC:
831                 *uwrq = IW_MODE_ADHOC;
832                 break;
833         default:
834                 *uwrq = IW_MODE_ADHOC;
835         }
836
837         return 0;
838 }
839
840 static int ks_wlan_set_encode(struct net_device *dev,
841                               struct iw_request_info *info,
842                               struct iw_point *dwrq, char *extra)
843 {
844         struct ks_wlan_private *priv = netdev_priv(dev);
845
846         struct wep_key key;
847         int index = (dwrq->flags & IW_ENCODE_INDEX);
848         int current_index = priv->reg.wep_index;
849         int i;
850
851         DPRINTK(2, "flags=%04X\n", dwrq->flags);
852
853         if (priv->sleep_mode == SLP_SLEEP)
854                 return -EPERM;
855
856         /* for SLEEP MODE */
857         /* index check */
858         if ((index < 0) || (index > 4))
859                 return -EINVAL;
860         else if (index == 0)
861                 index = current_index;
862         else
863                 index--;
864
865         /* Is WEP supported ? */
866         /* Basic checking: do we have a key to set ? */
867         if (dwrq->length > 0) {
868                 if (dwrq->length > MAX_KEY_SIZE) {      /* Check the size of the key */
869                         return -EINVAL;
870                 }
871                 if (dwrq->length > MIN_KEY_SIZE) {      /* Set the length */
872                         key.len = MAX_KEY_SIZE;
873                         priv->reg.privacy_invoked = 0x01;
874                         priv->need_commit |= SME_WEP_FLAG;
875                         wep_on_off = WEP_ON_128BIT;
876                 } else {
877                         if (dwrq->length > 0) {
878                                 key.len = MIN_KEY_SIZE;
879                                 priv->reg.privacy_invoked = 0x01;
880                                 priv->need_commit |= SME_WEP_FLAG;
881                                 wep_on_off = WEP_ON_64BIT;
882                         } else {        /* Disable the key */
883                                 key.len = 0;
884                         }
885                 }
886                 /* Check if the key is not marked as invalid */
887                 if (!(dwrq->flags & IW_ENCODE_NOKEY)) {
888                         /* Cleanup */
889                         memset(key.key, 0, MAX_KEY_SIZE);
890                         /* Copy the key in the driver */
891                         if (copy_from_user
892                             (key.key, dwrq->pointer, dwrq->length)) {
893                                 key.len = 0;
894                                 return -EFAULT;
895                         }
896                         /* Send the key to the card */
897                         priv->reg.wep_key[index].size = key.len;
898                         for (i = 0; i < (priv->reg.wep_key[index].size); i++)
899                                 priv->reg.wep_key[index].val[i] = key.key[i];
900
901                         priv->need_commit |= (SME_WEP_VAL1 << index);
902                         priv->reg.wep_index = index;
903                         priv->need_commit |= SME_WEP_INDEX;
904                 }
905         } else {
906                 if (dwrq->flags & IW_ENCODE_DISABLED) {
907                         priv->reg.wep_key[0].size = 0;
908                         priv->reg.wep_key[1].size = 0;
909                         priv->reg.wep_key[2].size = 0;
910                         priv->reg.wep_key[3].size = 0;
911                         priv->reg.privacy_invoked = 0x00;
912                         if (priv->reg.authenticate_type == AUTH_TYPE_SHARED_KEY)
913                                 priv->need_commit |= SME_MODE_SET;
914
915                         priv->reg.authenticate_type = AUTH_TYPE_OPEN_SYSTEM;
916                         wep_on_off = WEP_OFF;
917                         priv->need_commit |= SME_WEP_FLAG;
918                 } else {
919                         /* Do we want to just set the transmit key index ? */
920                         if ((index >= 0) && (index < 4)) {
921                                 /* set_wep_key(priv, index, 0, 0, 1);   xxx */
922                                 if (priv->reg.wep_key[index].size != 0) {
923                                         priv->reg.wep_index = index;
924                                         priv->need_commit |= SME_WEP_INDEX;
925                                 } else {
926                                         return -EINVAL;
927                                 }
928                         }
929                 }
930         }
931
932         /* Commit the changes if needed */
933         if (dwrq->flags & IW_ENCODE_MODE)
934                 priv->need_commit |= SME_WEP_FLAG;
935
936         if (dwrq->flags & IW_ENCODE_OPEN) {
937                 if (priv->reg.authenticate_type == AUTH_TYPE_SHARED_KEY)
938                         priv->need_commit |= SME_MODE_SET;
939
940                 priv->reg.authenticate_type = AUTH_TYPE_OPEN_SYSTEM;
941         } else if (dwrq->flags & IW_ENCODE_RESTRICTED) {
942                 if (priv->reg.authenticate_type == AUTH_TYPE_OPEN_SYSTEM)
943                         priv->need_commit |= SME_MODE_SET;
944
945                 priv->reg.authenticate_type = AUTH_TYPE_SHARED_KEY;
946         }
947 //      return -EINPROGRESS;            /* Call commit handler */
948         if (priv->need_commit) {
949                 ks_wlan_setup_parameter(priv, priv->need_commit);
950                 priv->need_commit = 0;
951         }
952         return 0;
953 }
954
955 static int ks_wlan_get_encode(struct net_device *dev,
956                               struct iw_request_info *info,
957                               struct iw_point *dwrq, char *extra)
958 {
959         struct ks_wlan_private *priv = netdev_priv(dev);
960         char zeros[16];
961         int index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
962
963         if (priv->sleep_mode == SLP_SLEEP)
964                 return -EPERM;
965
966         /* for SLEEP MODE */
967         dwrq->flags = IW_ENCODE_DISABLED;
968
969         /* Check encryption mode */
970         switch (priv->reg.authenticate_type) {
971         case AUTH_TYPE_OPEN_SYSTEM:
972                 dwrq->flags = IW_ENCODE_OPEN;
973                 break;
974         case AUTH_TYPE_SHARED_KEY:
975                 dwrq->flags = IW_ENCODE_RESTRICTED;
976                 break;
977         }
978
979         memset(zeros, 0, sizeof(zeros));
980
981         /* Which key do we want ? -1 -> tx index */
982         if ((index < 0) || (index >= 4))
983                 index = priv->reg.wep_index;
984         if (priv->reg.privacy_invoked) {
985                 dwrq->flags &= ~IW_ENCODE_DISABLED;
986                 /* dwrq->flags |= IW_ENCODE_NOKEY; */
987         }
988         dwrq->flags |= index + 1;
989         DPRINTK(2, "encoding flag = 0x%04X\n", dwrq->flags);
990         /* Copy the key to the user buffer */
991         if ((index >= 0) && (index < 4))
992                 dwrq->length = priv->reg.wep_key[index].size;
993         if (dwrq->length > 16)
994                 dwrq->length = 0;
995 #if 1   /* IW_ENCODE_NOKEY; */
996         if (dwrq->length) {
997                 if ((index >= 0) && (index < 4))
998                         memcpy(extra, priv->reg.wep_key[index].val,
999                                dwrq->length);
1000         } else {
1001                 memcpy(extra, zeros, dwrq->length);
1002         }
1003 #endif
1004         return 0;
1005 }
1006
1007 #ifndef KSC_OPNOTSUPP
1008 static int ks_wlan_set_txpow(struct net_device *dev,
1009                              struct iw_request_info *info,
1010                              struct iw_param *vwrq, char *extra)
1011 {
1012         return -EOPNOTSUPP;     /* Not Support */
1013 }
1014
1015 static int ks_wlan_get_txpow(struct net_device *dev,
1016                              struct iw_request_info *info,
1017                              struct iw_param *vwrq, char *extra)
1018 {
1019         if (priv->sleep_mode == SLP_SLEEP)
1020                 return -EPERM;
1021
1022         /* for SLEEP MODE */
1023         /* Not Support */
1024         vwrq->value = 0;
1025         vwrq->disabled = (vwrq->value == 0);
1026         vwrq->fixed = 1;
1027         return 0;
1028 }
1029
1030 static int ks_wlan_set_retry(struct net_device *dev,
1031                              struct iw_request_info *info,
1032                              struct iw_param *vwrq, char *extra)
1033 {
1034         return -EOPNOTSUPP;     /* Not Support */
1035 }
1036
1037 static int ks_wlan_get_retry(struct net_device *dev,
1038                              struct iw_request_info *info,
1039                              struct iw_param *vwrq, char *extra)
1040 {
1041         if (priv->sleep_mode == SLP_SLEEP)
1042                 return -EPERM;
1043
1044         /* for SLEEP MODE */
1045         /* Not Support */
1046         vwrq->value = 0;
1047         vwrq->disabled = (vwrq->value == 0);
1048         vwrq->fixed = 1;
1049         return 0;
1050 }
1051 #endif /* KSC_OPNOTSUPP */
1052
1053 static int ks_wlan_get_range(struct net_device *dev,
1054                              struct iw_request_info *info,
1055                              struct iw_point *dwrq, char *extra)
1056 {
1057         struct ks_wlan_private *priv = netdev_priv(dev);
1058         struct iw_range *range = (struct iw_range *)extra;
1059         int i, k;
1060
1061         DPRINTK(2, "\n");
1062
1063         if (priv->sleep_mode == SLP_SLEEP)
1064                 return -EPERM;
1065
1066         /* for SLEEP MODE */
1067         dwrq->length = sizeof(struct iw_range);
1068         memset(range, 0, sizeof(*range));
1069         range->min_nwid = 0x0000;
1070         range->max_nwid = 0x0000;
1071         range->num_channels = 14;
1072         /* Should be based on cap_rid.country to give only
1073          * what the current card support
1074          */
1075         k = 0;
1076         for (i = 0; i < 13; i++) {      /* channel 1 -- 13 */
1077                 range->freq[k].i = i + 1;       /* List index */
1078                 range->freq[k].m = frequency_list[i] * 100000;
1079                 range->freq[k++].e = 1; /* Values in table in MHz -> * 10^5 * 10 */
1080         }
1081         range->num_frequency = k;
1082         if (priv->reg.phy_type == D_11B_ONLY_MODE || priv->reg.phy_type == D_11BG_COMPATIBLE_MODE) {    /* channel 14 */
1083                 range->freq[13].i = 14; /* List index */
1084                 range->freq[13].m = frequency_list[13] * 100000;
1085                 range->freq[13].e = 1;  /* Values in table in MHz -> * 10^5 * 10 */
1086                 range->num_frequency = 14;
1087         }
1088
1089         /* Hum... Should put the right values there */
1090         range->max_qual.qual = 100;
1091         range->max_qual.level = 256 - 128;      /* 0 dBm? */
1092         range->max_qual.noise = 256 - 128;
1093         range->sensitivity = 1;
1094
1095         if (priv->reg.phy_type == D_11B_ONLY_MODE) {
1096                 range->bitrate[0] = 1e6;
1097                 range->bitrate[1] = 2e6;
1098                 range->bitrate[2] = 5.5e6;
1099                 range->bitrate[3] = 11e6;
1100                 range->num_bitrates = 4;
1101         } else {        /* D_11G_ONLY_MODE or D_11BG_COMPATIBLE_MODE */
1102                 range->bitrate[0] = 1e6;
1103                 range->bitrate[1] = 2e6;
1104                 range->bitrate[2] = 5.5e6;
1105                 range->bitrate[3] = 11e6;
1106
1107                 range->bitrate[4] = 6e6;
1108                 range->bitrate[5] = 9e6;
1109                 range->bitrate[6] = 12e6;
1110                 if (IW_MAX_BITRATES < 9) {
1111                         range->bitrate[7] = 54e6;
1112                         range->num_bitrates = 8;
1113                 } else {
1114                         range->bitrate[7] = 18e6;
1115                         range->bitrate[8] = 24e6;
1116                         range->bitrate[9] = 36e6;
1117                         range->bitrate[10] = 48e6;
1118                         range->bitrate[11] = 54e6;
1119
1120                         range->num_bitrates = 12;
1121                 }
1122         }
1123
1124         /* Set an indication of the max TCP throughput
1125          * in bit/s that we can expect using this interface.
1126          * May be use for QoS stuff... Jean II
1127          */
1128         if (i > 2)
1129                 range->throughput = 5000 * 1000;
1130         else
1131                 range->throughput = 1500 * 1000;
1132
1133         range->min_rts = 0;
1134         range->max_rts = 2347;
1135         range->min_frag = 256;
1136         range->max_frag = 2346;
1137
1138         range->encoding_size[0] = 5;    /* WEP: RC4 40 bits */
1139         range->encoding_size[1] = 13;   /* WEP: RC4 ~128 bits */
1140         range->num_encoding_sizes = 2;
1141         range->max_encoding_tokens = 4;
1142
1143         /* power management not support */
1144         range->pmp_flags = IW_POWER_ON;
1145         range->pmt_flags = IW_POWER_ON;
1146         range->pm_capa = 0;
1147
1148         /* Transmit Power - values are in dBm( or mW) */
1149         range->txpower[0] = -256;
1150         range->num_txpower = 1;
1151         range->txpower_capa = IW_TXPOW_DBM;
1152         /* range->txpower_capa = IW_TXPOW_MWATT; */
1153
1154         range->we_version_source = 21;
1155         range->we_version_compiled = WIRELESS_EXT;
1156
1157         range->retry_capa = IW_RETRY_ON;
1158         range->retry_flags = IW_RETRY_ON;
1159         range->r_time_flags = IW_RETRY_ON;
1160
1161         /* Experimental measurements - boundary 11/5.5 Mb/s
1162          *
1163          * Note : with or without the (local->rssi), results
1164          * are somewhat different. - Jean II
1165          */
1166         range->avg_qual.qual = 50;
1167         range->avg_qual.level = 186;    /* -70 dBm */
1168         range->avg_qual.noise = 0;
1169
1170         /* Event capability (kernel + driver) */
1171         range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
1172                                 IW_EVENT_CAPA_MASK(SIOCGIWAP) |
1173                                 IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
1174         range->event_capa[1] = IW_EVENT_CAPA_K_1;
1175         range->event_capa[4] = (IW_EVENT_CAPA_MASK(IWEVCUSTOM) |
1176                                 IW_EVENT_CAPA_MASK(IWEVMICHAELMICFAILURE));
1177
1178         /* encode extension (WPA) capability */
1179         range->enc_capa = (IW_ENC_CAPA_WPA |
1180                            IW_ENC_CAPA_WPA2 |
1181                            IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP);
1182         return 0;
1183 }
1184
1185 static int ks_wlan_set_power(struct net_device *dev,
1186                              struct iw_request_info *info,
1187                              struct iw_param *vwrq, char *extra)
1188 {
1189         struct ks_wlan_private *priv = netdev_priv(dev);
1190
1191         if (priv->sleep_mode == SLP_SLEEP)
1192                 return -EPERM;
1193
1194         if (vwrq->disabled) {
1195                 priv->reg.power_mgmt = POWER_MGMT_ACTIVE;
1196         } else {
1197                 if (priv->reg.operation_mode == MODE_INFRASTRUCTURE)
1198                         priv->reg.power_mgmt = POWER_MGMT_SAVE1;
1199                 else
1200                         return -EINVAL;
1201         }
1202
1203         hostif_sme_enqueue(priv, SME_POW_MNGMT_REQUEST);
1204
1205         return 0;
1206 }
1207
1208 static int ks_wlan_get_power(struct net_device *dev,
1209                              struct iw_request_info *info,
1210                              struct iw_param *vwrq, char *extra)
1211 {
1212         struct ks_wlan_private *priv = netdev_priv(dev);
1213
1214         if (priv->sleep_mode == SLP_SLEEP)
1215                 return -EPERM;
1216         /* for SLEEP MODE */
1217         if (priv->reg.power_mgmt > 0)
1218                 vwrq->disabled = 0;
1219         else
1220                 vwrq->disabled = 1;
1221
1222         return 0;
1223 }
1224
1225 static int ks_wlan_get_iwstats(struct net_device *dev,
1226                                struct iw_request_info *info,
1227                                struct iw_quality *vwrq, char *extra)
1228 {
1229         struct ks_wlan_private *priv = netdev_priv(dev);
1230
1231         if (priv->sleep_mode == SLP_SLEEP)
1232                 return -EPERM;
1233         /* for SLEEP MODE */
1234         vwrq->qual = 0; /* not supported */
1235         vwrq->level = priv->wstats.qual.level;
1236         vwrq->noise = 0;        /* not supported */
1237         vwrq->updated = 0;
1238
1239         return 0;
1240 }
1241
1242 #ifndef KSC_OPNOTSUPP
1243
1244 static int ks_wlan_set_sens(struct net_device *dev,
1245                             struct iw_request_info *info, struct iw_param *vwrq,
1246                             char *extra)
1247 {
1248         return -EOPNOTSUPP;     /* Not Support */
1249 }
1250
1251 static int ks_wlan_get_sens(struct net_device *dev,
1252                             struct iw_request_info *info, struct iw_param *vwrq,
1253                             char *extra)
1254 {
1255         /* Not Support */
1256         vwrq->value = 0;
1257         vwrq->disabled = (vwrq->value == 0);
1258         vwrq->fixed = 1;
1259         return 0;
1260 }
1261 #endif /* KSC_OPNOTSUPP */
1262
1263 /* Note : this is deprecated in favor of IWSCAN */
1264 static int ks_wlan_get_aplist(struct net_device *dev,
1265                               struct iw_request_info *info,
1266                               struct iw_point *dwrq, char *extra)
1267 {
1268         struct ks_wlan_private *priv = netdev_priv(dev);
1269         struct sockaddr *address = (struct sockaddr *)extra;
1270         struct iw_quality qual[LOCAL_APLIST_MAX];
1271
1272         int i;
1273
1274         if (priv->sleep_mode == SLP_SLEEP)
1275                 return -EPERM;
1276         /* for SLEEP MODE */
1277         for (i = 0; i < priv->aplist.size; i++) {
1278                 memcpy(address[i].sa_data, &(priv->aplist.ap[i].bssid[0]),
1279                        ETH_ALEN);
1280                 address[i].sa_family = ARPHRD_ETHER;
1281                 qual[i].level = 256 - priv->aplist.ap[i].rssi;
1282                 qual[i].qual = priv->aplist.ap[i].sq;
1283                 qual[i].noise = 0;      /* invalid noise value */
1284                 qual[i].updated = 7;
1285         }
1286         if (i) {
1287                 dwrq->flags = 1;        /* Should be define'd */
1288                 memcpy(extra + sizeof(struct sockaddr) * i,
1289                        &qual, sizeof(struct iw_quality) * i);
1290         }
1291         dwrq->length = i;
1292
1293         return 0;
1294 }
1295
1296 static int ks_wlan_set_scan(struct net_device *dev,
1297                             struct iw_request_info *info,
1298                             union iwreq_data *wrqu, char *extra)
1299 {
1300         struct ks_wlan_private *priv = netdev_priv(dev);
1301         struct iw_scan_req *req = NULL;
1302
1303         DPRINTK(2, "\n");
1304
1305         if (priv->sleep_mode == SLP_SLEEP)
1306                 return -EPERM;
1307
1308         /* for SLEEP MODE */
1309         /* specified SSID SCAN */
1310         if (wrqu->data.length == sizeof(struct iw_scan_req) &&
1311             wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1312                 req = (struct iw_scan_req *)extra;
1313                 priv->scan_ssid_len = req->essid_len;
1314                 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
1315         } else {
1316                 priv->scan_ssid_len = 0;
1317         }
1318
1319         priv->sme_i.sme_flag |= SME_AP_SCAN;
1320         hostif_sme_enqueue(priv, SME_BSS_SCAN_REQUEST);
1321
1322         /* At this point, just return to the user. */
1323
1324         return 0;
1325 }
1326
1327 /*
1328  * Translate scan data returned from the card to a card independent
1329  * format that the Wireless Tools will understand - Jean II
1330  */
1331 static inline char *ks_wlan_translate_scan(struct net_device *dev,
1332                                            struct iw_request_info *info,
1333                                            char *current_ev, char *end_buf,
1334                                            struct local_ap_t *ap)
1335 {
1336         /* struct ks_wlan_private *priv = (struct ks_wlan_private *)dev->priv; */
1337         struct iw_event iwe;    /* Temporary buffer */
1338         u16 capabilities;
1339         char *current_val;      /* For rates */
1340         int i;
1341         static const char rsn_leader[] = "rsn_ie=";
1342         static const char wpa_leader[] = "wpa_ie=";
1343         char buf0[RSN_IE_BODY_MAX * 2 + 30];
1344         char buf1[RSN_IE_BODY_MAX * 2 + 30];
1345         char *pbuf;
1346         /* First entry *MUST* be the AP MAC address */
1347         iwe.cmd = SIOCGIWAP;
1348         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1349         memcpy(iwe.u.ap_addr.sa_data, ap->bssid, ETH_ALEN);
1350         current_ev =
1351             iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1352                                  IW_EV_ADDR_LEN);
1353
1354         /* Other entries will be displayed in the order we give them */
1355
1356         /* Add the ESSID */
1357         iwe.u.data.length = ap->ssid.size;
1358         if (iwe.u.data.length > 32)
1359                 iwe.u.data.length = 32;
1360         iwe.cmd = SIOCGIWESSID;
1361         iwe.u.data.flags = 1;
1362         current_ev =
1363             iwe_stream_add_point(info, current_ev, end_buf, &iwe,
1364                                  ap->ssid.body);
1365
1366         /* Add mode */
1367         iwe.cmd = SIOCGIWMODE;
1368         capabilities = ap->capability;
1369         if (capabilities & (BSS_CAP_ESS | BSS_CAP_IBSS)) {
1370                 if (capabilities & BSS_CAP_ESS)
1371                         iwe.u.mode = IW_MODE_INFRA;
1372                 else
1373                         iwe.u.mode = IW_MODE_ADHOC;
1374                 current_ev =
1375                     iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1376                                          IW_EV_UINT_LEN);
1377         }
1378
1379         /* Add frequency */
1380         iwe.cmd = SIOCGIWFREQ;
1381         iwe.u.freq.m = ap->channel;
1382         iwe.u.freq.m = frequency_list[iwe.u.freq.m - 1] * 100000;
1383         iwe.u.freq.e = 1;
1384         current_ev =
1385             iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1386                                  IW_EV_FREQ_LEN);
1387
1388         /* Add quality statistics */
1389         iwe.cmd = IWEVQUAL;
1390         iwe.u.qual.level = 256 - ap->rssi;
1391         iwe.u.qual.qual = ap->sq;
1392         iwe.u.qual.noise = 0;   /* invalid noise value */
1393         current_ev =
1394             iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1395                                  IW_EV_QUAL_LEN);
1396
1397         /* Add encryption capability */
1398         iwe.cmd = SIOCGIWENCODE;
1399         if (capabilities & BSS_CAP_PRIVACY)
1400                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1401         else
1402                 iwe.u.data.flags = IW_ENCODE_DISABLED;
1403         iwe.u.data.length = 0;
1404         current_ev =
1405             iwe_stream_add_point(info, current_ev, end_buf, &iwe,
1406                                  ap->ssid.body);
1407
1408         /* Rate : stuffing multiple values in a single event require a bit
1409          * more of magic - Jean II
1410          */
1411         current_val = current_ev + IW_EV_LCP_LEN;
1412
1413         iwe.cmd = SIOCGIWRATE;
1414
1415         /* These two flags are ignored... */
1416         iwe.u.bitrate.fixed = 0;
1417         iwe.u.bitrate.disabled = 0;
1418
1419         /* Max 16 values */
1420         for (i = 0; i < 16; i++) {
1421                 /* NULL terminated */
1422                 if (i >= ap->rate_set.size)
1423                         break;
1424                 /* Bit rate given in 500 kb/s units (+ 0x80) */
1425                 iwe.u.bitrate.value = ((ap->rate_set.body[i] & 0x7f) * 500000);
1426                 /* Add new value to event */
1427                 current_val =
1428                     iwe_stream_add_value(info, current_ev, current_val, end_buf,
1429                                          &iwe, IW_EV_PARAM_LEN);
1430         }
1431         /* Check if we added any event */
1432         if ((current_val - current_ev) > IW_EV_LCP_LEN)
1433                 current_ev = current_val;
1434
1435 #define GENERIC_INFO_ELEM_ID 0xdd
1436 #define RSN_INFO_ELEM_ID 0x30
1437         if (ap->rsn_ie.id == RSN_INFO_ELEM_ID && ap->rsn_ie.size != 0) {
1438                 pbuf = &buf0[0];
1439                 memset(&iwe, 0, sizeof(iwe));
1440                 iwe.cmd = IWEVCUSTOM;
1441                 memcpy(buf0, rsn_leader, sizeof(rsn_leader) - 1);
1442                 iwe.u.data.length += sizeof(rsn_leader) - 1;
1443                 pbuf += sizeof(rsn_leader) - 1;
1444
1445                 pbuf += sprintf(pbuf, "%02x", ap->rsn_ie.id);
1446                 pbuf += sprintf(pbuf, "%02x", ap->rsn_ie.size);
1447                 iwe.u.data.length += 4;
1448
1449                 for (i = 0; i < ap->rsn_ie.size; i++)
1450                         pbuf += sprintf(pbuf, "%02x", ap->rsn_ie.body[i]);
1451                 iwe.u.data.length += (ap->rsn_ie.size) * 2;
1452
1453                 DPRINTK(4, "ap->rsn.size=%d\n", ap->rsn_ie.size);
1454
1455                 current_ev =
1456                     iwe_stream_add_point(info, current_ev, end_buf, &iwe,
1457                                          &buf0[0]);
1458         }
1459         if (ap->wpa_ie.id == GENERIC_INFO_ELEM_ID && ap->wpa_ie.size != 0) {
1460                 pbuf = &buf1[0];
1461                 memset(&iwe, 0, sizeof(iwe));
1462                 iwe.cmd = IWEVCUSTOM;
1463                 memcpy(buf1, wpa_leader, sizeof(wpa_leader) - 1);
1464                 iwe.u.data.length += sizeof(wpa_leader) - 1;
1465                 pbuf += sizeof(wpa_leader) - 1;
1466
1467                 pbuf += sprintf(pbuf, "%02x", ap->wpa_ie.id);
1468                 pbuf += sprintf(pbuf, "%02x", ap->wpa_ie.size);
1469                 iwe.u.data.length += 4;
1470
1471                 for (i = 0; i < ap->wpa_ie.size; i++)
1472                         pbuf += sprintf(pbuf, "%02x", ap->wpa_ie.body[i]);
1473                 iwe.u.data.length += (ap->wpa_ie.size) * 2;
1474
1475                 DPRINTK(4, "ap->rsn.size=%d\n", ap->wpa_ie.size);
1476                 DPRINTK(4, "iwe.u.data.length=%d\n", iwe.u.data.length);
1477
1478                 current_ev =
1479                     iwe_stream_add_point(info, current_ev, end_buf, &iwe,
1480                                          &buf1[0]);
1481         }
1482
1483         /* The other data in the scan result are not really
1484          * interesting, so for now drop it - Jean II
1485          */
1486         return current_ev;
1487 }
1488
1489 static int ks_wlan_get_scan(struct net_device *dev,
1490                             struct iw_request_info *info, struct iw_point *dwrq,
1491                             char *extra)
1492 {
1493         struct ks_wlan_private *priv = netdev_priv(dev);
1494         int i;
1495         char *current_ev = extra;
1496
1497         DPRINTK(2, "\n");
1498
1499         if (priv->sleep_mode == SLP_SLEEP)
1500                 return -EPERM;
1501         /* for SLEEP MODE */
1502         if (priv->sme_i.sme_flag & SME_AP_SCAN) {
1503                 DPRINTK(2, "flag AP_SCAN\n");
1504                 return -EAGAIN;
1505         }
1506
1507         if (priv->aplist.size == 0) {
1508                 /* Client error, no scan results...
1509                  * The caller need to restart the scan.
1510                  */
1511                 DPRINTK(2, "aplist 0\n");
1512                 return -ENODATA;
1513         }
1514
1515         /* Read and parse all entries */
1516         for (i = 0; i < priv->aplist.size; i++) {
1517                 if ((extra + dwrq->length) - current_ev <= IW_EV_ADDR_LEN) {
1518                         dwrq->length = 0;
1519                         return -E2BIG;
1520                 }
1521                 /* Translate to WE format this entry */
1522                 current_ev = ks_wlan_translate_scan(dev, info, current_ev,
1523                                                     extra + dwrq->length,
1524                                                     &priv->aplist.ap[i]);
1525         }
1526         /* Length of data */
1527         dwrq->length = (current_ev - extra);
1528         dwrq->flags = 0;
1529
1530         return 0;
1531 }
1532
1533 /* called after a bunch of SET operations */
1534 static int ks_wlan_config_commit(struct net_device *dev,
1535                                  struct iw_request_info *info, void *zwrq,
1536                                  char *extra)
1537 {
1538         struct ks_wlan_private *priv = netdev_priv(dev);
1539
1540         if (!priv->need_commit)
1541                 return 0;
1542
1543         ks_wlan_setup_parameter(priv, priv->need_commit);
1544         priv->need_commit = 0;
1545         return 0;
1546 }
1547
1548 /* set association ie params */
1549 static int ks_wlan_set_genie(struct net_device *dev,
1550                              struct iw_request_info *info,
1551                              struct iw_point *dwrq, char *extra)
1552 {
1553         struct ks_wlan_private *priv = netdev_priv(dev);
1554
1555         DPRINTK(2, "\n");
1556
1557         if (priv->sleep_mode == SLP_SLEEP)
1558                 return -EPERM;
1559         /* for SLEEP MODE */
1560         return 0;
1561 //      return -EOPNOTSUPP;
1562 }
1563
1564 static int ks_wlan_set_auth_mode(struct net_device *dev,
1565                                  struct iw_request_info *info,
1566                                  struct iw_param *vwrq, char *extra)
1567 {
1568         struct ks_wlan_private *priv = netdev_priv(dev);
1569         int index = (vwrq->flags & IW_AUTH_INDEX);
1570         int value = vwrq->value;
1571
1572         DPRINTK(2, "index=%d:value=%08X\n", index, value);
1573
1574         if (priv->sleep_mode == SLP_SLEEP)
1575                 return -EPERM;
1576         /* for SLEEP MODE */
1577         switch (index) {
1578         case IW_AUTH_WPA_VERSION:       /* 0 */
1579                 switch (value) {
1580                 case IW_AUTH_WPA_VERSION_DISABLED:
1581                         priv->wpa.version = value;
1582                         if (priv->wpa.rsn_enabled)
1583                                 priv->wpa.rsn_enabled = 0;
1584                         priv->need_commit |= SME_RSN;
1585                         break;
1586                 case IW_AUTH_WPA_VERSION_WPA:
1587                 case IW_AUTH_WPA_VERSION_WPA2:
1588                         priv->wpa.version = value;
1589                         if (!(priv->wpa.rsn_enabled))
1590                                 priv->wpa.rsn_enabled = 1;
1591                         priv->need_commit |= SME_RSN;
1592                         break;
1593                 default:
1594                         return -EOPNOTSUPP;
1595                 }
1596                 break;
1597         case IW_AUTH_CIPHER_PAIRWISE:   /* 1 */
1598                 switch (value) {
1599                 case IW_AUTH_CIPHER_NONE:
1600                         if (priv->reg.privacy_invoked) {
1601                                 priv->reg.privacy_invoked = 0x00;
1602                                 priv->need_commit |= SME_WEP_FLAG;
1603                         }
1604                         break;
1605                 case IW_AUTH_CIPHER_WEP40:
1606                 case IW_AUTH_CIPHER_TKIP:
1607                 case IW_AUTH_CIPHER_CCMP:
1608                 case IW_AUTH_CIPHER_WEP104:
1609                         if (!priv->reg.privacy_invoked) {
1610                                 priv->reg.privacy_invoked = 0x01;
1611                                 priv->need_commit |= SME_WEP_FLAG;
1612                         }
1613                         priv->wpa.pairwise_suite = value;
1614                         priv->need_commit |= SME_RSN_UNICAST;
1615                         break;
1616                 default:
1617                         return -EOPNOTSUPP;
1618                 }
1619                 break;
1620         case IW_AUTH_CIPHER_GROUP:      /* 2 */
1621                 switch (value) {
1622                 case IW_AUTH_CIPHER_NONE:
1623                         if (priv->reg.privacy_invoked) {
1624                                 priv->reg.privacy_invoked = 0x00;
1625                                 priv->need_commit |= SME_WEP_FLAG;
1626                         }
1627                         break;
1628                 case IW_AUTH_CIPHER_WEP40:
1629                 case IW_AUTH_CIPHER_TKIP:
1630                 case IW_AUTH_CIPHER_CCMP:
1631                 case IW_AUTH_CIPHER_WEP104:
1632                         if (!priv->reg.privacy_invoked) {
1633                                 priv->reg.privacy_invoked = 0x01;
1634                                 priv->need_commit |= SME_WEP_FLAG;
1635                         }
1636                         priv->wpa.group_suite = value;
1637                         priv->need_commit |= SME_RSN_MULTICAST;
1638                         break;
1639                 default:
1640                         return -EOPNOTSUPP;
1641                 }
1642                 break;
1643         case IW_AUTH_KEY_MGMT:  /* 3 */
1644                 switch (value) {
1645                 case IW_AUTH_KEY_MGMT_802_1X:
1646                 case IW_AUTH_KEY_MGMT_PSK:
1647                 case 0: /* NONE or 802_1X_NO_WPA */
1648                 case 4: /* WPA_NONE */
1649                         priv->wpa.key_mgmt_suite = value;
1650                         priv->need_commit |= SME_RSN_AUTH;
1651                         break;
1652                 default:
1653                         return -EOPNOTSUPP;
1654                 }
1655                 break;
1656         case IW_AUTH_80211_AUTH_ALG:    /* 6 */
1657                 switch (value) {
1658                 case IW_AUTH_ALG_OPEN_SYSTEM:
1659                         priv->wpa.auth_alg = value;
1660                         priv->reg.authenticate_type = AUTH_TYPE_OPEN_SYSTEM;
1661                         break;
1662                 case IW_AUTH_ALG_SHARED_KEY:
1663                         priv->wpa.auth_alg = value;
1664                         priv->reg.authenticate_type = AUTH_TYPE_SHARED_KEY;
1665                         break;
1666                 case IW_AUTH_ALG_LEAP:
1667                 default:
1668                         return -EOPNOTSUPP;
1669                 }
1670                 priv->need_commit |= SME_MODE_SET;
1671                 break;
1672         case IW_AUTH_WPA_ENABLED:       /* 7 */
1673                 priv->wpa.wpa_enabled = value;
1674                 break;
1675         case IW_AUTH_PRIVACY_INVOKED:   /* 10 */
1676                 if ((value && !priv->reg.privacy_invoked) ||
1677                     (!value && priv->reg.privacy_invoked)) {
1678                         priv->reg.privacy_invoked = value ? 0x01 : 0x00;
1679                         priv->need_commit |= SME_WEP_FLAG;
1680                 }
1681                 break;
1682         case IW_AUTH_RX_UNENCRYPTED_EAPOL:      /* 4 */
1683         case IW_AUTH_TKIP_COUNTERMEASURES:      /* 5 */
1684         case IW_AUTH_DROP_UNENCRYPTED:  /* 8 */
1685         case IW_AUTH_ROAMING_CONTROL:   /* 9 */
1686         default:
1687                 break;
1688         }
1689
1690         /* return -EINPROGRESS; */
1691         if (priv->need_commit) {
1692                 ks_wlan_setup_parameter(priv, priv->need_commit);
1693                 priv->need_commit = 0;
1694         }
1695         return 0;
1696 }
1697
1698 static int ks_wlan_get_auth_mode(struct net_device *dev,
1699                                  struct iw_request_info *info,
1700                                  struct iw_param *vwrq, char *extra)
1701 {
1702         struct ks_wlan_private *priv = netdev_priv(dev);
1703         int index = (vwrq->flags & IW_AUTH_INDEX);
1704
1705         DPRINTK(2, "index=%d\n", index);
1706
1707         if (priv->sleep_mode == SLP_SLEEP)
1708                 return -EPERM;
1709
1710         /* for SLEEP MODE */
1711         /*  WPA (not used ?? wpa_supplicant) */
1712         switch (index) {
1713         case IW_AUTH_WPA_VERSION:
1714                 vwrq->value = priv->wpa.version;
1715                 break;
1716         case IW_AUTH_CIPHER_PAIRWISE:
1717                 vwrq->value = priv->wpa.pairwise_suite;
1718                 break;
1719         case IW_AUTH_CIPHER_GROUP:
1720                 vwrq->value = priv->wpa.group_suite;
1721                 break;
1722         case IW_AUTH_KEY_MGMT:
1723                 vwrq->value = priv->wpa.key_mgmt_suite;
1724                 break;
1725         case IW_AUTH_80211_AUTH_ALG:
1726                 vwrq->value = priv->wpa.auth_alg;
1727                 break;
1728         case IW_AUTH_WPA_ENABLED:
1729                 vwrq->value = priv->wpa.rsn_enabled;
1730                 break;
1731         case IW_AUTH_RX_UNENCRYPTED_EAPOL:      /* OK??? */
1732         case IW_AUTH_TKIP_COUNTERMEASURES:
1733         case IW_AUTH_DROP_UNENCRYPTED:
1734         default:
1735                 /* return -EOPNOTSUPP; */
1736                 break;
1737         }
1738         return 0;
1739 }
1740
1741 /* set encoding token & mode (WPA)*/
1742 static int ks_wlan_set_encode_ext(struct net_device *dev,
1743                                   struct iw_request_info *info,
1744                                   struct iw_point *dwrq, char *extra)
1745 {
1746         struct ks_wlan_private *priv = netdev_priv(dev);
1747         struct iw_encode_ext *enc;
1748         int index = dwrq->flags & IW_ENCODE_INDEX;
1749         unsigned int commit = 0;
1750         struct wpa_key_t *key;
1751
1752         enc = (struct iw_encode_ext *)extra;
1753         if (!enc)
1754                 return -EINVAL;
1755
1756         DPRINTK(2, "flags=%04X:: ext_flags=%08X\n", dwrq->flags,
1757                 enc->ext_flags);
1758
1759         if (priv->sleep_mode == SLP_SLEEP)
1760                 return -EPERM;
1761
1762         /* for SLEEP MODE */
1763         if (index < 1 || index > 4)
1764                 return -EINVAL;
1765         index--;
1766         key = &priv->wpa.key[index];
1767
1768         if (dwrq->flags & IW_ENCODE_DISABLED)
1769                 key->key_len = 0;
1770
1771         key->ext_flags = enc->ext_flags;
1772         if (enc->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
1773                 priv->wpa.txkey = index;
1774                 commit |= SME_WEP_INDEX;
1775         } else if (enc->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
1776                 memcpy(&key->rx_seq[0], &enc->rx_seq[0], IW_ENCODE_SEQ_MAX_SIZE);
1777         }
1778
1779         memcpy(&key->addr.sa_data[0], &enc->addr.sa_data[0], ETH_ALEN);
1780
1781         switch (enc->alg) {
1782         case IW_ENCODE_ALG_NONE:
1783                 if (priv->reg.privacy_invoked) {
1784                         priv->reg.privacy_invoked = 0x00;
1785                         commit |= SME_WEP_FLAG;
1786                 }
1787                 key->key_len = 0;
1788
1789                 break;
1790         case IW_ENCODE_ALG_WEP:
1791         case IW_ENCODE_ALG_CCMP:
1792                 if (!priv->reg.privacy_invoked) {
1793                         priv->reg.privacy_invoked = 0x01;
1794                         commit |= SME_WEP_FLAG;
1795                 }
1796                 if (enc->key_len) {
1797                         memcpy(&key->key_val[0], &enc->key[0], enc->key_len);
1798                         key->key_len = enc->key_len;
1799                         commit |= (SME_WEP_VAL1 << index);
1800                 }
1801                 break;
1802         case IW_ENCODE_ALG_TKIP:
1803                 if (!priv->reg.privacy_invoked) {
1804                         priv->reg.privacy_invoked = 0x01;
1805                         commit |= SME_WEP_FLAG;
1806                 }
1807                 if (enc->key_len == 32) {
1808                         memcpy(&key->key_val[0], &enc->key[0], enc->key_len - 16);
1809                         key->key_len = enc->key_len - 16;
1810                         if (priv->wpa.key_mgmt_suite == 4) {    /* WPA_NONE */
1811                                 memcpy(&key->tx_mic_key[0], &enc->key[16], 8);
1812                                 memcpy(&key->rx_mic_key[0], &enc->key[16], 8);
1813                         } else {
1814                                 memcpy(&key->tx_mic_key[0], &enc->key[16], 8);
1815                                 memcpy(&key->rx_mic_key[0], &enc->key[24], 8);
1816                         }
1817                         commit |= (SME_WEP_VAL1 << index);
1818                 }
1819                 break;
1820         default:
1821                 return -EINVAL;
1822         }
1823         key->alg = enc->alg;
1824
1825         if (commit) {
1826                 if (commit & SME_WEP_INDEX)
1827                         hostif_sme_enqueue(priv, SME_SET_TXKEY);
1828                 if (commit & SME_WEP_VAL_MASK)
1829                         hostif_sme_enqueue(priv, SME_SET_KEY1 + index);
1830                 if (commit & SME_WEP_FLAG)
1831                         hostif_sme_enqueue(priv, SME_WEP_FLAG_REQUEST);
1832         }
1833
1834         return 0;
1835 }
1836
1837 /* get encoding token & mode (WPA)*/
1838 static int ks_wlan_get_encode_ext(struct net_device *dev,
1839                                   struct iw_request_info *info,
1840                                   struct iw_point *dwrq, char *extra)
1841 {
1842         struct ks_wlan_private *priv = netdev_priv(dev);
1843
1844         if (priv->sleep_mode == SLP_SLEEP)
1845                 return -EPERM;
1846
1847         /* for SLEEP MODE */
1848         /* WPA (not used ?? wpa_supplicant)
1849          * struct ks_wlan_private *priv = (struct ks_wlan_private *)dev->priv;
1850          * struct iw_encode_ext *enc;
1851          * enc = (struct iw_encode_ext *)extra;
1852          * int index = dwrq->flags & IW_ENCODE_INDEX;
1853          * WPA (not used ?? wpa_supplicant)
1854          */
1855         return 0;
1856 }
1857
1858 static int ks_wlan_set_pmksa(struct net_device *dev,
1859                              struct iw_request_info *info,
1860                              struct iw_point *dwrq, char *extra)
1861 {
1862         struct ks_wlan_private *priv = netdev_priv(dev);
1863         struct iw_pmksa *pmksa;
1864         int i;
1865         struct pmk_t *pmk;
1866         struct list_head *ptr;
1867
1868         DPRINTK(2, "\n");
1869
1870         if (priv->sleep_mode == SLP_SLEEP)
1871                 return -EPERM;
1872
1873         /* for SLEEP MODE */
1874         if (!extra)
1875                 return -EINVAL;
1876
1877         pmksa = (struct iw_pmksa *)extra;
1878         DPRINTK(2, "cmd=%d\n", pmksa->cmd);
1879
1880         switch (pmksa->cmd) {
1881         case IW_PMKSA_ADD:
1882                 if (list_empty(&priv->pmklist.head)) {  /* new list */
1883                         for (i = 0; i < PMK_LIST_MAX; i++) {
1884                                 pmk = &priv->pmklist.pmk[i];
1885                                 if (memcmp("\x00\x00\x00\x00\x00\x00",
1886                                            pmk->bssid, ETH_ALEN) == 0)
1887                                         break; /* loop */
1888                         }
1889                         memcpy(pmk->bssid, pmksa->bssid.sa_data, ETH_ALEN);
1890                         memcpy(pmk->pmkid, pmksa->pmkid, IW_PMKID_LEN);
1891                         list_add(&pmk->list, &priv->pmklist.head);
1892                         priv->pmklist.size++;
1893                         break;  /* case */
1894                 }
1895                 /* search cache data */
1896                 list_for_each(ptr, &priv->pmklist.head) {
1897                         pmk = list_entry(ptr, struct pmk_t, list);
1898                         if (memcmp(pmksa->bssid.sa_data, pmk->bssid, ETH_ALEN) == 0) {
1899                                 memcpy(pmk->pmkid, pmksa->pmkid, IW_PMKID_LEN);
1900                                 list_move(&pmk->list, &priv->pmklist.head);
1901                                 break; /* list_for_each */
1902                         }
1903                 }
1904                 if (ptr != &priv->pmklist.head) /* not find address. */
1905                         break;  /* case */
1906
1907                 if (priv->pmklist.size < PMK_LIST_MAX) {        /* new cache data */
1908                         for (i = 0; i < PMK_LIST_MAX; i++) {
1909                                 pmk = &priv->pmklist.pmk[i];
1910                                 if (memcmp("\x00\x00\x00\x00\x00\x00",
1911                                            pmk->bssid, ETH_ALEN) == 0)
1912                                         break; /* loop */
1913                         }
1914                         memcpy(pmk->bssid, pmksa->bssid.sa_data, ETH_ALEN);
1915                         memcpy(pmk->pmkid, pmksa->pmkid, IW_PMKID_LEN);
1916                         list_add(&pmk->list, &priv->pmklist.head);
1917                         priv->pmklist.size++;
1918                 } else {        /* overwrite old cache data */
1919                         pmk = list_entry(priv->pmklist.head.prev, struct pmk_t,
1920                                          list);
1921                         memcpy(pmk->bssid, pmksa->bssid.sa_data, ETH_ALEN);
1922                         memcpy(pmk->pmkid, pmksa->pmkid, IW_PMKID_LEN);
1923                         list_move(&pmk->list, &priv->pmklist.head);
1924                 }
1925                 break;
1926         case IW_PMKSA_REMOVE:
1927                 if (list_empty(&priv->pmklist.head)) {  /* list empty */
1928                         return -EINVAL;
1929                 }
1930                 /* search cache data */
1931                 list_for_each(ptr, &priv->pmklist.head) {
1932                         pmk = list_entry(ptr, struct pmk_t, list);
1933                         if (memcmp(pmksa->bssid.sa_data, pmk->bssid, ETH_ALEN) == 0) {
1934                                 eth_zero_addr(pmk->bssid);
1935                                 memset(pmk->pmkid, 0, IW_PMKID_LEN);
1936                                 list_del_init(&pmk->list);
1937                                 break;
1938                         }
1939                 }
1940                 if (ptr == &priv->pmklist.head) {       /* not find address. */
1941                         return 0;
1942                 }
1943
1944                 break;
1945         case IW_PMKSA_FLUSH:
1946                 memset(&priv->pmklist, 0, sizeof(priv->pmklist));
1947                 INIT_LIST_HEAD(&priv->pmklist.head);
1948                 for (i = 0; i < PMK_LIST_MAX; i++)
1949                         INIT_LIST_HEAD(&priv->pmklist.pmk[i].list);
1950                 break;
1951         default:
1952                 return -EINVAL;
1953         }
1954
1955         hostif_sme_enqueue(priv, SME_SET_PMKSA);
1956         return 0;
1957 }
1958
1959 static struct iw_statistics *ks_get_wireless_stats(struct net_device *dev)
1960 {
1961         struct ks_wlan_private *priv = netdev_priv(dev);
1962         struct iw_statistics *wstats = &priv->wstats;
1963
1964         if (!atomic_read(&update_phyinfo)) {
1965                 if (priv->dev_state < DEVICE_STATE_READY)
1966                         return NULL;    /* not finished initialize */
1967                 else
1968                         return wstats;
1969         }
1970
1971         /* Packets discarded in the wireless adapter due to wireless
1972          * specific problems
1973          */
1974         wstats->discard.nwid = 0;       /* Rx invalid nwid      */
1975         wstats->discard.code = 0;       /* Rx invalid crypt     */
1976         wstats->discard.fragment = 0;   /* Rx invalid frag      */
1977         wstats->discard.retries = 0;    /* Tx excessive retries */
1978         wstats->discard.misc = 0;       /* Invalid misc         */
1979         wstats->miss.beacon = 0;        /* Missed beacon        */
1980
1981         return wstats;
1982 }
1983
1984 static int ks_wlan_set_stop_request(struct net_device *dev,
1985                                     struct iw_request_info *info, __u32 *uwrq,
1986                                     char *extra)
1987 {
1988         struct ks_wlan_private *priv = netdev_priv(dev);
1989
1990         DPRINTK(2, "\n");
1991
1992         if (priv->sleep_mode == SLP_SLEEP)
1993                 return -EPERM;
1994
1995         /* for SLEEP MODE */
1996         if (!(*uwrq))
1997                 return -EINVAL;
1998
1999         hostif_sme_enqueue(priv, SME_STOP_REQUEST);
2000         return 0;
2001 }
2002
2003 #include <linux/ieee80211.h>
2004 static int ks_wlan_set_mlme(struct net_device *dev,
2005                             struct iw_request_info *info, struct iw_point *dwrq,
2006                             char *extra)
2007 {
2008         struct ks_wlan_private *priv = netdev_priv(dev);
2009         struct iw_mlme *mlme = (struct iw_mlme *)extra;
2010         __u32 mode;
2011
2012         DPRINTK(2, ":%d :%d\n", mlme->cmd, mlme->reason_code);
2013
2014         if (priv->sleep_mode == SLP_SLEEP)
2015                 return -EPERM;
2016
2017         /* for SLEEP MODE */
2018         switch (mlme->cmd) {
2019         case IW_MLME_DEAUTH:
2020                 if (mlme->reason_code == WLAN_REASON_MIC_FAILURE)
2021                         return 0;
2022                 /* fall through */
2023         case IW_MLME_DISASSOC:
2024                 mode = 1;
2025                 return ks_wlan_set_stop_request(dev, NULL, &mode, NULL);
2026         default:
2027                 return -EOPNOTSUPP;     /* Not Support */
2028         }
2029 }
2030
2031 static int ks_wlan_get_firmware_version(struct net_device *dev,
2032                                         struct iw_request_info *info,
2033                                         struct iw_point *dwrq, char *extra)
2034 {
2035         struct ks_wlan_private *priv = netdev_priv(dev);
2036
2037         strcpy(extra, priv->firmware_version);
2038         dwrq->length = priv->version_size + 1;
2039         return 0;
2040 }
2041
2042 static int ks_wlan_set_preamble(struct net_device *dev,
2043                                 struct iw_request_info *info, __u32 *uwrq,
2044                                 char *extra)
2045 {
2046         struct ks_wlan_private *priv = netdev_priv(dev);
2047
2048         if (priv->sleep_mode == SLP_SLEEP)
2049                 return -EPERM;
2050
2051         /* for SLEEP MODE */
2052         if (*uwrq == LONG_PREAMBLE) {   /* 0 */
2053                 priv->reg.preamble = LONG_PREAMBLE;
2054         } else if (*uwrq == SHORT_PREAMBLE) {   /* 1 */
2055                 priv->reg.preamble = SHORT_PREAMBLE;
2056         } else {
2057                 return -EINVAL;
2058         }
2059
2060         priv->need_commit |= SME_MODE_SET;
2061         return -EINPROGRESS;    /* Call commit handler */
2062 }
2063
2064 static int ks_wlan_get_preamble(struct net_device *dev,
2065                                 struct iw_request_info *info, __u32 *uwrq,
2066                                 char *extra)
2067 {
2068         struct ks_wlan_private *priv = netdev_priv(dev);
2069
2070         if (priv->sleep_mode == SLP_SLEEP)
2071                 return -EPERM;
2072
2073         /* for SLEEP MODE */
2074         *uwrq = priv->reg.preamble;
2075         return 0;
2076 }
2077
2078 static int ks_wlan_set_power_mgmt(struct net_device *dev,
2079                                   struct iw_request_info *info, __u32 *uwrq,
2080                                   char *extra)
2081 {
2082         struct ks_wlan_private *priv = netdev_priv(dev);
2083
2084         if (priv->sleep_mode == SLP_SLEEP)
2085                 return -EPERM;
2086
2087         /* for SLEEP MODE */
2088         if (*uwrq == POWER_MGMT_ACTIVE) {       /* 0 */
2089                 priv->reg.power_mgmt = POWER_MGMT_ACTIVE;
2090         } else if (*uwrq == POWER_MGMT_SAVE1) { /* 1 */
2091                 if (priv->reg.operation_mode == MODE_INFRASTRUCTURE)
2092                         priv->reg.power_mgmt = POWER_MGMT_SAVE1;
2093                 else
2094                         return -EINVAL;
2095         } else if (*uwrq == POWER_MGMT_SAVE2) { /* 2 */
2096                 if (priv->reg.operation_mode == MODE_INFRASTRUCTURE)
2097                         priv->reg.power_mgmt = POWER_MGMT_SAVE2;
2098                 else
2099                         return -EINVAL;
2100         } else {
2101                 return -EINVAL;
2102         }
2103
2104         hostif_sme_enqueue(priv, SME_POW_MNGMT_REQUEST);
2105
2106         return 0;
2107 }
2108
2109 static int ks_wlan_get_power_mgmt(struct net_device *dev,
2110                                   struct iw_request_info *info, __u32 *uwrq,
2111                                   char *extra)
2112 {
2113         struct ks_wlan_private *priv = netdev_priv(dev);
2114
2115         if (priv->sleep_mode == SLP_SLEEP)
2116                 return -EPERM;
2117
2118         /* for SLEEP MODE */
2119         *uwrq = priv->reg.power_mgmt;
2120         return 0;
2121 }
2122
2123 static int ks_wlan_set_scan_type(struct net_device *dev,
2124                                  struct iw_request_info *info, __u32 *uwrq,
2125                                  char *extra)
2126 {
2127         struct ks_wlan_private *priv = netdev_priv(dev);
2128
2129         if (priv->sleep_mode == SLP_SLEEP)
2130                 return -EPERM;
2131         /* for SLEEP MODE */
2132         if (*uwrq == ACTIVE_SCAN) {     /* 0 */
2133                 priv->reg.scan_type = ACTIVE_SCAN;
2134         } else if (*uwrq == PASSIVE_SCAN) {     /* 1 */
2135                 priv->reg.scan_type = PASSIVE_SCAN;
2136         } else {
2137                 return -EINVAL;
2138         }
2139
2140         return 0;
2141 }
2142
2143 static int ks_wlan_get_scan_type(struct net_device *dev,
2144                                  struct iw_request_info *info, __u32 *uwrq,
2145                                  char *extra)
2146 {
2147         struct ks_wlan_private *priv = netdev_priv(dev);
2148
2149         if (priv->sleep_mode == SLP_SLEEP)
2150                 return -EPERM;
2151         /* for SLEEP MODE */
2152         *uwrq = priv->reg.scan_type;
2153         return 0;
2154 }
2155
2156 static int ks_wlan_set_beacon_lost(struct net_device *dev,
2157                                    struct iw_request_info *info, __u32 *uwrq,
2158                                    char *extra)
2159 {
2160         struct ks_wlan_private *priv = netdev_priv(dev);
2161
2162         if (priv->sleep_mode == SLP_SLEEP)
2163                 return -EPERM;
2164         /* for SLEEP MODE */
2165         if (*uwrq >= BEACON_LOST_COUNT_MIN && *uwrq <= BEACON_LOST_COUNT_MAX)
2166                 priv->reg.beacon_lost_count = *uwrq;
2167         else
2168                 return -EINVAL;
2169
2170         if (priv->reg.operation_mode == MODE_INFRASTRUCTURE) {
2171                 priv->need_commit |= SME_MODE_SET;
2172                 return -EINPROGRESS;    /* Call commit handler */
2173         } else {
2174                 return 0;
2175         }
2176 }
2177
2178 static int ks_wlan_get_beacon_lost(struct net_device *dev,
2179                                    struct iw_request_info *info, __u32 *uwrq,
2180                                    char *extra)
2181 {
2182         struct ks_wlan_private *priv = netdev_priv(dev);
2183
2184         if (priv->sleep_mode == SLP_SLEEP)
2185                 return -EPERM;
2186         /* for SLEEP MODE */
2187         *uwrq = priv->reg.beacon_lost_count;
2188         return 0;
2189 }
2190
2191 static int ks_wlan_set_phy_type(struct net_device *dev,
2192                                 struct iw_request_info *info, __u32 *uwrq,
2193                                 char *extra)
2194 {
2195         struct ks_wlan_private *priv = netdev_priv(dev);
2196
2197         if (priv->sleep_mode == SLP_SLEEP)
2198                 return -EPERM;
2199         /* for SLEEP MODE */
2200         if (*uwrq == D_11B_ONLY_MODE) { /* 0 */
2201                 priv->reg.phy_type = D_11B_ONLY_MODE;
2202         } else if (*uwrq == D_11G_ONLY_MODE) {  /* 1 */
2203                 priv->reg.phy_type = D_11G_ONLY_MODE;
2204         } else if (*uwrq == D_11BG_COMPATIBLE_MODE) {   /* 2 */
2205                 priv->reg.phy_type = D_11BG_COMPATIBLE_MODE;
2206         } else {
2207                 return -EINVAL;
2208         }
2209
2210         priv->need_commit |= SME_MODE_SET;
2211         return -EINPROGRESS;    /* Call commit handler */
2212 }
2213
2214 static int ks_wlan_get_phy_type(struct net_device *dev,
2215                                 struct iw_request_info *info, __u32 *uwrq,
2216                                 char *extra)
2217 {
2218         struct ks_wlan_private *priv = netdev_priv(dev);
2219
2220         if (priv->sleep_mode == SLP_SLEEP)
2221                 return -EPERM;
2222         /* for SLEEP MODE */
2223         *uwrq = priv->reg.phy_type;
2224         return 0;
2225 }
2226
2227 static int ks_wlan_set_cts_mode(struct net_device *dev,
2228                                 struct iw_request_info *info, __u32 *uwrq,
2229                                 char *extra)
2230 {
2231         struct ks_wlan_private *priv = netdev_priv(dev);
2232
2233         if (priv->sleep_mode == SLP_SLEEP)
2234                 return -EPERM;
2235         /* for SLEEP MODE */
2236         if (*uwrq == CTS_MODE_FALSE) {  /* 0 */
2237                 priv->reg.cts_mode = CTS_MODE_FALSE;
2238         } else if (*uwrq == CTS_MODE_TRUE) {    /* 1 */
2239                 if (priv->reg.phy_type == D_11G_ONLY_MODE ||
2240                     priv->reg.phy_type == D_11BG_COMPATIBLE_MODE) {
2241                         priv->reg.cts_mode = CTS_MODE_TRUE;
2242                 } else {
2243                         priv->reg.cts_mode = CTS_MODE_FALSE;
2244                 }
2245         } else {
2246                 return -EINVAL;
2247         }
2248
2249         priv->need_commit |= SME_MODE_SET;
2250         return -EINPROGRESS;    /* Call commit handler */
2251 }
2252
2253 static int ks_wlan_get_cts_mode(struct net_device *dev,
2254                                 struct iw_request_info *info, __u32 *uwrq,
2255                                 char *extra)
2256 {
2257         struct ks_wlan_private *priv = netdev_priv(dev);
2258
2259         if (priv->sleep_mode == SLP_SLEEP)
2260                 return -EPERM;
2261         /* for SLEEP MODE */
2262         *uwrq = priv->reg.cts_mode;
2263         return 0;
2264 }
2265
2266 static int ks_wlan_set_sleep_mode(struct net_device *dev,
2267                                   struct iw_request_info *info,
2268                                   __u32 *uwrq, char *extra)
2269 {
2270         struct ks_wlan_private *priv = netdev_priv(dev);
2271
2272         DPRINTK(2, "\n");
2273
2274         if (*uwrq == SLP_SLEEP) {
2275                 priv->sleep_mode = *uwrq;
2276                 netdev_info(dev, "SET_SLEEP_MODE %d\n", priv->sleep_mode);
2277
2278                 hostif_sme_enqueue(priv, SME_STOP_REQUEST);
2279                 hostif_sme_enqueue(priv, SME_SLEEP_REQUEST);
2280
2281         } else if (*uwrq == SLP_ACTIVE) {
2282                 priv->sleep_mode = *uwrq;
2283                 netdev_info(dev, "SET_SLEEP_MODE %d\n", priv->sleep_mode);
2284                 hostif_sme_enqueue(priv, SME_SLEEP_REQUEST);
2285         } else {
2286                 netdev_err(dev, "SET_SLEEP_MODE %d error\n", *uwrq);
2287                 return -EINVAL;
2288         }
2289
2290         return 0;
2291 }
2292
2293 static int ks_wlan_get_sleep_mode(struct net_device *dev,
2294                                   struct iw_request_info *info,
2295                                   __u32 *uwrq, char *extra)
2296 {
2297         struct ks_wlan_private *priv = netdev_priv(dev);
2298
2299         DPRINTK(2, "GET_SLEEP_MODE %d\n", priv->sleep_mode);
2300         *uwrq = priv->sleep_mode;
2301
2302         return 0;
2303 }
2304
2305 #ifdef WPS
2306
2307 static int ks_wlan_set_wps_enable(struct net_device *dev,
2308                                   struct iw_request_info *info, __u32 *uwrq,
2309                                   char *extra)
2310 {
2311         struct ks_wlan_private *priv = netdev_priv(dev);
2312
2313         DPRINTK(2, "\n");
2314
2315         if (priv->sleep_mode == SLP_SLEEP)
2316                 return -EPERM;
2317         /* for SLEEP MODE */
2318         if (*uwrq == 0 || *uwrq == 1)
2319                 priv->wps.wps_enabled = *uwrq;
2320         else
2321                 return -EINVAL;
2322
2323         hostif_sme_enqueue(priv, SME_WPS_ENABLE_REQUEST);
2324
2325         return 0;
2326 }
2327
2328 static int ks_wlan_get_wps_enable(struct net_device *dev,
2329                                   struct iw_request_info *info, __u32 *uwrq,
2330                                   char *extra)
2331 {
2332         struct ks_wlan_private *priv = netdev_priv(dev);
2333
2334         DPRINTK(2, "\n");
2335
2336         if (priv->sleep_mode == SLP_SLEEP)
2337                 return -EPERM;
2338         /* for SLEEP MODE */
2339         *uwrq = priv->wps.wps_enabled;
2340         netdev_info(dev, "return=%d\n", *uwrq);
2341
2342         return 0;
2343 }
2344
2345 static int ks_wlan_set_wps_probe_req(struct net_device *dev,
2346                                      struct iw_request_info *info,
2347                                      struct iw_point *dwrq, char *extra)
2348 {
2349         u8 *p = extra;
2350         unsigned char len;
2351         struct ks_wlan_private *priv = netdev_priv(dev);
2352
2353         DPRINTK(2, "\n");
2354
2355         if (priv->sleep_mode == SLP_SLEEP)
2356                 return -EPERM;
2357         /* for SLEEP MODE */
2358         DPRINTK(2, "dwrq->length=%d\n", dwrq->length);
2359
2360         /* length check */
2361         if (p[1] + 2 != dwrq->length || dwrq->length > 256)
2362                 return -EINVAL;
2363
2364         priv->wps.ielen = p[1] + 2 + 1; /* IE header + IE + sizeof(len) */
2365         len = p[1] + 2; /* IE header + IE */
2366
2367         memcpy(priv->wps.ie, &len, sizeof(len));
2368         p = memcpy(priv->wps.ie + 1, p, len);
2369
2370         DPRINTK(2, "%d(%#x): %02X %02X %02X %02X ... %02X %02X %02X\n",
2371                 priv->wps.ielen, priv->wps.ielen, p[0], p[1], p[2], p[3],
2372                 p[priv->wps.ielen - 3], p[priv->wps.ielen - 2],
2373                 p[priv->wps.ielen - 1]);
2374
2375         hostif_sme_enqueue(priv, SME_WPS_PROBE_REQUEST);
2376
2377         return 0;
2378 }
2379 #endif /* WPS */
2380
2381 static int ks_wlan_set_tx_gain(struct net_device *dev,
2382                                struct iw_request_info *info, __u32 *uwrq,
2383                                char *extra)
2384 {
2385         struct ks_wlan_private *priv = netdev_priv(dev);
2386
2387         if (priv->sleep_mode == SLP_SLEEP)
2388                 return -EPERM;
2389         /* for SLEEP MODE */
2390         if (*uwrq >= 0 && *uwrq <= 0xFF)        /* 0-255 */
2391                 priv->gain.tx_gain = (uint8_t)*uwrq;
2392         else
2393                 return -EINVAL;
2394
2395         if (priv->gain.tx_gain < 0xFF)
2396                 priv->gain.tx_mode = 1;
2397         else
2398                 priv->gain.tx_mode = 0;
2399
2400         hostif_sme_enqueue(priv, SME_SET_GAIN);
2401         return 0;
2402 }
2403
2404 static int ks_wlan_get_tx_gain(struct net_device *dev,
2405                                struct iw_request_info *info, __u32 *uwrq,
2406                                char *extra)
2407 {
2408         struct ks_wlan_private *priv = netdev_priv(dev);
2409
2410         if (priv->sleep_mode == SLP_SLEEP)
2411                 return -EPERM;
2412         /* for SLEEP MODE */
2413         *uwrq = priv->gain.tx_gain;
2414         hostif_sme_enqueue(priv, SME_GET_GAIN);
2415         return 0;
2416 }
2417
2418 static int ks_wlan_set_rx_gain(struct net_device *dev,
2419                                struct iw_request_info *info, __u32 *uwrq,
2420                                char *extra)
2421 {
2422         struct ks_wlan_private *priv = netdev_priv(dev);
2423
2424         if (priv->sleep_mode == SLP_SLEEP)
2425                 return -EPERM;
2426         /* for SLEEP MODE */
2427         if (*uwrq >= 0 && *uwrq <= 0xFF)        /* 0-255 */
2428                 priv->gain.rx_gain = (uint8_t)*uwrq;
2429         else
2430                 return -EINVAL;
2431
2432         if (priv->gain.rx_gain < 0xFF)
2433                 priv->gain.rx_mode = 1;
2434         else
2435                 priv->gain.rx_mode = 0;
2436
2437         hostif_sme_enqueue(priv, SME_SET_GAIN);
2438         return 0;
2439 }
2440
2441 static int ks_wlan_get_rx_gain(struct net_device *dev,
2442                                struct iw_request_info *info, __u32 *uwrq,
2443                                char *extra)
2444 {
2445         struct ks_wlan_private *priv = netdev_priv(dev);
2446
2447         if (priv->sleep_mode == SLP_SLEEP)
2448                 return -EPERM;
2449         /* for SLEEP MODE */
2450         *uwrq = priv->gain.rx_gain;
2451         hostif_sme_enqueue(priv, SME_GET_GAIN);
2452         return 0;
2453 }
2454
2455 static int ks_wlan_get_eeprom_cksum(struct net_device *dev,
2456                                     struct iw_request_info *info, __u32 *uwrq,
2457                                     char *extra)
2458 {
2459         struct ks_wlan_private *priv = netdev_priv(dev);
2460
2461         *uwrq = priv->eeprom_checksum;
2462         return 0;
2463 }
2464
2465 static void print_hif_event(struct net_device *dev, int event)
2466 {
2467         switch (event) {
2468         case HIF_DATA_REQ:
2469                 netdev_info(dev, "HIF_DATA_REQ\n");
2470                 break;
2471         case HIF_DATA_IND:
2472                 netdev_info(dev, "HIF_DATA_IND\n");
2473                 break;
2474         case HIF_MIB_GET_REQ:
2475                 netdev_info(dev, "HIF_MIB_GET_REQ\n");
2476                 break;
2477         case HIF_MIB_GET_CONF:
2478                 netdev_info(dev, "HIF_MIB_GET_CONF\n");
2479                 break;
2480         case HIF_MIB_SET_REQ:
2481                 netdev_info(dev, "HIF_MIB_SET_REQ\n");
2482                 break;
2483         case HIF_MIB_SET_CONF:
2484                 netdev_info(dev, "HIF_MIB_SET_CONF\n");
2485                 break;
2486         case HIF_POWER_MGMT_REQ:
2487                 netdev_info(dev, "HIF_POWER_MGMT_REQ\n");
2488                 break;
2489         case HIF_POWER_MGMT_CONF:
2490                 netdev_info(dev, "HIF_POWER_MGMT_CONF\n");
2491                 break;
2492         case HIF_START_REQ:
2493                 netdev_info(dev, "HIF_START_REQ\n");
2494                 break;
2495         case HIF_START_CONF:
2496                 netdev_info(dev, "HIF_START_CONF\n");
2497                 break;
2498         case HIF_CONNECT_IND:
2499                 netdev_info(dev, "HIF_CONNECT_IND\n");
2500                 break;
2501         case HIF_STOP_REQ:
2502                 netdev_info(dev, "HIF_STOP_REQ\n");
2503                 break;
2504         case HIF_STOP_CONF:
2505                 netdev_info(dev, "HIF_STOP_CONF\n");
2506                 break;
2507         case HIF_PS_ADH_SET_REQ:
2508                 netdev_info(dev, "HIF_PS_ADH_SET_REQ\n");
2509                 break;
2510         case HIF_PS_ADH_SET_CONF:
2511                 netdev_info(dev, "HIF_PS_ADH_SET_CONF\n");
2512                 break;
2513         case HIF_INFRA_SET_REQ:
2514                 netdev_info(dev, "HIF_INFRA_SET_REQ\n");
2515                 break;
2516         case HIF_INFRA_SET_CONF:
2517                 netdev_info(dev, "HIF_INFRA_SET_CONF\n");
2518                 break;
2519         case HIF_ADH_SET_REQ:
2520                 netdev_info(dev, "HIF_ADH_SET_REQ\n");
2521                 break;
2522         case HIF_ADH_SET_CONF:
2523                 netdev_info(dev, "HIF_ADH_SET_CONF\n");
2524                 break;
2525         case HIF_AP_SET_REQ:
2526                 netdev_info(dev, "HIF_AP_SET_REQ\n");
2527                 break;
2528         case HIF_AP_SET_CONF:
2529                 netdev_info(dev, "HIF_AP_SET_CONF\n");
2530                 break;
2531         case HIF_ASSOC_INFO_IND:
2532                 netdev_info(dev, "HIF_ASSOC_INFO_IND\n");
2533                 break;
2534         case HIF_MIC_FAILURE_REQ:
2535                 netdev_info(dev, "HIF_MIC_FAILURE_REQ\n");
2536                 break;
2537         case HIF_MIC_FAILURE_CONF:
2538                 netdev_info(dev, "HIF_MIC_FAILURE_CONF\n");
2539                 break;
2540         case HIF_SCAN_REQ:
2541                 netdev_info(dev, "HIF_SCAN_REQ\n");
2542                 break;
2543         case HIF_SCAN_CONF:
2544                 netdev_info(dev, "HIF_SCAN_CONF\n");
2545                 break;
2546         case HIF_PHY_INFO_REQ:
2547                 netdev_info(dev, "HIF_PHY_INFO_REQ\n");
2548                 break;
2549         case HIF_PHY_INFO_CONF:
2550                 netdev_info(dev, "HIF_PHY_INFO_CONF\n");
2551                 break;
2552         case HIF_SLEEP_REQ:
2553                 netdev_info(dev, "HIF_SLEEP_REQ\n");
2554                 break;
2555         case HIF_SLEEP_CONF:
2556                 netdev_info(dev, "HIF_SLEEP_CONF\n");
2557                 break;
2558         case HIF_PHY_INFO_IND:
2559                 netdev_info(dev, "HIF_PHY_INFO_IND\n");
2560                 break;
2561         case HIF_SCAN_IND:
2562                 netdev_info(dev, "HIF_SCAN_IND\n");
2563                 break;
2564         case HIF_INFRA_SET2_REQ:
2565                 netdev_info(dev, "HIF_INFRA_SET2_REQ\n");
2566                 break;
2567         case HIF_INFRA_SET2_CONF:
2568                 netdev_info(dev, "HIF_INFRA_SET2_CONF\n");
2569                 break;
2570         case HIF_ADH_SET2_REQ:
2571                 netdev_info(dev, "HIF_ADH_SET2_REQ\n");
2572                 break;
2573         case HIF_ADH_SET2_CONF:
2574                 netdev_info(dev, "HIF_ADH_SET2_CONF\n");
2575         }
2576 }
2577
2578 /* get host command history */
2579 static int ks_wlan_hostt(struct net_device *dev, struct iw_request_info *info,
2580                          __u32 *uwrq, char *extra)
2581 {
2582         int i, event;
2583         struct ks_wlan_private *priv = netdev_priv(dev);
2584
2585         for (i = 63; i >= 0; i--) {
2586                 event =
2587                     priv->hostt.buff[(priv->hostt.qtail - 1 - i) %
2588                                      SME_EVENT_BUFF_SIZE];
2589                 print_hif_event(dev, event);
2590         }
2591         return 0;
2592 }
2593
2594 /* Structures to export the Wireless Handlers */
2595
2596 static const struct iw_priv_args ks_wlan_private_args[] = {
2597 /*{ cmd, set_args, get_args, name[16] } */
2598         {KS_WLAN_GET_FIRM_VERSION, IW_PRIV_TYPE_NONE,
2599          IW_PRIV_TYPE_CHAR | (128 + 1), "GetFirmwareVer"},
2600 #ifdef WPS
2601         {KS_WLAN_SET_WPS_ENABLE, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2602          IW_PRIV_TYPE_NONE, "SetWPSEnable"},
2603         {KS_WLAN_GET_WPS_ENABLE, IW_PRIV_TYPE_NONE,
2604          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetW"},
2605         {KS_WLAN_SET_WPS_PROBE_REQ, IW_PRIV_TYPE_BYTE | 2047, IW_PRIV_TYPE_NONE,
2606          "SetWPSProbeReq"},
2607 #endif /* WPS */
2608         {KS_WLAN_SET_PREAMBLE, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2609          IW_PRIV_TYPE_NONE, "SetPreamble"},
2610         {KS_WLAN_GET_PREAMBLE, IW_PRIV_TYPE_NONE,
2611          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetPreamble"},
2612         {KS_WLAN_SET_POWER_SAVE, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2613          IW_PRIV_TYPE_NONE, "SetPowerSave"},
2614         {KS_WLAN_GET_POWER_SAVE, IW_PRIV_TYPE_NONE,
2615          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetPowerSave"},
2616         {KS_WLAN_SET_SCAN_TYPE, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2617          IW_PRIV_TYPE_NONE, "SetScanType"},
2618         {KS_WLAN_GET_SCAN_TYPE, IW_PRIV_TYPE_NONE,
2619          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetScanType"},
2620         {KS_WLAN_SET_RX_GAIN, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2621          IW_PRIV_TYPE_NONE, "SetRxGain"},
2622         {KS_WLAN_GET_RX_GAIN, IW_PRIV_TYPE_NONE,
2623          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetRxGain"},
2624         {KS_WLAN_HOSTT, IW_PRIV_TYPE_NONE, IW_PRIV_TYPE_CHAR | (128 + 1),
2625          "hostt"},
2626         {KS_WLAN_SET_BEACON_LOST, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2627          IW_PRIV_TYPE_NONE, "SetBeaconLost"},
2628         {KS_WLAN_GET_BEACON_LOST, IW_PRIV_TYPE_NONE,
2629          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetBeaconLost"},
2630         {KS_WLAN_SET_SLEEP_MODE, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2631          IW_PRIV_TYPE_NONE, "SetSleepMode"},
2632         {KS_WLAN_GET_SLEEP_MODE, IW_PRIV_TYPE_NONE,
2633          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetSleepMode"},
2634         {KS_WLAN_SET_TX_GAIN, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2635          IW_PRIV_TYPE_NONE, "SetTxGain"},
2636         {KS_WLAN_GET_TX_GAIN, IW_PRIV_TYPE_NONE,
2637          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetTxGain"},
2638         {KS_WLAN_SET_PHY_TYPE, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2639          IW_PRIV_TYPE_NONE, "SetPhyType"},
2640         {KS_WLAN_GET_PHY_TYPE, IW_PRIV_TYPE_NONE,
2641          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetPhyType"},
2642         {KS_WLAN_SET_CTS_MODE, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
2643          IW_PRIV_TYPE_NONE, "SetCtsMode"},
2644         {KS_WLAN_GET_CTS_MODE, IW_PRIV_TYPE_NONE,
2645          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetCtsMode"},
2646         {KS_WLAN_GET_EEPROM_CKSUM, IW_PRIV_TYPE_NONE,
2647          IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "GetChecksum"},
2648 };
2649
2650 static const iw_handler ks_wlan_handler[] = {
2651         (iw_handler)ks_wlan_config_commit,      /* SIOCSIWCOMMIT */
2652         (iw_handler)ks_wlan_get_name,   /* SIOCGIWNAME */
2653         (iw_handler)NULL,       /* SIOCSIWNWID */
2654         (iw_handler)NULL,       /* SIOCGIWNWID */
2655         (iw_handler)ks_wlan_set_freq,   /* SIOCSIWFREQ */
2656         (iw_handler)ks_wlan_get_freq,   /* SIOCGIWFREQ */
2657         (iw_handler)ks_wlan_set_mode,   /* SIOCSIWMODE */
2658         (iw_handler)ks_wlan_get_mode,   /* SIOCGIWMODE */
2659 #ifndef KSC_OPNOTSUPP
2660         (iw_handler)ks_wlan_set_sens,   /* SIOCSIWSENS */
2661         (iw_handler)ks_wlan_get_sens,   /* SIOCGIWSENS */
2662 #else /* KSC_OPNOTSUPP */
2663         (iw_handler)NULL,       /* SIOCSIWSENS */
2664         (iw_handler)NULL,       /* SIOCGIWSENS */
2665 #endif /* KSC_OPNOTSUPP */
2666         (iw_handler)NULL,       /* SIOCSIWRANGE */
2667         (iw_handler)ks_wlan_get_range,  /* SIOCGIWRANGE */
2668         (iw_handler)NULL,       /* SIOCSIWPRIV */
2669         (iw_handler)NULL,       /* SIOCGIWPRIV */
2670         (iw_handler)NULL,       /* SIOCSIWSTATS */
2671         (iw_handler)ks_wlan_get_iwstats,        /* SIOCGIWSTATS */
2672         (iw_handler)NULL,       /* SIOCSIWSPY */
2673         (iw_handler)NULL,       /* SIOCGIWSPY */
2674         (iw_handler)NULL,       /* SIOCSIWTHRSPY */
2675         (iw_handler)NULL,       /* SIOCGIWTHRSPY */
2676         (iw_handler)ks_wlan_set_wap,    /* SIOCSIWAP */
2677         (iw_handler)ks_wlan_get_wap,    /* SIOCGIWAP */
2678 //      (iw_handler)NULL,                      /* SIOCSIWMLME */
2679         (iw_handler)ks_wlan_set_mlme,   /* SIOCSIWMLME */
2680         (iw_handler)ks_wlan_get_aplist, /* SIOCGIWAPLIST */
2681         (iw_handler)ks_wlan_set_scan,   /* SIOCSIWSCAN */
2682         (iw_handler)ks_wlan_get_scan,   /* SIOCGIWSCAN */
2683         (iw_handler)ks_wlan_set_essid,  /* SIOCSIWESSID */
2684         (iw_handler)ks_wlan_get_essid,  /* SIOCGIWESSID */
2685         (iw_handler)ks_wlan_set_nick,   /* SIOCSIWNICKN */
2686         (iw_handler)ks_wlan_get_nick,   /* SIOCGIWNICKN */
2687         (iw_handler)NULL,       /* -- hole -- */
2688         (iw_handler)NULL,       /* -- hole -- */
2689         (iw_handler)ks_wlan_set_rate,   /* SIOCSIWRATE */
2690         (iw_handler)ks_wlan_get_rate,   /* SIOCGIWRATE */
2691         (iw_handler)ks_wlan_set_rts,    /* SIOCSIWRTS */
2692         (iw_handler)ks_wlan_get_rts,    /* SIOCGIWRTS */
2693         (iw_handler)ks_wlan_set_frag,   /* SIOCSIWFRAG */
2694         (iw_handler)ks_wlan_get_frag,   /* SIOCGIWFRAG */
2695 #ifndef KSC_OPNOTSUPP
2696         (iw_handler)ks_wlan_set_txpow,  /* SIOCSIWTXPOW */
2697         (iw_handler)ks_wlan_get_txpow,  /* SIOCGIWTXPOW */
2698         (iw_handler)ks_wlan_set_retry,  /* SIOCSIWRETRY */
2699         (iw_handler)ks_wlan_get_retry,  /* SIOCGIWRETRY */
2700 #else /* KSC_OPNOTSUPP */
2701         (iw_handler)NULL,       /* SIOCSIWTXPOW */
2702         (iw_handler)NULL,       /* SIOCGIWTXPOW */
2703         (iw_handler)NULL,       /* SIOCSIWRETRY */
2704         (iw_handler)NULL,       /* SIOCGIWRETRY */
2705 #endif /* KSC_OPNOTSUPP */
2706         (iw_handler)ks_wlan_set_encode, /* SIOCSIWENCODE */
2707         (iw_handler)ks_wlan_get_encode, /* SIOCGIWENCODE */
2708         (iw_handler)ks_wlan_set_power,  /* SIOCSIWPOWER */
2709         (iw_handler)ks_wlan_get_power,  /* SIOCGIWPOWER */
2710         (iw_handler)NULL,       /* -- hole -- */
2711         (iw_handler)NULL,       /* -- hole -- */
2712 //      (iw_handler)NULL,                      /* SIOCSIWGENIE */
2713         (iw_handler)ks_wlan_set_genie,  /* SIOCSIWGENIE */
2714         (iw_handler)NULL,       /* SIOCGIWGENIE */
2715         (iw_handler)ks_wlan_set_auth_mode,      /* SIOCSIWAUTH */
2716         (iw_handler)ks_wlan_get_auth_mode,      /* SIOCGIWAUTH */
2717         (iw_handler)ks_wlan_set_encode_ext,     /* SIOCSIWENCODEEXT */
2718         (iw_handler)ks_wlan_get_encode_ext,     /* SIOCGIWENCODEEXT */
2719         (iw_handler)ks_wlan_set_pmksa,  /* SIOCSIWPMKSA */
2720         (iw_handler)NULL,       /* -- hole -- */
2721 };
2722
2723 /* private_handler */
2724 static const iw_handler ks_wlan_private_handler[] = {
2725         (iw_handler)NULL,       /*  0 */
2726         (iw_handler)NULL,       /*  1, used to be: KS_WLAN_GET_DRIVER_VERSION */
2727         (iw_handler)NULL,       /*  2 */
2728         (iw_handler)ks_wlan_get_firmware_version,       /*  3 KS_WLAN_GET_FIRM_VERSION */
2729 #ifdef WPS
2730         (iw_handler)ks_wlan_set_wps_enable,     /*  4 KS_WLAN_SET_WPS_ENABLE  */
2731         (iw_handler)ks_wlan_get_wps_enable,     /*  5 KS_WLAN_GET_WPS_ENABLE  */
2732         (iw_handler)ks_wlan_set_wps_probe_req,  /*  6 KS_WLAN_SET_WPS_PROBE_REQ */
2733 #else
2734         (iw_handler)NULL,       /*  4 */
2735         (iw_handler)NULL,       /*  5 */
2736         (iw_handler)NULL,       /*  6 */
2737 #endif /* WPS */
2738
2739         (iw_handler)ks_wlan_get_eeprom_cksum,   /*  7 KS_WLAN_GET_CONNECT */
2740         (iw_handler)ks_wlan_set_preamble,       /*  8 KS_WLAN_SET_PREAMBLE */
2741         (iw_handler)ks_wlan_get_preamble,       /*  9 KS_WLAN_GET_PREAMBLE */
2742         (iw_handler)ks_wlan_set_power_mgmt,     /* 10 KS_WLAN_SET_POWER_SAVE */
2743         (iw_handler)ks_wlan_get_power_mgmt,     /* 11 KS_WLAN_GET_POWER_SAVE */
2744         (iw_handler)ks_wlan_set_scan_type,      /* 12 KS_WLAN_SET_SCAN_TYPE */
2745         (iw_handler)ks_wlan_get_scan_type,      /* 13 KS_WLAN_GET_SCAN_TYPE */
2746         (iw_handler)ks_wlan_set_rx_gain,        /* 14 KS_WLAN_SET_RX_GAIN */
2747         (iw_handler)ks_wlan_get_rx_gain,        /* 15 KS_WLAN_GET_RX_GAIN */
2748         (iw_handler)ks_wlan_hostt,      /* 16 KS_WLAN_HOSTT */
2749         (iw_handler)NULL,       /* 17 */
2750         (iw_handler)ks_wlan_set_beacon_lost,    /* 18 KS_WLAN_SET_BECAN_LOST */
2751         (iw_handler)ks_wlan_get_beacon_lost,    /* 19 KS_WLAN_GET_BECAN_LOST */
2752         (iw_handler)ks_wlan_set_tx_gain,        /* 20 KS_WLAN_SET_TX_GAIN */
2753         (iw_handler)ks_wlan_get_tx_gain,        /* 21 KS_WLAN_GET_TX_GAIN */
2754         (iw_handler)ks_wlan_set_phy_type,       /* 22 KS_WLAN_SET_PHY_TYPE */
2755         (iw_handler)ks_wlan_get_phy_type,       /* 23 KS_WLAN_GET_PHY_TYPE */
2756         (iw_handler)ks_wlan_set_cts_mode,       /* 24 KS_WLAN_SET_CTS_MODE */
2757         (iw_handler)ks_wlan_get_cts_mode,       /* 25 KS_WLAN_GET_CTS_MODE */
2758         (iw_handler)NULL,       /* 26 */
2759         (iw_handler)NULL,       /* 27 */
2760         (iw_handler)ks_wlan_set_sleep_mode,     /* 28 KS_WLAN_SET_SLEEP_MODE */
2761         (iw_handler)ks_wlan_get_sleep_mode,     /* 29 KS_WLAN_GET_SLEEP_MODE */
2762         (iw_handler)NULL,       /* 30 */
2763         (iw_handler)NULL,       /* 31 */
2764 };
2765
2766 static const struct iw_handler_def ks_wlan_handler_def = {
2767         .num_standard = sizeof(ks_wlan_handler) / sizeof(iw_handler),
2768         .num_private = sizeof(ks_wlan_private_handler) / sizeof(iw_handler),
2769         .num_private_args =
2770             sizeof(ks_wlan_private_args) / sizeof(struct iw_priv_args),
2771         .standard = (iw_handler *)ks_wlan_handler,
2772         .private = (iw_handler *)ks_wlan_private_handler,
2773         .private_args = (struct iw_priv_args *)ks_wlan_private_args,
2774         .get_wireless_stats = ks_get_wireless_stats,
2775 };
2776
2777 static int ks_wlan_netdev_ioctl(struct net_device *dev, struct ifreq *rq,
2778                                 int cmd)
2779 {
2780         int ret;
2781         struct iwreq *wrq = (struct iwreq *)rq;
2782
2783         switch (cmd) {
2784         case SIOCIWFIRSTPRIV + 20:      /* KS_WLAN_SET_STOP_REQ */
2785                 ret = ks_wlan_set_stop_request(dev, NULL, &wrq->u.mode, NULL);
2786                 break;
2787                 // All other calls are currently unsupported
2788         default:
2789                 ret = -EOPNOTSUPP;
2790         }
2791
2792         DPRINTK(5, "return=%d\n", ret);
2793         return ret;
2794 }
2795
2796 static
2797 struct net_device_stats *ks_wlan_get_stats(struct net_device *dev)
2798 {
2799         struct ks_wlan_private *priv = netdev_priv(dev);
2800
2801         if (priv->dev_state < DEVICE_STATE_READY)
2802                 return NULL;    /* not finished initialize */
2803
2804         return &priv->nstats;
2805 }
2806
2807 static
2808 int ks_wlan_set_mac_address(struct net_device *dev, void *addr)
2809 {
2810         struct ks_wlan_private *priv = netdev_priv(dev);
2811         struct sockaddr *mac_addr = (struct sockaddr *)addr;
2812
2813         if (netif_running(dev))
2814                 return -EBUSY;
2815         memcpy(dev->dev_addr, mac_addr->sa_data, dev->addr_len);
2816         memcpy(priv->eth_addr, mac_addr->sa_data, ETH_ALEN);
2817
2818         priv->mac_address_valid = 0;
2819         hostif_sme_enqueue(priv, SME_MACADDRESS_SET_REQUEST);
2820         netdev_info(dev, "ks_wlan:  MAC ADDRESS = %pM\n", priv->eth_addr);
2821         return 0;
2822 }
2823
2824 static
2825 void ks_wlan_tx_timeout(struct net_device *dev)
2826 {
2827         struct ks_wlan_private *priv = netdev_priv(dev);
2828
2829         DPRINTK(1, "head(%d) tail(%d)!!\n", priv->tx_dev.qhead,
2830                 priv->tx_dev.qtail);
2831         if (!netif_queue_stopped(dev))
2832                 netif_stop_queue(dev);
2833         priv->nstats.tx_errors++;
2834         netif_wake_queue(dev);
2835 }
2836
2837 static
2838 int ks_wlan_start_xmit(struct sk_buff *skb, struct net_device *dev)
2839 {
2840         struct ks_wlan_private *priv = netdev_priv(dev);
2841         int ret;
2842
2843         DPRINTK(3, "in_interrupt()=%ld\n", in_interrupt());
2844
2845         if (!skb) {
2846                 netdev_err(dev, "ks_wlan:  skb == NULL!!!\n");
2847                 return 0;
2848         }
2849         if (priv->dev_state < DEVICE_STATE_READY) {
2850                 dev_kfree_skb(skb);
2851                 return 0;       /* not finished initialize */
2852         }
2853
2854         if (netif_running(dev))
2855                 netif_stop_queue(dev);
2856
2857         ret = hostif_data_request(priv, skb);
2858         netif_trans_update(dev);
2859
2860         if (ret)
2861                 DPRINTK(4, "hostif_data_request error: =%d\n", ret);
2862
2863         return 0;
2864 }
2865
2866 void send_packet_complete(struct ks_wlan_private *priv, struct sk_buff *skb)
2867 {
2868         DPRINTK(3, "\n");
2869
2870         priv->nstats.tx_packets++;
2871
2872         if (netif_queue_stopped(priv->net_dev))
2873                 netif_wake_queue(priv->net_dev);
2874
2875         if (skb) {
2876                 priv->nstats.tx_bytes += skb->len;
2877                 dev_kfree_skb(skb);
2878         }
2879 }
2880
2881 /*
2882  * Set or clear the multicast filter for this adaptor.
2883  * This routine is not state sensitive and need not be SMP locked.
2884  */
2885 static
2886 void ks_wlan_set_multicast_list(struct net_device *dev)
2887 {
2888         struct ks_wlan_private *priv = netdev_priv(dev);
2889
2890         DPRINTK(4, "\n");
2891         if (priv->dev_state < DEVICE_STATE_READY)
2892                 return; /* not finished initialize */
2893         hostif_sme_enqueue(priv, SME_MULTICAST_REQUEST);
2894 }
2895
2896 static
2897 int ks_wlan_open(struct net_device *dev)
2898 {
2899         struct ks_wlan_private *priv = netdev_priv(dev);
2900
2901         priv->cur_rx = 0;
2902
2903         if (!priv->mac_address_valid) {
2904                 netdev_err(dev, "ks_wlan : %s Not READY !!\n", dev->name);
2905                 return -EBUSY;
2906         }
2907         netif_start_queue(dev);
2908
2909         return 0;
2910 }
2911
2912 static
2913 int ks_wlan_close(struct net_device *dev)
2914 {
2915         netif_stop_queue(dev);
2916
2917         DPRINTK(4, "%s: Shutting down ethercard, status was 0x%4.4x.\n",
2918                 dev->name, 0x00);
2919
2920         return 0;
2921 }
2922
2923 /* Operational parameters that usually are not changed. */
2924 /* Time in jiffies before concluding the transmitter is hung. */
2925 #define TX_TIMEOUT  (3 * HZ)
2926 static const unsigned char dummy_addr[] = {
2927         0x00, 0x0b, 0xe3, 0x00, 0x00, 0x00
2928 };
2929
2930 static const struct net_device_ops ks_wlan_netdev_ops = {
2931         .ndo_start_xmit = ks_wlan_start_xmit,
2932         .ndo_open = ks_wlan_open,
2933         .ndo_stop = ks_wlan_close,
2934         .ndo_do_ioctl = ks_wlan_netdev_ioctl,
2935         .ndo_set_mac_address = ks_wlan_set_mac_address,
2936         .ndo_get_stats = ks_wlan_get_stats,
2937         .ndo_tx_timeout = ks_wlan_tx_timeout,
2938         .ndo_set_rx_mode = ks_wlan_set_multicast_list,
2939 };
2940
2941 int ks_wlan_net_start(struct net_device *dev)
2942 {
2943         struct ks_wlan_private *priv;
2944         /* int rc; */
2945
2946         priv = netdev_priv(dev);
2947         priv->mac_address_valid = 0;
2948         priv->need_commit = 0;
2949
2950         priv->device_open_status = 1;
2951
2952         /* phy information update timer */
2953         atomic_set(&update_phyinfo, 0);
2954         setup_timer(&update_phyinfo_timer, ks_wlan_update_phyinfo_timeout,
2955                     (unsigned long)priv);
2956
2957         /* dummy address set */
2958         memcpy(priv->eth_addr, dummy_addr, ETH_ALEN);
2959         dev->dev_addr[0] = priv->eth_addr[0];
2960         dev->dev_addr[1] = priv->eth_addr[1];
2961         dev->dev_addr[2] = priv->eth_addr[2];
2962         dev->dev_addr[3] = priv->eth_addr[3];
2963         dev->dev_addr[4] = priv->eth_addr[4];
2964         dev->dev_addr[5] = priv->eth_addr[5];
2965         dev->dev_addr[6] = 0x00;
2966         dev->dev_addr[7] = 0x00;
2967
2968         /* The ks_wlan-specific entries in the device structure. */
2969         dev->netdev_ops = &ks_wlan_netdev_ops;
2970         dev->wireless_handlers = (struct iw_handler_def *)&ks_wlan_handler_def;
2971         dev->watchdog_timeo = TX_TIMEOUT;
2972
2973         netif_carrier_off(dev);
2974
2975         return 0;
2976 }
2977
2978 int ks_wlan_net_stop(struct net_device *dev)
2979 {
2980         struct ks_wlan_private *priv = netdev_priv(dev);
2981
2982         priv->device_open_status = 0;
2983         del_timer_sync(&update_phyinfo_timer);
2984
2985         if (netif_running(dev))
2986                 netif_stop_queue(dev);
2987
2988         return 0;
2989 }
2990
2991 /**
2992  * is_connect_status() - return true if status is 'connected'
2993  * @status: high bit is used as FORCE_DISCONNECT, low bits used for
2994  *      connect status.
2995  */
2996 bool is_connect_status(u32 status)
2997 {
2998         return (status & CONNECT_STATUS_MASK) == CONNECT_STATUS;
2999 }
3000
3001 /**
3002  * is_disconnect_status() - return true if status is 'disconnected'
3003  * @status: high bit is used as FORCE_DISCONNECT, low bits used for
3004  *      disconnect status.
3005  */
3006 bool is_disconnect_status(u32 status)
3007 {
3008         return (status & CONNECT_STATUS_MASK) == DISCONNECT_STATUS;
3009 }