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