]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/rtl8187se/ieee80211/ieee80211_softmac.c
Staging: rtl8187se/ieee80211: remove dead _RTL8187_EXT_PATCH_ code
[mv-sheeva.git] / drivers / staging / rtl8187se / ieee80211 / ieee80211_softmac.c
1 /* IEEE 802.11 SoftMAC layer
2  * Copyright (c) 2005 Andrea Merello <andreamrl@tiscali.it>
3  *
4  * Mostly extracted from the rtl8180-sa2400 driver for the
5  * in-kernel generic ieee802.11 stack.
6  *
7  * Few lines might be stolen from other part of the ieee80211
8  * stack. Copyright who own it's copyright
9  *
10  * WPA code stolen from the ipw2200 driver.
11  * Copyright who own it's copyright.
12  *
13  * released under the GPL
14  */
15
16
17 #include "ieee80211.h"
18
19 #include <linux/random.h>
20 #include <linux/delay.h>
21 #include <linux/version.h>
22 #include <asm/uaccess.h>
23
24 #ifdef ENABLE_DOT11D
25 #include "dot11d.h"
26 #endif
27 u8 rsn_authen_cipher_suite[16][4] = {
28         {0x00,0x0F,0xAC,0x00}, //Use group key, //Reserved
29         {0x00,0x0F,0xAC,0x01}, //WEP-40         //RSNA default
30         {0x00,0x0F,0xAC,0x02}, //TKIP           //NONE          //{used just as default}
31         {0x00,0x0F,0xAC,0x03}, //WRAP-historical
32         {0x00,0x0F,0xAC,0x04}, //CCMP
33         {0x00,0x0F,0xAC,0x05}, //WEP-104
34 };
35
36 short ieee80211_is_54g(struct ieee80211_network net)
37 {
38         return ((net.rates_ex_len > 0) || (net.rates_len > 4));
39 }
40
41 short ieee80211_is_shortslot(struct ieee80211_network net)
42 {
43         return (net.capability & WLAN_CAPABILITY_SHORT_SLOT);
44 }
45
46 /* returns the total length needed for pleacing the RATE MFIE
47  * tag and the EXTENDED RATE MFIE tag if needed.
48  * It encludes two bytes per tag for the tag itself and its len
49  */
50 unsigned int ieee80211_MFIE_rate_len(struct ieee80211_device *ieee)
51 {
52         unsigned int rate_len = 0;
53
54         if (ieee->modulation & IEEE80211_CCK_MODULATION)
55                 rate_len = IEEE80211_CCK_RATE_LEN + 2;
56
57         if (ieee->modulation & IEEE80211_OFDM_MODULATION)
58
59                 rate_len += IEEE80211_OFDM_RATE_LEN + 2;
60
61         return rate_len;
62 }
63
64 /* pleace the MFIE rate, tag to the memory (double) poined.
65  * Then it updates the pointer so that
66  * it points after the new MFIE tag added.
67  */
68 void ieee80211_MFIE_Brate(struct ieee80211_device *ieee, u8 **tag_p)
69 {
70         u8 *tag = *tag_p;
71
72         if (ieee->modulation & IEEE80211_CCK_MODULATION){
73                 *tag++ = MFIE_TYPE_RATES;
74                 *tag++ = 4;
75                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
76                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
77                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
78                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
79         }
80
81         /* We may add an option for custom rates that specific HW might support */
82         *tag_p = tag;
83 }
84
85 void ieee80211_MFIE_Grate(struct ieee80211_device *ieee, u8 **tag_p)
86 {
87         u8 *tag = *tag_p;
88
89                 if (ieee->modulation & IEEE80211_OFDM_MODULATION){
90
91                 *tag++ = MFIE_TYPE_RATES_EX;
92                 *tag++ = 8;
93                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
94                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
95                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
96                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
97                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
98                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
99                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
100                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
101
102         }
103
104         /* We may add an option for custom rates that specific HW might support */
105         *tag_p = tag;
106 }
107
108
109 void ieee80211_WMM_Info(struct ieee80211_device *ieee, u8 **tag_p) {
110         u8 *tag = *tag_p;
111
112         *tag++ = MFIE_TYPE_GENERIC; //0
113         *tag++ = 7;
114         *tag++ = 0x00;
115         *tag++ = 0x50;
116         *tag++ = 0xf2;
117         *tag++ = 0x02;//5
118         *tag++ = 0x00;
119         *tag++ = 0x01;
120 #ifdef SUPPORT_USPD
121         if(ieee->current_network.wmm_info & 0x80) {
122                 *tag++ = 0x0f|MAX_SP_Len;
123         } else {
124                 *tag++ = MAX_SP_Len;
125         }
126 #else
127         *tag++ = MAX_SP_Len;
128 #endif
129         *tag_p = tag;
130 }
131
132 #ifdef THOMAS_TURBO
133 void ieee80211_TURBO_Info(struct ieee80211_device *ieee, u8 **tag_p) {
134         u8 *tag = *tag_p;
135
136         *tag++ = MFIE_TYPE_GENERIC; //0
137         *tag++ = 7;
138         *tag++ = 0x00;
139         *tag++ = 0xe0;
140         *tag++ = 0x4c;
141         *tag++ = 0x01;//5
142         *tag++ = 0x02;
143         *tag++ = 0x11;
144         *tag++ = 0x00;
145
146         *tag_p = tag;
147         printk(KERN_ALERT "This is enable turbo mode IE process\n");
148 }
149 #endif
150
151 void enqueue_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb)
152 {
153         int nh;
154         nh = (ieee->mgmt_queue_head +1) % MGMT_QUEUE_NUM;
155
156 /*
157  * if the queue is full but we have newer frames then
158  * just overwrites the oldest.
159  *
160  * if (nh == ieee->mgmt_queue_tail)
161  *              return -1;
162  */
163         ieee->mgmt_queue_head = nh;
164         ieee->mgmt_queue_ring[nh] = skb;
165
166         //return 0;
167 }
168
169 struct sk_buff *dequeue_mgmt(struct ieee80211_device *ieee)
170 {
171         struct sk_buff *ret;
172
173         if(ieee->mgmt_queue_tail == ieee->mgmt_queue_head)
174                 return NULL;
175
176         ret = ieee->mgmt_queue_ring[ieee->mgmt_queue_tail];
177
178         ieee->mgmt_queue_tail =
179                 (ieee->mgmt_queue_tail+1) % MGMT_QUEUE_NUM;
180
181         return ret;
182 }
183
184 void init_mgmt_queue(struct ieee80211_device *ieee)
185 {
186         ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
187 }
188
189
190 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl);
191
192 inline void softmac_mgmt_xmit(struct sk_buff *skb, struct ieee80211_device *ieee)
193 {
194         unsigned long flags;
195         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
196         struct ieee80211_hdr_3addr  *header=
197                 (struct ieee80211_hdr_3addr  *) skb->data;
198
199
200         spin_lock_irqsave(&ieee->lock, flags);
201
202         /* called with 2nd param 0, no mgmt lock required */
203         ieee80211_sta_wakeup(ieee,0);
204
205         if(single){
206                 if(ieee->queue_stop){
207
208                         enqueue_mgmt(ieee,skb);
209                 }else{
210                         header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
211
212                         if (ieee->seq_ctrl[0] == 0xFFF)
213                                 ieee->seq_ctrl[0] = 0;
214                         else
215                                 ieee->seq_ctrl[0]++;
216
217                         /* avoid watchdog triggers */
218                         ieee->dev->trans_start = jiffies;
219                         ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
220                 }
221
222                 spin_unlock_irqrestore(&ieee->lock, flags);
223         }else{
224                 spin_unlock_irqrestore(&ieee->lock, flags);
225                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
226
227                 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
228
229                 if (ieee->seq_ctrl[0] == 0xFFF)
230                         ieee->seq_ctrl[0] = 0;
231                 else
232                         ieee->seq_ctrl[0]++;
233
234                 /* avoid watchdog triggers */
235                 ieee->dev->trans_start = jiffies;
236                 ieee->softmac_hard_start_xmit(skb,ieee->dev);
237
238                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
239         }
240 }
241
242
243 inline void softmac_ps_mgmt_xmit(struct sk_buff *skb, struct ieee80211_device *ieee)
244 {
245
246         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
247         struct ieee80211_hdr_3addr  *header =
248                 (struct ieee80211_hdr_3addr  *) skb->data;
249
250
251         if(single){
252
253                 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
254
255                 if (ieee->seq_ctrl[0] == 0xFFF)
256                         ieee->seq_ctrl[0] = 0;
257                 else
258                         ieee->seq_ctrl[0]++;
259
260                 /* avoid watchdog triggers */
261                 ieee->dev->trans_start = jiffies;
262                 ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
263
264         }else{
265
266                 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
267
268                 if (ieee->seq_ctrl[0] == 0xFFF)
269                         ieee->seq_ctrl[0] = 0;
270                 else
271                         ieee->seq_ctrl[0]++;
272
273                 /* avoid watchdog triggers */
274                 ieee->dev->trans_start = jiffies;
275                 ieee->softmac_hard_start_xmit(skb,ieee->dev);
276
277         }
278 //      dev_kfree_skb_any(skb);//edit by thomas
279 }
280 //by amy for power save
281 inline struct sk_buff *ieee80211_disassociate_skb(
282                                                         struct ieee80211_network *beacon,
283                                                         struct ieee80211_device *ieee,
284                                                         u8      asRsn)
285 {
286         struct sk_buff *skb;
287         struct ieee80211_disassoc_frame *disass;
288
289         skb = dev_alloc_skb(sizeof(struct ieee80211_disassoc_frame));
290         if (!skb)
291                 return NULL;
292
293         disass = (struct ieee80211_disassoc_frame *) skb_put(skb,sizeof(struct ieee80211_disassoc_frame));
294         disass->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_DISASSOC);
295         disass->header.duration_id = 0;
296
297         memcpy(disass->header.addr1, beacon->bssid, ETH_ALEN);
298         memcpy(disass->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
299         memcpy(disass->header.addr3, beacon->bssid, ETH_ALEN);
300
301         disass->reasoncode = asRsn;
302         return skb;
303 }
304 void
305 SendDisassociation(
306         struct ieee80211_device *ieee,
307         u8*                     asSta,
308         u8                      asRsn
309 )
310 {
311         struct ieee80211_network *beacon = &ieee->current_network;
312         struct sk_buff *skb;
313         skb = ieee80211_disassociate_skb(beacon,ieee,asRsn);
314         if (skb){
315                 softmac_mgmt_xmit(skb, ieee);
316                 //dev_kfree_skb_any(skb);//edit by thomas
317         }
318 }
319
320 //by amy for power save
321 inline struct sk_buff *ieee80211_probe_req(struct ieee80211_device *ieee)
322 {
323         unsigned int len,rate_len;
324         u8 *tag;
325         struct sk_buff *skb;
326         struct ieee80211_probe_request *req;
327
328         len = ieee->current_network.ssid_len;
329
330         rate_len = ieee80211_MFIE_rate_len(ieee);
331
332         skb = dev_alloc_skb(sizeof(struct ieee80211_probe_request) +
333                             2 + len + rate_len);
334         if (!skb)
335                 return NULL;
336
337         req = (struct ieee80211_probe_request *) skb_put(skb,sizeof(struct ieee80211_probe_request));
338         req->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
339         req->header.duration_id = 0; //FIXME: is this OK ?
340
341         memset(req->header.addr1, 0xff, ETH_ALEN);
342         memcpy(req->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
343         memset(req->header.addr3, 0xff, ETH_ALEN);
344
345         tag = (u8 *) skb_put(skb,len+2+rate_len);
346
347         *tag++ = MFIE_TYPE_SSID;
348         *tag++ = len;
349         memcpy(tag, ieee->current_network.ssid, len);
350         tag += len;
351         ieee80211_MFIE_Brate(ieee,&tag);
352         ieee80211_MFIE_Grate(ieee,&tag);
353
354         return skb;
355 }
356
357 struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee);
358
359 void ext_ieee80211_send_beacon_wq(struct ieee80211_device *ieee)
360 {
361         struct sk_buff *skb;
362
363         //unsigned long flags;
364
365         skb = ieee80211_get_beacon_(ieee);
366
367         if (skb){
368                 softmac_mgmt_xmit(skb, ieee);
369                 ieee->softmac_stats.tx_beacons++;
370                 dev_kfree_skb_any(skb);//edit by thomas
371         }
372
373
374         //printk(KERN_WARNING "[1] beacon sending!\n");
375         ieee->beacon_timer.expires = jiffies +
376                 (MSECS( ieee->current_network.beacon_interval -5));
377
378         //spin_lock_irqsave(&ieee->beacon_lock,flags);
379         if(ieee->beacon_txing)
380                 add_timer(&ieee->beacon_timer);
381         //spin_unlock_irqrestore(&ieee->beacon_lock,flags);
382 }
383
384 void ieee80211_send_beacon(struct ieee80211_device *ieee)
385 {
386         struct sk_buff *skb;
387
388         //unsigned long flags;
389
390         skb = ieee80211_get_beacon_(ieee);
391
392         if (skb){
393                 softmac_mgmt_xmit(skb, ieee);
394                 ieee->softmac_stats.tx_beacons++;
395                 dev_kfree_skb_any(skb);//edit by thomas
396         }
397
398         //printk(KERN_WARNING "[1] beacon sending!\n");
399         ieee->beacon_timer.expires = jiffies +
400                 (MSECS( ieee->current_network.beacon_interval -5));
401
402         //spin_lock_irqsave(&ieee->beacon_lock,flags);
403         if(ieee->beacon_txing)
404                 add_timer(&ieee->beacon_timer);
405         //spin_unlock_irqrestore(&ieee->beacon_lock,flags);
406 }
407
408
409 void ieee80211_send_beacon_cb(unsigned long _ieee)
410 {
411         struct ieee80211_device *ieee =
412                 (struct ieee80211_device *) _ieee;
413         unsigned long flags;
414
415         spin_lock_irqsave(&ieee->beacon_lock, flags);
416         ieee80211_send_beacon(ieee);
417         spin_unlock_irqrestore(&ieee->beacon_lock, flags);
418 }
419
420 void ieee80211_send_probe(struct ieee80211_device *ieee)
421 {
422         struct sk_buff *skb;
423
424         skb = ieee80211_probe_req(ieee);
425         if (skb){
426                 softmac_mgmt_xmit(skb, ieee);
427                 ieee->softmac_stats.tx_probe_rq++;
428                 //dev_kfree_skb_any(skb);//edit by thomas
429         }
430 }
431
432 void ieee80211_send_probe_requests(struct ieee80211_device *ieee)
433 {
434         if (ieee->active_scan && (ieee->softmac_features & IEEE_SOFTMAC_PROBERQ)){
435                 ieee80211_send_probe(ieee);
436                 ieee80211_send_probe(ieee);
437         }
438 }
439
440 /* this performs syncro scan blocking the caller until all channels
441  * in the allowed channel map has been checked.
442  */
443 void ieee80211_softmac_scan_syncro(struct ieee80211_device *ieee)
444 {
445         short ch = 0;
446 #ifdef ENABLE_DOT11D
447         u8 channel_map[MAX_CHANNEL_NUMBER+1];
448         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
449 #endif
450         down(&ieee->scan_sem);
451 //      printk("==================> Sync scan\n");
452 //      dump_chnl_map(channel_map);
453
454         while(1)
455         {
456
457                 do{
458                         ch++;
459                         if (ch > MAX_CHANNEL_NUMBER)
460                                 goto out; /* scan completed */
461
462 #ifdef ENABLE_DOT11D
463                 }while(!channel_map[ch]);
464 #else
465                 }while(!ieee->channel_map[ch]);
466 #endif
467                 /* this fuction can be called in two situations
468                  * 1- We have switched to ad-hoc mode and we are
469                  *    performing a complete syncro scan before conclude
470                  *    there are no interesting cell and to create a
471                  *    new one. In this case the link state is
472                  *    IEEE80211_NOLINK until we found an interesting cell.
473                  *    If so the ieee8021_new_net, called by the RX path
474                  *    will set the state to IEEE80211_LINKED, so we stop
475                  *    scanning
476                  * 2- We are linked and the root uses run iwlist scan.
477                  *    So we switch to IEEE80211_LINKED_SCANNING to remember
478                  *    that we are still logically linked (not interested in
479                  *    new network events, despite for updating the net list,
480                  *    but we are temporarly 'unlinked' as the driver shall
481                  *    not filter RX frames and the channel is changing.
482                  * So the only situation in witch are interested is to check
483                  * if the state become LINKED because of the #1 situation
484                  */
485
486                 if (ieee->state == IEEE80211_LINKED)
487                         goto out;
488
489                 ieee->set_chan(ieee->dev, ch);
490 //              printk("=====>channel=%d   ",ch);
491 #ifdef ENABLE_DOT11D
492                 if(channel_map[ch] == 1)
493 #endif
494                 {
495 //                      printk("====send probe request\n");
496                         ieee80211_send_probe_requests(ieee);
497                 }
498                 /* this prevent excessive time wait when we
499                  * need to wait for a syncro scan to end..
500                  */
501                 if (ieee->sync_scan_hurryup)
502                         goto out;
503
504
505                 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
506
507         }
508 out:
509         ieee->sync_scan_hurryup = 0;
510         up(&ieee->scan_sem);
511 #ifdef ENABLE_DOT11D
512         if(IS_DOT11D_ENABLE(ieee))
513                 DOT11D_ScanComplete(ieee);
514 #endif
515 }
516
517 void ieee80211_softmac_ips_scan_syncro(struct ieee80211_device *ieee)
518 {
519         int ch;
520         unsigned int watch_dog = 0;
521 #ifdef ENABLE_DOT11D
522         u8 channel_map[MAX_CHANNEL_NUMBER+1];
523         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
524 #endif
525         down(&ieee->scan_sem);
526         ch = ieee->current_network.channel;
527 //              if(ieee->sync_scan_hurryup)
528 //      {
529
530 //              printk("stop scan sync\n");
531 //              goto out;
532 //      }
533 //      printk("=======hh===============>ips scan\n");
534         while(1)
535         {
536                 /* this fuction can be called in two situations
537                  * 1- We have switched to ad-hoc mode and we are
538                  *    performing a complete syncro scan before conclude
539                  *    there are no interesting cell and to create a
540                  *    new one. In this case the link state is
541                  *    IEEE80211_NOLINK until we found an interesting cell.
542                  *    If so the ieee8021_new_net, called by the RX path
543                  *    will set the state to IEEE80211_LINKED, so we stop
544                  *    scanning
545                  * 2- We are linked and the root uses run iwlist scan.
546                  *    So we switch to IEEE80211_LINKED_SCANNING to remember
547                  *    that we are still logically linked (not interested in
548                  *    new network events, despite for updating the net list,
549                  *    but we are temporarly 'unlinked' as the driver shall
550                  *    not filter RX frames and the channel is changing.
551                  * So the only situation in witch are interested is to check
552                  * if the state become LINKED because of the #1 situation
553                  */
554                 if (ieee->state == IEEE80211_LINKED)
555                 {
556                         goto out;
557                 }
558 #ifdef ENABLE_DOT11D
559                 if(channel_map[ieee->current_network.channel] > 0)
560 #endif
561                 {
562                         ieee->set_chan(ieee->dev, ieee->current_network.channel);
563 //                      printk("======>channel=%d  ",ieee->current_network.channel);
564                 }
565 #ifdef ENABLE_DOT11D
566                 if(channel_map[ieee->current_network.channel] == 1)
567 #endif
568                 {
569 //                      printk("====send probe request\n");
570                         ieee80211_send_probe_requests(ieee);
571                 }
572                 /* this prevent excessive time wait when we
573                  * need to wait for a syncro scan to end..
574                  */
575 //                if (ieee->sync_scan_hurryup)
576 //                        goto out;
577
578                 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
579
580                 do{
581                         if (watch_dog++ >= MAX_CHANNEL_NUMBER)
582                 //      if (++watch_dog >= 15);//MAX_CHANNEL_NUMBER)  //YJ,modified,080630
583                                 goto out; /* scan completed */
584
585                         ieee->current_network.channel = (ieee->current_network.channel + 1)%MAX_CHANNEL_NUMBER;
586 #ifdef ENABLE_DOT11D
587                 }while(!channel_map[ieee->current_network.channel]);
588 #else
589                 }while(!ieee->channel_map[ieee->current_network.channel]);
590 #endif
591         }
592 out:
593         //ieee->sync_scan_hurryup = 0;
594         //ieee->set_chan(ieee->dev, ch);
595         //ieee->current_network.channel = ch;
596         ieee->actscanning = false;
597         up(&ieee->scan_sem);
598 #ifdef ENABLE_DOT11D
599         if(IS_DOT11D_ENABLE(ieee))
600                 DOT11D_ScanComplete(ieee);
601 #endif
602 }
603
604
605 #if 0
606 /* called both by wq with ieee->lock held */
607 void ieee80211_softmac_scan(struct ieee80211_device *ieee)
608 {
609         short watchdog = 0;
610
611         do{
612                 ieee->current_network.channel =
613                         (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
614                 if (watchdog++ > MAX_CHANNEL_NUMBER)
615                                 return; /* no good chans */
616
617         }while(!ieee->channel_map[ieee->current_network.channel]);
618
619
620         schedule_work(&ieee->softmac_scan_wq);
621 }
622 #endif
623 #ifdef ENABLE_IPS
624 void ieee80211_softmac_scan_wq(struct work_struct *work)
625 {
626         struct delayed_work *dwork = to_delayed_work(work);
627         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, softmac_scan_wq);
628         static short watchdog = 0;
629 #ifdef ENABLE_DOT11D
630         u8 channel_map[MAX_CHANNEL_NUMBER+1];
631         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
632 #endif
633 //      printk("ieee80211_softmac_scan_wq ENABLE_IPS\n");
634 //      printk("in %s\n",__func__);
635         down(&ieee->scan_sem);
636
637         do{
638                 ieee->current_network.channel =
639                         (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
640                 if (watchdog++ > MAX_CHANNEL_NUMBER)
641                                 goto out; /* no good chans */
642
643 #ifdef ENABLE_DOT11D
644         }while(!channel_map[ieee->current_network.channel]);
645 #else
646         }while(!ieee->channel_map[ieee->current_network.channel]);
647 #endif
648
649         //printk("current_network.channel:%d\n", ieee->current_network.channel);
650         if (ieee->scanning == 0 )
651         {
652                 printk("error out, scanning = 0\n");
653                 goto out;
654         }
655         ieee->set_chan(ieee->dev, ieee->current_network.channel);
656 #ifdef ENABLE_DOT11D
657         if(channel_map[ieee->current_network.channel] == 1)
658 #endif
659                 ieee80211_send_probe_requests(ieee);
660
661         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, IEEE80211_SOFTMAC_SCAN_TIME);
662         up(&ieee->scan_sem);
663         return;
664 out:
665         ieee->actscanning = false;
666         watchdog = 0;
667         ieee->scanning = 0;
668         up(&ieee->scan_sem);
669
670 #ifdef ENABLE_DOT11D
671         if(IS_DOT11D_ENABLE(ieee))
672                 DOT11D_ScanComplete(ieee);
673 #endif
674         return;
675 }
676 #else
677 void ieee80211_softmac_scan_wq(struct work_struct *work)
678 {
679         struct delayed_work *dwork = to_delayed_work(work);
680         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, softmac_scan_wq);
681         short watchdog = 0;
682 #ifdef ENABLE_DOT11D
683         u8 channel_map[MAX_CHANNEL_NUMBER+1];
684         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
685 #endif
686 //      printk("enter scan wq,watchdog is %d\n",watchdog);
687         down(&ieee->scan_sem);
688
689         do{
690                 ieee->current_network.channel =
691                         (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
692                 if (watchdog++ > MAX_CHANNEL_NUMBER)
693                                 goto out; /* no good chans */
694
695 #ifdef ENABLE_DOT11D
696         }while(!channel_map[ieee->current_network.channel]);
697 #else
698         }while(!ieee->channel_map[ieee->current_network.channel]);
699 #endif
700
701 //      printk("current_network.channel:%d\n", ieee->current_network.channel);
702         if (ieee->scanning == 0 )
703         {
704                 printk("error out, scanning = 0\n");
705                 goto out;
706         }
707         ieee->set_chan(ieee->dev, ieee->current_network.channel);
708 #ifdef ENABLE_DOT11D
709         if(channel_map[ieee->current_network.channel] == 1)
710 #endif
711                 ieee80211_send_probe_requests(ieee);
712
713         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, IEEE80211_SOFTMAC_SCAN_TIME);
714 out:
715         up(&ieee->scan_sem);
716 #ifdef ENABLE_DOT11D
717         if(IS_DOT11D_ENABLE(ieee))
718                 DOT11D_ScanComplete(ieee);
719 #endif
720 }
721
722 #endif
723
724 void ieee80211_beacons_start(struct ieee80211_device *ieee)
725 {
726         unsigned long flags;
727
728         spin_lock_irqsave(&ieee->beacon_lock,flags);
729
730         ieee->beacon_txing = 1;
731         ieee80211_send_beacon(ieee);
732
733         spin_unlock_irqrestore(&ieee->beacon_lock,flags);
734 }
735
736 void ieee80211_beacons_stop(struct ieee80211_device *ieee)
737 {
738         unsigned long flags;
739
740         spin_lock_irqsave(&ieee->beacon_lock,flags);
741
742         ieee->beacon_txing = 0;
743         del_timer_sync(&ieee->beacon_timer);
744
745         spin_unlock_irqrestore(&ieee->beacon_lock,flags);
746
747 }
748
749
750 void ieee80211_stop_send_beacons(struct ieee80211_device *ieee)
751 {
752         if(ieee->stop_send_beacons)
753                 ieee->stop_send_beacons(ieee->dev);
754         if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
755                 ieee80211_beacons_stop(ieee);
756 }
757
758
759 void ieee80211_start_send_beacons(struct ieee80211_device *ieee)
760 {
761         if(ieee->start_send_beacons)
762                 ieee->start_send_beacons(ieee->dev);
763         if(ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
764                 ieee80211_beacons_start(ieee);
765 }
766
767
768 void ieee80211_softmac_stop_scan(struct ieee80211_device *ieee)
769 {
770 //      unsigned long flags;
771
772         //ieee->sync_scan_hurryup = 1;
773
774         down(&ieee->scan_sem);
775 //      spin_lock_irqsave(&ieee->lock, flags);
776
777         if (ieee->scanning == 1){
778                 ieee->scanning = 0;
779                 //del_timer_sync(&ieee->scan_timer);
780                 cancel_delayed_work(&ieee->softmac_scan_wq);
781         }
782
783 //      spin_unlock_irqrestore(&ieee->lock, flags);
784         up(&ieee->scan_sem);
785 }
786
787 void ieee80211_stop_scan(struct ieee80211_device *ieee)
788 {
789         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
790                 ieee80211_softmac_stop_scan(ieee);
791         else
792                 ieee->stop_scan(ieee->dev);
793 }
794
795 /* called with ieee->lock held */
796 void ieee80211_start_scan(struct ieee80211_device *ieee)
797 {
798 #ifdef ENABLE_DOT11D
799         if(IS_DOT11D_ENABLE(ieee) )
800         {
801                 if(IS_COUNTRY_IE_VALID(ieee))
802                 {
803                         RESET_CIE_WATCHDOG(ieee);
804                 }
805         }
806 #endif
807         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN){
808                 if (ieee->scanning == 0)
809                 {
810                         ieee->scanning = 1;
811                         //ieee80211_softmac_scan(ieee);
812                 //      queue_work(ieee->wq, &ieee->softmac_scan_wq);
813                 //care this,1203,2007,by lawrence
814 #if 1
815                         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq,0);
816 #endif
817                 }
818         }else
819                 ieee->start_scan(ieee->dev);
820
821 }
822
823 /* called with wx_sem held */
824 void ieee80211_start_scan_syncro(struct ieee80211_device *ieee)
825 {
826 #ifdef ENABLE_DOT11D
827         if(IS_DOT11D_ENABLE(ieee) )
828         {
829                 if(IS_COUNTRY_IE_VALID(ieee))
830                 {
831                         RESET_CIE_WATCHDOG(ieee);
832                 }
833         }
834 #endif
835         ieee->sync_scan_hurryup = 0;
836
837         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
838                 ieee80211_softmac_scan_syncro(ieee);
839         else
840                 ieee->scan_syncro(ieee->dev);
841
842 }
843
844 inline struct sk_buff *ieee80211_authentication_req(struct ieee80211_network *beacon,
845         struct ieee80211_device *ieee, int challengelen)
846 {
847         struct sk_buff *skb;
848         struct ieee80211_authentication *auth;
849
850         skb = dev_alloc_skb(sizeof(struct ieee80211_authentication) + challengelen);
851
852         if (!skb) return NULL;
853
854         auth = (struct ieee80211_authentication *)
855                 skb_put(skb, sizeof(struct ieee80211_authentication));
856
857         auth->header.frame_ctl = IEEE80211_STYPE_AUTH;
858         if (challengelen) auth->header.frame_ctl |= IEEE80211_FCTL_WEP;
859
860         auth->header.duration_id = 0x013a; //FIXME
861
862         memcpy(auth->header.addr1, beacon->bssid, ETH_ALEN);
863         memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
864         memcpy(auth->header.addr3, beacon->bssid, ETH_ALEN);
865
866         auth->algorithm = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
867
868         auth->transaction = cpu_to_le16(ieee->associate_seq);
869         ieee->associate_seq++;
870
871         auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
872
873         return skb;
874
875 }
876
877 static struct sk_buff* ieee80211_probe_resp(struct ieee80211_device *ieee, u8 *dest)
878 {
879         u8 *tag;
880         int beacon_size;
881         struct ieee80211_probe_response *beacon_buf;
882         struct sk_buff *skb;
883         int encrypt;
884         int atim_len,erp_len;
885         struct ieee80211_crypt_data* crypt;
886
887         char *ssid = ieee->current_network.ssid;
888         int ssid_len = ieee->current_network.ssid_len;
889         int rate_len = ieee->current_network.rates_len+2;
890         int rate_ex_len = ieee->current_network.rates_ex_len;
891         int wpa_ie_len = ieee->wpa_ie_len;
892         if(rate_ex_len > 0) rate_ex_len+=2;
893
894         if(ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
895                 atim_len = 4;
896         else
897                 atim_len = 0;
898
899         if(ieee80211_is_54g(ieee->current_network))
900                 erp_len = 3;
901         else
902                 erp_len = 0;
903
904         beacon_size = sizeof(struct ieee80211_probe_response)+
905                 ssid_len
906                 +3 //channel
907                 +rate_len
908                 +rate_ex_len
909                 +atim_len
910                 +wpa_ie_len
911                 +erp_len;
912
913         skb = dev_alloc_skb(beacon_size);
914
915         if (!skb)
916                 return NULL;
917
918         beacon_buf = (struct ieee80211_probe_response*) skb_put(skb, beacon_size);
919
920         memcpy (beacon_buf->header.addr1, dest,ETH_ALEN);
921         memcpy (beacon_buf->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
922         memcpy (beacon_buf->header.addr3, ieee->current_network.bssid, ETH_ALEN);
923
924         beacon_buf->header.duration_id = 0; //FIXME
925         beacon_buf->beacon_interval =
926                 cpu_to_le16(ieee->current_network.beacon_interval);
927         beacon_buf->capability =
928                 cpu_to_le16(ieee->current_network.capability & WLAN_CAPABILITY_IBSS);
929
930         if(ieee->short_slot && (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_SLOT))
931                 cpu_to_le16((beacon_buf->capability |= WLAN_CAPABILITY_SHORT_SLOT));
932
933         crypt = ieee->crypt[ieee->tx_keyidx];
934
935         encrypt = ieee->host_encrypt && crypt && crypt->ops &&
936                 ((0 == strcmp(crypt->ops->name, "WEP")) || wpa_ie_len);
937
938         if (encrypt)
939                 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
940
941
942         beacon_buf->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_RESP);
943
944         beacon_buf->info_element.id = MFIE_TYPE_SSID;
945         beacon_buf->info_element.len = ssid_len;
946
947         tag = (u8*) beacon_buf->info_element.data;
948
949         memcpy(tag, ssid, ssid_len);
950
951         tag += ssid_len;
952
953         *(tag++) = MFIE_TYPE_RATES;
954         *(tag++) = rate_len-2;
955         memcpy(tag,ieee->current_network.rates,rate_len-2);
956         tag+=rate_len-2;
957
958         *(tag++) = MFIE_TYPE_DS_SET;
959         *(tag++) = 1;
960         *(tag++) = ieee->current_network.channel;
961
962         if(atim_len){
963                 *(tag++) = MFIE_TYPE_IBSS_SET;
964                 *(tag++) = 2;
965                 *((u16*)(tag)) = cpu_to_le16(ieee->current_network.atim_window);
966                 tag+=2;
967         }
968
969         if(erp_len){
970                 *(tag++) = MFIE_TYPE_ERP;
971                 *(tag++) = 1;
972                 *(tag++) = 0;
973         }
974
975         if(rate_ex_len){
976                 *(tag++) = MFIE_TYPE_RATES_EX;
977                 *(tag++) = rate_ex_len-2;
978                 memcpy(tag,ieee->current_network.rates_ex,rate_ex_len-2);
979                 tag+=rate_ex_len-2;
980         }
981
982         if (wpa_ie_len)
983         {
984                 if (ieee->iw_mode == IW_MODE_ADHOC)
985                 {//as Windows will set pairwise key same as the group key which is not allowed in Linux, so set this for IOT issue. WB 2008.07.07
986                         memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
987                 }
988
989                 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
990         }
991
992         skb->dev = ieee->dev;
993         return skb;
994 }
995
996 struct sk_buff* ieee80211_assoc_resp(struct ieee80211_device *ieee, u8 *dest)
997 {
998         struct sk_buff *skb;
999         u8* tag;
1000
1001         struct ieee80211_crypt_data* crypt;
1002         struct ieee80211_assoc_response_frame *assoc;
1003         short encrypt;
1004
1005         unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
1006         int len = sizeof(struct ieee80211_assoc_response_frame) + rate_len;
1007
1008         skb = dev_alloc_skb(len);
1009
1010         if (!skb)
1011                 return NULL;
1012
1013         assoc = (struct ieee80211_assoc_response_frame *)
1014                 skb_put(skb,sizeof(struct ieee80211_assoc_response_frame));
1015
1016         assoc->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP);
1017         memcpy(assoc->header.addr1, dest,ETH_ALEN);
1018         memcpy(assoc->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
1019         memcpy(assoc->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1020         assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
1021                 WLAN_CAPABILITY_BSS : WLAN_CAPABILITY_IBSS);
1022
1023
1024         if(ieee->short_slot)
1025                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
1026
1027         if (ieee->host_encrypt)
1028                 crypt = ieee->crypt[ieee->tx_keyidx];
1029         else crypt = NULL;
1030
1031         encrypt = ( crypt && crypt->ops);
1032
1033         if (encrypt)
1034                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1035
1036         assoc->status = 0;
1037         assoc->aid = cpu_to_le16(ieee->assoc_id);
1038         if (ieee->assoc_id == 0x2007) ieee->assoc_id=0;
1039         else ieee->assoc_id++;
1040
1041         tag = (u8*) skb_put(skb, rate_len);
1042
1043         ieee80211_MFIE_Brate(ieee, &tag);
1044         ieee80211_MFIE_Grate(ieee, &tag);
1045
1046         return skb;
1047 }
1048
1049 struct sk_buff* ieee80211_auth_resp(struct ieee80211_device *ieee,int status, u8 *dest)
1050 {
1051         struct sk_buff *skb;
1052         struct ieee80211_authentication *auth;
1053
1054         skb = dev_alloc_skb(sizeof(struct ieee80211_authentication)+1);
1055
1056         if (!skb)
1057                 return NULL;
1058
1059         skb->len = sizeof(struct ieee80211_authentication);
1060
1061         auth = (struct ieee80211_authentication *)skb->data;
1062
1063         auth->status = cpu_to_le16(status);
1064         auth->transaction = cpu_to_le16(2);
1065         auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
1066
1067         memcpy(auth->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
1068         memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1069         memcpy(auth->header.addr1, dest, ETH_ALEN);
1070         auth->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_AUTH);
1071         return skb;
1072
1073
1074 }
1075
1076 struct sk_buff* ieee80211_null_func(struct ieee80211_device *ieee,short pwr)
1077 {
1078         struct sk_buff *skb;
1079         struct ieee80211_hdr_3addr* hdr;
1080
1081         skb = dev_alloc_skb(sizeof(struct ieee80211_hdr_3addr));
1082
1083         if (!skb)
1084                 return NULL;
1085
1086         hdr = (struct ieee80211_hdr_3addr*)skb_put(skb,sizeof(struct ieee80211_hdr_3addr));
1087
1088         memcpy(hdr->addr1, ieee->current_network.bssid, ETH_ALEN);
1089         memcpy(hdr->addr2, ieee->dev->dev_addr, ETH_ALEN);
1090         memcpy(hdr->addr3, ieee->current_network.bssid, ETH_ALEN);
1091
1092         hdr->frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
1093                 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS |
1094                 (pwr ? IEEE80211_FCTL_PM:0));
1095
1096         return skb;
1097
1098
1099 }
1100
1101
1102 void ieee80211_resp_to_assoc_rq(struct ieee80211_device *ieee, u8* dest)
1103 {
1104         struct sk_buff *buf = ieee80211_assoc_resp(ieee, dest);
1105
1106         if (buf){
1107                 softmac_mgmt_xmit(buf, ieee);
1108                 dev_kfree_skb_any(buf);//edit by thomas
1109         }
1110 }
1111
1112
1113 void ieee80211_resp_to_auth(struct ieee80211_device *ieee, int s, u8* dest)
1114 {
1115         struct sk_buff *buf = ieee80211_auth_resp(ieee, s, dest);
1116
1117         if (buf){
1118                 softmac_mgmt_xmit(buf, ieee);
1119                 dev_kfree_skb_any(buf);//edit by thomas
1120         }
1121 }
1122
1123
1124 void ieee80211_resp_to_probe(struct ieee80211_device *ieee, u8 *dest)
1125 {
1126
1127         struct sk_buff *buf = ieee80211_probe_resp(ieee, dest);
1128
1129         if (buf) {
1130                 softmac_mgmt_xmit(buf, ieee);
1131                 dev_kfree_skb_any(buf);//edit by thomas
1132         }
1133 }
1134
1135
1136 inline struct sk_buff *ieee80211_association_req(struct ieee80211_network *beacon,struct ieee80211_device *ieee)
1137 {
1138         struct sk_buff *skb;
1139         //unsigned long flags;
1140
1141         struct ieee80211_assoc_request_frame *hdr;
1142         u8 *tag;
1143         //short info_addr = 0;
1144         //int i;
1145         //u16 suite_count = 0;
1146         //u8 suit_select = 0;
1147         unsigned int wpa_len = beacon->wpa_ie_len;
1148         //struct net_device *dev = ieee->dev;
1149         //union iwreq_data wrqu;
1150         //u8 *buff;
1151         //u8 *p;
1152 #if 1
1153         // for testing purpose
1154         unsigned int rsn_len = beacon->rsn_ie_len;
1155 #else
1156         unsigned int rsn_len = beacon->rsn_ie_len - 4;
1157 #endif
1158         unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
1159         unsigned int wmm_info_len = beacon->QoS_Enable?9:0;
1160 #ifdef THOMAS_TURBO
1161         unsigned int turbo_info_len = beacon->Turbo_Enable?9:0;
1162 #endif
1163
1164         u8  encry_proto = ieee->wpax_type_notify & 0xff;
1165         //u8  pairwise_type = (ieee->wpax_type_notify >> 8) & 0xff;
1166         //u8  authen_type = (ieee->wpax_type_notify >> 16) & 0xff;
1167
1168         int len = 0;
1169
1170         //[0] Notify type of encryption: WPA/WPA2
1171         //[1] pair wise type
1172         //[2] authen type
1173         if(ieee->wpax_type_set) {
1174                 if (IEEE_PROTO_WPA == encry_proto) {
1175                         rsn_len = 0;
1176                 } else if (IEEE_PROTO_RSN == encry_proto) {
1177                         wpa_len = 0;
1178                 }
1179         }
1180 #ifdef THOMAS_TURBO
1181         len = sizeof(struct ieee80211_assoc_request_frame)+
1182                 + beacon->ssid_len//essid tagged val
1183                 + rate_len//rates tagged val
1184                 + wpa_len
1185                 + rsn_len
1186                 + wmm_info_len
1187                 + turbo_info_len;
1188 #else
1189         len = sizeof(struct ieee80211_assoc_request_frame)+
1190                 + beacon->ssid_len//essid tagged val
1191                 + rate_len//rates tagged val
1192                 + wpa_len
1193                 + rsn_len
1194                 + wmm_info_len;
1195 #endif
1196
1197         skb = dev_alloc_skb(len);
1198
1199         if (!skb)
1200                 return NULL;
1201
1202         hdr = (struct ieee80211_assoc_request_frame *)
1203                 skb_put(skb, sizeof(struct ieee80211_assoc_request_frame));
1204
1205
1206         hdr->header.frame_ctl = IEEE80211_STYPE_ASSOC_REQ;
1207         hdr->header.duration_id= 37; //FIXME
1208         memcpy(hdr->header.addr1, beacon->bssid, ETH_ALEN);
1209         memcpy(hdr->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1210         memcpy(hdr->header.addr3, beacon->bssid, ETH_ALEN);
1211         memcpy(ieee->ap_mac_addr, beacon->bssid, ETH_ALEN);//for HW security, John
1212
1213         hdr->capability = cpu_to_le16(WLAN_CAPABILITY_BSS);
1214         if (beacon->capability & WLAN_CAPABILITY_PRIVACY )
1215                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1216         if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1217                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1218
1219         if(ieee->short_slot)
1220                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
1221
1222         hdr->listen_interval = 0xa; //FIXME
1223
1224         hdr->info_element.id = MFIE_TYPE_SSID;
1225
1226         hdr->info_element.len = beacon->ssid_len;
1227         tag = skb_put(skb, beacon->ssid_len);
1228         memcpy(tag, beacon->ssid, beacon->ssid_len);
1229
1230         tag = skb_put(skb, rate_len);
1231
1232         ieee80211_MFIE_Brate(ieee, &tag);
1233         ieee80211_MFIE_Grate(ieee, &tag);
1234
1235         //add rsn==0 condition for ap's mix security mode(wpa+wpa2), john2007.8.9
1236         //choose AES encryption as default algorithm while using mixed mode
1237 #if 0
1238         if(rsn_len == 0){
1239
1240                 tag = skb_put(skb,wpa_len);
1241
1242                 if(wpa_len) {
1243
1244
1245                   //{add by david. 2006.8.31
1246                   //fix linksys compatibility bug
1247                   //}
1248                   if(wpa_len > 24) {//22+2, mean include the capability
1249                         beacon->wpa_ie[wpa_len - 2] = 0;
1250                   }
1251                 //multicast cipher OUI
1252                   if(  beacon->wpa_ie[11]==0x2      ){ //0x0050f202 is the oui of tkip
1253                   ieee->broadcast_key_type = KEY_TYPE_TKIP;
1254                 }
1255                   else if(  beacon->wpa_ie[11]==0x4      ){//0x0050f204 is the oui of ccmp
1256                   ieee->broadcast_key_type = KEY_TYPE_CCMP;
1257                 }
1258                 //unicast cipher OUI
1259                   if(   beacon->wpa_ie[14]==0
1260                         && beacon->wpa_ie[15]==0x50
1261                         && beacon->wpa_ie[16]==0xf2
1262                         && beacon->wpa_ie[17]==0x2      ){ //0x0050f202 is the oui of tkip
1263                   ieee->pairwise_key_type = KEY_TYPE_TKIP;
1264                 }
1265
1266                   else if(   beacon->wpa_ie[14]==0
1267                         && beacon->wpa_ie[15]==0x50
1268                         && beacon->wpa_ie[16]==0xf2
1269                         && beacon->wpa_ie[17]==0x4      ){//0x0050f204 is the oui of ccmp
1270                   ieee->pairwise_key_type = KEY_TYPE_CCMP;
1271                 }
1272                 //indicate the wpa_ie content to WPA_SUPPLICANT
1273                 buff = kmalloc(IW_CUSTOM_MAX, GFP_ATOMIC);
1274                 memset(buff, 0, IW_CUSTOM_MAX);
1275                 p=buff;
1276                 p += sprintf(p, "ASSOCINFO(ReqIEs=");
1277                 for(i=0;i<wpa_len;i++){
1278                         p += sprintf(p, "%02x", beacon->wpa_ie[i]);
1279                 }
1280                 p += sprintf(p, ")");
1281                 memset(&wrqu, 0, sizeof(wrqu) );
1282                 wrqu.data.length = p - buff;
1283
1284                 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buff);
1285                   memcpy(tag,beacon->wpa_ie,wpa_len);
1286                 }
1287
1288         }
1289
1290         if(rsn_len > 22) {
1291
1292                                                 if(     beacon->rsn_ie[4]==0x0 &&
1293                                 beacon->rsn_ie[5]==0xf &&
1294                                 beacon->rsn_ie[6]==0xac){
1295
1296                                 switch(beacon->rsn_ie[7]){
1297                                         case 0x1:
1298                                                 ieee->broadcast_key_type = KEY_TYPE_WEP40;
1299                                                 break;
1300                                         case 0x2:
1301                                                 ieee->broadcast_key_type = KEY_TYPE_TKIP;
1302                                                 break;
1303                                         case 0x4:
1304                                                 ieee->broadcast_key_type = KEY_TYPE_CCMP;
1305                                                 break;
1306                                         case 0x5:
1307                                                 ieee->broadcast_key_type = KEY_TYPE_WEP104;
1308                                                 break;
1309                                         default:
1310                                                 printk("fault suite type in RSN broadcast key\n");
1311                                                 break;
1312                                 }
1313                         }
1314
1315                         if(     beacon->rsn_ie[10]==0x0 &&
1316                                 beacon->rsn_ie[11]==0xf &&
1317                                 beacon->rsn_ie[12]==0xac){
1318                                 if(beacon->rsn_ie[8]==1){//not mixed mode
1319                                         switch(beacon->rsn_ie[13]){
1320                                                 case 0x2:
1321                                                         ieee->pairwise_key_type = KEY_TYPE_TKIP;
1322                                                         break;
1323                                                 case 0x4:
1324                                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
1325                                                         break;
1326                                                 default:
1327                                                         printk("fault suite type in RSN pairwise key\n");
1328                                                         break;
1329                                         }
1330                                 }
1331                                 else if(beacon->rsn_ie[8]==2){//mixed mode
1332                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
1333                                 }
1334                         }
1335
1336
1337
1338                 tag = skb_put(skb,22);
1339                 memcpy(tag,(beacon->rsn_ie + info_addr),8);
1340                 tag[1] =  20;
1341                 tag += 8;
1342                 info_addr += 8;
1343
1344                 spin_lock_irqsave(&ieee->wpax_suitlist_lock,flags);
1345                 for (i = 0; i < 2; i++) {
1346                         tag[0] = 1;
1347                         tag[1] = 0;
1348                         tag += 2;
1349                         suite_count = beacon->rsn_ie[info_addr] + \
1350                                       (beacon->rsn_ie[info_addr + 1] << 8);
1351                         info_addr += 2;
1352                         if(1 == suite_count) {
1353                                 memcpy(tag,(beacon->rsn_ie + info_addr),4);
1354                                 info_addr += 4;
1355                         } else {
1356                                 // if the wpax_type_notify has been set by the application,
1357                                 // just use it, otherwise just use the default one.
1358                                 if(ieee->wpax_type_set) {
1359                                         suit_select = ((0 == i) ? pairwise_type:authen_type)&0x0f ;
1360                                         memcpy(tag,rsn_authen_cipher_suite[suit_select],4);
1361                                 } else {
1362                                         //default set as ccmp, or none authentication
1363                                         if(i == 0) {
1364                                                 memcpy(tag,rsn_authen_cipher_suite[4],4);
1365                                         } else {
1366                                                 memcpy(tag,rsn_authen_cipher_suite[2],4);
1367                                         }
1368
1369                                 }
1370
1371                                 info_addr += (suite_count * 4);
1372                         }
1373                         tag += 4;
1374                 }
1375                 spin_unlock_irqrestore(&ieee->wpax_suitlist_lock,flags);
1376
1377                 tag[0] = 0;
1378                 tag[1] = beacon->rsn_ie[info_addr+1];
1379
1380         } else {
1381                 tag = skb_put(skb,rsn_len);
1382                 if(rsn_len) {
1383
1384
1385                         if(     beacon->rsn_ie[4]==0x0 &&
1386                                 beacon->rsn_ie[5]==0xf &&
1387                                 beacon->rsn_ie[6]==0xac){
1388                                 switch(beacon->rsn_ie[7]){
1389                                         case 0x1:
1390                                                 ieee->broadcast_key_type = KEY_TYPE_WEP40;
1391                                                 break;
1392                                         case 0x2:
1393                                                 ieee->broadcast_key_type = KEY_TYPE_TKIP;
1394                                                 break;
1395                                         case 0x4:
1396                                                 ieee->broadcast_key_type = KEY_TYPE_CCMP;
1397                                                 break;
1398                                         case 0x5:
1399                                                 ieee->broadcast_key_type = KEY_TYPE_WEP104;
1400                                                 break;
1401                                         default:
1402                                                 printk("fault suite type in RSN broadcast key\n");
1403                                                 break;
1404                                 }
1405                         }
1406                         if(     beacon->rsn_ie[10]==0x0 &&
1407                                 beacon->rsn_ie[11]==0xf &&
1408                                 beacon->rsn_ie[12]==0xac){
1409                                 if(beacon->rsn_ie[8]==1){//not mixed mode
1410                                         switch(beacon->rsn_ie[13]){
1411                                                 case 0x2:
1412                                                         ieee->pairwise_key_type = KEY_TYPE_TKIP;
1413                                                         break;
1414                                                 case 0x4:
1415                                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
1416                                                         break;
1417                                                 default:
1418                                                         printk("fault suite type in RSN pairwise key\n");
1419                                                         break;
1420                                         }
1421
1422                                 }
1423                                 else if(beacon->rsn_ie[8]==2){//mixed mode
1424                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
1425                                 }
1426                         }
1427
1428
1429                         beacon->rsn_ie[rsn_len - 2] = 0;
1430                         memcpy(tag,beacon->rsn_ie,rsn_len);
1431                 }
1432         }
1433 #else
1434         tag = skb_put(skb,ieee->wpa_ie_len);
1435         memcpy(tag,ieee->wpa_ie,ieee->wpa_ie_len);
1436 #endif
1437         tag = skb_put(skb,wmm_info_len);
1438         if(wmm_info_len) {
1439           ieee80211_WMM_Info(ieee, &tag);
1440         }
1441 #ifdef THOMAS_TURBO
1442         tag = skb_put(skb,turbo_info_len);
1443         if(turbo_info_len) {
1444                 ieee80211_TURBO_Info(ieee, &tag);
1445         }
1446 #endif
1447
1448         return skb;
1449 }
1450
1451 void ieee80211_associate_abort(struct ieee80211_device *ieee)
1452 {
1453
1454         unsigned long flags;
1455         spin_lock_irqsave(&ieee->lock, flags);
1456
1457         ieee->associate_seq++;
1458
1459         /* don't scan, and avoid to have the RX path possibily
1460          * try again to associate. Even do not react to AUTH or
1461          * ASSOC response. Just wait for the retry wq to be scheduled.
1462          * Here we will check if there are good nets to associate
1463          * with, so we retry or just get back to NO_LINK and scanning
1464          */
1465         if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING){
1466                 IEEE80211_DEBUG_MGMT("Authentication failed\n");
1467                 ieee->softmac_stats.no_auth_rs++;
1468         }else{
1469                 IEEE80211_DEBUG_MGMT("Association failed\n");
1470                 ieee->softmac_stats.no_ass_rs++;
1471         }
1472
1473         ieee->state = IEEE80211_ASSOCIATING_RETRY;
1474
1475         queue_delayed_work(ieee->wq, &ieee->associate_retry_wq,IEEE80211_SOFTMAC_ASSOC_RETRY_TIME);
1476
1477         spin_unlock_irqrestore(&ieee->lock, flags);
1478 }
1479
1480 void ieee80211_associate_abort_cb(unsigned long dev)
1481 {
1482         ieee80211_associate_abort((struct ieee80211_device *) dev);
1483 }
1484
1485
1486 void ieee80211_associate_step1(struct ieee80211_device *ieee)
1487 {
1488         struct ieee80211_network *beacon = &ieee->current_network;
1489         struct sk_buff *skb;
1490
1491         IEEE80211_DEBUG_MGMT("Stopping scan\n");
1492         ieee->softmac_stats.tx_auth_rq++;
1493         skb=ieee80211_authentication_req(beacon, ieee, 0);
1494         if (!skb){
1495
1496                 ieee80211_associate_abort(ieee);
1497         }
1498         else{
1499                 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATING ;
1500                 IEEE80211_DEBUG_MGMT("Sending authentication request\n");
1501                 //printk("---Sending authentication request\n");
1502                 softmac_mgmt_xmit(skb, ieee);
1503                 //BUGON when you try to add_timer twice, using mod_timer may be better, john0709
1504                 if(!timer_pending(&ieee->associate_timer)){
1505                         ieee->associate_timer.expires = jiffies + (HZ / 2);
1506                         add_timer(&ieee->associate_timer);
1507                 }
1508                 //If call dev_kfree_skb_any,a warning will ocur....
1509                 //KERNEL: assertion (!atomic_read(&skb->users)) failed at net/core/dev.c (1708)
1510                 //So ... 1204 by lawrence.
1511                 //printk("\nIn %s,line %d call kfree skb.",__func__,__LINE__);
1512                 //dev_kfree_skb_any(skb);//edit by thomas
1513         }
1514 }
1515
1516 void ieee80211_auth_challenge(struct ieee80211_device *ieee, u8 *challenge, int chlen)
1517 {
1518         u8 *c;
1519         struct sk_buff *skb;
1520         struct ieee80211_network *beacon = &ieee->current_network;
1521 //      int hlen = sizeof(struct ieee80211_authentication);
1522         del_timer_sync(&ieee->associate_timer);
1523         ieee->associate_seq++;
1524         ieee->softmac_stats.tx_auth_rq++;
1525
1526         skb = ieee80211_authentication_req(beacon, ieee, chlen+2);
1527         if (!skb)
1528                 ieee80211_associate_abort(ieee);
1529         else{
1530                 c = skb_put(skb, chlen+2);
1531                 *(c++) = MFIE_TYPE_CHALLENGE;
1532                 *(c++) = chlen;
1533                 memcpy(c, challenge, chlen);
1534
1535                 IEEE80211_DEBUG_MGMT("Sending authentication challenge response\n");
1536
1537                 ieee80211_encrypt_fragment(ieee, skb, sizeof(struct ieee80211_hdr_3addr  ));
1538
1539                 softmac_mgmt_xmit(skb, ieee);
1540                 if (!timer_pending(&ieee->associate_timer)){
1541                 //printk("=========>add timer again, to crash\n");
1542                 ieee->associate_timer.expires = jiffies + (HZ / 2);
1543                 add_timer(&ieee->associate_timer);
1544                 }
1545                 dev_kfree_skb_any(skb);//edit by thomas
1546         }
1547         kfree(challenge);
1548 }
1549
1550 void ieee80211_associate_step2(struct ieee80211_device *ieee)
1551 {
1552         struct sk_buff* skb;
1553         struct ieee80211_network *beacon = &ieee->current_network;
1554
1555         del_timer_sync(&ieee->associate_timer);
1556
1557         IEEE80211_DEBUG_MGMT("Sending association request\n");
1558         ieee->softmac_stats.tx_ass_rq++;
1559         skb=ieee80211_association_req(beacon, ieee);
1560         if (!skb)
1561                 ieee80211_associate_abort(ieee);
1562         else{
1563                 softmac_mgmt_xmit(skb, ieee);
1564                 if (!timer_pending(&ieee->associate_timer)){
1565                 ieee->associate_timer.expires = jiffies + (HZ / 2);
1566                 add_timer(&ieee->associate_timer);
1567                 }
1568                 //dev_kfree_skb_any(skb);//edit by thomas
1569         }
1570 }
1571
1572 void ieee80211_associate_complete_wq(struct work_struct *work)
1573 {
1574         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_complete_wq);
1575
1576         printk(KERN_INFO "Associated successfully\n");
1577         if(ieee80211_is_54g(ieee->current_network) &&
1578                 (ieee->modulation & IEEE80211_OFDM_MODULATION)){
1579
1580                 ieee->rate = 540;
1581                 printk(KERN_INFO"Using G rates\n");
1582         }else{
1583                 ieee->rate = 110;
1584                 printk(KERN_INFO"Using B rates\n");
1585         }
1586         ieee->link_change(ieee->dev);
1587         notify_wx_assoc_event(ieee);
1588         if (ieee->data_hard_resume)
1589                 ieee->data_hard_resume(ieee->dev);
1590         netif_carrier_on(ieee->dev);
1591 }
1592
1593 void ieee80211_associate_complete(struct ieee80211_device *ieee)
1594 {
1595         int i;
1596         del_timer_sync(&ieee->associate_timer);
1597
1598         for(i = 0; i < 6; i++) {
1599           //ieee->seq_ctrl[i] = 0;
1600         }
1601         ieee->state = IEEE80211_LINKED;
1602         IEEE80211_DEBUG_MGMT("Successfully associated\n");
1603
1604         queue_work(ieee->wq, &ieee->associate_complete_wq);
1605 }
1606
1607 void ieee80211_associate_procedure_wq(struct work_struct *work)
1608 {
1609         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_procedure_wq);
1610
1611         ieee->sync_scan_hurryup = 1;
1612         down(&ieee->wx_sem);
1613
1614         if (ieee->data_hard_stop)
1615                 ieee->data_hard_stop(ieee->dev);
1616
1617         ieee80211_stop_scan(ieee);
1618         ieee->set_chan(ieee->dev, ieee->current_network.channel);
1619
1620         ieee->associate_seq = 1;
1621         ieee80211_associate_step1(ieee);
1622
1623         up(&ieee->wx_sem);
1624 }
1625
1626 inline void ieee80211_softmac_new_net(struct ieee80211_device *ieee, struct ieee80211_network *net)
1627 {
1628         u8 tmp_ssid[IW_ESSID_MAX_SIZE+1];
1629         int tmp_ssid_len = 0;
1630
1631         short apset,ssidset,ssidbroad,apmatch,ssidmatch;
1632
1633         /* we are interested in new new only if we are not associated
1634          * and we are not associating / authenticating
1635          */
1636         if (ieee->state != IEEE80211_NOLINK)
1637                 return;
1638
1639         if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability & WLAN_CAPABILITY_BSS))
1640                 return;
1641
1642         if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability & WLAN_CAPABILITY_IBSS))
1643                 return;
1644
1645
1646         if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC){
1647                 /* if the user specified the AP MAC, we need also the essid
1648                  * This could be obtained by beacons or, if the network does not
1649                  * broadcast it, it can be put manually.
1650                  */
1651                 apset = ieee->wap_set;//(memcmp(ieee->current_network.bssid, zero,ETH_ALEN)!=0 );
1652                 ssidset = ieee->ssid_set;//ieee->current_network.ssid[0] != '\0';
1653                 ssidbroad =  !(net->ssid_len == 0 || net->ssid[0]== '\0');
1654                 apmatch = (memcmp(ieee->current_network.bssid, net->bssid, ETH_ALEN)==0);
1655
1656                 if(ieee->current_network.ssid_len != net->ssid_len)
1657                         ssidmatch = 0;
1658                 else
1659                         ssidmatch = (0==strncmp(ieee->current_network.ssid, net->ssid, net->ssid_len));
1660
1661                 //printk("cur: %s, %d, net:%s, %d\n", ieee->current_network.ssid, ieee->current_network.ssid_len, net->ssid, net->ssid_len);
1662                 //printk("apset=%d apmatch=%d ssidset=%d ssidbroad=%d ssidmatch=%d\n",apset,apmatch,ssidset,ssidbroad,ssidmatch);
1663
1664                 if (    /* if the user set the AP check if match.
1665                          * if the network does not broadcast essid we check the user supplyed ANY essid
1666                          * if the network does broadcast and the user does not set essid it is OK
1667                          * if the network does broadcast and the user did set essid chech if essid match
1668                          */
1669                                 ( apset && apmatch &&
1670                                   ((ssidset && ssidbroad && ssidmatch) || (ssidbroad && !ssidset) || (!ssidbroad && ssidset)) ) ||
1671                                 /* if the ap is not set, check that the user set the bssid
1672                                  * and the network does bradcast and that those two bssid matches
1673                                  */
1674                                 (!apset && ssidset && ssidbroad && ssidmatch)
1675                    ){
1676
1677
1678                         /* if the essid is hidden replace it with the
1679                          * essid provided by the user.
1680                          */
1681                         if (!ssidbroad){
1682                                 strncpy(tmp_ssid, ieee->current_network.ssid, IW_ESSID_MAX_SIZE);
1683                                 tmp_ssid_len = ieee->current_network.ssid_len;
1684                         }
1685                         memcpy(&ieee->current_network, net, sizeof(struct ieee80211_network));
1686
1687                         if (!ssidbroad){
1688                                 strncpy(ieee->current_network.ssid, tmp_ssid, IW_ESSID_MAX_SIZE);
1689                                 ieee->current_network.ssid_len = tmp_ssid_len;
1690                         }
1691                         printk(KERN_INFO"Linking with %s: channel is %d\n",ieee->current_network.ssid,ieee->current_network.channel);
1692
1693                         if (ieee->iw_mode == IW_MODE_INFRA){
1694                                 ieee->state = IEEE80211_ASSOCIATING;
1695                                 ieee->beinretry = false;
1696                                 queue_work(ieee->wq, &ieee->associate_procedure_wq);
1697                         }else{
1698                                 if(ieee80211_is_54g(ieee->current_network) &&
1699                                                 (ieee->modulation & IEEE80211_OFDM_MODULATION)){
1700                                         ieee->rate = 540;
1701                                         printk(KERN_INFO"Using G rates\n");
1702                                 }else{
1703                                         ieee->rate = 110;
1704                                         printk(KERN_INFO"Using B rates\n");
1705                                 }
1706                                 ieee->state = IEEE80211_LINKED;
1707                                 ieee->beinretry = false;
1708                         }
1709
1710                 }
1711         }
1712
1713 }
1714
1715 void ieee80211_softmac_check_all_nets(struct ieee80211_device *ieee)
1716 {
1717         unsigned long flags;
1718         struct ieee80211_network *target;
1719
1720         spin_lock_irqsave(&ieee->lock, flags);
1721         list_for_each_entry(target, &ieee->network_list, list) {
1722
1723                 /* if the state become different that NOLINK means
1724                  * we had found what we are searching for
1725                  */
1726
1727                 if (ieee->state != IEEE80211_NOLINK)
1728                         break;
1729
1730                 if (ieee->scan_age == 0 || time_after(target->last_scanned + ieee->scan_age, jiffies))
1731                         ieee80211_softmac_new_net(ieee, target);
1732         }
1733
1734         spin_unlock_irqrestore(&ieee->lock, flags);
1735
1736 }
1737
1738
1739 static inline u16 auth_parse(struct sk_buff *skb, u8** challenge, int *chlen)
1740 {
1741         struct ieee80211_authentication *a;
1742         u8 *t;
1743         if (skb->len <  (sizeof(struct ieee80211_authentication)-sizeof(struct ieee80211_info_element))){
1744                 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n",skb->len);
1745                 return 0xcafe;
1746         }
1747         *challenge = NULL;
1748         a = (struct ieee80211_authentication*) skb->data;
1749         if(skb->len > (sizeof(struct ieee80211_authentication) +3)){
1750                 t = skb->data + sizeof(struct ieee80211_authentication);
1751
1752                 if(*(t++) == MFIE_TYPE_CHALLENGE){
1753                         *chlen = *(t++);
1754                         *challenge = (u8*)kmalloc(*chlen, GFP_ATOMIC);
1755                         memcpy(*challenge, t, *chlen);
1756                 }
1757         }
1758
1759         return cpu_to_le16(a->status);
1760
1761 }
1762
1763
1764 int auth_rq_parse(struct sk_buff *skb,u8* dest)
1765 {
1766         struct ieee80211_authentication *a;
1767
1768         if (skb->len <  (sizeof(struct ieee80211_authentication)-sizeof(struct ieee80211_info_element))){
1769                 IEEE80211_DEBUG_MGMT("invalid len in auth request: %d\n",skb->len);
1770                 return -1;
1771         }
1772         a = (struct ieee80211_authentication*) skb->data;
1773
1774         memcpy(dest,a->header.addr2, ETH_ALEN);
1775
1776         if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1777                 return  WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1778
1779         return WLAN_STATUS_SUCCESS;
1780 }
1781
1782 static short probe_rq_parse(struct ieee80211_device *ieee, struct sk_buff *skb, u8 *src)
1783 {
1784         u8 *tag;
1785         u8 *skbend;
1786         u8 *ssid=NULL;
1787         u8 ssidlen = 0;
1788
1789         struct ieee80211_hdr_3addr   *header =
1790                 (struct ieee80211_hdr_3addr   *) skb->data;
1791
1792         if (skb->len < sizeof (struct ieee80211_hdr_3addr  ))
1793                 return -1; /* corrupted */
1794
1795         memcpy(src,header->addr2, ETH_ALEN);
1796
1797         skbend = (u8*)skb->data + skb->len;
1798
1799         tag = skb->data + sizeof (struct ieee80211_hdr_3addr  );
1800
1801         while (tag+1 < skbend){
1802                 if (*tag == 0){
1803                         ssid = tag+2;
1804                         ssidlen = *(tag+1);
1805                         break;
1806                 }
1807                 tag++; /* point to the len field */
1808                 tag = tag + *(tag); /* point to the last data byte of the tag */
1809                 tag++; /* point to the next tag */
1810         }
1811
1812         //IEEE80211DMESG("Card MAC address is "MACSTR, MAC2STR(src));
1813         if (ssidlen == 0) return 1;
1814
1815         if (!ssid) return 1; /* ssid not found in tagged param */
1816         return (!strncmp(ssid, ieee->current_network.ssid, ssidlen));
1817
1818 }
1819
1820 int assoc_rq_parse(struct sk_buff *skb,u8* dest)
1821 {
1822         struct ieee80211_assoc_request_frame *a;
1823
1824         if (skb->len < (sizeof(struct ieee80211_assoc_request_frame) -
1825                 sizeof(struct ieee80211_info_element))) {
1826
1827                 IEEE80211_DEBUG_MGMT("invalid len in auth request:%d \n", skb->len);
1828                 return -1;
1829         }
1830
1831         a = (struct ieee80211_assoc_request_frame*) skb->data;
1832
1833         memcpy(dest,a->header.addr2,ETH_ALEN);
1834
1835         return 0;
1836 }
1837
1838 static inline u16 assoc_parse(struct sk_buff *skb, int *aid)
1839 {
1840         struct ieee80211_assoc_response_frame *a;
1841         if (skb->len <  sizeof(struct ieee80211_assoc_response_frame)){
1842                 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
1843                 return 0xcafe;
1844         }
1845
1846         a = (struct ieee80211_assoc_response_frame*) skb->data;
1847         *aid = le16_to_cpu(a->aid) & 0x3fff;
1848         return le16_to_cpu(a->status);
1849 }
1850
1851 static inline void
1852 ieee80211_rx_probe_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1853 {
1854         u8 dest[ETH_ALEN];
1855
1856         //IEEE80211DMESG("Rx probe");
1857         ieee->softmac_stats.rx_probe_rq++;
1858         //DMESG("Dest is "MACSTR, MAC2STR(dest));
1859         if (probe_rq_parse(ieee, skb, dest)){
1860                 //IEEE80211DMESG("Was for me!");
1861                 ieee->softmac_stats.tx_probe_rs++;
1862                 ieee80211_resp_to_probe(ieee, dest);
1863         }
1864 }
1865
1866 inline void
1867 ieee80211_rx_auth_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1868 {
1869         u8 dest[ETH_ALEN];
1870         int status;
1871         //IEEE80211DMESG("Rx probe");
1872         ieee->softmac_stats.rx_auth_rq++;
1873
1874         if ((status = auth_rq_parse(skb, dest))!= -1){
1875                 ieee80211_resp_to_auth(ieee, status, dest);
1876         }
1877         //DMESG("Dest is "MACSTR, MAC2STR(dest));
1878
1879 }
1880
1881  inline void
1882 ieee80211_rx_assoc_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1883 {
1884
1885         u8 dest[ETH_ALEN];
1886         //unsigned long flags;
1887
1888         ieee->softmac_stats.rx_ass_rq++;
1889         if (assoc_rq_parse(skb,dest) != -1){
1890                 ieee80211_resp_to_assoc_rq(ieee, dest);
1891         }
1892
1893         printk(KERN_INFO"New client associated: "MAC_FMT"\n", MAC_ARG(dest));
1894         //FIXME
1895         #if 0
1896         spin_lock_irqsave(&ieee->lock,flags);
1897         add_associate(ieee,dest);
1898         spin_unlock_irqrestore(&ieee->lock,flags);
1899         #endif
1900 }
1901
1902
1903
1904 void ieee80211_sta_ps_send_null_frame(struct ieee80211_device *ieee, short pwr)
1905 {
1906
1907         struct sk_buff *buf = ieee80211_null_func(ieee, pwr);
1908
1909         if (buf)
1910                 softmac_ps_mgmt_xmit(buf, ieee);
1911
1912 }
1913
1914
1915 short ieee80211_sta_ps_sleep(struct ieee80211_device *ieee, u32 *time_h, u32 *time_l)
1916 {
1917 #if 0
1918         int timeout = ieee->ps_timeout;
1919 #else
1920         int timeout = 0;
1921 #endif
1922         u8 dtim;
1923         /*if(ieee->ps == IEEE80211_PS_DISABLED ||
1924                 ieee->iw_mode != IW_MODE_INFRA ||
1925                 ieee->state != IEEE80211_LINKED)
1926
1927                 return 0;
1928         */
1929         dtim = ieee->current_network.dtim_data;
1930         //printk("DTIM\n");
1931
1932         if(!(dtim & IEEE80211_DTIM_VALID))
1933                 return 0;
1934         else
1935                 timeout = ieee->current_network.beacon_interval;
1936
1937         //printk("VALID\n");
1938         ieee->current_network.dtim_data = IEEE80211_DTIM_INVALID;
1939
1940         if(dtim & ((IEEE80211_DTIM_UCAST | IEEE80211_DTIM_MBCAST)& ieee->ps))
1941                 return 2;
1942
1943         if(!time_after(jiffies, ieee->dev->trans_start + MSECS(timeout)))
1944                 return 0;
1945
1946         if(!time_after(jiffies, ieee->last_rx_ps_time + MSECS(timeout)))
1947                 return 0;
1948
1949         if((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE ) &&
1950                 (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1951                 return 0;
1952 #if 0
1953         if(time_l){
1954                 *time_l = ieee->current_network.last_dtim_sta_time[0]
1955                         + (ieee->current_network.beacon_interval
1956                         * ieee->current_network.dtim_period) * 1000;
1957         }
1958 #else
1959         if(time_l){
1960                 *time_l = ieee->current_network.last_dtim_sta_time[0]
1961                         + MSECS((ieee->current_network.beacon_interval));
1962                         //* ieee->current_network.dtim_period));
1963                         //printk("beacon_interval:%x, dtim_period:%x, totol to Msecs:%x, HZ:%x\n", ieee->current_network.beacon_interval, ieee->current_network.dtim_period, MSECS(((ieee->current_network.beacon_interval * ieee->current_network.dtim_period))), HZ);
1964         }
1965
1966 #endif
1967         if(time_h){
1968                 *time_h = ieee->current_network.last_dtim_sta_time[1];
1969                 if(time_l && *time_l < ieee->current_network.last_dtim_sta_time[0])
1970                         *time_h += 1;
1971         }
1972
1973         return 1;
1974
1975
1976 }
1977
1978 inline void ieee80211_sta_ps(struct ieee80211_device *ieee)
1979 {
1980
1981         u32 th,tl;
1982         short sleep;
1983
1984         unsigned long flags,flags2;
1985
1986         spin_lock_irqsave(&ieee->lock, flags);
1987
1988         if((ieee->ps == IEEE80211_PS_DISABLED ||
1989
1990                 ieee->iw_mode != IW_MODE_INFRA ||
1991                 ieee->state != IEEE80211_LINKED)){
1992
1993                 //#warning CHECK_LOCK_HERE
1994                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1995
1996                 ieee80211_sta_wakeup(ieee, 1);
1997
1998                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1999         }
2000
2001         sleep = ieee80211_sta_ps_sleep(ieee,&th, &tl);
2002 //      printk("===>%s,%d[2 wake, 1 sleep, 0 do nothing], ieee->sta_sleep = %d\n",__func__, sleep,ieee->sta_sleep);
2003         /* 2 wake, 1 sleep, 0 do nothing */
2004         if(sleep == 0)
2005                 goto out;
2006
2007         if(sleep == 1){
2008
2009                 if(ieee->sta_sleep == 1)
2010                         ieee->enter_sleep_state(ieee->dev,th,tl);
2011
2012                 else if(ieee->sta_sleep == 0){
2013         //              printk("send null 1\n");
2014                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2015
2016                         if(ieee->ps_is_queue_empty(ieee->dev)){
2017
2018
2019                                 ieee->sta_sleep = 2;
2020
2021                                 ieee->ps_request_tx_ack(ieee->dev);
2022
2023                                 ieee80211_sta_ps_send_null_frame(ieee,1);
2024
2025                                 ieee->ps_th = th;
2026                                 ieee->ps_tl = tl;
2027                         }
2028                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2029
2030                 }
2031
2032
2033         }else if(sleep == 2){
2034 //#warning CHECK_LOCK_HERE
2035                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2036
2037         //      printk("send wakeup packet\n");
2038                 ieee80211_sta_wakeup(ieee,1);
2039
2040                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2041         }
2042
2043 out:
2044         spin_unlock_irqrestore(&ieee->lock, flags);
2045
2046 }
2047
2048 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl)
2049 {
2050         if(ieee->sta_sleep == 0){
2051                 if(nl){
2052                 //      printk("Warning: driver is probably failing to report TX ps error\n");
2053                         ieee->ps_request_tx_ack(ieee->dev);
2054                         ieee80211_sta_ps_send_null_frame(ieee, 0);
2055                 }
2056                 return;
2057
2058         }
2059
2060         if(ieee->sta_sleep == 1)
2061                 ieee->sta_wake_up(ieee->dev);
2062
2063         ieee->sta_sleep = 0;
2064
2065         if(nl){
2066                 ieee->ps_request_tx_ack(ieee->dev);
2067                 ieee80211_sta_ps_send_null_frame(ieee, 0);
2068         }
2069 }
2070
2071 void ieee80211_ps_tx_ack(struct ieee80211_device *ieee, short success)
2072 {
2073         unsigned long flags,flags2;
2074
2075         spin_lock_irqsave(&ieee->lock, flags);
2076         if(ieee->sta_sleep == 2){
2077                 /* Null frame with PS bit set */
2078                 if(success){
2079
2080                 //      printk("==================> %s::enter sleep state\n",__func__);
2081                         ieee->sta_sleep = 1;
2082                         ieee->enter_sleep_state(ieee->dev,ieee->ps_th,ieee->ps_tl);
2083                 }
2084                 /* if the card report not success we can't be sure the AP
2085                  * has not RXed so we can't assume the AP believe us awake
2086                  */
2087         }
2088         /* 21112005 - tx again null without PS bit if lost */
2089         else {
2090
2091                 if((ieee->sta_sleep == 0) && !success){
2092                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2093                         ieee80211_sta_ps_send_null_frame(ieee, 0);
2094                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2095                 }
2096         }
2097         spin_unlock_irqrestore(&ieee->lock, flags);
2098 }
2099
2100 inline int
2101 ieee80211_rx_frame_softmac(struct ieee80211_device *ieee, struct sk_buff *skb,
2102                         struct ieee80211_rx_stats *rx_stats, u16 type,
2103                         u16 stype)
2104 {
2105         struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *) skb->data;
2106         u16 errcode;
2107         u8* challenge=NULL;
2108         int chlen=0;
2109         int aid=0;
2110         struct ieee80211_assoc_response_frame *assoc_resp;
2111         struct ieee80211_info_element *info_element;
2112
2113         if(!ieee->proto_started)
2114                 return 0;
2115
2116         if(ieee->sta_sleep || (ieee->ps != IEEE80211_PS_DISABLED &&
2117                 ieee->iw_mode == IW_MODE_INFRA &&
2118                 ieee->state == IEEE80211_LINKED))
2119
2120                 tasklet_schedule(&ieee->ps_task);
2121
2122         if(WLAN_FC_GET_STYPE(header->frame_ctl) != IEEE80211_STYPE_PROBE_RESP &&
2123                 WLAN_FC_GET_STYPE(header->frame_ctl) != IEEE80211_STYPE_BEACON)
2124                 ieee->last_rx_ps_time = jiffies;
2125
2126         switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
2127
2128                 case IEEE80211_STYPE_ASSOC_RESP:
2129                 case IEEE80211_STYPE_REASSOC_RESP:
2130
2131                         IEEE80211_DEBUG_MGMT("received [RE]ASSOCIATION RESPONSE (%d)\n",
2132                                         WLAN_FC_GET_STYPE(header->frame_ctl));
2133                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2134                                 ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATED &&
2135                                 ieee->iw_mode == IW_MODE_INFRA){
2136                                 if (0 == (errcode=assoc_parse(skb, &aid))){
2137                                         u16 left;
2138
2139                                         ieee->state=IEEE80211_LINKED;
2140                                         ieee->assoc_id = aid;
2141                                         ieee->softmac_stats.rx_ass_ok++;
2142
2143                                         //printk(KERN_WARNING "nic_type = %s", (rx_stats->nic_type == 1)?"rtl8187":"rtl8187B");
2144                                         if(1 == rx_stats->nic_type) //card type is 8187
2145                                         {
2146                                                 goto associate_complete;
2147                                         }
2148                                         assoc_resp = (struct ieee80211_assoc_response_frame*)skb->data;
2149                                         info_element =  &assoc_resp->info_element;
2150                                         left = skb->len - ((void*)info_element - (void*)assoc_resp);
2151
2152                                         while (left >= sizeof(struct ieee80211_info_element_hdr)) {
2153                                                 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
2154                                                         printk(KERN_WARNING "[re]associate reeponse error!");
2155                                                         return 1;
2156                                                 }
2157                                                 switch (info_element->id) {
2158                                                   case MFIE_TYPE_GENERIC:
2159                                                          IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n", info_element->len);
2160                                                         if (info_element->len >= 8  &&
2161                                                             info_element->data[0] == 0x00 &&
2162                                                             info_element->data[1] == 0x50 &&
2163                                                             info_element->data[2] == 0xf2 &&
2164                                                             info_element->data[3] == 0x02 &&
2165                                                             info_element->data[4] == 0x01) {
2166                                                             // Not care about version at present.
2167                                                             //WMM Parameter Element
2168                                                             memcpy(ieee->current_network.wmm_param,(u8*)(info_element->data\
2169                                                                                     + 8),(info_element->len - 8));
2170
2171                                                             if (((ieee->current_network.wmm_info^info_element->data[6])& \
2172                                                                                     0x0f)||(!ieee->init_wmmparam_flag)) {
2173                                                               //refresh paramete element for current network
2174                                                               // update the register parameter for hardware
2175                                                               ieee->init_wmmparam_flag = 1;
2176                                                               queue_work(ieee->wq, &ieee->wmm_param_update_wq);
2177
2178                                                             }
2179                                                             //update info_element for current network
2180                                                             ieee->current_network.wmm_info  = info_element->data[6];
2181                                                         }
2182                                                         break;
2183                                                   default:
2184                                                         //nothing to do at present!!!
2185                                                         break;
2186                                                 }
2187
2188                                                 left -= sizeof(struct ieee80211_info_element_hdr) +
2189                                                         info_element->len;
2190                                                 info_element = (struct ieee80211_info_element *)
2191                                                         &info_element->data[info_element->len];
2192                                         }
2193                                         if(!ieee->init_wmmparam_flag) //legacy AP, reset the AC_xx_param register
2194                                         {
2195                                                 queue_work(ieee->wq,&ieee->wmm_param_update_wq);
2196                                                 ieee->init_wmmparam_flag = 1;//indicate AC_xx_param upated since last associate
2197                                         }
2198 associate_complete:
2199                                         ieee80211_associate_complete(ieee);
2200                                 }else{
2201                                         ieee->softmac_stats.rx_ass_err++;
2202                                         IEEE80211_DEBUG_MGMT(
2203                                                 "Association response status code 0x%x\n",
2204                                                 errcode);
2205                                         ieee80211_associate_abort(ieee);
2206                                 }
2207                         }
2208                         break;
2209
2210                 case IEEE80211_STYPE_ASSOC_REQ:
2211                 case IEEE80211_STYPE_REASSOC_REQ:
2212
2213                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2214                                 ieee->iw_mode == IW_MODE_MASTER)
2215
2216                                 ieee80211_rx_assoc_rq(ieee, skb);
2217                         break;
2218
2219                 case IEEE80211_STYPE_AUTH:
2220
2221                         if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE){
2222                                 if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING &&
2223                                 ieee->iw_mode == IW_MODE_INFRA){
2224
2225                                                 IEEE80211_DEBUG_MGMT("Received authentication response");
2226
2227                                                 if (0 == (errcode=auth_parse(skb, &challenge, &chlen))){
2228                                                         if(ieee->open_wep || !challenge){
2229                                                                 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATED;
2230                                                                 ieee->softmac_stats.rx_auth_rs_ok++;
2231
2232                                                                 ieee80211_associate_step2(ieee);
2233                                                         }else{
2234                                                                 ieee80211_auth_challenge(ieee, challenge, chlen);
2235                                                         }
2236                                                 }else{
2237                                                         ieee->softmac_stats.rx_auth_rs_err++;
2238                                                         IEEE80211_DEBUG_MGMT("Authentication respose status code 0x%x",errcode);
2239                                                         ieee80211_associate_abort(ieee);
2240                                                 }
2241
2242                                         }else if (ieee->iw_mode == IW_MODE_MASTER){
2243                                                 ieee80211_rx_auth_rq(ieee, skb);
2244                                         }
2245                                 }
2246                         break;
2247
2248                 case IEEE80211_STYPE_PROBE_REQ:
2249
2250                         if ((ieee->softmac_features & IEEE_SOFTMAC_PROBERS) &&
2251                                 ((ieee->iw_mode == IW_MODE_ADHOC ||
2252                                 ieee->iw_mode == IW_MODE_MASTER) &&
2253                                 ieee->state == IEEE80211_LINKED))
2254
2255                                 ieee80211_rx_probe_rq(ieee, skb);
2256                         break;
2257
2258                 case IEEE80211_STYPE_DISASSOC:
2259                 case IEEE80211_STYPE_DEAUTH:
2260                         /* FIXME for now repeat all the association procedure
2261                         * both for disassociation and deauthentication
2262                         */
2263                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2264                                 (ieee->state == IEEE80211_LINKED) &&
2265                                 (ieee->iw_mode == IW_MODE_INFRA) &&
2266                                 (!memcmp(header->addr2,ieee->current_network.bssid,ETH_ALEN))){
2267                                 ieee->state = IEEE80211_ASSOCIATING;
2268                                 ieee->softmac_stats.reassoc++;
2269
2270                                 //notify_wx_assoc_event(ieee);  //YJ,del,080828, do not notify os here
2271                                 queue_work(ieee->wq, &ieee->associate_procedure_wq);
2272                         }
2273
2274                         break;
2275
2276                 default:
2277                         return -1;
2278                         break;
2279         }
2280
2281         //dev_kfree_skb_any(skb);
2282         return 0;
2283 }
2284
2285
2286
2287 /* following are for a simplier TX queue management.
2288  * Instead of using netif_[stop/wake]_queue the driver
2289  * will uses these two function (plus a reset one), that
2290  * will internally uses the kernel netif_* and takes
2291  * care of the ieee802.11 fragmentation.
2292  * So the driver receives a fragment per time and might
2293  * call the stop function when it want without take care
2294  * to have enought room to TX an entire packet.
2295  * This might be useful if each fragment need it's own
2296  * descriptor, thus just keep a total free memory > than
2297  * the max fragmentation treshold is not enought.. If the
2298  * ieee802.11 stack passed a TXB struct then you needed
2299  * to keep N free descriptors where
2300  * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
2301  * In this way you need just one and the 802.11 stack
2302  * will take care of buffering fragments and pass them to
2303  * to the driver later, when it wakes the queue.
2304  */
2305
2306 void ieee80211_softmac_xmit(struct ieee80211_txb *txb, struct ieee80211_device *ieee)
2307 {
2308
2309
2310         unsigned long flags;
2311         int  i;
2312
2313         spin_lock_irqsave(&ieee->lock,flags);
2314         #if 0
2315         if(ieee->queue_stop){
2316                 IEEE80211DMESG("EE: IEEE hard_start_xmit invoked when kernel queue should be stopped");
2317                 netif_stop_queue(ieee->dev);
2318                 ieee->ieee_stats.swtxstop++;
2319                 //dev_kfree_skb_any(skb);
2320                 err = 1;
2321                 goto exit;
2322         }
2323
2324         ieee->stats.tx_bytes+=skb->len;
2325
2326
2327         txb=ieee80211_skb_to_txb(ieee,skb);
2328
2329
2330         if(txb==NULL){
2331                 IEEE80211DMESG("WW: IEEE stack failed to provide txb");
2332                 //dev_kfree_skb_any(skb);
2333                 err = 1;
2334                 goto exit;
2335         }
2336         #endif
2337
2338         /* called with 2nd parm 0, no tx mgmt lock required */
2339         ieee80211_sta_wakeup(ieee,0);
2340
2341         for(i = 0; i < txb->nr_frags; i++) {
2342
2343                 if (ieee->queue_stop){
2344                         ieee->tx_pending.txb = txb;
2345                         ieee->tx_pending.frag = i;
2346                         goto exit;
2347                 }else{
2348                         ieee->softmac_data_hard_start_xmit(
2349                                 txb->fragments[i],
2350                                 ieee->dev,ieee->rate);
2351                                 //(i+1)<txb->nr_frags);
2352                         ieee->stats.tx_packets++;
2353                         ieee->stats.tx_bytes += txb->fragments[i]->len;
2354                         ieee->dev->trans_start = jiffies;
2355                 }
2356         }
2357
2358         ieee80211_txb_free(txb);
2359
2360         exit:
2361         spin_unlock_irqrestore(&ieee->lock,flags);
2362
2363 }
2364
2365 /* called with ieee->lock acquired */
2366 void ieee80211_resume_tx(struct ieee80211_device *ieee)
2367 {
2368         int i;
2369         for(i = ieee->tx_pending.frag; i < ieee->tx_pending.txb->nr_frags; i++) {
2370
2371                 if (ieee->queue_stop){
2372                         ieee->tx_pending.frag = i;
2373                         return;
2374                 }else{
2375
2376                         ieee->softmac_data_hard_start_xmit(
2377                                 ieee->tx_pending.txb->fragments[i],
2378                                 ieee->dev,ieee->rate);
2379                                 //(i+1)<ieee->tx_pending.txb->nr_frags);
2380                         ieee->stats.tx_packets++;
2381                         ieee->dev->trans_start = jiffies;
2382                 }
2383         }
2384
2385
2386         ieee80211_txb_free(ieee->tx_pending.txb);
2387         ieee->tx_pending.txb = NULL;
2388 }
2389
2390
2391 void ieee80211_reset_queue(struct ieee80211_device *ieee)
2392 {
2393         unsigned long flags;
2394
2395         spin_lock_irqsave(&ieee->lock,flags);
2396         init_mgmt_queue(ieee);
2397         if (ieee->tx_pending.txb){
2398                 ieee80211_txb_free(ieee->tx_pending.txb);
2399                 ieee->tx_pending.txb = NULL;
2400         }
2401         ieee->queue_stop = 0;
2402         spin_unlock_irqrestore(&ieee->lock,flags);
2403
2404 }
2405
2406 void ieee80211_wake_queue(struct ieee80211_device *ieee)
2407 {
2408
2409         unsigned long flags;
2410         struct sk_buff *skb;
2411         struct ieee80211_hdr_3addr  *header;
2412
2413         spin_lock_irqsave(&ieee->lock,flags);
2414         if (! ieee->queue_stop) goto exit;
2415
2416         ieee->queue_stop = 0;
2417
2418         if(ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE){
2419                 while (!ieee->queue_stop && (skb = dequeue_mgmt(ieee))){
2420
2421                         header = (struct ieee80211_hdr_3addr  *) skb->data;
2422
2423                         header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2424
2425                         if (ieee->seq_ctrl[0] == 0xFFF)
2426                                 ieee->seq_ctrl[0] = 0;
2427                         else
2428                                 ieee->seq_ctrl[0]++;
2429
2430                         //printk(KERN_ALERT "ieee80211_wake_queue \n");
2431                         ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
2432                         dev_kfree_skb_any(skb);//edit by thomas
2433                 }
2434         }
2435         if (!ieee->queue_stop && ieee->tx_pending.txb)
2436                 ieee80211_resume_tx(ieee);
2437
2438         if (!ieee->queue_stop && netif_queue_stopped(ieee->dev)){
2439                 ieee->softmac_stats.swtxawake++;
2440                 netif_wake_queue(ieee->dev);
2441         }
2442
2443 exit :
2444         spin_unlock_irqrestore(&ieee->lock,flags);
2445 }
2446
2447
2448 void ieee80211_stop_queue(struct ieee80211_device *ieee)
2449 {
2450         //unsigned long flags;
2451         //spin_lock_irqsave(&ieee->lock,flags);
2452
2453         if (! netif_queue_stopped(ieee->dev)){
2454                 netif_stop_queue(ieee->dev);
2455                 ieee->softmac_stats.swtxstop++;
2456         }
2457         ieee->queue_stop = 1;
2458         //spin_unlock_irqrestore(&ieee->lock,flags);
2459
2460 }
2461
2462
2463 inline void ieee80211_randomize_cell(struct ieee80211_device *ieee)
2464 {
2465
2466         get_random_bytes(ieee->current_network.bssid, ETH_ALEN);
2467
2468         /* an IBSS cell address must have the two less significant
2469          * bits of the first byte = 2
2470          */
2471         ieee->current_network.bssid[0] &= ~0x01;
2472         ieee->current_network.bssid[0] |= 0x02;
2473 }
2474
2475 /* called in user context only */
2476 void ieee80211_start_master_bss(struct ieee80211_device *ieee)
2477 {
2478         ieee->assoc_id = 1;
2479
2480         if (ieee->current_network.ssid_len == 0){
2481                 strncpy(ieee->current_network.ssid,
2482                         IEEE80211_DEFAULT_TX_ESSID,
2483                         IW_ESSID_MAX_SIZE);
2484
2485                 ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
2486                 ieee->ssid_set = 1;
2487         }
2488
2489         memcpy(ieee->current_network.bssid, ieee->dev->dev_addr, ETH_ALEN);
2490
2491         ieee->set_chan(ieee->dev, ieee->current_network.channel);
2492         ieee->state = IEEE80211_LINKED;
2493         ieee->link_change(ieee->dev);
2494         notify_wx_assoc_event(ieee);
2495
2496         if (ieee->data_hard_resume)
2497                 ieee->data_hard_resume(ieee->dev);
2498
2499         netif_carrier_on(ieee->dev);
2500 }
2501
2502 void ieee80211_start_monitor_mode(struct ieee80211_device *ieee)
2503 {
2504         if(ieee->raw_tx){
2505
2506                 if (ieee->data_hard_resume)
2507                         ieee->data_hard_resume(ieee->dev);
2508
2509                 netif_carrier_on(ieee->dev);
2510         }
2511 }
2512
2513 void ieee80211_start_ibss_wq(struct work_struct *work)
2514 {
2515         struct delayed_work *dwork = to_delayed_work(work);
2516         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, start_ibss_wq);
2517
2518         /* iwconfig mode ad-hoc will schedule this and return
2519          * on the other hand this will block further iwconfig SET
2520          * operations because of the wx_sem hold.
2521          * Anyway some most set operations set a flag to speed-up
2522          * (abort) this wq (when syncro scanning) before sleeping
2523          * on the semaphore
2524          */
2525
2526         down(&ieee->wx_sem);
2527
2528
2529         if (ieee->current_network.ssid_len == 0){
2530                 strcpy(ieee->current_network.ssid,IEEE80211_DEFAULT_TX_ESSID);
2531                 ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
2532                 ieee->ssid_set = 1;
2533         }
2534
2535         /* check if we have this cell in our network list */
2536         ieee80211_softmac_check_all_nets(ieee);
2537
2538 #ifdef ENABLE_DOT11D
2539         if(ieee->state == IEEE80211_NOLINK)
2540                 ieee->current_network.channel = 10;
2541 #endif
2542         /* if not then the state is not linked. Maybe the user swithced to
2543          * ad-hoc mode just after being in monitor mode, or just after
2544          * being very few time in managed mode (so the card have had no
2545          * time to scan all the chans..) or we have just run up the iface
2546          * after setting ad-hoc mode. So we have to give another try..
2547          * Here, in ibss mode, should be safe to do this without extra care
2548          * (in bss mode we had to make sure no-one tryed to associate when
2549          * we had just checked the ieee->state and we was going to start the
2550          * scan) beacause in ibss mode the ieee80211_new_net function, when
2551          * finds a good net, just set the ieee->state to IEEE80211_LINKED,
2552          * so, at worst, we waste a bit of time to initiate an unneeded syncro
2553          * scan, that will stop at the first round because it sees the state
2554          * associated.
2555          */
2556         if (ieee->state == IEEE80211_NOLINK)
2557                 ieee80211_start_scan_syncro(ieee);
2558
2559         /* the network definitively is not here.. create a new cell */
2560         if (ieee->state == IEEE80211_NOLINK){
2561                 printk("creating new IBSS cell\n");
2562                 if(!ieee->wap_set)
2563                         ieee80211_randomize_cell(ieee);
2564
2565                 if(ieee->modulation & IEEE80211_CCK_MODULATION){
2566
2567                         ieee->current_network.rates_len = 4;
2568
2569                         ieee->current_network.rates[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
2570                         ieee->current_network.rates[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
2571                         ieee->current_network.rates[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
2572                         ieee->current_network.rates[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
2573
2574                 }else
2575                         ieee->current_network.rates_len = 0;
2576
2577                 if(ieee->modulation & IEEE80211_OFDM_MODULATION){
2578                         ieee->current_network.rates_ex_len = 8;
2579
2580                         ieee->current_network.rates_ex[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
2581                         ieee->current_network.rates_ex[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
2582                         ieee->current_network.rates_ex[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
2583                         ieee->current_network.rates_ex[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
2584                         ieee->current_network.rates_ex[4] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
2585                         ieee->current_network.rates_ex[5] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
2586                         ieee->current_network.rates_ex[6] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
2587                         ieee->current_network.rates_ex[7] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
2588
2589                         ieee->rate = 540;
2590                 }else{
2591                         ieee->current_network.rates_ex_len = 0;
2592                         ieee->rate = 110;
2593                 }
2594
2595                 // By default, WMM function will be disabled in IBSS mode
2596                 ieee->current_network.QoS_Enable = 0;
2597
2598                 ieee->current_network.atim_window = 0;
2599                 ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
2600                 if(ieee->short_slot)
2601                         ieee->current_network.capability |= WLAN_CAPABILITY_SHORT_SLOT;
2602
2603         }
2604
2605         ieee->state = IEEE80211_LINKED;
2606         ieee->set_chan(ieee->dev, ieee->current_network.channel);
2607         ieee->link_change(ieee->dev);
2608
2609         notify_wx_assoc_event(ieee);
2610
2611         ieee80211_start_send_beacons(ieee);
2612         printk(KERN_WARNING "after sending beacon packet!\n");
2613
2614         if (ieee->data_hard_resume)
2615                 ieee->data_hard_resume(ieee->dev);
2616
2617         netif_carrier_on(ieee->dev);
2618
2619         up(&ieee->wx_sem);
2620 }
2621 inline void ieee80211_start_ibss(struct ieee80211_device *ieee)
2622 {
2623         queue_delayed_work(ieee->wq, &ieee->start_ibss_wq, 100);
2624 }
2625
2626 /* this is called only in user context, with wx_sem held */
2627 void ieee80211_start_bss(struct ieee80211_device *ieee)
2628 {
2629         unsigned long flags;
2630 #ifdef ENABLE_DOT11D
2631         //
2632         // Ref: 802.11d 11.1.3.3
2633         // STA shall not start a BSS unless properly formed Beacon frame including a Country IE.
2634         //
2635         if(IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee))
2636         {
2637                 if(! ieee->bGlobalDomain)
2638                 {
2639                         return;
2640                 }
2641         }
2642 #endif
2643         /* check if we have already found the net we
2644          * are interested in (if any).
2645          * if not (we are disassociated and we are not
2646          * in associating / authenticating phase) start the background scanning.
2647          */
2648         ieee80211_softmac_check_all_nets(ieee);
2649
2650         /* ensure no-one start an associating process (thus setting
2651          * the ieee->state to ieee80211_ASSOCIATING) while we
2652          * have just cheked it and we are going to enable scan.
2653          * The ieee80211_new_net function is always called with
2654          * lock held (from both ieee80211_softmac_check_all_nets and
2655          * the rx path), so we cannot be in the middle of such function
2656          */
2657         spin_lock_irqsave(&ieee->lock, flags);
2658
2659 //#ifdef ENABLE_IPS
2660 //      printk("start bss ENABLE_IPS\n");
2661 //#else
2662         if (ieee->state == IEEE80211_NOLINK){
2663                 ieee->actscanning = true;
2664                 ieee80211_start_scan(ieee);
2665         }
2666 //#endif
2667         spin_unlock_irqrestore(&ieee->lock, flags);
2668 }
2669
2670 /* called only in userspace context */
2671 void ieee80211_disassociate(struct ieee80211_device *ieee)
2672 {
2673         netif_carrier_off(ieee->dev);
2674
2675         if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2676                         ieee80211_reset_queue(ieee);
2677
2678         if (ieee->data_hard_stop)
2679                         ieee->data_hard_stop(ieee->dev);
2680
2681 #ifdef ENABLE_DOT11D
2682         if(IS_DOT11D_ENABLE(ieee))
2683                 Dot11d_Reset(ieee);
2684 #endif
2685         ieee->state = IEEE80211_NOLINK;
2686         ieee->link_change(ieee->dev);
2687         notify_wx_assoc_event(ieee);
2688
2689 }
2690 void ieee80211_associate_retry_wq(struct work_struct *work)
2691 {
2692         struct delayed_work *dwork = to_delayed_work(work);
2693         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, associate_retry_wq);
2694         unsigned long flags;
2695         down(&ieee->wx_sem);
2696         if(!ieee->proto_started)
2697                 goto exit;
2698         if(ieee->state != IEEE80211_ASSOCIATING_RETRY)
2699                 goto exit;
2700         /* until we do not set the state to IEEE80211_NOLINK
2701         * there are no possibility to have someone else trying
2702         * to start an association procdure (we get here with
2703         * ieee->state = IEEE80211_ASSOCIATING).
2704         * When we set the state to IEEE80211_NOLINK it is possible
2705         * that the RX path run an attempt to associate, but
2706         * both ieee80211_softmac_check_all_nets and the
2707         * RX path works with ieee->lock held so there are no
2708         * problems. If we are still disassociated then start a scan.
2709         * the lock here is necessary to ensure no one try to start
2710         * an association procedure when we have just checked the
2711         * state and we are going to start the scan.
2712         */
2713         ieee->state = IEEE80211_NOLINK;
2714         ieee->beinretry = true;
2715         ieee80211_softmac_check_all_nets(ieee);
2716
2717         spin_lock_irqsave(&ieee->lock, flags);
2718
2719         if(ieee->state == IEEE80211_NOLINK){
2720                 ieee->beinretry = false;
2721                 ieee->actscanning = true;
2722                 ieee80211_start_scan(ieee);
2723         }
2724         //YJ,add,080828, notify os here
2725         if(ieee->state == IEEE80211_NOLINK)
2726         {
2727                 notify_wx_assoc_event(ieee);
2728         }
2729         //YJ,add,080828,end
2730         spin_unlock_irqrestore(&ieee->lock, flags);
2731
2732 exit:
2733         up(&ieee->wx_sem);
2734 }
2735
2736 struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee)
2737 {
2738         u8 broadcast_addr[] = {0xff,0xff,0xff,0xff,0xff,0xff};
2739
2740         struct sk_buff *skb = NULL;
2741         struct ieee80211_probe_response *b;
2742
2743         skb = ieee80211_probe_resp(ieee, broadcast_addr);
2744         if (!skb)
2745                 return NULL;
2746
2747         b = (struct ieee80211_probe_response *) skb->data;
2748         b->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_BEACON);
2749
2750         return skb;
2751
2752 }
2753
2754 struct sk_buff *ieee80211_get_beacon(struct ieee80211_device *ieee)
2755 {
2756         struct sk_buff *skb;
2757         struct ieee80211_probe_response *b;
2758
2759         skb = ieee80211_get_beacon_(ieee);
2760         if(!skb)
2761                 return NULL;
2762
2763         b = (struct ieee80211_probe_response *) skb->data;
2764         b->header.seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2765
2766         if (ieee->seq_ctrl[0] == 0xFFF)
2767                 ieee->seq_ctrl[0] = 0;
2768         else
2769                 ieee->seq_ctrl[0]++;
2770
2771         return skb;
2772 }
2773
2774 void ieee80211_softmac_stop_protocol(struct ieee80211_device *ieee)
2775 {
2776         ieee->sync_scan_hurryup = 1;
2777         down(&ieee->wx_sem);
2778         ieee80211_stop_protocol(ieee);
2779         up(&ieee->wx_sem);
2780 }
2781
2782
2783 void ieee80211_stop_protocol(struct ieee80211_device *ieee)
2784 {
2785         if (!ieee->proto_started)
2786                 return;
2787
2788         ieee->proto_started = 0;
2789
2790         ieee80211_stop_send_beacons(ieee);
2791         if((ieee->iw_mode == IW_MODE_INFRA)&&(ieee->state == IEEE80211_LINKED)) {
2792                 SendDisassociation(ieee,NULL,WLAN_REASON_DISASSOC_STA_HAS_LEFT);
2793         }
2794         del_timer_sync(&ieee->associate_timer);
2795         cancel_delayed_work(&ieee->associate_retry_wq);
2796         cancel_delayed_work(&ieee->start_ibss_wq);
2797         ieee80211_stop_scan(ieee);
2798
2799         ieee80211_disassociate(ieee);
2800 }
2801
2802 void ieee80211_softmac_start_protocol(struct ieee80211_device *ieee)
2803 {
2804         ieee->sync_scan_hurryup = 0;
2805         down(&ieee->wx_sem);
2806         ieee80211_start_protocol(ieee);
2807         up(&ieee->wx_sem);
2808 }
2809
2810 void ieee80211_start_protocol(struct ieee80211_device *ieee)
2811 {
2812         short ch = 0;
2813         int i = 0;
2814
2815         if (ieee->proto_started)
2816                 return;
2817
2818         ieee->proto_started = 1;
2819
2820         if (ieee->current_network.channel == 0){
2821                 do{
2822                         ch++;
2823                         if (ch > MAX_CHANNEL_NUMBER)
2824                                 return; /* no channel found */
2825
2826 #ifdef ENABLE_DOT11D
2827                 }while(!GET_DOT11D_INFO(ieee)->channel_map[ch]);
2828 #else
2829                 }while(!ieee->channel_map[ch]);
2830 #endif
2831
2832                 ieee->current_network.channel = ch;
2833         }
2834
2835         if (ieee->current_network.beacon_interval == 0)
2836                 ieee->current_network.beacon_interval = 100;
2837         ieee->set_chan(ieee->dev,ieee->current_network.channel);
2838
2839         for(i = 0; i < 17; i++) {
2840           ieee->last_rxseq_num[i] = -1;
2841           ieee->last_rxfrag_num[i] = -1;
2842           ieee->last_packet_time[i] = 0;
2843         }
2844
2845         ieee->init_wmmparam_flag = 0;//reinitialize AC_xx_PARAM registers.
2846
2847
2848         /* if the user set the MAC of the ad-hoc cell and then
2849          * switch to managed mode, shall we  make sure that association
2850          * attempts does not fail just because the user provide the essid
2851          * and the nic is still checking for the AP MAC ??
2852          */
2853         switch (ieee->iw_mode) {
2854                 case IW_MODE_AUTO:
2855                         ieee->iw_mode = IW_MODE_INFRA;
2856                         //not set break here intentionly
2857                 case IW_MODE_INFRA:
2858                         ieee80211_start_bss(ieee);
2859                         break;
2860
2861                 case IW_MODE_ADHOC:
2862                         ieee80211_start_ibss(ieee);
2863                         break;
2864
2865                 case IW_MODE_MASTER:
2866                         ieee80211_start_master_bss(ieee);
2867                 break;
2868
2869                 case IW_MODE_MONITOR:
2870                         ieee80211_start_monitor_mode(ieee);
2871                         break;
2872
2873                 default:
2874                         ieee->iw_mode = IW_MODE_INFRA;
2875                         ieee80211_start_bss(ieee);
2876                         break;
2877         }
2878 }
2879
2880
2881 #define DRV_NAME  "Ieee80211"
2882 void ieee80211_softmac_init(struct ieee80211_device *ieee)
2883 {
2884         int i;
2885         memset(&ieee->current_network, 0, sizeof(struct ieee80211_network));
2886
2887         ieee->state = IEEE80211_NOLINK;
2888         ieee->sync_scan_hurryup = 0;
2889         for(i = 0; i < 5; i++) {
2890           ieee->seq_ctrl[i] = 0;
2891         }
2892
2893         ieee->assoc_id = 0;
2894         ieee->queue_stop = 0;
2895         ieee->scanning = 0;
2896         ieee->softmac_features = 0; //so IEEE2100-like driver are happy
2897         ieee->wap_set = 0;
2898         ieee->ssid_set = 0;
2899         ieee->proto_started = 0;
2900         ieee->basic_rate = IEEE80211_DEFAULT_BASIC_RATE;
2901         ieee->rate = 3;
2902 //#ifdef ENABLE_LPS
2903         ieee->ps = IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST;
2904 //#else
2905 //      ieee->ps = IEEE80211_PS_DISABLED;
2906 //#endif
2907         ieee->sta_sleep = 0;
2908 //by amy
2909         ieee->bInactivePs = false;
2910         ieee->actscanning = false;
2911         ieee->ListenInterval = 2;
2912         ieee->NumRxDataInPeriod = 0; //YJ,add,080828
2913         ieee->NumRxBcnInPeriod = 0; //YJ,add,080828
2914         ieee->NumRxOkTotal = 0;//+by amy 080312
2915         ieee->NumRxUnicast = 0;//YJ,add,080828,for keep alive
2916         ieee->beinretry = false;
2917         ieee->bHwRadioOff = false;
2918 //by amy
2919
2920         init_mgmt_queue(ieee);
2921 #if 0
2922         init_timer(&ieee->scan_timer);
2923         ieee->scan_timer.data = (unsigned long)ieee;
2924         ieee->scan_timer.function = ieee80211_softmac_scan_cb;
2925 #endif
2926         ieee->tx_pending.txb = NULL;
2927
2928         init_timer(&ieee->associate_timer);
2929         ieee->associate_timer.data = (unsigned long)ieee;
2930         ieee->associate_timer.function = ieee80211_associate_abort_cb;
2931
2932         init_timer(&ieee->beacon_timer);
2933         ieee->beacon_timer.data = (unsigned long) ieee;
2934         ieee->beacon_timer.function = ieee80211_send_beacon_cb;
2935
2936 #ifdef PF_SYNCTHREAD
2937         ieee->wq = create_workqueue(DRV_NAME,0);
2938 #else
2939         ieee->wq = create_workqueue(DRV_NAME);
2940 #endif
2941         INIT_DELAYED_WORK(&ieee->start_ibss_wq,(void*) ieee80211_start_ibss_wq);
2942         INIT_WORK(&ieee->associate_complete_wq,(void*) ieee80211_associate_complete_wq);
2943         INIT_WORK(&ieee->associate_procedure_wq,(void*) ieee80211_associate_procedure_wq);
2944         INIT_DELAYED_WORK(&ieee->softmac_scan_wq,(void*) ieee80211_softmac_scan_wq);
2945         INIT_DELAYED_WORK(&ieee->associate_retry_wq,(void*) ieee80211_associate_retry_wq);
2946         INIT_WORK(&ieee->wx_sync_scan_wq,(void*) ieee80211_wx_sync_scan_wq);
2947 //      INIT_WORK(&ieee->watch_dog_wq,(void*) ieee80211_watch_dog_wq);
2948
2949         sema_init(&ieee->wx_sem, 1);
2950         sema_init(&ieee->scan_sem, 1);
2951
2952         spin_lock_init(&ieee->mgmt_tx_lock);
2953         spin_lock_init(&ieee->beacon_lock);
2954
2955         tasklet_init(&ieee->ps_task,
2956              (void(*)(unsigned long)) ieee80211_sta_ps,
2957              (unsigned long)ieee);
2958 #ifdef ENABLE_DOT11D
2959         ieee->pDot11dInfo = kmalloc(sizeof(RT_DOT11D_INFO), GFP_ATOMIC);
2960 #endif
2961 }
2962
2963 void ieee80211_softmac_free(struct ieee80211_device *ieee)
2964 {
2965         down(&ieee->wx_sem);
2966
2967         del_timer_sync(&ieee->associate_timer);
2968         cancel_delayed_work(&ieee->associate_retry_wq);
2969
2970
2971         //add for RF power on power of by lizhaoming 080512
2972         cancel_delayed_work(&ieee->GPIOChangeRFWorkItem);
2973
2974         destroy_workqueue(ieee->wq);
2975 #ifdef ENABLE_DOT11D
2976         if(NULL != ieee->pDot11dInfo)
2977                 kfree(ieee->pDot11dInfo);
2978 #endif
2979         up(&ieee->wx_sem);
2980 }
2981
2982 /********************************************************
2983  * Start of WPA code.                                   *
2984  * this is stolen from the ipw2200 driver               *
2985  ********************************************************/
2986
2987
2988 static int ieee80211_wpa_enable(struct ieee80211_device *ieee, int value)
2989 {
2990         /* This is called when wpa_supplicant loads and closes the driver
2991          * interface. */
2992         printk("%s WPA\n",value ? "enabling" : "disabling");
2993         ieee->wpa_enabled = value;
2994         return 0;
2995 }
2996
2997
2998 void ieee80211_wpa_assoc_frame(struct ieee80211_device *ieee, char *wpa_ie, int wpa_ie_len)
2999 {
3000         /* make sure WPA is enabled */
3001         ieee80211_wpa_enable(ieee, 1);
3002
3003         ieee80211_disassociate(ieee);
3004 }
3005
3006
3007 static int ieee80211_wpa_mlme(struct ieee80211_device *ieee, int command, int reason)
3008 {
3009
3010         int ret = 0;
3011
3012         switch (command) {
3013         case IEEE_MLME_STA_DEAUTH:
3014                 // silently ignore
3015                 break;
3016
3017         case IEEE_MLME_STA_DISASSOC:
3018                 ieee80211_disassociate(ieee);
3019                 break;
3020
3021         default:
3022                 printk("Unknown MLME request: %d\n", command);
3023                 ret = -EOPNOTSUPP;
3024         }
3025
3026         return ret;
3027 }
3028
3029
3030 static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee,
3031                               struct ieee_param *param, int plen)
3032 {
3033         u8 *buf;
3034
3035         if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
3036             (param->u.wpa_ie.len && param->u.wpa_ie.data == NULL))
3037                 return -EINVAL;
3038
3039         if (param->u.wpa_ie.len) {
3040                 buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL);
3041                 if (buf == NULL)
3042                         return -ENOMEM;
3043
3044                 memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
3045                 kfree(ieee->wpa_ie);
3046                 ieee->wpa_ie = buf;
3047                 ieee->wpa_ie_len = param->u.wpa_ie.len;
3048         } else {
3049                 kfree(ieee->wpa_ie);
3050                 ieee->wpa_ie = NULL;
3051                 ieee->wpa_ie_len = 0;
3052         }
3053
3054         ieee80211_wpa_assoc_frame(ieee, ieee->wpa_ie, ieee->wpa_ie_len);
3055         return 0;
3056 }
3057
3058 #define AUTH_ALG_OPEN_SYSTEM                    0x1
3059 #define AUTH_ALG_SHARED_KEY                     0x2
3060
3061 static int ieee80211_wpa_set_auth_algs(struct ieee80211_device *ieee, int value)
3062 {
3063
3064         struct ieee80211_security sec = {
3065                 .flags = SEC_AUTH_MODE,
3066         };
3067         int ret = 0;
3068
3069         if (value & AUTH_ALG_SHARED_KEY) {
3070                 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
3071                 ieee->open_wep = 0;
3072         } else {
3073                 sec.auth_mode = WLAN_AUTH_OPEN;
3074                 ieee->open_wep = 1;
3075         }
3076
3077         if (ieee->set_security)
3078                 ieee->set_security(ieee->dev, &sec);
3079         else
3080                 ret = -EOPNOTSUPP;
3081
3082         return ret;
3083 }
3084
3085 static int ieee80211_wpa_set_param(struct ieee80211_device *ieee, u8 name, u32 value)
3086 {
3087         int ret=0;
3088         unsigned long flags;
3089
3090         switch (name) {
3091         case IEEE_PARAM_WPA_ENABLED:
3092                 ret = ieee80211_wpa_enable(ieee, value);
3093                 break;
3094
3095         case IEEE_PARAM_TKIP_COUNTERMEASURES:
3096                 ieee->tkip_countermeasures=value;
3097                 break;
3098
3099         case IEEE_PARAM_DROP_UNENCRYPTED: {
3100                 /* HACK:
3101                  *
3102                  * wpa_supplicant calls set_wpa_enabled when the driver
3103                  * is loaded and unloaded, regardless of if WPA is being
3104                  * used.  No other calls are made which can be used to
3105                  * determine if encryption will be used or not prior to
3106                  * association being expected.  If encryption is not being
3107                  * used, drop_unencrypted is set to false, else true -- we
3108                  * can use this to determine if the CAP_PRIVACY_ON bit should
3109                  * be set.
3110                  */
3111                 struct ieee80211_security sec = {
3112                         .flags = SEC_ENABLED,
3113                         .enabled = value,
3114                 };
3115                 ieee->drop_unencrypted = value;
3116                 /* We only change SEC_LEVEL for open mode. Others
3117                  * are set by ipw_wpa_set_encryption.
3118                  */
3119                 if (!value) {
3120                         sec.flags |= SEC_LEVEL;
3121                         sec.level = SEC_LEVEL_0;
3122                 }
3123                 else {
3124                         sec.flags |= SEC_LEVEL;
3125                         sec.level = SEC_LEVEL_1;
3126                 }
3127                 if (ieee->set_security)
3128                         ieee->set_security(ieee->dev, &sec);
3129                 break;
3130         }
3131
3132         case IEEE_PARAM_PRIVACY_INVOKED:
3133                 ieee->privacy_invoked=value;
3134                 break;
3135
3136         case IEEE_PARAM_AUTH_ALGS:
3137                 ret = ieee80211_wpa_set_auth_algs(ieee, value);
3138                 break;
3139
3140         case IEEE_PARAM_IEEE_802_1X:
3141                 ieee->ieee802_1x=value;
3142                 break;
3143         case IEEE_PARAM_WPAX_SELECT:
3144                 // added for WPA2 mixed mode
3145                 //printk(KERN_WARNING "------------------------>wpax value = %x\n", value);
3146                 spin_lock_irqsave(&ieee->wpax_suitlist_lock,flags);
3147                 ieee->wpax_type_set = 1;
3148                 ieee->wpax_type_notify = value;
3149                 spin_unlock_irqrestore(&ieee->wpax_suitlist_lock,flags);
3150                 break;
3151
3152         default:
3153                 printk("Unknown WPA param: %d\n",name);
3154                 ret = -EOPNOTSUPP;
3155         }
3156
3157         return ret;
3158 }
3159
3160 /* implementation borrowed from hostap driver */
3161
3162 static int ieee80211_wpa_set_encryption(struct ieee80211_device *ieee,
3163                                   struct ieee_param *param, int param_len)
3164 {
3165         int ret = 0;
3166
3167         struct ieee80211_crypto_ops *ops;
3168         struct ieee80211_crypt_data **crypt;
3169
3170         struct ieee80211_security sec = {
3171                 .flags = 0,
3172         };
3173
3174         param->u.crypt.err = 0;
3175         param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
3176
3177         if (param_len !=
3178             (int) ((char *) param->u.crypt.key - (char *) param) +
3179             param->u.crypt.key_len) {
3180                 printk("Len mismatch %d, %d\n", param_len,
3181                                param->u.crypt.key_len);
3182                 return -EINVAL;
3183         }
3184         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
3185             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
3186             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
3187                 if (param->u.crypt.idx >= WEP_KEYS)
3188                         return -EINVAL;
3189                 crypt = &ieee->crypt[param->u.crypt.idx];
3190         } else {
3191                 return -EINVAL;
3192         }
3193
3194         if (strcmp(param->u.crypt.alg, "none") == 0) {
3195                 if (crypt) {
3196                         sec.enabled = 0;
3197                         // FIXME FIXME
3198                         //sec.encrypt = 0;
3199                         sec.level = SEC_LEVEL_0;
3200                         sec.flags |= SEC_ENABLED | SEC_LEVEL;
3201                         ieee80211_crypt_delayed_deinit(ieee, crypt);
3202                 }
3203                 goto done;
3204         }
3205         sec.enabled = 1;
3206 // FIXME FIXME
3207 //      sec.encrypt = 1;
3208         sec.flags |= SEC_ENABLED;
3209
3210         /* IPW HW cannot build TKIP MIC, host decryption still needed. */
3211         if (!(ieee->host_encrypt || ieee->host_decrypt) &&
3212             strcmp(param->u.crypt.alg, "TKIP"))
3213                 goto skip_host_crypt;
3214
3215         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3216         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
3217                 request_module("ieee80211_crypt_wep");
3218                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3219         } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
3220                 request_module("ieee80211_crypt_tkip");
3221                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3222         } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
3223                 request_module("ieee80211_crypt_ccmp");
3224                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3225         }
3226         if (ops == NULL) {
3227                 printk("unknown crypto alg '%s'\n", param->u.crypt.alg);
3228                 param->u.crypt.err = IEEE_CRYPT_ERR_UNKNOWN_ALG;
3229                 ret = -EINVAL;
3230                 goto done;
3231         }
3232
3233         if (*crypt == NULL || (*crypt)->ops != ops) {
3234                 struct ieee80211_crypt_data *new_crypt;
3235
3236                 ieee80211_crypt_delayed_deinit(ieee, crypt);
3237
3238                 new_crypt = (struct ieee80211_crypt_data *)
3239                         kmalloc(sizeof(*new_crypt), GFP_KERNEL);
3240                 if (new_crypt == NULL) {
3241                         ret = -ENOMEM;
3242                         goto done;
3243                 }
3244                 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
3245                 new_crypt->ops = ops;
3246                 if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
3247                         new_crypt->priv =
3248                                 new_crypt->ops->init(param->u.crypt.idx);
3249
3250                 if (new_crypt->priv == NULL) {
3251                         kfree(new_crypt);
3252                         param->u.crypt.err = IEEE_CRYPT_ERR_CRYPT_INIT_FAILED;
3253                         ret = -EINVAL;
3254                         goto done;
3255                 }
3256
3257                 *crypt = new_crypt;
3258         }
3259
3260         if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
3261             (*crypt)->ops->set_key(param->u.crypt.key,
3262                                    param->u.crypt.key_len, param->u.crypt.seq,
3263                                    (*crypt)->priv) < 0) {
3264                 printk("key setting failed\n");
3265                 param->u.crypt.err = IEEE_CRYPT_ERR_KEY_SET_FAILED;
3266                 ret = -EINVAL;
3267                 goto done;
3268         }
3269
3270  skip_host_crypt:
3271         if (param->u.crypt.set_tx) {
3272                 ieee->tx_keyidx = param->u.crypt.idx;
3273                 sec.active_key = param->u.crypt.idx;
3274                 sec.flags |= SEC_ACTIVE_KEY;
3275         } else
3276                 sec.flags &= ~SEC_ACTIVE_KEY;
3277
3278         if (param->u.crypt.alg != NULL) {
3279                 memcpy(sec.keys[param->u.crypt.idx],
3280                        param->u.crypt.key,
3281                        param->u.crypt.key_len);
3282                 sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
3283                 sec.flags |= (1 << param->u.crypt.idx);
3284
3285                 if (strcmp(param->u.crypt.alg, "WEP") == 0) {
3286                         sec.flags |= SEC_LEVEL;
3287                         sec.level = SEC_LEVEL_1;
3288                 } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
3289                         sec.flags |= SEC_LEVEL;
3290                         sec.level = SEC_LEVEL_2;
3291                 } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
3292                         sec.flags |= SEC_LEVEL;
3293                         sec.level = SEC_LEVEL_3;
3294                 }
3295         }
3296  done:
3297         if (ieee->set_security)
3298                 ieee->set_security(ieee->dev, &sec);
3299
3300         /* Do not reset port if card is in Managed mode since resetting will
3301          * generate new IEEE 802.11 authentication which may end up in looping
3302          * with IEEE 802.1X.  If your hardware requires a reset after WEP
3303          * configuration (for example... Prism2), implement the reset_port in
3304          * the callbacks structures used to initialize the 802.11 stack. */
3305         if (ieee->reset_on_keychange &&
3306             ieee->iw_mode != IW_MODE_INFRA &&
3307             ieee->reset_port &&
3308             ieee->reset_port(ieee->dev)) {
3309                 printk("reset_port failed\n");
3310                 param->u.crypt.err = IEEE_CRYPT_ERR_CARD_CONF_FAILED;
3311                 return -EINVAL;
3312         }
3313
3314         return ret;
3315 }
3316
3317 int ieee80211_wpa_supplicant_ioctl(struct ieee80211_device *ieee, struct iw_point *p)
3318 {
3319         struct ieee_param *param;
3320         int ret=0;
3321
3322         down(&ieee->wx_sem);
3323         //IEEE_DEBUG_INFO("wpa_supplicant: len=%d\n", p->length);
3324
3325         if (p->length < sizeof(struct ieee_param) || !p->pointer){
3326                 ret = -EINVAL;
3327                 goto out;
3328         }
3329
3330         param = (struct ieee_param *)kmalloc(p->length, GFP_KERNEL);
3331         if (param == NULL){
3332                 ret = -ENOMEM;
3333                 goto out;
3334         }
3335         if (copy_from_user(param, p->pointer, p->length)) {
3336                 kfree(param);
3337                 ret = -EFAULT;
3338                 goto out;
3339         }
3340
3341         switch (param->cmd) {
3342
3343         case IEEE_CMD_SET_WPA_PARAM:
3344                 ret = ieee80211_wpa_set_param(ieee, param->u.wpa_param.name,
3345                                         param->u.wpa_param.value);
3346                 break;
3347
3348         case IEEE_CMD_SET_WPA_IE:
3349                 ret = ieee80211_wpa_set_wpa_ie(ieee, param, p->length);
3350                 break;
3351
3352         case IEEE_CMD_SET_ENCRYPTION:
3353                 ret = ieee80211_wpa_set_encryption(ieee, param, p->length);
3354                 break;
3355
3356         case IEEE_CMD_MLME:
3357                 ret = ieee80211_wpa_mlme(ieee, param->u.mlme.command,
3358                                    param->u.mlme.reason_code);
3359                 break;
3360
3361         default:
3362                 printk("Unknown WPA supplicant request: %d\n",param->cmd);
3363                 ret = -EOPNOTSUPP;
3364                 break;
3365         }
3366
3367         if (ret == 0 && copy_to_user(p->pointer, param, p->length))
3368                 ret = -EFAULT;
3369
3370         kfree(param);
3371 out:
3372         up(&ieee->wx_sem);
3373
3374         return ret;
3375 }
3376
3377 void notify_wx_assoc_event(struct ieee80211_device *ieee)
3378 {
3379         union iwreq_data wrqu;
3380         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3381         if (ieee->state == IEEE80211_LINKED)
3382                 memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid, ETH_ALEN);
3383         else
3384                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
3385         wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
3386 }
3387
3388
3389 #if 0
3390 EXPORT_SYMBOL(ieee80211_get_beacon);
3391 EXPORT_SYMBOL(ieee80211_wake_queue);
3392 EXPORT_SYMBOL(ieee80211_stop_queue);
3393 EXPORT_SYMBOL(ieee80211_reset_queue);
3394 EXPORT_SYMBOL(ieee80211_softmac_stop_protocol);
3395 EXPORT_SYMBOL(ieee80211_softmac_start_protocol);
3396 EXPORT_SYMBOL(ieee80211_is_shortslot);
3397 EXPORT_SYMBOL(ieee80211_is_54g);
3398 EXPORT_SYMBOL(ieee80211_wpa_supplicant_ioctl);
3399 EXPORT_SYMBOL(ieee80211_ps_tx_ack);
3400 EXPORT_SYMBOL(ieee80211_start_protocol);
3401 EXPORT_SYMBOL(ieee80211_stop_protocol);
3402 EXPORT_SYMBOL(notify_wx_assoc_event);
3403 EXPORT_SYMBOL(ieee80211_stop_send_beacons);
3404 EXPORT_SYMBOL(SendDisassociation);
3405 EXPORT_SYMBOL(ieee80211_disassociate);
3406 EXPORT_SYMBOL(ieee80211_start_scan);
3407 EXPORT_SYMBOL(ieee80211_softmac_ips_scan_syncro);
3408 EXPORT_SYMBOL(ieee80211_sta_ps_send_null_frame);
3409 #endif