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