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